Skip to content

Commit

Permalink
[ACS-8834] KR page on Auth token expire page get cancel popup with co…
Browse files Browse the repository at this point in the history
…ntinue refresh (#10250)
  • Loading branch information
jacekpluta authored Sep 27, 2024
1 parent ac5bbfa commit e9bfcbd
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>).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', () => {
Expand All @@ -43,63 +75,73 @@ describe('UnsavedChangesGuard', () => {
} as MatDialogRef<UnsavedChangesDialogComponent>);
});

it('should return true if unsaved is set to false', () => {
guard.unsaved = false;
expect(guard.canDeactivate()).toBeTrue();
});
describe('with Auth', () => {
beforeEach(() => {
spyOn(authService, 'isLoggedIn').and.returnValue(true);
});

it('should return true if unsaved was not set', () => {
expect(guard.canDeactivate()).toBeTrue();
});
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 when unsaved is set to true and result of dialog is true', (done) => {
guard.unsaved = true;
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;

(guard.canDeactivate() as Observable<boolean>).subscribe((allowed) => {
expect(allowed).toBeTrue();
done();
expectGuardToBe(true, done);
afterClosed$.next(true);
});
afterClosed$.next(true);
});

it('should return false when unsaved is set to true and result of dialog is false', (done) => {
guard.unsaved = true;
it('should set unsaved to false when unsaved is set to true and result of dialog is true', (done) => {
guard.unsaved = true;

(guard.canDeactivate() as Observable<boolean>).subscribe((allowed) => {
expect(allowed).toBeFalse();
done();
expectGuardToBe(false, done, true);
afterClosed$.next(true);
});
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;
it('should return false if afterClosed subject value was undefined', (done) => {
guard.unsaved = true;

(guard.canDeactivate() as Observable<boolean>).subscribe(() => {
expect(guard.unsaved).toBeTrue();
done();
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);
});
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 keep unsaved set to true when unsaved was to true and result of dialog is false', (done) => {
guard.unsaved = true;

(guard.canDeactivate() as Observable<boolean>).subscribe(() => {
expect(guard.unsaved).toBeFalse();
done();
expectGuardToBe(true, done, true);
afterClosed$.next(false);
});
afterClosed$.next(true);
});

it('should return false if afterClosed subject value was undefined', (done) => {
guard.unsaved = true;
describe('Without auth', () => {
beforeEach(() => {
spyOn(authService, 'isLoggedIn').and.returnValue(false);
});

it('should return true when user is logged out of authenticationService and authGuardBaseService.withCredentials returns false', (done) => {
expectGuardToBe(true, done);
afterClosed$.next(true);
});

it('should return false when authGuardBaseService.withCredentials returns true', (done) => {
guard.unsaved = true;
spyOnProperty(authGuardService, 'withCredentials').and.returnValue(true);

(guard.canDeactivate() as Observable<boolean>).subscribe((result) => {
expect(result).toBeFalse();
done();
expectGuardToBe(false, done);
afterClosed$.next(false);
});
afterClosed$.next(undefined);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,14 +34,18 @@ export class UnsavedChangesGuard implements CanDeactivate<any> {
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.
*
* @returns boolean | Observable<boolean> true when there is no unsaved changes or changes can be discarded, false otherwise.
*/
canDeactivate(): boolean | Observable<boolean> {
if (!this.authenticationService.isLoggedIn() && !this.authGuardBaseService.withCredentials) {
return of(true);
}

return this.unsaved
? this.dialog
.open<UnsavedChangesDialogComponent>(UnsavedChangesDialogComponent, {
Expand Down

0 comments on commit e9bfcbd

Please sign in to comment.