first commit

This commit is contained in:
2024-09-17 12:11:39 +08:00
commit 42fbcc5d89
236 changed files with 27582 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
import { test as setup } from "@playwright/test"
import { firstSuperuser, firstSuperuserPassword } from "./config.ts"
const authFile = "playwright/.auth/user.json"
setup("authenticate", async ({ page }) => {
await page.goto("/login")
await page.getByPlaceholder("Email").fill(firstSuperuser)
await page.getByPlaceholder("Password").fill(firstSuperuserPassword)
await page.getByRole("button", { name: "Log In" }).click()
await page.waitForURL("/")
await page.context().storageState({ path: authFile })
})

21
frontend/tests/config.ts Normal file
View File

@@ -0,0 +1,21 @@
import path from "node:path"
import { fileURLToPath } from "node:url"
import dotenv from "dotenv"
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
dotenv.config({ path: path.join(__dirname, "../../.env") })
const { FIRST_SUPERUSER, FIRST_SUPERUSER_PASSWORD } = process.env
if (typeof FIRST_SUPERUSER !== "string") {
throw new Error("Environment variable FIRST_SUPERUSER is undefined")
}
if (typeof FIRST_SUPERUSER_PASSWORD !== "string") {
throw new Error("Environment variable FIRST_SUPERUSER_PASSWORD is undefined")
}
export const firstSuperuser = FIRST_SUPERUSER as string
export const firstSuperuserPassword = FIRST_SUPERUSER_PASSWORD as string

View File

@@ -0,0 +1,117 @@
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: [] } })
type OptionsType = {
exact?: boolean
}
const fillForm = async (page: Page, email: string, password: string) => {
await page.getByPlaceholder("Email").fill(email)
await page.getByPlaceholder("Password", { exact: true }).fill(password)
}
const verifyInput = async (
page: Page,
placeholder: string,
options?: OptionsType,
) => {
const input = page.getByPlaceholder(placeholder, options)
await expect(input).toBeVisible()
await expect(input).toHaveText("")
await expect(input).toBeEditable()
}
test("Inputs are visible, empty and editable", async ({ page }) => {
await page.goto("/login")
await verifyInput(page, "Email")
await verifyInput(page, "Password", { exact: true })
})
test("Log In button is visible", async ({ page }) => {
await page.goto("/login")
await expect(page.getByRole("button", { name: "Log In" })).toBeVisible()
})
test("Forgot Password link is visible", async ({ page }) => {
await page.goto("/login")
await expect(
page.getByRole("link", { name: "Forgot password?" }),
).toBeVisible()
})
test("Log in with valid email and password ", async ({ page }) => {
await page.goto("/login")
await fillForm(page, firstSuperuser, firstSuperuserPassword)
await page.getByRole("button", { name: "Log In" }).click()
await page.waitForURL("/")
await expect(
page.getByText("Welcome back, nice to see you again!"),
).toBeVisible()
})
test("Log in with invalid email", async ({ page }) => {
await page.goto("/login")
await fillForm(page, "invalidemail", firstSuperuserPassword)
await page.getByRole("button", { name: "Log In" }).click()
await expect(page.getByText("Invalid email address")).toBeVisible()
})
test("Log in with invalid password", async ({ page }) => {
const password = randomPassword()
await page.goto("/login")
await fillForm(page, firstSuperuser, password)
await page.getByRole("button", { name: "Log In" }).click()
await expect(page.getByText("Incorrect email or password")).toBeVisible()
})
// Log out
test("Successful log out", async ({ page }) => {
await page.goto("/login")
await fillForm(page, firstSuperuser, firstSuperuserPassword)
await page.getByRole("button", { name: "Log In" }).click()
await page.waitForURL("/")
await expect(
page.getByText("Welcome back, nice to see you again!"),
).toBeVisible()
await page.getByTestId("user-menu").click()
await page.getByRole("menuitem", { name: "Log out" }).click()
await page.waitForURL("/login")
})
test("Logged-out user cannot access protected routes", async ({ page }) => {
await page.goto("/login")
await fillForm(page, firstSuperuser, firstSuperuserPassword)
await page.getByRole("button", { name: "Log In" }).click()
await page.waitForURL("/")
await expect(
page.getByText("Welcome back, nice to see you again!"),
).toBeVisible()
await page.getByTestId("user-menu").click()
await page.getByRole("menuitem", { name: "Log out" }).click()
await page.waitForURL("/login")
await page.goto("/settings")
await page.waitForURL("/login")
})

View File

@@ -0,0 +1,121 @@
import { expect, test } from "@playwright/test"
import { findLastEmail } from "./utils/mailcatcher"
import { randomEmail, randomPassword } from "./utils/random"
import { logInUser, signUpNewUser } from "./utils/user"
test.use({ storageState: { cookies: [], origins: [] } })
test("Password Recovery title is visible", async ({ page }) => {
await page.goto("/recover-password")
await expect(
page.getByRole("heading", { name: "Password Recovery" }),
).toBeVisible()
})
test("Input is visible, empty and editable", async ({ page }) => {
await page.goto("/recover-password")
await expect(page.getByPlaceholder("Email")).toBeVisible()
await expect(page.getByPlaceholder("Email")).toHaveText("")
await expect(page.getByPlaceholder("Email")).toBeEditable()
})
test("Continue button is visible", async ({ page }) => {
await page.goto("/recover-password")
await expect(page.getByRole("button", { name: "Continue" })).toBeVisible()
})
test("User can reset password successfully using the link", async ({
page,
request,
}) => {
const fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
const newPassword = randomPassword()
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
await page.goto("/recover-password")
await page.getByPlaceholder("Email").fill(email)
await page.getByRole("button", { name: "Continue" }).click()
const emailData = await findLastEmail({
request,
filter: (e) => e.recipients.includes(`<${email}>`),
timeout: 5000,
})
await page.goto(`http://localhost:1080/messages/${emailData.id}.html`)
const selector = 'a[href*="/reset-password?token="]'
let url = await page.getAttribute(selector, "href")
// TODO: update var instead of doing a replace
url = url!.replace("http://localhost/", "http://localhost:5173/")
// Set the new password and confirm it
await page.goto(url)
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, 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(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 fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
const weakPassword = "123"
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
await page.goto("/recover-password")
await page.getByPlaceholder("Email").fill(email)
await page.getByRole("button", { name: "Continue" }).click()
const emailData = await findLastEmail({
request,
filter: (e) => e.recipients.includes(`<${email}>`),
timeout: 5000,
})
await page.goto(`http://localhost:1080/messages/${emailData.id}.html`)
const selector = 'a[href*="/reset-password?token="]'
let url = await page.getAttribute(selector, "href")
url = url!.replace("http://localhost/", "http://localhost:5173/")
// Set a weak new password
await page.goto(url)
await page.getByLabel("Set Password").fill(weakPassword)
await page.getByLabel("Confirm Password").fill(weakPassword)
await page.getByRole("button", { name: "Reset Password" }).click()
await expect(
page.getByText("Password must be at least 8 characters"),
).toBeVisible()
})

View File

@@ -0,0 +1,169 @@
import { type Page, expect, test } from "@playwright/test"
import { randomEmail, randomPassword } from "./utils/random"
test.use({ storageState: { cookies: [], origins: [] } })
type OptionsType = {
exact?: boolean
}
const fillForm = async (
page: Page,
full_name: string,
email: string,
password: string,
confirm_password: string,
) => {
await page.getByPlaceholder("Full Name").fill(full_name)
await page.getByPlaceholder("Email").fill(email)
await page.getByPlaceholder("Password", { exact: true }).fill(password)
await page.getByPlaceholder("Repeat Password").fill(confirm_password)
}
const verifyInput = async (
page: Page,
placeholder: string,
options?: OptionsType,
) => {
const input = page.getByPlaceholder(placeholder, options)
await expect(input).toBeVisible()
await expect(input).toHaveText("")
await expect(input).toBeEditable()
}
test("Inputs are visible, empty and editable", async ({ page }) => {
await page.goto("/signup")
await verifyInput(page, "Full Name")
await verifyInput(page, "Email")
await verifyInput(page, "Password", { exact: true })
await verifyInput(page, "Repeat Password")
})
test("Sign Up button is visible", async ({ page }) => {
await page.goto("/signup")
await expect(page.getByRole("button", { name: "Sign Up" })).toBeVisible()
})
test("Log In link is visible", async ({ page }) => {
await page.goto("/signup")
await expect(page.getByRole("link", { name: "Log In" })).toBeVisible()
})
test("Sign up with valid name, email, and password", async ({ page }) => {
const full_name = "Test User"
const email = randomEmail()
const password = randomPassword()
await page.goto("/signup")
await fillForm(page, full_name, email, password, password)
await page.getByRole("button", { name: "Sign Up" }).click()
})
test("Sign up with invalid email", async ({ page }) => {
await page.goto("/signup")
await fillForm(
page,
"Playwright Test",
"invalid-email",
"changethis",
"changethis",
)
await page.getByRole("button", { name: "Sign Up" }).click()
await expect(page.getByText("Invalid email address")).toBeVisible()
})
test("Sign up with existing email", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
// Sign up with an email
await page.goto("/signup")
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, fullName, email, password, password)
await page.getByRole("button", { name: "Sign Up" }).click()
await page
.getByText("The user with this email already exists in the system")
.click()
})
test("Sign up with weak password", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const password = "weak"
await page.goto("/signup")
await fillForm(page, fullName, email, password, password)
await page.getByRole("button", { name: "Sign Up" }).click()
await expect(
page.getByText("Password must be at least 8 characters"),
).toBeVisible()
})
test("Sign up with mismatched passwords", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
const password2 = randomPassword()
await page.goto("/signup")
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 fullName = ""
const email = randomEmail()
const password = randomPassword()
await page.goto("/signup")
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 fullName = "Test User"
const email = ""
const password = randomPassword()
await page.goto("/signup")
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 fullName = ""
const email = randomEmail()
const password = ""
await page.goto("/signup")
await fillForm(page, fullName, email, password, password)
await page.getByRole("button", { name: "Sign Up" }).click()
await expect(page.getByText("Password is required")).toBeVisible()
})

View File

@@ -0,0 +1,288 @@
import { expect, test } from "@playwright/test"
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"]
// User Information
test("My profile tab is active by default", async ({ page }) => {
await page.goto("/settings")
await expect(page.getByRole("tab", { name: "My profile" })).toHaveAttribute(
"aria-selected",
"true",
)
})
test("All tabs are visible", async ({ page }) => {
await page.goto("/settings")
for (const tab of tabs) {
await expect(page.getByRole("tab", { name: tab })).toBeVisible()
}
})
test.describe("Edit user full name and email successfully", () => {
test.use({ storageState: { cookies: [], origins: [] } })
test("Edit user name with a valid name", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const updatedName = "Test User 2"
const password = randomPassword()
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "My profile" }).click()
await page.getByRole("button", { name: "Edit" }).click()
await page.getByLabel("Full name").fill(updatedName)
await page.getByRole("button", { name: "Save" }).click()
await expect(page.getByText("User updated successfully")).toBeVisible()
// Check if the new name is displayed on the page
await expect(
page.getByLabel("My profile").getByText(updatedName, { exact: true }),
).toBeVisible()
})
test("Edit user email with a valid email", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const updatedEmail = randomEmail()
const password = randomPassword()
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "My profile" }).click()
await page.getByRole("button", { name: "Edit" }).click()
await page.getByLabel("Email").fill(updatedEmail)
await page.getByRole("button", { name: "Save" }).click()
await expect(page.getByText("User updated successfully")).toBeVisible()
await expect(
page.getByLabel("My profile").getByText(updatedEmail, { exact: true }),
).toBeVisible()
})
})
test.describe("Edit user with invalid data", () => {
test.use({ storageState: { cookies: [], origins: [] } })
test("Edit user email with an invalid email", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
const invalidEmail = ""
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "My profile" }).click()
await page.getByRole("button", { name: "Edit" }).click()
await page.getByLabel("Email").fill(invalidEmail)
await page.locator("body").click()
await expect(page.getByText("Email is required")).toBeVisible()
})
test("Cancel edit action restores original name", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
const updatedName = "Test User"
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "My profile" }).click()
await page.getByRole("button", { name: "Edit" }).click()
await page.getByLabel("Full name").fill(updatedName)
await page.getByRole("button", { name: "Cancel" }).first().click()
await expect(
page.getByLabel("My profile").getByText(fullName, { exact: true }),
).toBeVisible()
})
test("Cancel edit action restores original email", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
const updatedEmail = randomEmail()
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "My profile" }).click()
await page.getByRole("button", { name: "Edit" }).click()
await page.getByLabel("Email").fill(updatedEmail)
await page.getByRole("button", { name: "Cancel" }).first().click()
await expect(
page.getByLabel("My profile").getByText(email, { exact: true }),
).toBeVisible()
})
})
// Change Password
test.describe("Change password successfully", () => {
test.use({ storageState: { cookies: [], origins: [] } })
test("Update password successfully", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
const NewPassword = randomPassword()
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "Password" }).click()
await page.getByLabel("Current Password*").fill(password)
await page.getByLabel("Set Password*").fill(NewPassword)
await page.getByLabel("Confirm Password*").fill(NewPassword)
await page.getByRole("button", { name: "Save" }).click()
await expect(page.getByText("Password updated successfully.")).toBeVisible()
await logOutUser(page)
// Check if the user can log in with the new password
await logInUser(page, email, NewPassword)
})
})
test.describe("Change password with invalid data", () => {
test.use({ storageState: { cookies: [], origins: [] } })
test("Update password with weak passwords", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
const weakPassword = "weak"
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "Password" }).click()
await page.getByLabel("Current Password*").fill(password)
await page.getByLabel("Set Password*").fill(weakPassword)
await page.getByLabel("Confirm Password*").fill(weakPassword)
await expect(
page.getByText("Password must be at least 8 characters"),
).toBeVisible()
})
test("New password and confirmation password do not match", async ({
page,
}) => {
const fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
const newPassword = randomPassword()
const confirmPassword = randomPassword()
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "Password" }).click()
await page.getByLabel("Current Password*").fill(password)
await page.getByLabel("Set Password*").fill(newPassword)
await page.getByLabel("Confirm Password*").fill(confirmPassword)
await page.getByRole("button", { name: "Save" }).click()
await expect(page.getByText("Passwords do not match")).toBeVisible()
})
test("Current password and new password are the same", async ({ page }) => {
const fullName = "Test User"
const email = randomEmail()
const password = randomPassword()
// Sign up a new user
await signUpNewUser(page, fullName, email, password)
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "Password" }).click()
await page.getByLabel("Current Password*").fill(password)
await page.getByLabel("Set Password*").fill(password)
await page.getByLabel("Confirm Password*").fill(password)
await page.getByRole("button", { name: "Save" }).click()
await expect(
page.getByText("New password cannot be the same as the current one"),
).toBeVisible()
})
})
// Appearance
test("Appearance tab is visible", async ({ page }) => {
await page.goto("/settings")
await page.getByRole("tab", { name: "Appearance" }).click()
await expect(page.getByLabel("Appearance")).toBeVisible()
})
test("User can switch from light mode to dark mode", async ({ page }) => {
await page.goto("/settings")
await page.getByRole("tab", { name: "Appearance" }).click()
await page.getByLabel("Appearance").locator("span").nth(3).click()
const isDarkMode = await page.evaluate(() =>
document.body.classList.contains("chakra-ui-dark"),
)
expect(isDarkMode).toBe(true)
})
test("User can switch from dark mode to light mode", async ({ page }) => {
await page.goto("/settings")
await page.getByRole("tab", { name: "Appearance" }).click()
await page.getByLabel("Appearance").locator("span").first().click()
const isLightMode = await page.evaluate(() =>
document.body.classList.contains("chakra-ui-light"),
)
expect(isLightMode).toBe(true)
})
test("Selected mode is preserved across sessions", async ({ page }) => {
await page.goto("/settings")
await page.getByRole("tab", { name: "Appearance" }).click()
await page.getByLabel("Appearance").locator("span").nth(3).click()
await logOutUser(page)
await logInUser(page, firstSuperuser, firstSuperuserPassword)
const isDarkMode = await page.evaluate(() =>
document.body.classList.contains("chakra-ui-dark"),
)
expect(isDarkMode).toBe(true)
})

View File

@@ -0,0 +1,59 @@
import type { APIRequestContext } from "@playwright/test"
type Email = {
id: number
recipients: string[]
subject: string
}
async function findEmail({
request,
filter,
}: { request: APIRequestContext; filter?: (email: Email) => boolean }) {
const response = await request.get("http://localhost:1080/messages")
let emails = await response.json()
if (filter) {
emails = emails.filter(filter)
}
const email = emails[emails.length - 1]
if (email) {
return email as Email
}
return null
}
export function findLastEmail({
request,
filter,
timeout = 5000,
}: {
request: APIRequestContext
filter?: (email: Email) => boolean
timeout?: number
}) {
const timeoutPromise = new Promise<never>((_, reject) =>
setTimeout(
() => reject(new Error("Timeout while trying to get latest email")),
timeout,
),
)
const checkEmails = async () => {
while (true) {
const emailData = await findEmail({ request, filter })
if (emailData) {
return emailData
}
// Wait for 100ms before checking again
await new Promise((resolve) => setTimeout(resolve, 100))
}
}
return Promise.race([timeoutPromise, checkEmails()])
}

View File

@@ -0,0 +1,13 @@
export const randomEmail = () =>
`test_${Math.random().toString(36).substring(7)}@example.com`
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()
.replace(/\s+/g, "-")
.replace(/[^\w-]+/g, "")

View File

@@ -0,0 +1,38 @@
import { type Page, expect } from "@playwright/test"
export async function signUpNewUser(
page: Page,
name: string,
email: string,
password: string,
) {
await page.goto("/signup")
await page.getByPlaceholder("Full Name").fill(name)
await page.getByPlaceholder("Email").fill(email)
await page.getByPlaceholder("Password", { exact: true }).fill(password)
await page.getByPlaceholder("Repeat Password").fill(password)
await page.getByRole("button", { name: "Sign Up" }).click()
await expect(
page.getByText("Your account has been created successfully"),
).toBeVisible()
await page.goto("/login")
}
export async function logInUser(page: Page, email: string, password: string) {
await page.goto("/login")
await page.getByPlaceholder("Email").fill(email)
await page.getByPlaceholder("Password", { exact: true }).fill(password)
await page.getByRole("button", { name: "Log In" }).click()
await page.waitForURL("/")
await expect(
page.getByText("Welcome back, nice to see you again!"),
).toBeVisible()
}
export async function logOutUser(page: Page) {
await page.getByTestId("user-menu").click()
await page.getByRole("menuitem", { name: "Log out" }).click()
await page.goto("/login")
}