Skip to content

Commit

Permalink
[Issue comixed#270] Fix wrong message when using invalid credentials …
Browse files Browse the repository at this point in the history
…to log.
  • Loading branch information
BRUCELLA2 committed May 22, 2020
1 parent c519a50 commit e7f3c62
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
Expand Up @@ -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');
Expand Down
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }));
}
Expand Down
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions comixed-frontend/src/app/user/effects/authentication.effects.ts
Expand Up @@ -150,6 +150,22 @@ export class AuthenticationEffects {
map(() => new AuthCheckState())
);

@Effect()
authenticationFailed$: Observable<Action> = 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<Action> = this.actions$.pipe(
ofType(AuthenticationActionTypes.AUTH_SET_PREFERENCE),
Expand Down
2 changes: 1 addition & 1 deletion comixed-frontend/src/app/xhr.interceptor.ts
Expand Up @@ -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:
Expand Down

0 comments on commit e7f3c62

Please sign in to comment.