-
-
+
+
+
-
+
+ {(urlErrorMessage || formError) && (
+
+
+ {urlErrorMessage ? (
+
{
+ stripErrorQuery();
+ }}
+ className="w-full"
+ />
+ ) : null}
+ {formError ? (
+ {
+ setFormError("");
+ }}
+ className="w-full"
+ />
+ ) : null}
+
+
+ )}
+
+ {!sent ? (
+
+ ) : null}
- {(urlErrorMessage || formError) && (
-
-
- {urlErrorMessage ? (
-
{
- stripErrorQuery();
- }}
- className="w-full"
- />
- ) : null}
- {formError ? (
- {
- setFormError("");
- }}
- className="w-full"
- />
- ) : null}
-
-
- )}
-
- {!sent ? (
-
- ) : null}
+ >
+ )}
+
);
}
diff --git a/app/contexts/AuthModalContext.tsx b/app/contexts/AuthModalContext.tsx
index 55d4364..f7d4929 100644
--- a/app/contexts/AuthModalContext.tsx
+++ b/app/contexts/AuthModalContext.tsx
@@ -94,6 +94,7 @@ export function AuthModalProvider({ children }: { children: ReactNode }) {
variant={opts.variant ?? "default"}
magicLinkNextPath={opts.nextPath}
onDismiss={keepRuleDismiss}
+ onClose={closeLogin}
/>
diff --git a/app/hooks/index.ts b/app/hooks/index.ts
index 256749d..3a89a01 100644
--- a/app/hooks/index.ts
+++ b/app/hooks/index.ts
@@ -19,6 +19,7 @@ export { useMediaQuery,
export { useAsyncConfirm } from "./useAsyncConfirm";
export type { AsyncConfirmOptions } from "./useAsyncConfirm";
export { useBeforeUnloadGuard } from "./useBeforeUnloadGuard";
+export { useVisualViewportHeight } from "./useVisualViewportHeight";
export type {
SchemaOrganization,
SchemaWebSite,
diff --git a/app/hooks/useVisualViewportHeight.ts b/app/hooks/useVisualViewportHeight.ts
new file mode 100644
index 0000000..83e741f
--- /dev/null
+++ b/app/hooks/useVisualViewportHeight.ts
@@ -0,0 +1,33 @@
+import { useEffect, useState } from "react";
+
+/**
+ * Visible viewport height, including the shrink when a phone keyboard opens.
+ *
+ * @param enabled When false, skip listeners and return `undefined`.
+ * @returns Pixel height of `visualViewport`, or `undefined` when disabled.
+ *
+ * @example
+ * const viewportHeight = useVisualViewportHeight(isOpen);
+ */
+export function useVisualViewportHeight(enabled: boolean): number | undefined {
+ const [height, setHeight] = useState
(undefined);
+
+ useEffect(() => {
+ if (!enabled) return;
+ const vp = window.visualViewport;
+ const sync = () => {
+ setHeight(vp?.height ?? window.innerHeight);
+ };
+ sync();
+ vp?.addEventListener("resize", sync);
+ vp?.addEventListener("scroll", sync);
+ window.addEventListener("resize", sync);
+ return () => {
+ vp?.removeEventListener("resize", sync);
+ vp?.removeEventListener("scroll", sync);
+ window.removeEventListener("resize", sync);
+ };
+ }, [enabled]);
+
+ return enabled ? height : undefined;
+}
diff --git a/lib/server/magicLinkMail.ts b/lib/server/magicLinkMail.ts
new file mode 100644
index 0000000..ce840ac
--- /dev/null
+++ b/lib/server/magicLinkMail.ts
@@ -0,0 +1,123 @@
+import { ASSETS, getAssetPath } from "../assetUtils";
+import { MAGIC_LINK_TTL_MINUTES } from "../privacyRetention";
+
+export const MAGIC_LINK_LOGO_PATH = getAssetPath(ASSETS.LOGO_MARK);
+
+export type MagicLinkMailInput = {
+ verifyUrl: string;
+ origin: string;
+};
+
+export type MagicLinkMailContent = {
+ subject: string;
+ previewText: string;
+ text: string;
+ html: string;
+};
+
+function escapeHtml(value: string): string {
+ return value
+ .replace(/&/g, "&")
+ .replace(//g, ">")
+ .replace(/"/g, """);
+}
+
+function originHost(origin: string): string {
+ try {
+ return new URL(origin).host;
+ } catch {
+ return origin.replace(/^https?:\/\//, "").replace(/\/$/, "");
+ }
+}
+
+export function buildMagicLinkMail(
+ input: MagicLinkMailInput,
+): MagicLinkMailContent {
+ const origin = input.origin.replace(/\/$/, "");
+ const minutes = MAGIC_LINK_TTL_MINUTES;
+ const subject = "Your login link for CommunityRule";
+ const previewText = `It works once, for ${minutes} minutes.`;
+ const heading = "Log in to CommunityRule";
+ const intro = `Use the button to log in. It works once, for ${minutes} minutes.`;
+ const ignore =
+ "If you did not ask to log in, ignore this email. Nobody can log in without the link.";
+ const madeBy =
+ "CommunityRule is made by the Media Economies Design Lab at the University of Colorado Boulder.";
+ const privacyUrl = `${origin}/privacy`;
+ const aboutUrl = `${origin}/about`;
+ const logoUrl = `${origin}${MAGIC_LINK_LOGO_PATH}`;
+ const host = originHost(origin);
+
+ const text = [
+ heading,
+ "",
+ intro,
+ "",
+ `Log in: ${input.verifyUrl}`,
+ "",
+ ignore,
+ "",
+ madeBy,
+ `Privacy ${privacyUrl}`,
+ `About CommunityRule ${aboutUrl}`,
+ "",
+ `Sent because you asked to log in at ${host}.`,
+ ].join("\n");
+
+ const href = escapeHtml(input.verifyUrl);
+ const html = `
+
+
+
+
+${escapeHtml(subject)}
+
+
+${escapeHtml(
+ previewText,
+ )}
+
+
+
+
+
+
+
+
+ | CommunityRule |
+
+
+ |
+
+
+ |
+
+
+
+ ${escapeHtml(heading)}
+ ${escapeHtml(intro)}
+
+ Log in
+
+
+ ${escapeHtml(ignore)}
+
+ |
+
+
+ |
+
+
+
+`;
+
+ return { subject, previewText, text, html };
+}
diff --git a/lib/server/mail.ts b/lib/server/mail.ts
index e0838d9..bdb1d10 100644
--- a/lib/server/mail.ts
+++ b/lib/server/mail.ts
@@ -6,6 +6,7 @@ import {
type WorkshopInquiryMailInput,
} from "../workshopSignupMail";
import { MAGIC_LINK_TTL_MINUTES } from "../privacyRetention";
+import { buildMagicLinkMail } from "./magicLinkMail";
import { getSmtpUrl } from "./env";
import {
buildRuleStakeholderInviteMail,
@@ -87,16 +88,12 @@ async function sendHtmlMail(opts: {
export async function sendMagicLinkEmail(
to: string,
verifyUrl: string,
+ origin: string,
): Promise {
- const { text, html } = buildVerifyLinkParts(
- verifyUrl,
- `Open this link to log in (it expires in ${MAGIC_LINK_TTL_MINUTES} minutes):`,
- "If you did not request this, you can ignore this email.\n\nSent because you saved a draft at communityrule.info",
- "Log in",
- );
+ const { subject, text, html } = buildMagicLinkMail({ verifyUrl, origin });
await sendHtmlMail({
to,
- subject: "Log in to Community Rule",
+ subject,
text,
html,
devLog: `[dev] Magic link for ${to}: ${verifyUrl}`,
diff --git a/messages/en/modals/askOrganizerInquiry.json b/messages/en/modals/askOrganizerInquiry.json
index 0e012ec..f3a315b 100644
--- a/messages/en/modals/askOrganizerInquiry.json
+++ b/messages/en/modals/askOrganizerInquiry.json
@@ -1,15 +1,17 @@
{
- "_comment": "CR-107 Ask an organizer modal (Figma 22078-587823)",
- "title": "Ask an Organizer",
+ "_comment": "Ask an organizer modal (Figma 22078-587823, sent/failed 25032:48584 / 25032:48642)",
+ "title": "Ask an organizer",
"description": "Have a question about organizing? Send it over and an experienced organizer will get back to you.",
"emailLabel": "Email address",
"emailPlaceholder": "you@example.com",
"questionLabel": "Your question",
"questionPlaceholder": "What would you like to know?",
- "submitButton": "Confirm Question",
+ "submitButton": "Send question",
"closeAfterSuccess": "Close",
- "successTitle": "Thanks, we received your question",
- "successDescription": "Check your inbox and an organizer will reply when they can.",
+ "successTitle": "Thanks",
+ "successDescription": "One of us will write back within two working days.",
+ "sendFailedTitle": "We could not send that.",
+ "sendFailedDescription": "Your question is still here. Try again in a moment.",
"genericError": "Something went wrong. Please try again.",
"rateLimitedError": "Too many attempts. Please wait a bit and try again.",
"ariaDialog": "Ask an organizer",
diff --git a/messages/en/pages/login.json b/messages/en/pages/login.json
index 85d176d..b68d268 100644
--- a/messages/en/pages/login.json
+++ b/messages/en/pages/login.json
@@ -1,6 +1,6 @@
{
"title": "Log in to CommunityRule",
- "subtitle": "Enter your email and we'll send you a magic link to log in. No password needed!",
+ "subtitle": "Enter your email and we'll send you a login link. No password needed.",
"saveProgressTitle": "Save your progress?",
"saveProgressSubtitle": "We need your email to save, and we'll send you a magic link to log in. If you don't save now you could lose your progress.",
"keepRuleTitle": "Log in to keep this rule on your profile",
@@ -11,9 +11,15 @@
"continueWithoutSaving": "Continue without saving",
"emailLabel": "Email address",
"emailPlaceholder": "you@example.com",
- "sendMagicLink": "Send me a magic link",
+ "sendMagicLink": "Send login link",
+ "sendNewLink": "Send a new link",
+ "sendAgain": "Send it again",
+ "close": "Close",
"successTitle": "Check your email",
"successBody": "We sent a login link that works for 60 minutes. Open it on this device to continue. If you don't see it, check spam or promotions.",
+ "sentBody": "We sent a login link to {email}. It works for {minutes} minutes.",
+ "expiredTitle": "That link has expired",
+ "expiredBody": "That login link has expired or was already used. Send a new one.",
"legalPrefix": "By continuing, you agree to our ",
"legalAnd": " and ",
"legalSuffix": ".",
@@ -21,7 +27,7 @@
"loadingFallback": "Loading…",
"errors": {
"network": "Something went wrong. Check your connection and try again.",
- "emailInvalid": "Enter a valid email address.",
+ "emailInvalid": "Enter an email address like name@example.org.",
"rateLimited": "Too many requests. Try again in {seconds} seconds.",
"generic": "Something went wrong. Try again.",
"invalidLink": "That login link is not valid. Request a new one from the login page.",
diff --git a/stories/modals/Login.stories.tsx b/stories/modals/Login.stories.tsx
index 28e9832..d129644 100644
--- a/stories/modals/Login.stories.tsx
+++ b/stories/modals/Login.stories.tsx
@@ -109,7 +109,7 @@ export const HeaderOverlayBlurred = {
ariaLabelledBy="login-modal-heading"
>
Loading…}>
-
+ {}} />
@@ -154,7 +154,53 @@ export const FullPageRouteSolid = {
}
>
Loading…}>
-
+ {}} />
+
+
+ ),
+};
+
+export const LinkExpired = {
+ name: "Link expired",
+ parameters: {
+ nextjs: {
+ navigation: {
+ pathname: "/login",
+ query: { error: "expired_link" },
+ },
+ },
+ docs: {
+ description: {
+ story:
+ "Expired or used login links land on this heading, with Send a new link.",
+ },
+ },
+ },
+ decorators: [
+ (Story: () => React.ReactNode) => (
+
+
+
+ ),
+ ],
+ render: () => (
+ {}}
+ backdropVariant="solid"
+ usePortal={false}
+ ariaLabelledBy="login-modal-heading"
+ belowCard={
+
+ {backToHome}
+
+ }
+ >
+ Loading…}>
+ {}} />
),
diff --git a/tests/components/AskOrganizerInquiry.test.tsx b/tests/components/AskOrganizerInquiry.test.tsx
new file mode 100644
index 0000000..f4951a8
--- /dev/null
+++ b/tests/components/AskOrganizerInquiry.test.tsx
@@ -0,0 +1,98 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
+import { screen, waitFor } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import "@testing-library/jest-dom/vitest";
+import { renderWithProviders } from "../utils/test-utils";
+import AskOrganizerInquiryModal from "../../app/components/modals/AskOrganizerInquiry";
+
+describe("AskOrganizerInquiryModal", () => {
+ const fetchMock = vi.fn();
+
+ beforeEach(() => {
+ fetchMock.mockReset();
+ vi.stubGlobal("fetch", fetchMock);
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ it("opens as a phone sheet and sends the question into the sent state", async () => {
+ const user = userEvent.setup();
+ fetchMock.mockResolvedValue({
+ ok: true,
+ json: async () => ({ ok: true }),
+ });
+ renderWithProviders(
+ ,
+ );
+
+ await waitFor(() => {
+ expect(screen.getByRole("dialog")).toHaveClass("h-full", "max-h-full");
+ });
+ expect(
+ screen.getByRole("heading", { name: "Ask an organizer" }),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: /send question/i }),
+ ).toBeInTheDocument();
+
+ await user.type(
+ screen.getByRole("textbox", { name: /email address/i }),
+ "rosa@example.org",
+ );
+ await user.type(
+ screen.getByRole("textbox", { name: /your question/i }),
+ "How do we decide who holds the keys to our shared accounts?",
+ );
+ await user.click(screen.getByRole("button", { name: /send question/i }));
+
+ expect(
+ await screen.findByRole("heading", { name: "Thanks" }),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByText(/one of us will write back within two working days/i),
+ ).toBeInTheDocument();
+ expect(
+ screen.getAllByRole("button", { name: /^close$/i }).length,
+ ).toBeGreaterThan(1);
+ });
+
+ it("keeps the question and shows the danger toast when send fails", async () => {
+ const user = userEvent.setup();
+ fetchMock.mockRejectedValue(new Error("network"));
+ renderWithProviders(
+ ,
+ );
+
+ await user.type(
+ screen.getByRole("textbox", { name: /email address/i }),
+ "rosa@example.org",
+ );
+ await user.type(
+ screen.getByRole("textbox", { name: /your question/i }),
+ "How do we decide who holds the keys to our shared accounts?",
+ );
+ await user.click(screen.getByRole("button", { name: /send question/i }));
+
+ await waitFor(() => {
+ expect(
+ screen.getAllByText("We could not send that.").length,
+ ).toBeGreaterThan(0);
+ });
+ expect(
+ screen.getAllByText(/your question is still here/i).length,
+ ).toBeGreaterThan(0);
+ expect(
+ screen.getByRole("textbox", { name: /email address/i }),
+ ).toHaveValue("rosa@example.org");
+ expect(
+ screen.getByRole("textbox", { name: /your question/i }),
+ ).toHaveValue(
+ "How do we decide who holds the keys to our shared accounts?",
+ );
+ expect(
+ screen.getByRole("button", { name: /send question/i }),
+ ).toBeInTheDocument();
+ });
+});
diff --git a/tests/components/Create.test.tsx b/tests/components/Create.test.tsx
index 9f07018..b4de667 100644
--- a/tests/components/Create.test.tsx
+++ b/tests/components/Create.test.tsx
@@ -289,12 +289,10 @@ describe("Create", () => {
,
);
- const closeButton = screen.getByLabelText("Close");
- screen.getByLabelText("Test Input"); // Verify input is rendered
+ const input = screen.getByLabelText("Test Input");
- // Focus should start on first focusable element (close button)
await waitFor(() => {
- expect(closeButton).toHaveFocus();
+ expect(input).toHaveFocus();
});
// Tab should move focus to next element
diff --git a/tests/components/Login.test.tsx b/tests/components/Login.test.tsx
index 757bf22..8f525e0 100644
--- a/tests/components/Login.test.tsx
+++ b/tests/components/Login.test.tsx
@@ -156,8 +156,8 @@ describe("Login", () => {
expect(screen.getByRole("dialog")).toBeInTheDocument();
});
expect(
- screen.getByRole("link", { name: /back to home/i }),
- ).toBeInTheDocument();
+ screen.getAllByRole("link", { name: /back to home/i }).length,
+ ).toBeGreaterThan(0);
});
it("does not render a more-options kebab", async () => {
diff --git a/tests/components/LoginForm.test.tsx b/tests/components/LoginForm.test.tsx
index 2c368d7..dc21934 100644
--- a/tests/components/LoginForm.test.tsx
+++ b/tests/components/LoginForm.test.tsx
@@ -73,7 +73,7 @@ describe("LoginForm", () => {
screen.getByRole("textbox", { name: /email address/i }),
).toHaveAttribute("maxLength", String(EMAIL_MAX_LEN));
expect(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
).toBeInTheDocument();
});
@@ -95,10 +95,10 @@ describe("LoginForm", () => {
"not-an-email",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
expect(
- await screen.findByText(/enter a valid email address/i),
+ await screen.findByText(/enter an email address like name@example.org/i),
).toBeInTheDocument();
expect(requestMagicLink).not.toHaveBeenCalled();
});
@@ -112,7 +112,7 @@ describe("LoginForm", () => {
" Pat@Example.COM ",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
await waitFor(() => {
expect(requestMagicLink).toHaveBeenCalledWith(
@@ -124,8 +124,14 @@ describe("LoginForm", () => {
expect(
await screen.findByRole("heading", { name: /check your email/i }),
).toBeInTheDocument();
- expect(screen.getByText(/we sent a login link/i)).toBeInTheDocument();
- expect(screen.getByText(/check spam or promotions/i)).toBeInTheDocument();
+ expect(
+ screen.getByText(/we sent a login link to pat@example.com/i),
+ ).toBeInTheDocument();
+ expect(screen.getByText(/it works for 60 minutes/i)).toBeInTheDocument();
+ expect(screen.getByRole("button", { name: /^close$/i })).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: /send it again/i }),
+ ).toBeInTheDocument();
});
it("submits a long email without treating length as invalid", async () => {
@@ -139,7 +145,7 @@ describe("LoginForm", () => {
email,
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
await waitFor(() => {
expect(requestMagicLink).toHaveBeenCalledWith(email, "/", undefined);
@@ -162,7 +168,7 @@ describe("LoginForm", () => {
"save@example.com",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
await waitFor(() => {
expect(requestMagicLink).toHaveBeenCalledWith(
@@ -199,7 +205,7 @@ describe("LoginForm", () => {
"guest@example.com",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
await waitFor(() => {
expect(requestMagicLink).toHaveBeenCalledWith(
@@ -237,7 +243,7 @@ describe("LoginForm", () => {
"guest@example.com",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
await waitFor(() => {
expect(requestMagicLink).toHaveBeenCalledWith(
@@ -286,7 +292,7 @@ describe("LoginForm", () => {
"a@b.co",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
await waitFor(() => {
expect(requestMagicLink).toHaveBeenCalledWith(
@@ -309,7 +315,7 @@ describe("LoginForm", () => {
"ok@example.com",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
expect(await screen.findByText("Server says no")).toBeInTheDocument();
});
@@ -327,7 +333,7 @@ describe("LoginForm", () => {
"ok@example.com",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
expect(
await screen.findByText(/try again in 4 seconds/i),
@@ -338,7 +344,13 @@ describe("LoginForm", () => {
navMock.searchParams = new URLSearchParams("error=expired_link");
renderLoginForm();
expect(
- screen.getByText(/that login link has expired/i),
+ screen.getByRole("heading", { name: /that link has expired/i }),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByText(/expired or was already used/i),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: /send a new link/i }),
).toBeInTheDocument();
});
@@ -346,7 +358,10 @@ describe("LoginForm", () => {
navMock.searchParams = new URLSearchParams("error=invalid_link");
renderLoginForm();
expect(
- screen.getByText(/that login link is not valid/i),
+ screen.getByRole("heading", { name: /that link has expired/i }),
+ ).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: /send a new link/i }),
).toBeInTheDocument();
});
@@ -383,10 +398,30 @@ describe("LoginForm", () => {
"ok@example.com",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
expect(
await screen.findByText(/check your connection/i),
).toBeInTheDocument();
});
+
+ it("send it again requests another link from the sent state", async () => {
+ const user = userEvent.setup();
+ vi.mocked(requestMagicLink).mockResolvedValue({ ok: true });
+ renderLoginForm();
+ await user.type(
+ screen.getByRole("textbox", { name: /email address/i }),
+ "pat@example.com",
+ );
+ await user.click(
+ screen.getByRole("button", { name: /send login link/i }),
+ );
+ expect(
+ await screen.findByRole("button", { name: /send it again/i }),
+ ).toBeInTheDocument();
+ await user.click(screen.getByRole("button", { name: /send it again/i }));
+ await waitFor(() => {
+ expect(requestMagicLink).toHaveBeenCalledTimes(2);
+ });
+ });
});
diff --git a/tests/contexts/AuthModalContext.test.tsx b/tests/contexts/AuthModalContext.test.tsx
index 84fbc22..bcb0f62 100644
--- a/tests/contexts/AuthModalContext.test.tsx
+++ b/tests/contexts/AuthModalContext.test.tsx
@@ -177,7 +177,7 @@ describe("AuthModalProvider (header overlay)", () => {
"guest@example.com",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
await waitFor(() => {
expect(requestMagicLink).toHaveBeenCalledWith(
@@ -211,7 +211,7 @@ describe("AuthModalProvider (header overlay)", () => {
"guest@example.com",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
await waitFor(() => {
expect(requestMagicLink).toHaveBeenCalledWith(
@@ -285,14 +285,14 @@ describe("AuthModalProvider (header overlay)", () => {
screen.getByRole("heading", { name: /log in to invite your members/i }),
).toBeInTheDocument();
expect(
- screen.getByRole("button", { name: /back to your rule/i }),
- ).toBeInTheDocument();
+ screen.getAllByRole("button", { name: /back to your rule/i }).length,
+ ).toBeGreaterThan(0);
await user.type(
screen.getByRole("textbox", { name: /email address/i }),
"guest@example.com",
);
await user.click(
- screen.getByRole("button", { name: /send me a magic link/i }),
+ screen.getByRole("button", { name: /send login link/i }),
);
await waitFor(() => {
expect(requestMagicLink).toHaveBeenCalledWith(
diff --git a/tests/unit/authMagicLinkRequestRoute.test.ts b/tests/unit/authMagicLinkRequestRoute.test.ts
index 18ee53c..3d56cf7 100644
--- a/tests/unit/authMagicLinkRequestRoute.test.ts
+++ b/tests/unit/authMagicLinkRequestRoute.test.ts
@@ -111,6 +111,7 @@ describe("POST /api/auth/magic-link/request", () => {
expect(sendMagicLinkEmailMock).toHaveBeenCalledWith(
"member@example.com",
expect.stringContaining("/api/auth/magic-link/verify?token="),
+ "https://x.test",
);
});
@@ -130,6 +131,7 @@ describe("POST /api/auth/magic-link/request", () => {
expect(sendMagicLinkEmailMock).toHaveBeenCalledWith(
email,
expect.stringContaining("/api/auth/magic-link/verify?token="),
+ "https://x.test",
);
});
diff --git a/tests/unit/magicLinkMail.test.ts b/tests/unit/magicLinkMail.test.ts
new file mode 100644
index 0000000..eeec16d
--- /dev/null
+++ b/tests/unit/magicLinkMail.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from "vitest";
+import { MAGIC_LINK_TTL_MINUTES } from "../../lib/privacyRetention";
+import { buildMagicLinkMail } from "../../lib/server/magicLinkMail";
+
+const VERIFY_URL =
+ "https://communityrule.info/api/auth/magic-link/verify?token=abc";
+
+describe("buildMagicLinkMail", () => {
+ const mail = buildMagicLinkMail({
+ verifyUrl: VERIFY_URL,
+ origin: "https://communityrule.info",
+ });
+
+ it("uses the drawn subject, once-and-minutes line, and log in button", () => {
+ expect(mail.subject).toBe("Your login link for CommunityRule");
+ expect(mail.previewText).toBe(
+ `It works once, for ${MAGIC_LINK_TTL_MINUTES} minutes.`,
+ );
+ expect(mail.text).toContain("Log in to CommunityRule");
+ expect(mail.text).toContain(VERIFY_URL);
+ expect(mail.text).toContain(
+ `It works once, for ${MAGIC_LINK_TTL_MINUTES} minutes.`,
+ );
+ expect(mail.html).toContain("background:#000000");
+ expect(mail.html).toContain(">Log in");
+ expect(mail.html).toContain(`href="${VERIFY_URL}"`);
+ expect(mail.html).toContain("/privacy");
+ expect(mail.html).toContain("/about");
+ });
+
+ it("does not invent a first name", () => {
+ expect(mail.html).not.toContain("Hi ");
+ expect(mail.text).not.toContain("Hi ");
+ });
+});
diff --git a/tests/unit/privacyRetention.test.ts b/tests/unit/privacyRetention.test.ts
index 133a75d..f53c77f 100644
--- a/tests/unit/privacyRetention.test.ts
+++ b/tests/unit/privacyRetention.test.ts
@@ -4,8 +4,8 @@ import { MAGIC_LINK_TTL_MINUTES } from "../../lib/privacyRetention";
describe("privacy retention copy", () => {
it("states the login link lifetime in the sent state", () => {
- expect(login.successBody).toContain(
- `works for ${MAGIC_LINK_TTL_MINUTES} minutes`,
- );
+ expect(
+ login.sentBody.replace("{minutes}", String(MAGIC_LINK_TTL_MINUTES)),
+ ).toContain(`works for ${MAGIC_LINK_TTL_MINUTES} minutes`);
});
});