From 4020ca40f5ac0e829aa12003e0f5cd0d106ce58e Mon Sep 17 00:00:00 2001 From: aronmal Date: Mon, 10 Jul 2023 20:52:03 +0200 Subject: [PATCH] Reworked Azure auth testing --- leaky-ships/__tests__/auth.js | 100 +++++++++++++------------------- leaky-ships/build-start-test.sh | 16 +++++ leaky-ships/jest.config.js | 4 -- leaky-ships/package.json | 3 +- leaky-ships/stop-server.sh | 1 + 5 files changed, 60 insertions(+), 64 deletions(-) create mode 100755 leaky-ships/build-start-test.sh create mode 100755 leaky-ships/stop-server.sh diff --git a/leaky-ships/__tests__/auth.js b/leaky-ships/__tests__/auth.js index d629a6b..71f5efd 100644 --- a/leaky-ships/__tests__/auth.js +++ b/leaky-ships/__tests__/auth.js @@ -1,70 +1,52 @@ -describe("Azure AD", () => { - it("Go to MS sign in", async () => { - await page.goto("https://leaky-ships.mal-noh.de/") - await page.screenshot({ path: "__tests__/screenshots/a.png" }) - await page.click("#start") - await page.screenshot({ path: "__tests__/screenshots/b.png" }) - await page.waitForSelector("#login") - await page.screenshot({ path: "__tests__/screenshots/c.png" }) - await page.click("#login") - await page.screenshot({ path: "__tests__/screenshots/d.png" }) - await page.waitForSelector("#microsoft") - await page.screenshot({ path: "__tests__/screenshots/e.png" }) - await page.click("#microsoft") - await page.screenshot({ path: "__tests__/screenshots/f.png" }) - }, 10000) +describe("Check Azure AD auth", () => { + const callbackUrl = "https://leaky-ships.mal-noh.de/" - it("MS sign in", async () => { - // Listen for frame navigations and log the URL changes - // page.on("framenavigated", (frame) => { - // const frameUrl = frame.url() - // console.log("Window Location Changed:", frameUrl) - // }) + it("Login process...", async () => { + let redirected = false + let thirdParty = false + page.on("framenavigated", (frame) => { + if (redirected) return + const frameUrl = frame.url() + // console.log("Window Location Changed:", frameUrl) + if (frameUrl === callbackUrl) redirected = true + }) + try { + await page.goto("https://leaky-ships.mal-noh.de/signin") + await page.waitForSelector("#microsoft") + await page.click("#microsoft") - await page.screenshot({ path: "__tests__/screenshots/1.png" }) - await page.waitForNavigation() - await page.screenshot({ path: "__tests__/screenshots/2.png" }) - // await page.screenshot({ path: "__tests__/screenshots/4.png" }) - const email = process.env.AUTH_EMAIL - const password = process.env.AUTH_PW + thirdParty = true - console.log(email) + await page.waitForNavigation() - await page.waitForSelector('input[type="email"]') - await page.waitForSelector('input[type="password"]') - await page.screenshot({ path: "__tests__/screenshots/3.png" }) + await page.waitForSelector('input[type="email"]') + const emailInput = await page.$('input[type="email"]') + await emailInput.type(process.env.AUTH_EMAIL) - // Find the email input field and type the email - const emailInput = await page.$('input[type="email"]') - await emailInput.type(email) - await page.screenshot({ path: "__tests__/screenshots/4.png" }) + await page.waitForSelector('input[value="Next"]') + const nextInput = await page.$('input[value="Next"]') + await nextInput.click() - await page.waitForSelector('input[value="Next"]') - // Submit the form (if necessary) - const submitInput1 = await page.$('input[type="submit"]') - await submitInput1.click() - await page.screenshot({ path: "__tests__/screenshots/5.png" }) + await page.waitForSelector('input[type="password"]') + const passwordInput = await page.$('input[type="password"]') + await passwordInput.type(process.env.AUTH_PW) - // Find the password input field and type the password - const passwordInput = await page.$('input[type="password"]') - await passwordInput.type(password) - await page.screenshot({ path: "__tests__/screenshots/6.png" }) + await page.waitForSelector('input[value="Sign in"]') + const signinInput = await page.$('input[value="Sign in"]') + await signinInput.click() - await page.waitForSelector('input[value="Sign in"]') - // Submit the form (if necessary) - const submitInput = await page.$('input[value="Sign in"]') - await submitInput.click() - await page.screenshot({ path: "__tests__/screenshots/7.png" }) + await page.waitForSelector('input[value="No"]') + const noInput = await page.$('input[value="No"]') + await noInput.click() - await page.waitForSelector('input[value="No"]') - // Submit the form (if necessary) - const noInput = await page.$('input[value="No"]') - await noInput.click() - await page.screenshot({ path: "__tests__/screenshots/8.png" }) - - const callbackUrl = "https://leaky-ships.mal-noh.de/" - await page.waitForFunction(`window.location.href === '${callbackUrl}'`) - // await page.waitForNavigation({ waitUntil: "networkidle0" }) - await page.screenshot({ path: "__tests__/screenshots/9.png" }) + await page.waitForFunction(`window.location.href === '${callbackUrl}'`) + } catch (e) { + if (!redirected || thirdParty) throw e + } }, 15000) + + it("Is logged in", async () => { + await page.goto("https://leaky-ships.mal-noh.de/signin") + await page.waitForFunction(`window.location.href === '${callbackUrl}'`) + }) }) diff --git a/leaky-ships/build-start-test.sh b/leaky-ships/build-start-test.sh new file mode 100755 index 0000000..23b9786 --- /dev/null +++ b/leaky-ships/build-start-test.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Build the project +pnpm run build + +# Start the server in the background +pnpm run start & + +# Store the process ID (PID) of the server +SERVER_PID=$! + +# Run the tests +pnpm test + +# Abort the server process +kill $SERVER_PID diff --git a/leaky-ships/jest.config.js b/leaky-ships/jest.config.js index f5bcc30..9c24ba5 100644 --- a/leaky-ships/jest.config.js +++ b/leaky-ships/jest.config.js @@ -1,9 +1,5 @@ module.exports = { verbose: true, preset: "jest-puppeteer", - watchPathIgnorePatterns: [ - "/(?!__tests__)/", // Exclude all paths except for __tests__ - "/(?!\\.next/)/", // Exclude all paths except for .next/ - ], setupFiles: ["dotenv/config"], } diff --git a/leaky-ships/package.json b/leaky-ships/package.json index ef1cf7e..08b0342 100644 --- a/leaky-ships/package.json +++ b/leaky-ships/package.json @@ -7,7 +7,8 @@ "build": "next build", "start": "next start", "lint": "next lint", - "test": "jest --watchAll" + "test": "jest -w 1", + "build-start-test": "./build-start-test.sh" }, "dependencies": { "@fortawesome/fontawesome-svg-core": "^6.4.0", diff --git a/leaky-ships/stop-server.sh b/leaky-ships/stop-server.sh new file mode 100755 index 0000000..1498237 --- /dev/null +++ b/leaky-ships/stop-server.sh @@ -0,0 +1 @@ +kill $(lsof -i :3000 -t) \ No newline at end of file