mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ACS-8834] KR page on Auth token expire page get cancel popup with continue refresh (#10250)
This commit is contained in:
@@ -20,15 +20,47 @@ import { MatDialog, MatDialogModule, MatDialogRef } from '@angular/material/dial
|
|||||||
import { Observable, Subject } from 'rxjs';
|
import { Observable, Subject } from 'rxjs';
|
||||||
import { UnsavedChangesDialogComponent } from './unsaved-changes-dialog.component';
|
import { UnsavedChangesDialogComponent } from './unsaved-changes-dialog.component';
|
||||||
import { UnsavedChangesGuard } from './unsaved-changes.guard';
|
import { UnsavedChangesGuard } from './unsaved-changes.guard';
|
||||||
|
import { AuthenticationService, AuthGuardService, NoopAuthModule } from '@alfresco/adf-core';
|
||||||
|
|
||||||
describe('UnsavedChangesGuard', () => {
|
describe('UnsavedChangesGuard', () => {
|
||||||
let guard: 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(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
imports: [MatDialogModule]
|
imports: [MatDialogModule, NoopAuthModule],
|
||||||
|
providers: [
|
||||||
|
{
|
||||||
|
provide: AuthenticationService,
|
||||||
|
useValue: {
|
||||||
|
isLoggedIn: () => true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: AuthGuardService,
|
||||||
|
useValue: {
|
||||||
|
get withCredentials() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
});
|
});
|
||||||
guard = TestBed.inject(UnsavedChangesGuard);
|
guard = TestBed.inject(UnsavedChangesGuard);
|
||||||
|
authService = TestBed.inject(AuthenticationService);
|
||||||
|
TestBed.inject(AuthGuardService);
|
||||||
|
authGuardService = TestBed.inject(AuthGuardService);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('canDeactivate', () => {
|
describe('canDeactivate', () => {
|
||||||
@@ -43,63 +75,73 @@ describe('UnsavedChangesGuard', () => {
|
|||||||
} as MatDialogRef<UnsavedChangesDialogComponent>);
|
} as MatDialogRef<UnsavedChangesDialogComponent>);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return true if unsaved is set to false', () => {
|
describe('with Auth', () => {
|
||||||
guard.unsaved = false;
|
beforeEach(() => {
|
||||||
expect(guard.canDeactivate()).toBeTrue();
|
spyOn(authService, 'isLoggedIn').and.returnValue(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return true if unsaved was not set', () => {
|
it('should return true if unsaved is set to false and user is logged in', () => {
|
||||||
expect(guard.canDeactivate()).toBeTrue();
|
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();
|
||||||
(guard.canDeactivate() as Observable<boolean>).subscribe((allowed) => {
|
});
|
||||||
expect(allowed).toBeTrue();
|
|
||||||
done();
|
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) => {
|
describe('Without auth', () => {
|
||||||
guard.unsaved = true;
|
beforeEach(() => {
|
||||||
|
spyOn(authService, 'isLoggedIn').and.returnValue(false);
|
||||||
(guard.canDeactivate() as Observable<boolean>).subscribe((allowed) => {
|
|
||||||
expect(allowed).toBeFalse();
|
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
afterClosed$.next(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should keep unsaved set to true when unsaved was to true and result of dialog is false', (done) => {
|
it('should return true when user is logged out of authenticationService and authGuardBaseService.withCredentials returns false', (done) => {
|
||||||
guard.unsaved = true;
|
expectGuardToBe(true, done);
|
||||||
|
afterClosed$.next(true);
|
||||||
(guard.canDeactivate() as Observable<boolean>).subscribe(() => {
|
|
||||||
expect(guard.unsaved).toBeTrue();
|
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
afterClosed$.next(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should set unsaved to false when unsaved is set to true and result of dialog is true', (done) => {
|
it('should return false when authGuardBaseService.withCredentials returns true', (done) => {
|
||||||
guard.unsaved = true;
|
guard.unsaved = true;
|
||||||
|
spyOnProperty(authGuardService, 'withCredentials').and.returnValue(true);
|
||||||
|
|
||||||
(guard.canDeactivate() as Observable<boolean>).subscribe(() => {
|
expectGuardToBe(false, done);
|
||||||
expect(guard.unsaved).toBeFalse();
|
afterClosed$.next(false);
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
afterClosed$.next(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return false if afterClosed subject value was undefined', (done) => {
|
|
||||||
guard.unsaved = true;
|
|
||||||
|
|
||||||
(guard.canDeactivate() as Observable<boolean>).subscribe((result) => {
|
|
||||||
expect(result).toBeFalse();
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
afterClosed$.next(undefined);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -17,11 +17,12 @@
|
|||||||
|
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { CanDeactivate } from '@angular/router';
|
import { CanDeactivate } from '@angular/router';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable, of } from 'rxjs';
|
||||||
import { MatDialog } from '@angular/material/dialog';
|
import { MatDialog } from '@angular/material/dialog';
|
||||||
import { UnsavedChangesDialogComponent } from './unsaved-changes-dialog.component';
|
import { UnsavedChangesDialogComponent } from './unsaved-changes-dialog.component';
|
||||||
import { map, tap } from 'rxjs/operators';
|
import { map, tap } from 'rxjs/operators';
|
||||||
import { UnsavedChangesDialogData } from './unsaved-changes-dialog.model';
|
import { UnsavedChangesDialogData } from './unsaved-changes-dialog.model';
|
||||||
|
import { AuthenticationService, AuthGuardService } from '../../auth';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Guard responsible for protecting leaving page with unsaved changes.
|
* Guard responsible for protecting leaving page with unsaved changes.
|
||||||
@@ -33,7 +34,7 @@ export class UnsavedChangesGuard implements CanDeactivate<any> {
|
|||||||
unsaved = false;
|
unsaved = false;
|
||||||
data: UnsavedChangesDialogData;
|
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.
|
* 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<any> {
|
|||||||
* @returns boolean | Observable<boolean> true when there is no unsaved changes or changes can be discarded, false otherwise.
|
* @returns boolean | Observable<boolean> true when there is no unsaved changes or changes can be discarded, false otherwise.
|
||||||
*/
|
*/
|
||||||
canDeactivate(): boolean | Observable<boolean> {
|
canDeactivate(): boolean | Observable<boolean> {
|
||||||
|
if (!this.authenticationService.isLoggedIn() && !this.authGuardBaseService.withCredentials) {
|
||||||
|
return of(true);
|
||||||
|
}
|
||||||
|
|
||||||
return this.unsaved
|
return this.unsaved
|
||||||
? this.dialog
|
? this.dialog
|
||||||
.open<UnsavedChangesDialogComponent>(UnsavedChangesDialogComponent, {
|
.open<UnsavedChangesDialogComponent>(UnsavedChangesDialogComponent, {
|
||||||
|
Reference in New Issue
Block a user