//go:build e2e // Package e2e runs parsimony behind a real Postfix in Docker. // // make e2e // // One Postfix container plays every MTA role: it receives list mail and hands // it to parsimony over LMTP, accepts the relayed copies, and delivers them to // local maildirs. members.test has three mailboxes (alice, bob, carol); any // other user gets a real "550 5.1.1 User unknown" from Postfix. package e2e import ( "encoding/json" "fmt" "io" "net" "net/http" "net/smtp" "os" "os/exec" "path/filepath" "regexp" "strings" "testing" "time" ) const ( token = "e2e-token" domain = "lists.example.test" ) var ( suffix = fmt.Sprint(os.Getpid()) network = "pm-e2e-net-" + suffix postfixC = "pm-e2e-postfix-" + suffix parsimonyC = "pm-e2e-parsimony-" + suffix smtpAddr string apiBase string webBase string ) func docker(args ...string) (string, error) { out, err := exec.Command("docker", args...).CombinedOutput() return strings.TrimSpace(string(out)), err } func mustDocker(args ...string) string { out, err := docker(args...) if err != nil { fmt.Fprintf(os.Stderr, "docker %s: %v\n%s\n", strings.Join(args, " "), err, out) cleanup() os.Exit(2) } return out } func freePort() int { ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { panic(err) } defer ln.Close() return ln.Addr().(*net.TCPAddr).Port } func cleanup() { docker("rm", "-f", parsimonyC, postfixC) docker("network", "rm", network) } func TestMain(m *testing.M) { root, _ := filepath.Abs("..") image := os.Getenv("E2E_PARSIMONY_IMAGE") if image == "" { image = "parsimony:e2e" mustDocker("build", "-t", image, root) } mustDocker("build", "-t", "parsimony-e2e-postfix", filepath.Join(root, "e2e", "postfix")) mustDocker("network", "create", network) apiPort, webPort := freePort(), freePort() apiBase = fmt.Sprintf("http://127.0.0.1:%d", apiPort) webBase = fmt.Sprintf("http://127.0.0.1:%d", webPort) cfg := filepath.Join(os.TempDir(), "pm-e2e-"+suffix+".yaml") os.WriteFile(cfg, []byte(fmt.Sprintf(`db: /data/parsimony.db base_url: %s lmtp: network: tcp addr: 0.0.0.0:2424 relay: addr: postfix:25 concurrency: 4 api: listen: 0.0.0.0:8080 web: listen: 0.0.0.0:8081 `, webBase)), 0o644) defer os.Remove(cfg) mustDocker("run", "-d", "--name", postfixC, "--network", network, "--network-alias", "postfix", "-p", "127.0.0.1::25", "parsimony-e2e-postfix") mustDocker("run", "-d", "--name", parsimonyC, "--network", network, "--network-alias", "parsimony", "--read-only", "--cap-drop", "ALL", "-e", "PARSIMONY_API_TOKEN="+token, "-v", cfg+":/etc/parsimony/parsimony.yaml:ro", "-p", fmt.Sprintf("127.0.0.1:%d:8080", apiPort), "-p", fmt.Sprintf("127.0.0.1:%d:8081", webPort), image) smtpAddr = mustDocker("port", postfixC, "25/tcp") smtpAddr = strings.Split(strings.Split(smtpAddr, "\n")[0], " ")[0] // "127.0.0.1:49153" if !waitFor(60*time.Second, func() bool { resp, err := http.Get(apiBase + "/api/v1/health") if err != nil { return false } resp.Body.Close() return resp.StatusCode == 200 && smtpBanner() }) { logs, _ := docker("logs", parsimonyC) plogs, _ := docker("logs", postfixC) fmt.Fprintf(os.Stderr, "stack did not start\n--- parsimony\n%s\n--- postfix\n%s\n", logs, plogs) cleanup() os.Exit(2) } code := m.Run() if code != 0 { logs, _ := docker("logs", parsimonyC) plogs, _ := docker("logs", postfixC) fmt.Fprintf(os.Stderr, "\n=== parsimony log ===\n%s\n=== postfix log ===\n%s\n", logs, plogs) } cleanup() os.Exit(code) } // smtpBanner reports whether Postfix answers with its 220 greeting. A plain TCP // connect is not enough: Docker's proxy accepts connections before Postfix listens. func smtpBanner() bool { conn, err := net.DialTimeout("tcp", smtpAddr, 2*time.Second) if err != nil { return false } defer conn.Close() conn.SetReadDeadline(time.Now().Add(2 * time.Second)) buf := make([]byte, 64) n, _ := conn.Read(buf) return strings.HasPrefix(string(buf[:n]), "220") } func waitFor(d time.Duration, cond func() bool) bool { deadline := time.Now().Add(d) for time.Now().Before(deadline) { if cond() { return true } time.Sleep(200 * time.Millisecond) } return false } // --- helpers --------------------------------------------------------------- func call(t *testing.T, method, path, body string, out any) int { t.Helper() req, _ := http.NewRequest(method, apiBase+path, strings.NewReader(body)) req.Header.Set("Authorization", "Bearer "+token) resp, err := http.DefaultClient.Do(req) if err != nil { t.Fatal(err) } defer resp.Body.Close() b, _ := io.ReadAll(resp.Body) if out != nil && len(b) > 0 { if err := json.Unmarshal(b, out); err != nil { t.Fatalf("%s %s: %v: %s", method, path, err, b) } } return resp.StatusCode } func mustCall(t *testing.T, want int, method, path, body string, out any) { t.Helper() if got := call(t, method, path, body, out); got != want { t.Fatalf("%s %s = %d, want %d", method, path, got, want) } } var msgCounter int // send submits a message to Postfix like any MTA would. func send(t *testing.T, from, to, subject, body string, headers ...string) { t.Helper() msgCounter++ msg := fmt.Sprintf("From: %s\r\nTo: %s\r\nSubject: %s\r\nMessage-ID: \r\nDate: %s\r\n%s\r\n%s\r\n", from, to, subject, suffix, msgCounter, "tests.example", time.Now().Format(time.RFC1123Z), strings.Join(headers, ""), body) if err := submit(from, to, msg); err != nil { t.Fatalf("send %q to %s: %v", subject, to, err) } } // submit speaks SMTP to Postfix with timeouts, so a stuck server fails the test instead of hanging it. func submit(from, to, msg string) error { conn, err := net.DialTimeout("tcp", smtpAddr, 5*time.Second) if err != nil { return err } conn.SetDeadline(time.Now().Add(30 * time.Second)) c, err := smtp.NewClient(conn, "127.0.0.1") if err != nil { conn.Close() return err } defer c.Close() if err := c.Mail(from); err != nil { return err } if err := c.Rcpt(to); err != nil { return err } w, err := c.Data() if err != nil { return err } if _, err := w.Write([]byte(msg)); err != nil { return err } if err := w.Close(); err != nil { return err } return c.Quit() } // mailbox returns the raw messages delivered to a members.test user. func mailbox(user string) []string { script := fmt.Sprintf(`for f in /var/mail/vhosts/members.test/%s/new/*; do [ -f "$f" ] && { echo "=====FILE====="; cat "$f"; }; done; true`, user) out, _ := docker("exec", postfixC, "sh", "-c", script) var msgs []string for _, part := range strings.Split(out, "=====FILE=====\n") { if strings.TrimSpace(part) != "" { msgs = append(msgs, part) } } return msgs } func waitMail(t *testing.T, user, contains string) string { t.Helper() var found string ok := waitFor(30*time.Second, func() bool { for _, m := range mailbox(user) { if strings.Contains(m, contains) { found = m return true } } return false }) if !ok { t.Fatalf("%s never received a message containing %q; mailbox has %d message(s)", user, contains, len(mailbox(user))) } return found } func header(msg, name string) string { re := regexp.MustCompile(`(?im)^` + regexp.QuoteMeta(name) + `:[ \t]*(.*(?:\r?\n[ \t]+.*)*)`) if m := re.FindStringSubmatch(msg); m != nil { return strings.TrimSpace(strings.ReplaceAll(strings.ReplaceAll(m[1], "\r\n", " "), "\n", " ")) } return "" } func postfixLog() string { out, _ := docker("logs", postfixC) return out } type member struct { Address string `json:"address"` Disabled bool `json:"disabled"` BounceCount int `json:"bounce_count"` } func members(t *testing.T) map[string]member { t.Helper() var ms []member mustCall(t, 200, "GET", "/api/v1/lists/1/members", "", &ms) out := map[string]member{} for _, m := range ms { out[m.Address] = m } return out } func queuePending(t *testing.T) int { t.Helper() var q struct { Pending int `json:"pending"` } mustCall(t, 200, "GET", "/api/v1/queue", "", &q) return q.Pending } func waitQueueEmpty(t *testing.T) { t.Helper() if !waitFor(30*time.Second, func() bool { return queuePending(t) == 0 }) { t.Fatalf("queue did not drain: %d pending", queuePending(t)) } } // --- the scenario ------------------------------------------------------------ // The subtests build on each other (members, bounce counts, archive), so they run in order. func TestWholePath(t *testing.T) { list := "dev@" + domain var unsubURL string t.Run("create the list and its members", func(t *testing.T) { mustCall(t, 201, "POST", "/api/v1/lists", `{"name":"dev","domain":"`+domain+`","title":"Dev"}`, nil) mustCall(t, 200, "PATCH", "/api/v1/lists/1", `{"archive_policy":"public"}`, nil) for _, a := range []string{"alice", "bob", "ghost"} { // ghost has no mailbox: Postfix will say 5.1.1 mustCall(t, 201, "POST", "/api/v1/lists/1/members", `{"address":"`+a+`@members.test","display_name":"`+a+`"}`, nil) } }) t.Run("a post reaches the members through Postfix with the right headers", func(t *testing.T) { send(t, "Alice Smith ", list, "First post", "hello everyone, write to carol@members.test") msg := waitMail(t, "bob", "First post") waitMail(t, "alice", "First post") if got := header(msg, "List-Id"); got != "Dev " { t.Errorf("List-Id = %q", got) } if got := header(msg, "From"); got != `"Alice Smith via Dev" <`+list+`>` { t.Errorf("From = %q", got) } if got := header(msg, "Reply-To"); !strings.Contains(got, "alice@members.test") { t.Errorf("Reply-To = %q", got) } if got := header(msg, "Return-Path"); !regexp.MustCompile(`^$`).MatchString(got) { t.Errorf("Return-Path must be a signed VERP address, got %q", got) } if header(msg, "Precedence") != "list" || header(msg, "List-Post") != "" { t.Errorf("list headers missing:\n%s", msg) } lu := header(msg, "List-Unsubscribe") m := regexp.MustCompile(`<(` + regexp.QuoteMeta(webBase) + `/unsubscribe/\d+-[0-9a-f]{12})>`).FindStringSubmatch(lu) if m == nil || !strings.Contains(lu, "") { t.Fatalf("List-Unsubscribe = %q", lu) } unsubURL = m[1] if header(msg, "List-Unsubscribe-Post") != "List-Unsubscribe=One-Click" { t.Error("one-click header missing") } if !strings.Contains(msg, "hello everyone") { t.Error("body missing") } // Each recipient has its own unsubscribe link. aliceMsg := waitMail(t, "alice", "First post") if header(aliceMsg, "List-Unsubscribe") == lu { t.Error("recipients must not share an unsubscribe link") } }) t.Run("the post is archived and public pages hide addresses", func(t *testing.T) { var page struct { Total int `json:"total"` } mustCall(t, 200, "GET", "/api/v1/lists/1/archive?q=first", "", &page) if page.Total != 1 { t.Fatalf("archive total = %d", page.Total) } resp, err := http.Get(webBase + "/archive/" + list + "/1") if err != nil { t.Fatal(err) } b, _ := io.ReadAll(resp.Body) resp.Body.Close() body := string(b) if resp.StatusCode != 200 || !strings.Contains(body, "First post") || !strings.Contains(body, "carol@…") || strings.Contains(body, "carol@members.test") { t.Fatalf("public page: %d\n%.600s", resp.StatusCode, body) } }) t.Run("a real 5.1.1 from Postfix counts as a hard bounce and disables the member after three", func(t *testing.T) { if !waitFor(30*time.Second, func() bool { return members(t)["ghost@members.test"].BounceCount == 1 }) { t.Fatalf("ghost: %+v\n%s", members(t)["ghost@members.test"], postfixLog()) } if m := members(t); m["alice@members.test"].BounceCount != 0 || m["bob@members.test"].BounceCount != 0 { t.Fatalf("only the unknown mailbox may bounce: %+v", m) } send(t, "alice@members.test", list, "Second post", "two") send(t, "alice@members.test", list, "Third post", "three") if !waitFor(30*time.Second, func() bool { return members(t)["ghost@members.test"].Disabled }) { t.Fatalf("ghost: %+v", members(t)["ghost@members.test"]) } waitQueueEmpty(t) waitMail(t, "bob", "Third post") // Once disabled, nobody tries ghost again: the count stays at 3. send(t, "alice@members.test", list, "Fourth post", "four") waitMail(t, "bob", "Fourth post") waitQueueEmpty(t) if g := members(t)["ghost@members.test"]; g.BounceCount != 3 || !g.Disabled { t.Fatalf("ghost: %+v", g) } }) t.Run("a non-member is refused with a real LMTP 550", func(t *testing.T) { before := len(mailbox("bob")) send(t, "mallory@elsewhere.test", list, "Spam", "buy now") if !waitFor(30*time.Second, func() bool { l := postfixLog() return strings.Contains(l, "only list members may post") && strings.Contains(l, "status=bounced") }) { t.Fatalf("Postfix never reported the refusal:\n%s", postfixLog()) } time.Sleep(time.Second) if len(mailbox("bob")) != before { t.Fatal("a refused post must not be delivered") } }) t.Run("join and confirm by email", func(t *testing.T) { send(t, "carol@members.test", "dev+join@"+domain, "join", "please") confirm := waitMail(t, "carol", "Confirm your subscription") reply := regexp.MustCompile(`dev\+confirm-[0-9a-f]+@` + regexp.QuoteMeta(domain)).FindString(header(confirm, "Reply-To")) if reply == "" { t.Fatalf("no confirm address in Reply-To:\n%s", confirm) } if _, ok := members(t)["carol@members.test"]; ok { t.Fatal("carol must not be a member before confirming") } send(t, "carol@members.test", reply, "Re: Confirm", "ok") if !waitFor(30*time.Second, func() bool { _, ok := members(t)["carol@members.test"]; return ok }) { t.Fatalf("carol never became a member\n%s", postfixLog()) } send(t, "alice@members.test", list, "Welcome carol", "hi") waitMail(t, "carol", "Welcome carol") }) t.Run("leave by email", func(t *testing.T) { send(t, "carol@members.test", "dev+leave@"+domain, "leave", "bye") confirm := waitMail(t, "carol", "Confirm your unsubscription") reply := regexp.MustCompile(`dev\+confirm-[0-9a-f]+@` + regexp.QuoteMeta(domain)).FindString(header(confirm, "Reply-To")) send(t, "carol@members.test", reply, "Re: Confirm", "yes") if !waitFor(30*time.Second, func() bool { _, ok := members(t)["carol@members.test"]; return !ok }) { t.Fatal("carol is still a member") } }) t.Run("moderation holds a stranger's post until approved", func(t *testing.T) { mustCall(t, 200, "PATCH", "/api/v1/lists/1", `{"post_policy":"moderated"}`, nil) send(t, "dave@elsewhere.test", list, "Needs approval", "pending") var held []struct { ID int64 `json:"id"` Subject string `json:"subject"` } if !waitFor(30*time.Second, func() bool { call(t, "GET", "/api/v1/lists/1/held", "", &held) return len(held) == 1 }) { t.Fatalf("held: %+v", held) } if held[0].Subject != "Needs approval" { t.Fatalf("held: %+v", held) } time.Sleep(time.Second) for _, m := range mailbox("alice") { if strings.Contains(m, "Needs approval") { t.Fatal("held mail must not be delivered") } } mustCall(t, 204, "POST", fmt.Sprintf("/api/v1/held/%d/approve", held[0].ID), "", nil) waitMail(t, "alice", "Needs approval") mustCall(t, 200, "PATCH", "/api/v1/lists/1", `{"post_policy":"members"}`, nil) }) t.Run("one-click unsubscribe from a real message", func(t *testing.T) { if _, ok := members(t)["bob@members.test"]; !ok { t.Fatal("bob should still be subscribed") } // A GET (link prefetch) must not unsubscribe. resp, err := http.Get(unsubURL) if err != nil { t.Fatal(err) } resp.Body.Close() if resp.StatusCode != 200 { t.Fatalf("confirm page: %d", resp.StatusCode) } if _, ok := members(t)["bob@members.test"]; !ok { t.Fatal("GET must not unsubscribe") } resp, err = http.Post(unsubURL, "application/x-www-form-urlencoded", strings.NewReader("List-Unsubscribe=One-Click")) if err != nil { t.Fatal(err) } resp.Body.Close() if resp.StatusCode != 200 { t.Fatalf("one-click: %d", resp.StatusCode) } // The link came from bob's copy, so bob (and only bob) is gone. ms := members(t) if _, ok := ms["bob@members.test"]; ok { t.Fatal("bob must be unsubscribed") } if _, ok := ms["alice@members.test"]; !ok { t.Fatal("alice must stay") } before := len(mailbox("bob")) send(t, "alice@members.test", list, "After bob left", "x") waitMail(t, "alice", "After bob left") waitQueueEmpty(t) if len(mailbox("bob")) != before { t.Fatal("bob must not receive mail after unsubscribing") } }) t.Run("the CLI works inside the read-only container", func(t *testing.T) { out, err := docker("exec", parsimonyC, "/parsimony", "list", "ls") if err != nil || !strings.Contains(out, list) { t.Fatalf("list ls: %v\n%s", err, out) } // Bulk import through stdin, as when migrating from Mailman. imp := exec.Command("docker", "exec", "-i", parsimonyC, "/parsimony", "member", "import", list, "-") imp.Stdin = strings.NewReader("# exported subscribers\nalice@members.test\nerin@members.test,Erin\n") b, err := imp.CombinedOutput() if err != nil || !strings.Contains(string(b), "Imported 2") { t.Fatalf("member import: %v\n%s", err, b) } out, _ = docker("exec", parsimonyC, "/parsimony", "member", "ls", list, "--emails") if !strings.Contains(out, "erin@members.test") || !strings.Contains(out, "alice@members.test") { t.Fatalf("members: %s", out) } // Failures give a non-zero exit status. if out, err := docker("exec", parsimonyC, "/parsimony", "list", "show", "ghost@"+domain); err == nil || !strings.Contains(out, "no such list") { t.Fatalf("an unknown list must fail: %v %s", err, out) } if out, err := docker("exec", parsimonyC, "/parsimony", "queue", "ls"); err != nil || !strings.Contains(out, "Pending:") { t.Fatalf("queue ls: %v %s", err, out) } }) t.Run("the queue ends empty", func(t *testing.T) { waitQueueEmpty(t) }) }