From ed61369c2be1f6876c4ebb5c295588e4a387e83c Mon Sep 17 00:00:00 2001 From: Alejandra <90076947+alejsdev@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:45:05 -0500 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Add=20random=20password=20?= =?UTF-8?q?util=20and=20refactor=20tests=20(#1277)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/tests/login.spec.ts | 6 ++-- frontend/tests/reset-password.spec.ts | 32 +++++++++++---------- frontend/tests/sign-up.spec.ts | 40 +++++++++++++-------------- frontend/tests/user-settings.spec.ts | 28 +++++++++---------- frontend/tests/utils/random.ts | 2 ++ 5 files changed, 57 insertions(+), 51 deletions(-) diff --git a/frontend/tests/login.spec.ts b/frontend/tests/login.spec.ts index ed1a850..97c2284 100644 --- a/frontend/tests/login.spec.ts +++ b/frontend/tests/login.spec.ts @@ -1,5 +1,6 @@ import { type Page, expect, test } from "@playwright/test" import { firstSuperuser, firstSuperuserPassword } from "./config.ts" +import { randomPassword } from "./utils/random.ts" test.use({ storageState: { cookies: [], origins: [] } }) @@ -67,9 +68,10 @@ test("Log in with invalid email", async ({ page }) => { }) test("Log in with invalid password", async ({ page }) => { + const password = randomPassword() + await page.goto("/login") - // TODO: Add a random password utility - await fillForm(page, firstSuperuser, "changethat") + await fillForm(page, firstSuperuser, password) await page.getByRole("button", { name: "Log In" }).click() await expect(page.getByText("Incorrect email or password")).toBeVisible() diff --git a/frontend/tests/reset-password.spec.ts b/frontend/tests/reset-password.spec.ts index 59cbcee..88ec798 100644 --- a/frontend/tests/reset-password.spec.ts +++ b/frontend/tests/reset-password.spec.ts @@ -1,6 +1,6 @@ import { expect, test } from "@playwright/test" import { findLastEmail } from "./utils/mailcatcher" -import { randomEmail } from "./utils/random" +import { randomEmail, randomPassword } from "./utils/random" import { logInUser, signUpNewUser } from "./utils/user" test.use({ storageState: { cookies: [], origins: [] } }) @@ -31,13 +31,13 @@ test("User can reset password successfully using the link", async ({ page, request, }) => { - const full_name = "Test User" + const fullName = "Test User" const email = randomEmail() - const password = "changethis" - const new_password = "changethat" + const password = randomPassword() + const newPassword = randomPassword() // Sign up a new user - await signUpNewUser(page, full_name, email, password) + await signUpNewUser(page, fullName, email, password) await page.goto("/recover-password") await page.getByPlaceholder("Email").fill(email) @@ -62,34 +62,36 @@ test("User can reset password successfully using the link", async ({ // Set the new password and confirm it await page.goto(url) - await page.getByLabel("Set Password").fill(new_password) - await page.getByLabel("Confirm Password").fill(new_password) + await page.getByLabel("Set Password").fill(newPassword) + await page.getByLabel("Confirm Password").fill(newPassword) await page.getByRole("button", { name: "Reset Password" }).click() await expect(page.getByText("Password updated successfully")).toBeVisible() // Check if the user is able to login with the new password - await logInUser(page, email, new_password) + await logInUser(page, email, newPassword) }) test("Expired or invalid reset link", async ({ page }) => { + const password = randomPassword() const invalidUrl = "/reset-password?token=invalidtoken" await page.goto(invalidUrl) - await page.getByLabel("Set Password").fill("newpassword") - await page.getByLabel("Confirm Password").fill("newpassword") + await page.getByLabel("Set Password").fill(password) + await page.getByLabel("Confirm Password").fill(password) await page.getByRole("button", { name: "Reset Password" }).click() await expect(page.getByText("Invalid token")).toBeVisible() }) test("Weak new password validation", async ({ page, request }) => { - const full_name = "Test User" + const fullName = "Test User" const email = randomEmail() - const password = "password" + const password = randomPassword() + const weakPassword = "123" // Sign up a new user - await signUpNewUser(page, full_name, email, password) + await signUpNewUser(page, fullName, email, password) await page.goto("/recover-password") await page.getByPlaceholder("Email").fill(email) @@ -109,8 +111,8 @@ test("Weak new password validation", async ({ page, request }) => { // Set a weak new password await page.goto(url) - await page.getByLabel("Set Password").fill("123") - await page.getByLabel("Confirm Password").fill("123") + await page.getByLabel("Set Password").fill(weakPassword) + await page.getByLabel("Confirm Password").fill(weakPassword) await page.getByRole("button", { name: "Reset Password" }).click() await expect( diff --git a/frontend/tests/sign-up.spec.ts b/frontend/tests/sign-up.spec.ts index 3f08e92..a666123 100644 --- a/frontend/tests/sign-up.spec.ts +++ b/frontend/tests/sign-up.spec.ts @@ -1,6 +1,6 @@ import { type Page, expect, test } from "@playwright/test" -import { randomEmail } from "./utils/random" +import { randomEmail, randomPassword } from "./utils/random" test.use({ storageState: { cookies: [], origins: [] } }) @@ -56,7 +56,7 @@ test("Log In link is visible", async ({ page }) => { test("Sign up with valid name, email, and password", async ({ page }) => { const full_name = "Test User" const email = randomEmail() - const password = "changethis" + const password = randomPassword() await page.goto("/signup") await fillForm(page, full_name, email, password, password) @@ -79,20 +79,20 @@ test("Sign up with invalid email", async ({ page }) => { }) test("Sign up with existing email", async ({ page }) => { - const full_name = "Test User" + const fullName = "Test User" const email = randomEmail() - const password = "changethis" + const password = randomPassword() // Sign up with an email await page.goto("/signup") - await fillForm(page, full_name, email, password, password) + await fillForm(page, fullName, email, password, password) await page.getByRole("button", { name: "Sign Up" }).click() // Sign up again with the same email await page.goto("/signup") - await fillForm(page, full_name, email, password, password) + await fillForm(page, fullName, email, password, password) await page.getByRole("button", { name: "Sign Up" }).click() await page @@ -101,13 +101,13 @@ test("Sign up with existing email", async ({ page }) => { }) test("Sign up with weak password", async ({ page }) => { - const full_name = "Test User" + const fullName = "Test User" const email = randomEmail() const password = "weak" await page.goto("/signup") - await fillForm(page, full_name, email, password, password) + await fillForm(page, fullName, email, password, password) await page.getByRole("button", { name: "Sign Up" }).click() await expect( @@ -116,53 +116,53 @@ test("Sign up with weak password", async ({ page }) => { }) test("Sign up with mismatched passwords", async ({ page }) => { - const full_name = "Test User" + const fullName = "Test User" const email = randomEmail() - const password = "changethis" - const password2 = "changethat" + const password = randomPassword() + const password2 = randomPassword() await page.goto("/signup") - await fillForm(page, full_name, email, password, password2) + await fillForm(page, fullName, email, password, password2) await page.getByRole("button", { name: "Sign Up" }).click() await expect(page.getByText("Passwords do not match")).toBeVisible() }) test("Sign up with missing full name", async ({ page }) => { - const full_name = "" + const fullName = "" const email = randomEmail() - const password = "changethis" + const password = randomPassword() await page.goto("/signup") - await fillForm(page, full_name, email, password, password) + await fillForm(page, fullName, email, password, password) await page.getByRole("button", { name: "Sign Up" }).click() await expect(page.getByText("Full Name is required")).toBeVisible() }) test("Sign up with missing email", async ({ page }) => { - const full_name = "Test User" + const fullName = "Test User" const email = "" - const password = "changethis" + const password = randomPassword() await page.goto("/signup") - await fillForm(page, full_name, email, password, password) + await fillForm(page, fullName, email, password, password) await page.getByRole("button", { name: "Sign Up" }).click() await expect(page.getByText("Email is required")).toBeVisible() }) test("Sign up with missing password", async ({ page }) => { - const full_name = "" + const fullName = "" const email = randomEmail() const password = "" await page.goto("/signup") - await fillForm(page, full_name, email, password, password) + await fillForm(page, fullName, email, password, password) await page.getByRole("button", { name: "Sign Up" }).click() await expect(page.getByText("Password is required")).toBeVisible() diff --git a/frontend/tests/user-settings.spec.ts b/frontend/tests/user-settings.spec.ts index b24f1ec..a3a8a27 100644 --- a/frontend/tests/user-settings.spec.ts +++ b/frontend/tests/user-settings.spec.ts @@ -1,7 +1,7 @@ import { expect, test } from "@playwright/test" -import { randomEmail } from "./utils/random" -import { logInUser, logOutUser, signUpNewUser } from "./utils/user" import { firstSuperuser, firstSuperuserPassword } from "./config.ts" +import { randomEmail, randomPassword } from "./utils/random" +import { logInUser, logOutUser, signUpNewUser } from "./utils/user" const tabs = ["My profile", "Password", "Appearance"] @@ -29,7 +29,7 @@ test.describe("Edit user full name and email successfully", () => { const fullName = "Test User" const email = randomEmail() const updatedName = "Test User 2" - const password = "password" + const password = randomPassword() // Sign up a new user await signUpNewUser(page, fullName, email, password) @@ -53,7 +53,7 @@ test.describe("Edit user full name and email successfully", () => { const fullName = "Test User" const email = randomEmail() const updatedEmail = randomEmail() - const password = "password" + const password = randomPassword() // Sign up a new user await signUpNewUser(page, fullName, email, password) @@ -79,7 +79,7 @@ test.describe("Edit user with invalid data", () => { test("Edit user email with an invalid email", async ({ page }) => { const fullName = "Test User" const email = randomEmail() - const password = "password" + const password = randomPassword() const invalidEmail = "" // Sign up a new user @@ -99,7 +99,7 @@ test.describe("Edit user with invalid data", () => { test("Cancel edit action restores original name", async ({ page }) => { const fullName = "Test User" const email = randomEmail() - const password = "password" + const password = randomPassword() const updatedName = "Test User" // Sign up a new user @@ -121,7 +121,7 @@ test.describe("Edit user with invalid data", () => { test("Cancel edit action restores original email", async ({ page }) => { const fullName = "Test User" const email = randomEmail() - const password = "password" + const password = randomPassword() const updatedEmail = randomEmail() // Sign up a new user @@ -149,8 +149,8 @@ test.describe("Change password successfully", () => { test("Update password successfully", async ({ page }) => { const fullName = "Test User" const email = randomEmail() - const password = "password" - const NewPassword = "newPassword" + const password = randomPassword() + const NewPassword = randomPassword() // Sign up a new user await signUpNewUser(page, fullName, email, password) @@ -179,7 +179,7 @@ test.describe("Change password with invalid data", () => { test("Update password with weak passwords", async ({ page }) => { const fullName = "Test User" const email = randomEmail() - const password = "password" + const password = randomPassword() const weakPassword = "weak" // Sign up a new user @@ -203,9 +203,9 @@ test.describe("Change password with invalid data", () => { }) => { const fullName = "Test User" const email = randomEmail() - const password = "password" - const newPassword = "newPassword" - const confirmPassword = "confirmPassword" + const password = randomPassword() + const newPassword = randomPassword() + const confirmPassword = randomPassword() // Sign up a new user await signUpNewUser(page, fullName, email, password) @@ -225,7 +225,7 @@ test.describe("Change password with invalid data", () => { test("Current password and new password are the same", async ({ page }) => { const fullName = "Test User" const email = randomEmail() - const password = "password" + const password = randomPassword() // Sign up a new user await signUpNewUser(page, fullName, email, password) diff --git a/frontend/tests/utils/random.ts b/frontend/tests/utils/random.ts index bd2b43b..d96f083 100644 --- a/frontend/tests/utils/random.ts +++ b/frontend/tests/utils/random.ts @@ -4,6 +4,8 @@ export const randomEmail = () => export const randomTeamName = () => `Team ${Math.random().toString(36).substring(7)}` +export const randomPassword = () => `${Math.random().toString(36).substring(2)}` + export const slugify = (text: string) => text .toLowerCase()