]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.spec.ts
import 15.2.5
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / components / pwd-expiration-notification / pwd-expiration-notification.component.spec.ts
CommitLineData
9f95a23c
TL
1import { HttpClientTestingModule } from '@angular/common/http/testing';
2import { Component } from '@angular/core';
3import { ComponentFixture, TestBed } from '@angular/core/testing';
4import { Routes } from '@angular/router';
5import { RouterTestingModule } from '@angular/router/testing';
6
7import { of as observableOf } from 'rxjs';
8
9import { AlertModule } from 'ngx-bootstrap/alert';
10
11import { configureTestBed } from '../../../../testing/unit-test-helper';
12
13import { SettingsService } from '../../api/settings.service';
14import { AuthStorageService } from '../../services/auth-storage.service';
15import { PwdExpirationNotificationComponent } from './pwd-expiration-notification.component';
16
17describe('PwdExpirationNotificationComponent', () => {
18 let component: PwdExpirationNotificationComponent;
19 let fixture: ComponentFixture<PwdExpirationNotificationComponent>;
20 let settingsService: SettingsService;
21 let authStorageService: AuthStorageService;
22
23 @Component({ selector: 'cd-fake', template: '' })
24 class FakeComponent {}
25
26 const routes: Routes = [{ path: 'login', component: FakeComponent }];
27
f6b5b4d7
TL
28 const spyOnDate = (fakeDate: string) => {
29 const dateValue = Date;
30 spyOn(global, 'Date').and.callFake((date) => new dateValue(date ? date : fakeDate));
31 };
32
9f95a23c
TL
33 configureTestBed({
34 declarations: [PwdExpirationNotificationComponent, FakeComponent],
35 imports: [
36 AlertModule.forRoot(),
37 HttpClientTestingModule,
38 RouterTestingModule.withRoutes(routes)
39 ],
40 providers: [SettingsService, AuthStorageService]
41 });
42
43 describe('password expiration date has been set', () => {
44 beforeEach(() => {
45 authStorageService = TestBed.get(AuthStorageService);
46 settingsService = TestBed.get(SettingsService);
47 spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(1645488000);
48 spyOn(settingsService, 'getStandardSettings').and.returnValue(
49 observableOf({
50 user_pwd_expiration_warning_1: 10,
51 user_pwd_expiration_warning_2: 5,
52 user_pwd_expiration_span: 90
53 })
54 );
55 fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
56 component = fixture.componentInstance;
57 fixture.detectChanges();
58 });
59
60 it('should create', () => {
61 component.ngOnInit();
62 expect(component).toBeTruthy();
63 });
64
65 it('should set warning levels', () => {
66 component.ngOnInit();
67 expect(component.pwdExpirationSettings.pwdExpirationWarning1).toBe(10);
68 expect(component.pwdExpirationSettings.pwdExpirationWarning2).toBe(5);
69 });
70
71 it('should calculate password expiration in days', () => {
f6b5b4d7 72 spyOnDate('2022-02-18T00:00:00.000Z');
9f95a23c
TL
73 component.ngOnInit();
74 expect(component['expirationDays']).toBe(4);
75 });
76
77 it('should set alert type warning correctly', () => {
f6b5b4d7 78 spyOnDate('2022-02-14T00:00:00.000Z');
9f95a23c
TL
79 component.ngOnInit();
80 expect(component['alertType']).toBe('warning');
f6b5b4d7 81 expect(component.displayNotification).toBeTruthy();
9f95a23c
TL
82 });
83
84 it('should set alert type danger correctly', () => {
f6b5b4d7 85 spyOnDate('2022-02-18T00:00:00.000Z');
9f95a23c
TL
86 component.ngOnInit();
87 expect(component['alertType']).toBe('danger');
f6b5b4d7
TL
88 expect(component.displayNotification).toBeTruthy();
89 });
90
91 it('should not display if date is far', () => {
92 spyOnDate('2022-01-01T00:00:00.000Z');
93 component.ngOnInit();
94 expect(component.displayNotification).toBeFalsy();
9f95a23c
TL
95 });
96 });
97
98 describe('password expiration date has not been set', () => {
99 beforeEach(() => {
100 authStorageService = TestBed.get(AuthStorageService);
101 spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(null);
102 fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
103 component = fixture.componentInstance;
104 fixture.detectChanges();
105 });
106
107 it('should calculate no expirationDays', () => {
108 component.ngOnInit();
109 expect(component['expirationDays']).toBeUndefined();
110 });
111 });
112});