aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/app/user/auth.guard.spec.ts
blob: 6a36fea68fc6452db735f8fc24ab7e90d83fdfd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Observable, of } from 'rxjs';

import { AuthGuard, AuthStrategy } from './auth.guard';
import { UserInfo } from './entities';

describe('AuthGuard', () => {
  class ConfiurableAuthGuard extends AuthGuard {
    constructor(mockInternalUserService: any) {
      super(mockInternalUserService);
    }

    authStrategy: AuthStrategy = 'all';
  }

  let mockUserService: { userInfo$: Observable<UserInfo | null> };
  let guard: ConfiurableAuthGuard;
  let onAuthFialedSpy: jasmine.Spy;

  const mockRoles = ['role1', 'role2'];

  interface ActivateResultMap {
    nologin: boolean;
    loginWithNoRole: boolean;
    loginWithMockRoles: boolean;
  }


  function createTest(authStrategy: AuthStrategy, result: ActivateResultMap): () => void {
    return () => {
      guard.authStrategy = authStrategy;

      function testWith(userInfo: UserInfo | null, r: boolean) {
        mockUserService.userInfo$ = of(userInfo);

        const rawResult = guard.canActivate(<any>null, <any>null);
        if (typeof rawResult === 'boolean') {
          expect(rawResult).toBe(r);
        } else if (rawResult instanceof Observable) {
          rawResult.subscribe(next => expect(next).toBe(r));
        } else {
          throw new Error('Unsupported return type.');
        }
      }

      testWith(null, result.nologin);
      testWith({ username: 'user', roles: [] }, result.loginWithNoRole);
      testWith({ username: 'user', roles: mockRoles }, result.loginWithMockRoles);
    };
  }

  beforeEach(() => {
    mockUserService = { userInfo$: of(null) };
    guard = new ConfiurableAuthGuard(mockUserService);
    onAuthFialedSpy = spyOn(guard, 'onAuthFailed');
  });


  it('all should work', createTest('all', { nologin: true, loginWithNoRole: true, loginWithMockRoles: true }));
  it('require login should work', createTest('requirelogin', { nologin: false, loginWithNoRole: true, loginWithMockRoles: true }));
  it('require no login should work', createTest('requirenologin', { nologin: true, loginWithNoRole: false, loginWithMockRoles: false }));
  it('good roles should work', createTest(mockRoles, { nologin: false, loginWithNoRole: false, loginWithMockRoles: true }));
  it('bad roles should work', createTest(['role3'], { nologin: false, loginWithNoRole: false, loginWithMockRoles: false }));

  it('auth failed callback should be called', () => {
    guard.authStrategy = 'requirelogin';
    (<Observable<boolean>>guard.canActivate(<any>null, <any>null)).subscribe();
    expect(onAuthFialedSpy).toHaveBeenCalled();
  });
});