Skip to content

Commit 305ad99

Browse files
authored
♻️ Add random password util and refactor tests (fastapi#1277)
1 parent 6fa6d4b commit 305ad99

File tree

5 files changed

+57
-51
lines changed

5 files changed

+57
-51
lines changed

frontend/tests/login.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type Page, expect, test } from "@playwright/test"
22
import { firstSuperuser, firstSuperuserPassword } from "./config.ts"
3+
import { randomPassword } from "./utils/random.ts"
34

45
test.use({ storageState: { cookies: [], origins: [] } })
56

@@ -67,9 +68,10 @@ test("Log in with invalid email", async ({ page }) => {
6768
})
6869

6970
test("Log in with invalid password", async ({ page }) => {
71+
const password = randomPassword()
72+
7073
await page.goto("/login")
71-
// TODO: Add a random password utility
72-
await fillForm(page, firstSuperuser, "changethat")
74+
await fillForm(page, firstSuperuser, password)
7375
await page.getByRole("button", { name: "Log In" }).click()
7476

7577
await expect(page.getByText("Incorrect email or password")).toBeVisible()

frontend/tests/reset-password.spec.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test } from "@playwright/test"
22
import { findLastEmail } from "./utils/mailcatcher"
3-
import { randomEmail } from "./utils/random"
3+
import { randomEmail, randomPassword } from "./utils/random"
44
import { logInUser, signUpNewUser } from "./utils/user"
55

66
test.use({ storageState: { cookies: [], origins: [] } })
@@ -31,13 +31,13 @@ test("User can reset password successfully using the link", async ({
3131
page,
3232
request,
3333
}) => {
34-
const full_name = "Test User"
34+
const fullName = "Test User"
3535
const email = randomEmail()
36-
const password = "changethis"
37-
const new_password = "changethat"
36+
const password = randomPassword()
37+
const newPassword = randomPassword()
3838

3939
// Sign up a new user
40-
await signUpNewUser(page, full_name, email, password)
40+
await signUpNewUser(page, fullName, email, password)
4141

4242
await page.goto("/recover-password")
4343
await page.getByPlaceholder("Email").fill(email)
@@ -62,34 +62,36 @@ test("User can reset password successfully using the link", async ({
6262
// Set the new password and confirm it
6363
await page.goto(url)
6464

65-
await page.getByLabel("Set Password").fill(new_password)
66-
await page.getByLabel("Confirm Password").fill(new_password)
65+
await page.getByLabel("Set Password").fill(newPassword)
66+
await page.getByLabel("Confirm Password").fill(newPassword)
6767
await page.getByRole("button", { name: "Reset Password" }).click()
6868
await expect(page.getByText("Password updated successfully")).toBeVisible()
6969

7070
// Check if the user is able to login with the new password
71-
await logInUser(page, email, new_password)
71+
await logInUser(page, email, newPassword)
7272
})
7373

7474
test("Expired or invalid reset link", async ({ page }) => {
75+
const password = randomPassword()
7576
const invalidUrl = "/reset-password?token=invalidtoken"
7677

7778
await page.goto(invalidUrl)
7879

79-
await page.getByLabel("Set Password").fill("newpassword")
80-
await page.getByLabel("Confirm Password").fill("newpassword")
80+
await page.getByLabel("Set Password").fill(password)
81+
await page.getByLabel("Confirm Password").fill(password)
8182
await page.getByRole("button", { name: "Reset Password" }).click()
8283

8384
await expect(page.getByText("Invalid token")).toBeVisible()
8485
})
8586

8687
test("Weak new password validation", async ({ page, request }) => {
87-
const full_name = "Test User"
88+
const fullName = "Test User"
8889
const email = randomEmail()
89-
const password = "password"
90+
const password = randomPassword()
91+
const weakPassword = "123"
9092

9193
// Sign up a new user
92-
await signUpNewUser(page, full_name, email, password)
94+
await signUpNewUser(page, fullName, email, password)
9395

9496
await page.goto("/recover-password")
9597
await page.getByPlaceholder("Email").fill(email)
@@ -109,8 +111,8 @@ test("Weak new password validation", async ({ page, request }) => {
109111

110112
// Set a weak new password
111113
await page.goto(url)
112-
await page.getByLabel("Set Password").fill("123")
113-
await page.getByLabel("Confirm Password").fill("123")
114+
await page.getByLabel("Set Password").fill(weakPassword)
115+
await page.getByLabel("Confirm Password").fill(weakPassword)
114116
await page.getByRole("button", { name: "Reset Password" }).click()
115117

116118
await expect(

frontend/tests/sign-up.spec.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type Page, expect, test } from "@playwright/test"
22

3-
import { randomEmail } from "./utils/random"
3+
import { randomEmail, randomPassword } from "./utils/random"
44

55
test.use({ storageState: { cookies: [], origins: [] } })
66

@@ -56,7 +56,7 @@ test("Log In link is visible", async ({ page }) => {
5656
test("Sign up with valid name, email, and password", async ({ page }) => {
5757
const full_name = "Test User"
5858
const email = randomEmail()
59-
const password = "changethis"
59+
const password = randomPassword()
6060

6161
await page.goto("/signup")
6262
await fillForm(page, full_name, email, password, password)
@@ -79,20 +79,20 @@ test("Sign up with invalid email", async ({ page }) => {
7979
})
8080

8181
test("Sign up with existing email", async ({ page }) => {
82-
const full_name = "Test User"
82+
const fullName = "Test User"
8383
const email = randomEmail()
84-
const password = "changethis"
84+
const password = randomPassword()
8585

8686
// Sign up with an email
8787
await page.goto("/signup")
8888

89-
await fillForm(page, full_name, email, password, password)
89+
await fillForm(page, fullName, email, password, password)
9090
await page.getByRole("button", { name: "Sign Up" }).click()
9191

9292
// Sign up again with the same email
9393
await page.goto("/signup")
9494

95-
await fillForm(page, full_name, email, password, password)
95+
await fillForm(page, fullName, email, password, password)
9696
await page.getByRole("button", { name: "Sign Up" }).click()
9797

9898
await page
@@ -101,13 +101,13 @@ test("Sign up with existing email", async ({ page }) => {
101101
})
102102

103103
test("Sign up with weak password", async ({ page }) => {
104-
const full_name = "Test User"
104+
const fullName = "Test User"
105105
const email = randomEmail()
106106
const password = "weak"
107107

108108
await page.goto("/signup")
109109

110-
await fillForm(page, full_name, email, password, password)
110+
await fillForm(page, fullName, email, password, password)
111111
await page.getByRole("button", { name: "Sign Up" }).click()
112112

113113
await expect(
@@ -116,53 +116,53 @@ test("Sign up with weak password", async ({ page }) => {
116116
})
117117

118118
test("Sign up with mismatched passwords", async ({ page }) => {
119-
const full_name = "Test User"
119+
const fullName = "Test User"
120120
const email = randomEmail()
121-
const password = "changethis"
122-
const password2 = "changethat"
121+
const password = randomPassword()
122+
const password2 = randomPassword()
123123

124124
await page.goto("/signup")
125125

126-
await fillForm(page, full_name, email, password, password2)
126+
await fillForm(page, fullName, email, password, password2)
127127
await page.getByRole("button", { name: "Sign Up" }).click()
128128

129129
await expect(page.getByText("Passwords do not match")).toBeVisible()
130130
})
131131

132132
test("Sign up with missing full name", async ({ page }) => {
133-
const full_name = ""
133+
const fullName = ""
134134
const email = randomEmail()
135-
const password = "changethis"
135+
const password = randomPassword()
136136

137137
await page.goto("/signup")
138138

139-
await fillForm(page, full_name, email, password, password)
139+
await fillForm(page, fullName, email, password, password)
140140
await page.getByRole("button", { name: "Sign Up" }).click()
141141

142142
await expect(page.getByText("Full Name is required")).toBeVisible()
143143
})
144144

145145
test("Sign up with missing email", async ({ page }) => {
146-
const full_name = "Test User"
146+
const fullName = "Test User"
147147
const email = ""
148-
const password = "changethis"
148+
const password = randomPassword()
149149

150150
await page.goto("/signup")
151151

152-
await fillForm(page, full_name, email, password, password)
152+
await fillForm(page, fullName, email, password, password)
153153
await page.getByRole("button", { name: "Sign Up" }).click()
154154

155155
await expect(page.getByText("Email is required")).toBeVisible()
156156
})
157157

158158
test("Sign up with missing password", async ({ page }) => {
159-
const full_name = ""
159+
const fullName = ""
160160
const email = randomEmail()
161161
const password = ""
162162

163163
await page.goto("/signup")
164164

165-
await fillForm(page, full_name, email, password, password)
165+
await fillForm(page, fullName, email, password, password)
166166
await page.getByRole("button", { name: "Sign Up" }).click()
167167

168168
await expect(page.getByText("Password is required")).toBeVisible()

frontend/tests/user-settings.spec.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect, test } from "@playwright/test"
2-
import { randomEmail } from "./utils/random"
3-
import { logInUser, logOutUser, signUpNewUser } from "./utils/user"
42
import { firstSuperuser, firstSuperuserPassword } from "./config.ts"
3+
import { randomEmail, randomPassword } from "./utils/random"
4+
import { logInUser, logOutUser, signUpNewUser } from "./utils/user"
55

66
const tabs = ["My profile", "Password", "Appearance"]
77

@@ -29,7 +29,7 @@ test.describe("Edit user full name and email successfully", () => {
2929
const fullName = "Test User"
3030
const email = randomEmail()
3131
const updatedName = "Test User 2"
32-
const password = "password"
32+
const password = randomPassword()
3333

3434
// Sign up a new user
3535
await signUpNewUser(page, fullName, email, password)
@@ -53,7 +53,7 @@ test.describe("Edit user full name and email successfully", () => {
5353
const fullName = "Test User"
5454
const email = randomEmail()
5555
const updatedEmail = randomEmail()
56-
const password = "password"
56+
const password = randomPassword()
5757

5858
// Sign up a new user
5959
await signUpNewUser(page, fullName, email, password)
@@ -79,7 +79,7 @@ test.describe("Edit user with invalid data", () => {
7979
test("Edit user email with an invalid email", async ({ page }) => {
8080
const fullName = "Test User"
8181
const email = randomEmail()
82-
const password = "password"
82+
const password = randomPassword()
8383
const invalidEmail = ""
8484

8585
// Sign up a new user
@@ -99,7 +99,7 @@ test.describe("Edit user with invalid data", () => {
9999
test("Cancel edit action restores original name", async ({ page }) => {
100100
const fullName = "Test User"
101101
const email = randomEmail()
102-
const password = "password"
102+
const password = randomPassword()
103103
const updatedName = "Test User"
104104

105105
// Sign up a new user
@@ -121,7 +121,7 @@ test.describe("Edit user with invalid data", () => {
121121
test("Cancel edit action restores original email", async ({ page }) => {
122122
const fullName = "Test User"
123123
const email = randomEmail()
124-
const password = "password"
124+
const password = randomPassword()
125125
const updatedEmail = randomEmail()
126126

127127
// Sign up a new user
@@ -149,8 +149,8 @@ test.describe("Change password successfully", () => {
149149
test("Update password successfully", async ({ page }) => {
150150
const fullName = "Test User"
151151
const email = randomEmail()
152-
const password = "password"
153-
const NewPassword = "newPassword"
152+
const password = randomPassword()
153+
const NewPassword = randomPassword()
154154

155155
// Sign up a new user
156156
await signUpNewUser(page, fullName, email, password)
@@ -179,7 +179,7 @@ test.describe("Change password with invalid data", () => {
179179
test("Update password with weak passwords", async ({ page }) => {
180180
const fullName = "Test User"
181181
const email = randomEmail()
182-
const password = "password"
182+
const password = randomPassword()
183183
const weakPassword = "weak"
184184

185185
// Sign up a new user
@@ -203,9 +203,9 @@ test.describe("Change password with invalid data", () => {
203203
}) => {
204204
const fullName = "Test User"
205205
const email = randomEmail()
206-
const password = "password"
207-
const newPassword = "newPassword"
208-
const confirmPassword = "confirmPassword"
206+
const password = randomPassword()
207+
const newPassword = randomPassword()
208+
const confirmPassword = randomPassword()
209209

210210
// Sign up a new user
211211
await signUpNewUser(page, fullName, email, password)
@@ -225,7 +225,7 @@ test.describe("Change password with invalid data", () => {
225225
test("Current password and new password are the same", async ({ page }) => {
226226
const fullName = "Test User"
227227
const email = randomEmail()
228-
const password = "password"
228+
const password = randomPassword()
229229

230230
// Sign up a new user
231231
await signUpNewUser(page, fullName, email, password)

frontend/tests/utils/random.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export const randomEmail = () =>
44
export const randomTeamName = () =>
55
`Team ${Math.random().toString(36).substring(7)}`
66

7+
export const randomPassword = () => `${Math.random().toString(36).substring(2)}`
8+
79
export const slugify = (text: string) =>
810
text
911
.toLowerCase()

0 commit comments

Comments
 (0)