2022-05-14 03:02:10 +00:00
|
|
|
import { expect } from "@playwright/test";
|
2023-07-12 17:27:41 +00:00
|
|
|
import { uuid } from "short-uuid";
|
|
|
|
|
|
|
|
import { verifyPassword } from "@calcom/features/auth/lib/verifyPassword";
|
|
|
|
import prisma from "@calcom/prisma";
|
2022-01-07 20:23:37 +00:00
|
|
|
|
2022-05-14 03:02:10 +00:00
|
|
|
import { test } from "../lib/fixtures";
|
|
|
|
|
2023-03-01 20:18:51 +00:00
|
|
|
test.afterEach(({ users }) => users.deleteAll());
|
|
|
|
|
2022-05-14 03:02:10 +00:00
|
|
|
test("Can reset forgotten password", async ({ page, users }) => {
|
|
|
|
const user = await users.create();
|
2023-07-12 17:27:41 +00:00
|
|
|
|
2022-01-07 20:23:37 +00:00
|
|
|
// Got to reset password flow
|
|
|
|
await page.goto("/auth/forgot-password");
|
|
|
|
|
2022-05-14 03:02:10 +00:00
|
|
|
await page.fill('input[name="email"]', `${user.username}@example.com`);
|
2023-07-12 17:27:41 +00:00
|
|
|
await page.press('input[name="email"]', "Enter");
|
|
|
|
|
|
|
|
// wait for confirm page.
|
|
|
|
await page.waitForSelector("text=Reset link sent");
|
|
|
|
|
|
|
|
// As a workaround, we query the db for the last created password request
|
|
|
|
// there should be one, otherwise we throw
|
|
|
|
const { id } = await prisma.resetPasswordRequest.findFirstOrThrow({
|
|
|
|
where: {
|
|
|
|
email: `${user.username}@example.com`,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
createdAt: "desc",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test when a user changes his email after starting the password reset flow
|
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
email: `${user.username}@example.com`,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
email: `${user.username}-2@example.com`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await page.goto(`/auth/forgot-password/${id}`);
|
|
|
|
|
|
|
|
await page.waitForSelector("text=That request is expired.");
|
2022-01-07 20:23:37 +00:00
|
|
|
|
2023-07-12 17:27:41 +00:00
|
|
|
// Change the email back to continue testing.
|
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
email: `${user.username}-2@example.com`,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
email: `${user.username}@example.com`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await page.goto(`/auth/forgot-password/${id}`);
|
|
|
|
|
|
|
|
const newPassword = `${user.username}-123CAL-${uuid().toString()}`; // To match the password policy
|
2022-01-07 20:23:37 +00:00
|
|
|
|
2022-01-19 15:54:54 +00:00
|
|
|
// Wait for page to fully load
|
|
|
|
await page.waitForSelector("text=Reset Password");
|
2022-01-07 20:23:37 +00:00
|
|
|
|
2023-07-12 17:27:41 +00:00
|
|
|
await page.fill('input[name="new_password"]', newPassword);
|
2022-01-07 20:23:37 +00:00
|
|
|
await page.click('button[type="submit"]');
|
|
|
|
|
2023-05-30 03:12:17 +00:00
|
|
|
await page.waitForSelector("text=Password updated");
|
2022-01-17 18:15:18 +00:00
|
|
|
|
2022-09-12 09:25:54 +00:00
|
|
|
await expect(page.locator(`text=Password updated`)).toBeVisible();
|
2023-07-12 17:27:41 +00:00
|
|
|
// now we check our DB to confirm the password was indeed updated.
|
|
|
|
// we're not logging in to the UI to speed up test performance.
|
|
|
|
const updatedUser = await prisma.user.findUniqueOrThrow({
|
|
|
|
where: {
|
|
|
|
email: `${user.username}@example.com`,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
password: true,
|
|
|
|
},
|
|
|
|
});
|
2022-01-07 20:23:37 +00:00
|
|
|
|
2023-07-12 17:27:41 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
await expect(await verifyPassword(newPassword, updatedUser.password!)).toBeTruthy();
|
|
|
|
|
|
|
|
// finally, make sure the same URL cannot be used to reset the password again, as it should be expired.
|
|
|
|
await page.goto(`/auth/forgot-password/${id}`);
|
2022-01-17 18:15:18 +00:00
|
|
|
|
2023-07-12 17:27:41 +00:00
|
|
|
await page.waitForSelector("text=That request is expired.");
|
2022-01-07 20:23:37 +00:00
|
|
|
});
|