How to test password reset end to end
Password reset is one of the most important flows in an app and one of the least tested. When it breaks, users get locked out and only find out when they need it. It usually goes untested for one reason: the middle step happens in an inbox. With a real inbox your test can read, the whole flow becomes an ordinary test.
This guide uses Playwright and the saci.cc npm package, but the ideas apply to any tool.
What's worth testing
Seeing the email arrive isn't enough. A password reset test that catches real problems checks that:
- The link arrives and opens the right page, on the right domain, over HTTPS.
- The new password works and the old one doesn't.
- The link works only once. A reusable link is a security hole.
- An email without an account reveals nothing. The page says the same thing whether or not the account exists, and no email goes out.
- The email doesn't contain the password, old or new.
Set up the key and the inbox
Create a free key on your account page and put it in the SACI_API_KEY environment variable. In CI, store it as a secret. Then install the client:
npm install --save-dev saci.cc
Each test creates its own inbox with createInbox(), so tests don't get in each other's way, even in parallel. Inboxes created with your key are private: only your account can read them, which matters here, because a reset link grants access to the account.
The full test
The test creates an account with the inbox's address, asks for a reset link, opens it, changes the password and logs in with the new one:
import { test, expect } from "@playwright/test";
import { createInbox } from "saci.cc";
test("resets the password by email", async ({ page }) => {
test.setTimeout(90_000);
const inbox = await createInbox();
await signUp(page, inbox.address, "old-password-123"); // your sign-up
const requestedAt = new Date();
await page.goto("/forgot-password");
await page.getByLabel("Email").fill(inbox.address);
await page.getByRole("button", { name: "Send link" }).click();
const link = await inbox.waitForLink({ match: "/reset-password", after: requestedAt });
expect(link).toMatch(/^https:\/\/your-app\.com\//);
await page.goto(link);
await page.getByLabel("New password").fill("new-password-456");
await page.getByRole("button", { name: "Save" }).click();
await logIn(page, inbox.address, "new-password-456");
await expect(page.getByText("My account")).toBeVisible();
});Two details matter:
after: requestedAtskips the emails already in the inbox. Without it, the welcome email from sign-up could be read instead of the reset email.matchpicks the right link. Reset emails usually have other links too: the logo, the help center, unsubscribe.
The link works only once
After changing the password, open the same link again. The app should refuse it:
await page.goto(link);
await expect(page.getByText("This link has expired")).toBeVisible();In the same test, check that the old password stopped working too. Apps often save the new password without invalidating old sessions and tokens.
No account: same answer, no email
If the page says "we couldn't find that account", anyone can find out who your customers are. The answer must be the same either way. To prove no email went out, wait a little and check that nothing arrived:
import { createInbox, SaciTimeoutError } from "saci.cc";
test("doesn't reveal whether the email has an account", async ({ page }) => {
test.setTimeout(60_000);
const inbox = await createInbox(); // nobody signed up with it
await page.goto("/forgot-password");
await page.getByLabel("Email").fill(inbox.address);
await page.getByRole("button", { name: "Send link" }).click();
await expect(page.getByText("If there's an account, we sent a link")).toBeVisible();
await expect(inbox.waitForEmail({ timeout: 20_000 })).rejects.toBeInstanceOf(SaciTimeoutError);
});Twenty seconds is enough for most sending providers. It makes the test a bit slower, so it's worth running only in CI rather than on every local change.
Check what the email says
waitForEmail returns the whole email, with the text, the HTML and every link. Use it to catch regressions in the email itself:
const email = await inbox.waitForEmail({ subject: "Reset your password", after: requestedAt });
expect(email.text).not.toContain("old-password-123");
expect(email.links.every((url) => url.startsWith("https://"))).toBe(true);
expect(email.verificationLink).toContain("/reset-password");verificationLink is the link saci.cc recognized as the one to confirm the address, sign in or reset the password. It helps when your app changes the link's path and you don't want the test to break.
In Cypress
Cypress runs tests in the browser, so the client lives in a task in cypress.config.ts:
on("task", {
createInbox: async () => (await saci.createInbox()).address,
waitForResetLink: ({ address, after }: { address: string; after: string }) =>
saci.inbox(address).waitForLink({ match: "/reset-password", after }),
});Pass after as an ISO string (new Date().toISOString()), because tasks only take serializable values, and call it with { timeout: 90_000 } on cy.task.
In short
- One inbox per test, created with
createInbox()and your key. waitForLink({ match, after })picks the right link and skips older emails.- Also test what must not happen: a reused link, the old password still working, an email to someone without an account.
- For the rest of the sign-up flow, see the Playwright guide.