|
| 1 | +/** |
| 2 | + * The email gate is the one place the installation's mail config decides whether a watch may |
| 3 | + * subscribe. Both variables are required: with either one missing the channel would be created |
| 4 | + * but never deliver, so the refusal has to come off the env, not off the caller. |
| 5 | + */ |
| 6 | + |
| 7 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 8 | + |
| 9 | +const mocks = vi.hoisted(() => ({ |
| 10 | + env: {} as { ALERT_FROM_EMAIL?: string; ALERT_EMAIL_TRANSPORT?: string }, |
| 11 | + canAccessDashboardAgent: vi.fn(async () => true), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("~/env.server", () => ({ env: mocks.env })); |
| 15 | +vi.mock("~/v3/canAccessDashboardAgent.server", () => ({ |
| 16 | + canAccessDashboardAgent: mocks.canAccessDashboardAgent, |
| 17 | +})); |
| 18 | +vi.mock("~/db.server", () => ({ prisma: {}, $replica: {}, sqlDatabaseSchema: undefined })); |
| 19 | +vi.mock("~/v3/alertsWorker.server", () => ({ alertsWorker: { enqueue: vi.fn() } })); |
| 20 | +vi.mock("~/v3/services/alerts/createAlertChannel.server", () => ({ |
| 21 | + CreateAlertChannelService: class {}, |
| 22 | +})); |
| 23 | +vi.mock("~/services/logger.server", () => ({ |
| 24 | + logger: { debug: vi.fn(), error: vi.fn(), warn: vi.fn(), info: vi.fn() }, |
| 25 | +})); |
| 26 | + |
| 27 | +import { canUseDashboardAgentEmailAlerts } from "~/services/dashboardAgentWatchAlerts.server"; |
| 28 | + |
| 29 | +const PARAMS = { |
| 30 | + userId: "usr_1", |
| 31 | + organizationId: "org_1", |
| 32 | + organizationSlug: "acme", |
| 33 | + orgFeatureFlags: null, |
| 34 | + projectId: "proj_1", |
| 35 | +}; |
| 36 | + |
| 37 | +describe("canUseDashboardAgentEmailAlerts", () => { |
| 38 | + beforeEach(() => { |
| 39 | + mocks.env.ALERT_FROM_EMAIL = "alerts@example.com"; |
| 40 | + mocks.env.ALERT_EMAIL_TRANSPORT = "smtp"; |
| 41 | + }); |
| 42 | + |
| 43 | + test("refuses when only the from address is missing", async () => { |
| 44 | + mocks.env.ALERT_FROM_EMAIL = undefined; |
| 45 | + |
| 46 | + await expect(canUseDashboardAgentEmailAlerts(PARAMS)).resolves.toEqual({ |
| 47 | + allowed: false, |
| 48 | + reason: "email_alerts_not_configured", |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + test("refuses when only the transport is missing", async () => { |
| 53 | + mocks.env.ALERT_EMAIL_TRANSPORT = undefined; |
| 54 | + |
| 55 | + await expect(canUseDashboardAgentEmailAlerts(PARAMS)).resolves.toEqual({ |
| 56 | + allowed: false, |
| 57 | + reason: "email_alerts_not_configured", |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + test("allows when both are configured and the base gate passes", async () => { |
| 62 | + await expect(canUseDashboardAgentEmailAlerts(PARAMS)).resolves.toEqual({ allowed: true }); |
| 63 | + }); |
| 64 | +}); |
0 commit comments