Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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 { FileService } from '../../core/shared/file.service';
import { HALEndpointService } from '../../core/shared/hal-endpoint.service';
Expand Down Expand Up @@ -170,6 +171,68 @@ describe('ClarinLicenseAgreementPageComponent', () => {
expect(hardRedirectService.redirect).toHaveBeenCalledWith('file-link?dtoken=download-token');
}));

/**
* The body of the request that stored the user metadata.
*/
function sentMetadata(): ClarinUserMetadata[] {
return requestService.send.calls.mostRecent().args[0].body;
}

function requiredInfo(...names: string[]): ClarinLicenseRequiredInfo[] {
return names.map(name => Object.assign(new ClarinLicenseRequiredInfo(), { name }));
}

it('should send the IP when an anonymous user has no user metadata', fakeAsync(() => {
component.userMetadata$.next(null);
component.requiredInfo$.next([]);

component.accept();
flushMicrotasks();

expect(sentMetadata().map(entry => [entry.metadataKey, entry.metadataValue])).toEqual([['IP', '127.0.0.1']]);
}));

it('should send the IP when the user metadata list is empty', fakeAsync(() => {
component.userMetadata$.next(userMetadata([]));
component.requiredInfo$.next([]);

component.accept();
flushMicrotasks();

expect(sentMetadata().map(entry => entry.metadataKey)).toEqual(['IP']);
}));

it('should send only the keys the licence requires, plus the current IP', fakeAsync(() => {
component.userMetadata$.next(userMetadata([
{ metadataKey: 'NAME', metadataValue: 'Jane Tester' },
{ metadataKey: 'COUNTRY', metadataValue: 'CZ' },
{ metadataKey: 'IP', metadataValue: '198.51.100.7' },
]));
component.requiredInfo$.next(requiredInfo('NAME'));

component.accept();
flushMicrotasks();

expect(sentMetadata().map(entry => [entry.metadataKey, entry.metadataValue]))
.toEqual([['NAME', 'Jane Tester'], ['IP', '127.0.0.1']]);
}));

it('should allow an empty ORGANIZATION but block an empty REQUIRED_ORGANIZATION', fakeAsync(() => {
component.userMetadata$.next(null);

component.requiredInfo$.next(requiredInfo('ORGANIZATION'));
component.accept();
flushMicrotasks();
expect(requestService.send).toHaveBeenCalled();

requestService.send.calls.reset();
component.userMetadata$.next(null);
component.requiredInfo$.next(requiredInfo('REQUIRED_ORGANIZATION'));
component.accept();
flushMicrotasks();
expect(requestService.send).not.toHaveBeenCalled();
}));

describe('when the download link goes by e-mail', () => {
let router;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,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.
*/
Expand Down Expand Up @@ -258,10 +263,16 @@ export class ClarinLicenseAgreementPageComponent implements OnInit {
if (!this.isDownloadingZIP() && isNotEmpty(this.accessToken)) {
url += '&accessToken=' + encodeURIComponent(this.accessToken);
}
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(), {
Expand Down Expand Up @@ -479,13 +490,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);
});

Expand Down
Loading