]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
3273a4e84f23d05ac36b39749c829f0bbc6a83a5
[ceph-ci.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 { of as observableOf } from 'rxjs';
8
9 import { AlertModule } from 'ngx-bootstrap/alert';
10
11 import { configureTestBed } from '../../../../testing/unit-test-helper';
12
13 import { SettingsService } from '../../api/settings.service';
14 import { AuthStorageService } from '../../services/auth-storage.service';
15 import { PwdExpirationNotificationComponent } from './pwd-expiration-notification.component';
16
17 describe('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
28   configureTestBed({
29     declarations: [PwdExpirationNotificationComponent, FakeComponent],
30     imports: [
31       AlertModule.forRoot(),
32       HttpClientTestingModule,
33       RouterTestingModule.withRoutes(routes)
34     ],
35     providers: [SettingsService, AuthStorageService]
36   });
37
38   describe('password expiration date has been set', () => {
39     beforeEach(() => {
40       authStorageService = TestBed.get(AuthStorageService);
41       settingsService = TestBed.get(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       const dateValue = Date;
68       spyOn(global, 'Date').and.callFake((date) => {
69         if (date) {
70           return new dateValue(date);
71         } else {
72           return new Date('2022-02-18T00:00:00.000Z');
73         }
74       });
75       component.ngOnInit();
76       expect(component['expirationDays']).toBe(4);
77     });
78
79     it('should set alert type warning correctly', () => {
80       const dateValue = Date;
81       spyOn(global, 'Date').and.callFake((date) => {
82         if (date) {
83           return new dateValue(date);
84         } else {
85           return new Date('2022-02-14T00:00:00.000Z');
86         }
87       });
88       component.ngOnInit();
89       expect(component['alertType']).toBe('warning');
90     });
91
92     it('should set alert type danger correctly', () => {
93       const dateValue = Date;
94       spyOn(global, 'Date').and.callFake((date) => {
95         if (date) {
96           return new dateValue(date);
97         } else {
98           return new Date('2022-02-18T00:00:00.000Z');
99         }
100       });
101       component.ngOnInit();
102       expect(component['alertType']).toBe('danger');
103     });
104   });
105
106   describe('password expiration date has not been set', () => {
107     beforeEach(() => {
108       authStorageService = TestBed.get(AuthStorageService);
109       spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(null);
110       fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
111       component = fixture.componentInstance;
112       fixture.detectChanges();
113     });
114
115     it('should calculate no expirationDays', () => {
116       component.ngOnInit();
117       expect(component['expirationDays']).toBeUndefined();
118     });
119   });
120 });