86 lines
4.8 KiB
TypeScript
86 lines
4.8 KiB
TypeScript
import { expect, test, type Page, type Response } from "@playwright/test";
|
|
|
|
// The ordinary E2E stand runs with EMAIL_PROVIDER=disabled, so no
|
|
// verification message is ever delivered. Registration on the real backend
|
|
// must then wait for the message without claiming that it went out, and the
|
|
// unconfirmed account must not sign in, read its cabinet, or be confirmed by
|
|
// a guessed code or a forged link. The mail-fixture stand delivers mail, so
|
|
// the spec does not run there.
|
|
const mailFixtureEnabled = process.env.E2E_MAIL_FIXTURE === "1";
|
|
|
|
test.use({ trace: "off", screenshot: "off", video: "off" });
|
|
|
|
function apiResponse(page: Page, method: string, path: string): Promise<Response> {
|
|
return page.waitForResponse(
|
|
(response) =>
|
|
response.request().method() === method && new URL(response.url()).pathname === path,
|
|
);
|
|
}
|
|
|
|
async function problemCode(response: Response): Promise<string | undefined> {
|
|
return ((await response.json()) as { code?: string }).code;
|
|
}
|
|
|
|
test.describe("registration while the mail channel is unavailable", () => {
|
|
test.skip(mailFixtureEnabled, "Runs on the stand whose mail provider is disabled.");
|
|
|
|
test("waits for the message and keeps the unconfirmed account locked", async ({ page }) => {
|
|
const email = `mail-off-${crypto.randomUUID()}@example.test`;
|
|
const password = "correct horse battery staple 2026";
|
|
|
|
await page.goto("/register");
|
|
await page.getByLabel("Электронная почта").fill(email);
|
|
await page.getByLabel("Пароль", { exact: true }).fill(password);
|
|
await page.getByRole("radio", { name: /Кандидат/ }).check();
|
|
await page.getByRole("checkbox", { name: /принимаю его/ }).check();
|
|
const registration = apiResponse(page, "POST", "/api/v1/identity/registrations");
|
|
await page.getByRole("button", { name: "Создать аккаунт" }).click();
|
|
const accepted = await registration;
|
|
expect(accepted.status()).toBe(202);
|
|
expect(await accepted.json()).toEqual({ status: "accepted" });
|
|
|
|
// The step waits for the message and never says it was sent or delivered.
|
|
await expect(page.getByRole("heading", { name: "Запрос принят" })).toBeVisible();
|
|
await expect(page.getByText(/письмо с кодом и ссылкой придет на него/)).toBeVisible();
|
|
await expect(page.getByText(/Письмо может прийти не сразу/)).toBeVisible();
|
|
expect(await page.locator("main").innerText()).not.toMatch(/отправлен|доставлен/i);
|
|
|
|
// A guessed code does not confirm the address.
|
|
await page.getByLabel("Код из письма", { exact: true }).fill("000000");
|
|
const guessed = apiResponse(page, "POST", "/api/v1/identity/email-verifications/code");
|
|
await page.getByRole("button", { name: "Подтвердить код" }).click();
|
|
const guessedResult = await guessed;
|
|
expect(guessedResult.status()).toBe(400);
|
|
expect(await problemCode(guessedResult)).toBe("verification_token_invalid");
|
|
await expect(page.getByRole("alert")).toContainText("Код не подошел");
|
|
|
|
// Neither does a forged link.
|
|
await page.goto(`/verify-email#token=${"A".repeat(43)}`);
|
|
const linkForm = page.getByRole("region", { name: "Подтвердить адрес" });
|
|
await linkForm.getByLabel("Пароль, выбранный при регистрации").fill(password);
|
|
const forged = apiResponse(page, "POST", "/api/v1/identity/email-verifications");
|
|
await linkForm.getByRole("button", { name: "Подтвердить почту" }).click();
|
|
const forgedResult = await forged;
|
|
expect(forgedResult.status()).toBe(400);
|
|
expect(await problemCode(forgedResult)).toBe("verification_token_invalid");
|
|
await expect(linkForm.getByRole("alert")).toContainText("Не удалось подтвердить почту");
|
|
|
|
// The correct password does not open a session while the address waits.
|
|
await page.goto("/login");
|
|
const signIn = page.getByRole("region", { name: "Войти в аккаунт" });
|
|
await signIn.getByLabel("Электронная почта").fill(email);
|
|
await signIn.getByLabel("Пароль", { exact: true }).fill(password);
|
|
const login = apiResponse(page, "POST", "/api/v1/identity/sessions");
|
|
await signIn.getByRole("button", { name: "Войти" }).click();
|
|
const loginResult = await login;
|
|
expect(loginResult.status()).toBe(403);
|
|
expect(await problemCode(loginResult)).toBe("email_not_verified");
|
|
await expect(signIn.getByRole("alert")).toContainText("Почта не подтверждена");
|
|
|
|
expect((await page.request.get("/api/v1/identity/sessions/current")).status()).toBe(401);
|
|
expect((await page.request.get("/api/v1/me/profile")).status()).toBe(401);
|
|
await page.goto("/candidate");
|
|
await expect(page.getByRole("heading", { name: "Вход" })).toBeVisible();
|
|
});
|
|
});
|