Skip to content

Commit

Permalink
Potentially fixed unit tests for AuthGuards
Browse files Browse the repository at this point in the history
  • Loading branch information
swapnil-verma-gl committed Jul 2, 2024
1 parent 12c4182 commit e29c7a8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 54 deletions.
54 changes: 34 additions & 20 deletions lib/core/src/lib/auth/guard/auth-guard-bpm.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { TestBed } from '@angular/core/testing';
import { AppConfigService } from '../../app-config/app-config.service';
import { AuthGuardBpm } from './auth-guard-bpm.service';
import { AuthenticationService } from '../services/authentication.service';
import { RouterStateSnapshot, Router } from '@angular/router';
import { RouterStateSnapshot, Router, UrlTree } from '@angular/router';
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
import { TranslateModule } from '@ngx-translate/core';
import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.service';
Expand All @@ -29,7 +29,6 @@ import { EMPTY, of } from 'rxjs';
import { OidcAuthenticationService } from '../oidc/oidc-authentication.service';

describe('AuthGuardService BPM', () => {
let authGuard: AuthGuardBpm;
let authService: AuthenticationService;
let basicAlfrescoAuthService: BasicAlfrescoAuthService;
let oidcAuthenticationService: OidcAuthenticationService;
Expand Down Expand Up @@ -57,7 +56,6 @@ describe('AuthGuardService BPM', () => {
basicAlfrescoAuthService = TestBed.inject(BasicAlfrescoAuthService);
oidcAuthenticationService = TestBed.inject(OidcAuthenticationService);
authService = TestBed.inject(AuthenticationService);
authGuard = TestBed.inject(AuthGuardBpm);
router = TestBed.inject(Router);
appConfigService = TestBed.inject(AppConfigService);

Expand All @@ -66,6 +64,22 @@ describe('AuthGuardService BPM', () => {
appConfigService.config.oauth2 = {};
});

const runAuthGuardWithContext = async (state: RouterStateSnapshot): Promise<boolean | UrlTree> => {
const result = TestBed.runInInjectionContext(() => AuthGuardBpm(state));
if (result instanceof Observable) {
return handleObservableResult(result);
} else {
return result;
}
};

const handleObservableResult = (result: Observable<boolean | UrlTree>): Promise<boolean | UrlTree> =>
new Promise<boolean | UrlTree>((resolve) => {
result.subscribe((value) => {
resolve(value);
});
});

it('should redirect url if the alfresco js api is NOT logged in and isOAuth with silentLogin', async () => {
spyOn(router, 'navigateByUrl').and.stub();
spyOn(authService, 'isBpmLoggedIn').and.returnValue(false);
Expand All @@ -85,15 +99,15 @@ describe('AuthGuardService BPM', () => {

const route = { url: 'abc' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(await runAuthGuardWithContext(route)).toBeFalsy();
expect(oidcAuthenticationService.ssoLogin).toHaveBeenCalledTimes(1);
});

it('if the alfresco js api is logged in should canActivate be true', async () => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(true);
const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeTruthy();
expect(await runAuthGuardWithContext(route)).toBeTruthy();
});

it('if the alfresco js api is configured with withCredentials true should canActivate be true', async () => {
Expand All @@ -102,15 +116,15 @@ describe('AuthGuardService BPM', () => {

const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeTruthy();
expect(await runAuthGuardWithContext(route)).toBeTruthy();
});

it('if the alfresco js api is NOT logged in should canActivate be false', async () => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(false);
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(await runAuthGuardWithContext(route)).toBeFalsy();
});

it('if the alfresco js api is NOT logged in should trigger a redirect event', async () => {
Expand All @@ -120,7 +134,7 @@ describe('AuthGuardService BPM', () => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(false);
const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(await runAuthGuardWithContext(route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/login?redirectUrl=some-url'));
});

Expand All @@ -131,7 +145,7 @@ describe('AuthGuardService BPM', () => {
appConfigService.config.oauth2.silentLogin = false;
const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(await runAuthGuardWithContext(route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
});

Expand All @@ -142,16 +156,16 @@ describe('AuthGuardService BPM', () => {
appConfigService.config.oauth2.silentLogin = undefined;
const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(await runAuthGuardWithContext(route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
});

it('should set redirect url', () => {
it('should set redirect url', async () => {
spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: 'some-url' } as RouterStateSnapshot;

authGuard.canActivate(null, route);
await runAuthGuardWithContext(route);

expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({
provider: 'BPM',
Expand All @@ -160,12 +174,12 @@ describe('AuthGuardService BPM', () => {
expect(basicAlfrescoAuthService.getRedirect()).toEqual('some-url');
});

it('should set redirect navigation commands with query params', () => {
it('should set redirect navigation commands with query params', async () => {
spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: 'some-url;q=123' } as RouterStateSnapshot;

authGuard.canActivate(null, route);
await runAuthGuardWithContext(route);

expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({
provider: 'BPM',
Expand All @@ -174,12 +188,12 @@ describe('AuthGuardService BPM', () => {
expect(basicAlfrescoAuthService.getRedirect()).toEqual('some-url;q=123');
});

it('should set redirect navigation commands with query params', () => {
it('should set redirect navigation commands with query params', async () => {
spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: '/' } as RouterStateSnapshot;

authGuard.canActivate(null, route);
await runAuthGuardWithContext(route);

expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({
provider: 'BPM',
Expand All @@ -188,13 +202,13 @@ describe('AuthGuardService BPM', () => {
expect(basicAlfrescoAuthService.getRedirect()).toEqual('/');
});

it('should get redirect url from config if there is one configured', () => {
it('should get redirect url from config if there is one configured', async () => {
appConfigService.config.loginRoute = 'fakeLoginRoute';
spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: 'some-url' } as RouterStateSnapshot;

authGuard.canActivate(null, route);
await runAuthGuardWithContext(route);

expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({
provider: 'BPM',
Expand All @@ -203,7 +217,7 @@ describe('AuthGuardService BPM', () => {
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/fakeLoginRoute?redirectUrl=some-url'));
});

it('should to close the material dialog if is redirect to the login', () => {
it('should to close the material dialog if is redirect to the login', async () => {
const materialDialog = TestBed.inject(MatDialog);

spyOn(materialDialog, 'closeAll');
Expand All @@ -212,7 +226,7 @@ describe('AuthGuardService BPM', () => {
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: 'some-url' } as RouterStateSnapshot;

authGuard.canActivate(null, route);
await runAuthGuardWithContext(route);

expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({
provider: 'BPM',
Expand Down
54 changes: 34 additions & 20 deletions lib/core/src/lib/auth/guard/auth-guard-ecm.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { TestBed } from '@angular/core/testing';
import { AppConfigService } from '../../app-config/app-config.service';
import { AuthGuardEcm } from './auth-guard-ecm.service';
import { AuthenticationService } from '../services/authentication.service';
import { RouterStateSnapshot, Router } from '@angular/router';
import { RouterStateSnapshot, Router, UrlTree } from '@angular/router';
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
import { TranslateModule } from '@ngx-translate/core';
import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.service';
Expand All @@ -30,7 +30,6 @@ import { EMPTY, of } from 'rxjs';
import { OidcAuthenticationService } from '../oidc/oidc-authentication.service';

describe('AuthGuardService ECM', () => {
let authGuard: AuthGuardEcm;
let authService: AuthenticationService;
let basicAlfrescoAuthService: BasicAlfrescoAuthService;
let oidcAuthenticationService: OidcAuthenticationService;
Expand Down Expand Up @@ -59,7 +58,6 @@ describe('AuthGuardService ECM', () => {
oidcAuthenticationService = TestBed.inject(OidcAuthenticationService);
basicAlfrescoAuthService = TestBed.inject(BasicAlfrescoAuthService);
authService = TestBed.inject(AuthenticationService);
authGuard = TestBed.inject(AuthGuardEcm);
router = TestBed.inject(Router);
appConfigService = TestBed.inject(AppConfigService);

Expand All @@ -68,11 +66,27 @@ describe('AuthGuardService ECM', () => {
appConfigService.config.oauth2 = {};
});

const runAuthGuardWithContext = async (state: RouterStateSnapshot): Promise<boolean | UrlTree> => {
const result = TestBed.runInInjectionContext(() => AuthGuardEcm(state));
if (result instanceof Observable) {
return handleObservableResult(result);
} else {
return result;
}
};

const handleObservableResult = (result: Observable<boolean | UrlTree>): Promise<boolean | UrlTree> =>
new Promise<boolean | UrlTree>((resolve) => {
result.subscribe((value) => {
resolve(value);
});
});

it('if the alfresco js api is logged in should canActivate be true', async () => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(true);
const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeTruthy();
expect(await runAuthGuardWithContext(route)).toBeTruthy();
});

it('if the alfresco js api is configured with withCredentials true should canActivate be true', async () => {
Expand All @@ -81,15 +95,15 @@ describe('AuthGuardService ECM', () => {

const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeTruthy();
expect(await runAuthGuardWithContext(route)).toBeTruthy();
});

it('if the alfresco js api is NOT logged in should canActivate be false', async () => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(await runAuthGuardWithContext(route)).toBeFalsy();
});

it('if the alfresco js api is NOT logged in should trigger a redirect event', async () => {
Expand All @@ -99,7 +113,7 @@ describe('AuthGuardService ECM', () => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(await runAuthGuardWithContext(route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/login?redirectUrl=some-url'));
});

Expand All @@ -110,7 +124,7 @@ describe('AuthGuardService ECM', () => {
appConfigService.config.oauth2.silentLogin = false;
const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(await runAuthGuardWithContext(route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
});

Expand All @@ -131,7 +145,7 @@ describe('AuthGuardService ECM', () => {

const route = { url: 'abc' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(await runAuthGuardWithContext(route)).toBeFalsy();
expect(oidcAuthenticationService.ssoLogin).toHaveBeenCalledTimes(1);
});

Expand All @@ -142,16 +156,16 @@ describe('AuthGuardService ECM', () => {
appConfigService.config.oauth2.silentLogin = undefined;
const route = { url: 'some-url' } as RouterStateSnapshot;

expect(await authGuard.canActivate(null, route)).toBeFalsy();
expect(await runAuthGuardWithContext(route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalled();
});

it('should set redirect navigation commands', () => {
it('should set redirect navigation commands', async () => {
spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: 'some-url' } as RouterStateSnapshot;

authGuard.canActivate(null, route);
await runAuthGuardWithContext(route);

expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({
provider: 'ECM',
Expand All @@ -160,12 +174,12 @@ describe('AuthGuardService ECM', () => {
expect(basicAlfrescoAuthService.getRedirect()).toEqual('some-url');
});

it('should set redirect navigation commands with query params', () => {
it('should set redirect navigation commands with query params', async () => {
spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: 'some-url;q=123' } as RouterStateSnapshot;

authGuard.canActivate(null, route);
await runAuthGuardWithContext(route);

expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({
provider: 'ECM',
Expand All @@ -174,12 +188,12 @@ describe('AuthGuardService ECM', () => {
expect(basicAlfrescoAuthService.getRedirect()).toEqual('some-url;q=123');
});

it('should set redirect navigation commands with query params', () => {
it('should set redirect navigation commands with query params', async () => {
spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: '/' } as RouterStateSnapshot;

authGuard.canActivate(null, route);
await runAuthGuardWithContext(route);

expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({
provider: 'ECM',
Expand All @@ -188,13 +202,13 @@ describe('AuthGuardService ECM', () => {
expect(basicAlfrescoAuthService.getRedirect()).toEqual('/');
});

it('should get redirect url from config if there is one configured', () => {
it('should get redirect url from config if there is one configured', async () => {
appConfigService.config.loginRoute = 'fakeLoginRoute';
spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough();
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: 'some-url' } as RouterStateSnapshot;

authGuard.canActivate(null, route);
await runAuthGuardWithContext(route);

expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({
provider: 'ECM',
Expand All @@ -203,7 +217,7 @@ describe('AuthGuardService ECM', () => {
expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/fakeLoginRoute?redirectUrl=some-url'));
});

it('should to close the material dialog if is redirect to the login', () => {
it('should to close the material dialog if is redirect to the login', async () => {
const materialDialog = TestBed.inject(MatDialog);

spyOn(materialDialog, 'closeAll');
Expand All @@ -212,7 +226,7 @@ describe('AuthGuardService ECM', () => {
spyOn(router, 'navigateByUrl').and.stub();
const route = { url: 'some-url' } as RouterStateSnapshot;

authGuard.canActivate(null, route);
await runAuthGuardWithContext(route);

expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({
provider: 'ECM',
Expand Down
Loading

0 comments on commit e29c7a8

Please sign in to comment.