|
| 1 | +import * as puppeteer from "puppeteer"; |
| 2 | +import { Screenshot, setupCredentials, enterCredentials } from "../../../e2eTestUtils/TestUtils"; |
| 3 | +import { LabClient } from "../../../e2eTestUtils/LabClient"; |
| 4 | +import { LabApiQueryParams } from "../../../e2eTestUtils/LabApiQueryParams"; |
| 5 | +import { AzureEnvironments, AppTypes } from "../../../e2eTestUtils/Constants"; |
| 6 | +import { BrowserCacheUtils } from "../../../e2eTestUtils/BrowserCacheTestUtils"; |
| 7 | + |
| 8 | +const SCREENSHOT_BASE_FOLDER_NAME = `${__dirname}/screenshots/home-tests`; |
| 9 | + |
| 10 | +async function verifyTokenStore(BrowserCache: BrowserCacheUtils, scopes: string[]): Promise<void> { |
| 11 | + const tokenStore = await BrowserCache.getTokens(); |
| 12 | + expect(tokenStore.idTokens.length).toBe(1); |
| 13 | + expect(tokenStore.accessTokens.length).toBe(1); |
| 14 | + expect(tokenStore.refreshTokens.length).toBe(1); |
| 15 | + expect(await BrowserCache.getAccountFromCache(tokenStore.idTokens[0])).not.toBeNull(); |
| 16 | + expect(await BrowserCache.accessTokenForScopesExists(tokenStore.accessTokens, scopes)).toBeTruthy; |
| 17 | + const storage = await BrowserCache.getWindowStorage(); |
| 18 | + expect(Object.keys(storage).length).toBe(4); |
| 19 | +} |
| 20 | + |
| 21 | +describe('/ (Home Page)', () => { |
| 22 | + let browser: puppeteer.Browser; |
| 23 | + let context: puppeteer.BrowserContext; |
| 24 | + let page: puppeteer.Page; |
| 25 | + let port: number; |
| 26 | + let username: string; |
| 27 | + let accountPwd: string; |
| 28 | + let BrowserCache: BrowserCacheUtils; |
| 29 | + |
| 30 | + beforeAll(async () => { |
| 31 | + // @ts-ignore |
| 32 | + browser = await global.__BROWSER__; |
| 33 | + // @ts-ignore |
| 34 | + port = global.__PORT__; |
| 35 | + |
| 36 | + const labApiParams: LabApiQueryParams = { |
| 37 | + azureEnvironment: AzureEnvironments.PPE, |
| 38 | + appType: AppTypes.CLOUD |
| 39 | + }; |
| 40 | + |
| 41 | + const labClient = new LabClient(); |
| 42 | + const envResponse = await labClient.getVarsByCloudEnvironment(labApiParams); |
| 43 | + |
| 44 | + [username, accountPwd] = await setupCredentials(envResponse[0], labClient); |
| 45 | + }); |
| 46 | + |
| 47 | + beforeEach(async () => { |
| 48 | + context = await browser.createIncognitoBrowserContext(); |
| 49 | + page = await context.newPage(); |
| 50 | + BrowserCache = new BrowserCacheUtils(page, "localStorage"); |
| 51 | + await page.goto(`http://localhost:${port}`); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(async () => { |
| 55 | + await page.close(); |
| 56 | + await context.close(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("Home page - children are rendered after logging in with loginRedirect", async () => { |
| 60 | + const testName = "redirectBaseCase"; |
| 61 | + const screenshot = new Screenshot(`${SCREENSHOT_BASE_FOLDER_NAME}/${testName}`); |
| 62 | + await screenshot.takeScreenshot(page, "Page loaded"); |
| 63 | + |
| 64 | + // Initiate Login |
| 65 | + const [signInButton] = await page.$x("//button[contains(., 'Login')]"); |
| 66 | + await signInButton.click(); |
| 67 | + await page.waitForTimeout(70); |
| 68 | + await screenshot.takeScreenshot(page, "Login button clicked"); |
| 69 | + const [loginRedirectButton] = await page.$x("//div//button[contains(., 'Login using Redirect')]"); |
| 70 | + await loginRedirectButton.click(); |
| 71 | + |
| 72 | + await enterCredentials(page, screenshot, username, accountPwd); |
| 73 | + |
| 74 | + // Verify UI now displays logged in content |
| 75 | + const [signedIn] = await page.$x("//p[contains(., 'Login successful!')]"); |
| 76 | + expect(signedIn).toBeDefined(); |
| 77 | + const [logoutButton] = await page.$x("//button[contains(., 'Logout')]"); |
| 78 | + await logoutButton.click(); |
| 79 | + const logoutButtons = await page.$x("//div//button[contains(., 'Logout using')]"); |
| 80 | + expect(logoutButtons.length).toBe(2); |
| 81 | + await logoutButton.click(); |
| 82 | + await screenshot.takeScreenshot(page, "App signed in"); |
| 83 | + |
| 84 | + // Verify tokens are in cache |
| 85 | + await verifyTokenStore(BrowserCache, ["User.Read"]); |
| 86 | + |
| 87 | + // Navigate to profile page |
| 88 | + const [profileButton] = await page.$x("//span[contains(., 'Profile')]"); |
| 89 | + await profileButton.click(); |
| 90 | + await screenshot.takeScreenshot(page, "Profile page loaded"); |
| 91 | + |
| 92 | + // Verify displays profile page without activating MsalGuard |
| 93 | + const [profileFirstName] = await page.$x("//strong[contains(., 'First Name: ')]"); |
| 94 | + expect(profileFirstName).toBeDefined(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("Home page - children are rendered after logging in with loginPopup", async () => { |
| 98 | + const testName = "popupBaseCase"; |
| 99 | + const screenshot = new Screenshot(`${SCREENSHOT_BASE_FOLDER_NAME}/${testName}`); |
| 100 | + await screenshot.takeScreenshot(page, "Page loaded"); |
| 101 | + |
| 102 | + // Initiate Login |
| 103 | + const [signInButton] = await page.$x("//button[contains(., 'Login')]"); |
| 104 | + await signInButton.click(); |
| 105 | + await page.waitForTimeout(70); |
| 106 | + await screenshot.takeScreenshot(page, "Login button clicked"); |
| 107 | + const [loginPopupButton] = await page.$x("//button[contains(., 'Login using Popup')]"); |
| 108 | + const newPopupWindowPromise = new Promise<puppeteer.Page>(resolve => page.once("popup", resolve)); |
| 109 | + await loginPopupButton.click(); |
| 110 | + const popupPage = await newPopupWindowPromise; |
| 111 | + const popupWindowClosed = new Promise<void>(resolve => popupPage.once("close", resolve)); |
| 112 | + |
| 113 | + await enterCredentials(popupPage, screenshot, username, accountPwd); |
| 114 | + await popupWindowClosed; |
| 115 | + |
| 116 | + await page.waitForXPath("//p[contains(., 'Login successful!')]", {timeout: 3000}); |
| 117 | + await screenshot.takeScreenshot(page, "Popup closed"); |
| 118 | + |
| 119 | + // Verify UI now displays logged in content |
| 120 | + const [signedIn] = await page.$x("//p[contains(., 'Login successful!')]"); |
| 121 | + expect(signedIn).toBeDefined(); |
| 122 | + const [logoutButton] = await page.$x("//button[contains(., 'Logout')]"); |
| 123 | + await logoutButton.click(); |
| 124 | + const logoutButtons = await page.$x("//button[contains(., 'Logout using')]"); |
| 125 | + expect(logoutButtons.length).toBe(2); |
| 126 | + await logoutButton.click(); |
| 127 | + await screenshot.takeScreenshot(page, "App signed in"); |
| 128 | + |
| 129 | + // Verify tokens are in cache |
| 130 | + await verifyTokenStore(BrowserCache, ["User.Read"]); |
| 131 | + |
| 132 | + // Navigate to profile page |
| 133 | + const [profileButton] = await page.$x("//span[contains(., 'Profile')]"); |
| 134 | + await profileButton.click(); |
| 135 | + await screenshot.takeScreenshot(page, "Profile page loaded"); |
| 136 | + |
| 137 | + // Verify displays profile page without activating MsalGuard |
| 138 | + const [profileFirstName] = await page.$x("//strong[contains(., 'First Name: ')]"); |
| 139 | + expect(profileFirstName).toBeDefined(); |
| 140 | + }); |
| 141 | + } |
| 142 | +); |
0 commit comments