]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
3cdfbd730dcc8ec293b406bd9b0300ffdb8372e1
[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 { SettingsService } from '~/app/shared/api/settings.service';
11 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
12 import { configureTestBed } from '~/testing/unit-test-helper';
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   const spyOnDate = (fakeDate: string) => {
27     const dateValue = Date;
28     spyOn(global, 'Date').and.callFake((date) => new dateValue(date ? date : fakeDate));
29   };
30
31   configureTestBed({
32     declarations: [PwdExpirationNotificationComponent, FakeComponent],
33     imports: [NgbAlertModule, HttpClientTestingModule, RouterTestingModule.withRoutes(routes)],
34     providers: [SettingsService, AuthStorageService]
35   });
36
37   describe('password expiration date has been set', () => {
38     beforeEach(() => {
39       authStorageService = TestBed.inject(AuthStorageService);
40       settingsService = TestBed.inject(SettingsService);
41       spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(1645488000);
42       spyOn(settingsService, 'getStandardSettings').and.returnValue(
43         observableOf({
44           user_pwd_expiration_warning_1: 10,
45           user_pwd_expiration_warning_2: 5,
46           user_pwd_expiration_span: 90
47         })
48       );
49       fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
50       component = fixture.componentInstance;
51       fixture.detectChanges();
52     });
53
54     it('should create', () => {
55       component.ngOnInit();
56       expect(component).toBeTruthy();
57     });
58
59     it('should set warning levels', () => {
60       component.ngOnInit();
61       expect(component.pwdExpirationSettings.pwdExpirationWarning1).toBe(10);
62       expect(component.pwdExpirationSettings.pwdExpirationWarning2).toBe(5);
63     });
64
65     it('should calculate password expiration in days', () => {
66       spyOnDate('2022-02-18T00:00:00.000Z');
67       component.ngOnInit();
68       expect(component['expirationDays']).toBe(4);
69     });
70
71     it('should set alert type warning correctly', () => {
72       spyOnDate('2022-02-14T00:00:00.000Z');
73       component.ngOnInit();
74       expect(component['alertType']).toBe('warning');
75       expect(component.displayNotification).toBeTruthy();
76     });
77
78     it('should set alert type danger correctly', () => {
79       spyOnDate('2022-02-18T00:00:00.000Z');
80       component.ngOnInit();
81       expect(component['alertType']).toBe('danger');
82       expect(component.displayNotification).toBeTruthy();
83     });
84
85     it('should not display if date is far', () => {
86       spyOnDate('2022-01-01T00:00:00.000Z');
87       component.ngOnInit();
88       expect(component.displayNotification).toBeFalsy();
89     });
90   });
91
92   describe('password expiration date has not been set', () => {
93     beforeEach(() => {
94       authStorageService = TestBed.inject(AuthStorageService);
95       spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(null);
96       fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
97       component = fixture.componentInstance;
98       fixture.detectChanges();
99     });
100
101     it('should calculate no expirationDays', () => {
102       component.ngOnInit();
103       expect(component['expirationDays']).toBeUndefined();
104     });
105   });
106 });