Skip to content

Commit

Permalink
♻️ Add random password util and refactor tests (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
alejsdev authored Aug 1, 2024
1 parent 17e4fb9 commit ed61369
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 51 deletions.
6 changes: 4 additions & 2 deletions frontend/tests/login.spec.ts
Original file line number Diff line number Diff line change
@@ -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: [] } })

Expand Down Expand Up @@ -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()
Expand Down
32 changes: 17 additions & 15 deletions frontend/tests/reset-password.spec.ts
Original file line number Diff line number Diff line change
@@ -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: [] } })
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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(
Expand Down
40 changes: 20 additions & 20 deletions frontend/tests/sign-up.spec.ts
Original file line number Diff line number Diff line change
@@ -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: [] } })

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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()
Expand Down
28 changes: 14 additions & 14 deletions frontend/tests/user-settings.spec.ts
Original file line number Diff line number Diff line change
@@ -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"]

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions frontend/tests/utils/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ed61369

Please sign in to comment.