Merge pull request 'End-to-end test for newsletters' (#41) from test/e2e-newsletter into main

This commit is contained in:
squidd 2026-11-10 14:49:25 +01:00
commit bc1ea2e7c3

View file

@ -22,6 +22,7 @@ import (
"net"
"net/http"
"net/smtp"
"net/url"
"os"
"os/exec"
"path/filepath"
@ -649,6 +650,78 @@ func TestWholePath(t *testing.T) {
}
})
t.Run("a newsletter: only the publisher posts, stats count the deliveries, the web form asks for confirmation", func(t *testing.T) {
news := "news@" + domain
mustCall(t, 201, "POST", "/api/v1/lists", `{"name":"news","domain":"`+domain+`","title":"News"}`, nil)
mustCall(t, 200, "PATCH", "/api/v1/lists/2", `{"post_policy":"publishers","archive_policy":"public"}`, nil)
mustCall(t, 201, "POST", "/api/v1/lists/2/members", `{"address":"alice@members.test","display_name":"alice","publisher":true}`, nil)
for _, a := range []string{"bob", "ghost", "dave"} { // dave has no mailbox either: the refusal below bounces to nowhere
mustCall(t, 201, "POST", "/api/v1/lists/2/members", `{"address":"`+a+`@members.test","display_name":"`+a+`"}`, nil)
}
// A plain subscriber is refused by a real LMTP 550 and nothing is kept.
send(t, "dave@members.test", news, "Subscriber post", "let me in")
if !waitFor(30*time.Second, func() bool { return strings.Contains(postfixLog(), "only the publishers of this list may post") }) {
t.Fatalf("Postfix never reported the refusal:\n%s", postfixLog())
}
var held []struct{ ID int64 }
mustCall(t, 200, "GET", "/api/v1/lists/2/held", "", &held)
if len(held) != 0 {
t.Fatalf("a refused post must not be held: %+v", held)
}
// The publisher posts: the subscriber receives it, with the list headers.
send(t, "alice@members.test", news, "Issue 1", "the first issue")
got := waitMail(t, "bob", "Issue 1")
if header(got, "List-Id") == "" || header(got, "List-Unsubscribe") == "" {
t.Fatalf("newsletter mail lacks list headers:\n%s", got)
}
for _, m := range mailbox("bob") {
if header(m, "Subject") == "Subscriber post" {
t.Fatal("the refused post was delivered")
}
}
// Stats: 4 recipients; alice and bob are accepted, ghost and dave have no mailbox.
var posts []struct {
Subject string `json:"subject"`
Recipients int `json:"recipients"`
Sent int `json:"sent"`
Pending int `json:"pending"`
Failed int `json:"failed"`
Dropped int `json:"dropped"`
}
if !waitFor(30*time.Second, func() bool {
call(t, "GET", "/api/v1/lists/2/posts", "", &posts)
return len(posts) == 1 && posts[0].Pending == 0
}) {
t.Fatalf("posts: %+v", posts)
}
p := posts[0]
if p.Subject != "Issue 1" || p.Recipients != 4 || p.Sent < 2 || p.Sent+p.Failed+p.Dropped != 4 {
t.Fatalf("stats: %+v", p)
}
// The public form: a neutral page, one confirmation mail, and nothing for a bad address.
resp, err := http.PostForm(webBase+"/subscribe/"+news, url.Values{"email": {"carol@members.test"}})
if err != nil {
t.Fatal(err)
}
page, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 || !strings.Contains(string(page), "confirmation message is on its way") {
t.Fatalf("subscribe: %d\n%.400s", resp.StatusCode, page)
}
confirm := waitMail(t, "carol", "news+confirm-")
if reply := regexp.MustCompile(`news\+confirm-[0-9a-f]+@`).FindString(header(confirm, "Reply-To")); reply == "" {
t.Fatalf("no confirm address:\n%s", confirm)
}
if r, err := http.Get(webBase + "/subscribe/" + list); err != nil || r.StatusCode != 200 {
t.Fatalf("the public open list should have a form too: %v", err)
}
waitQueueEmpty(t)
})
t.Run("everything the list delivered is DKIM-signed and still verifies after Postfix", func(t *testing.T) {
checked := 0
for _, user := range []string{"alice", "bob", "carol"} {