Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(email): require an explicit Cap Cloud flag for hosted behavior

Rishabh Bothra22a860ea9c82
diff --git a/apps/web/__tests__/unit/auth-options.test.ts b/apps/web/__tests__/unit/auth-options.test.ts
index ce8ff722a..01c5a76ef 100644
--- a/apps/web/__tests__/unit/auth-options.test.ts
+++ b/apps/web/__tests__/unit/auth-options.test.ts
@@ -170,10 +170,10 @@ describe("authOptions", () => {
afterEach(() => vi.unstubAllEnvs());
- it.each([false, true])(
- "only logs undelivered codes for self-hosts (Cap Cloud: %j)",
+ it.each([undefined, "", "false", "true"])(
+ "only logs undelivered codes for self-hosts (NEXT_PUBLIC_IS_CAP: %j)",
async (isCap) => {
- mocks.isCap = isCap ? "true" : undefined;
+ mocks.isCap = isCap;
vi.stubEnv("NODE_ENV", "production");
const log = vi.spyOn(console, "log").mockImplementation(() => {});
const provider = authOptions().providers.find(
@@ -188,7 +188,7 @@ describe("authOptions", () => {
provider,
theme: {},
});
- if (isCap) {
+ if (isCap === "true") {
await expect(result).rejects.toThrow("RESEND_API_KEY");
expect(log).not.toHaveBeenCalled();
} else {
diff --git a/apps/web/__tests__/unit/email-config.test.ts b/apps/web/__tests__/unit/email-config.test.ts
index 562f28be8..cd8f6830f 100644
--- a/apps/web/__tests__/unit/email-config.test.ts
+++ b/apps/web/__tests__/unit/email-config.test.ts
@@ -40,14 +40,18 @@ describe("email config", () => {
);
});
- it("treats a missing RESEND_FROM_DOMAIN as not configured on self-hosted builds", async () => {
- mocks.env.RESEND_API_KEY = "re_test";
- expect(getEmailDeliveryStatus().configured).toBe(false);
-
- const result = await sendEmail(message);
- expect(mocks.send).not.toHaveBeenCalled();
- expect(result?.error?.name).toBe("email_not_configured");
- });
+ it.each([undefined, "", "false"])(
+ "requires a sender domain with NEXT_PUBLIC_IS_CAP=%j",
+ async (isCap) => {
+ mocks.build.NEXT_PUBLIC_IS_CAP = isCap;
+ mocks.env.RESEND_API_KEY = "re_test";
+ expect(getEmailDeliveryStatus().configured).toBe(false);
+
+ const result = await sendEmail(message);
+ expect(mocks.send).not.toHaveBeenCalled();
+ expect(result?.error?.name).toBe("email_not_configured");
+ },
+ );
it("sends an explicit sender without a default from-domain", async () => {
mocks.env.RESEND_API_KEY = "re_test";
@@ -64,16 +68,47 @@ describe("email config", () => {
expect(result).toEqual({ data: { id: "email_2" }, error: null });
});
- it("sends from the configured domain and returns the provider id", async () => {
+ it.each([undefined, "false"])(
+ "sends from the configured domain with NEXT_PUBLIC_IS_CAP=%j",
+ async (isCap) => {
+ mocks.build.NEXT_PUBLIC_IS_CAP = isCap;
+ mocks.env.RESEND_API_KEY = "re_test";
+ mocks.env.RESEND_FROM_DOMAIN = "example.com";
+ mocks.send.mockResolvedValue({ data: { id: "email_1" }, error: null });
+
+ const result = await sendEmail(message);
+ expect(mocks.send.mock.calls[0]?.[0].from).toBe("[email protected]");
+ expect(result).toEqual({ data: { id: "email_1" }, error: null });
+ },
+ );
+
+ it("uses the Cap Cloud sender when explicitly enabled", async () => {
+ mocks.build.NEXT_PUBLIC_IS_CAP = "true";
mocks.env.RESEND_API_KEY = "re_test";
- mocks.env.RESEND_FROM_DOMAIN = "example.com";
- mocks.send.mockResolvedValue({ data: { id: "email_1" }, error: null });
+ mocks.send.mockResolvedValue({ data: { id: "cloud_email" }, error: null });
- const result = await sendEmail(message);
- expect(mocks.send.mock.calls[0]?.[0].from).toBe("[email protected]");
- expect(result).toEqual({ data: { id: "email_1" }, error: null });
+ expect(getEmailDeliveryStatus().configured).toBe(true);
+ await sendEmail(message);
+
+ expect(mocks.send.mock.calls[0]?.[0].from).toBe(
+ "Cap Auth <[email protected]>",
+ );
});
+ it.each([undefined, "false"])(
+ "skips marketing on self-hosts with NEXT_PUBLIC_IS_CAP=%j",
+ async (isCap) => {
+ mocks.build.NEXT_PUBLIC_IS_CAP = isCap;
+ mocks.env.RESEND_API_KEY = "re_test";
+ mocks.env.RESEND_FROM_DOMAIN = "example.com";
+ mocks.send.mockResolvedValue({ data: { id: "marketing" }, error: null });
+
+ await sendEmail({ ...message, marketing: true });
+
+ expect(mocks.send).not.toHaveBeenCalled();
+ },
+ );
+
it("surfaces provider rejections instead of claiming success", async () => {
mocks.env.RESEND_API_KEY = "re_test";
mocks.env.RESEND_FROM_DOMAIN = "example.com";
diff --git a/apps/web/__tests__/unit/mobile-email-session.test.ts b/apps/web/__tests__/unit/mobile-email-session.test.ts
index c6855f180..9ed1bbad8 100644
--- a/apps/web/__tests__/unit/mobile-email-session.test.ts
+++ b/apps/web/__tests__/unit/mobile-email-session.test.ts
@@ -255,25 +255,29 @@ describe("mobile email sessions", () => {
const wrongCode = (code: string) => (code === "123456" ? "654321" : "123456");
- it("logs a usable code for a self-host with email disabled", async () => {
- mocks.emailStatus = {
- configured: false,
- reason: "Email is not configured",
- };
- vi.stubEnv("NODE_ENV", "production");
- const log = vi.spyOn(console, "log").mockImplementation(() => {});
- const response = await post("request", { email: "[email protected]" });
- expect(response.status).toBe(200);
- expect(mocks.sendEmail).not.toHaveBeenCalled();
- const codeLine = log.mock.calls.find(([line]) =>
- /^Code: \d{6}$/.test(line),
- );
- expect(codeLine).toBeDefined();
- const code = String(codeLine?.[0]).slice("Code: ".length);
- expect(
- (await post("verify", { email: "[email protected]", code })).status,
- ).toBe(200);
- });
+ it.each([undefined, "", "false"])(
+ "logs a usable self-host code with NEXT_PUBLIC_IS_CAP=%j and email disabled",
+ async (isCap) => {
+ mocks.isCap = isCap;
+ mocks.emailStatus = {
+ configured: false,
+ reason: "Email is not configured",
+ };
+ vi.stubEnv("NODE_ENV", "production");
+ const log = vi.spyOn(console, "log").mockImplementation(() => {});
+ const response = await post("request", { email: "[email protected]" });
+ expect(response.status).toBe(200);
+ expect(mocks.sendEmail).not.toHaveBeenCalled();
+ const codeLine = log.mock.calls.find(([line]) =>
+ /^Code: \d{6}$/.test(line),
+ );
+ expect(codeLine).toBeDefined();
+ const code = String(codeLine?.[0]).slice("Code: ".length);
+ expect(
+ (await post("verify", { email: "[email protected]", code })).status,
+ ).toBe(200);
+ },
+ );
it("fails on Cap Cloud without printing a code when email is disabled", async () => {
mocks.emailStatus = {
diff --git a/apps/web/app/api/mobile/[...route]/route.ts b/apps/web/app/api/mobile/[...route]/route.ts
index a82816b39..b1b3d2562 100644
--- a/apps/web/app/api/mobile/[...route]/route.ts
+++ b/apps/web/app/api/mobile/[...route]/route.ts
@@ -160,7 +160,10 @@ const hashEmailCode = (code: string) =>
const sendMobileEmailCode = async (email: string, code: string) => {
const emailStatus = getEmailDeliveryStatus();
if (!emailStatus.configured) {
- if (process.env.NODE_ENV === "production" && buildEnv.NEXT_PUBLIC_IS_CAP) {
+ if (
+ process.env.NODE_ENV === "production" &&
+ buildEnv.NEXT_PUBLIC_IS_CAP === "true"
+ ) {
throw new Error(emailStatus.reason);
}
console.log("");
diff --git a/packages/database/auth/auth-options.ts b/packages/database/auth/auth-options.ts
index 119d6338d..bd2637dc8 100644
--- a/packages/database/auth/auth-options.ts
+++ b/packages/database/auth/auth-options.ts
@@ -263,7 +263,7 @@ export const authOptions = (ssoContext?: SsoAuthContext): NextAuthOptions => {
if (!emailStatus.configured) {
if (
process.env.NODE_ENV === "production" &&
- buildEnv.NEXT_PUBLIC_IS_CAP
+ buildEnv.NEXT_PUBLIC_IS_CAP === "true"
) {
throw new Error(emailStatus.reason);
}
diff --git a/packages/database/emails/config.ts b/packages/database/emails/config.ts
index 87ce2ea35..e9069f0d6 100644
--- a/packages/database/emails/config.ts
+++ b/packages/database/emails/config.ts
@@ -19,7 +19,10 @@ export function getEmailDeliveryStatus(): EmailDeliveryStatus {
reason: `${EMAIL_NOT_CONFIGURED_MESSAGE}: RESEND_API_KEY is not set`,
};
}
- if (!buildEnv.NEXT_PUBLIC_IS_CAP && !env.RESEND_FROM_DOMAIN?.trim()) {
+ if (
+ buildEnv.NEXT_PUBLIC_IS_CAP !== "true" &&
+ !env.RESEND_FROM_DOMAIN?.trim()
+ ) {
return {
configured: false,
reason: `${EMAIL_NOT_CONFIGURED_MESSAGE}: RESEND_FROM_DOMAIN is not set`,
@@ -85,12 +88,13 @@ export const sendEmail = async ({
};
}
- if (marketing && !buildEnv.NEXT_PUBLIC_IS_CAP) return;
+ const isCap = buildEnv.NEXT_PUBLIC_IS_CAP === "true";
+ if (marketing && !isCap) return;
const from = fromOverride
? fromOverride
: marketing
? "Richie from Cap <[email protected]>"
- : buildEnv.NEXT_PUBLIC_IS_CAP
+ : isCap
? "Cap Auth <[email protected]>"
: env.RESEND_FROM_DOMAIN?.trim()
? `auth@${env.RESEND_FROM_DOMAIN.trim()}`