Add test for xsrf and change to Post so xsrf is tested for validity

This commit is contained in:
Nathan Buckingham
2023-02-03 15:45:44 -05:00
parent 34c07fd904
commit 6d99f51d78
2 changed files with 16 additions and 6 deletions

View File

@@ -1,24 +1,29 @@
import { AuthRequestService } from './auth-request.service';
import { RequestService } from '../data/request.service';
import { ServerAuthRequestService } from './server-auth-request.service';
import { HttpXsrfTokenExtractorMock } from '../../shared/mocks/http-xsrf-token-extractor.mock';
describe(`ServerAuthRequestService`, () => {
let href: string;
let requestService: RequestService;
let service: AuthRequestService;
let xsrfExtractor: HttpXsrfTokenExtractorMock;
const mockToken = 'mockToken';
beforeEach(() => {
href = 'https://rest.api/auth/shortlivedtokens';
requestService = jasmine.createSpyObj('requestService', {
'generateRequestId': '8bb0582d-5013-4337-af9c-763beb25aae2'
});
service = new ServerAuthRequestService(null, requestService, null);
xsrfExtractor = new HttpXsrfTokenExtractorMock(mockToken);
service = new ServerAuthRequestService(null, requestService, null, xsrfExtractor);
});
describe(`createShortLivedTokenRequest`, () => {
it(`should return a GetRequest`, () => {
it(`should return a PostRequest`, () => {
const result = (service as any).createShortLivedTokenRequest(href);
expect(result.constructor.name).toBe('GetRequest');
expect(result.constructor.name).toBe('PostRequest');
});
it(`should return a request with the given href`, () => {
@@ -26,6 +31,11 @@ describe(`ServerAuthRequestService`, () => {
expect(result.href).toBe(href) ;
});
it(`should return a request with a xsrf header`, () => {
const result = (service as any).createShortLivedTokenRequest(href);
expect(result.options.headers.get('X-XSRF-TOKEN')).toBe(mockToken);
});
it(`should have a responseMsToLive of 2 seconds`, () => {
const result = (service as any).createShortLivedTokenRequest(href);
expect(result.responseMsToLive).toBe(2 * 1000) ;