Files
nuntius/internal/email/confirmation_test.go
T

68 lines
2.3 KiB
Go
Raw Normal View History

// 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)
}
}