Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript
9.0 KB
Raw Blame
import assert from "node:assert/strict";
import { mkdtemp, readdir, rm } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { test } from "node:test";

import type { Resend } from "resend";

import {
  CAPTURE_IN_PRODUCTION_ERROR_MESSAGE,
  canSendEmail,
  createEmailService,
  createLazyEmailService,
  isCaptureEmailDriverAllowed,
  type EmailService,
} from "./email.ts";

// What the Resend client is asked to do when the service turns a request
// away for rate. Nothing here talks to Resend.

type Answer = {
  data: { id: string } | null;
  error: { message: string; name: string; statusCode: number } | null;
  headers: Record<string, string> | null;
};

const RATE_LIMITED: Answer = {
  data: null,
  error: {
    message:
      "Too many requests. Please limit the number of requests per second.",
    name: "rate_limit_exceeded",
    statusCode: 429,
  },
  headers: { "retry-after": "1" },
};

const SENT: Answer = { data: { id: "email_1" }, error: null, headers: {} };

const MESSAGE = {
  subject: "Your signed copy",
  template: "completion_notification" as const,
  textBody: "Attached.",
  to: "[email protected]",
};

/** An email service whose Resend client gives these answers, in order. */
function serviceAnswering(answers: Answer[]) {
  const waits: number[] = [];
  let requests = 0;
  const next = async () => {
    const answer = answers[Math.min(requests, answers.length - 1)];

    requests += 1;

    return answer;
  };
  const service = createEmailService({
    resendClient: {
      batch: { send: next },
      emails: { send: next },
    } as unknown as Resend,
    resolveAddresses: async () => ({
      senderAddress: "Example <[email protected]>",
      supportAddress: null,
    }),
    wait: async (milliseconds) => {
      waits.push(milliseconds);
    },
  });

  return { requests: () => requests, service, waits };
}

test("a message Resend turns away for rate is sent again after the wait it names", async () => {
  const { requests, service, waits } = serviceAnswering([
    RATE_LIMITED,
    { ...RATE_LIMITED, headers: { "retry-after": "2" } },
    SENT,
  ]);

  const delivery = await service.send(MESSAGE);

  assert.equal(delivery.messageId, "email_1");
  assert.equal(requests(), 3);
  assert.deepEqual(waits, [1000, 2000]);
});

test("a rate limit that does not clear is reported after a few tries", async () => {
  const { requests, service, waits } = serviceAnswering([RATE_LIMITED]);

  await assert.rejects(service.send(MESSAGE), /Too many requests/);
  // The first try and three more.
  assert.equal(requests(), 4);
  assert.equal(waits.length, 3);
});

test("the wait is a second when Resend names none, and is never long", async () => {
  const { service, waits } = serviceAnswering([
    { ...RATE_LIMITED, headers: null },
    { ...RATE_LIMITED, headers: { "retry-after": "3600" } },
    SENT,
  ]);

  await service.send(MESSAGE);

  assert.deepEqual(waits, [1000, 5000]);
});

test("a used-up quota is reported at once: waiting a second does not bring it back", async () => {
  const { requests, service, waits } = serviceAnswering([
    {
      data: null,
      error: {
        message: "You have exceeded your daily email sending quota.",
        name: "daily_quota_exceeded",
        statusCode: 429,
      },
      headers: { "retry-after": "1" },
    },
  ]);

  await assert.rejects(service.send(MESSAGE), /daily email sending quota/);
  assert.equal(requests(), 1);
  assert.deepEqual(waits, []);
});

test("a batch turned away for rate is sent again too", async () => {
  const { requests, service } = serviceAnswering([
    RATE_LIMITED,
    {
      data: { data: [{ id: "email_1" }] } as unknown as { id: string },
      error: null,
      headers: {},
    },
  ]);

  const deliveries = await service.sendBatch([MESSAGE]);

  assert.equal(deliveries[0]?.messageId, "email_1");
  assert.equal(requests(), 2);
});

test("a lazy email service is only built when it first sends, and only once", async () => {
  let built = 0;
  const service = createLazyEmailService(() => {
    built += 1;

    return {
      async send(message) {
        return { accepted: [message.to], messageId: "m-1", rejected: [] };
      },
      async sendBatch() {
        return [];
      },
    } satisfies EmailService;
  });

  assert.equal(built, 0);
  await service.send(MESSAGE);
  await service.sendBatch([MESSAGE]);
  assert.equal(built, 1);
});

test("a lazy email service with no provider fails the send, not its creation (issue #96)", async () => {
  const service = createLazyEmailService(() => createEmailService({ env: {} }));

  await assert.rejects(service.send(MESSAGE), /RESEND_API_KEY/);
});

test("signup knows when there is no email provider to send with (issue #95)", () => {
  assert.equal(canSendEmail({ NODE_ENV: "production" }), false);
  assert.equal(
    canSendEmail({ NODE_ENV: "production", RESEND_API_KEY: "re_test" }),
    true,
  );
  assert.equal(
    canSendEmail({ EMAIL_DRIVER: "capture", NODE_ENV: "development" }),
    true,
  );
  // A local production build on localhost may still use the capture driver.
  assert.equal(
    canSendEmail({
      APP_URL: "http://localhost:3207",
      EMAIL_DRIVER: "capture",
      NODE_ENV: "production",
    }),
    true,
  );
  // The capture driver drops mail, so a real site cannot count on it, even
  // when a Resend key is also present in the environment.
  assert.equal(
    canSendEmail({
      APP_URL: "https://sign.example.com",
      EMAIL_DRIVER: "capture",
      NODE_ENV: "production",
    }),
    false,
  );
  assert.equal(
    canSendEmail({
      APP_URL: "https://sign.example.com",
      EMAIL_DRIVER: "capture",
      NODE_ENV: "production",
      RESEND_API_KEY: "re_test",
    }),
    false,
  );
  assert.equal(canSendEmail({ EMAIL_DRIVER: "capture", VERCEL: "1" }), false);
  assert.equal(
    canSendEmail({
      EMAIL_DRIVER: "capture",
      NODE_ENV: "production",
      VERCEL: "1",
    }),
    false,
  );
});

test("the capture driver is allowed in development and local production builds, but refused in deployed production and on Vercel (issue #212)", async () => {
  assert.equal(isCaptureEmailDriverAllowed({}), true);
  assert.equal(isCaptureEmailDriverAllowed({ NODE_ENV: "development" }), true);
  assert.equal(
    isCaptureEmailDriverAllowed({
      APP_URL: "http://localhost:3207",
      NODE_ENV: "production",
    }),
    true,
  );
  assert.equal(
    isCaptureEmailDriverAllowed({
      APP_URL: "http://127.0.0.1:3207",
      NODE_ENV: "production",
    }),
    true,
  );
  assert.equal(
    isCaptureEmailDriverAllowed({
      APP_URL: "http://[::1]:3207",
      NODE_ENV: "production",
    }),
    true,
  );

  assert.equal(
    isCaptureEmailDriverAllowed({
      APP_URL: "https://sign.example.com",
      NODE_ENV: "production",
    }),
    false,
  );
  assert.equal(isCaptureEmailDriverAllowed({ NODE_ENV: "production" }), false);
  assert.equal(
    isCaptureEmailDriverAllowed({
      APP_URL: "http://sign.example.com",
      NODE_ENV: "production",
    }),
    false,
  );
  assert.equal(isCaptureEmailDriverAllowed({ VERCEL: "1" }), false);
  assert.equal(
    isCaptureEmailDriverAllowed({ NODE_ENV: "production", VERCEL: "1" }),
    false,
  );
  assert.equal(
    isCaptureEmailDriverAllowed({
      APP_URL: "http://localhost:3207",
      NODE_ENV: "production",
      VERCEL: "1",
    }),
    false,
  );

  const outboxDir = await mkdtemp(path.join(os.tmpdir(), "signudoc-capture-"));
  const resolveAddresses = async () => ({
    senderAddress: "Example <[email protected]>",
    supportAddress: null,
  });

  try {
    // Allowed in development and in a loopback production build: writes captured files.
    const devService = createEmailService({
      env: {
        EMAIL_CAPTURE_DIR: outboxDir,
        EMAIL_DRIVER: "capture",
        NODE_ENV: "development",
      },
      resolveAddresses,
    });

    await devService.send(MESSAGE);
    assert.equal((await readdir(outboxDir)).length, 1);

    const localProdService = createEmailService({
      env: {
        APP_URL: "http://localhost:3207",
        EMAIL_CAPTURE_DIR: outboxDir,
        EMAIL_DRIVER: "capture",
        NODE_ENV: "production",
      },
      resolveAddresses,
    });

    await localProdService.send(MESSAGE);
    assert.equal((await readdir(outboxDir)).length, 2);

    // Refused in deployed production and on Vercel: throws and writes nothing.
    for (const refusedEnv of [
      {
        APP_URL: "https://sign.example.com",
        EMAIL_CAPTURE_DIR: outboxDir,
        EMAIL_DRIVER: "capture",
        NODE_ENV: "production",
      },
      {
        EMAIL_CAPTURE_DIR: outboxDir,
        EMAIL_DRIVER: "capture",
        VERCEL: "1",
      },
      {
        EMAIL_CAPTURE_DIR: outboxDir,
        EMAIL_DRIVER: "capture",
        NODE_ENV: "production",
        VERCEL: "1",
      },
    ]) {
      assert.throws(
        () => createEmailService({ env: refusedEnv, resolveAddresses }),
        { message: CAPTURE_IN_PRODUCTION_ERROR_MESSAGE },
      );
    }

    assert.equal((await readdir(outboxDir)).length, 2);
  } finally {
    await rm(outboxDir, { force: true, recursive: true });
  }
});