♻️ Add random password util and refactor tests (#1277)

This commit is contained in:
Alejandra
2024-08-01 11:45:05 -05:00
committed by GitHub
parent 17e4fb9141
commit ed61369c2b
5 changed files with 57 additions and 51 deletions

View File

@@ -1,5 +1,6 @@
import { type Page, expect, test } from "@playwright/test" import { type Page, expect, test } from "@playwright/test"
import { firstSuperuser, firstSuperuserPassword } from "./config.ts" import { firstSuperuser, firstSuperuserPassword } from "./config.ts"
import { randomPassword } from "./utils/random.ts"
test.use({ storageState: { cookies: [], origins: [] } }) 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 }) => { test("Log in with invalid password", async ({ page }) => {
const password = randomPassword()
await page.goto("/login") await page.goto("/login")
// TODO: Add a random password utility await fillForm(page, firstSuperuser, password)
await fillForm(page, firstSuperuser, "changethat")
await page.getByRole("button", { name: "Log In" }).click() await page.getByRole("button", { name: "Log In" }).click()
await expect(page.getByText("Incorrect email or password")).toBeVisible() await expect(page.getByText("Incorrect email or password")).toBeVisible()

View File

@@ -1,6 +1,6 @@
import { expect, test } from "@playwright/test" import { expect, test } from "@playwright/test"
import { findLastEmail } from "./utils/mailcatcher" import { findLastEmail } from "./utils/mailcatcher"
import { randomEmail } from "./utils/random" import { randomEmail, randomPassword } from "./utils/random"
import { logInUser, signUpNewUser } from "./utils/user" import { logInUser, signUpNewUser } from "./utils/user"
test.use({ storageState: { cookies: [], origins: [] } }) test.use({ storageState: { cookies: [], origins: [] } })
@@ -31,13 +31,13 @@ test("User can reset password successfully using the link", async ({
page, page,
request, request,
}) => { }) => {
const full_name = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "changethis" const password = randomPassword()
const new_password = "changethat" const newPassword = randomPassword()
// Sign up a new user // 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.goto("/recover-password")
await page.getByPlaceholder("Email").fill(email) 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 // Set the new password and confirm it
await page.goto(url) await page.goto(url)
await page.getByLabel("Set Password").fill(new_password) await page.getByLabel("Set Password").fill(newPassword)
await page.getByLabel("Confirm Password").fill(new_password) await page.getByLabel("Confirm Password").fill(newPassword)
await page.getByRole("button", { name: "Reset Password" }).click() await page.getByRole("button", { name: "Reset Password" }).click()
await expect(page.getByText("Password updated successfully")).toBeVisible() await expect(page.getByText("Password updated successfully")).toBeVisible()
// Check if the user is able to login with the new password // 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 }) => { test("Expired or invalid reset link", async ({ page }) => {
const password = randomPassword()
const invalidUrl = "/reset-password?token=invalidtoken" const invalidUrl = "/reset-password?token=invalidtoken"
await page.goto(invalidUrl) await page.goto(invalidUrl)
await page.getByLabel("Set Password").fill("newpassword") await page.getByLabel("Set Password").fill(password)
await page.getByLabel("Confirm Password").fill("newpassword") await page.getByLabel("Confirm Password").fill(password)
await page.getByRole("button", { name: "Reset Password" }).click() await page.getByRole("button", { name: "Reset Password" }).click()
await expect(page.getByText("Invalid token")).toBeVisible() await expect(page.getByText("Invalid token")).toBeVisible()
}) })
test("Weak new password validation", async ({ page, request }) => { test("Weak new password validation", async ({ page, request }) => {
const full_name = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "password" const password = randomPassword()
const weakPassword = "123"
// Sign up a new user // 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.goto("/recover-password")
await page.getByPlaceholder("Email").fill(email) await page.getByPlaceholder("Email").fill(email)
@@ -109,8 +111,8 @@ test("Weak new password validation", async ({ page, request }) => {
// Set a weak new password // Set a weak new password
await page.goto(url) await page.goto(url)
await page.getByLabel("Set Password").fill("123") await page.getByLabel("Set Password").fill(weakPassword)
await page.getByLabel("Confirm Password").fill("123") await page.getByLabel("Confirm Password").fill(weakPassword)
await page.getByRole("button", { name: "Reset Password" }).click() await page.getByRole("button", { name: "Reset Password" }).click()
await expect( await expect(

View File

@@ -1,6 +1,6 @@
import { type Page, expect, test } from "@playwright/test" import { type Page, expect, test } from "@playwright/test"
import { randomEmail } from "./utils/random" import { randomEmail, randomPassword } from "./utils/random"
test.use({ storageState: { cookies: [], origins: [] } }) 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 }) => { test("Sign up with valid name, email, and password", async ({ page }) => {
const full_name = "Test User" const full_name = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "changethis" const password = randomPassword()
await page.goto("/signup") await page.goto("/signup")
await fillForm(page, full_name, email, password, password) 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 }) => { test("Sign up with existing email", async ({ page }) => {
const full_name = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "changethis" const password = randomPassword()
// Sign up with an email // Sign up with an email
await page.goto("/signup") 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.getByRole("button", { name: "Sign Up" }).click()
// Sign up again with the same email // Sign up again with the same email
await page.goto("/signup") 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.getByRole("button", { name: "Sign Up" }).click()
await page await page
@@ -101,13 +101,13 @@ test("Sign up with existing email", async ({ page }) => {
}) })
test("Sign up with weak password", async ({ page }) => { test("Sign up with weak password", async ({ page }) => {
const full_name = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "weak" const password = "weak"
await page.goto("/signup") 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.getByRole("button", { name: "Sign Up" }).click()
await expect( await expect(
@@ -116,53 +116,53 @@ test("Sign up with weak password", async ({ page }) => {
}) })
test("Sign up with mismatched passwords", async ({ page }) => { test("Sign up with mismatched passwords", async ({ page }) => {
const full_name = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "changethis" const password = randomPassword()
const password2 = "changethat" const password2 = randomPassword()
await page.goto("/signup") 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 page.getByRole("button", { name: "Sign Up" }).click()
await expect(page.getByText("Passwords do not match")).toBeVisible() await expect(page.getByText("Passwords do not match")).toBeVisible()
}) })
test("Sign up with missing full name", async ({ page }) => { test("Sign up with missing full name", async ({ page }) => {
const full_name = "" const fullName = ""
const email = randomEmail() const email = randomEmail()
const password = "changethis" const password = randomPassword()
await page.goto("/signup") 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.getByRole("button", { name: "Sign Up" }).click()
await expect(page.getByText("Full Name is required")).toBeVisible() await expect(page.getByText("Full Name is required")).toBeVisible()
}) })
test("Sign up with missing email", async ({ page }) => { test("Sign up with missing email", async ({ page }) => {
const full_name = "Test User" const fullName = "Test User"
const email = "" const email = ""
const password = "changethis" const password = randomPassword()
await page.goto("/signup") 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.getByRole("button", { name: "Sign Up" }).click()
await expect(page.getByText("Email is required")).toBeVisible() await expect(page.getByText("Email is required")).toBeVisible()
}) })
test("Sign up with missing password", async ({ page }) => { test("Sign up with missing password", async ({ page }) => {
const full_name = "" const fullName = ""
const email = randomEmail() const email = randomEmail()
const password = "" const password = ""
await page.goto("/signup") 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.getByRole("button", { name: "Sign Up" }).click()
await expect(page.getByText("Password is required")).toBeVisible() await expect(page.getByText("Password is required")).toBeVisible()

View File

@@ -1,7 +1,7 @@
import { expect, test } from "@playwright/test" 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 { firstSuperuser, firstSuperuserPassword } from "./config.ts"
import { randomEmail, randomPassword } from "./utils/random"
import { logInUser, logOutUser, signUpNewUser } from "./utils/user"
const tabs = ["My profile", "Password", "Appearance"] const tabs = ["My profile", "Password", "Appearance"]
@@ -29,7 +29,7 @@ test.describe("Edit user full name and email successfully", () => {
const fullName = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const updatedName = "Test User 2" const updatedName = "Test User 2"
const password = "password" const password = randomPassword()
// Sign up a new user // Sign up a new user
await signUpNewUser(page, fullName, email, password) await signUpNewUser(page, fullName, email, password)
@@ -53,7 +53,7 @@ test.describe("Edit user full name and email successfully", () => {
const fullName = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const updatedEmail = randomEmail() const updatedEmail = randomEmail()
const password = "password" const password = randomPassword()
// Sign up a new user // Sign up a new user
await signUpNewUser(page, fullName, email, password) 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 }) => { test("Edit user email with an invalid email", async ({ page }) => {
const fullName = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "password" const password = randomPassword()
const invalidEmail = "" const invalidEmail = ""
// Sign up a new user // 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 }) => { test("Cancel edit action restores original name", async ({ page }) => {
const fullName = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "password" const password = randomPassword()
const updatedName = "Test User" const updatedName = "Test User"
// Sign up a new 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 }) => { test("Cancel edit action restores original email", async ({ page }) => {
const fullName = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "password" const password = randomPassword()
const updatedEmail = randomEmail() const updatedEmail = randomEmail()
// Sign up a new user // Sign up a new user
@@ -149,8 +149,8 @@ test.describe("Change password successfully", () => {
test("Update password successfully", async ({ page }) => { test("Update password successfully", async ({ page }) => {
const fullName = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "password" const password = randomPassword()
const NewPassword = "newPassword" const NewPassword = randomPassword()
// Sign up a new user // Sign up a new user
await signUpNewUser(page, fullName, email, password) 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 }) => { test("Update password with weak passwords", async ({ page }) => {
const fullName = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "password" const password = randomPassword()
const weakPassword = "weak" const weakPassword = "weak"
// Sign up a new user // Sign up a new user
@@ -203,9 +203,9 @@ test.describe("Change password with invalid data", () => {
}) => { }) => {
const fullName = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "password" const password = randomPassword()
const newPassword = "newPassword" const newPassword = randomPassword()
const confirmPassword = "confirmPassword" const confirmPassword = randomPassword()
// Sign up a new user // Sign up a new user
await signUpNewUser(page, fullName, email, password) 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 }) => { test("Current password and new password are the same", async ({ page }) => {
const fullName = "Test User" const fullName = "Test User"
const email = randomEmail() const email = randomEmail()
const password = "password" const password = randomPassword()
// Sign up a new user // Sign up a new user
await signUpNewUser(page, fullName, email, password) await signUpNewUser(page, fullName, email, password)

View File

@@ -4,6 +4,8 @@ export const randomEmail = () =>
export const randomTeamName = () => export const randomTeamName = () =>
`Team ${Math.random().toString(36).substring(7)}` `Team ${Math.random().toString(36).substring(7)}`
export const randomPassword = () => `${Math.random().toString(36).substring(2)}`
export const slugify = (text: string) => export const slugify = (text: string) =>
text text
.toLowerCase() .toLowerCase()