Skip to content

Commit 3531696

Browse files
Feat/multiple user support in e2e tests (#1182)
1 parent beae6ff commit 3531696

File tree

7 files changed

+102
-27
lines changed

7 files changed

+102
-27
lines changed

.github/workflows/e2e-tests.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ jobs:
4444
GITHUB_ID: ${{ secrets.E2E_GITHUB_ID }}
4545
GITHUB_SECRET: ${{ secrets.E2E_GITHUB_SECRET }}
4646
NEXTAUTH_SECRET: ${{ secrets.NEXTAUTH_SECRET }}
47-
E2E_USER_EMAIL: [email protected]
48-
E2E_USER_ID: 8e3179ce-f32b-4d0a-ba3b-234d66b836ad
47+
E2E_USER_ONE_EMAIL: e2e-user-one@codu.co
48+
E2E_USER_ONE_ID: 8e3179ce-f32b-4d0a-ba3b-234d66b836ad
4949
E2E_USER_ONE_SESSION_ID: df8a11f2-f20a-43d6-80a0-a213f1efedc1
50+
E2E_USER_TWO_EMAIL: [email protected]
51+
E2E_USER_TWO_ID: a15a104a-0e34-4101-8800-ed25c9231345
52+
E2E_USER_TWO_SESSION_ID: 10134766-bc6c-4b52-83d7-46ec0a4cb95d
5053

5154
steps:
5255
- name: Checkout repository

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,29 @@ NEXTAUTH_URL=http://localhost:3000/api/auth
144144
### E2E_USER_ONE_SESSION_ID
145145

146146
This is the sessionToken uuid that is used to identify a users current active session.
147-
This is currently hardcoded and there is no reason to change this until we require multiple E2E test users within the same test suite
147+
This is currently hardcoded and their is no reason to change this value.
148+
**Note: This value must be different to E2E_USER_TWO_SESSION_ID**
148149

149-
### E2E_USER_ID
150150

151-
This is the userId if the E2E user used for testing .
152-
This is currently hardcoded and there is no reason to change this until we require multiple E2E test users within the same test suite
151+
### E2E_USER_TWO_SESSION_ID
152+
153+
This is the sessionToken uuid that is used to identify a users current active session.
154+
This is currently hardcoded and their is no reason to change this value.
155+
**Note: This value must be different to E2E_USER_ONE_SESSION_ID**
156+
157+
158+
### E2E_USER_ONE_ID
159+
160+
This is the userId of one of our E2E users and is used for testing.
161+
This is currently hardcoded and there is no reason to change this value.
162+
**Note: This value must be different from E2E_USER_TWO_ID**
163+
164+
165+
### E2E_USER_TWO_ID
166+
167+
This is the userId of one of our E2E users and is used for testing.
168+
This is currently hardcoded and there is no reason to change this value.
169+
**Note: This value must be different from E2E_USER_ONE_ID**
153170

154171
For more information, you can read the documentation [here](https://next-auth.js.org/configuration/options).
155172
**Example .env file can be found [here](./sample.env). You can rename this to .env to get started**

drizzle/seed.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ import postgres from "postgres";
1010

1111
const DATABASE_URL = process.env.DATABASE_URL || "";
1212
// These can be removed in a follow on PR. Until this hits main we cant add E2E_USER_* stuff to the env.
13-
const E2E_SESSION_ID =
13+
const E2E_USER_ONE_SESSION_ID =
1414
process.env.E2E_USER_ONE_SESSION_ID || "df8a11f2-f20a-43d6-80a0-a213f1efedc1";
15-
const E2E_USER_ID =
16-
process.env.E2E_USER_ID || "8e3179ce-f32b-4d0a-ba3b-234d66b836ad";
17-
const E2E_USER_EMAIL = process.env.E2E_USER_EMAIL || "[email protected]";
15+
const E2E_USER_ONE_ID =
16+
process.env.E2E_USER_ONE_ID || "8e3179ce-f32b-4d0a-ba3b-234d66b836ad";
17+
const E2E_USER_ONE_EMAIL =
18+
process.env.E2E_USER_ONE_EMAIL || "[email protected]";
19+
20+
const E2E_USER_TWO_SESSION_ID =
21+
process.env.E2E_USER_TWO_SESSION_ID || "10134766-bc6c-4b52-83d7-46ec0a4cb95d";
22+
const E2E_USER_TWO_ID =
23+
process.env.E2E_USER_TWO_ID || "a15a104a-0e34-4101-8800-ed25c9231345";
24+
const E2E_USER_TWO_EMAIL =
25+
process.env.E2E_USER_TWO_EMAIL || "[email protected]";
1826

1927
if (!DATABASE_URL) {
2028
throw new Error("DATABASE_URL is not set");
@@ -116,27 +124,25 @@ ${chance.paragraph()}
116124
return users;
117125
};
118126

119-
const seedE2EUser = async () => {
120-
const name = "E2E Test User";
121-
127+
const seedE2EUser = async (email: string, id: string, name: string) => {
122128
const [existingE2EUser] = await db
123129
.selectDistinct()
124130
.from(user)
125-
.where(eq(user.id, E2E_USER_ID));
131+
.where(eq(user.id, id));
126132

127133
if (existingE2EUser) {
128134
console.log("E2E Test user already exists. Skipping creation");
129135
return existingE2EUser;
130136
}
131137

132138
const userData = {
133-
id: E2E_USER_ID,
139+
id: id,
134140
username: `${name.split(" ").join("-").toLowerCase()}-${chance.integer({
135141
min: 0,
136142
max: 999,
137143
})}`,
138144
name,
139-
email: E2E_USER_EMAIL,
145+
email,
140146
image: `https://robohash.org/${encodeURIComponent(name)}?bgset=bg1`,
141147
location: chance.country({ full: true }),
142148
bio: chance.sentence({ words: 10 }),
@@ -146,11 +152,11 @@ ${chance.paragraph()}
146152
return createdUser;
147153
};
148154

149-
const seedE2EUserSession = async (userId: string) => {
155+
const seedE2EUserSession = async (userId: string, sessionToken: string) => {
150156
const [existingE2EUserSession] = await db
151157
.selectDistinct()
152158
.from(session)
153-
.where(eq(session.sessionToken, E2E_SESSION_ID));
159+
.where(eq(session.sessionToken, sessionToken));
154160

155161
if (existingE2EUserSession) {
156162
console.log("E2E Test session already exists. Skipping creation");
@@ -164,7 +170,7 @@ ${chance.paragraph()}
164170
.insert(session)
165171
.values({
166172
userId,
167-
sessionToken: E2E_SESSION_ID,
173+
sessionToken,
168174
// Set session to expire in 6 months.
169175
expires: new Date(currentDate.setMonth(currentDate.getMonth() + 6)),
170176
})
@@ -249,8 +255,19 @@ ${chance.paragraph()}
249255

250256
try {
251257
await addUserData();
252-
const user = await seedE2EUser();
253-
await seedE2EUserSession(user.id);
258+
const userOne = await seedE2EUser(
259+
E2E_USER_ONE_EMAIL,
260+
E2E_USER_ONE_ID,
261+
"E2E Test User One",
262+
);
263+
const userTwo = await seedE2EUser(
264+
E2E_USER_TWO_EMAIL,
265+
E2E_USER_TWO_ID,
266+
"E2E Test User Two",
267+
);
268+
269+
await seedE2EUserSession(userOne.id, E2E_USER_ONE_SESSION_ID);
270+
await seedE2EUserSession(userTwo.id, E2E_USER_TWO_SESSION_ID);
254271
} catch (error) {
255272
console.log("Error:", error);
256273
}

e2e/articles.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ test.describe("Authenticated Articles Page", () => {
251251
page.getByRole("heading", { name: "Lorem Ipsum" }),
252252
).toBeVisible();
253253
await expect(
254-
page.getByRole("heading", { name: "Written by E2E Test User" }),
254+
page.getByRole("heading", { name: "Written by E2E Test User One" }),
255255
).toBeVisible();
256256
await expect(
257257
page.getByRole("heading", { name: "Discussion (0)" }),

e2e/teardown.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@ dotenv.config(); // Load .env file contents into process.env
55

66
export const teardown = async () => {
77
try {
8-
if (!process.env.DATABASE_URL || !process.env.E2E_USER_ID)
8+
if (
9+
!process.env.DATABASE_URL ||
10+
!process.env.E2E_USER_ONE_ID ||
11+
!process.env.E2E_USER_TWO_ID
12+
)
913
throw new Error("Missing env variables for DB clean up script");
1014
const db = postgres(process.env.DATABASE_URL as string);
1115

12-
// the test suit adds posts created by the E2E user. We want to remove them between test runs
16+
// the test suit adds posts created by the E2E users. We want to remove them between test runs
1317
await db`
14-
DELETE FROM "Post" WHERE "userId" = ${process.env.E2E_USER_ID as string}
18+
DELETE FROM "Post" WHERE "userId" = ${process.env.E2E_USER_ONE_ID as string}
19+
`;
20+
await db`
21+
DELETE FROM "Post" WHERE "userId" = ${process.env.E2E_USER_TWO_ID as string}
1522
`;
1623
// the test suit adds comments created by the E2E user. We want to remove them between test runs
1724
await db`
18-
DELETE FROM "Comment" WHERE "userId" = ${process.env.E2E_USER_ID as string}
25+
DELETE FROM "Comment" WHERE "userId" = ${process.env.E2E_USER_ONE_ID as string}
26+
`;
27+
await db`
28+
DELETE FROM "Comment" WHERE "userId" = ${process.env.E2E_USER_TWO_ID as string}
1929
`;
2030

2131
console.log("DB clean up successful");

e2e/utils/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,29 @@ export const loggedInAsUserOne = async (page: Page) => {
2323
throw Error("Error while authenticating E2E test user one");
2424
}
2525
};
26+
27+
export const loggedInAsUserTwo = async (page: Page) => {
28+
try {
29+
expect(process.env.E2E_USER_TWO_SESSION_ID).toBeDefined();
30+
31+
await page.context().clearCookies();
32+
33+
await page.context().addCookies([
34+
{
35+
name: "next-auth.session-token",
36+
value: process.env.E2E_USER_TWO_SESSION_ID as string,
37+
domain: "localhost",
38+
path: "/",
39+
sameSite: "Lax",
40+
},
41+
]);
42+
43+
expect(
44+
(await page.context().cookies()).find(
45+
(cookie) => cookie.name === "next-auth.session-token",
46+
),
47+
).toBeTruthy();
48+
} catch (err) {
49+
throw Error("Error while authenticating E2E test user two");
50+
}
51+
};

sample.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ NEXTAUTH_URL=http://localhost:3000/api/auth
66
DATABASE_URL=postgresql://postgres:[email protected]:5432/postgres
77

88
E2E_USER_EMAIL=[email protected]
9-
E2E_USER_ID=8e3179ce-f32b-4d0a-ba3b-234d66b836ad
9+
E2E_USER_ONE_ID=8e3179ce-f32b-4d0a-ba3b-234d66b836ad
10+
E2E_USER_TWO_ID=a15a104a-0e34-4101-8800-ed25c9231345
1011
E2E_USER_ONE_SESSION_ID=df8a11f2-f20a-43d6-80a0-a213f1efedc1
12+
E2E_USER_TWO_SESSION_ID=10134766-bc6c-4b52-83d7-46ec0a4cb95d

0 commit comments

Comments
 (0)