refactor: logout test
This commit is contained in:
parent
c666b47668
commit
fd69f2db88
@ -12,7 +12,7 @@ describe("globalSetup", () => {
|
|||||||
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
|
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should keep us logged in if we don't reset the browser", async () => {
|
it("should keep us logged in using the storageState", async () => {
|
||||||
// See the editor
|
// See the editor
|
||||||
const codeServerEditor = await page.isVisible(".monaco-workbench")
|
const codeServerEditor = await page.isVisible(".monaco-workbench")
|
||||||
expect(codeServerEditor).toBeTruthy()
|
expect(codeServerEditor).toBeTruthy()
|
||||||
|
@ -25,10 +25,16 @@ describe("logout", () => {
|
|||||||
expect(await page.isVisible(logoutButton))
|
expect(await page.isVisible(logoutButton))
|
||||||
|
|
||||||
await page.hover(logoutButton)
|
await page.hover(logoutButton)
|
||||||
|
// TODO(@jsjoeio)
|
||||||
|
// Look into how we're attaching the handlers for the logout feature
|
||||||
|
// We need to see how it's done upstream and add logging to the
|
||||||
|
// handlers themselves.
|
||||||
|
// They may be attached too slowly, hence why we need this timeout
|
||||||
|
await page.waitForTimeout(2000)
|
||||||
|
|
||||||
await page.click(logoutButton)
|
// Recommended by Playwright for async navigation
|
||||||
// it takes a couple seconds for url to change
|
// https://github.com/microsoft/playwright/issues/1987#issuecomment-620182151
|
||||||
await page.waitForLoadState("networkidle")
|
await Promise.all([page.waitForNavigation(), page.click(logoutButton)])
|
||||||
const currentUrl = page.url()
|
const currentUrl = page.url()
|
||||||
expect(currentUrl).toBe(`${CODE_SERVER_ADDRESS}/login`)
|
expect(currentUrl).toBe(`${CODE_SERVER_ADDRESS}/login`)
|
||||||
})
|
})
|
||||||
|
@ -11,10 +11,7 @@ import {
|
|||||||
trimSlashes,
|
trimSlashes,
|
||||||
normalize,
|
normalize,
|
||||||
} from "../../src/common/util"
|
} from "../../src/common/util"
|
||||||
import { Cookie as CookieEnum } from "../../src/node/routes/login"
|
import { loggerModule } from "../utils/helpers"
|
||||||
import { hash } from "../../src/node/util"
|
|
||||||
import { PASSWORD } from "../utils/constants"
|
|
||||||
import { checkForCookie, createCookieIfDoesntExist, loggerModule, Cookie } from "../utils/helpers"
|
|
||||||
|
|
||||||
const dom = new JSDOM()
|
const dom = new JSDOM()
|
||||||
global.document = dom.window.document
|
global.document = dom.window.document
|
||||||
@ -252,58 +249,4 @@ describe("util", () => {
|
|||||||
expect(loggerModule.logger.error).toHaveBeenCalledWith("api: oh no")
|
expect(loggerModule.logger.error).toHaveBeenCalledWith("api: oh no")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("checkForCookie", () => {
|
|
||||||
it("should check if the cookie exists and has a value", () => {
|
|
||||||
const fakeCookies: Cookie[] = [
|
|
||||||
{
|
|
||||||
name: CookieEnum.Key,
|
|
||||||
value: hash(PASSWORD),
|
|
||||||
domain: "localhost",
|
|
||||||
secure: false,
|
|
||||||
sameSite: "Lax",
|
|
||||||
httpOnly: false,
|
|
||||||
expires: 18000,
|
|
||||||
path: "/",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
expect(checkForCookie(fakeCookies, CookieEnum.Key)).toBe(true)
|
|
||||||
})
|
|
||||||
it("should return false if there are no cookies", () => {
|
|
||||||
const fakeCookies: Cookie[] = []
|
|
||||||
expect(checkForCookie(fakeCookies, "key")).toBe(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("createCookieIfDoesntExist", () => {
|
|
||||||
it("should create a cookie if it doesn't exist", () => {
|
|
||||||
const cookies: Cookie[] = []
|
|
||||||
const cookieToStore = {
|
|
||||||
name: CookieEnum.Key,
|
|
||||||
value: hash(PASSWORD),
|
|
||||||
domain: "localhost",
|
|
||||||
secure: false,
|
|
||||||
sameSite: "Lax" as const,
|
|
||||||
httpOnly: false,
|
|
||||||
expires: 18000,
|
|
||||||
path: "/",
|
|
||||||
}
|
|
||||||
expect(createCookieIfDoesntExist(cookies, cookieToStore)).toStrictEqual([cookieToStore])
|
|
||||||
})
|
|
||||||
it("should return the same cookies if the cookie already exists", () => {
|
|
||||||
const PASSWORD = "123supersecure"
|
|
||||||
const cookieToStore = {
|
|
||||||
name: CookieEnum.Key,
|
|
||||||
value: hash(PASSWORD),
|
|
||||||
domain: "localhost",
|
|
||||||
secure: false,
|
|
||||||
sameSite: "Lax" as const,
|
|
||||||
httpOnly: false,
|
|
||||||
expires: 18000,
|
|
||||||
path: "/",
|
|
||||||
}
|
|
||||||
const cookies: Cookie[] = [cookieToStore]
|
|
||||||
expect(createCookieIfDoesntExist(cookies, cookieToStore)).toStrictEqual(cookies)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
@ -1,39 +1,3 @@
|
|||||||
// Borrowed from playwright
|
|
||||||
export interface Cookie {
|
|
||||||
name: string
|
|
||||||
value: string
|
|
||||||
domain: string
|
|
||||||
path: string
|
|
||||||
/**
|
|
||||||
* Unix time in seconds.
|
|
||||||
*/
|
|
||||||
expires: number
|
|
||||||
httpOnly: boolean
|
|
||||||
secure: boolean
|
|
||||||
sameSite: "Strict" | "Lax" | "None"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a cookie exists in array of cookies
|
|
||||||
*/
|
|
||||||
export function checkForCookie(cookies: Array<Cookie>, key: string): boolean {
|
|
||||||
// Check for a cookie where the name is equal to key
|
|
||||||
return Boolean(cookies.find((cookie) => cookie.name === key))
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a login cookie if one doesn't already exist
|
|
||||||
*/
|
|
||||||
export function createCookieIfDoesntExist(cookies: Array<Cookie>, cookieToStore: Cookie): Array<Cookie> {
|
|
||||||
const cookieName = cookieToStore.name
|
|
||||||
const doesCookieExist = checkForCookie(cookies, cookieName)
|
|
||||||
if (!doesCookieExist) {
|
|
||||||
const updatedCookies = [...cookies, cookieToStore]
|
|
||||||
return updatedCookies
|
|
||||||
}
|
|
||||||
return cookies
|
|
||||||
}
|
|
||||||
|
|
||||||
export const loggerModule = {
|
export const loggerModule = {
|
||||||
field: jest.fn(),
|
field: jest.fn(),
|
||||||
level: 2,
|
level: 2,
|
||||||
|
Loading…
Reference in New Issue
Block a user