package cli import ( "bytes" "context" "encoding/json" "fmt" "net/http" "net/http/httptest" "os" "strings" "testing" "time" "codeberg.org/squidd/parsimony/internal/api" "codeberg.org/squidd/parsimony/internal/store" ) const token = "cli-token" type fakeReleaser struct{ released []int64 } func (f *fakeReleaser) Release(_ context.Context, id int64) error { f.released = append(f.released, id) return nil } type env struct { s *store.Store srv *httptest.Server rel *fakeReleaser } // newEnv serves the real API handler over HTTP: the CLI talks to the real thing. func newEnv(t *testing.T) *env { t.Helper() s, err := store.Open(":memory:") if err != nil { t.Fatal(err) } rel := &fakeReleaser{} srv := httptest.NewServer(api.New(s, token, rel)) t.Cleanup(func() { srv.Close(); s.Close() }) return &env{s: s, srv: srv, rel: rel} } // run executes the CLI like a shell would and returns exit code, stdout and stderr. func (e *env) run(t *testing.T, args ...string) (int, string, string) { t.Helper() return e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, "", args...) } func (e *env) runEnv(t *testing.T, vars map[string]string, stdin string, args ...string) (int, string, string) { t.Helper() var out, errb bytes.Buffer code := Run(args, IO{In: strings.NewReader(stdin), Out: &out, Err: &errb}, func(k string) string { return vars[k] }) return code, out.String(), errb.String() } func (e *env) ok(t *testing.T, args ...string) string { t.Helper() code, out, errs := e.run(t, args...) if code != 0 { t.Fatalf("%v: exit %d\nstdout: %s\nstderr: %s", args, code, out, errs) } return out } func (e *env) fails(t *testing.T, wantCode int, wantErr string, args ...string) { t.Helper() code, out, errs := e.run(t, args...) if code != wantCode { t.Fatalf("%v: exit %d, want %d\nstdout: %s\nstderr: %s", args, code, wantCode, out, errs) } if !strings.Contains(errs, wantErr) { t.Fatalf("%v: stderr %q lacks %q", args, errs, wantErr) } if out != "" { t.Fatalf("%v: errors must not print to stdout: %q", args, out) } } func TestHelpAndUsageErrors(t *testing.T) { e := newEnv(t) for _, args := range [][]string{{"help"}, {"-h"}, {"--help"}} { code, out, _ := e.run(t, args...) if code != 0 || !strings.Contains(out, "list") || !strings.Contains(out, "PARSIMONY_API_TOKEN") { t.Fatalf("%v: %d %q", args, code, out) } } e.fails(t, 2, "usage") e.fails(t, 2, "unknown command", "frobnicate") e.fails(t, 2, "unknown command", "list", "frobnicate") e.fails(t, 2, "usage: parsimony list add", "list", "add") e.fails(t, 2, "usage: parsimony member add", "member", "add", "dev@example.org") e.fails(t, 2, "flag provided but not defined", "list", "ls", "--nope") } func TestAuthAndConnectionProblems(t *testing.T) { e := newEnv(t) code, _, errs := e.runEnv(t, map[string]string{"PARSIMONY_URL": e.srv.URL}, "", "list", "ls") if code != 1 || !strings.Contains(errs, "PARSIMONY_API_TOKEN is not set") { t.Fatalf("no token: %d %q", code, errs) } code, _, errs = e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": "wrong", "PARSIMONY_URL": e.srv.URL}, "", "list", "ls") if code != 1 || !strings.Contains(errs, "invalid or missing token") { t.Fatalf("wrong token: %d %q", code, errs) } code, _, errs = e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": "http://127.0.0.1:1"}, "", "list", "ls") if code != 1 || !strings.Contains(errs, "cannot reach") { t.Fatalf("unreachable: %d %q", code, errs) } } func TestURLFlagBeatsEnvironmentAndTrailingSlashIsFine(t *testing.T) { e := newEnv(t) vars := map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": "http://127.0.0.1:1"} code, _, errs := e.runEnv(t, vars, "", "--url", e.srv.URL+"/", "list", "ls") if code != 0 { t.Fatalf("--url must win over PARSIMONY_URL: %d %q", code, errs) } code, _, errs = e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token}, "", "list", "ls") if code != 1 || !strings.Contains(errs, "cannot reach") { t.Fatalf("default URL is the local daemon: %d %q", code, errs) } } func TestListLifecycle(t *testing.T) { e := newEnv(t) if out := e.ok(t, "list", "ls"); !strings.Contains(out, "No lists") { t.Fatalf("empty: %q", out) } out := e.ok(t, "list", "add", "dev@example.org", "--title", "Developers") if !strings.Contains(out, "Created dev@example.org") { t.Fatalf("add: %q", out) } e.ok(t, "list", "add", "news@example.org") out = e.ok(t, "list", "ls") for _, want := range []string{"ID", "ADDRESS", "TITLE", "POST", "JOIN", "ARCHIVE", "dev@example.org", "Developers", "members", "open", "private", "news@example.org"} { if !strings.Contains(out, want) { t.Fatalf("ls lacks %q:\n%s", want, out) } } e.fails(t, 1, "already exists", "list", "add", "dev@example.org") e.fails(t, 2, "name@domain", "list", "add", "nodomain") e.fails(t, 2, "name@domain", "list", "add", "@example.org") e.fails(t, 1, "name and domain", "list", "add", "a+b@example.org") for _, ref := range []string{"dev@example.org", "DEV@Example.org", "1"} { out = e.ok(t, "list", "show", ref) if !strings.Contains(out, "dev@example.org") || !strings.Contains(out, "Developers") || !strings.Contains(out, "archive") { t.Fatalf("show %s: %q", ref, out) } } e.fails(t, 1, "no such list", "list", "show", "ghost@example.org") e.fails(t, 1, "no such list", "list", "show", "99") e.fails(t, 1, "no such list", "list", "show", "notanumber") out = e.ok(t, "list", "set", "dev@example.org", "--post", "moderated", "--archive", "public") if !strings.Contains(out, "Updated dev@example.org") { t.Fatalf("set: %q", out) } l, _ := e.s.FindList("dev@example.org") if l.PostPolicy != "moderated" || l.ArchivePolicy != "public" || l.JoinPolicy != "open" || l.Title != "Developers" { t.Fatalf("only the given fields change: %+v", l) } e.ok(t, "list", "set", "1", "--title", "Core devs", "--join", "closed") l, _ = e.s.FindList("dev@example.org") if l.Title != "Core devs" || l.JoinPolicy != "closed" || l.PostPolicy != "moderated" { t.Fatalf("%+v", l) } e.fails(t, 2, "nothing to change", "list", "set", "dev@example.org") e.fails(t, 1, "post_policy", "list", "set", "dev@example.org", "--post", "bogus") e.fails(t, 1, "no such list", "list", "set", "ghost@example.org", "--title", "x") } func TestListRemoveNeedsYes(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") e.fails(t, 2, "--yes", "list", "rm", "dev@example.org") if _, err := e.s.FindList("dev@example.org"); err != nil { t.Fatal("without --yes nothing is deleted") } out := e.ok(t, "list", "rm", "dev@example.org", "--yes") if !strings.Contains(out, "Deleted dev@example.org") { t.Fatalf("rm: %q", out) } if _, err := e.s.FindList("dev@example.org"); err == nil { t.Fatal("list must be gone") } e.fails(t, 1, "no such list", "list", "rm", "dev@example.org", "--yes") } func TestJSONOutputIsValidAndStable(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org", "--title", "Dev") var lists []map[string]any if err := json.Unmarshal([]byte(e.ok(t, "--json", "list", "ls")), &lists); err != nil || len(lists) != 1 || lists[0]["address"] != "dev@example.org" { t.Fatalf("json ls: %v %v", err, lists) } var one map[string]any if err := json.Unmarshal([]byte(e.ok(t, "--json", "list", "show", "dev@example.org")), &one); err != nil || one["title"] != "Dev" { t.Fatalf("json show: %v %v", err, one) } e.s.CreateList("empty", "example.org", "E") var ms []any if err := json.Unmarshal([]byte(e.ok(t, "--json", "member", "ls", "empty@example.org")), &ms); err != nil || ms == nil || len(ms) != 0 { t.Fatalf("an empty result is [] in JSON: %v %v", err, ms) } } func TestMembers(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") if out := e.ok(t, "member", "ls", "dev@example.org"); !strings.Contains(out, "No members") { t.Fatalf("empty: %q", out) } out := e.ok(t, "member", "add", "dev@example.org", "Alice@Example.com", "--name", "Alice") if !strings.Contains(out, "Added alice@example.com") { t.Fatalf("add: %q", out) } e.ok(t, "member", "add", "1", "bob@example.com") out = e.ok(t, "member", "ls", "dev@example.org") for _, want := range []string{"ADDRESS", "NAME", "STATUS", "alice@example.com", "Alice", "bob@example.com", "active"} { if !strings.Contains(out, want) { t.Fatalf("ls lacks %q:\n%s", want, out) } } if out := e.ok(t, "member", "ls", "dev@example.org", "--emails"); out != "alice@example.com\nbob@example.com\n" { t.Fatalf("--emails: %q", out) } e.fails(t, 1, "valid email", "member", "add", "dev@example.org", "not-an-address") e.fails(t, 1, "no such list", "member", "add", "ghost@example.org", "a@b.com") // Bounce state is visible, and adding again re-enables. l, _ := e.s.FindList("dev@example.org") ms, _ := e.s.Members(l.ID) e.s.RecordHardBounce(ms[1].ID, 3) out = e.ok(t, "member", "ls", "dev@example.org") if !strings.Contains(out, "active (1 bounce)") { t.Fatalf("a member with a few bounces is still active: %s", out) } for i := 0; i < 3; i++ { e.s.RecordHardBounce(ms[0].ID, 3) } out = e.ok(t, "member", "ls", "dev@example.org") if !strings.Contains(out, "disabled (3 bounces)") { t.Fatalf("bounce state: %s", out) } e.ok(t, "member", "add", "dev@example.org", "alice@example.com") if out = e.ok(t, "member", "ls", "dev@example.org"); strings.Contains(out, "disabled") { t.Fatalf("re-adding re-enables: %s", out) } out = e.ok(t, "member", "rm", "dev@example.org", "bob@example.com") if !strings.Contains(out, "Removed bob@example.com") { t.Fatalf("rm: %q", out) } e.fails(t, 1, "not found", "member", "rm", "dev@example.org", "bob@example.com") if ok, _ := e.s.IsMember(l.ID, "alice@example.com"); !ok { t.Fatal("alice must stay") } } func TestMemberImport(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") input := "# subscribers exported from Mailman\n\nalice@example.com, Alice Smith\nbob@example.com\n carol@example.com ,Carol \nnot-an-address\ndave@example.com,Dave,extra\n" code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, input, "member", "import", "dev@example.org", "-") if code != 1 { t.Fatalf("a failed line must make the exit code non-zero, got %d", code) } if !strings.Contains(out, "Imported 4") || !strings.Contains(out, "1 failed") { t.Fatalf("summary: %q", out) } if !strings.Contains(errs, "line 6") || !strings.Contains(errs, "not-an-address") || !strings.Contains(errs, "valid email") { t.Fatalf("the failing line is reported with its number: %q", errs) } l, _ := e.s.FindList("dev@example.org") ms, _ := e.s.Members(l.ID) names := map[string]string{} for _, m := range ms { names[m.Address] = m.DisplayName } if len(ms) != 4 || names["alice@example.com"] != "Alice Smith" || names["carol@example.com"] != "Carol" || names["dave@example.com"] != "Dave" { t.Fatalf("members: %v", names) } // Importing again is idempotent; a file works as well as stdin. path := t.TempDir() + "/subs.txt" if err := os.WriteFile(path, []byte("alice@example.com\nerin@example.com\n"), 0o600); err != nil { t.Fatal(err) } out = e.ok(t, "member", "import", "dev@example.org", path) if !strings.Contains(out, "Imported 2") || strings.Contains(out, "failed") { t.Fatalf("file import: %q", out) } e.fails(t, 1, "no such file", "member", "import", "dev@example.org", "/nonexistent/file.txt") e.fails(t, 1, "no such list", "member", "import", "ghost@example.org", path) } func TestHeldQueue(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") l, _ := e.s.FindList("dev@example.org") if out := e.ok(t, "held", "ls", "dev@example.org"); !strings.Contains(out, "Nothing is held") { t.Fatalf("empty: %q", out) } id1, _ := e.s.HoldMessage(l.ID, "bob@x.com", []byte("From: Bob \r\nSubject: First held\r\n\r\nbody")) id2, _ := e.s.HoldMessage(l.ID, "eve@x.com", []byte("Subject: Second held\r\n\r\nbody")) id3, _ := e.s.HoldMessage(l.ID, "anon@x.com", []byte("From: anon@x.com\r\n\r\nno subject here")) out := e.ok(t, "held", "ls", "dev@example.org") for _, want := range []string{"ID", "FROM", "SUBJECT", "SIZE", "bob@x.com", "First held", "eve@x.com", "Second held", "(no subject)"} { if !strings.Contains(out, want) { t.Fatalf("held ls lacks %q:\n%s", want, out) } } out = e.ok(t, "held", "approve", fmt.Sprint(id1)) if !strings.Contains(out, "Approved") || len(e.rel.released) != 1 || e.rel.released[0] != id1 { t.Fatalf("approve: %q %v", out, e.rel.released) } out = e.ok(t, "held", "discard", fmt.Sprint(id2)) if !strings.Contains(out, "Discarded") { t.Fatalf("discard: %q", out) } e.ok(t, "held", "discard", fmt.Sprint(id3)) if h, _ := e.s.ListHeld(l.ID); len(h) != 1 { // the approved one stays: the fake releaser only records it t.Fatalf("held left: %d", len(h)) } e.fails(t, 1, "not found", "held", "discard", fmt.Sprint(id2)) e.fails(t, 2, "usage: parsimony held approve", "held", "approve") e.fails(t, 2, "not a valid id", "held", "approve", "abc") } func TestArchive(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") l, _ := e.s.FindList("dev@example.org") if out := e.ok(t, "archive", "ls", "dev@example.org"); !strings.Contains(out, "archive is empty") { t.Fatalf("empty: %q", out) } for i, subj := range []string{"Deploy failed", "Lunch plans", "Deploy fixed"} { e.s.ArchiveMessage(l.ID, store.Message{MessageID: fmt.Sprintf("<%d@x>", i), Subject: subj, Sender: "Alice", Date: time.Unix(1_700_000_000+int64(i), 0), Body: "body of " + subj}) } out := e.ok(t, "archive", "ls", "dev@example.org") for _, want := range []string{"ID", "DATE", "FROM", "SUBJECT", "Deploy fixed", "Lunch plans", "Deploy failed", "3 messages"} { if !strings.Contains(out, want) { t.Fatalf("archive ls lacks %q:\n%s", want, out) } } if strings.Index(out, "Deploy fixed") > strings.Index(out, "Deploy failed") { t.Fatal("newest first") } out = e.ok(t, "archive", "ls", "dev@example.org", "--q", "deploy") if strings.Contains(out, "Lunch") || !strings.Contains(out, "2 messages") { t.Fatalf("search: %q", out) } out = e.ok(t, "archive", "ls", "dev@example.org", "--limit", "1", "--offset", "1") if !strings.Contains(out, "Lunch plans") || strings.Contains(out, "Deploy fixed") || !strings.Contains(out, "Showing 1 of 3 messages") { t.Fatalf("paging: %q", out) } if out = e.ok(t, "archive", "ls", "dev@example.org", "--q", "zzz"); !strings.Contains(out, "No messages found") { t.Fatalf("no match: %q", out) } out = e.ok(t, "archive", "show", "1") if !strings.Contains(out, "Subject: Deploy failed") || !strings.Contains(out, "body of Deploy failed") { t.Fatalf("show: %q", out) } if exact := e.ok(t, "archive", "show", "1"); exact != "Subject: Deploy failed\nFrom: Alice\nDate: 2023-11-14 22:13\n\nbody of Deploy failed\n" { t.Fatalf("show must print exactly the message, got %q", exact) } var shown map[string]any if err := json.Unmarshal([]byte(e.ok(t, "--json", "archive", "show", "1")), &shown); err != nil || shown["subject"] != "Deploy failed" { t.Fatalf("--json show must be a single JSON document: %v %v", err, shown) } e.fails(t, 1, "not found", "archive", "show", "99") out = e.ok(t, "archive", "rm", "1", "--yes") if !strings.Contains(out, "Deleted") { t.Fatalf("rm: %q", out) } e.fails(t, 2, "--yes", "archive", "rm", "2") if _, total, _ := e.s.ListMessages(l.ID, 10, 0); total != 2 { t.Fatalf("total = %d", total) } } func TestQueueListIsCapped(t *testing.T) { e := newEnv(t) var items []store.NewOutbound for i := 0; i < 105; i++ { items = append(items, store.NewOutbound{From: "b@x.org", Rcpt: fmt.Sprintf("u%d@x.com", i)}) } e.s.Enqueue([]byte("x"), items) out := e.ok(t, "queue", "ls") if !strings.Contains(out, "Pending: 105") || !strings.Contains(out, "Showing 100 of 105 deliveries") { t.Fatalf("queue ls:\n%.300s ... %s", out, out[max(0, len(out)-80):]) } } func TestURLWithTrailingSlashDoesNotProduceDoubleSlashes(t *testing.T) { e := newEnv(t) var paths []string spy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { paths = append(paths, r.URL.Path) e.srv.Config.Handler.ServeHTTP(w, r) })) defer spy.Close() vars := map[string]string{"PARSIMONY_API_TOKEN": token} if code, _, errs := e.runEnv(t, vars, "", "--url", spy.URL+"//", "list", "ls"); code != 0 { t.Fatalf("%d %s", code, errs) } if len(paths) != 1 || paths[0] != "/api/v1/lists" { t.Fatalf("request paths: %v", paths) } } func TestQueue(t *testing.T) { e := newEnv(t) if out := e.ok(t, "queue", "ls"); !strings.Contains(out, "Pending: 0") || !strings.Contains(out, "queue is empty") { t.Fatalf("empty: %q", out) } e.s.Enqueue([]byte("x"), []store.NewOutbound{{From: "b@x.org", Rcpt: "a@x.com"}, {From: "b@x.org", Rcpt: "b@x.com"}}) due, _ := e.s.DueOutbox(10) e.s.RetryOutbox(due[1].ID, time.Now().Add(time.Hour), "connection refused") out := e.ok(t, "queue", "ls") for _, want := range []string{"Pending: 2", "Retrying: 1", "ID", "RECIPIENT", "ATTEMPTS", "LAST ERROR", "a@x.com", "b@x.com", "connection refused"} { if !strings.Contains(out, want) { t.Fatalf("queue ls lacks %q:\n%s", want, out) } } if strings.Index(out, "b@x.com") > strings.Index(out, "a@x.com") { t.Fatal("failing deliveries first") } e.ok(t, "queue", "rm", fmt.Sprint(due[1].ID)) e.fails(t, 1, "not found", "queue", "rm", fmt.Sprint(due[1].ID)) if st, _ := e.s.OutboxStats(); st.Pending != 1 { t.Fatalf("pending = %d", st.Pending) } } const cliMbox = "From alice@example.com Tue Jan 2 15:04:05 2024\nFrom: Alice \nSubject: Kickoff\nMessage-ID: <1@x>\nDate: Mon, 01 Jan 2024 10:00:00 +0000\n\nlet's start\n\n" + "From bob@example.com Tue Jan 2 15:04:05 2024\nFrom: Bob \nSubject: Re: Kickoff\nMessage-ID: <2@x>\nIn-Reply-To: <1@x>\nDate: Mon, 01 Jan 2024 11:00:00 +0000\n\nagreed\n" func TestArchiveImportFromFileAndStdin(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") path := t.TempDir() + "/dev.mbox" if err := os.WriteFile(path, []byte(cliMbox), 0o600); err != nil { t.Fatal(err) } out := e.ok(t, "archive", "import", "dev@example.org", path) if !strings.Contains(out, "Imported 2 messages into dev@example.org") || strings.Contains(out, "failed") || strings.Contains(out, "already") { t.Fatalf("import: %q", out) } l, _ := e.s.FindList("dev@example.org") if _, total, _ := e.s.ListMessages(l.ID, 10, 0); total != 2 { t.Fatalf("stored %d", total) } // Again, through stdin: nothing new, and it says so. code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, cliMbox, "archive", "import", "1", "-") if code != 0 || !strings.Contains(out, "Imported 0 messages") || !strings.Contains(out, "2 already present") { t.Fatalf("second import: %d %q %q", code, out, errs) } } func TestArchiveImportReportsFailures(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") bad := cliMbox + "\nFrom x@y.z Tue Jan 2 15:04:05 2024\n\nno headers\n" code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, bad, "archive", "import", "dev@example.org", "-") if code != 1 { t.Fatalf("a failed message must give a non-zero exit status, got %d", code) } if !strings.Contains(out, "Imported 2 messages") || !strings.Contains(out, "1 failed") { t.Fatalf("summary: %q", out) } if !strings.Contains(errs, "message 3") || !strings.Contains(errs, "no headers") { t.Fatalf("the failure is reported: %q", errs) } } func TestArchiveImportErrorsAndJSON(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") e.fails(t, 1, "no such file", "archive", "import", "dev@example.org", "/nonexistent/x.mbox") e.fails(t, 1, "no such list", "archive", "import", "ghost@example.org", "/dev/null") e.fails(t, 2, "usage: parsimony archive import", "archive", "import", "dev@example.org") _, out, _ := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, cliMbox, "--json", "archive", "import", "dev@example.org", "-") var sum map[string]any if err := json.Unmarshal([]byte(out), &sum); err != nil || sum["imported"].(float64) != 2 { t.Fatalf("json: %v %q", err, out) } } func TestLongRunningUploadsAreNotCutByTheShortRequestTimeout(t *testing.T) { e := newEnv(t) e.ok(t, "list", "add", "dev@example.org") var cl *client c := &cmd{getenv: func(k string) string { return map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}[k] }} cl, err := c.client() if err != nil { t.Fatal(err) } if cl.http.Timeout == 0 { t.Fatal("ordinary requests need a timeout") } if cl.upload == nil || cl.upload.Timeout != 0 { t.Fatal("uploads of big archives must not have an overall timeout") } }