Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1273 no js issue #1275

Merged
merged 3 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions src/app/core/auth/auth.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,10 @@ export class ResetAuthenticationMessagesAction implements Action {
export class RetrieveAuthMethodsAction implements Action {
public type: string = AuthActionTypes.RETRIEVE_AUTH_METHODS;

payload: {
status: AuthStatus;
blocking: boolean;
};
payload: AuthStatus;

constructor(status: AuthStatus, blocking: boolean) {
this.payload = { status, blocking };
constructor(authStatus: AuthStatus) {
this.payload = authStatus;
}
}

Expand All @@ -311,14 +308,10 @@ export class RetrieveAuthMethodsAction implements Action {
*/
export class RetrieveAuthMethodsSuccessAction implements Action {
public type: string = AuthActionTypes.RETRIEVE_AUTH_METHODS_SUCCESS;
payload: AuthMethod[];

payload: {
authMethods: AuthMethod[];
blocking: boolean;
};

constructor(authMethods: AuthMethod[], blocking: boolean ) {
this.payload = { authMethods, blocking };
constructor(authMethods: AuthMethod[] ) {
this.payload = authMethods;
}
}

Expand All @@ -329,12 +322,6 @@ export class RetrieveAuthMethodsSuccessAction implements Action {
*/
export class RetrieveAuthMethodsErrorAction implements Action {
public type: string = AuthActionTypes.RETRIEVE_AUTH_METHODS_ERROR;

payload: boolean;

constructor(blocking: boolean) {
this.payload = blocking;
}
}

/**
Expand Down
109 changes: 19 additions & 90 deletions src/app/core/auth/auth.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ describe('AuthEffects', () => {
let initialState;
let token;
let store: MockStore<AppState>;
let authStatus;

function init() {
authServiceStub = new AuthServiceStub();
token = authServiceStub.getToken();
authStatus = Object.assign(new AuthStatus(), {});
initialState = {
core: {
auth: {
Expand Down Expand Up @@ -219,38 +217,16 @@ describe('AuthEffects', () => {
expect(authEffects.checkTokenCookie$).toBeObservable(expected);
});

describe('on CSR', () => {
it('should return a RETRIEVE_AUTH_METHODS action in response to a CHECK_AUTHENTICATION_TOKEN_COOKIE action when authenticated is false', () => {
spyOn((authEffects as any).authService, 'checkAuthenticationCookie').and.returnValue(
observableOf(
{ authenticated: false })
);
spyOn((authEffects as any).authService, 'getRetrieveAuthMethodsAction').and.returnValue(
new RetrieveAuthMethodsAction({ authenticated: false } as AuthStatus, false)
);
actions = hot('--a-', { a: { type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN_COOKIE } });

const expected = cold('--b-', { b: new RetrieveAuthMethodsAction({ authenticated: false } as AuthStatus, false) });

expect(authEffects.checkTokenCookie$).toBeObservable(expected);
});
});

describe('on SSR', () => {
it('should return a RETRIEVE_AUTH_METHODS action in response to a CHECK_AUTHENTICATION_TOKEN_COOKIE action when authenticated is false', () => {
spyOn((authEffects as any).authService, 'checkAuthenticationCookie').and.returnValue(
observableOf(
{ authenticated: false })
);
spyOn((authEffects as any).authService, 'getRetrieveAuthMethodsAction').and.returnValue(
new RetrieveAuthMethodsAction({ authenticated: false } as AuthStatus, true)
);
actions = hot('--a-', { a: { type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN_COOKIE } });
it('should return a RETRIEVE_AUTH_METHODS action in response to a CHECK_AUTHENTICATION_TOKEN_COOKIE action when authenticated is false', () => {
spyOn((authEffects as any).authService, 'checkAuthenticationCookie').and.returnValue(
observableOf(
{ authenticated: false })
);
actions = hot('--a-', { a: { type: AuthActionTypes.CHECK_AUTHENTICATION_TOKEN_COOKIE } });

const expected = cold('--b-', { b: new RetrieveAuthMethodsAction({ authenticated: false } as AuthStatus, true) });
const expected = cold('--b-', { b: new RetrieveAuthMethodsAction({ authenticated: false } as AuthStatus) });

expect(authEffects.checkTokenCookie$).toBeObservable(expected);
});
expect(authEffects.checkTokenCookie$).toBeObservable(expected);
});
});

Expand Down Expand Up @@ -383,74 +359,27 @@ describe('AuthEffects', () => {

describe('retrieveMethods$', () => {

describe('on CSR', () => {
describe('when retrieve authentication methods succeeded', () => {
it('should return a RETRIEVE_AUTH_METHODS_SUCCESS action in response to a RETRIEVE_AUTH_METHODS action', () => {
actions = hot('--a-', { a:
{
type: AuthActionTypes.RETRIEVE_AUTH_METHODS,
payload: { status: authStatus, blocking: false}
}
});
describe('when retrieve authentication methods succeeded', () => {
it('should return a RETRIEVE_AUTH_METHODS_SUCCESS action in response to a RETRIEVE_AUTH_METHODS action', () => {
actions = hot('--a-', { a: { type: AuthActionTypes.RETRIEVE_AUTH_METHODS } });

const expected = cold('--b-', { b: new RetrieveAuthMethodsSuccessAction(authMethodsMock, false) });
const expected = cold('--b-', { b: new RetrieveAuthMethodsSuccessAction(authMethodsMock) });

expect(authEffects.retrieveMethods$).toBeObservable(expected);
});
});

describe('when retrieve authentication methods failed', () => {
it('should return a RETRIEVE_AUTH_METHODS_ERROR action in response to a RETRIEVE_AUTH_METHODS action', () => {
spyOn((authEffects as any).authService, 'retrieveAuthMethodsFromAuthStatus').and.returnValue(observableThrow(''));

actions = hot('--a-', { a:
{
type: AuthActionTypes.RETRIEVE_AUTH_METHODS,
payload: { status: authStatus, blocking: false}
}
});

const expected = cold('--b-', { b: new RetrieveAuthMethodsErrorAction(false) });

expect(authEffects.retrieveMethods$).toBeObservable(expected);
});
expect(authEffects.retrieveMethods$).toBeObservable(expected);
});
});

describe('on SSR', () => {
describe('when retrieve authentication methods succeeded', () => {
it('should return a RETRIEVE_AUTH_METHODS_SUCCESS action in response to a RETRIEVE_AUTH_METHODS action', () => {
actions = hot('--a-', { a:
{
type: AuthActionTypes.RETRIEVE_AUTH_METHODS,
payload: { status: authStatus, blocking: true}
}
});

const expected = cold('--b-', { b: new RetrieveAuthMethodsSuccessAction(authMethodsMock, true) });
describe('when retrieve authentication methods failed', () => {
it('should return a RETRIEVE_AUTH_METHODS_ERROR action in response to a RETRIEVE_AUTH_METHODS action', () => {
spyOn((authEffects as any).authService, 'retrieveAuthMethodsFromAuthStatus').and.returnValue(observableThrow(''));

expect(authEffects.retrieveMethods$).toBeObservable(expected);
});
});
actions = hot('--a-', { a: { type: AuthActionTypes.RETRIEVE_AUTH_METHODS } });

describe('when retrieve authentication methods failed', () => {
it('should return a RETRIEVE_AUTH_METHODS_ERROR action in response to a RETRIEVE_AUTH_METHODS action', () => {
spyOn((authEffects as any).authService, 'retrieveAuthMethodsFromAuthStatus').and.returnValue(observableThrow(''));

actions = hot('--a-', { a:
{
type: AuthActionTypes.RETRIEVE_AUTH_METHODS,
payload: { status: authStatus, blocking: true}
}
});
const expected = cold('--b-', { b: new RetrieveAuthMethodsErrorAction() });

const expected = cold('--b-', { b: new RetrieveAuthMethodsErrorAction(true) });

expect(authEffects.retrieveMethods$).toBeObservable(expected);
});
expect(authEffects.retrieveMethods$).toBeObservable(expected);
});
});

});

describe('clearInvalidTokenOnRehydrate$', () => {
Expand Down
18 changes: 10 additions & 8 deletions src/app/core/auth/auth.effects.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Injectable, NgZone } from '@angular/core';

import {
asyncScheduler,
combineLatest as observableCombineLatest,
Observable,
of as observableOf,
timer,
asyncScheduler, queueScheduler
queueScheduler,
timer
} from 'rxjs';
import { catchError, filter, map, switchMap, take, tap, observeOn } from 'rxjs/operators';
import { catchError, filter, map, observeOn, switchMap, take, tap } from 'rxjs/operators';
// import @ngrx
import { Actions, Effect, ofType } from '@ngrx/effects';
import { Action, select, Store } from '@ngrx/store';
Expand Down Expand Up @@ -43,7 +44,8 @@ import {
RetrieveAuthMethodsAction,
RetrieveAuthMethodsErrorAction,
RetrieveAuthMethodsSuccessAction,
RetrieveTokenAction, SetUserAsIdleAction
RetrieveTokenAction,
SetUserAsIdleAction
} from './auth.actions';
import { hasValue } from '../../shared/empty.util';
import { environment } from '../../../environments/environment';
Expand Down Expand Up @@ -161,7 +163,7 @@ export class AuthEffects {
if (response.authenticated) {
return new RetrieveTokenAction();
} else {
return this.authService.getRetrieveAuthMethodsAction(response);
return new RetrieveAuthMethodsAction(response);
}
}),
catchError((error) => observableOf(new AuthenticatedErrorAction(error)))
Expand Down Expand Up @@ -250,10 +252,10 @@ export class AuthEffects {
.pipe(
ofType(AuthActionTypes.RETRIEVE_AUTH_METHODS),
switchMap((action: RetrieveAuthMethodsAction) => {
return this.authService.retrieveAuthMethodsFromAuthStatus(action.payload.status)
return this.authService.retrieveAuthMethodsFromAuthStatus(action.payload)
.pipe(
map((authMethodModels: AuthMethod[]) => new RetrieveAuthMethodsSuccessAction(authMethodModels, action.payload.blocking)),
catchError((error) => observableOf(new RetrieveAuthMethodsErrorAction(action.payload.blocking)))
map((authMethodModels: AuthMethod[]) => new RetrieveAuthMethodsSuccessAction(authMethodModels)),
catchError((error) => observableOf(new RetrieveAuthMethodsErrorAction()))
);
})
);
Expand Down
61 changes: 7 additions & 54 deletions src/app/core/auth/auth.reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import {
RetrieveAuthMethodsAction,
RetrieveAuthMethodsErrorAction,
RetrieveAuthMethodsSuccessAction,
SetRedirectUrlAction, SetUserAsIdleAction, UnsetUserAsIdleAction
SetRedirectUrlAction,
SetUserAsIdleAction,
UnsetUserAsIdleAction
} from './auth.actions';
import { AuthTokenInfo } from './models/auth-token-info.model';
import { EPersonMock } from '../../shared/testing/eperson.mock';
Expand Down Expand Up @@ -551,7 +553,7 @@ describe('authReducer', () => {
authMethods: [],
idle: false
};
const action = new RetrieveAuthMethodsAction(new AuthStatus(), true);
const action = new RetrieveAuthMethodsAction(new AuthStatus());
const newState = authReducer(initialState, action);
state = {
authenticated: false,
Expand All @@ -577,7 +579,7 @@ describe('authReducer', () => {
new AuthMethod(AuthMethodType.Password),
new AuthMethod(AuthMethodType.Shibboleth, 'location')
];
const action = new RetrieveAuthMethodsSuccessAction(authMethods, false);
const action = new RetrieveAuthMethodsSuccessAction(authMethods);
const newState = authReducer(initialState, action);
state = {
authenticated: false,
Expand All @@ -590,7 +592,7 @@ describe('authReducer', () => {
expect(newState).toEqual(state);
});

it('should properly set the state, in response to a RETRIEVE_AUTH_METHODS_SUCCESS action with blocking as true', () => {
it('should properly set the state, in response to a RETRIEVE_AUTH_METHODS_ERROR action', () => {
initialState = {
authenticated: false,
loaded: false,
Expand All @@ -599,34 +601,8 @@ describe('authReducer', () => {
authMethods: [],
idle: false
};
const authMethods = [
new AuthMethod(AuthMethodType.Password),
new AuthMethod(AuthMethodType.Shibboleth, 'location')
];
const action = new RetrieveAuthMethodsSuccessAction(authMethods, true);
const newState = authReducer(initialState, action);
state = {
authenticated: false,
loaded: false,
blocking: true,
loading: false,
authMethods: authMethods,
idle: false
};
expect(newState).toEqual(state);
});

it('should properly set the state, in response to a RETRIEVE_AUTH_METHODS_ERROR action ', () => {
initialState = {
authenticated: false,
loaded: false,
blocking: true,
loading: true,
authMethods: [],
idle: false
};

const action = new RetrieveAuthMethodsErrorAction(false);
const action = new RetrieveAuthMethodsErrorAction();
const newState = authReducer(initialState, action);
state = {
authenticated: false,
Expand Down Expand Up @@ -680,27 +656,4 @@ describe('authReducer', () => {
};
expect(newState).toEqual(state);
});

it('should properly set the state, in response to a RETRIEVE_AUTH_METHODS_ERROR action with blocking as true', () => {
initialState = {
authenticated: false,
loaded: false,
blocking: true,
loading: true,
authMethods: [],
idle: false
};

const action = new RetrieveAuthMethodsErrorAction(true);
const newState = authReducer(initialState, action);
state = {
authenticated: false,
loaded: false,
blocking: true,
loading: false,
authMethods: [new AuthMethod(AuthMethodType.Password)],
idle: false
};
expect(newState).toEqual(state);
});
});
7 changes: 3 additions & 4 deletions src/app/core/auth/auth.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
RedirectWhenTokenExpiredAction,
RefreshTokenSuccessAction,
RetrieveAuthenticatedEpersonSuccessAction,
RetrieveAuthMethodsErrorAction,
RetrieveAuthMethodsSuccessAction,
SetRedirectUrlAction
} from './auth.actions';
Expand Down Expand Up @@ -217,14 +216,14 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
case AuthActionTypes.RETRIEVE_AUTH_METHODS_SUCCESS:
return Object.assign({}, state, {
loading: false,
blocking: (action as RetrieveAuthMethodsSuccessAction).payload.blocking,
authMethods: (action as RetrieveAuthMethodsSuccessAction).payload.authMethods
blocking: false,
authMethods: (action as RetrieveAuthMethodsSuccessAction).payload
});

case AuthActionTypes.RETRIEVE_AUTH_METHODS_ERROR:
return Object.assign({}, state, {
loading: false,
blocking: (action as RetrieveAuthMethodsErrorAction).payload,
blocking: false,
authMethods: [new AuthMethod(AuthMethodType.Password)]
});

Expand Down
Loading