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';
7 import { of as observableOf } from 'rxjs';
9 import { AlertModule } from 'ngx-bootstrap/alert';
11 import { configureTestBed } from '../../../../testing/unit-test-helper';
13 import { SettingsService } from '../../api/settings.service';
14 import { AuthStorageService } from '../../services/auth-storage.service';
15 import { PwdExpirationNotificationComponent } from './pwd-expiration-notification.component';
17 describe('PwdExpirationNotificationComponent', () => {
18 let component: PwdExpirationNotificationComponent;
19 let fixture: ComponentFixture<PwdExpirationNotificationComponent>;
20 let settingsService: SettingsService;
21 let authStorageService: AuthStorageService;
23 @Component({ selector: 'cd-fake', template: '' })
24 class FakeComponent {}
26 const routes: Routes = [{ path: 'login', component: FakeComponent }];
29 declarations: [PwdExpirationNotificationComponent, FakeComponent],
31 AlertModule.forRoot(),
32 HttpClientTestingModule,
33 RouterTestingModule.withRoutes(routes)
35 providers: [SettingsService, AuthStorageService]
38 describe('password expiration date has been set', () => {
40 authStorageService = TestBed.get(AuthStorageService);
41 settingsService = TestBed.get(SettingsService);
42 spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(1645488000);
43 spyOn(settingsService, 'getStandardSettings').and.returnValue(
45 user_pwd_expiration_warning_1: 10,
46 user_pwd_expiration_warning_2: 5,
47 user_pwd_expiration_span: 90
50 fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
51 component = fixture.componentInstance;
52 fixture.detectChanges();
55 it('should create', () => {
57 expect(component).toBeTruthy();
60 it('should set warning levels', () => {
62 expect(component.pwdExpirationSettings.pwdExpirationWarning1).toBe(10);
63 expect(component.pwdExpirationSettings.pwdExpirationWarning2).toBe(5);
66 it('should calculate password expiration in days', () => {
67 const dateValue = Date;
68 spyOn(global, 'Date').and.callFake((date) => {
70 return new dateValue(date);
72 return new Date('2022-02-18T00:00:00.000Z');
76 expect(component['expirationDays']).toBe(4);
79 it('should set alert type warning correctly', () => {
80 const dateValue = Date;
81 spyOn(global, 'Date').and.callFake((date) => {
83 return new dateValue(date);
85 return new Date('2022-02-14T00:00:00.000Z');
89 expect(component['alertType']).toBe('warning');
92 it('should set alert type danger correctly', () => {
93 const dateValue = Date;
94 spyOn(global, 'Date').and.callFake((date) => {
96 return new dateValue(date);
98 return new Date('2022-02-18T00:00:00.000Z');
101 component.ngOnInit();
102 expect(component['alertType']).toBe('danger');
106 describe('password expiration date has not been set', () => {
108 authStorageService = TestBed.get(AuthStorageService);
109 spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(null);
110 fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
111 component = fixture.componentInstance;
112 fixture.detectChanges();
115 it('should calculate no expirationDays', () => {
116 component.ngOnInit();
117 expect(component['expirationDays']).toBeUndefined();