Skip to content
Snippets Groups Projects
auth.reducer.spec.ts 12.3 KiB
Newer Older
import { authReducer, AuthState } from './auth.reducer';
Giuseppe Digilio's avatar
Giuseppe Digilio committed
import {
  AddAuthenticationMessageAction,
  AuthenticateAction,
  AuthenticatedAction,
  AuthenticatedErrorAction,
  AuthenticatedSuccessAction,
  AuthenticationErrorAction,
  AuthenticationSuccessAction,
  CheckAuthenticationTokenAction,
  CheckAuthenticationTokenErrorAction,
  LogOutAction,
  LogOutErrorAction,
  LogOutSuccessAction,
Giuseppe Digilio's avatar
Giuseppe Digilio committed
  RedirectWhenAuthenticationIsRequiredAction,
  RedirectWhenTokenExpiredAction,
Giuseppe Digilio's avatar
Giuseppe Digilio committed
  RefreshTokenAction,
  RefreshTokenErrorAction,
Giuseppe Digilio's avatar
Giuseppe Digilio committed
  RefreshTokenSuccessAction,
  ResetAuthenticationMessagesAction, RetrieveAuthenticatedEpersonErrorAction, RetrieveAuthenticatedEpersonSuccessAction,
Giuseppe Digilio's avatar
Giuseppe Digilio committed
  SetRedirectUrlAction
Giuseppe Digilio's avatar
Giuseppe Digilio committed
} from './auth.actions';
import { AuthTokenInfo } from './models/auth-token-info.model';
lotte's avatar
lotte committed
import { EPersonMock } from '../../shared/testing/eperson.mock';
Giuseppe Digilio's avatar
Giuseppe Digilio committed

describe('authReducer', () => {

  let initialState: AuthState;
  let state: AuthState;
  const mockTokenInfo = new AuthTokenInfo('test_token');
  const mockError = new Error('Test error message');

  it('should properly set the state, in response to a AUTHENTICATE action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      loading: false,
    };
    const action = new AuthenticateAction('user', 'password');
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      loaded: false,
      error: undefined,
      loading: true,
      info: undefined
    };

    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a AUTHENTICATE_SUCCESS action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      error: undefined,
      loading: true,
      info: undefined
    };
    const action = new AuthenticationSuccessAction(mockTokenInfo);
    const newState = authReducer(initialState, action);

    expect(newState).toEqual(initialState);
  });

  it('should properly set the state, in response to a AUTHENTICATE_ERROR action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      error: undefined,
      loading: true,
      info: undefined
    };
    const action = new AuthenticationErrorAction(mockError);
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      loaded: false,
      loading: false,
      info: undefined,
      authToken: undefined,
      error: 'Test error message'
    };

    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a AUTHENTICATED action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      error: undefined,
      loading: true,
      info: undefined
    };
    const action = new AuthenticatedAction(mockTokenInfo);
    const newState = authReducer(initialState, action);

    expect(newState).toEqual(initialState);
  });

  it('should properly set the state, in response to a AUTHENTICATED_SUCCESS action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      error: undefined,
      loading: true,
      info: undefined
    };
    const action = new AuthenticatedSuccessAction(true, mockTokenInfo, EPersonMock._links.self.href);
Giuseppe Digilio's avatar
Giuseppe Digilio committed
    const newState = authReducer(initialState, action);
    state = {
      authenticated: true,
      authToken: mockTokenInfo,
Giuseppe Digilio's avatar
Giuseppe Digilio committed
      error: undefined,
      loading: true,
      info: undefined
Giuseppe Digilio's avatar
Giuseppe Digilio committed
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a AUTHENTICATED_ERROR action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      error: undefined,
      loading: true,
      info: undefined
    };
    const action = new AuthenticatedErrorAction(mockError);
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      authToken: undefined,
      error: 'Test error message',
      loaded: true,
      loading: false,
      info: undefined
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a CHECK_AUTHENTICATION_TOKEN action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      loading: false,
    };
    const action = new CheckAuthenticationTokenAction();
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      loaded: false,
      loading: true,
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a CHECK_AUTHENTICATION_TOKEN_ERROR action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      loading: true,
    };
    const action = new CheckAuthenticationTokenErrorAction();
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      loaded: false,
      loading: false,
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a LOG_OUT action', () => {
    initialState = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: true,
      error: undefined,
      loading: false,
      info: undefined,
lotte's avatar
lotte committed
      user: EPersonMock
Giuseppe Digilio's avatar
Giuseppe Digilio committed
    };

    const action = new LogOutAction();
    const newState = authReducer(initialState, action);

    expect(newState).toEqual(initialState);
  });

  it('should properly set the state, in response to a LOG_OUT_SUCCESS action', () => {
    initialState = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: true,
      error: undefined,
      loading: false,
      info: undefined,
lotte's avatar
lotte committed
      user: EPersonMock
Giuseppe Digilio's avatar
Giuseppe Digilio committed
    };

    const action = new LogOutSuccessAction();
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      authToken: undefined,
      error: undefined,
      loaded: false,
      loading: false,
      info: undefined,
      refreshing: false,
      user: undefined
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a LOG_OUT_ERROR action', () => {
    initialState = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: true,
      error: undefined,
      loading: false,
      info: undefined,
lotte's avatar
lotte committed
      user: EPersonMock
Giuseppe Digilio's avatar
Giuseppe Digilio committed
    };

    const action = new LogOutErrorAction(mockError);
    const newState = authReducer(initialState, action);
    state = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: true,
      error: 'Test error message',
      loading: false,
      info: undefined,
lotte's avatar
lotte committed
      user: EPersonMock
Giuseppe Digilio's avatar
Giuseppe Digilio committed
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a RETRIEVE_AUTHENTICATED_EPERSON_SUCCESS action', () => {
    initialState = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: false,
      error: undefined,
      loading: true,
      info: undefined
    };
    const action = new RetrieveAuthenticatedEpersonSuccessAction(EPersonMock);
    const newState = authReducer(initialState, action);
    state = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: true,
      error: undefined,
      loading: false,
      info: undefined,
      user: EPersonMock
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a RETRIEVE_AUTHENTICATED_EPERSON_ERROR action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      error: undefined,
      loading: true,
      info: undefined
    };
    const action = new RetrieveAuthenticatedEpersonErrorAction(mockError);
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      authToken: undefined,
      error: 'Test error message',
      loaded: true,
      loading: false,
      info: undefined
    };
    expect(newState).toEqual(state);
  });

Giuseppe Digilio's avatar
Giuseppe Digilio committed
  it('should properly set the state, in response to a REFRESH_TOKEN action', () => {
    initialState = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: true,
      error: undefined,
      loading: false,
      info: undefined,
lotte's avatar
lotte committed
      user: EPersonMock
Giuseppe Digilio's avatar
Giuseppe Digilio committed
    };
    const newTokenInfo = new AuthTokenInfo('Refreshed token');
    const action = new RefreshTokenAction(newTokenInfo);
    const newState = authReducer(initialState, action);
    state = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: true,
      error: undefined,
      loading: false,
      info: undefined,
lotte's avatar
lotte committed
      user: EPersonMock,
Giuseppe Digilio's avatar
Giuseppe Digilio committed
      refreshing: true
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a REFRESH_TOKEN_SUCCESS action', () => {
    initialState = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: true,
      error: undefined,
      loading: false,
      info: undefined,
lotte's avatar
lotte committed
      user: EPersonMock,
Giuseppe Digilio's avatar
Giuseppe Digilio committed
      refreshing: true
    };
    const newTokenInfo = new AuthTokenInfo('Refreshed token');
    const action = new RefreshTokenSuccessAction(newTokenInfo);
    const newState = authReducer(initialState, action);
    state = {
      authenticated: true,
      authToken: newTokenInfo,
      loaded: true,
      error: undefined,
      loading: false,
      info: undefined,
lotte's avatar
lotte committed
      user: EPersonMock,
Giuseppe Digilio's avatar
Giuseppe Digilio committed
      refreshing: false
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a REFRESH_TOKEN_ERROR action', () => {
    initialState = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: true,
      error: undefined,
      loading: false,
      info: undefined,
lotte's avatar
lotte committed
      user: EPersonMock,
Giuseppe Digilio's avatar
Giuseppe Digilio committed
      refreshing: true
    };
    const action = new RefreshTokenErrorAction();
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      authToken: undefined,
      error: undefined,
      loaded: false,
      loading: false,
      info: undefined,
      refreshing: false,
      user: undefined
    };
    expect(newState).toEqual(state);
  });

  beforeEach(() => {
    initialState = {
      authenticated: true,
      authToken: mockTokenInfo,
      loaded: true,
      error: undefined,
      loading: false,
      info: undefined,
lotte's avatar
lotte committed
      user: EPersonMock
Giuseppe Digilio's avatar
Giuseppe Digilio committed
    };

    state = {
      authenticated: false,
      authToken: undefined,
      loaded: false,
      loading: false,
      error: undefined,
      info: 'Message',
      user: undefined
    };
  });

  it('should properly set the state, in response to a REDIRECT_AUTHENTICATION_REQUIRED action', () => {
    const action = new RedirectWhenAuthenticationIsRequiredAction('Message');
    const newState = authReducer(initialState, action);
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a REDIRECT_TOKEN_EXPIRED action', () => {
    const action = new RedirectWhenTokenExpiredAction('Message');
    const newState = authReducer(initialState, action);
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a ADD_MESSAGE action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      loading: false,
    };
    const action = new AddAuthenticationMessageAction('Message');
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      loaded: false,
      loading: false,
      info: 'Message'
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a RESET_MESSAGES action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      loading: false,
      error: 'Error',
      info: 'Message'
    };
    const action = new ResetAuthenticationMessagesAction();
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      loaded: false,
      loading: false,
      error: undefined,
      info: undefined
    };
    expect(newState).toEqual(state);
  });

  it('should properly set the state, in response to a SET_REDIRECT_URL action', () => {
    initialState = {
      authenticated: false,
      loaded: false,
      loading: false
    };
    const action = new SetRedirectUrlAction('redirect.url');
    const newState = authReducer(initialState, action);
    state = {
      authenticated: false,
      loaded: false,
      loading: false,
      redirectUrl: 'redirect.url'
    };
    expect(newState).toEqual(state);
  });
});