74612: 403 page + redirect to login on unauthorized

This commit is contained in:
Kristof De Langhe
2020-11-18 14:05:36 +01:00
parent 12da991630
commit ab755d3fd9
27 changed files with 221 additions and 35 deletions

View File

@@ -14,7 +14,7 @@ import {
getResourceLinksFromResponse,
getResponseFromEntry,
getSucceededRemoteData,
redirectOn404Or401
redirectOn4xx
} from './operators';
import { RemoteData } from '../data/remote-data';
import { RemoteDataError } from '../data/remote-data-error';
@@ -200,39 +200,74 @@ describe('Core Module - RxJS Operators', () => {
});
});
describe('redirectOn404Or401', () => {
describe('redirectOn4xx', () => {
let router;
let authService;
beforeEach(() => {
router = jasmine.createSpyObj('router', ['navigateByUrl']);
authService = jasmine.createSpyObj('authService', {
isAuthenticated: observableOf(true),
setRedirectUrl: {}
});
});
it('should call navigateByUrl to a 404 page, when the remote data contains a 404 error', () => {
const testRD = createFailedRemoteDataObject(undefined, new RemoteDataError(404, 'Not Found', 'Object was not found'));
observableOf(testRD).pipe(redirectOn404Or401(router)).subscribe();
observableOf(testRD).pipe(redirectOn4xx(router, authService)).subscribe();
expect(router.navigateByUrl).toHaveBeenCalledWith('/404', { skipLocationChange: true });
});
it('should call navigateByUrl to a 403 page, when the remote data contains a 403 error', () => {
const testRD = createFailedRemoteDataObject(undefined, new RemoteDataError(403, 'Forbidden', 'Forbidden access'));
observableOf(testRD).pipe(redirectOn4xx(router, authService)).subscribe();
expect(router.navigateByUrl).toHaveBeenCalledWith('/403', { skipLocationChange: true });
});
it('should call navigateByUrl to a 401 page, when the remote data contains a 401 error', () => {
const testRD = createFailedRemoteDataObject(undefined, new RemoteDataError(401, 'Unauthorized', 'The current user is unauthorized'));
observableOf(testRD).pipe(redirectOn404Or401(router)).subscribe();
observableOf(testRD).pipe(redirectOn4xx(router, authService)).subscribe();
expect(router.navigateByUrl).toHaveBeenCalledWith('/401', { skipLocationChange: true });
});
it('should not call navigateByUrl to a 404 or 401 page, when the remote data contains another error than a 404 or 401', () => {
it('should not call navigateByUrl to a 404, 403 or 401 page, when the remote data contains another error than a 404, 403 or 401', () => {
const testRD = createFailedRemoteDataObject(undefined, new RemoteDataError(500, 'Server Error', 'Something went wrong'));
observableOf(testRD).pipe(redirectOn404Or401(router)).subscribe();
observableOf(testRD).pipe(redirectOn4xx(router, authService)).subscribe();
expect(router.navigateByUrl).not.toHaveBeenCalled();
});
it('should not call navigateByUrl to a 404 or 401 page, when the remote data contains no error', () => {
it('should not call navigateByUrl to a 404, 403 or 401 page, when the remote data contains no error', () => {
const testRD = createSuccessfulRemoteDataObject(undefined);
observableOf(testRD).pipe(redirectOn404Or401(router)).subscribe();
observableOf(testRD).pipe(redirectOn4xx(router, authService)).subscribe();
expect(router.navigateByUrl).not.toHaveBeenCalled();
});
describe('when the user is not authenticated', () => {
beforeEach(() => {
(authService.isAuthenticated as jasmine.Spy).and.returnValue(observableOf(false));
});
it('should set the redirect url and navigate to login when the remote data contains a 401 error', () => {
const testRD = createFailedRemoteDataObject(undefined, new RemoteDataError(401, 'Unauthorized', 'The current user is unauthorized'));
observableOf(testRD).pipe(redirectOn4xx(router, authService)).subscribe();
expect(authService.setRedirectUrl).toHaveBeenCalled();
expect(router.navigateByUrl).toHaveBeenCalledWith('login');
});
it('should set the redirect url and navigate to login when the remote data contains a 403 error', () => {
const testRD = createFailedRemoteDataObject(undefined, new RemoteDataError(403, 'Forbidden', 'Forbidden access'));
observableOf(testRD).pipe(redirectOn4xx(router, authService)).subscribe();
expect(authService.setRedirectUrl).toHaveBeenCalled();
expect(router.navigateByUrl).toHaveBeenCalledWith('login');
});
});
});
describe('getResponseFromEntry', () => {