Skip to content

Commit

Permalink
feature/msal-angular/msal-guard-stricter: fix some testing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgoeman committed Jul 4, 2024
1 parent 4064f4f commit 736fcea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
37 changes: 18 additions & 19 deletions lib/msal-angular/src/msal.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import {
AccountInfo,
AuthenticationResult,
BrowserSystemOptions,
InteractionStatus,
InteractionType,
IPublicClientApplication,
LogLevel,
PublicClientApplication,
UrlString,
} from "@azure/msal-browser";
import { of } from "rxjs";
import { BehaviorSubject, of } from "rxjs";
import {
MsalModule,
MsalGuard,
MsalService,
MsalBroadcastService,
} from "./public-api";
import { MsalGuardConfiguration } from "./msal.guard.config";
import { bootstrapApplication } from "@angular/platform-browser";

let guard: MsalGuard;
let authService: MsalService;
Expand Down Expand Up @@ -388,14 +390,12 @@ describe("MsalGuard", () => {

it("returns false for option enableCheckForExpiredToken is true and token is expired and silentRefresh fails", (done) => {
enableCheckForExpiredToken = true;
silentAuthRequest = {};
initializeMsal();

authService.handleRedirectObservable().subscribe();

spyOn(MsalService.prototype, "handleRedirectObservable").and.returnValue(
//@ts-ignore
of("test")
);
authService.handleRedirectObservable().subscribe((result) => {
console.log("handleRedirectObservable result", result);
});

spyOn(
PublicClientApplication.prototype,
Expand All @@ -410,6 +410,8 @@ describe("MsalGuard", () => {
of({ accessToken: undefined } as AuthenticationResult)
);

spyOn(guard as any, "loginInteractively").and.returnValue(of(false));

guard.canActivate(routeMock, routeStateMock).subscribe((result) => {
expect(result).toBeFalse();
done();
Expand All @@ -419,14 +421,12 @@ describe("MsalGuard", () => {
it("returns false for option enableCheckForExpiredToken is true and token is not expired but not within minimumSecondsBeforeTokenExpiration and silentRefresh fails", (done) => {
enableCheckForExpiredToken = true;
minimumSecondsBeforeTokenExpiration = 60;
silentAuthRequest = {};
initializeMsal();

authService.handleRedirectObservable().subscribe();

spyOn(MsalService.prototype, "handleRedirectObservable").and.returnValue(
//@ts-ignore
of("test")
);
authService.handleRedirectObservable().subscribe((result) => {
console.log("handleRedirectObservable result", result);
});

spyOn(
PublicClientApplication.prototype,
Expand All @@ -441,6 +441,8 @@ describe("MsalGuard", () => {
of({ accessToken: undefined } as AuthenticationResult)
);

spyOn(guard as any, "loginInteractively").and.returnValue(of(false));

guard.canActivate(routeMock, routeStateMock).subscribe((result) => {
expect(result).toBeFalse();
done();
Expand All @@ -452,12 +454,9 @@ describe("MsalGuard", () => {
silentAuthRequest = {}; // set silentauth request or it will not be tried
initializeMsal();

authService.handleRedirectObservable().subscribe();

spyOn(MsalService.prototype, "handleRedirectObservable").and.returnValue(
//@ts-ignore
of("test")
);
authService.handleRedirectObservable().subscribe((result) => {
console.log("handleRedirectObservable result", result);
});

spyOn(
PublicClientApplication.prototype,
Expand Down
13 changes: 11 additions & 2 deletions lib/msal-angular/src/msal.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,17 @@ export class MsalGuard {
return this.authService.acquireTokenSilent(silentRequest);
}),
map((authResult) => {
this.authService.getLogger().info("Guard - silent refresh succeeded");
return !!authResult.accessToken;
if (!!authResult.accessToken) {
this.authService
.getLogger()
.info("Guard - silent refresh succeeded");
return true;
} else {
this.authService
.getLogger()
.warning("Guard - silent refresh did not return a token");
return false;
}
}),
catchError((err) => {
this.authService
Expand Down

0 comments on commit 736fcea

Please sign in to comment.