forked from fahrPool/fahrPool
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
import { getEmailConfig } from "./email-config";
|
|
|
|
const VARS = ["SMTP_HOST", "SMTP_PORT", "SMTP_FROM", "SMTP_USER", "SMTP_PASSWORD", "SMTP_SECURE"] as const;
|
|
let alt: Record<string, string | undefined> = {};
|
|
|
|
beforeEach(() => {
|
|
alt = Object.fromEntries(VARS.map((v) => [v, process.env[v]]));
|
|
for (const v of VARS) delete process.env[v];
|
|
});
|
|
|
|
afterEach(() => {
|
|
for (const v of VARS) {
|
|
if (alt[v] === undefined) delete process.env[v];
|
|
else process.env[v] = alt[v];
|
|
}
|
|
});
|
|
|
|
function setzePflicht() {
|
|
process.env.SMTP_HOST = "smtp.example.de";
|
|
process.env.SMTP_PORT = "587";
|
|
process.env.SMTP_FROM = "Aushilfsfahrer <noreply@example.de>";
|
|
}
|
|
|
|
describe("getEmailConfig", () => {
|
|
it("liefert die vollstaendige Konfiguration", () => {
|
|
setzePflicht();
|
|
process.env.SMTP_USER = "user";
|
|
process.env.SMTP_PASSWORD = "pw";
|
|
process.env.SMTP_SECURE = "true";
|
|
expect(getEmailConfig()).toEqual({
|
|
host: "smtp.example.de",
|
|
port: 587,
|
|
secure: true,
|
|
from: "Aushilfsfahrer <noreply@example.de>",
|
|
auth: { user: "user", pass: "pw" },
|
|
});
|
|
});
|
|
|
|
it("ist null, wenn eine Pflichtvariable fehlt", () => {
|
|
setzePflicht();
|
|
delete process.env.SMTP_FROM;
|
|
expect(getEmailConfig()).toBeNull();
|
|
});
|
|
|
|
it("ist null bei ungueltigem Port", () => {
|
|
setzePflicht();
|
|
process.env.SMTP_PORT = "abc";
|
|
expect(getEmailConfig()).toBeNull();
|
|
});
|
|
|
|
it("ohne Passwort keine Authentifizierung", () => {
|
|
setzePflicht();
|
|
process.env.SMTP_USER = "user";
|
|
expect(getEmailConfig()?.auth).toBeNull();
|
|
});
|
|
|
|
it("secure ist false, wenn SMTP_SECURE nicht 'true' ist", () => {
|
|
setzePflicht();
|
|
process.env.SMTP_SECURE = "yes";
|
|
expect(getEmailConfig()?.secure).toBe(false);
|
|
});
|
|
});
|