Skip to content

Commit fd69f2d

Browse files
committed
refactor: logout test
1 parent c666b47 commit fd69f2d

File tree

4 files changed

+11
-98
lines changed

4 files changed

+11
-98
lines changed

test/e2e/globalSetup.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("globalSetup", () => {
1212
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
1313
})
1414

15-
it("should keep us logged in if we don't reset the browser", async () => {
15+
it("should keep us logged in using the storageState", async () => {
1616
// See the editor
1717
const codeServerEditor = await page.isVisible(".monaco-workbench")
1818
expect(codeServerEditor).toBeTruthy()

test/e2e/logout.test.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ describe("logout", () => {
2525
expect(await page.isVisible(logoutButton))
2626

2727
await page.hover(logoutButton)
28+
// TODO(@jsjoeio)
29+
// Look into how we're attaching the handlers for the logout feature
30+
// We need to see how it's done upstream and add logging to the
31+
// handlers themselves.
32+
// They may be attached too slowly, hence why we need this timeout
33+
await page.waitForTimeout(2000)
2834

29-
await page.click(logoutButton)
30-
// it takes a couple seconds for url to change
31-
await page.waitForLoadState("networkidle")
35+
// Recommended by Playwright for async navigation
36+
// https://github.com/microsoft/playwright/issues/1987#issuecomment-620182151
37+
await Promise.all([page.waitForNavigation(), page.click(logoutButton)])
3238
const currentUrl = page.url()
3339
expect(currentUrl).toBe(`${CODE_SERVER_ADDRESS}/login`)
3440
})

test/unit/util.test.ts

+1-58
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import {
1111
trimSlashes,
1212
normalize,
1313
} from "../../src/common/util"
14-
import { Cookie as CookieEnum } from "../../src/node/routes/login"
15-
import { hash } from "../../src/node/util"
16-
import { PASSWORD } from "../utils/constants"
17-
import { checkForCookie, createCookieIfDoesntExist, loggerModule, Cookie } from "../utils/helpers"
14+
import { loggerModule } from "../utils/helpers"
1815

1916
const dom = new JSDOM()
2017
global.document = dom.window.document
@@ -252,58 +249,4 @@ describe("util", () => {
252249
expect(loggerModule.logger.error).toHaveBeenCalledWith("api: oh no")
253250
})
254251
})
255-
256-
describe("checkForCookie", () => {
257-
it("should check if the cookie exists and has a value", () => {
258-
const fakeCookies: Cookie[] = [
259-
{
260-
name: CookieEnum.Key,
261-
value: hash(PASSWORD),
262-
domain: "localhost",
263-
secure: false,
264-
sameSite: "Lax",
265-
httpOnly: false,
266-
expires: 18000,
267-
path: "/",
268-
},
269-
]
270-
expect(checkForCookie(fakeCookies, CookieEnum.Key)).toBe(true)
271-
})
272-
it("should return false if there are no cookies", () => {
273-
const fakeCookies: Cookie[] = []
274-
expect(checkForCookie(fakeCookies, "key")).toBe(false)
275-
})
276-
})
277-
278-
describe("createCookieIfDoesntExist", () => {
279-
it("should create a cookie if it doesn't exist", () => {
280-
const cookies: Cookie[] = []
281-
const cookieToStore = {
282-
name: CookieEnum.Key,
283-
value: hash(PASSWORD),
284-
domain: "localhost",
285-
secure: false,
286-
sameSite: "Lax" as const,
287-
httpOnly: false,
288-
expires: 18000,
289-
path: "/",
290-
}
291-
expect(createCookieIfDoesntExist(cookies, cookieToStore)).toStrictEqual([cookieToStore])
292-
})
293-
it("should return the same cookies if the cookie already exists", () => {
294-
const PASSWORD = "123supersecure"
295-
const cookieToStore = {
296-
name: CookieEnum.Key,
297-
value: hash(PASSWORD),
298-
domain: "localhost",
299-
secure: false,
300-
sameSite: "Lax" as const,
301-
httpOnly: false,
302-
expires: 18000,
303-
path: "/",
304-
}
305-
const cookies: Cookie[] = [cookieToStore]
306-
expect(createCookieIfDoesntExist(cookies, cookieToStore)).toStrictEqual(cookies)
307-
})
308-
})
309252
})

test/utils/helpers.ts

-36
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,3 @@
1-
// Borrowed from playwright
2-
export interface Cookie {
3-
name: string
4-
value: string
5-
domain: string
6-
path: string
7-
/**
8-
* Unix time in seconds.
9-
*/
10-
expires: number
11-
httpOnly: boolean
12-
secure: boolean
13-
sameSite: "Strict" | "Lax" | "None"
14-
}
15-
16-
/**
17-
* Checks if a cookie exists in array of cookies
18-
*/
19-
export function checkForCookie(cookies: Array<Cookie>, key: string): boolean {
20-
// Check for a cookie where the name is equal to key
21-
return Boolean(cookies.find((cookie) => cookie.name === key))
22-
}
23-
24-
/**
25-
* Creates a login cookie if one doesn't already exist
26-
*/
27-
export function createCookieIfDoesntExist(cookies: Array<Cookie>, cookieToStore: Cookie): Array<Cookie> {
28-
const cookieName = cookieToStore.name
29-
const doesCookieExist = checkForCookie(cookies, cookieName)
30-
if (!doesCookieExist) {
31-
const updatedCookies = [...cookies, cookieToStore]
32-
return updatedCookies
33-
}
34-
return cookies
35-
}
36-
371
export const loggerModule = {
382
field: jest.fn(),
393
level: 2,

0 commit comments

Comments
 (0)