forked from AzureAD/microsoft-authentication-library-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
277 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,10 @@ | |
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { AccountInfo } from "@azure/msal-browser"; | ||
import { AccountInfo, AuthenticationResult } from "@azure/msal-browser"; | ||
|
||
export const TEST_CONFIG = { | ||
MSAL_CLIENT_ID: "0813e1d1-ad72-46a9-8665-399bba48c201" | ||
MSAL_CLIENT_ID: "0813e1d1-ad72-46a9-8665-399bba48c201", | ||
}; | ||
|
||
export const TEST_DATA_CLIENT_INFO = { | ||
|
@@ -29,3 +29,16 @@ export const testAccount: AccountInfo = { | |
username: "[email protected]", | ||
name: "Abe Lincoln" | ||
}; | ||
|
||
export const testResult: AuthenticationResult = { | ||
uniqueId: "unique-id", | ||
tenantId: "tenant-id", | ||
scopes: ["openid", "profile"], | ||
idToken: "test-id-token", | ||
idTokenClaims: {}, | ||
accessToken: "test-access-token", | ||
fromCache: false, | ||
expiresOn: new Date(Date.now() + (3600000)), | ||
account: testAccount, | ||
tokenType: "Bearer" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
244 changes: 244 additions & 0 deletions
244
lib/msal-react/test/components/MsalAuthenticationTemplate.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import React from "react"; | ||
import { render, screen, waitFor } from "@testing-library/react"; | ||
import "@testing-library/jest-dom"; | ||
import { testAccount, testResult, TEST_CONFIG } from "../TestConstants"; | ||
import { MsalProvider, MsalAuthenticationTemplate } from "../../src/index"; | ||
import { PublicClientApplication, Configuration, InteractionType, EventType, AccountInfo, EventCallbackFunction, EventMessage, PopupRequest } from "@azure/msal-browser"; | ||
|
||
describe("MsalAuthenticationTemplate tests", () => { | ||
let pca: PublicClientApplication; | ||
const msalConfig: Configuration = { | ||
auth: { | ||
clientId: TEST_CONFIG.MSAL_CLIENT_ID | ||
} | ||
}; | ||
|
||
let eventCallback: EventCallbackFunction; | ||
let handleRedirectSpy: jest.SpyInstance; | ||
let accounts: AccountInfo[] = []; | ||
|
||
beforeEach(() => { | ||
pca = new PublicClientApplication(msalConfig); | ||
jest.spyOn(pca, "addEventCallback").mockImplementation((callbackFn) => { | ||
eventCallback = callbackFn; | ||
return "callbackId"; | ||
}); | ||
handleRedirectSpy = jest.spyOn(pca, "handleRedirectPromise").mockImplementation(() => { | ||
const eventMessage: EventMessage = { | ||
eventType: EventType.HANDLE_REDIRECT_END, | ||
interactionType: InteractionType.Redirect, | ||
payload: null, | ||
error: null, | ||
timestamp: 10000 | ||
} | ||
eventCallback(eventMessage); | ||
return Promise.resolve(null); | ||
}); | ||
|
||
jest.spyOn(pca, "getAllAccounts").mockImplementation(() => accounts); | ||
}); | ||
|
||
afterEach(() => { | ||
// cleanup on exiting | ||
jest.clearAllMocks(); | ||
accounts = []; | ||
}); | ||
|
||
test("Calls loginPopup if no account is signed in", async () => { | ||
const loginPopupSpy = jest.spyOn(pca, "loginPopup").mockImplementation((request) => { | ||
expect(request).toBe(undefined); | ||
accounts = [testAccount]; | ||
const eventMessage: EventMessage = { | ||
eventType: EventType.LOGIN_SUCCESS, | ||
interactionType: InteractionType.Popup, | ||
payload: null, | ||
error: null, | ||
timestamp: 10000 | ||
} | ||
eventCallback(eventMessage); | ||
|
||
return Promise.resolve(testResult); | ||
}); | ||
|
||
render( | ||
<MsalProvider instance={pca}> | ||
<p>This text will always display.</p> | ||
<MsalAuthenticationTemplate interactionType={InteractionType.Popup}> | ||
<span> A user is authenticated!</span> | ||
</MsalAuthenticationTemplate> | ||
</MsalProvider> | ||
); | ||
|
||
await waitFor(() => expect(handleRedirectSpy).toHaveBeenCalledTimes(1)); | ||
await waitFor(() => expect(loginPopupSpy).toHaveBeenCalledTimes(1)); | ||
expect(screen.queryByText("This text will always display.")).toBeInTheDocument(); | ||
expect(screen.queryByText("A user is authenticated!")).toBeInTheDocument(); | ||
}); | ||
|
||
test("Calls loginRedirect if no account is signed in", async () => { | ||
const loginRedirectSpy = jest.spyOn(pca, "loginRedirect").mockImplementation((request) => { | ||
expect(request).toBe(undefined); | ||
accounts = [testAccount]; | ||
const eventMessage: EventMessage = { | ||
eventType: EventType.LOGIN_SUCCESS, | ||
interactionType: InteractionType.Redirect, | ||
payload: null, | ||
error: null, | ||
timestamp: 10000 | ||
} | ||
eventCallback(eventMessage); | ||
return Promise.resolve(); | ||
}); | ||
|
||
render( | ||
<MsalProvider instance={pca}> | ||
<p>This text will always display.</p> | ||
<MsalAuthenticationTemplate interactionType={InteractionType.Redirect}> | ||
<span> A user is authenticated!</span> | ||
</MsalAuthenticationTemplate> | ||
</MsalProvider> | ||
); | ||
|
||
await waitFor(() => expect(handleRedirectSpy).toHaveBeenCalledTimes(1)); | ||
await waitFor(() => expect(loginRedirectSpy).toHaveBeenCalledTimes(1)); | ||
expect(screen.queryByText("This text will always display.")).toBeInTheDocument(); | ||
expect(screen.queryByText("A user is authenticated!")).toBeInTheDocument(); | ||
}); | ||
|
||
test("Calls ssoSilent if no account is signed in", async () => { | ||
const ssoSilentSpy = jest.spyOn(pca, "ssoSilent").mockImplementation((request) => { | ||
expect(request).toBe(undefined); | ||
accounts = [testAccount]; | ||
const eventMessage: EventMessage = { | ||
eventType: EventType.LOGIN_SUCCESS, | ||
interactionType: InteractionType.Silent, | ||
payload: null, | ||
error: null, | ||
timestamp: 10000 | ||
} | ||
eventCallback(eventMessage); | ||
return Promise.resolve(testResult); | ||
}); | ||
|
||
render( | ||
<MsalProvider instance={pca}> | ||
<p>This text will always display.</p> | ||
<MsalAuthenticationTemplate interactionType={InteractionType.Silent}> | ||
<span> A user is authenticated!</span> | ||
</MsalAuthenticationTemplate> | ||
</MsalProvider> | ||
); | ||
|
||
await waitFor(() => expect(handleRedirectSpy).toHaveBeenCalledTimes(1)); | ||
await waitFor(() => expect(ssoSilentSpy).toHaveBeenCalledTimes(1)); | ||
expect(screen.queryByText("This text will always display.")).toBeInTheDocument(); | ||
expect(screen.queryByText("A user is authenticated!")).toBeInTheDocument(); | ||
}); | ||
|
||
test("Calls loginPopup with provided request if no account is signed in", async () => { | ||
const loginRequest: PopupRequest = { | ||
scopes: ["openid"], | ||
redirectUri: "http://localhost" | ||
}; | ||
const loginPopupSpy = jest.spyOn(pca, "loginPopup").mockImplementation((request) => { | ||
expect(request).toBe(loginRequest); | ||
accounts = [testAccount]; | ||
const eventMessage: EventMessage = { | ||
eventType: EventType.LOGIN_SUCCESS, | ||
interactionType: InteractionType.Popup, | ||
payload: null, | ||
error: null, | ||
timestamp: 10000 | ||
} | ||
eventCallback(eventMessage); | ||
|
||
return Promise.resolve(testResult); | ||
}); | ||
|
||
render( | ||
<MsalProvider instance={pca}> | ||
<p>This text will always display.</p> | ||
<MsalAuthenticationTemplate interactionType={InteractionType.Popup} authenticationRequest={loginRequest}> | ||
<span> A user is authenticated!</span> | ||
</MsalAuthenticationTemplate> | ||
</MsalProvider> | ||
); | ||
|
||
await waitFor(() => expect(handleRedirectSpy).toHaveBeenCalledTimes(1)); | ||
await waitFor(() => expect(loginPopupSpy).toHaveBeenCalledTimes(1)); | ||
expect(screen.queryByText("This text will always display.")).toBeInTheDocument(); | ||
expect(screen.queryByText("A user is authenticated!")).toBeInTheDocument(); | ||
}); | ||
|
||
test("Calls loginRedirect with provided request if no account is signed in", async () => { | ||
const loginRequest: PopupRequest = { | ||
scopes: ["openid"], | ||
redirectUri: "http://localhost" | ||
}; | ||
const loginRedirectSpy = jest.spyOn(pca, "loginRedirect").mockImplementation((request) => { | ||
expect(request).toBe(loginRequest); | ||
accounts = [testAccount]; | ||
const eventMessage: EventMessage = { | ||
eventType: EventType.LOGIN_SUCCESS, | ||
interactionType: InteractionType.Redirect, | ||
payload: null, | ||
error: null, | ||
timestamp: 10000 | ||
} | ||
eventCallback(eventMessage); | ||
return Promise.resolve(); | ||
}); | ||
|
||
render( | ||
<MsalProvider instance={pca}> | ||
<p>This text will always display.</p> | ||
<MsalAuthenticationTemplate interactionType={InteractionType.Redirect} authenticationRequest={loginRequest}> | ||
<span> A user is authenticated!</span> | ||
</MsalAuthenticationTemplate> | ||
</MsalProvider> | ||
); | ||
|
||
await waitFor(() => expect(handleRedirectSpy).toHaveBeenCalledTimes(1)); | ||
await waitFor(() => expect(loginRedirectSpy).toHaveBeenCalledTimes(1)); | ||
expect(screen.queryByText("This text will always display.")).toBeInTheDocument(); | ||
expect(screen.queryByText("A user is authenticated!")).toBeInTheDocument(); | ||
}); | ||
|
||
test("Calls ssoSilent with provided request if no account is signed in", async () => { | ||
const loginRequest: PopupRequest = { | ||
scopes: ["openid"] | ||
}; | ||
const ssoSilentSpy = jest.spyOn(pca, "ssoSilent").mockImplementation((request) => { | ||
expect(request).toBe(loginRequest); | ||
accounts = [testAccount]; | ||
const eventMessage: EventMessage = { | ||
eventType: EventType.LOGIN_SUCCESS, | ||
interactionType: InteractionType.Silent, | ||
payload: null, | ||
error: null, | ||
timestamp: 10000 | ||
} | ||
eventCallback(eventMessage); | ||
return Promise.resolve(testResult); | ||
}); | ||
|
||
render( | ||
<MsalProvider instance={pca}> | ||
<p>This text will always display.</p> | ||
<MsalAuthenticationTemplate interactionType={InteractionType.Silent} authenticationRequest={loginRequest}> | ||
<span> A user is authenticated!</span> | ||
</MsalAuthenticationTemplate> | ||
</MsalProvider> | ||
); | ||
|
||
await waitFor(() => expect(handleRedirectSpy).toHaveBeenCalledTimes(1)); | ||
await waitFor(() => expect(ssoSilentSpy).toHaveBeenCalledTimes(1)); | ||
expect(screen.queryByText("This text will always display.")).toBeInTheDocument(); | ||
expect(screen.queryByText("A user is authenticated!")).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.