From 3e0e2b301cd809a2b61fa398fe7076d0b4446330 Mon Sep 17 00:00:00 2001 From: milanmajchrak <90026355+milanmajchrak@users.noreply.github.com> Date: Mon, 2 Nov 2026 10:16:24 +0100 Subject: [PATCH] LINDAT/Backport ufal: send only the fields the licence asks for on download * fix(license): ensure IP address is included in anonymous user payload (ufal/dspace-angular PR 163) * fix(license): ensure IP address is included in anonymous user payload * test(license): add missing template dependencies to spec (cherry picked from commit e183ad854f8bab39edd91e94f0d92983055ea121) * issue 162: filter clarinusermetadata/manage payload to contain only required metadata keys (ufal/dspace-angular PR 164) * issue 162: filter clarinusermetadata/manage payload to contain only fields required by the license * test(license): cover required-info payload filter and optional ORGANIZATION Spec cases for ufal/dspace-angular PR 164 on top of the ufal/dspace-angular PR 163 spec setup: - only metadata keys required by the license (plus the current IP) are sent - an empty ORGANIZATION does not block "I agree"; an empty REQUIRED_ORGANIZATION still does Both cases fail without ufal/dspace-angular PR 164's component change. --------- (cherry picked from commit a026caf97a2bd23c9a75836fdda269a93d66c498) --------- (cherry picked from commit 33013a0d17627d9cb71c1e55f6689550c0563135) Co-authored-by: Ondrej Kosarko Co-authored-by: Amad Ul Hassan Co-authored-by: Milan Kuchtiak Co-Authored-By: Claude Fable 5.2 --- ...n-license-agreement-page.component.spec.ts | 200 ++++++++++++++++-- ...clarin-license-agreement-page.component.ts | 30 ++- 2 files changed, 208 insertions(+), 22 deletions(-) diff --git a/src/app/bitstream-page/clarin-license-agreement-page/clarin-license-agreement-page.component.spec.ts b/src/app/bitstream-page/clarin-license-agreement-page/clarin-license-agreement-page.component.spec.ts index b0ce6171bc4..3df8399df5e 100644 --- a/src/app/bitstream-page/clarin-license-agreement-page/clarin-license-agreement-page.component.spec.ts +++ b/src/app/bitstream-page/clarin-license-agreement-page/clarin-license-agreement-page.component.spec.ts @@ -1,26 +1,196 @@ -import { TestBed } from '@angular/core/testing'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { Pipe, PipeTransform } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { HttpClient } from '@angular/common/http'; +import { Router } from '@angular/router'; +import { TranslateModule } from '@ngx-translate/core'; +import { of as observableOf } from 'rxjs'; import { ClarinLicenseAgreementPageComponent } from './clarin-license-agreement-page.component'; +import { AuthService } from '../../core/auth/auth.service'; +import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-build.service'; +import { BundleDataService } from '../../core/data/bundle-data.service'; +import { ClarinLicenseResourceMappingService } from '../../core/data/clarin/clarin-license-resource-mapping-data.service'; +import { ClarinUserMetadataDataService } from '../../core/data/clarin/clarin-user-metadata.service'; +import { ClarinUserRegistrationDataService } from '../../core/data/clarin/clarin-user-registration.service'; +import { ConfigurationDataService } from '../../core/data/configuration-data.service'; +import { ItemDataService } from '../../core/data/item-data.service'; +import { buildPaginatedList } from '../../core/data/paginated-list.model'; +import { RequestService } from '../../core/data/request.service'; +import { HardRedirectService } from '../../core/services/hard-redirect.service'; +import { Bitstream } from '../../core/shared/bitstream.model'; +import { ClarinLicenseRequiredInfo } from '../../core/shared/clarin/clarin-license.resource-type'; +import { ClarinUserMetadata } from '../../core/shared/clarin/clarin-user-metadata.model'; +import { Item } from '../../core/shared/item.model'; +import { FileService } from '../../core/shared/file.service'; +import { HALEndpointService } from '../../core/shared/hal-endpoint.service'; +import { getMockRemoteDataBuildService } from '../../shared/mocks/remote-data-build.service.mock'; +import { getMockRequestService } from '../../shared/mocks/request.service.mock'; +import { NotificationsService } from '../../shared/notifications/notifications.service'; +import { HtmlContentService } from '../../shared/html-content.service'; +import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub'; +import { VarDirective } from '../../shared/utils/var.directive'; + +@Pipe({ name: 'dsSafeHtml' }) +class MockDsSafeHtmlPipe implements PipeTransform { + transform(value: unknown): unknown { + return value; + } +} describe('ClarinLicenseAgreementPageComponent', () => { - // TODO uncomment and create tests - // let component: ClarinLicenseAgreementPageComponent; - // let fixture: ComponentFixture; + let component: ClarinLicenseAgreementPageComponent; + let fixture: ComponentFixture; + let requestService: jasmine.SpyObj; + let router: { routerState: { snapshot: { url: string } }; navigate: jasmine.Spy }; + + const restUrl = 'https://rest.api'; beforeEach(async () => { + requestService = getMockRequestService(); + router = { + routerState: { snapshot: { url: '/bitstreams/download' } }, + navigate: jasmine.createSpy('navigate'), + }; + await TestBed.configureTestingModule({ - declarations: [ ClarinLicenseAgreementPageComponent ] + declarations: [ ClarinLicenseAgreementPageComponent, VarDirective, MockDsSafeHtmlPipe ], + imports: [ CommonModule, TranslateModule.forRoot() ], + providers: [ + { provide: ClarinLicenseResourceMappingService, useValue: jasmine.createSpyObj('clarinLicenseResourceMappingService', ['searchBy']) }, + { provide: ConfigurationDataService, useValue: jasmine.createSpyObj('configurationDataService', ['findByPropertyName']) }, + { provide: BundleDataService, useValue: jasmine.createSpyObj('bundleService', ['findByItem']) }, + { provide: ClarinUserRegistrationDataService, useValue: jasmine.createSpyObj('userRegistrationService', ['searchBy']) }, + { provide: NotificationsService, useValue: new NotificationsServiceStub() }, + { provide: ItemDataService, useValue: jasmine.createSpyObj('itemService', ['searchBy']) }, + { provide: AuthService, useValue: jasmine.createSpyObj('authService', { + isAuthenticated: observableOf(false), + getAuthenticatedUserFromStore: observableOf(undefined), + }) }, + { provide: HttpClient, useValue: jasmine.createSpyObj('http', ['get']) }, + { provide: Router, useValue: router }, + { provide: HALEndpointService, useValue: jasmine.createSpyObj('halService', { getRootHref: restUrl }) }, + { provide: RemoteDataBuildService, useValue: getMockRemoteDataBuildService() }, + { provide: HardRedirectService, useValue: jasmine.createSpyObj('hardRedirectService', ['redirect']) }, + { provide: RequestService, useValue: requestService }, + { provide: ClarinUserMetadataDataService, useValue: jasmine.createSpyObj('clarinUserMetadataDataService', ['searchBy']) }, + { provide: HtmlContentService, useValue: jasmine.createSpyObj('htmlContentService', { + getHmtlContentByPathAndLocale: Promise.resolve(''), + }) }, + { provide: FileService, useValue: jasmine.createSpyObj('fileService', { + retrieveFileDownloadLink: observableOf('content-url'), + }) }, + ], }) .compileComponents(); }); - // TODO uncomment and create tests - // beforeEach(() => { - // fixture = TestBed.createComponent(ClarinLicenseAgreementPageComponent); - // component = fixture.componentInstance; - // fixture.detectChanges(); - // }); - // - // it('should create', () => { - // expect(component).toBeTruthy(); - // }); + beforeEach(() => { + fixture = TestBed.createComponent(ClarinLicenseAgreementPageComponent); + component = fixture.componentInstance; + component.bitstream$ = observableOf(Object.assign(new Bitstream(), { uuid: 'bitstream-uuid' })); + }); + + describe('accept', () => { + + // Anonymous user with no required extra fields: null userMetadata$ must still produce an IP entry in the POST body. + it('should send IP metadata in the request payload when userMetadata$ is null for an anonymous user', () => { + const testIpAddress = '203.0.113.42'; + const bitstream = Object.assign(new Bitstream(), { uuid: 'bitstream-uuid' }); + + component.currentUser$.next(null); + component.userMetadata$.next(null); + component.requiredInfo$.next([]); + component.ipAddress$.next(testIpAddress); + component.item$.next(Object.assign(new Item(), { uuid: 'item-uuid' })); + component.bitstream$ = observableOf(bitstream); + requestService.send.calls.reset(); + + component.accept(); + + expect(requestService.send).toHaveBeenCalled(); + const postRequest = requestService.send.calls.mostRecent().args[0]; + expect(postRequest.body).toEqual(jasmine.arrayContaining([ + jasmine.objectContaining({ + metadataKey: 'IP', + metadataValue: testIpAddress, + }), + ])); + const ipEntry = postRequest.body.find((entry) => entry.metadataKey === 'IP'); + expect(ipEntry.metadataValue).toBeTruthy(); + expect(component.userMetadata$.value).not.toBeNull(); + expect(component.userMetadata$.value.page).toEqual(jasmine.arrayContaining([ + jasmine.objectContaining({ + metadataKey: 'IP', + metadataValue: testIpAddress, + }), + ])); + }); + + // Same fix path when userMetadata$ exists but its page array is empty. + it('should send IP metadata in the request payload when userMetadata$.page is empty', () => { + const testIpAddress = '203.0.113.42'; + const bitstream = Object.assign(new Bitstream(), { uuid: 'bitstream-uuid' }); + + component.currentUser$.next(null); + component.userMetadata$.next(buildPaginatedList(undefined, [], false, undefined)); + component.requiredInfo$.next([]); + component.ipAddress$.next(testIpAddress); + component.item$.next(Object.assign(new Item(), { uuid: 'item-uuid' })); + component.bitstream$ = observableOf(bitstream); + requestService.send.calls.reset(); + + component.accept(); + + expect(requestService.send).toHaveBeenCalledWith(jasmine.objectContaining({ + body: jasmine.arrayContaining([ + jasmine.objectContaining({ + metadataKey: 'IP', + metadataValue: testIpAddress, + }), + ]), + })); + expect(component.userMetadata$.value).not.toBeNull(); + expect(component.userMetadata$.value.page.some((entry) => entry.metadataKey === 'IP')).toBeTrue(); + }); + + // Keys from a previous transaction that the license no longer requires must not be sent. + it('should send only the metadata keys required by the license, plus the current IP', () => { + const testIpAddress = '203.0.113.42'; + + component.userMetadata$.next(buildPaginatedList(undefined, [ + Object.assign(new ClarinUserMetadata(), { metadataKey: 'NAME', metadataValue: 'Jane Tester' }), + Object.assign(new ClarinUserMetadata(), { metadataKey: 'COUNTRY', metadataValue: 'CZ' }), + Object.assign(new ClarinUserMetadata(), { metadataKey: 'IP', metadataValue: '198.51.100.7' }), + ], false, undefined)); + component.requiredInfo$.next([Object.assign(new ClarinLicenseRequiredInfo(), { name: 'NAME' })]); + component.ipAddress$.next(testIpAddress); + component.item$.next(Object.assign(new Item(), { uuid: 'item-uuid' })); + requestService.send.calls.reset(); + + component.accept(); + + expect(requestService.send).toHaveBeenCalled(); + const body = requestService.send.calls.mostRecent().args[0].body; + expect(body.map((entry: ClarinUserMetadata) => entry.metadataKey)).toEqual(['NAME', 'IP']); + expect(body.find((entry: ClarinUserMetadata) => entry.metadataKey === 'IP').metadataValue).toEqual(testIpAddress); + }); + + // ORGANIZATION is optional and may stay empty; REQUIRED_ORGANIZATION is mandatory. + it('should allow an empty ORGANIZATION but block an empty REQUIRED_ORGANIZATION', () => { + component.userMetadata$.next(null); + component.ipAddress$.next('203.0.113.42'); + component.item$.next(Object.assign(new Item(), { uuid: 'item-uuid' })); + + component.requiredInfo$.next([Object.assign(new ClarinLicenseRequiredInfo(), { name: 'ORGANIZATION' })]); + requestService.send.calls.reset(); + component.accept(); + expect(requestService.send).toHaveBeenCalled(); + + component.requiredInfo$.next([Object.assign(new ClarinLicenseRequiredInfo(), { name: 'REQUIRED_ORGANIZATION' })]); + requestService.send.calls.reset(); + component.accept(); + expect(requestService.send).not.toHaveBeenCalled(); + }); + + }); }); diff --git a/src/app/bitstream-page/clarin-license-agreement-page/clarin-license-agreement-page.component.ts b/src/app/bitstream-page/clarin-license-agreement-page/clarin-license-agreement-page.component.ts index 38636514dcf..7dcee84f332 100644 --- a/src/app/bitstream-page/clarin-license-agreement-page/clarin-license-agreement-page.component.ts +++ b/src/app/bitstream-page/clarin-license-agreement-page/clarin-license-agreement-page.component.ts @@ -117,6 +117,11 @@ export class ClarinLicenseAgreementPageComponent implements OnInit { */ LICENSE_PATH_SEZNAM_CZ = 'szn-dataset-licence.html'; + /** + * The list of metadata keys that are optional and do not require user input. + */ + OPTIONAL_METADATA_KEYS = ['ORGANIZATION']; + /** * The content of the Seznam dataset license. Fetch from the static file. */ @@ -207,10 +212,16 @@ export class ClarinLicenseAgreementPageComponent implements OnInit { CLARIN_USER_METADATA_MANAGE; url += this.isDownloadingZIP() ? '/zip?itemUUID=' + this.item$.value.uuid : '?bitstreamUUID=' + this.getBitstreamUUID(); + if (isEmpty(this.userMetadata$.value) || isEmpty(this.userMetadata$.value.page)) { + this.userMetadata$.next(buildPaginatedList(undefined, [], false, undefined)); + } if (this.userMetadata$.value?.page) { // Filter the page array to exclude items with metadataKey "IP" + // and exclude items that are not in the requiredInfo list this.userMetadata$.value.page = - this.userMetadata$.value.page.filter(item => item.metadataKey !== 'IP'); + this.userMetadata$.value.page.filter(item => item.metadataKey !== 'IP' && + this.requiredInfo$?.value?.some(requiredInfo => + requiredInfo.name === item.metadataKey)); } // Add IP address into request. Every restricted download must have stored IP address in the `user_metadata` table. this.userMetadata$.value?.page.push(Object.assign(new ClarinUserMetadata(), { @@ -422,13 +433,18 @@ export class ClarinLicenseAgreementPageComponent implements OnInit { // Every requiredInfo.name === userMetadata.metadataKey must have the value in the userMetadata.metadataValue this.requiredInfo$?.value?.forEach(requiredInfo => { let hasMetadataValue = false; - this.userMetadata$?.value?.page?.forEach(userMetadata => { - if (userMetadata.metadataKey === requiredInfo.name) { - if (isNotEmpty(userMetadata.metadataValue)) { - hasMetadataValue = true; + // The OPTIONAL metadata keys don't require user input, so we consider them as filled in + if (this.OPTIONAL_METADATA_KEYS.includes(requiredInfo.name)) { + hasMetadataValue = true; + } else { + this.userMetadata$?.value?.page?.forEach(userMetadata => { + if (userMetadata.metadataKey === requiredInfo.name) { + if (isNotEmpty(userMetadata.metadataValue)) { + hasMetadataValue = true; + } } - } - }); + }); + } areFilledIn.push(hasMetadataValue); });