//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 ( "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/base64" "encoding/json" "encoding/pem" "fmt" "io" "net" "net/http" "net/smtp" "net/url" "os" "os/exec" "path/filepath" "regexp" "strings" "testing" "time" "github.com/emersion/go-msgauth/dkim" ) const ( token = "e2e-token-0123456789-abcdefghij" 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 dkimPub *rsa.PublicKey // the list domain's signing key 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) // A DKIM key for the list domain, mounted read-only into the container. key, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { panic(err) } dkimPub = &key.PublicKey keyFile := filepath.Join(os.TempDir(), "pm-e2e-dkim-"+suffix+".pem") os.WriteFile(keyFile, pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}), 0o644) defer os.Remove(keyFile) 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 allow: [10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16] relay: addr: postfix:25 concurrency: 4 api: listen: 0.0.0.0:8080 web: listen: 0.0.0.0:8081 dkim: - domain: lists.example.test selector: e2e key_file: /etc/parsimony/dkim.pem `, 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", "-v", keyFile+":/etc/parsimony/dkim.pem: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 } // verifyDKIM checks the signatures of a delivered message against the test key. func verifyDKIM(t *testing.T, raw string) []*dkim.Verification { t.Helper() der, _ := x509.MarshalPKIXPublicKey(dkimPub) vs, err := dkim.VerifyWithOptions(strings.NewReader(raw), &dkim.VerifyOptions{ LookupTXT: func(name string) ([]string, error) { if name == "e2e._domainkey."+domain { return []string{"v=DKIM1; k=rsa; p=" + base64.StdEncoding.EncodeToString(der)}, nil } return nil, fmt.Errorf("no record %s", name) }, }) if err != nil { t.Fatalf("verify: %v", err) } return vs } 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("a Mailman-style mbox can be imported and browsed", func(t *testing.T) { mbox := "From MAILER-DAEMON Mon Jan 1 10:00:00 2024\n" + "From: =?UTF-8?Q?Jos=C3=A9_Garc=C3=ADa?= \nSubject: =?UTF-8?Q?Caff=C3=A8_a_las_nueve?=\nMessage-ID: \nDate: Mon, 01 Jan 2024 10:00:00 +0000\nMIME-Version: 1.0\nContent-Type: multipart/alternative; boundary=BB\n\n" + "--BB\nContent-Type: text/plain; charset=utf-8\nContent-Transfer-Encoding: quoted-printable\n\nCiao a tutti, scrivetemi a jose@old-list.example\n>From the old archive: caff=C3=A8!\n--BB\nContent-Type: text/html\n\n

html copy

\n--BB--\n\n" + "From carol@members.test Tue Jan 2 09:00:00 2024\n" + "From: Carol \nSubject: Re: Caff\xc3\xa8\nMessage-ID: \nIn-Reply-To: \nDate: Tue, 02 Jan 2024 09:00:00 +0000\n\nsee you there\n" imp := exec.Command("docker", "exec", "-i", parsimonyC, "/parsimony", "archive", "import", list, "-") imp.Stdin = strings.NewReader(mbox) out, err := imp.CombinedOutput() if err != nil || !strings.Contains(string(out), "Imported 2 messages") { t.Fatalf("archive import: %v\n%s", err, out) } // Importing again adds nothing. again := exec.Command("docker", "exec", "-i", parsimonyC, "/parsimony", "archive", "import", list, "-") again.Stdin = strings.NewReader(mbox) out, err = again.CombinedOutput() if err != nil || !strings.Contains(string(out), "Imported 0 messages") || !strings.Contains(string(out), "2 already present") { t.Fatalf("second import: %v\n%s", err, out) } var page struct { Messages []struct { ID int64 `json:"id"` ThreadID int64 `json:"thread_id"` Subject string `json:"subject"` Sender string `json:"sender"` } `json:"messages"` } mustCall(t, 200, "GET", "/api/v1/lists/1/archive?q=nueve", "", &page) if len(page.Messages) != 1 || page.Messages[0].Subject != "Caffè a las nueve" || page.Messages[0].Sender != "José García" { t.Fatalf("encoded headers must be decoded: %+v", page.Messages) } root := page.Messages[0] var msg struct { Body string `json:"body"` } mustCall(t, 200, "GET", fmt.Sprintf("/api/v1/archive/%d", root.ID), "", &msg) if !strings.Contains(msg.Body, "caffè!") || !strings.Contains(msg.Body, "\nFrom the old archive") || strings.Contains(msg.Body, "html copy") { t.Fatalf("body: %q", msg.Body) } var all struct { Messages []struct { ThreadID int64 `json:"thread_id"` Subject string `json:"subject"` } `json:"messages"` } mustCall(t, 200, "GET", "/api/v1/lists/1/archive?q=caffe", "", &all) if len(all.Messages) != 2 || all.Messages[0].ThreadID != all.Messages[1].ThreadID { t.Fatalf("the reply must be in its parent's thread: %+v", all.Messages) } // The list's archive is public: imported messages show up on the web, with addresses hidden. resp, err := http.Get(fmt.Sprintf("%s/archive/%s/%d", webBase, list, root.ID)) if err != nil { t.Fatal(err) } b, _ := io.ReadAll(resp.Body) resp.Body.Close() if resp.StatusCode != 200 || !strings.Contains(string(b), "José García") || !strings.Contains(string(b), "jose@…") || strings.Contains(string(b), "jose@old-list.example") { t.Fatalf("public page: %d\n%.700s", resp.StatusCode, b) } }) 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"} { for _, m := range mailbox(user) { vs := verifyDKIM(t, m) if len(vs) != 1 || vs[0].Err != nil || vs[0].Domain != domain { t.Fatalf("%s: message %q is not validly signed by %s: %+v", user, header(m, "Subject"), domain, vs) } if from := header(m, "From"); !strings.Contains(from, "@"+domain) { t.Fatalf("a signature for the list domain only makes sense when From is on it: %q", from) } checked++ } } if checked < 8 { t.Fatalf("expected posts, confirmations and approved mail, checked only %d", checked) } // A copy changed after delivery must fail verification. m := mailbox("bob")[0] tampered := strings.Replace(m, "Subject: ", "Subject: [edited] ", 1) if vs := verifyDKIM(t, tampered); len(vs) != 1 || vs[0].Err == nil { t.Fatalf("a modified message must not verify: %+v", vs) } }) t.Run("dkim keygen works in the read-only container", func(t *testing.T) { out, err := docker("exec", parsimonyC, "/parsimony", "dkim", "keygen", "keys.example.test", "--out", "/data/e2e-key.pem") if err != nil || !strings.Contains(out, "mail._domainkey.keys.example.test.") || !strings.Contains(out, "v=DKIM1; k=rsa; p=") { t.Fatalf("keygen: %v\n%s", err, out) } if out, err := docker("exec", parsimonyC, "/parsimony", "dkim", "keygen", "keys.example.test", "--out", "/data/e2e-key.pem"); err == nil || !strings.Contains(out, "already exists") { t.Fatalf("must not overwrite: %v %s", err, out) } if out, err := docker("exec", parsimonyC, "/parsimony", "dkim", "record", "keys.example.test", "--key", "/data/e2e-key.pem"); err != nil || !strings.Contains(out, "v=DKIM1") { t.Fatalf("record: %v %s", err, out) } }) t.Run("the queue ends empty", func(t *testing.T) { waitQueueEmpty(t) }) }