diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index b1922382..90250dc2 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -44,12 +44,6 @@ jobs: GITHUB_ID: ${{ secrets.E2E_GITHUB_ID }} GITHUB_SECRET: ${{ secrets.E2E_GITHUB_SECRET }} NEXTAUTH_SECRET: ${{ secrets.NEXTAUTH_SECRET }} - E2E_USER_ONE_EMAIL: e2e-user-one@codu.co - E2E_USER_ONE_ID: 8e3179ce-f32b-4d0a-ba3b-234d66b836ad - E2E_USER_ONE_SESSION_ID: df8a11f2-f20a-43d6-80a0-a213f1efedc1 - E2E_USER_TWO_EMAIL: e2e-user-two@codu.co - E2E_USER_TWO_ID: a15a104a-0e34-4101-8800-ed25c9231345 - E2E_USER_TWO_SESSION_ID: 10134766-bc6c-4b52-83d7-46ec0a4cb95d steps: - name: Checkout repository diff --git a/E2E Overview.md b/E2E Overview.md index 54197f59..d99e9104 100644 --- a/E2E Overview.md +++ b/E2E Overview.md @@ -2,15 +2,7 @@ To run the end-to-end tests using Playwright, you need to configure your environment and follow these steps: -### Environment Variables -Ensure you have the following variables set in your `.env` file: - -- `E2E_USER_ID`: The ID of the E2E user for testing. -- `E2E_USER_EMAIL`: The email of the E2E user for testing. -- `E2E_USER_ONE_SESSION_ID`: The session ID that the user will use to authenticate. - -Note: The sample `.env` file is fine to use. ### Session and User Setup @@ -38,11 +30,9 @@ For UI mode: npx playwright test --ui ``` -### Additional E2E Environment Variables +### Additional E2E constants - **E2E_USER_ONE_SESSION_ID**: This is the session token UUID for one E2E user. - **E2E_USER_TWO_SESSION_ID**: This is the session token UUID for another E2E user. - **E2E_USER_ONE_ID**: The user ID of one of the E2E users. - **E2E_USER_TWO_ID**: The user ID of another E2E user. - -These values are currently hardcoded and should be unique for each user. diff --git a/drizzle/seed.ts b/drizzle/seed.ts index ce795e56..87991d02 100644 --- a/drizzle/seed.ts +++ b/drizzle/seed.ts @@ -7,22 +7,16 @@ import "dotenv/config"; import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js"; import postgres from "postgres"; +import { + E2E_USER_ONE_EMAIL, + E2E_USER_ONE_ID, + E2E_USER_TWO_EMAIL, + E2E_USER_TWO_ID, + E2E_USER_ONE_SESSION_ID, + E2E_USER_TWO_SESSION_ID, +} from "../e2e/constants"; const DATABASE_URL = process.env.DATABASE_URL || ""; -// These can be removed in a follow on PR. Until this hits main we cant add E2E_USER_* stuff to the env. -const E2E_USER_ONE_SESSION_ID = - process.env.E2E_USER_ONE_SESSION_ID || "df8a11f2-f20a-43d6-80a0-a213f1efedc1"; -const E2E_USER_ONE_ID = - process.env.E2E_USER_ONE_ID || "8e3179ce-f32b-4d0a-ba3b-234d66b836ad"; -const E2E_USER_ONE_EMAIL = - process.env.E2E_USER_ONE_EMAIL || "e2e-user-one@codu.co"; - -const E2E_USER_TWO_SESSION_ID = - process.env.E2E_USER_TWO_SESSION_ID || "10134766-bc6c-4b52-83d7-46ec0a4cb95d"; -const E2E_USER_TWO_ID = - process.env.E2E_USER_TWO_ID || "a15a104a-0e34-4101-8800-ed25c9231345"; -const E2E_USER_TWO_EMAIL = - process.env.E2E_USER_TWO_EMAIL || "e2e-user-two@codu.co"; if (!DATABASE_URL) { throw new Error("DATABASE_URL is not set"); diff --git a/e2e/constants/constants.ts b/e2e/constants/constants.ts new file mode 100644 index 00000000..c5d853ac --- /dev/null +++ b/e2e/constants/constants.ts @@ -0,0 +1,12 @@ +export const articleContent = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas vitae ipsum id metus vestibulum rutrum eget a diam. Integer eget vulputate risus, ac convallis nulla. Mauris sed augue nunc. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam congue posuere tempor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut ac augue non libero ullamcorper ornare. Ut commodo ligula vitae malesuada maximus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam sagittis justo non justo placerat, a dapibus sapien volutpat. Nullam ullamcorper sodales justo sed."; + +export const articleExcerpt = "Lorem ipsum dolor sit amet"; + +export const E2E_USER_ONE_EMAIL = "e2e@codu.co"; +export const E2E_USER_ONE_ID = "8e3179ce-f32b-4d0a-ba3b-234d66b836ad"; +export const E2E_USER_ONE_SESSION_ID = "df8a11f2-f20a-43d6-80a0-a213f1efedc1"; + +export const E2E_USER_TWO_EMAIL = "e2e-user-two@codu.co"; +export const E2E_USER_TWO_ID = "a15a104a-0e34-4101-8800-ed25c9231345"; +export const E2E_USER_TWO_SESSION_ID = "10134766-bc6c-4b52-83d7-46ec0a4cb95d"; diff --git a/e2e/constants/index.ts b/e2e/constants/index.ts new file mode 100644 index 00000000..b04bfcf7 --- /dev/null +++ b/e2e/constants/index.ts @@ -0,0 +1 @@ +export * from "./constants"; diff --git a/e2e/setup.ts b/e2e/setup.ts index 561543ef..df3a101a 100644 --- a/e2e/setup.ts +++ b/e2e/setup.ts @@ -1,19 +1,12 @@ -import dotenv from "dotenv"; import postgres from "postgres"; import { drizzle } from "drizzle-orm/postgres-js"; import { post, comment } from "@/server/db/schema"; - -dotenv.config(); // Load .env file contents into process.env +import { E2E_USER_ONE_ID, E2E_USER_TWO_ID } from "./constants"; export const setup = async () => { - if ( - !process.env.DATABASE_URL || - !process.env.E2E_USER_ONE_ID || - !process.env.E2E_USER_TWO_ID - ) { - throw new Error("Missing env variables for DB clean up script"); - } - const db = drizzle(postgres(process.env.DATABASE_URL as string)); + const db = drizzle( + postgres("postgresql://postgres:secret@127.0.0.1:5432/postgres"), + ); const addE2EArticleAndComment = async ( authorId: string, @@ -52,10 +45,7 @@ export const setup = async () => { try { console.log("creating articles"); - await addE2EArticleAndComment( - process.env.E2E_USER_ONE_ID as string, - process.env.E2E_USER_TWO_ID as string, - ); + await addE2EArticleAndComment(E2E_USER_ONE_ID, E2E_USER_TWO_ID); console.log("DB setup successful"); } catch (err) { console.log("Error while setting up DB before E2E test run", err); diff --git a/e2e/teardown.ts b/e2e/teardown.ts index d5b1a416..a9b223b8 100644 --- a/e2e/teardown.ts +++ b/e2e/teardown.ts @@ -1,31 +1,23 @@ -import dotenv from "dotenv"; import postgres from "postgres"; - -dotenv.config(); // Load .env file contents into process.env +import { E2E_USER_ONE_ID, E2E_USER_TWO_ID } from "./constants"; export const teardown = async () => { try { - if ( - !process.env.DATABASE_URL || - !process.env.E2E_USER_ONE_ID || - !process.env.E2E_USER_TWO_ID - ) - throw new Error("Missing env variables for DB clean up script"); - const db = postgres(process.env.DATABASE_URL as string); + const db = postgres("postgresql://postgres:secret@127.0.0.1:5432/postgres"); // the test suit adds posts created by the E2E users. We want to remove them between test runs await db` - DELETE FROM "Post" WHERE "userId" = ${process.env.E2E_USER_ONE_ID as string} + DELETE FROM "Post" WHERE "userId" = ${E2E_USER_ONE_ID} `; await db` - DELETE FROM "Post" WHERE "userId" = ${process.env.E2E_USER_TWO_ID as string} + DELETE FROM "Post" WHERE "userId" = ${E2E_USER_TWO_ID} `; // the test suit adds comments created by the E2E user. We want to remove them between test runs await db` - DELETE FROM "Comment" WHERE "userId" = ${process.env.E2E_USER_ONE_ID as string} + DELETE FROM "Comment" WHERE "userId" = ${E2E_USER_ONE_ID} `; await db` - DELETE FROM "Comment" WHERE "userId" = ${process.env.E2E_USER_TWO_ID as string} + DELETE FROM "Comment" WHERE "userId" = ${E2E_USER_TWO_ID} `; console.log("DB clean up successful"); diff --git a/e2e/utils/utils.ts b/e2e/utils/utils.ts index 1d918d92..408f16e6 100644 --- a/e2e/utils/utils.ts +++ b/e2e/utils/utils.ts @@ -1,13 +1,12 @@ import { expect, Page } from "@playwright/test"; +import { E2E_USER_ONE_SESSION_ID, E2E_USER_TWO_SESSION_ID } from "../constants"; export const loggedInAsUserOne = async (page: Page) => { try { - expect(process.env.E2E_USER_ONE_SESSION_ID).toBeDefined(); - await page.context().addCookies([ { name: "next-auth.session-token", - value: process.env.E2E_USER_ONE_SESSION_ID as string, + value: E2E_USER_ONE_SESSION_ID, domain: "localhost", path: "/", sameSite: "Lax", @@ -26,14 +25,12 @@ export const loggedInAsUserOne = async (page: Page) => { export const loggedInAsUserTwo = async (page: Page) => { try { - expect(process.env.E2E_USER_TWO_SESSION_ID).toBeDefined(); - await page.context().clearCookies(); await page.context().addCookies([ { name: "next-auth.session-token", - value: process.env.E2E_USER_TWO_SESSION_ID as string, + value: E2E_USER_TWO_SESSION_ID, domain: "localhost", path: "/", sameSite: "Lax", diff --git a/sample.env b/sample.env index c3602d3a..b3601bc9 100644 --- a/sample.env +++ b/sample.env @@ -4,9 +4,3 @@ GITLAB_ID= ### Replace with GitLab OAuth ID (https://gitlab.com/-/user_settings GITLAB_SECRET= ### Replace with GitLab OAuth Secret (https://gitlab.com/-/user_settings/applications) NEXTAUTH_URL=http://localhost:3000/api/auth DATABASE_URL=postgresql://postgres:secret@127.0.0.1:5432/postgres - -E2E_USER_EMAIL=e2e@codu.co -E2E_USER_ONE_ID=8e3179ce-f32b-4d0a-ba3b-234d66b836ad -E2E_USER_TWO_ID=a15a104a-0e34-4101-8800-ed25c9231345 -E2E_USER_ONE_SESSION_ID=df8a11f2-f20a-43d6-80a0-a213f1efedc1 -E2E_USER_TWO_SESSION_ID=10134766-bc6c-4b52-83d7-46ec0a4cb95d