From e9bfcbd3fbc0cbbd578ebe509efe67ba337c2ca8 Mon Sep 17 00:00:00 2001 From: jacekpluta <73617938+jacekpluta@users.noreply.github.com> Date: Fri, 27 Sep 2024 09:58:22 +0200 Subject: [PATCH] [ACS-8834] KR page on Auth token expire page get cancel popup with continue refresh (#10250) --- .../unsaved-changes.guard.spec.ts | 140 ++++++++++++------ .../unsaved-changes.guard.ts | 9 +- 2 files changed, 98 insertions(+), 51 deletions(-) diff --git a/lib/core/src/lib/dialogs/unsaved-changes-dialog/unsaved-changes.guard.spec.ts b/lib/core/src/lib/dialogs/unsaved-changes-dialog/unsaved-changes.guard.spec.ts index 7da1eaf899..dcaf09ae37 100644 --- a/lib/core/src/lib/dialogs/unsaved-changes-dialog/unsaved-changes.guard.spec.ts +++ b/lib/core/src/lib/dialogs/unsaved-changes-dialog/unsaved-changes.guard.spec.ts @@ -20,15 +20,47 @@ import { MatDialog, MatDialogModule, MatDialogRef } from '@angular/material/dial import { Observable, Subject } from 'rxjs'; import { UnsavedChangesDialogComponent } from './unsaved-changes-dialog.component'; import { UnsavedChangesGuard } from './unsaved-changes.guard'; +import { AuthenticationService, AuthGuardService, NoopAuthModule } from '@alfresco/adf-core'; describe('UnsavedChangesGuard', () => { let guard: UnsavedChangesGuard; + let authService: AuthenticationService; + let authGuardService: AuthGuardService; + + const expectGuardToBe = (condition: boolean, done: DoneFn, checkUnsaved?: boolean) => { + (guard.canDeactivate() as Observable).subscribe((allowed) => { + if (checkUnsaved) { + allowed = guard.unsaved; + } + condition ? expect(allowed).toBeTrue() : expect(allowed).toBeFalse(); + done(); + }); + }; beforeEach(() => { TestBed.configureTestingModule({ - imports: [MatDialogModule] + imports: [MatDialogModule, NoopAuthModule], + providers: [ + { + provide: AuthenticationService, + useValue: { + isLoggedIn: () => true + } + }, + { + provide: AuthGuardService, + useValue: { + get withCredentials() { + return false; + } + } + } + ] }); guard = TestBed.inject(UnsavedChangesGuard); + authService = TestBed.inject(AuthenticationService); + TestBed.inject(AuthGuardService); + authGuardService = TestBed.inject(AuthGuardService); }); describe('canDeactivate', () => { @@ -43,63 +75,73 @@ describe('UnsavedChangesGuard', () => { } as MatDialogRef); }); - it('should return true if unsaved is set to false', () => { - guard.unsaved = false; - expect(guard.canDeactivate()).toBeTrue(); - }); - - it('should return true if unsaved was not set', () => { - expect(guard.canDeactivate()).toBeTrue(); - }); - - it('should return true when unsaved is set to true and result of dialog is true', (done) => { - guard.unsaved = true; - - (guard.canDeactivate() as Observable).subscribe((allowed) => { - expect(allowed).toBeTrue(); - done(); + describe('with Auth', () => { + beforeEach(() => { + spyOn(authService, 'isLoggedIn').and.returnValue(true); + }); + + it('should return true if unsaved is set to false and user is logged in', () => { + guard.unsaved = false; + expect(guard.canDeactivate()).toBeTrue(); + }); + + it('should return true if unsaved was not set and user is logged in', () => { + expect(guard.canDeactivate()).toBeTrue(); + }); + + it('should return true when unsaved is set to true and result of dialog is true', (done) => { + guard.unsaved = true; + + expectGuardToBe(true, done); + afterClosed$.next(true); + }); + + it('should set unsaved to false when unsaved is set to true and result of dialog is true', (done) => { + guard.unsaved = true; + + expectGuardToBe(false, done, true); + afterClosed$.next(true); + }); + + it('should return false if afterClosed subject value was undefined', (done) => { + guard.unsaved = true; + + expectGuardToBe(false, done); + afterClosed$.next(undefined); + }); + + it('should return false when unsaved is set to true and result of dialog is false', (done) => { + guard.unsaved = true; + + expectGuardToBe(false, done); + afterClosed$.next(false); + }); + + it('should keep unsaved set to true when unsaved was to true and result of dialog is false', (done) => { + guard.unsaved = true; + + expectGuardToBe(true, done, true); + afterClosed$.next(false); }); - afterClosed$.next(true); }); - it('should return false when unsaved is set to true and result of dialog is false', (done) => { - guard.unsaved = true; - - (guard.canDeactivate() as Observable).subscribe((allowed) => { - expect(allowed).toBeFalse(); - done(); + describe('Without auth', () => { + beforeEach(() => { + spyOn(authService, 'isLoggedIn').and.returnValue(false); }); - afterClosed$.next(false); - }); - it('should keep unsaved set to true when unsaved was to true and result of dialog is false', (done) => { - guard.unsaved = true; - - (guard.canDeactivate() as Observable).subscribe(() => { - expect(guard.unsaved).toBeTrue(); - done(); + it('should return true when user is logged out of authenticationService and authGuardBaseService.withCredentials returns false', (done) => { + expectGuardToBe(true, done); + afterClosed$.next(true); }); - afterClosed$.next(false); - }); - it('should set unsaved to false when unsaved is set to true and result of dialog is true', (done) => { - guard.unsaved = true; + it('should return false when authGuardBaseService.withCredentials returns true', (done) => { + guard.unsaved = true; + spyOnProperty(authGuardService, 'withCredentials').and.returnValue(true); - (guard.canDeactivate() as Observable).subscribe(() => { - expect(guard.unsaved).toBeFalse(); - done(); + expectGuardToBe(false, done); + afterClosed$.next(false); }); - afterClosed$.next(true); - }); - - it('should return false if afterClosed subject value was undefined', (done) => { - guard.unsaved = true; - - (guard.canDeactivate() as Observable).subscribe((result) => { - expect(result).toBeFalse(); - done(); - }); - afterClosed$.next(undefined); }); }); }); diff --git a/lib/core/src/lib/dialogs/unsaved-changes-dialog/unsaved-changes.guard.ts b/lib/core/src/lib/dialogs/unsaved-changes-dialog/unsaved-changes.guard.ts index 606d72899b..ca9df5ee00 100644 --- a/lib/core/src/lib/dialogs/unsaved-changes-dialog/unsaved-changes.guard.ts +++ b/lib/core/src/lib/dialogs/unsaved-changes-dialog/unsaved-changes.guard.ts @@ -17,11 +17,12 @@ import { Injectable } from '@angular/core'; import { CanDeactivate } from '@angular/router'; -import { Observable } from 'rxjs'; +import { Observable, of } from 'rxjs'; import { MatDialog } from '@angular/material/dialog'; import { UnsavedChangesDialogComponent } from './unsaved-changes-dialog.component'; import { map, tap } from 'rxjs/operators'; import { UnsavedChangesDialogData } from './unsaved-changes-dialog.model'; +import { AuthenticationService, AuthGuardService } from '../../auth'; /** * Guard responsible for protecting leaving page with unsaved changes. @@ -33,7 +34,7 @@ export class UnsavedChangesGuard implements CanDeactivate { unsaved = false; data: UnsavedChangesDialogData; - constructor(private dialog: MatDialog) {} + constructor(private dialog: MatDialog, private authenticationService: AuthenticationService, private authGuardBaseService: AuthGuardService) {} /** * Allows to deactivate route when there is no unsaved changes, otherwise displays dialog to confirm discarding changes. @@ -41,6 +42,10 @@ export class UnsavedChangesGuard implements CanDeactivate { * @returns boolean | Observable true when there is no unsaved changes or changes can be discarded, false otherwise. */ canDeactivate(): boolean | Observable { + if (!this.authenticationService.isLoggedIn() && !this.authGuardBaseService.withCredentials) { + return of(true); + } + return this.unsaved ? this.dialog .open(UnsavedChangesDialogComponent, {