]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.spec.ts
import ceph quincy 17.2.4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / auth.service.spec.ts
CommitLineData
11fdf7f2
TL
1import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2import { fakeAsync, TestBed, tick } from '@angular/core/testing';
f67539c2 3import { Router, Routes } from '@angular/router';
11fdf7f2
TL
4import { RouterTestingModule } from '@angular/router/testing';
5
f67539c2 6import { configureTestBed } from '~/testing/unit-test-helper';
11fdf7f2
TL
7import { AuthStorageService } from '../services/auth-storage.service';
8import { AuthService } from './auth.service';
9
10describe('AuthService', () => {
11 let service: AuthService;
12 let httpTesting: HttpTestingController;
13
e306af50 14 const routes: Routes = [{ path: 'login', children: [] }];
11fdf7f2
TL
15
16 configureTestBed({
17 providers: [AuthService, AuthStorageService],
18 imports: [HttpClientTestingModule, RouterTestingModule.withRoutes(routes)]
19 });
20
21 beforeEach(() => {
f67539c2
TL
22 service = TestBed.inject(AuthService);
23 httpTesting = TestBed.inject(HttpTestingController);
11fdf7f2
TL
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' };
adb31ebb 36 const fakeResponse = { username: 'foo' };
9f95a23c 37 service.login(fakeCredentials).subscribe();
11fdf7f2
TL
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');
11fdf7f2
TL
44 }));
45
f67539c2
TL
46 it('should logout and remove the user', () => {
47 const router = TestBed.inject(Router);
48 spyOn(router, 'navigate').and.stub();
49
11fdf7f2
TL
50 service.logout();
51 const req = httpTesting.expectOne('api/auth/logout');
52 expect(req.request.method).toBe('POST');
53 req.flush({ redirect_url: '#/login' });
11fdf7f2 54 expect(localStorage.getItem('dashboard_username')).toBe(null);
f67539c2
TL
55 expect(router.navigate).toBeCalledTimes(1);
56 });
11fdf7f2 57});