-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Add test for signing up and setup for dev
mailcatcher
(#115)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
- Loading branch information
Showing
5 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expect, test } from "@playwright/test" | ||
import { findLastEmail } from "./utils/mailcatcher" | ||
import { randomEmail } from "./utils/random" | ||
|
||
test.use({ storageState: { cookies: [], origins: [] } }) | ||
|
||
test("Signup", async ({ page, request }) => { | ||
const email = randomEmail() | ||
|
||
await page.goto("/signup") | ||
await page.getByPlaceholder("Full Name").fill("Playwright Test") | ||
await page.getByPlaceholder("Email").fill(email) | ||
await page.getByPlaceholder("Password", { exact: true }).fill("changethis") | ||
await page.getByPlaceholder("Repeat Password").fill("changethis") | ||
await page.getByRole("button", { name: "Sign Up" }).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*="/verify-email?token="]' | ||
|
||
let url = await page.getAttribute(selector, "href") | ||
|
||
// TODO: update var instead of doing a replace | ||
url = url!.replace("http://localhost/", "http://localhost:5173/") | ||
|
||
await page.goto(url) | ||
|
||
await expect(page.getByTestId("result")).toContainText( | ||
"Successful Email Verification", | ||
{ timeout: 5000 }, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { 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 findEmailPromise = new Promise<Email>(async (resolve, reject) => { | ||
try { | ||
while (true) { | ||
const emailData = await findEmail({ request, filter }) | ||
|
||
if (emailData) { | ||
resolve(emailData) | ||
return | ||
} | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 100)) | ||
} | ||
} catch (e) { | ||
reject(e) | ||
} | ||
}) | ||
|
||
return Promise.race([timeoutPromise, findEmailPromise]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const randomEmail = () => | ||
`test-${Math.random().toString(36).substring(7)}@example.com` |