From b0fd55463bda2855e3e7f53f7c12b2ecaf71257d Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Fri, 12 Feb 2021 12:08:34 -0700 Subject: [PATCH] refactor: add constants.ts with PASSWORD, etc --- test/constants.ts | 3 +++ test/e2e.test.ts | 2 +- test/globalSetup.ts | 5 +++-- test/goHome.test.ts | 9 +++++---- test/login.test.ts | 5 +++-- test/util.test.ts | 3 +-- 6 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 test/constants.ts diff --git a/test/constants.ts b/test/constants.ts new file mode 100644 index 00000000..ac2250e1 --- /dev/null +++ b/test/constants.ts @@ -0,0 +1,3 @@ +export const CODE_SERVER_ADDRESS = process.env.CODE_SERVER_ADDRESS || "http://localhost:8080" +export const PASSWORD = process.env.PASSWORD || "e45432jklfdsab" +export const STORAGE = process.env.STORAGE || "" diff --git a/test/e2e.test.ts b/test/e2e.test.ts index b7cf3739..915b111c 100644 --- a/test/e2e.test.ts +++ b/test/e2e.test.ts @@ -17,7 +17,7 @@ afterEach(async () => { }) it("should see the login page", async () => { - await page.goto("http://localhost:8080") + await page.goto(process.env) // It should send us to the login page expect(await page.title()).toBe("code-server login") }) diff --git a/test/globalSetup.ts b/test/globalSetup.ts index dcf5047d..8a974688 100644 --- a/test/globalSetup.ts +++ b/test/globalSetup.ts @@ -2,6 +2,7 @@ // so that it authenticates us into code-server // ensuring that we're logged in before we run any tests import { chromium } from "playwright" +import { CODE_SERVER_ADDRESS, PASSWORD } from "./constants" module.exports = async () => { console.log("🚨 Running Global Setup for Jest Tests") @@ -10,9 +11,9 @@ module.exports = async () => { const context = await browser.newContext() const page = await context.newPage() - await page.goto(process.env.CODE_SERVER_ADDRESS || "http://localhost:8080", { waitUntil: "domcontentloaded" }) + await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "domcontentloaded" }) // Type in password - await page.fill(".password", process.env.PASSWORD || "password") + await page.fill(".password", PASSWORD) // Click the submit button and login await page.click(".submit") diff --git a/test/goHome.test.ts b/test/goHome.test.ts index 86431c56..e9a56dd5 100644 --- a/test/goHome.test.ts +++ b/test/goHome.test.ts @@ -1,6 +1,7 @@ import { chromium, Page, Browser, BrowserContext, Cookie } from "playwright" import { createCookieIfDoesntExist } from "../src/common/util" import { hash } from "../src/node/util" +import { CODE_SERVER_ADDRESS, PASSWORD, STORAGE } from "./constants" async function setTimeoutPromise(milliseconds: number): Promise { return new Promise((resolve, _) => { @@ -18,12 +19,12 @@ describe("go home", () => { beforeAll(async () => { browser = await chromium.launch() // Create a new context with the saved storage state - const storageState = JSON.parse(process.env.STORAGE || "{}") + const storageState = JSON.parse(STORAGE) || {} const cookieToStore = { sameSite: "Lax" as const, name: "key", - value: hash(process.env.PASSWORD || ""), + value: hash(PASSWORD), domain: "localhost", path: "/", expires: -1, @@ -72,7 +73,7 @@ describe("go home", () => { it("should see a 'Go Home' button in the Application Menu that goes to /healthz", async () => { let requestedGoHomeUrl = false - const GO_HOME_URL = `${process.env.CODE_SERVER_ADDRESS}/healthz` + const GO_HOME_URL = `${CODE_SERVER_ADDRESS}/healthz` page.on("request", (request) => { // This ensures that we did make a request to the GO_HOME_URL // Most reliable way to test button @@ -89,7 +90,7 @@ describe("go home", () => { // waitUntil: "domcontentloaded" // In case the page takes a long time to load - await page.goto(process.env.CODE_SERVER_ADDRESS || "http://localhost:8080", { waitUntil: "domcontentloaded" }) + await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "domcontentloaded" }) // Click the Home menu await page.click(".home-bar ul[aria-label='Home'] li") diff --git a/test/login.test.ts b/test/login.test.ts index 5358adaa..b269acbd 100644 --- a/test/login.test.ts +++ b/test/login.test.ts @@ -1,4 +1,5 @@ import { chromium, Page, Browser, BrowserContext } from "playwright" +import { CODE_SERVER_ADDRESS, PASSWORD } from "./constants" describe("login", () => { let browser: Browser @@ -25,9 +26,9 @@ describe("login", () => { }) it("should be able to login", async () => { - await page.goto(process.env.CODE_SERVER_ADDRESS || "http://localhost:8080") + await page.goto(CODE_SERVER_ADDRESS) // Type in password - await page.fill(".password", process.env.PASSWORD || "password") + await page.fill(".password", PASSWORD) // Click the submit button and login await page.click(".submit") // See the editor diff --git a/test/util.test.ts b/test/util.test.ts index 91a4315f..535b60a1 100644 --- a/test/util.test.ts +++ b/test/util.test.ts @@ -19,6 +19,7 @@ import { } from "../src/common/util" import { Cookie as CookieEnum } from "../src/node/routes/login" import { hash } from "../src/node/util" +import { PASSWORD } from "./constants" const dom = new JSDOM() global.document = dom.window.document @@ -263,7 +264,6 @@ describe("util", () => { describe("checkForCookie", () => { it("should check if the cookie exists and has a value", () => { - const PASSWORD = "123supersecure!" const fakeCookies: Cookie[] = [ { name: CookieEnum.Key, @@ -286,7 +286,6 @@ describe("util", () => { describe("createCookieIfDoesntExist", () => { it("should create a cookie if it doesn't exist", () => { - const PASSWORD = "123supersecure" const cookies: Cookie[] = [] const cookieToStore = { name: CookieEnum.Key,