]>
Commit | Line | Data |
---|---|---|
1 | import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | |
2 | import { fakeAsync, TestBed, tick } from '@angular/core/testing'; | |
3 | import { Routes } from '@angular/router'; | |
4 | import { RouterTestingModule } from '@angular/router/testing'; | |
5 | ||
6 | import { configureTestBed } from '../../../testing/unit-test-helper'; | |
7 | import { AuthStorageService } from '../services/auth-storage.service'; | |
8 | import { AuthService } from './auth.service'; | |
9 | ||
10 | describe('AuthService', () => { | |
11 | let service: AuthService; | |
12 | let httpTesting: HttpTestingController; | |
13 | ||
14 | const routes: Routes = [{ path: 'login', children: [] }]; | |
15 | ||
16 | configureTestBed({ | |
17 | providers: [AuthService, AuthStorageService], | |
18 | imports: [HttpClientTestingModule, RouterTestingModule.withRoutes(routes)] | |
19 | }); | |
20 | ||
21 | beforeEach(() => { | |
22 | service = TestBed.get(AuthService); | |
23 | httpTesting = TestBed.get(HttpTestingController); | |
24 | }); | |
25 | ||
26 | afterEach(() => { | |
27 | httpTesting.verify(); | |
28 | }); | |
29 | ||
30 | it('should be created', () => { | |
31 | expect(service).toBeTruthy(); | |
32 | }); | |
33 | ||
34 | it('should login and save the user', fakeAsync(() => { | |
35 | const fakeCredentials = { username: 'foo', password: 'bar' }; | |
36 | const fakeResponse = { username: 'foo', token: 'tokenbytes' }; | |
37 | service.login(fakeCredentials).subscribe(); | |
38 | const req = httpTesting.expectOne('api/auth'); | |
39 | expect(req.request.method).toBe('POST'); | |
40 | expect(req.request.body).toEqual(fakeCredentials); | |
41 | req.flush(fakeResponse); | |
42 | tick(); | |
43 | expect(localStorage.getItem('dashboard_username')).toBe('foo'); | |
44 | expect(localStorage.getItem('access_token')).toBe('tokenbytes'); | |
45 | })); | |
46 | ||
47 | it('should logout and remove the user', fakeAsync(() => { | |
48 | service.logout(); | |
49 | const req = httpTesting.expectOne('api/auth/logout'); | |
50 | expect(req.request.method).toBe('POST'); | |
51 | req.flush({ redirect_url: '#/login' }); | |
52 | tick(); | |
53 | expect(localStorage.getItem('dashboard_username')).toBe(null); | |
54 | })); | |
55 | }); |