mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
71504: Shortlived tokens + file-download-link test cases
This commit is contained in:
@@ -1,17 +1,14 @@
|
|||||||
import { async, inject, TestBed } from '@angular/core/testing';
|
import { async, inject, TestBed } from '@angular/core/testing';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { ActivatedRoute, Router } from '@angular/router';
|
import { ActivatedRoute, Router } from '@angular/router';
|
||||||
|
|
||||||
import { Store, StoreModule } from '@ngrx/store';
|
import { Store, StoreModule } from '@ngrx/store';
|
||||||
import { REQUEST } from '@nguniversal/express-engine/tokens';
|
import { REQUEST } from '@nguniversal/express-engine/tokens';
|
||||||
import { of as observableOf } from 'rxjs';
|
import { of as observableOf } from 'rxjs';
|
||||||
|
|
||||||
import { authReducer, AuthState } from './auth.reducer';
|
import { authReducer, AuthState } from './auth.reducer';
|
||||||
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
|
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
|
||||||
import { AuthService, IMPERSONATING_COOKIE } from './auth.service';
|
import { AuthService, IMPERSONATING_COOKIE } from './auth.service';
|
||||||
import { RouterStub } from '../../shared/testing/router.stub';
|
import { RouterStub } from '../../shared/testing/router.stub';
|
||||||
import { ActivatedRouteStub } from '../../shared/testing/active-router.stub';
|
import { ActivatedRouteStub } from '../../shared/testing/active-router.stub';
|
||||||
|
|
||||||
import { CookieService } from '../services/cookie.service';
|
import { CookieService } from '../services/cookie.service';
|
||||||
import { AuthRequestServiceStub } from '../../shared/testing/auth-request-service.stub';
|
import { AuthRequestServiceStub } from '../../shared/testing/auth-request-service.stub';
|
||||||
import { AuthRequestService } from './auth-request.service';
|
import { AuthRequestService } from './auth-request.service';
|
||||||
@@ -49,6 +46,7 @@ describe('AuthService test', () => {
|
|||||||
let storage: CookieService;
|
let storage: CookieService;
|
||||||
let token: AuthTokenInfo;
|
let token: AuthTokenInfo;
|
||||||
let authenticatedState;
|
let authenticatedState;
|
||||||
|
let unAuthenticatedState;
|
||||||
let linkService;
|
let linkService;
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
@@ -67,6 +65,13 @@ describe('AuthService test', () => {
|
|||||||
authToken: token,
|
authToken: token,
|
||||||
user: EPersonMock
|
user: EPersonMock
|
||||||
};
|
};
|
||||||
|
unAuthenticatedState = {
|
||||||
|
authenticated: false,
|
||||||
|
loaded: true,
|
||||||
|
loading: false,
|
||||||
|
authToken: undefined,
|
||||||
|
user: undefined
|
||||||
|
};
|
||||||
authRequest = new AuthRequestServiceStub();
|
authRequest = new AuthRequestServiceStub();
|
||||||
routeStub = new ActivatedRouteStub();
|
routeStub = new ActivatedRouteStub();
|
||||||
linkService = {
|
linkService = {
|
||||||
@@ -214,6 +219,12 @@ describe('AuthService test', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return the shortlived token when user is logged in', () => {
|
||||||
|
authService.getShortlivedToken().subscribe((shortlivedToken: string) => {
|
||||||
|
expect(shortlivedToken).toEqual(authRequest.mockShortLivedToken);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should return token object when it is valid', () => {
|
it('should return token object when it is valid', () => {
|
||||||
authService.hasValidAuthenticationToken().subscribe((tokenState: AuthTokenInfo) => {
|
authService.hasValidAuthenticationToken().subscribe((tokenState: AuthTokenInfo) => {
|
||||||
expect(tokenState).toBe(token);
|
expect(tokenState).toBe(token);
|
||||||
@@ -448,4 +459,44 @@ describe('AuthService test', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('when user is not logged in', () => {
|
||||||
|
beforeEach(async(() => {
|
||||||
|
init();
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
imports: [
|
||||||
|
StoreModule.forRoot({ authReducer }, {
|
||||||
|
runtimeChecks: {
|
||||||
|
strictStateImmutability: false,
|
||||||
|
strictActionImmutability: false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
],
|
||||||
|
providers: [
|
||||||
|
{ provide: AuthRequestService, useValue: authRequest },
|
||||||
|
{ provide: REQUEST, useValue: {} },
|
||||||
|
{ provide: Router, useValue: routerStub },
|
||||||
|
{ provide: RouteService, useValue: routeServiceStub },
|
||||||
|
{ provide: RemoteDataBuildService, useValue: linkService },
|
||||||
|
CookieService,
|
||||||
|
AuthService
|
||||||
|
]
|
||||||
|
}).compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(inject([CookieService, AuthRequestService, Store, Router, RouteService], (cookieService: CookieService, authReqService: AuthRequestService, store: Store<AppState>, router: Router, routeService: RouteService) => {
|
||||||
|
store
|
||||||
|
.subscribe((state) => {
|
||||||
|
(state as any).core = Object.create({});
|
||||||
|
(state as any).core.auth = unAuthenticatedState;
|
||||||
|
});
|
||||||
|
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store);
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('should return null for the shortlived token', () => {
|
||||||
|
authService.getShortlivedToken().subscribe((shortlivedToken: string) => {
|
||||||
|
expect(shortlivedToken).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
45
src/app/core/auth/token-response-parsing.service.spec.ts
Normal file
45
src/app/core/auth/token-response-parsing.service.spec.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import { TokenResponseParsingService } from './token-response-parsing.service';
|
||||||
|
import { DSpaceRESTV2Response } from '../dspace-rest-v2/dspace-rest-v2-response.model';
|
||||||
|
import { TokenResponse } from '../cache/response.models';
|
||||||
|
|
||||||
|
describe('TokenResponseParsingService', () => {
|
||||||
|
let service: TokenResponseParsingService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
service = new TokenResponseParsingService();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('parse', () => {
|
||||||
|
it('should return a TokenResponse containing the token', () => {
|
||||||
|
const data = {
|
||||||
|
payload: {
|
||||||
|
token: 'valid-token'
|
||||||
|
},
|
||||||
|
statusCode: 200,
|
||||||
|
statusText: 'OK'
|
||||||
|
} as DSpaceRESTV2Response;
|
||||||
|
const expected = new TokenResponse(data.payload.token, true, 200, 'OK');
|
||||||
|
expect(service.parse(undefined, data)).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return an empty TokenResponse when payload doesn\'t contain a token', () => {
|
||||||
|
const data = {
|
||||||
|
payload: {},
|
||||||
|
statusCode: 200,
|
||||||
|
statusText: 'OK'
|
||||||
|
} as DSpaceRESTV2Response;
|
||||||
|
const expected = new TokenResponse(null, false, 200, 'OK');
|
||||||
|
expect(service.parse(undefined, data)).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return an error TokenResponse when the response failed', () => {
|
||||||
|
const data = {
|
||||||
|
payload: {},
|
||||||
|
statusCode: 400,
|
||||||
|
statusText: 'BAD REQUEST'
|
||||||
|
} as DSpaceRESTV2Response;
|
||||||
|
const expected = new TokenResponse(null, false, 400, 'BAD REQUEST');
|
||||||
|
expect(service.parse(undefined, data)).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@@ -1,14 +1,33 @@
|
|||||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
import { FileDownloadLinkComponent } from './file-download-link.component';
|
import { FileDownloadLinkComponent } from './file-download-link.component';
|
||||||
|
import { AuthService } from '../../core/auth/auth.service';
|
||||||
|
import { FileService } from '../../core/shared/file.service';
|
||||||
|
import { of as observableOf } from 'rxjs';
|
||||||
|
|
||||||
describe('FileDownloadLinkComponent', () => {
|
describe('FileDownloadLinkComponent', () => {
|
||||||
let component: FileDownloadLinkComponent;
|
let component: FileDownloadLinkComponent;
|
||||||
let fixture: ComponentFixture<FileDownloadLinkComponent>;
|
let fixture: ComponentFixture<FileDownloadLinkComponent>;
|
||||||
|
|
||||||
|
let authService: AuthService;
|
||||||
|
let fileService: FileService;
|
||||||
|
let href: string;
|
||||||
|
|
||||||
|
function init() {
|
||||||
|
authService = jasmine.createSpyObj('authService', {
|
||||||
|
isAuthenticated: observableOf(true)
|
||||||
|
});
|
||||||
|
fileService = jasmine.createSpyObj('fileService', ['downloadFile']);
|
||||||
|
href = 'test-download-file-link';
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(async(() => {
|
beforeEach(async(() => {
|
||||||
|
init();
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
declarations: [ FileDownloadLinkComponent ]
|
declarations: [ FileDownloadLinkComponent ],
|
||||||
|
providers: [
|
||||||
|
{ provide: AuthService, useValue: authService },
|
||||||
|
{ provide: FileService, useValue: fileService }
|
||||||
|
]
|
||||||
})
|
})
|
||||||
.compileComponents();
|
.compileComponents();
|
||||||
}));
|
}));
|
||||||
@@ -16,10 +35,23 @@ describe('FileDownloadLinkComponent', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
fixture = TestBed.createComponent(FileDownloadLinkComponent);
|
fixture = TestBed.createComponent(FileDownloadLinkComponent);
|
||||||
component = fixture.componentInstance;
|
component = fixture.componentInstance;
|
||||||
|
component.href = href;
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create', () => {
|
describe('downloadFile', () => {
|
||||||
expect(component).toBeTruthy();
|
let result;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
result = component.downloadFile();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call fileService.downloadFile with the provided href', () => {
|
||||||
|
expect(fileService.downloadFile).toHaveBeenCalledWith(href);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false', () => {
|
||||||
|
expect(result).toEqual(false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -9,6 +9,7 @@ import { EPersonMock } from './eperson.mock';
|
|||||||
export class AuthRequestServiceStub {
|
export class AuthRequestServiceStub {
|
||||||
protected mockUser: EPerson = EPersonMock;
|
protected mockUser: EPerson = EPersonMock;
|
||||||
protected mockTokenInfo = new AuthTokenInfo('test_token');
|
protected mockTokenInfo = new AuthTokenInfo('test_token');
|
||||||
|
protected mockShortLivedToken = 'test-shortlived-token';
|
||||||
|
|
||||||
public postToEndpoint(method: string, body: any, options?: HttpOptions): Observable<any> {
|
public postToEndpoint(method: string, body: any, options?: HttpOptions): Observable<any> {
|
||||||
const authStatusStub: AuthStatus = new AuthStatus();
|
const authStatusStub: AuthStatus = new AuthStatus();
|
||||||
@@ -82,4 +83,8 @@ export class AuthRequestServiceStub {
|
|||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getShortlivedToken() {
|
||||||
|
return observableOf(this.mockShortLivedToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user