mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
Fix lint and test issues
This commit is contained in:
@@ -11,6 +11,7 @@ import { HttpOptions } from '../dspace-rest/dspace-rest.service';
|
||||
import objectContaining = jasmine.objectContaining;
|
||||
import { AuthStatus } from './models/auth-status.model';
|
||||
import { RestRequestMethod } from '../data/rest-request-method';
|
||||
import { Observable, of as observableOf } from 'rxjs';
|
||||
|
||||
describe(`AuthRequestService`, () => {
|
||||
let halService: HALEndpointService;
|
||||
@@ -34,8 +35,8 @@ describe(`AuthRequestService`, () => {
|
||||
super(hes, rs, rdbs);
|
||||
}
|
||||
|
||||
protected createShortLivedTokenRequest(href: string): PostRequest {
|
||||
return new PostRequest(this.requestService.generateRequestId(), href);
|
||||
protected createShortLivedTokenRequest(href: string): Observable<PostRequest> {
|
||||
return observableOf(new PostRequest(this.requestService.generateRequestId(), href));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,8 @@
|
||||
import { AuthRequestService } from './auth-request.service';
|
||||
import { RequestService } from '../data/request.service';
|
||||
import { BrowserAuthRequestService } from './browser-auth-request.service';
|
||||
import { Observable } from 'rxjs';
|
||||
import { PostRequest } from '../data/request.models';
|
||||
|
||||
describe(`BrowserAuthRequestService`, () => {
|
||||
let href: string;
|
||||
@@ -17,13 +19,17 @@ describe(`BrowserAuthRequestService`, () => {
|
||||
|
||||
describe(`createShortLivedTokenRequest`, () => {
|
||||
it(`should return a PostRequest`, () => {
|
||||
const result = (service as any).createShortLivedTokenRequest(href);
|
||||
const obs = (service as any).createShortLivedTokenRequest(href) as Observable<PostRequest>;
|
||||
obs.subscribe((result: PostRequest) => {
|
||||
expect(result.constructor.name).toBe('PostRequest');
|
||||
});
|
||||
});
|
||||
|
||||
it(`should return a request with the given href`, () => {
|
||||
const result = (service as any).createShortLivedTokenRequest(href);
|
||||
expect(result.href).toBe(href) ;
|
||||
const obs = (service as any).createShortLivedTokenRequest(href) as Observable<PostRequest>;
|
||||
obs.subscribe((result: PostRequest) => {
|
||||
expect(result.href).toBe(href);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -1,44 +1,60 @@
|
||||
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';
|
||||
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
|
||||
import { Observable, of as observableOf } from 'rxjs';
|
||||
import { XSRF_RESPONSE_HEADER } from '../xsrf/xsrf.interceptor';
|
||||
import { HALEndpointService } from '../shared/hal-endpoint.service';
|
||||
import { PostRequest } from '../data/request.models';
|
||||
|
||||
describe(`ServerAuthRequestService`, () => {
|
||||
let href: string;
|
||||
let requestService: RequestService;
|
||||
let service: AuthRequestService;
|
||||
let xsrfExtractor: HttpXsrfTokenExtractorMock;
|
||||
|
||||
const mockToken = 'mockToken';
|
||||
let httpClient: HttpClient;
|
||||
let httpResponse: HttpResponse<any>;
|
||||
let halService: HALEndpointService;
|
||||
const mockToken = 'mock-token';
|
||||
|
||||
beforeEach(() => {
|
||||
href = 'https://rest.api/auth/shortlivedtokens';
|
||||
requestService = jasmine.createSpyObj('requestService', {
|
||||
'generateRequestId': '8bb0582d-5013-4337-af9c-763beb25aae2'
|
||||
});
|
||||
xsrfExtractor = new HttpXsrfTokenExtractorMock(mockToken);
|
||||
service = new ServerAuthRequestService(null, requestService, null, xsrfExtractor);
|
||||
httpResponse = {
|
||||
body: { bar: false },
|
||||
headers: new HttpHeaders({ XSRF_RESPONSE_HEADER: mockToken }),
|
||||
statusText: '200'
|
||||
} as HttpResponse<any>;
|
||||
httpClient = jasmine.createSpyObj('httpClient', {
|
||||
get: observableOf(httpResponse),
|
||||
});
|
||||
halService = jasmine.createSpyObj('halService', {
|
||||
'getRootHref': '/api'
|
||||
});
|
||||
service = new ServerAuthRequestService(halService, requestService, null, httpClient);
|
||||
});
|
||||
|
||||
describe(`createShortLivedTokenRequest`, () => {
|
||||
it(`should return a PostRequest`, () => {
|
||||
const result = (service as any).createShortLivedTokenRequest(href);
|
||||
const obs = (service as any).createShortLivedTokenRequest(href) as Observable<PostRequest>;
|
||||
obs.subscribe((result: PostRequest) => {
|
||||
expect(result.constructor.name).toBe('PostRequest');
|
||||
});
|
||||
});
|
||||
|
||||
it(`should return a request with the given href`, () => {
|
||||
const result = (service as any).createShortLivedTokenRequest(href);
|
||||
const obs = (service as any).createShortLivedTokenRequest(href) as Observable<PostRequest>;
|
||||
obs.subscribe((result: PostRequest) => {
|
||||
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);
|
||||
const obs = (service as any).createShortLivedTokenRequest(href) as Observable<PostRequest>;
|
||||
obs.subscribe((result: PostRequest) => {
|
||||
expect(result.options.headers.get(XSRF_RESPONSE_HEADER)).toBe(mockToken);
|
||||
});
|
||||
|
||||
it(`should have a responseMsToLive of 2 seconds`, () => {
|
||||
const result = (service as any).createShortLivedTokenRequest(href);
|
||||
expect(result.responseMsToLive).toBe(2 * 1000) ;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -6,7 +6,6 @@ import { RequestService } from '../data/request.service';
|
||||
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
|
||||
import {
|
||||
HttpHeaders,
|
||||
HttpXsrfTokenExtractor,
|
||||
HttpClient,
|
||||
HttpResponse
|
||||
} from '@angular/common/http';
|
||||
@@ -53,15 +52,16 @@ export class ServerAuthRequestService extends AuthRequestService {
|
||||
.set('Cookie', `${DSPACE_XSRF_COOKIE}=${xsrfToken}`)),
|
||||
map((headers: HttpHeaders) =>
|
||||
// Create a new PostRequest using those headers and the given href
|
||||
new PostRequest(
|
||||
Object.assign(new PostRequest(
|
||||
this.requestService.generateRequestId(),
|
||||
href,
|
||||
{},
|
||||
{
|
||||
headers: headers,
|
||||
}
|
||||
))
|
||||
},
|
||||
),{})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,16 +1,16 @@
|
||||
import { XhrFactory } from '@angular/common';
|
||||
import { Injectable } from '@angular/core';
|
||||
import * as xhr2 from 'xhr2';
|
||||
import { prototype, XMLHttpRequest } from 'xhr2';
|
||||
|
||||
/**
|
||||
* Overrides the default XhrFactoru server side, to allow us to set cookies in requests to the
|
||||
* Overrides the default XhrFactory server side, to allow us to set cookies in requests to the
|
||||
* backend. This was added to be able to perform a working XSRF request from the node server, as it
|
||||
* needs to set a cookie for the XSRF token
|
||||
*/
|
||||
@Injectable()
|
||||
export class ServerXhrService implements XhrFactory {
|
||||
build(): XMLHttpRequest {
|
||||
xhr2.prototype._restrictedHeaders.cookie = false;
|
||||
return new xhr2.XMLHttpRequest();
|
||||
prototype._restrictedHeaders.cookie = false;
|
||||
return new XMLHttpRequest();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user