Files
nuntius/internal/email/confirmation_test.go
petrbalvin 3a38f00dc0
Test / test (push) Successful in 2m1s
Release / gates (push) Successful in 1m57s
Release / build (amd64, freebsd) (push) Successful in 1m26s
Release / build (amd64, linux) (push) Successful in 1m30s
Release / build (arm64, freebsd) (push) Successful in 1m28s
Release / build (arm64, linux) (push) Successful in 1m49s
Release / build (loong64, linux) (push) Successful in 1m30s
Release / build (riscv64, linux) (push) Successful in 1m29s
Release / release (push) Successful in 41s
feat: contact form backend for linux and freebsd servers
Assisted-by: GLM 5.3 Flash
2026-09-29 00:32:56 +02:00

68 lines
2.3 KiB
Go

// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: MIT
//go:build linux || freebsd
package email
import (
"strings"
"testing"
"sourcedock.dev/petrbalvin/nuntius/internal/config"
)
func TestComposeConfirmation(t *testing.T) {
form := &config.Form{Name: "newsletter"}
msg := string(composeConfirmation("noreply@example.com", "jane@example.com",
"owner@example.com", form,
"https://example.com/api/news/confirm?token=abc123"))
headers := msg[:strings.Index(msg, "\r\n\r\n")]
if !strings.Contains(headers, "Subject: [nuntius/newsletter] Confirm your subscription") {
t.Errorf("subject missing in:\n%s", headers)
}
if strings.Count(headers, "Subject:") != 1 {
t.Errorf("exactly one Subject header expected:\n%s", headers)
}
if !strings.Contains(headers, "Reply-To: owner@example.com") {
t.Errorf("Reply-To should reach the owner:\n%s", headers)
}
if !strings.Contains(msg, "https://example.com/api/news/confirm?token=abc123") {
t.Error("confirmation link missing from the body")
}
if !strings.Contains(msg, "Delivered by nuntius\r\n") {
t.Error("default brand footer missing from the body")
}
}
// A configured prefix and brand replace the default nuntius wording; an
// empty prefix drops the bracket segment and an empty brand drops the
// footer line entirely.
func TestComposeConfirmationPrefixAndBrand(t *testing.T) {
form := &config.Form{
Name: "news",
SubjectPrefix: new("web"),
EmailBrand: new("Acme Mail"),
}
msg := string(composeConfirmation("noreply@example.com", "jane@example.com",
"owner@example.com", form, "https://example.com/confirm?token=abc"))
if !strings.Contains(msg, "Subject: [web/news] Confirm your subscription") {
t.Errorf("configured prefix missing:\n%s", msg)
}
if !strings.Contains(msg, "Delivered by Acme Mail\r\n") {
t.Errorf("configured brand missing:\n%s", msg)
}
form.SubjectPrefix = new("")
form.EmailBrand = new("")
msg = string(composeConfirmation("noreply@example.com", "jane@example.com",
"owner@example.com", form, "https://example.com/confirm?token=abc"))
if !strings.Contains(msg, "Subject: Confirm your subscription\r\n") {
t.Errorf("empty prefix must drop the bracket segment:\n%s", msg)
}
if strings.Contains(msg, "Delivered by") {
t.Errorf("empty brand must drop the footer:\n%s", msg)
}
}