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

@@ -18,12 +18,14 @@ import {
LogOutErrorAction,
LogOutSuccessAction,
RefreshTokenErrorAction,
RefreshTokenSuccessAction
RefreshTokenSuccessAction,
RetrieveAuthenticatedEpersonAction,
RetrieveAuthenticatedEpersonErrorAction,
RetrieveAuthenticatedEpersonSuccessAction
} from './auth.actions';
import { AuthServiceStub } from '../../shared/testing/auth-service-stub';
import { AuthService } from './auth.service';
import { AuthState } from './auth.reducer';
import { EPersonMock } from '../../shared/testing/eperson-mock';
describe('AuthEffects', () => {
@@ -42,13 +44,14 @@ describe('AuthEffects', () => {
authServiceStub = new AuthServiceStub();
token = authServiceStub.getToken();
}
beforeEach(() => {
init();
TestBed.configureTestingModule({
providers: [
AuthEffects,
{provide: AuthService, useValue: authServiceStub},
{provide: Store, useValue: store},
{ provide: AuthService, useValue: authServiceStub },
{ provide: Store, useValue: store },
provideMockActions(() => actions),
// other providers
],
@@ -63,11 +66,11 @@ describe('AuthEffects', () => {
actions = hot('--a-', {
a: {
type: AuthActionTypes.AUTHENTICATE,
payload: {email: 'user', password: 'password'}
payload: { email: 'user', password: 'password' }
}
});
const expected = cold('--b-', {b: new AuthenticationSuccessAction(token)});
const expected = cold('--b-', { b: new AuthenticationSuccessAction(token) });
expect(authEffects.authenticate$).toBeObservable(expected);
});
@@ -80,11 +83,11 @@ describe('AuthEffects', () => {
actions = hot('--a-', {
a: {
type: AuthActionTypes.AUTHENTICATE,
payload: {email: 'user', password: 'wrongpassword'}
payload: { email: 'user', password: 'wrongpassword' }
}
});
const expected = cold('--b-', {b: new AuthenticationErrorAction(new Error('Message Error test'))});
const expected = cold('--b-', { b: new AuthenticationErrorAction(new Error('Message Error test')) });
expect(authEffects.authenticate$).toBeObservable(expected);
});
@@ -94,9 +97,9 @@ describe('AuthEffects', () => {
describe('authenticateSuccess$', () => {
it('should return a AUTHENTICATED action in response to a AUTHENTICATE_SUCCESS action', () => {
actions = hot('--a-', {a: {type: AuthActionTypes.AUTHENTICATE_SUCCESS, payload: token}});
actions = hot('--a-', { a: { type: AuthActionTypes.AUTHENTICATE_SUCCESS, payload: token } });
const expected = cold('--b-', {b: new AuthenticatedAction(token)});
const expected = cold('--b-', { b: new AuthenticatedAction(token) });
expect(authEffects.authenticateSuccess$).toBeObservable(expected);
});
@@ -106,9 +109,9 @@ describe('AuthEffects', () => {
describe('when token is valid', () => {
it('should return a AUTHENTICATED_SUCCESS action in response to a AUTHENTICATED action', () => {
actions = hot('--a-', {a: {type: AuthActionTypes.AUTHENTICATED, payload: token}});
actions = hot('--a-', { a: { type: AuthActionTypes.AUTHENTICATED, payload: token } });
const expected = cold('--b-', {b: new AuthenticatedSuccessAction(true, token, EPersonMock)});
const expected = cold('--b-', { b: new AuthenticatedSuccessAction(true, token, EPersonMock._links.self.href) });
expect(authEffects.authenticated$).toBeObservable(expected);
});
@@ -118,23 +121,42 @@ describe('AuthEffects', () => {
it('should return a AUTHENTICATED_ERROR action in response to a AUTHENTICATED action', () => {
spyOn((authEffects as any).authService, 'authenticatedUser').and.returnValue(observableThrow(new Error('Message Error test')));
actions = hot('--a-', {a: {type: AuthActionTypes.AUTHENTICATED, payload: token}});
actions = hot('--a-', { a: { type: AuthActionTypes.AUTHENTICATED, payload: token } });
const expected = cold('--b-', {b: new AuthenticatedErrorAction(new Error('Message Error test'))});
const expected = cold('--b-', { b: new AuthenticatedErrorAction(new Error('Message Error test')) });
expect(authEffects.authenticated$).toBeObservable(expected);
});
});
});
describe('authenticatedSuccess$', () => {
it('should return a RETRIEVE_AUTHENTICATED_EPERSON action in response to a AUTHENTICATED_SUCCESS action', () => {
actions = hot('--a-', {
a: {
type: AuthActionTypes.AUTHENTICATED_SUCCESS, payload: {
authenticated: true,
authToken: token,
userHref: EPersonMock._links.self.href
}
}
});
const expected = cold('--b-', { b: new RetrieveAuthenticatedEpersonAction(EPersonMock._links.self.href) });
expect(authEffects.authenticatedSuccess$).toBeObservable(expected);
});
});
describe('checkToken$', () => {
describe('when check token succeeded', () => {
it('should return a AUTHENTICATED action in response to a CHECK_AUTHENTICATION_TOKEN action', () => {
actions = hot('--a-', {a: {type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN}});
actions = hot('--a-', { a: { type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN } });
const expected = cold('--b-', {b: new AuthenticatedAction(token)});
const expected = cold('--b-', { b: new AuthenticatedAction(token) });
expect(authEffects.checkToken$).toBeObservable(expected);
});
@@ -144,23 +166,53 @@ describe('AuthEffects', () => {
it('should return a CHECK_AUTHENTICATION_TOKEN_ERROR action in response to a CHECK_AUTHENTICATION_TOKEN action', () => {
spyOn((authEffects as any).authService, 'hasValidAuthenticationToken').and.returnValue(observableThrow(''));
actions = hot('--a-', {a: {type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN, payload: token}});
actions = hot('--a-', { a: { type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN, payload: token } });
const expected = cold('--b-', {b: new CheckAuthenticationTokenErrorAction()});
const expected = cold('--b-', { b: new CheckAuthenticationTokenErrorAction() });
expect(authEffects.checkToken$).toBeObservable(expected);
});
})
});
describe('retrieveAuthenticatedEperson$', () => {
describe('when request is successful', () => {
it('should return a RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS action in response to a RETRIEVE_AUTHENTICATED_EPERSON action', () => {
actions = hot('--a-', {
a: {
type: AuthActionTypes.RETRIEVE_AUTHENTICATED_EPERSON,
payload: EPersonMock._links.self.href
}
});
const expected = cold('--b-', { b: new RetrieveAuthenticatedEpersonSuccessAction(EPersonMock) });
expect(authEffects.retrieveAuthenticatedEperson$).toBeObservable(expected);
});
});
describe('when request is not successful', () => {
it('should return a RETRIEVE_AUTHENTICATED_EPERSON_ERROR action in response to a RETRIEVE_AUTHENTICATED_EPERSON action', () => {
spyOn((authEffects as any).authService, 'retrieveAuthenticatedUserByHref').and.returnValue(observableThrow(new Error('Message Error test')));
actions = hot('--a-', { a: { type: AuthActionTypes.RETRIEVE_AUTHENTICATED_EPERSON, payload: token } });
const expected = cold('--b-', { b: new RetrieveAuthenticatedEpersonErrorAction(new Error('Message Error test')) });
expect(authEffects.retrieveAuthenticatedEperson$).toBeObservable(expected);
});
});
});
describe('refreshToken$', () => {
describe('when refresh token succeeded', () => {
it('should return a REFRESH_TOKEN_SUCCESS action in response to a REFRESH_TOKEN action', () => {
actions = hot('--a-', {a: {type: AuthActionTypes.REFRESH_TOKEN}});
actions = hot('--a-', { a: { type: AuthActionTypes.REFRESH_TOKEN } });
const expected = cold('--b-', {b: new RefreshTokenSuccessAction(token)});
const expected = cold('--b-', { b: new RefreshTokenSuccessAction(token) });
expect(authEffects.refreshToken$).toBeObservable(expected);
});
@@ -170,9 +222,9 @@ describe('AuthEffects', () => {
it('should return a REFRESH_TOKEN_ERROR action in response to a REFRESH_TOKEN action', () => {
spyOn((authEffects as any).authService, 'refreshAuthenticationToken').and.returnValue(observableThrow(''));
actions = hot('--a-', {a: {type: AuthActionTypes.REFRESH_TOKEN, payload: token}});
actions = hot('--a-', { a: { type: AuthActionTypes.REFRESH_TOKEN, payload: token } });
const expected = cold('--b-', {b: new RefreshTokenErrorAction()});
const expected = cold('--b-', { b: new RefreshTokenErrorAction() });
expect(authEffects.refreshToken$).toBeObservable(expected);
});
@@ -184,9 +236,9 @@ describe('AuthEffects', () => {
describe('when refresh token succeeded', () => {
it('should return a LOG_OUT_SUCCESS action in response to a LOG_OUT action', () => {
actions = hot('--a-', {a: {type: AuthActionTypes.LOG_OUT}});
actions = hot('--a-', { a: { type: AuthActionTypes.LOG_OUT } });
const expected = cold('--b-', {b: new LogOutSuccessAction()});
const expected = cold('--b-', { b: new LogOutSuccessAction() });
expect(authEffects.logOut$).toBeObservable(expected);
});
@@ -196,9 +248,9 @@ describe('AuthEffects', () => {
it('should return a REFRESH_TOKEN_ERROR action in response to a LOG_OUT action', () => {
spyOn((authEffects as any).authService, 'logout').and.returnValue(observableThrow(new Error('Message Error test')));
actions = hot('--a-', {a: {type: AuthActionTypes.LOG_OUT, payload: token}});
actions = hot('--a-', { a: { type: AuthActionTypes.LOG_OUT, payload: token } });
const expected = cold('--b-', {b: new LogOutErrorAction(new Error('Message Error test'))});
const expected = cold('--b-', { b: new LogOutErrorAction(new Error('Message Error test')) });
expect(authEffects.logOut$).toBeObservable(expected);
});