Skip to content

Commit 2dc56ad

Browse files
committed
refactor: manually add cookie goHome
1 parent b02d2fb commit 2dc56ad

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

test/goHome.test.ts

+44-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { chromium, Page, Browser, BrowserContext } from "playwright"
1+
import { chromium, Page, Browser, BrowserContext, Cookie } from "playwright"
2+
import { createCookieIfDoesntExist } from "../src/common/util"
3+
import { hash } from "../src/node/util"
24

35
describe("login", () => {
46
let browser: Browser
@@ -9,7 +11,37 @@ describe("login", () => {
911
browser = await chromium.launch()
1012
// Create a new context with the saved storage state
1113
const storageState = JSON.parse(process.env.STORAGE || "")
12-
context = await browser.newContext({ storageState, recordVideo: { dir: "./test/videos/" } })
14+
15+
//
16+
const cookieToStore = {
17+
sameSite: "Lax" as const,
18+
name: "key",
19+
value: hash(process.env.PASSWORD || ""),
20+
domain: "localhost",
21+
path: "/",
22+
expires: -1,
23+
httpOnly: false,
24+
secure: false,
25+
}
26+
27+
// For some odd reason, the login method used in globalSetup.ts doesn't always work
28+
// I don't know if it's on playwright clearing our cookies by accident
29+
// or if it's our cookies disappearing.
30+
// This means we need an additional check to make sure we're logged in.
31+
// We do this by manually adding the cookie to the browser environment
32+
// if it's not there at the time the test starts
33+
const cookies: Cookie[] = storageState.cookies || []
34+
// If the cookie exists in cookies then
35+
// this will return the cookies with no changes
36+
// otherwise if it doesn't exist, it will create it
37+
// hence the name maybeUpdatedCookies
38+
const maybeUpdatedCookies = createCookieIfDoesntExist(cookies, cookieToStore)
39+
console.log("here are the cookies", maybeUpdatedCookies)
40+
41+
context = await browser.newContext({
42+
storageState: { cookies: maybeUpdatedCookies },
43+
recordVideo: { dir: "./test/videos/" },
44+
})
1345
done()
1446
})
1547

@@ -27,7 +59,16 @@ describe("login", () => {
2759
done()
2860
})
2961

62+
// NOTE: this test will fail if you do not run code-server with --home $CODE_SERVER_ADDRESS/healthz
3063
it("should see a 'Go Home' button in the Application Menu that goes to /healthz", async (done) => {
64+
// Ideally, this test should pass and finish before the timeout set in the Jest config
65+
// However, if it doesn't, we don't want a memory leak so we set this backup timeout
66+
// Otherwise Jest may throw this error
67+
// "Jest did not exit one second after the test run has completed.
68+
// This usually means that there are asynchronous operations that weren't stopped in your tests.
69+
// Consider running Jest with `--detectOpenHandles` to troubleshoot this issue."
70+
const backupTimeout = setTimeout(() => done(), 20000)
71+
3172
const GO_HOME_URL = `${process.env.CODE_SERVER_ADDRESS}/healthz`
3273
let requestedGoHomeUrl = false
3374
page.on("request", (request) => {
@@ -38,6 +79,7 @@ describe("login", () => {
3879
if (request.url() === GO_HOME_URL) {
3980
requestedGoHomeUrl = true
4081
expect(requestedGoHomeUrl).toBeTruthy()
82+
clearTimeout(backupTimeout)
4183

4284
// This ensures Jest knows we're done here.
4385
done()
@@ -52,19 +94,6 @@ describe("login", () => {
5294
// In case the page takes a long time to load
5395
await page.goto(process.env.CODE_SERVER_ADDRESS || "http://localhost:8080", { waitUntil: "domcontentloaded" })
5496

55-
// For some odd reason, the login method used in globalSetup.ts
56-
// I don't know if it's on playwright clearing our cookies by accident
57-
// or if it's our cookies disappearing.
58-
// This means we need an additional check to make sure we're logged in
59-
// otherwise this test will hang and fail.
60-
const currentPageURL = await page.url()
61-
const isLoginPage = currentPageURL.includes("login")
62-
if (isLoginPage) {
63-
await page.fill(".password", process.env.PASSWORD || "password")
64-
// Click the submit button and login
65-
await page.click(".submit")
66-
}
67-
6897
// Click the Application menu
6998
await page.click(".menubar-menu-button[title='Application Menu']")
7099
// See the Go Home button

0 commit comments

Comments
 (0)