2021-02-04 21:54:08 +00:00
|
|
|
import { chromium, Page, Browser, BrowserContext, Cookie } from "playwright"
|
|
|
|
import { createCookieIfDoesntExist } from "../src/common/util"
|
|
|
|
import { hash } from "../src/node/util"
|
2021-02-12 19:08:34 +00:00
|
|
|
import { CODE_SERVER_ADDRESS, PASSWORD, STORAGE } from "./constants"
|
2021-01-28 18:48:57 +00:00
|
|
|
|
2021-02-11 18:18:15 +00:00
|
|
|
describe("go home", () => {
|
2021-01-28 18:48:57 +00:00
|
|
|
let browser: Browser
|
|
|
|
let page: Page
|
|
|
|
let context: BrowserContext
|
|
|
|
|
2021-02-11 18:18:15 +00:00
|
|
|
beforeAll(async () => {
|
2021-01-28 23:23:55 +00:00
|
|
|
browser = await chromium.launch()
|
2021-02-01 21:38:53 +00:00
|
|
|
// Create a new context with the saved storage state
|
2021-02-12 19:08:34 +00:00
|
|
|
const storageState = JSON.parse(STORAGE) || {}
|
2021-02-04 21:54:08 +00:00
|
|
|
|
|
|
|
const cookieToStore = {
|
|
|
|
sameSite: "Lax" as const,
|
|
|
|
name: "key",
|
2021-02-12 19:08:34 +00:00
|
|
|
value: hash(PASSWORD),
|
2021-02-04 21:54:08 +00:00
|
|
|
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
|
2021-02-12 18:58:56 +00:00
|
|
|
//
|
|
|
|
// TODO(@jsjoeio)
|
|
|
|
// The playwright storage thing sometimes works and sometimes doesn't. We should investigate this further
|
|
|
|
// at some point.
|
|
|
|
// See discussion: https://github.com/cdr/code-server/pull/2648#discussion_r575434946
|
|
|
|
|
2021-02-04 21:54:08 +00:00
|
|
|
const maybeUpdatedCookies = createCookieIfDoesntExist(cookies, cookieToStore)
|
|
|
|
|
|
|
|
context = await browser.newContext({
|
|
|
|
storageState: { cookies: maybeUpdatedCookies },
|
|
|
|
recordVideo: { dir: "./test/videos/" },
|
|
|
|
})
|
2021-01-28 18:48:57 +00:00
|
|
|
})
|
|
|
|
|
2021-02-11 18:18:15 +00:00
|
|
|
afterAll(async () => {
|
2021-02-01 21:38:53 +00:00
|
|
|
// Remove password from local storage
|
|
|
|
await context.clearCookies()
|
|
|
|
|
2021-01-28 23:23:55 +00:00
|
|
|
await context.close()
|
2021-02-12 19:37:27 +00:00
|
|
|
await browser.close()
|
2021-01-28 18:48:57 +00:00
|
|
|
})
|
|
|
|
|
2021-02-11 18:18:15 +00:00
|
|
|
beforeEach(async () => {
|
2021-01-28 18:48:57 +00:00
|
|
|
page = await context.newPage()
|
|
|
|
})
|
|
|
|
|
2021-02-04 21:54:08 +00:00
|
|
|
// NOTE: this test will fail if you do not run code-server with --home $CODE_SERVER_ADDRESS/healthz
|
2021-02-11 18:18:15 +00:00
|
|
|
it("should see a 'Go Home' button in the Application Menu that goes to /healthz", async () => {
|
2021-02-12 19:08:34 +00:00
|
|
|
const GO_HOME_URL = `${CODE_SERVER_ADDRESS}/healthz`
|
2021-02-02 20:58:51 +00: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-01 21:38:53 +00:00
|
|
|
|
|
|
|
// waitUntil: "domcontentloaded"
|
2021-01-28 23:23:55 +00:00
|
|
|
// In case the page takes a long time to load
|
2021-02-12 19:08:34 +00:00
|
|
|
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "domcontentloaded" })
|
2021-02-02 20:58:51 +00:00
|
|
|
|
2021-02-11 00:00:22 +00:00
|
|
|
// Click the Home menu
|
|
|
|
await page.click(".home-bar ul[aria-label='Home'] li")
|
2021-01-28 18:48:57 +00:00
|
|
|
// See the Go Home button
|
2021-01-28 23:23:55 +00:00
|
|
|
const goHomeButton = "a.action-menu-item span[aria-label='Go Home']"
|
2021-01-28 18:48:57 +00:00
|
|
|
expect(await page.isVisible(goHomeButton))
|
2021-02-02 20:58:51 +00:00
|
|
|
|
|
|
|
// Click it and navigate to /healthz
|
2021-01-28 23:23:55 +00:00
|
|
|
// NOTE: ran into issues of it failing intermittently
|
|
|
|
// without having button: "middle"
|
2021-02-12 19:21:01 +00:00
|
|
|
await Promise.all([page.waitForNavigation(), page.click(goHomeButton, { button: "middle" })])
|
2021-02-11 18:18:15 +00:00
|
|
|
expect(page.url()).toBe(GO_HOME_URL)
|
2021-01-28 18:48:57 +00:00
|
|
|
})
|
|
|
|
})
|