/* * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ import * as puppeteer from "puppeteer"; import { Screenshot, createFolder, setupCredentials, RETRY_TIMES, approveRemoteConnect, enterCredentials, enterDeviceCode, validateCacheLocation, NodeCacheTestUtils, LabClient, LabApiQueryParams, AppTypes, AzureEnvironments, } from "e2e-test-utils"; import { Configuration, PublicClientApplication } from "@azure/msal-node"; import path from "path"; // Set test cache name/location const TEST_CACHE_LOCATION = `${__dirname}/data/aad.cache.json`; // Get flow-specific routes from sample application const getTokenDeviceCode = require("../index"); // Build cachePlugin const cachePlugin = require("../../cachePlugin.js")(TEST_CACHE_LOCATION); // Load scenario configuration const config = require("../config/AAD.json"); describe("Device Code AAD Prod Tests", () => { jest.setTimeout(120000); jest.retryTimes(RETRY_TIMES); let browser: puppeteer.Browser; let context: puppeteer.BrowserContext; let page: puppeteer.Page; let publicClientApplication: PublicClientApplication; let clientConfig: Configuration; let username: string; let accountPwd: string; const screenshotFolder = path.join(__dirname, "screenshots/device-code/aad"); beforeAll(async () => { await validateCacheLocation(TEST_CACHE_LOCATION); // @ts-ignore browser = await global.__BROWSER__; createFolder(screenshotFolder); // Configure Lab API Query Parameters const labApiParms: LabApiQueryParams = { azureEnvironment: AzureEnvironments.CLOUD, appType: AppTypes.CLOUD, }; const labClient = new LabClient(); const envResponse = await labClient.getVarsByCloudEnvironment( labApiParms ); [username, accountPwd] = await setupCredentials( envResponse[0], labClient ); }); afterAll(async () => { await browser.close(); }); describe("Acquire Token", () => { beforeAll(async () => { clientConfig = { auth: config.authOptions, cache: { cachePlugin } }; publicClientApplication = new PublicClientApplication(clientConfig); }); beforeEach(async () => { context = await browser.createBrowserContext(); page = await context.newPage(); page.setDefaultTimeout(30000); }); afterEach(async () => { await page.close(); await context.close(); await NodeCacheTestUtils.resetCache(TEST_CACHE_LOCATION); }); it("Performs acquire token with Device Code flow", async () => { const screenshot = new Screenshot(`${screenshotFolder}/BaseCase`); const deviceCodeCallback = async (deviceCodeResponse: any) => { const { userCode, verificationUri } = deviceCodeResponse; await enterDeviceCode( page, screenshot, userCode, verificationUri ); await approveRemoteConnect(page, screenshot); await enterCredentials(page, screenshot, username, accountPwd); // The AAD redirect chain can briefly detach the current frame between // navigations. Retry until the success page settles. for (let i = 0; i < 5; i++) { try { await page.waitForSelector("#message", { timeout: 15000 }); break; } catch (e) { if (i >= 4 || !String(e).includes("detached")) throw e; await new Promise((r) => setTimeout(r, 1000)); } } await screenshot.takeScreenshot( page, "SuccessfulDeviceCodeMessage" ); }; await getTokenDeviceCode(config, publicClientApplication, { deviceCodeCallback: deviceCodeCallback, }); const cachedTokens = await NodeCacheTestUtils.waitForTokens( TEST_CACHE_LOCATION, 2000 ); expect(cachedTokens.accessTokens.length).toBe(1); expect(cachedTokens.idTokens.length).toBe(1); expect(cachedTokens.refreshTokens.length).toBe(1); }); }); });