Added action to retrieve authenticated eperson object

This commit is contained in:
Giuseppe Digilio
2020-03-02 13:09:17 +01:00
parent df8ef2b5c3
commit 18e17f0dad
11 changed files with 281 additions and 83 deletions

View File

@@ -5,14 +5,12 @@ import { ActivatedRoute, Router } from '@angular/router';
import { Store, StoreModule } from '@ngrx/store';
import { REQUEST } from '@nguniversal/express-engine/tokens';
import { of as observableOf } from 'rxjs';
import { LinkService } from '../cache/builders/link.service';
import { authReducer, AuthState } from './auth.reducer';
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
import { AuthService } from './auth.service';
import { RouterStub } from '../../shared/testing/router-stub';
import { ActivatedRouteStub } from '../../shared/testing/active-router-stub';
import { CookieService } from '../services/cookie.service';
import { AuthRequestServiceStub } from '../../shared/testing/auth-request-service-stub';
import { AuthRequestService } from './auth-request.service';
@@ -23,12 +21,21 @@ import { EPersonMock } from '../../shared/testing/eperson-mock';
import { AppState } from '../../app.reducer';
import { ClientCookieService } from '../services/client-cookie.service';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { getMockRemoteDataBuildService } from '../../shared/mocks/mock-remote-data-build.service';
import { routeServiceStub } from '../../shared/testing/route-service-stub';
import { RouteService } from '../services/route.service';
import { Observable } from 'rxjs/internal/Observable';
import { RemoteData } from '../data/remote-data';
import { createSuccessfulRemoteDataObject$ } from '../../shared/testing/utils';
import { EPersonDataService } from '../eperson/eperson-data.service';
describe('AuthService test', () => {
const mockEpersonDataService: any = {
findByHref(href: string): Observable<RemoteData<EPerson>> {
return createSuccessfulRemoteDataObject$(EPersonMock);
}
};
let mockStore: Store<AuthState>;
let authService: AuthService;
let routeServiceMock: RouteService;
@@ -62,7 +69,7 @@ describe('AuthService test', () => {
linkService = {
resolveLinks: {}
};
spyOn(linkService, 'resolveLinks').and.returnValue({authenticated: true, eperson: observableOf({payload: {}})});
spyOn(linkService, 'resolveLinks').and.returnValue({ authenticated: true, eperson: observableOf({ payload: {} }) });
}
@@ -83,7 +90,7 @@ describe('AuthService test', () => {
{ provide: RouteService, useValue: routeServiceStub },
{ provide: ActivatedRoute, useValue: routeStub },
{ provide: Store, useValue: mockStore },
{ provide: LinkService, useValue: linkService },
{ provide: EPersonDataService, useValue: mockEpersonDataService },
CookieService,
AuthService
],
@@ -101,8 +108,14 @@ describe('AuthService test', () => {
expect(authService.authenticate.bind(null, 'user', 'passwordwrong')).toThrow();
});
it('should return the authenticated user object when user token is valid', () => {
authService.authenticatedUser(new AuthTokenInfo('test_token')).subscribe((user: EPerson) => {
it('should return the authenticated user href when user token is valid', () => {
authService.authenticatedUser(new AuthTokenInfo('test_token')).subscribe((userHref: string) => {
expect(userHref).toBeDefined();
});
});
it('should return the authenticated user', () => {
authService.retrieveAuthenticatedUserByHref(EPersonMock._links.self.href).subscribe((user: EPerson) => {
expect(user).toBeDefined();
});
});
@@ -159,7 +172,7 @@ describe('AuthService test', () => {
(state as any).core = Object.create({});
(state as any).core.auth = authenticatedState;
});
authService = new AuthService({}, window, undefined, authReqService, router, routeService, cookieService, store, linkService);
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store);
}));
it('should return true when user is logged in', () => {
@@ -221,7 +234,7 @@ describe('AuthService test', () => {
(state as any).core = Object.create({});
(state as any).core.auth = authenticatedState;
});
authService = new AuthService({}, window, undefined, authReqService, router, routeService, cookieService, store, linkService);
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store);
storage = (authService as any).storage;
routeServiceMock = TestBed.get(RouteService);
routerStub = TestBed.get(Router);
@@ -250,7 +263,7 @@ describe('AuthService test', () => {
expect(storage.remove).toHaveBeenCalled();
});
it ('should set redirect url to previous page', () => {
it('should set redirect url to previous page', () => {
spyOn(routeServiceMock, 'getHistory').and.callThrough();
spyOn(routerStub, 'navigateByUrl');
authService.redirectAfterLoginSuccess(true);
@@ -258,7 +271,7 @@ describe('AuthService test', () => {
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/collection/123');
});
it ('should set redirect url to current page', () => {
it('should set redirect url to current page', () => {
spyOn(routeServiceMock, 'getHistory').and.callThrough();
spyOn(routerStub, 'navigateByUrl');
authService.redirectAfterLoginSuccess(false);
@@ -266,7 +279,7 @@ describe('AuthService test', () => {
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/home');
});
it ('should redirect to / and not to /login', () => {
it('should redirect to / and not to /login', () => {
spyOn(routeServiceMock, 'getHistory').and.returnValue(observableOf(['/login', '/login']));
spyOn(routerStub, 'navigateByUrl');
authService.redirectAfterLoginSuccess(true);
@@ -274,7 +287,7 @@ describe('AuthService test', () => {
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/');
});
it ('should redirect to / when no redirect url is found', () => {
it('should redirect to / when no redirect url is found', () => {
spyOn(routeServiceMock, 'getHistory').and.returnValue(observableOf(['']));
spyOn(routerStub, 'navigateByUrl');
authService.redirectAfterLoginSuccess(true);