Files
shop/tests/unit/contactMail.test.ts
T
libretech 6c41faf36a fix(contact): send the contact form through the mail service's /v1/send/message
POST /api/contact posted { to, subject, body, replyTo } to ${mailApiUrl}/send,
which the mail service (libreshop/mail src/app.py) does not have, so every
message failed with a 500. It now posts { to_email, subject, message } to
POST /v1/send/message, still to paperwork@muellerprints.de, with the same
subject and text as before. The service sets no Reply-To; the sender's
address stays in the text. A line break in the subject is replaced by a
space, so it cannot start another mail header.

mailApiUrl was read from runtimeConfig without being declared there, so
NUXT_MAIL_API_URL could not set it and the fallback http://mail:2222 was
always used. It is declared now, with that default; mp's compose.yml sets
no NUXT_MAIL_API_URL.

contactMail and mailSendUrl (server/utils/contactMail.ts) are pure and tested.

Refs libretech/mp#71
2026-10-09 02:32:44 +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);
}
});