]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
07c9a8a4d8ea1c612f2b25ead09c808146c2a516
[ceph.git] /
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 { configureTestBed } from '../../../../testing/unit-test-helper';
11 import { SettingsService } from '../../api/settings.service';
12 import { AuthStorageService } from '../../services/auth-storage.service';
13 import { PwdExpirationNotificationComponent } from './pwd-expiration-notification.component';
14
15 describe('PwdExpirationNotificationComponent', () => {
16   let component: PwdExpirationNotificationComponent;
17   let fixture: ComponentFixture<PwdExpirationNotificationComponent>;
18   let settingsService: SettingsService;
19   let authStorageService: AuthStorageService;
20
21   @Component({ selector: 'cd-fake', template: '' })
22   class FakeComponent {}
23
24   const routes: Routes = [{ path: 'login', component: FakeComponent }];
25
26   configureTestBed({
27     declarations: [PwdExpirationNotificationComponent, FakeComponent],
28     imports: [NgbAlertModule, HttpClientTestingModule, RouterTestingModule.withRoutes(routes)],
29     providers: [SettingsService, AuthStorageService]
30   });
31
32   describe('password expiration date has been set', () => {
33     beforeEach(() => {
34       authStorageService = TestBed.inject(AuthStorageService);
35       settingsService = TestBed.inject(SettingsService);
36       spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(1645488000);
37       spyOn(settingsService, 'getStandardSettings').and.returnValue(
38         observableOf({
39           user_pwd_expiration_warning_1: 10,
40           user_pwd_expiration_warning_2: 5,
41           user_pwd_expiration_span: 90
42         })
43       );
44       fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
45       component = fixture.componentInstance;
46       fixture.detectChanges();
47     });
48
49     it('should create', () => {
50       component.ngOnInit();
51       expect(component).toBeTruthy();
52     });
53
54     it('should set warning levels', () => {
55       component.ngOnInit();
56       expect(component.pwdExpirationSettings.pwdExpirationWarning1).toBe(10);
57       expect(component.pwdExpirationSettings.pwdExpirationWarning2).toBe(5);
58     });
59
60     it('should calculate password expiration in days', () => {
61       const dateValue = Date;
62       spyOn(global, 'Date').and.callFake((date) => {
63         if (date) {
64           return new dateValue(date);
65         } else {
66           return new Date('2022-02-18T00:00:00.000Z');
67         }
68       });
69       component.ngOnInit();
70       expect(component['expirationDays']).toBe(4);
71     });
72
73     it('should set alert type warning correctly', () => {
74       const dateValue = Date;
75       spyOn(global, 'Date').and.callFake((date) => {
76         if (date) {
77           return new dateValue(date);
78         } else {
79           return new Date('2022-02-14T00:00:00.000Z');
80         }
81       });
82       component.ngOnInit();
83       expect(component['alertType']).toBe('warning');
84     });
85
86     it('should set alert type danger correctly', () => {
87       const dateValue = Date;
88       spyOn(global, 'Date').and.callFake((date) => {
89         if (date) {
90           return new dateValue(date);
91         } else {
92           return new Date('2022-02-18T00:00:00.000Z');
93         }
94       });
95       component.ngOnInit();
96       expect(component['alertType']).toBe('danger');
97     });
98   });
99
100   describe('password expiration date has not been set', () => {
101     beforeEach(() => {
102       authStorageService = TestBed.inject(AuthStorageService);
103       spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(null);
104       fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
105       component = fixture.componentInstance;
106       fixture.detectChanges();
107     });
108
109     it('should calculate no expirationDays', () => {
110       component.ngOnInit();
111       expect(component['expirationDays']).toBeUndefined();
112     });
113   });
114 });