Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

6.7 KB
Raw Blame
import { describe, expect, test } from "vitest";
import {
  createFormEmailDataProxy,
  getDefaultFormEmailBodyExpression,
  resolveEmailRecipientsExpression,
  resolveEmailSenderExpression,
  resolveEmailResourceSettings,
} from "./email-resource";
import { parseEmailMailboxes, parseEmailSender } from "./email-addresses";

describe("email addresses", () => {
  test("keeps named, quoted-comma, plain, and duplicate mailboxes", () => {
    expect(
      parseEmailMailboxes(
        '"Example, Alex" <[email protected]>, [email protected], [email protected]'
      )
    ).toEqual([
      { name: "Example, Alex", address: "[email protected]" },
      { address: "[email protected]" },
      { address: "[email protected]" },
    ]);
    expect(parseEmailSender("[email protected]")).toEqual({
      address: "[email protected]",
    });
    expect(parseEmailSender("Acme <[email protected]>")).toEqual({
      name: "Acme",
      address: "[email protected]",
    });
    expect(parseEmailSender("[email protected], [email protected]")).toBeUndefined();
  });

  test("rejects malformed and header-injection input", () => {
    for (const value of [
      "Bad <foo>",
      "[email protected]\r\nBcc: [email protected]",
      "[email protected]\n",
      "Group: [email protected];",
    ]) {
      expect(parseEmailMailboxes(value)).toBeUndefined();
    }
  });
});

describe("bound Email Resource addresses", () => {
  test("resolves one Reply to mailbox and a comma-separated recipient list", () => {
    expect(resolveEmailSenderExpression("Acme <[email protected]>")).toEqual({
      name: "Acme",
      address: "[email protected]",
    });
    expect(
      resolveEmailRecipientsExpression(
        "First <[email protected]>, [email protected]"
      )
    ).toEqual([
      { name: "First", address: "[email protected]" },
      { address: "[email protected]" },
    ]);
  });

  test.each([
    ["empty", ""],
    ["non-string", { address: "[email protected]" }],
    ["invalid mailbox", "invalid"],
  ])("rejects %s resolved Reply to values", (_, value) => {
    expect(() => resolveEmailSenderExpression(value)).toThrow();
  });

  test.each([
    ["empty", ""],
    ["non-string", ["[email protected]"]],
    ["invalid mailbox", "invalid"],
  ])("rejects %s resolved recipient values", (_, value) => {
    expect(() => resolveEmailRecipientsExpression(value)).toThrow();
  });
});

describe("Email Resource defaults", () => {
  test("formats submitted values as readable lines without excluded fields", () => {
    const formData = createFormEmailDataProxy(
      {
        name: "Ada",
        subject: "Question",
        interests: ["Design", "Code"],
        attachment: new File(["note"], "note.txt"),
        password: "secret",
        address: { city: "Lisbon" },
      },
      { excludeKeys: ["password"] }
    );
    expect(`Submitted fields:\n${formData}`).toBe(
      "Submitted fields:\nName: Ada\nSubject: Question\nInterests: Design, Code\nAttachment: note.txt\nAddress:\n  City: Lisbon"
    );
    expect(formData.subject).toBe("Question");
    expect(String(formData.address)).toBe('{"city":"Lisbon"}');
  });

  const projectMeta = {
    contactEmail: '"Team, West" <[email protected]>',
    emailSender: "Owner Name <[email protected]>",
  };

  test("inherits project recipients and Reply to, and applies Resource copy", () => {
    expect(
      resolveEmailResourceSettings({
        projectMeta,
        ownerEmail: "[email protected]",
      })
    ).toMatchObject({
      recipients: [{ name: "Team, West", address: "[email protected]" }],
      sender: { name: "Owner Name", address: "[email protected]" },
      fromName: "Owner Name",
      subject: '"New form submission"',
      body: '"A new form was submitted."',
    });
    const settings = {
      recipientMode: "custom" as const,
      recipients: "[email protected]",
      sender: "Custom <[email protected]>",
      subject: '"Custom subject"',
      body: '"Resource body"',
    };
    expect(
      resolveEmailResourceSettings({
        settings,
        projectMeta,
        ownerEmail: "[email protected]",
      })
    ).toMatchObject({
      recipients: [{ address: "[email protected]" }],
      sender: { name: "Custom", address: "[email protected]" },
      fromName: "Custom",
      subject: '"Custom subject"',
      body: '"Resource body"',
    });
    expect(
      resolveEmailResourceSettings({
        settings: {
          recipientMode: settings.recipientMode,
          recipients: settings.recipients,
        },
        projectMeta,
        ownerEmail: "[email protected]",
      })
    ).toMatchObject({
      sender: { name: "Owner Name", address: "[email protected]" },
      subject: '"New form submission"',
      body: '"A new form was submitted."',
    });
    expect(
      resolveEmailResourceSettings({
        settings: { sender: "" },
        projectMeta,
      }).sender
    ).toEqual({ name: "Owner Name", address: "[email protected]" });
  });

  test("falls back to owner for recipients but leaves Reply to empty", () => {
    expect(
      resolveEmailResourceSettings({
        ownerEmail: "[email protected]",
      })
    ).toMatchObject({
      recipients: [{ address: "[email protected]" }],
      sender: undefined,
      fromName: "Site Owner",
    });
    const expression = getDefaultFormEmailBodyExpression(
      "formData",
      "browserInfo"
    );
    expect(expression).toContain("Form data:");
    expect(expression).toContain("${formData}");
    expect(expression).toContain("${browserInfo}");
  });

  test("uses Site Owner when no profile name is available", () => {
    expect(resolveEmailResourceSettings({}).fromName).toBe("Site Owner");
  });

  test("visitor mode resolves one recipient and marks inherited confirmation copy", () => {
    expect(
      resolveEmailResourceSettings({
        settings: { recipientMode: "visitor", visitorEmailField: "email" },
        projectMeta: {
          contactEmail: "[email protected]",
        },
      })
    ).toMatchObject({
      recipientMode: "visitor",
      visitorEmailField: "email",
      recipients: [],
      subject: '"We received your submission"',
      body: '""',
      confirmationBody: "default",
      includeAttachments: true,
    });
    expect(
      resolveEmailResourceSettings({ settings: { recipientMode: "visitor" } })
        .confirmationSubject
    ).toBe("default");
    expect(
      resolveEmailResourceSettings({
        settings: { recipientMode: "visitor", body: '""' },
      }).confirmationBody
    ).toBeUndefined();
  });

  test("allows visitor Email Resource attachments to be explicitly disabled", () => {
    expect(
      resolveEmailResourceSettings({
        settings: {
          recipientMode: "visitor",
          visitorEmailField: "email",
          includeAttachments: false,
        },
      }).includeAttachments
    ).toBe(false);
  });
});