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"
2
4
3
5
describe ( "login" , ( ) => {
4
6
let browser : Browser
@@ -9,7 +11,37 @@ describe("login", () => {
9
11
browser = await chromium . launch ( )
10
12
// Create a new context with the saved storage state
11
13
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
+ } )
13
45
done ( )
14
46
} )
15
47
@@ -27,7 +59,16 @@ describe("login", () => {
27
59
done ( )
28
60
} )
29
61
62
+ // NOTE: this test will fail if you do not run code-server with --home $CODE_SERVER_ADDRESS/healthz
30
63
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
+
31
72
const GO_HOME_URL = `${ process . env . CODE_SERVER_ADDRESS } /healthz`
32
73
let requestedGoHomeUrl = false
33
74
page . on ( "request" , ( request ) => {
@@ -38,6 +79,7 @@ describe("login", () => {
38
79
if ( request . url ( ) === GO_HOME_URL ) {
39
80
requestedGoHomeUrl = true
40
81
expect ( requestedGoHomeUrl ) . toBeTruthy ( )
82
+ clearTimeout ( backupTimeout )
41
83
42
84
// This ensures Jest knows we're done here.
43
85
done ( )
@@ -52,19 +94,6 @@ describe("login", () => {
52
94
// In case the page takes a long time to load
53
95
await page . goto ( process . env . CODE_SERVER_ADDRESS || "http://localhost:8080" , { waitUntil : "domcontentloaded" } )
54
96
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
-
68
97
// Click the Application menu
69
98
await page . click ( ".menubar-menu-button[title='Application Menu']" )
70
99
// See the Go Home button
0 commit comments