mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
Fix broken tests via a new HttpXsrfTokenExtractorMock class.
This commit is contained in:
@@ -21,6 +21,10 @@ import { UploaderService } from '../../shared/uploader/uploader.service';
|
|||||||
import { HostWindowService } from '../../shared/host-window.service';
|
import { HostWindowService } from '../../shared/host-window.service';
|
||||||
import { HostWindowServiceStub } from '../../shared/testing/host-window-service.stub';
|
import { HostWindowServiceStub } from '../../shared/testing/host-window-service.stub';
|
||||||
import { UploaderComponent } from '../../shared/uploader/uploader.component';
|
import { UploaderComponent } from '../../shared/uploader/uploader.component';
|
||||||
|
import { HttpXsrfTokenExtractor } from '@angular/common/http';
|
||||||
|
import { CookieService } from '../../core/services/cookie.service';
|
||||||
|
import { CookieServiceMock } from '../../shared/mocks/cookie.service.mock';
|
||||||
|
import { HttpXsrfTokenExtractorMock } from '../../shared/mocks/http-xsrf-token-extractor.mock';
|
||||||
|
|
||||||
describe('MyDSpaceNewSubmissionComponent test', () => {
|
describe('MyDSpaceNewSubmissionComponent test', () => {
|
||||||
|
|
||||||
@@ -55,6 +59,8 @@ describe('MyDSpaceNewSubmissionComponent test', () => {
|
|||||||
ChangeDetectorRef,
|
ChangeDetectorRef,
|
||||||
MyDSpaceNewSubmissionComponent,
|
MyDSpaceNewSubmissionComponent,
|
||||||
UploaderService,
|
UploaderService,
|
||||||
|
{ provide: HttpXsrfTokenExtractor, useValue: new HttpXsrfTokenExtractorMock('mock-token') },
|
||||||
|
{ provide: CookieService, useValue: new CookieServiceMock() },
|
||||||
{ provide: HostWindowService, useValue: new HostWindowServiceStub(800) },
|
{ provide: HostWindowService, useValue: new HostWindowServiceStub(800) },
|
||||||
],
|
],
|
||||||
schemas: [NO_ERRORS_SCHEMA]
|
schemas: [NO_ERRORS_SCHEMA]
|
||||||
|
@@ -1,32 +1,20 @@
|
|||||||
import { TestBed } from '@angular/core/testing';
|
import { TestBed } from '@angular/core/testing';
|
||||||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
|
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
|
||||||
import { HttpHeaders, HTTP_INTERCEPTORS, HttpResponse, HttpXsrfTokenExtractor, HttpErrorResponse } from '@angular/common/http';
|
import { HttpHeaders, HTTP_INTERCEPTORS, HttpXsrfTokenExtractor } from '@angular/common/http';
|
||||||
import { DspaceRestService } from '../dspace-rest/dspace-rest.service';
|
import { DspaceRestService } from '../dspace-rest/dspace-rest.service';
|
||||||
import { RestRequestMethod } from '../data/rest-request-method';
|
import { RestRequestMethod } from '../data/rest-request-method';
|
||||||
import { CookieService } from '../services/cookie.service';
|
import { CookieService } from '../services/cookie.service';
|
||||||
import { CookieServiceMock } from '../../shared/mocks/cookie.service.mock';
|
import { CookieServiceMock } from '../../shared/mocks/cookie.service.mock';
|
||||||
import { XsrfInterceptor } from './xsrf.interceptor';
|
import { XsrfInterceptor } from './xsrf.interceptor';
|
||||||
|
import { HttpXsrfTokenExtractorMock } from '../../shared/mocks/http-xsrf-token-extractor.mock';
|
||||||
/**
|
|
||||||
* A Mock TokenExtractor which just returns whatever token it is initialized with.
|
|
||||||
* This mock object is injected into our XsrfInterceptor, so that it always finds
|
|
||||||
* the same fake XSRF token.
|
|
||||||
*/
|
|
||||||
class MockTokenExtractor extends HttpXsrfTokenExtractor {
|
|
||||||
constructor(private token: string | null) { super(); }
|
|
||||||
|
|
||||||
getToken(): string | null { return this.token; }
|
|
||||||
}
|
|
||||||
|
|
||||||
describe(`XsrfInterceptor`, () => {
|
describe(`XsrfInterceptor`, () => {
|
||||||
let service: DspaceRestService;
|
let service: DspaceRestService;
|
||||||
let httpMock: HttpTestingController;
|
let httpMock: HttpTestingController;
|
||||||
let cookieService: CookieService;
|
let cookieService: CookieService;
|
||||||
|
|
||||||
// Create a MockTokenExtractor which always returns "test-token". This will
|
// mock XSRF token
|
||||||
// be used as the test HttpXsrfTokenExtractor, see below.
|
|
||||||
const testToken = 'test-token';
|
const testToken = 'test-token';
|
||||||
const mockTokenExtractor = new MockTokenExtractor(testToken);
|
|
||||||
|
|
||||||
// Mock payload/statuses are dummy content as we are not testing the results
|
// Mock payload/statuses are dummy content as we are not testing the results
|
||||||
// of any below requests. We are only testing for X-XSRF-TOKEN header.
|
// of any below requests. We are only testing for X-XSRF-TOKEN header.
|
||||||
@@ -46,7 +34,7 @@ describe(`XsrfInterceptor`, () => {
|
|||||||
useClass: XsrfInterceptor,
|
useClass: XsrfInterceptor,
|
||||||
multi: true,
|
multi: true,
|
||||||
},
|
},
|
||||||
{ provide: HttpXsrfTokenExtractor, useValue: mockTokenExtractor },
|
{ provide: HttpXsrfTokenExtractor, useValue: new HttpXsrfTokenExtractorMock(testToken) },
|
||||||
{ provide: CookieService, useValue: new CookieServiceMock() }
|
{ provide: CookieService, useValue: new CookieServiceMock() }
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
12
src/app/shared/mocks/http-xsrf-token-extractor.mock.ts
Normal file
12
src/app/shared/mocks/http-xsrf-token-extractor.mock.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { HttpXsrfTokenExtractor } from '@angular/common/http';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Mock TokenExtractor which just returns whatever token it is initialized with.
|
||||||
|
* This mock object is injected into our XsrfInterceptor, so that it always finds
|
||||||
|
* the same fake XSRF token.
|
||||||
|
*/
|
||||||
|
export class HttpXsrfTokenExtractorMock extends HttpXsrfTokenExtractor {
|
||||||
|
constructor(private token: string | null) { super(); }
|
||||||
|
|
||||||
|
getToken(): string | null { return this.token; }
|
||||||
|
}
|
@@ -10,6 +10,10 @@ import { UploaderComponent } from './uploader.component';
|
|||||||
import { FileUploadModule } from 'ng2-file-upload';
|
import { FileUploadModule } from 'ng2-file-upload';
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
import { createTestComponent } from '../testing/utils.test';
|
import { createTestComponent } from '../testing/utils.test';
|
||||||
|
import { HttpXsrfTokenExtractor } from '@angular/common/http';
|
||||||
|
import { CookieService } from '../../core/services/cookie.service';
|
||||||
|
import { CookieServiceMock } from '../mocks/cookie.service.mock';
|
||||||
|
import { HttpXsrfTokenExtractorMock } from '../mocks/http-xsrf-token-extractor.mock';
|
||||||
|
|
||||||
describe('Chips component', () => {
|
describe('Chips component', () => {
|
||||||
|
|
||||||
@@ -33,7 +37,9 @@ describe('Chips component', () => {
|
|||||||
ChangeDetectorRef,
|
ChangeDetectorRef,
|
||||||
ScrollToService,
|
ScrollToService,
|
||||||
UploaderComponent,
|
UploaderComponent,
|
||||||
UploaderService
|
UploaderService,
|
||||||
|
{ provide: HttpXsrfTokenExtractor, useValue: new HttpXsrfTokenExtractorMock('mock-token') },
|
||||||
|
{ provide: CookieService, useValue: new CookieServiceMock() },
|
||||||
],
|
],
|
||||||
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user