2021-02-05 05:54:08 +08:00
|
|
|
import { chromium, Page, Browser, BrowserContext, Cookie } from "playwright"
|
|
|
|
import { createCookieIfDoesntExist } from "../src/common/util"
|
|
|
|
import { hash } from "../src/node/util"
|
2021-01-29 02:48:57 +08:00
|
|
|
|
|
|
|
describe("login", () => {
|
|
|
|
let browser: Browser
|
|
|
|
let page: Page
|
|
|
|
let context: BrowserContext
|
|
|
|
|
2021-02-03 04:58:51 +08:00
|
|
|
beforeAll(async (done) => {
|
2021-01-29 07:23:55 +08:00
|
|
|
browser = await chromium.launch()
|
2021-02-02 05:38:53 +08:00
|
|
|
// Create a new context with the saved storage state
|
|
|
|
const storageState = JSON.parse(process.env.STORAGE || "")
|
2021-02-05 05:54:08 +08:00
|
|
|
|
|
|
|
const cookieToStore = {
|
|
|
|
sameSite: "Lax" as const,
|
|
|
|
name: "key",
|
|
|
|
value: hash(process.env.PASSWORD || ""),
|
|
|
|
domain: "localhost",
|
|
|
|
path: "/",
|
|
|
|
expires: -1,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
// For some odd reason, the login method used in globalSetup.ts doesn't always work
|
|
|
|
// I don't know if it's on playwright clearing our cookies by accident
|
|
|
|
// or if it's our cookies disappearing.
|
|
|
|
// This means we need an additional check to make sure we're logged in.
|
|
|
|
// We do this by manually adding the cookie to the browser environment
|
|
|
|
// if it's not there at the time the test starts
|
|
|
|
const cookies: Cookie[] = storageState.cookies || []
|
|
|
|
// If the cookie exists in cookies then
|
|
|
|
// this will return the cookies with no changes
|
|
|
|
// otherwise if it doesn't exist, it will create it
|
|
|
|
// hence the name maybeUpdatedCookies
|
|
|
|
const maybeUpdatedCookies = createCookieIfDoesntExist(cookies, cookieToStore)
|
|
|
|
|
|
|
|
context = await browser.newContext({
|
|
|
|
storageState: { cookies: maybeUpdatedCookies },
|
|
|
|
recordVideo: { dir: "./test/videos/" },
|
|
|
|
})
|
2021-02-03 04:58:51 +08:00
|
|
|
done()
|
2021-01-29 02:48:57 +08:00
|
|
|
})
|
|
|
|
|
2021-02-03 04:58:51 +08:00
|
|
|
afterAll(async (done) => {
|
2021-02-02 05:38:53 +08:00
|
|
|
// Remove password from local storage
|
|
|
|
await context.clearCookies()
|
|
|
|
|
2021-01-29 02:48:57 +08:00
|
|
|
await browser.close()
|
2021-01-29 07:23:55 +08:00
|
|
|
await context.close()
|
2021-02-03 04:58:51 +08:00
|
|
|
done()
|
2021-01-29 02:48:57 +08:00
|
|
|
})
|
|
|
|
|
2021-02-03 04:58:51 +08:00
|
|
|
beforeEach(async (done) => {
|
2021-01-29 02:48:57 +08:00
|
|
|
page = await context.newPage()
|
2021-02-03 04:58:51 +08:00
|
|
|
done()
|
2021-01-29 02:48:57 +08:00
|
|
|
})
|
|
|
|
|
2021-02-05 05:54:08 +08:00
|
|
|
// NOTE: this test will fail if you do not run code-server with --home $CODE_SERVER_ADDRESS/healthz
|
2021-02-03 04:58:51 +08:00
|
|
|
it("should see a 'Go Home' button in the Application Menu that goes to /healthz", async (done) => {
|
2021-02-05 05:54:26 +08:00
|
|
|
let requestedGoHomeUrl = false
|
2021-02-05 05:54:08 +08:00
|
|
|
// Ideally, this test should pass and finish before the timeout set in the Jest config
|
|
|
|
// However, if it doesn't, we don't want a memory leak so we set this backup timeout
|
|
|
|
// Otherwise Jest may throw this error
|
|
|
|
// "Jest did not exit one second after the test run has completed.
|
|
|
|
// This usually means that there are asynchronous operations that weren't stopped in your tests.
|
|
|
|
// Consider running Jest with `--detectOpenHandles` to troubleshoot this issue."
|
2021-02-05 05:54:26 +08:00
|
|
|
const backupTimeout = setTimeout(() => {
|
|
|
|
// If it's not true by this point then the test should fail
|
|
|
|
expect(requestedGoHomeUrl).toBeTruthy()
|
|
|
|
done()
|
|
|
|
}, 20000)
|
2021-02-05 05:54:08 +08:00
|
|
|
|
2021-01-29 07:23:55 +08:00
|
|
|
const GO_HOME_URL = `${process.env.CODE_SERVER_ADDRESS}/healthz`
|
|
|
|
page.on("request", (request) => {
|
|
|
|
// This ensures that we did make a request to the GO_HOME_URL
|
|
|
|
// Most reliable way to test button
|
|
|
|
// because we don't care if the request has a response
|
|
|
|
// only that it was made
|
|
|
|
if (request.url() === GO_HOME_URL) {
|
|
|
|
requestedGoHomeUrl = true
|
2021-02-03 04:58:51 +08:00
|
|
|
expect(requestedGoHomeUrl).toBeTruthy()
|
2021-02-05 05:54:08 +08:00
|
|
|
clearTimeout(backupTimeout)
|
2021-02-03 04:58:51 +08:00
|
|
|
|
|
|
|
// This ensures Jest knows we're done here.
|
|
|
|
done()
|
2021-01-29 07:23:55 +08:00
|
|
|
}
|
|
|
|
})
|
2021-02-03 04:58:51 +08:00
|
|
|
// Sometimes a dialog shows up when you navigate
|
|
|
|
// asking if you're sure you want to leave
|
|
|
|
// so we listen if it comes, we accept it
|
|
|
|
page.on("dialog", (dialog) => dialog.accept())
|
2021-02-02 05:38:53 +08:00
|
|
|
|
|
|
|
// waitUntil: "domcontentloaded"
|
2021-01-29 07:23:55 +08:00
|
|
|
// In case the page takes a long time to load
|
2021-02-02 05:38:53 +08:00
|
|
|
await page.goto(process.env.CODE_SERVER_ADDRESS || "http://localhost:8080", { waitUntil: "domcontentloaded" })
|
2021-02-03 04:58:51 +08:00
|
|
|
|
2021-02-11 08:00:22 +08:00
|
|
|
// Click the Home menu
|
|
|
|
await page.click(".home-bar ul[aria-label='Home'] li")
|
2021-01-29 02:48:57 +08:00
|
|
|
// See the Go Home button
|
2021-01-29 07:23:55 +08:00
|
|
|
const goHomeButton = "a.action-menu-item span[aria-label='Go Home']"
|
2021-01-29 02:48:57 +08:00
|
|
|
expect(await page.isVisible(goHomeButton))
|
2021-02-03 04:58:51 +08:00
|
|
|
|
|
|
|
// Click it and navigate to /healthz
|
2021-01-29 07:23:55 +08:00
|
|
|
// NOTE: ran into issues of it failing intermittently
|
|
|
|
// without having button: "middle"
|
|
|
|
await page.click(goHomeButton, { button: "middle" })
|
2021-01-29 02:48:57 +08:00
|
|
|
})
|
|
|
|
})
|