forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 10
fix(license): ensure IP address is included in anonymous user payload #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amadulhaxxani
wants to merge
2
commits into
clarin-v7
Choose a base branch
from
158-bad-request-payload-for-anonymous-user-confirming-the-license
base: clarin-v7
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
159 changes: 144 additions & 15 deletions
159
...stream-page/clarin-license-agreement-page/clarin-license-agreement-page.component.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,155 @@ | ||
| 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 { 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; | ||
| } | ||
| } | ||
|
|
||
|
amadulhaxxani marked this conversation as resolved.
|
||
| describe('ClarinLicenseAgreementPageComponent', () => { | ||
| // TODO uncomment and create tests | ||
| // let component: ClarinLicenseAgreementPageComponent; | ||
| // let fixture: ComponentFixture<ClarinLicenseAgreementPageComponent>; | ||
| let component: ClarinLicenseAgreementPageComponent; | ||
| let fixture: ComponentFixture<ClarinLicenseAgreementPageComponent>; | ||
| let requestService: jasmine.SpyObj<RequestService>; | ||
| 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(); | ||
| }); | ||
|
|
||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.