import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
+import { RouterModule } from '@angular/router';
import { NgBootstrapFormValidationModule } from 'ng-bootstrap-form-validation';
import { ChartsModule } from 'ng2-charts';
import { LoadingPanelComponent } from './loading-panel/loading-panel.component';
import { ModalComponent } from './modal/modal.component';
import { NotificationsSidebarComponent } from './notifications-sidebar/notifications-sidebar.component';
+import { PwdExpirationNotificationComponent } from './pwd-expiration-notification/pwd-expiration-notification.component';
import { RefreshSelectorComponent } from './refresh-selector/refresh-selector.component';
import { SelectBadgesComponent } from './select-badges/select-badges.component';
import { SelectComponent } from './select/select.component';
ModalModule.forRoot(),
DirectivesModule,
BsDropdownModule,
- NgBootstrapFormValidationModule
+ NgBootstrapFormValidationModule,
+ RouterModule
],
declarations: [
ViewCacheComponent,
RefreshSelectorComponent,
ConfigOptionComponent,
AlertPanelComponent,
- FormModalComponent
+ FormModalComponent,
+ PwdExpirationNotificationComponent
],
providers: [],
exports: [
SelectComponent,
RefreshSelectorComponent,
ConfigOptionComponent,
- AlertPanelComponent
+ AlertPanelComponent,
+ PwdExpirationNotificationComponent
],
entryComponents: [
ModalComponent,
--- /dev/null
+import { HttpClientTestingModule } from '@angular/common/http/testing';
+import { Component } from '@angular/core';
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+import { Routes } from '@angular/router';
+import { RouterTestingModule } from '@angular/router/testing';
+
+import { of as observableOf } from 'rxjs';
+
+import { AlertModule } from 'ngx-bootstrap/alert';
+
+import { configureTestBed } from '../../../../testing/unit-test-helper';
+
+import { SettingsService } from '../../api/settings.service';
+import { AuthStorageService } from '../../services/auth-storage.service';
+import { PwdExpirationNotificationComponent } from './pwd-expiration-notification.component';
+
+describe('PwdExpirationNotificationComponent', () => {
+ let component: PwdExpirationNotificationComponent;
+ let fixture: ComponentFixture<PwdExpirationNotificationComponent>;
+ let settingsService: SettingsService;
+ let authStorageService: AuthStorageService;
+
+ @Component({ selector: 'cd-fake', template: '' })
+ class FakeComponent {}
+
+ const routes: Routes = [{ path: 'login', component: FakeComponent }];
+
+ configureTestBed({
+ declarations: [PwdExpirationNotificationComponent, FakeComponent],
+ imports: [
+ AlertModule.forRoot(),
+ HttpClientTestingModule,
+ RouterTestingModule.withRoutes(routes)
+ ],
+ providers: [SettingsService, AuthStorageService]
+ });
+
+ describe('password expiration date has been set', () => {
+ beforeEach(() => {
+ authStorageService = TestBed.get(AuthStorageService);
+ settingsService = TestBed.get(SettingsService);
+ spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(1645488000);
+ spyOn(settingsService, 'pwdExpirationSettings').and.returnValue(
+ observableOf({
+ user_pwd_expiration_warning_1: 10,
+ user_pwd_expiration_warning_2: 5,
+ user_pwd_expiration_span: 90
+ })
+ );
+ fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
+ component = fixture.componentInstance;
+ fixture.detectChanges();
+ });
+
+ it('should create', () => {
+ component.ngOnInit();
+ expect(component).toBeTruthy();
+ });
+
+ it('should set warning levels', () => {
+ component.ngOnInit();
+ expect(component.pwdExpirationSettings.pwdExpirationWarning1).toBe(10);
+ expect(component.pwdExpirationSettings.pwdExpirationWarning2).toBe(5);
+ });
+
+ it('should calculate password expiration in days', () => {
+ const dateValue = Date;
+ spyOn(global, 'Date').and.callFake((date) => {
+ if (date) {
+ return new dateValue(date);
+ } else {
+ return new Date('2022-02-18T00:00:00.000Z');
+ }
+ });
+ component.ngOnInit();
+ expect(component['expirationDays']).toBe(4);
+ });
+
+ it('should set alert type warning correctly', () => {
+ const dateValue = Date;
+ spyOn(global, 'Date').and.callFake((date) => {
+ if (date) {
+ return new dateValue(date);
+ } else {
+ return new Date('2022-02-14T00:00:00.000Z');
+ }
+ });
+ component.ngOnInit();
+ expect(component['alertType']).toBe('warning');
+ });
+
+ it('should set alert type danger correctly', () => {
+ const dateValue = Date;
+ spyOn(global, 'Date').and.callFake((date) => {
+ if (date) {
+ return new dateValue(date);
+ } else {
+ return new Date('2022-02-18T00:00:00.000Z');
+ }
+ });
+ component.ngOnInit();
+ expect(component['alertType']).toBe('danger');
+ });
+ });
+
+ describe('password expiration date has not been set', () => {
+ beforeEach(() => {
+ authStorageService = TestBed.get(AuthStorageService);
+ spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(null);
+ fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
+ component = fixture.componentInstance;
+ fixture.detectChanges();
+ });
+
+ it('should calculate no expirationDays', () => {
+ component.ngOnInit();
+ expect(component['expirationDays']).toBeUndefined();
+ });
+ });
+});
--- /dev/null
+import { Component, OnInit } from '@angular/core';
+
+import { SettingsService } from '../../api/settings.service';
+import { CdPwdExpirationSettings } from '../../models/cd-pwd-expiration-settings';
+import { AuthStorageService } from '../../services/auth-storage.service';
+
+@Component({
+ selector: 'cd-pwd-expiration-notification',
+ templateUrl: './pwd-expiration-notification.component.html',
+ styleUrls: ['./pwd-expiration-notification.component.scss']
+})
+export class PwdExpirationNotificationComponent implements OnInit {
+ alertType: string;
+ expirationDays: number;
+ pwdExpirationSettings: CdPwdExpirationSettings;
+
+ constructor(
+ private settingsService: SettingsService,
+ private authStorageService: AuthStorageService
+ ) {}
+
+ ngOnInit() {
+ this.settingsService.pwdExpirationSettings().subscribe((pwdExpirationSettings) => {
+ this.pwdExpirationSettings = new CdPwdExpirationSettings(pwdExpirationSettings);
+ const pwdExpirationDate = this.authStorageService.getPwdExpirationDate();
+ if (pwdExpirationDate) {
+ this.expirationDays = this.getExpirationDays(pwdExpirationDate);
+ if (this.expirationDays <= this.pwdExpirationSettings.pwdExpirationWarning2) {
+ this.alertType = 'danger';
+ } else {
+ this.alertType = 'warning';
+ }
+ }
+ });
+ }
+
+ private getExpirationDays(pwdExpirationDate: number): number {
+ if (pwdExpirationDate) {
+ const current = new Date();
+ const expiration = new Date(pwdExpirationDate * 1000);
+ return Math.floor((expiration.valueOf() - current.valueOf()) / (1000 * 3600 * 24));
+ }
+ }
+}