From dcc6685c1630afa17854f1ee47c22b74f2407b35 Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Wed, 1 Jul 2020 14:31:59 +0000 Subject: [PATCH] mgr/dashboard: Hide password notification when expiration date is far Fixes: https://tracker.ceph.com/issues/46306 Signed-off-by: Tiago Melo --- ...-expiration-notification.component.spec.ts | 40 ++++++++----------- .../pwd-expiration-notification.component.ts | 5 ++- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.spec.ts index 07c9a8a4d8e..63f59601f50 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.spec.ts @@ -23,6 +23,11 @@ describe('PwdExpirationNotificationComponent', () => { const routes: Routes = [{ path: 'login', component: FakeComponent }]; + const spyOnDate = (fakeDate: string) => { + const dateValue = Date; + spyOn(global, 'Date').and.callFake((date) => new dateValue(date ? date : fakeDate)); + }; + configureTestBed({ declarations: [PwdExpirationNotificationComponent, FakeComponent], imports: [NgbAlertModule, HttpClientTestingModule, RouterTestingModule.withRoutes(routes)], @@ -58,42 +63,29 @@ describe('PwdExpirationNotificationComponent', () => { }); 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'); - } - }); + spyOnDate('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'); - } - }); + spyOnDate('2022-02-14T00:00:00.000Z'); component.ngOnInit(); expect(component['alertType']).toBe('warning'); + expect(component.displayNotification).toBeTruthy(); }); 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'); - } - }); + spyOnDate('2022-02-18T00:00:00.000Z'); component.ngOnInit(); expect(component['alertType']).toBe('danger'); + expect(component.displayNotification).toBeTruthy(); + }); + + it('should not display if date is far', () => { + spyOnDate('2022-01-01T00:00:00.000Z'); + component.ngOnInit(); + expect(component.displayNotification).toBeFalsy(); }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.ts index 83e1664643b..c713d4c7b6d 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.ts @@ -31,8 +31,9 @@ export class PwdExpirationNotificationComponent implements OnInit, OnDestroy { } else { this.alertType = 'warning'; } - this.displayNotification = true; - this.authStorageService.isPwdDisplayedSource.next(true); + this.displayNotification = + this.expirationDays <= this.pwdExpirationSettings.pwdExpirationWarning1; + this.authStorageService.isPwdDisplayedSource.next(this.displayNotification); } }); } -- 2.39.5