Reworked Azure auth testing

This commit is contained in:
aronmal 2023-07-10 20:52:03 +02:00
parent ce328cd1cd
commit 4020ca40f5
Signed by: aronmal
GPG key ID: 816B7707426FC612
5 changed files with 60 additions and 64 deletions

View file

@ -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}'`)
})
})

16
leaky-ships/build-start-test.sh Executable file
View file

@ -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

View file

@ -1,9 +1,5 @@
module.exports = {
verbose: true,
preset: "jest-puppeteer",
watchPathIgnorePatterns: [
"<rootDir>/(?!__tests__)/", // Exclude all paths except for __tests__
"<rootDir>/(?!\\.next/)/", // Exclude all paths except for .next/
],
setupFiles: ["dotenv/config"],
}

View file

@ -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",

1
leaky-ships/stop-server.sh Executable file
View file

@ -0,0 +1 @@
kill $(lsof -i :3000 -t)