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