Skip to content

Build your personal knowledge base with Trilium Notes

test(login): unit-test the login and set-password pages

Elian Doran886b86da06a5
diff --git a/apps/client/src/login_page.spec.tsx b/apps/client/src/login_page.spec.tsx
new file mode 100644
index 0000000000..ec4ed902fd
--- /dev/null
+++ b/apps/client/src/login_page.spec.tsx
@@ -0,0 +1,158 @@
+import { render } from "preact";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+// t() returns the key so assertions are deterministic and not tied to English text;
+// initLocale is a no-op (no http backend under test).
+vi.mock("./services/i18n", () => ({
+ t: (key: string) => key,
+ initLocale: vi.fn(),
+ getCurrentLanguage: () => "en"
+}));
+
+import { App, PasswordLogin } from "./login_page";
+
+let container: HTMLDivElement;
+function renderInto(vnode: preact.ComponentChild) {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ render(vnode, container);
+ return container;
+}
+
+const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
+
+function mockFetch(resp: { ok?: boolean; status?: number; json?: () => Promise<unknown> } | "reject") {
+ const fn = resp === "reject"
+ ? vi.fn().mockRejectedValue(new Error("network down"))
+ : vi.fn().mockResolvedValue({ ok: false, status: 200, json: async () => ({}), ...resp });
+ globalThis.fetch = fn as typeof fetch;
+ return fn;
+}
+
+afterEach(() => {
+ render(null, container);
+ container.remove();
+ vi.restoreAllMocks();
+});
+
+describe("PasswordLogin", () => {
+ function setup(totpEnabled = false) {
+ const onError = vi.fn();
+ const c = renderInto(<PasswordLogin illustration={null} totpEnabled={totpEnabled} error={null} errorId={0} onError={onError} />);
+ const form = c.querySelector("form");
+ const password = c.querySelector('input[name="password"]') as HTMLInputElement;
+ const totp = c.querySelector('input[name="totpToken"]') as HTMLInputElement | null;
+ const submit = () => form?.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
+ return { onError, password, totp, submit };
+ }
+
+ it("posts the password read from the DOM (autofill-safe) and navigates on success", async () => {
+ const assign = vi.spyOn(window.location, "assign").mockImplementation(() => {});
+ const fetchFn = mockFetch({ ok: true, status: 200 });
+ const { password, submit } = setup();
+
+ // Simulate browser autofill: set .value WITHOUT firing an input event.
+ password.value = "autofilled";
+ submit();
+ await flush();
+
+ const body = fetchFn.mock.calls[0]?.[1]?.body as URLSearchParams;
+ expect(body.get("password")).toBe("autofilled");
+ expect(assign).toHaveBeenCalledWith(".");
+ });
+
+ it("includes the TOTP token only when TOTP is enabled", async () => {
+ vi.spyOn(window.location, "assign").mockImplementation(() => {});
+ const fetchFn = mockFetch({ ok: true, status: 200 });
+ const { password, totp, submit } = setup(true);
+ password.value = "secret";
+ if (totp) totp.value = "123456";
+ submit();
+ await flush();
+
+ const body = fetchFn.mock.calls[0]?.[1]?.body as URLSearchParams;
+ expect(body.get("totpToken")).toBe("123456");
+ });
+
+ it("reports an incorrect password on 401 {factor:'password'}", async () => {
+ mockFetch({ ok: false, status: 401, json: async () => ({ factor: "password" }) });
+ const { onError, password, submit } = setup();
+ password.value = "wrong";
+ submit();
+ await flush();
+ expect(onError).toHaveBeenCalledWith("login.incorrect-password");
+ });
+
+ it("reports a bad TOTP for a 6-digit second factor", async () => {
+ mockFetch({ ok: false, status: 401, json: async () => ({ factor: "totp" }) });
+ const { onError, password, totp, submit } = setup(true);
+ password.value = "secret";
+ if (totp) totp.value = "123456";
+ submit();
+ await flush();
+ expect(onError).toHaveBeenCalledWith("login.incorrect-totp");
+ });
+
+ it("reports a bad recovery code when the second factor has recovery-code shape", async () => {
+ mockFetch({ ok: false, status: 401, json: async () => ({ factor: "totp" }) });
+ const { onError, password, totp, submit } = setup(true);
+ password.value = "secret";
+ if (totp) totp.value = "abcdefghijklmnopqrstuv=="; // 22 chars + "=="
+ submit();
+ await flush();
+ expect(onError).toHaveBeenCalledWith("login.incorrect-recovery-code");
+ });
+
+ it("reports rate limiting on 429 rather than a credential error", async () => {
+ mockFetch({ ok: false, status: 429 });
+ const { onError, password, submit } = setup();
+ password.value = "secret";
+ submit();
+ await flush();
+ expect(onError).toHaveBeenCalledWith("login.too-many-attempts");
+ });
+
+ it("reports a connection error when fetch rejects (network failure)", async () => {
+ mockFetch("reject");
+ const { onError, password, submit } = setup();
+ password.value = "secret";
+ submit();
+ await flush();
+ expect(onError).toHaveBeenCalledWith("login.connection-error");
+ });
+
+ it("disables the submit button while a request is in flight", async () => {
+ globalThis.fetch = vi.fn().mockReturnValue(new Promise(() => {})) as typeof fetch; // never resolves
+ const c = renderInto(<PasswordLogin illustration={null} totpEnabled={false} error={null} errorId={0} onError={vi.fn()} />);
+ const button = c.querySelector('button[type="submit"]') as HTMLButtonElement;
+ const password = c.querySelector('input[name="password"]') as HTMLInputElement;
+ expect(button.disabled).toBe(false);
+
+ password.value = "secret";
+ c.querySelector("form")?.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));
+ await flush();
+ expect(button.disabled).toBe(true);
+ });
+});
+
+describe("login App — SSO branch", () => {
+ beforeEach(() => {
+ window.glob.login = { ssoEnabled: true, ssoIssuerName: "Google", ssoIssuerIcon: "", totpEnabled: false };
+ });
+ afterEach(() => {
+ window.glob.login = undefined;
+ });
+
+ it("navigates to the OpenID route when the SSO button is clicked", () => {
+ const hrefSpy = vi.spyOn(window.location, "href", "set").mockImplementation(() => {});
+ const button = renderInto(<App />).querySelector("button");
+ button?.click();
+ expect(hrefSpy).toHaveBeenCalledWith("/authenticate");
+ });
+
+ it("shows the one-shot SSO error when present", () => {
+ window.glob.login = { ssoEnabled: true, ssoIssuerName: "Google", totpEnabled: false, ssoError: "wrong_account" };
+ const c = renderInto(<App />);
+ expect(c.textContent).toContain("login.sso-wrong-account");
+ });
+});
diff --git a/apps/client/src/login_page.tsx b/apps/client/src/login_page.tsx
index bb8e4b4fde..8099b000e2 100644
--- a/apps/client/src/login_page.tsx
+++ b/apps/client/src/login_page.tsx
@@ -26,7 +26,7 @@ async function main() {
document.body.replaceChildren(bodyWrapper);
}
-function App() {
+export function App() {
const config = window.glob.login;
const illustration = <img src={logo} alt="" className="illustration-logo" />;
@@ -76,7 +76,7 @@ function App() {
);
}
-function PasswordLogin({ illustration, totpEnabled, error, errorId, onError }: {
+export function PasswordLogin({ illustration, totpEnabled, error, errorId, onError }: {
illustration: preact.ComponentChildren;
totpEnabled: boolean;
error: string | null;
@@ -196,4 +196,7 @@ function ssoErrorMessage(ssoError: string | false | undefined): string | null {
return ssoError === "wrong_account" ? t("login.sso-wrong-account") : t("login.sso-not-enrolled");
}
-main();
+// Skip the bootstrap render under test, where the components are imported directly.
+if (import.meta.env.MODE !== "test") {
+ void main();
+}
diff --git a/apps/client/src/set_password_page.spec.tsx b/apps/client/src/set_password_page.spec.tsx
new file mode 100644
index 0000000000..be48f5342c
--- /dev/null
+++ b/apps/client/src/set_password_page.spec.tsx
@@ -0,0 +1,77 @@
+import { render } from "preact";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("./services/i18n", () => ({
+ t: (key: string) => key,
+ initLocale: vi.fn(),
+ getCurrentLanguage: () => "en"
+}));
+
+import { App } from "./set_password_page";
+
+let container: HTMLDivElement;
+function renderInto(vnode: preact.ComponentChild) {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ render(vnode, container);
+ return container;
+}
+afterEach(() => { render(null, container); container.remove(); });
+
+// Controlled inputs update state asynchronously, so flush before asserting on the re-render.
+const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
+function typeInto(input: HTMLInputElement, value: string) {
+ input.value = value;
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+}
+
+describe("set-password App", () => {
+ function fields(c: HTMLElement) {
+ const [password1, password2] = Array.from(c.querySelectorAll('input[type="password"]')) as HTMLInputElement[];
+ const button = c.querySelector('button[type="submit"]') as HTMLButtonElement;
+ return { password1, password2, button };
+ }
+
+ it("enables submit only when the passwords match and meet the minimum length", async () => {
+ const c = renderInto(<App />);
+ const { password1, password2, button } = fields(c);
+
+ expect(button.disabled).toBe(true); // both empty
+
+ typeInto(password1, "abc"); // below the 4-char minimum
+ await flush();
+ expect(button.disabled).toBe(true);
+ expect(c.textContent).toContain("set_password.password-too-short");
+
+ typeInto(password1, "abcd");
+ typeInto(password2, "abce"); // mismatch
+ await flush();
+ expect(button.disabled).toBe(true);
+ expect(c.textContent).toContain("set_password.passwords-dont-match");
+
+ typeInto(password2, "abcd"); // matching + long enough
+ await flush();
+ expect(button.disabled).toBe(false);
+ });
+
+ it("submits to the set-password route via a native POST form, but only once valid", async () => {
+ const c = renderInto(<App />);
+ const form = c.querySelector("form");
+ expect(form?.getAttribute("method")?.toLowerCase()).toBe("post");
+ expect(form?.getAttribute("action")).toBe("set-password");
+
+ // Invalid → the form blocks its own submission.
+ const blocked = new Event("submit", { bubbles: true, cancelable: true });
+ form?.dispatchEvent(blocked);
+ expect(blocked.defaultPrevented).toBe(true);
+
+ // Valid → submission is allowed through to the server.
+ const { password1, password2 } = fields(c);
+ typeInto(password1, "abcd");
+ typeInto(password2, "abcd");
+ await flush();
+ const allowed = new Event("submit", { bubbles: true, cancelable: true });
+ form?.dispatchEvent(allowed);
+ expect(allowed.defaultPrevented).toBe(false);
+ });
+});
diff --git a/apps/client/src/set_password_page.tsx b/apps/client/src/set_password_page.tsx
index ca6924ef5d..9e9ada5dc1 100644
--- a/apps/client/src/set_password_page.tsx
+++ b/apps/client/src/set_password_page.tsx
@@ -27,7 +27,7 @@ async function main() {
document.body.replaceChildren(bodyWrapper);
}
-function App() {
+export function App() {
const password1Ref = useRef<HTMLInputElement>(null);
const [ password1, setPassword1 ] = useState("");
const [ password2, setPassword2 ] = useState("");
@@ -84,4 +84,7 @@ function App() {
);
}
-main();
+// Skip the bootstrap render under test, where the component is imported directly.
+if (import.meta.env.MODE !== "test") {
+ void main();
+}