Files
shop/tests/unit/contactMail.test.ts
T
libretech d46bc9380e
build / test (push) Successful in 10s
build / build (push) Skipped
Checkout: forward only the checkout fields, show clear errors, fix the contact form (#16)
Co-authored-by: Michael Czechowski <mail@librete.ch>
2026-10-15 10:31:57 +02:00

71 lines
2.8 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { CONTACT_RECIPIENT, contactMail, mailSendUrl } from "../../server/utils/contactMail.ts";
// components/ContactForm.vue posts these fields; it sends "Kontaktanfrage über Website" when the subject is left empty.
const form = {
name: "Erika Mustermann",
email: "erika@example.org",
subject: "Notizbuch mit Firmenlogo",
message: "Hallo,\nkönnt ihr 50 Stück liefern?"
};
const required = { ok: false, message: "Name, E-Mail und Nachricht sind erforderlich" };
test("sends to the mail service's /v1/send/message", () => {
assert.equal(mailSendUrl("http://mail:2222"), "http://mail:2222/v1/send/message");
assert.equal(mailSendUrl("http://mail:2222/"), "http://mail:2222/v1/send/message");
});
test("builds the body the mail service reads: to_email, subject and message", () => {
assert.deepEqual(contactMail(form), {
ok: true,
mail: {
to_email: "paperwork@muellerprints.de",
subject: "Kontaktanfrage: Notizbuch mit Firmenlogo",
message: [
"Name: Erika Mustermann",
"E-Mail: erika@example.org",
"Betreff: Notizbuch mit Firmenlogo",
"",
"Nachricht:",
"Hallo,\nkönnt ihr 50 Stück liefern?",
"",
"---",
"Diese Nachricht wurde über das Kontaktformular auf muellerprints.de gesendet."
].join("\n")
}
});
assert.equal(CONTACT_RECIPIENT, "paperwork@muellerprints.de");
});
test("fills in the subject when the form sends none", () => {
for (const subject of [undefined, "", " "]) {
const result = contactMail({ ...form, subject });
assert.equal(result.ok && result.mail.subject, "Kontaktanfrage: Anfrage über Website");
assert.equal(result.ok && result.mail.message.split("\n")[2], "Betreff: Kontaktanfrage über Website");
}
});
test("keeps a line break in the subject from starting another mail header", () => {
const result = contactMail({ ...form, subject: "Frage\r\nBcc: someone@example.org\nX-Spam: yes" });
assert.equal(result.ok && result.mail.subject, "Kontaktanfrage: Frage Bcc: someone@example.org X-Spam: yes");
});
test("requires a name, an e-mail address and a message", () => {
for (const field of ["name", "email", "message"]) {
assert.deepEqual(contactMail({ ...form, [field]: undefined }), required, field);
assert.deepEqual(contactMail({ ...form, [field]: " " }), required, field);
assert.deepEqual(contactMail({ ...form, [field]: ["a"] }), required, field);
}
for (const body of [undefined, null, "name=Erika", [form]]) assert.deepEqual(contactMail(body), required);
});
test("rejects an invalid e-mail address", () => {
for (const email of ["erika", "erika@example", "erika @example.org", "@example.org"]) {
assert.deepEqual(contactMail({ ...form, email }), { ok: false, message: "Ungültige E-Mail-Adresse" }, email);
}
});