Skip to content

Commit a5f1063

Browse files
committed
test(frontend): add some tests to auth effects
1 parent a98aca1 commit a5f1063

File tree

4 files changed

+77
-15
lines changed

4 files changed

+77
-15
lines changed

src/main/typescript/app/core/store/auth/auth.actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ export class AuthErrorAction implements Action {
6262
}
6363

6464
export class ClearAuthErrorAction implements Action {
65-
type = ActionTypes.CLEAR_AUTH_STATE;
65+
type = ActionTypes.CLEAR_AUTH_ERROR;
6666

6767
constructor() {
6868
}
6969
}
7070

7171

7272
export class ClearAuthStateAction implements Action {
73-
type = ActionTypes.CLEAR_AUTH_ERROR;
73+
type = ActionTypes.CLEAR_AUTH_STATE;
7474

7575
constructor() {
7676
}

src/main/typescript/app/core/store/auth/auth.effects.spec.ts

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
/* tslint:disable:no-unused-variable */
2-
import {TestBed, inject} from '@angular/core/testing';
31
import {Http, BaseRequestOptions, ConnectionBackend, RequestOptions} from '@angular/http';
42
import {MockBackend} from '@angular/http/testing';
5-
import {CoreModule} from '../../core.module';
63
import {AuthEffects} from './auth.effects';
4+
import {EffectsTestingModule, EffectsRunner} from '@ngrx/effects/testing';
5+
import {LogoutAction, ActionTypes, LoadAndProcessTokenAction, AuthSuccessAction} from './auth.actions';
6+
import {TestBed, inject} from '@angular/core/testing';
7+
import {routerActions} from '@ngrx/router-store';
8+
79

810
describe('Effects: Auth', () => {
911

12+
let runner: EffectsRunner = null;
13+
let authEffects: AuthEffects = null;
14+
1015
beforeEach(() => {
1116
TestBed.configureTestingModule({
1217
imports: [
13-
CoreModule
18+
EffectsTestingModule
1419
],
1520
providers: [
1621
BaseRequestOptions,
@@ -27,9 +32,60 @@ describe('Effects: Auth', () => {
2732
});
2833
});
2934

35+
beforeEach(inject([EffectsRunner, AuthEffects], (_runner: EffectsRunner, _authEffects: AuthEffects) => {
36+
runner = _runner;
37+
authEffects = _authEffects;
38+
_authEffects.jwtToken = null;
39+
}
40+
));
41+
3042
it('should exist', inject([AuthEffects], (service: AuthEffects) => {
3143
expect(service).toBeTruthy();
3244
}));
3345

46+
it('should return a CLEAR_AUTH_STATE action after logging out', () => {
47+
runner.queue(new LogoutAction());
48+
49+
authEffects.logout$.subscribe(result => {
50+
expect(result.type).toEqual(ActionTypes.CLEAR_AUTH_STATE);
51+
});
52+
53+
});
54+
55+
it('should return a PROCESS_TOKEN action after LoadAndProcessTokenAction when token is set', () => {
56+
authEffects.jwtToken = 'NON_EMPTY';
57+
runner.queue(new LoadAndProcessTokenAction());
58+
authEffects.loadAndProcessToken$.subscribe(result => {
59+
expect(result.type).toEqual(ActionTypes.PROCESS_TOKEN);
60+
});
61+
});
62+
63+
it('should return a no action after LoadAndProcessTokenAction when token is not set', () => {
64+
authEffects.jwtToken = null;
65+
runner.queue(new LoadAndProcessTokenAction());
66+
67+
authEffects.loadAndProcessToken$.subscribe(result => {
68+
expect(result).toBeFalsy();
69+
expect(result).toBeTruthy();
70+
});
71+
});
72+
73+
it('should return actions after auth success', () => {
74+
authEffects.jwtToken = null;
75+
runner.queue(new AuthSuccessAction('TOKEN'));
76+
let eventCount = 0;
77+
authEffects.authSuccess$.subscribe(result => {
78+
eventCount++;
79+
if (eventCount === 1) {
80+
expect(result.type).toEqual(routerActions.GO);
81+
}
82+
83+
if (eventCount === 2) {
84+
expect(result.type).toEqual(ActionTypes.PROCESS_TOKEN);
85+
}
86+
87+
});
88+
});
89+
3490

3591
});

src/main/typescript/app/core/store/auth/auth.effects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class AuthEffects {
7171
});
7272

7373
@LocalStorage('token')
74-
private jwtToken: string;
74+
public jwtToken: string;
7575

7676
constructor(private http: Http, private actions$: Actions) {
7777
}

src/main/typescript/app/core/store/auth/auth.reducer.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ import 'rxjs/add/operator/let';
44
import {AuthState, initialState} from './auth.state';
55
import {Actions, ActionTypes, ProcessTokenAction, AuthErrorAction} from './auth.actions';
66

7-
export function decodeAccessToken(access_token: string) {
8-
return JSON.parse(window.atob(access_token.split('.')[1]));
7+
export function decodeAccessToken(access_token: string) {
8+
try {
9+
return JSON.parse(window.atob(access_token.split('.')[1]));
10+
} catch (e) {
11+
console.log(`access token '${access_token}' is not correct`);
12+
return null;
13+
}
14+
915
}
1016

1117
export function authReducer(state = initialState, action: Actions): AuthState {
@@ -17,12 +23,12 @@ export function authReducer(state = initialState, action: Actions): AuthState {
1723
case ActionTypes.PROCESS_TOKEN:
1824
const processTokenAction: ProcessTokenAction = action as ProcessTokenAction;
1925
const userData = decodeAccessToken(processTokenAction.payload);
20-
return Object.assign({}, initialState, {
21-
authenticated: true,
22-
tokenExpirationDate: new Date(userData.exp * 1000),
23-
userData: userData,
24-
jwtToken: processTokenAction.payload
25-
});
26+
return (!userData) ? state : Object.assign({}, initialState, {
27+
authenticated: true,
28+
tokenExpirationDate: new Date(userData.exp * 1000),
29+
userData: userData,
30+
jwtToken: processTokenAction.payload
31+
});
2632

2733
case ActionTypes.AUTH_ERROR:
2834
const authErrorAction: AuthErrorAction = action as AuthErrorAction;

0 commit comments

Comments
 (0)