|
1 | 1 | import { TestBed } from '@angular/core/testing'; |
| 2 | +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
2 | 3 | import { AuthService } from './auth.service'; |
3 | 4 |
|
4 | 5 | describe('AuthService', () => { |
5 | 6 | let service: AuthService; |
| 7 | + let httpMock: HttpTestingController; |
6 | 8 | const sessionUserKey = 'dsomm.auth.currentUser'; |
| 9 | + const authUsers = [ |
| 10 | + { username: 'admin', password: 'dsomm-admin' }, |
| 11 | + { username: 'viewer', password: 'dsomm-view' }, |
| 12 | + ]; |
7 | 13 |
|
8 | 14 | beforeEach(() => { |
9 | | - TestBed.configureTestingModule({}); |
| 15 | + TestBed.configureTestingModule({ |
| 16 | + imports: [HttpClientTestingModule], |
| 17 | + }); |
10 | 18 | service = TestBed.inject(AuthService); |
| 19 | + httpMock = TestBed.inject(HttpTestingController); |
11 | 20 | sessionStorage.removeItem(sessionUserKey); |
12 | 21 | }); |
13 | 22 |
|
14 | 23 | afterEach(() => { |
| 24 | + httpMock.verify(); |
15 | 25 | sessionStorage.removeItem(sessionUserKey); |
16 | 26 | }); |
17 | 27 |
|
18 | | - it('logs in a static user with valid credentials', () => { |
| 28 | + it('logs in a configured user with valid credentials', async () => { |
| 29 | + await loadAuthConfig(); |
| 30 | + |
19 | 31 | const loggedIn = service.login('admin', 'dsomm-admin'); |
20 | 32 |
|
21 | 33 | expect(loggedIn).toBeTrue(); |
22 | 34 | expect(service.isAuthenticated()).toBeTrue(); |
23 | 35 | expect(service.getCurrentUser()).toBe('admin'); |
24 | 36 | }); |
25 | 37 |
|
26 | | - it('rejects invalid credentials', () => { |
| 38 | + it('rejects invalid credentials', async () => { |
| 39 | + await loadAuthConfig(); |
| 40 | + |
27 | 41 | const loggedIn = service.login('admin', 'wrong-password'); |
28 | 42 |
|
29 | 43 | expect(loggedIn).toBeFalse(); |
30 | 44 | expect(service.isAuthenticated()).toBeFalse(); |
31 | 45 | expect(service.getCurrentUser()).toBeNull(); |
32 | 46 | }); |
33 | 47 |
|
34 | | - it('clears the authenticated user on logout', () => { |
| 48 | + it('clears the authenticated user on logout', async () => { |
| 49 | + await loadAuthConfig(); |
| 50 | + |
35 | 51 | service.login('viewer', 'dsomm-view'); |
36 | 52 |
|
37 | 53 | service.logout(); |
38 | 54 |
|
39 | 55 | expect(service.isAuthenticated()).toBeFalse(); |
40 | 56 | expect(service.getCurrentUser()).toBeNull(); |
41 | 57 | }); |
| 58 | + |
| 59 | + it('rejects login when the auth config cannot be loaded', async () => { |
| 60 | + const configLoaded = service.loadConfig(); |
| 61 | + const request = httpMock.expectOne('assets/auth-config.json'); |
| 62 | + request.flush('Not found', { status: 404, statusText: 'Not Found' }); |
| 63 | + |
| 64 | + await configLoaded; |
| 65 | + |
| 66 | + expect(service.login('admin', 'dsomm-admin')).toBeFalse(); |
| 67 | + expect(service.isAuthenticated()).toBeFalse(); |
| 68 | + }); |
| 69 | + |
| 70 | + async function loadAuthConfig(): Promise<void> { |
| 71 | + const configLoaded = service.loadConfig(); |
| 72 | + const request = httpMock.expectOne('assets/auth-config.json'); |
| 73 | + expect(request.request.method).toBe('GET'); |
| 74 | + request.flush({ users: authUsers }); |
| 75 | + await configLoaded; |
| 76 | + } |
42 | 77 | }); |
0 commit comments