94 lines
2.8 KiB
TypeScript
94 lines
2.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
booleanEnv,
|
|
csvEnv,
|
|
optionalEnv,
|
|
requiredEnv,
|
|
trustedOrigins,
|
|
type Env,
|
|
} from "../src/env";
|
|
import { buildAuthEmail, sendEmail } from "../src/email";
|
|
import { createAuthPlugins } from "../src/plugins";
|
|
|
|
const env: Env = {
|
|
BETTER_AUTH_URL: "http://localhost:8788",
|
|
TRUSTED_ORIGINS: " http://localhost:8787, https://app.example.com ",
|
|
};
|
|
|
|
describe("auth env helpers", () => {
|
|
it("parses trusted origins from comma-separated env", () => {
|
|
expect(trustedOrigins(env)).toEqual(["http://localhost:8787", "https://app.example.com"]);
|
|
});
|
|
|
|
it("parses optional csv values", () => {
|
|
expect(csvEnv(" google, github ,, ")).toEqual(["google", "github"]);
|
|
});
|
|
|
|
it("parses boolean env values", () => {
|
|
expect(booleanEnv("true")).toBe(true);
|
|
expect(booleanEnv("1")).toBe(true);
|
|
expect(booleanEnv("false")).toBe(false);
|
|
expect(booleanEnv(undefined)).toBe(false);
|
|
});
|
|
|
|
it("throws for missing required env", () => {
|
|
expect(() => requiredEnv({}, "MAIL_FROM")).toThrow("Missing required env: MAIL_FROM");
|
|
});
|
|
|
|
it("returns optional env values", () => {
|
|
expect(optionalEnv({ MAIL_FROM: "noreply@example.com" }, "MAIL_FROM")).toBe(
|
|
"noreply@example.com",
|
|
);
|
|
expect(optionalEnv({}, "MAIL_FROM")).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("auth email adapter", () => {
|
|
it("builds a verification email", () => {
|
|
const message = buildAuthEmail({
|
|
kind: "verify-email",
|
|
to: "user@example.com",
|
|
url: "https://auth.example.com/verify",
|
|
});
|
|
expect(message.subject).toBe("Verify your email");
|
|
expect(message.to).toBe("user@example.com");
|
|
expect(message.text).toContain("https://auth.example.com/verify");
|
|
});
|
|
|
|
it("fails clearly when no mail provider is configured", async () => {
|
|
await expect(
|
|
sendEmail(
|
|
{
|
|
BETTER_AUTH_URL: "http://localhost:8788",
|
|
TRUSTED_ORIGINS: "http://localhost:8787",
|
|
},
|
|
{
|
|
to: "user@example.com",
|
|
subject: "Test",
|
|
text: "Test",
|
|
},
|
|
),
|
|
).rejects.toThrow("MAIL_PROVIDER is not configured");
|
|
});
|
|
});
|
|
|
|
describe("auth plugins", () => {
|
|
it("does not enable captcha without provider config", () => {
|
|
const plugins = createAuthPlugins({
|
|
BETTER_AUTH_URL: "http://localhost:8788",
|
|
TRUSTED_ORIGINS: "http://localhost:8787",
|
|
});
|
|
expect(plugins.map((plugin) => plugin.id)).not.toContain("captcha");
|
|
});
|
|
|
|
it("enables captcha when Cloudflare Turnstile config is present", () => {
|
|
const plugins = createAuthPlugins({
|
|
BETTER_AUTH_URL: "http://localhost:8788",
|
|
TRUSTED_ORIGINS: "http://localhost:8787",
|
|
CAPTCHA_PROVIDER: "cloudflare-turnstile",
|
|
CAPTCHA_SECRET_KEY: "secret",
|
|
});
|
|
expect(plugins.map((plugin) => plugin.id)).toContain("captcha");
|
|
});
|
|
});
|