From e7f3c62fbfcc195bafe5e3329f7e2f6f9f4884b3 Mon Sep 17 00:00:00 2001 From: BRUCELLA2 Date: Fri, 22 May 2020 21:40:41 +0200 Subject: [PATCH] [Issue #270] Fix wrong message when using invalid credentials to log. --- .../user/adaptors/authentication.adaptor.spec.ts | 11 +++++++++++ .../app/user/adaptors/authentication.adaptor.ts | 7 ++++++- .../user/effects/authentication.effects.spec.ts | 15 +++++++++++++++ .../app/user/effects/authentication.effects.ts | 16 ++++++++++++++++ comixed-frontend/src/app/xhr.interceptor.ts | 2 +- 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/comixed-frontend/src/app/user/adaptors/authentication.adaptor.spec.ts b/comixed-frontend/src/app/user/adaptors/authentication.adaptor.spec.ts index 50da5fa4b..ff1aafd6b 100644 --- a/comixed-frontend/src/app/user/adaptors/authentication.adaptor.spec.ts +++ b/comixed-frontend/src/app/user/adaptors/authentication.adaptor.spec.ts @@ -206,6 +206,17 @@ describe('AuthenticationAdaptor', () => { }); }); + describe('when login failed', () => { + beforeEach(() => { + spyOn(store, 'dispatch'); + auth_adaptor.authenticationFailed(); + }); + + it('fires an action', () => { + expect(store.dispatch).toHaveBeenCalledWith(new AuthActions.AuthLoginFailed()); + }); + }); + describe('when setting a user preference', () => { beforeEach(() => { spyOn(store, 'dispatch'); diff --git a/comixed-frontend/src/app/user/adaptors/authentication.adaptor.ts b/comixed-frontend/src/app/user/adaptors/authentication.adaptor.ts index 7468c7ea9..e233a8c39 100644 --- a/comixed-frontend/src/app/user/adaptors/authentication.adaptor.ts +++ b/comixed-frontend/src/app/user/adaptors/authentication.adaptor.ts @@ -22,7 +22,7 @@ import { Roles } from 'app/models/ui/roles'; import { AppState, User } from 'app/user'; import { AuthCheckState, - AuthHideLogin, + AuthHideLogin, AuthLoginFailed, AuthLogout, AuthSetPreference, AuthShowLogin, @@ -137,6 +137,11 @@ export class AuthenticationAdaptor { this.store.dispatch(new AuthLogout()); } + authenticationFailed(): void { + this.store.dispatch(new AuthLoginFailed()); + this.store.dispatch(new AuthHideLogin()); + } + setPreference(name: string, value: string): void { this.store.dispatch(new AuthSetPreference({ name: name, value: value })); } diff --git a/comixed-frontend/src/app/user/effects/authentication.effects.spec.ts b/comixed-frontend/src/app/user/effects/authentication.effects.spec.ts index a8bef82b6..787417e7b 100644 --- a/comixed-frontend/src/app/user/effects/authentication.effects.spec.ts +++ b/comixed-frontend/src/app/user/effects/authentication.effects.spec.ts @@ -221,6 +221,21 @@ describe('AuthenticationEffects', () => { }); }); + describe('when the login failed', () => { + it('fires an action', () => { + const action = new AuthActions.AuthLoginFailed(); + const outcome = new AuthActions.AuthCheckState(); + + actions = hot('-a', { a: action }); + + const expected = cold('-b', { b: outcome }); + expect(effects.authenticationFailed$).toBeObservable(expected); + expect(messageService.add).toHaveBeenCalledWith( + objectContaining({severity: 'error' }) + ); + }); + }); + describe('when setting a preference', () => { it('fires an action one success', () => { const serviceResponse = USER; diff --git a/comixed-frontend/src/app/user/effects/authentication.effects.ts b/comixed-frontend/src/app/user/effects/authentication.effects.ts index 74394acda..65d983739 100644 --- a/comixed-frontend/src/app/user/effects/authentication.effects.ts +++ b/comixed-frontend/src/app/user/effects/authentication.effects.ts @@ -150,6 +150,22 @@ export class AuthenticationEffects { map(() => new AuthCheckState()) ); + @Effect() + authenticationFailed$: Observable = this.actions$.pipe( + ofType(AuthenticationActionTypes.AUTH_LOGIN_FAILED), + tap(action => this.logger.debug('effect: logging failed:', action)), + tap(() => this.tokenService.signout()), + tap(() => + this.messageService.add({ + severity: 'error', + detail: this.translateService.instant( + 'authentication-effects.submit-login-data.failure.detail' + ) + }) + ), + map(() => new AuthCheckState()) + ); + @Effect() setPreference$: Observable = this.actions$.pipe( ofType(AuthenticationActionTypes.AUTH_SET_PREFERENCE), diff --git a/comixed-frontend/src/app/xhr.interceptor.ts b/comixed-frontend/src/app/xhr.interceptor.ts index 3baba9db9..650c18c1a 100644 --- a/comixed-frontend/src/app/xhr.interceptor.ts +++ b/comixed-frontend/src/app/xhr.interceptor.ts @@ -77,7 +77,7 @@ export class XhrInterceptor implements HttpInterceptor { return; case 401: this.logger.error('[XHR] user not authenticated:', error); - this.authenticationAdaptor.startLogout(); + this.authenticationAdaptor.authenticationFailed(); this.router.navigateByUrl('/'); break; default: