Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

1.9 KB
Raw Blame
import { afterEach, beforeEach, expect, test, vi } from "vitest";

const createTransport = vi.fn((_options: unknown) => ({}));

vi.mock("nodemailer", () => ({
  createTransport: (options: unknown) => createTransport(options),
}));

const { createTransportForProvider } = await import("./transport");

const SMTP_ENV_VARS = [
  "SMTP_USER",
  "SMTP_PWD",
  "SMTP_HOST",
  "SMTP_PORT",
  "SMTP_SECURE",
];

const savedEnv: Record<string, string | undefined> = {};

beforeEach(() => {
  createTransport.mockClear();
  for (const name of SMTP_ENV_VARS) {
    savedEnv[name] = process.env[name];
    delete process.env[name];
  }
});

afterEach(() => {
  for (const name of SMTP_ENV_VARS) {
    if (savedEnv[name] === undefined) {
      delete process.env[name];
    } else {
      process.env[name] = savedEnv[name];
    }
  }
});

test("enables auth when both SMTP_USER and SMTP_PWD are set", () => {
  process.env.SMTP_USER = "[email protected]";
  process.env.SMTP_PWD = "password";

  createTransportForProvider("smtp");

  expect(createTransport).toHaveBeenCalledWith(
    expect.objectContaining({
      auth: { user: "[email protected]", pass: "password" },
    }),
  );
});

test("does not send partial auth when only SMTP_USER is set", () => {
  process.env.SMTP_USER = "[email protected]";

  createTransportForProvider("smtp");

  expect(createTransport).toHaveBeenCalledWith(
    expect.objectContaining({ auth: undefined }),
  );
});

test("does not send partial auth when only SMTP_PWD is set", () => {
  process.env.SMTP_PWD = "password";

  createTransportForProvider("smtp");

  expect(createTransport).toHaveBeenCalledWith(
    expect.objectContaining({ auth: undefined }),
  );
});

test("leaves auth undefined when neither is set", () => {
  createTransportForProvider("smtp");

  expect(createTransport).toHaveBeenCalledWith(
    expect.objectContaining({ auth: undefined }),
  );
});