Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache lookup B2C tests #7540

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import * as puppeteer from "puppeteer";
import {
Screenshot,
Expand All @@ -22,18 +22,13 @@ import {
AppTypes,
AzureEnvironments,
} from "e2e-test-utils";

import { PublicClientApplication, TokenCache } from "@azure/msal-node";

// Set test cache name/location
const TEST_CACHE_LOCATION = `${__dirname}/data/b2c-aad.cache.json`;

// Get flow-specific routes from sample application
const getTokenSilent = require("../index");

// Build cachePlugin
const cachePlugin = require("../../cachePlugin.js")(TEST_CACHE_LOCATION);

// Load scenario configuration
const config = require("../config/B2C-AAD.json");

Expand All @@ -45,32 +40,24 @@ describe.skip("Silent Flow B2C Tests (aad account)", () => {
let page: puppeteer.Page;
let port: number;
let homeRoute: string;

let publicClientApplication: PublicClientApplication;
let msalTokenCache: TokenCache;
let server: any;

let username: string;
let accountPwd: string;

const screenshotFolder = `${SCREENSHOT_BASE_FOLDER_NAME}/silent-flow/b2c/aad-account`;

beforeAll(async () => {
await validateCacheLocation(TEST_CACHE_LOCATION);
// @ts-ignore
browser = await global.__BROWSER__;

// To run tests in parallel, each test needs to run on a unique port
port = 3006;
homeRoute = `${SAMPLE_HOME_URL}:${port}`;

createFolder(SCREENSHOT_BASE_FOLDER_NAME);

const labApiParms: LabApiQueryParams = {
azureEnvironment: AzureEnvironments.CLOUD,
appType: AppTypes.CLOUD,
};

const labClient = new LabClient();
const envResponse = await labClient.getVarsByCloudEnvironment(
labApiParms
Expand All @@ -79,12 +66,10 @@ describe.skip("Silent Flow B2C Tests (aad account)", () => {
envResponse[0],
labClient
);

publicClientApplication = new PublicClientApplication({
auth: config.authOptions,
cache: { cachePlugin },
});

msalTokenCache = publicClientApplication.getTokenCache();
server = getTokenSilent(
config,
Expand All @@ -94,28 +79,24 @@ describe.skip("Silent Flow B2C Tests (aad account)", () => {
);
await NodeCacheTestUtils.resetCache(TEST_CACHE_LOCATION);
});

afterAll(async () => {
await browser.close();
if (server) {
server.close();
}
});

describe("AcquireToken", () => {
beforeEach(async () => {
context = await browser.createBrowserContext();
page = await context.newPage();
page.setDefaultTimeout(ONE_SECOND_IN_MS * 5);
await page.goto(homeRoute, { waitUntil: "networkidle0" });
});

afterEach(async () => {
await page.close();
await context.close();
await NodeCacheTestUtils.resetCache(TEST_CACHE_LOCATION);
});

it("Performs acquire token with Auth Code flow", async () => {
const screenshot = new Screenshot(
`${screenshotFolder}/AcquireTokenAuthCode`
Expand All @@ -137,7 +118,29 @@ describe.skip("Silent Flow B2C Tests (aad account)", () => {
expect(cachedTokens.idTokens.length).toBe(1);
expect(cachedTokens.refreshTokens.length).toBe(1);
});

it("Performs acquire token silent when tokens are only present in persistent cache", async () => {
const screenshot = new Screenshot(
`${screenshotFolder}/AcquireTokenSilentFromPersistent`
);
await clickSignIn(page, screenshot);
await b2cAadPpeAccountEnterCredentials(
page,
screenshot,
username,
accountPwd
);
await page.waitForSelector("#acquireTokenSilent");
await publicClientApplication.clearCache();
await screenshot.takeScreenshot(page, "ATS");
await page.click("#acquireTokenSilent");
const cachedTokens = await NodeCacheTestUtils.waitForTokens(
TEST_CACHE_LOCATION,
ONE_SECOND_IN_MS * 2
);
expect(cachedTokens.accessTokens.length).toBe(1);
expect(cachedTokens.idTokens.length).toBe(1);
expect(cachedTokens.refreshTokens.length).toBe(1);
});
it("Performs acquire token silent", async () => {
const screenshot = new Screenshot(
`${screenshotFolder}/AcquireTokenSilent`
Expand All @@ -160,7 +163,6 @@ describe.skip("Silent Flow B2C Tests (aad account)", () => {
const htmlBody = await page.evaluate(() => document.body.innerHTML);
expect(htmlBody).toContain("Silent token acquisition successful");
});

it("Refreshes an expired access token", async () => {
const screenshot = new Screenshot(
`${screenshotFolder}/RefreshExpiredToken`
Expand All @@ -173,7 +175,6 @@ describe.skip("Silent Flow B2C Tests (aad account)", () => {
accountPwd
);
await page.waitForSelector("#acquireTokenSilent");

let tokens = await NodeCacheTestUtils.waitForTokens(
TEST_CACHE_LOCATION,
ONE_SECOND_IN_MS * 2
Expand All @@ -185,7 +186,6 @@ describe.skip("Silent Flow B2C Tests (aad account)", () => {
ONE_SECOND_IN_MS * 2
);
const expiredAccessToken = tokens.accessTokens[0];

// Wait to ensure new token has new iat
await new Promise((r) => setTimeout(r, ONE_SECOND_IN_MS));
await page.click("#acquireTokenSilent");
Expand All @@ -200,7 +200,6 @@ describe.skip("Silent Flow B2C Tests (aad account)", () => {
"acquireTokenSilentGotTokens"
);
const htmlBody = await page.evaluate(() => document.body.innerHTML);

expect(htmlBody).toContain("Silent token acquisition successful");
expect(Number(originalAccessToken.expiresOn)).toBeGreaterThan(0);
expect(Number(expiredAccessToken.expiresOn)).toBe(0);
Expand All @@ -210,21 +209,18 @@ describe.skip("Silent Flow B2C Tests (aad account)", () => {
);
});
});

describe("Get All Accounts", () => {
describe("Authenticated", () => {
beforeEach(async () => {
context = await browser.createBrowserContext();
page = await context.newPage();
await page.goto(homeRoute, { waitUntil: "networkidle0" });
});

afterEach(async () => {
await page.close();
await context.close();
await NodeCacheTestUtils.resetCache(TEST_CACHE_LOCATION);
});

it("Gets all cached accounts", async () => {
const screenshot = new Screenshot(
`${screenshotFolder}/GetAllAccounts`
Expand Down Expand Up @@ -261,20 +257,17 @@ describe.skip("Silent Flow B2C Tests (aad account)", () => {
expect(accounts.length).toBe(1);
});
});

describe("Unauthenticated", () => {
beforeEach(async () => {
context = await browser.createBrowserContext();
page = await context.newPage();
await publicClientApplication.clearCache();
});

afterEach(async () => {
await page.close();
await context.close();
await NodeCacheTestUtils.resetCache(TEST_CACHE_LOCATION);
});

it("Returns empty account array", async () => {
const screenshot = new Screenshot(
`${screenshotFolder}/NoCachedAccounts`
Expand Down
Loading