-
-
Notifications
You must be signed in to change notification settings - Fork 145
chore: moving out user auth out into a util for E2E tests #1181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
02aabfe
984b47d
ec6021d
d11dd23
b78ae71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import test from "@playwright/test"; | ||
import { loggedInAsUserOne } from "./utils"; | ||
|
||
test.describe("Unauthenticated my-posts Page", () => { | ||
test.beforeEach(async ({ page }) => { | ||
await page.context().clearCookies(); | ||
}); | ||
// | ||
// Replace with tests for unauthenticated users | ||
}); | ||
|
||
test.describe("Authenticated my-posts Page", () => { | ||
test.beforeEach(async ({ page }) => { | ||
await loggedInAsUserOne(page); | ||
}); | ||
// | ||
// Replace with tests for authenticated users | ||
}); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,14 +1,15 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import test from "@playwright/test"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { loggedInAsUserOne } from "./utils"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
test.describe("Unauthenticated setttings Page", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
test.beforeEach(async ({ page }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await page.context().clearCookies(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Replace with tests for unauthenticated users | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
test.describe("Authenticated settings Page", () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
test.beforeEach(async ({ page }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await loggedInAsUserOne(page); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Replace with tests for authenticated users | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
1
to
15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider adding test organization comments To improve maintainability and clarity, consider adding JSDoc comments for each test suite to document the test organization and coverage goals. import test from "@playwright/test";
import { loggedInAsUserOne } from "./utils";
+/**
+ * Test suite for unauthenticated user interactions with the settings page.
+ * Verifies proper handling of unauthorized access and redirects.
+ */
test.describe("Unauthenticated settings Page", () => {
// TODO: Implement tests for unauthenticated scenarios
});
+/**
+ * Test suite for authenticated user interactions with the settings page.
+ * Verifies settings management functionality for logged-in users.
+ */
test.describe("Authenticated settings Page", () => {
test.beforeEach(async ({ page }) => {
await loggedInAsUserOne(page);
});
// TODO: Implement tests for authenticated scenarios
}); 📝 Committable suggestion
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./utils"; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,27 @@ | ||||||||||||||||||||||||||||||||||||||||||
import { expect, Page } from "@playwright/test"; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
export const loggedInAsUserOne = async (page: Page) => { | ||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||
expect(process.env.E2E_USER_ONE_SESSION_ID).toBeDefined(); | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add type safety for environment variable. Consider adding runtime type checking for the environment variable value, not just its existence. - expect(process.env.E2E_USER_ONE_SESSION_ID).toBeDefined();
+ const sessionId = process.env.E2E_USER_ONE_SESSION_ID;
+ expect(sessionId).toBeDefined();
+ expect(typeof sessionId === 'string' && sessionId.length > 0).toBeTruthy(); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
await page.context().addCookies([ | ||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||
name: "next-auth.session-token", | ||||||||||||||||||||||||||||||||||||||||||
value: process.env.E2E_USER_ONE_SESSION_ID as string, | ||||||||||||||||||||||||||||||||||||||||||
domain: "localhost", | ||||||||||||||||||||||||||||||||||||||||||
path: "/", | ||||||||||||||||||||||||||||||||||||||||||
sameSite: "Lax", | ||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||
]); | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+7
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhance cookie security configuration. The cookie configuration is missing important security flags:
Apply this diff to improve security: await page.context().addCookies([
{
name: "next-auth.session-token",
value: process.env.E2E_USER_ONE_SESSION_ID as string,
domain: "localhost",
path: "/",
sameSite: "Lax",
+ httpOnly: true,
+ secure: process.env.NODE_ENV === "production"
},
]); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
expect( | ||||||||||||||||||||||||||||||||||||||||||
(await page.context().cookies()).find( | ||||||||||||||||||||||||||||||||||||||||||
(cookie) => cookie.name === "next-auth.session-token", | ||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||
).toBeTruthy(); | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+17
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Strengthen cookie verification. The current verification only checks for cookie existence. Consider validating the cookie value matches what was set. expect(
- (await page.context().cookies()).find(
- (cookie) => cookie.name === "next-auth.session-token",
- ),
- ).toBeTruthy();
+ (await page.context().cookies()).find(
+ (cookie) => cookie.name === "next-auth.session-token" &&
+ cookie.value === sessionId
+ ),
+ ).toBeTruthy("Session cookie was not set correctly");
|
||||||||||||||||||||||||||||||||||||||||||
} catch (err) { | ||||||||||||||||||||||||||||||||||||||||||
// console.log(err); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
throw Error("Error while authenticating E2E test user one"); | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve error handling and logging. The error handling could be enhanced:
- // console.log(err);
-
- throw Error("Error while authenticating E2E test user one");
+ throw new Error(
+ `Error while authenticating E2E test user one: ${err instanceof Error ? err.message : String(err)}`
+ ); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
}; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typo in test description
There's a typo in "setttings" (three t's) in the test suite description.
📝 Committable suggestion