-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathuser-settings.spec.ts
330 lines (270 loc) · 10.2 KB
/
user-settings.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { expect, test } from "@playwright/test"
import { firstSuperuser, firstSuperuserPassword } from "./config.ts"
import { createUser } from "./utils/privateApi.ts"
import { randomEmail, randomPassword } from "./utils/random"
import { logInUser, logOutUser } 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 email = randomEmail()
const updatedName = "Test User 2"
const password = randomPassword()
await createUser({ 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 email = randomEmail()
const updatedEmail = randomEmail()
const password = randomPassword()
await createUser({ 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 email = randomEmail()
const password = randomPassword()
const invalidEmail = ""
await createUser({ 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 email = randomEmail()
const password = randomPassword()
const updatedName = "Test User"
const user = await createUser({ 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(user.full_name as string, { exact: true }),
).toBeVisible()
})
test("Cancel edit action restores original email", async ({ page }) => {
const email = randomEmail()
const password = randomPassword()
const updatedEmail = randomEmail()
await createUser({ 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 email = randomEmail()
const password = randomPassword()
const NewPassword = randomPassword()
await createUser({ email, password })
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "Password" }).click()
await page.getByPlaceholder("Current Password").fill(password)
await page.getByPlaceholder("New Password").fill(NewPassword)
await page.getByPlaceholder("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 email = randomEmail()
const password = randomPassword()
const weakPassword = "weak"
await createUser({ email, password })
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "Password" }).click()
await page.getByPlaceholder("Current Password").fill(password)
await page.getByPlaceholder("New Password").fill(weakPassword)
await page.getByPlaceholder("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 email = randomEmail()
const password = randomPassword()
const newPassword = randomPassword()
const confirmPassword = randomPassword()
await createUser({ email, password })
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "Password" }).click()
await page.getByPlaceholder("Current Password").fill(password)
await page.getByPlaceholder("New Password").fill(newPassword)
await page.getByPlaceholder("Confirm Password").fill(confirmPassword)
await page.getByLabel("Password", { exact: true }).locator("form").click()
await expect(page.getByText("The passwords do not match")).toBeVisible()
})
test("Current password and new password are the same", async ({ page }) => {
const email = randomEmail()
const password = randomPassword()
await createUser({ email, password })
// Log in the user
await logInUser(page, email, password)
await page.goto("/settings")
await page.getByRole("tab", { name: "Password" }).click()
await page.getByPlaceholder("Current Password").fill(password)
await page.getByPlaceholder("New Password").fill(password)
await page.getByPlaceholder("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 and vice versa", async ({
page,
}) => {
await page.goto("/settings")
await page.getByRole("tab", { name: "Appearance" }).click()
// Ensure the initial state is light mode
if (
await page.evaluate(() =>
document.documentElement.classList.contains("dark"),
)
) {
await page
.locator("label")
.filter({ hasText: "Light Mode" })
.locator("span")
.first()
.click()
}
let isLightMode = await page.evaluate(() =>
document.documentElement.classList.contains("light"),
)
expect(isLightMode).toBe(true)
await page
.locator("label")
.filter({ hasText: "Dark Mode" })
.locator("span")
.first()
.click()
const isDarkMode = await page.evaluate(() =>
document.documentElement.classList.contains("dark"),
)
expect(isDarkMode).toBe(true)
await page
.locator("label")
.filter({ hasText: "Light Mode" })
.locator("span")
.first()
.click()
isLightMode = await page.evaluate(() =>
document.documentElement.classList.contains("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()
// Ensure the initial state is light mode
if (
await page.evaluate(() =>
document.documentElement.classList.contains("dark"),
)
) {
await page
.locator("label")
.filter({ hasText: "Light Mode" })
.locator("span")
.first()
.click()
}
const isLightMode = await page.evaluate(() =>
document.documentElement.classList.contains("light"),
)
expect(isLightMode).toBe(true)
await page
.locator("label")
.filter({ hasText: "Dark Mode" })
.locator("span")
.first()
.click()
let isDarkMode = await page.evaluate(() =>
document.documentElement.classList.contains("dark"),
)
expect(isDarkMode).toBe(true)
await logOutUser(page)
await logInUser(page, firstSuperuser, firstSuperuserPassword)
isDarkMode = await page.evaluate(() =>
document.documentElement.classList.contains("dark"),
)
expect(isDarkMode).toBe(true)
})