parsimony/internal/api/posts_test.go
tgiachi 93a3edc58f
All checks were successful
ci / admin (push) Successful in 37s
ci / vuln (push) Successful in 48s
ci / go (push) Successful in 3m9s
chore: move the module to git.kmalloc.org and track cmd/parsimony
- Rename the Go module from codeberg.org/squidd/parsimony to
  git.kmalloc.org/squid/parsimony in go.mod and every import.
- Fix .gitignore: the bare `parsimony` pattern also ignored the cmd/parsimony
  folder, so the main package was never committed and a clean checkout could
  not be built. Anchor it to the root (`/parsimony`) and add cmd/parsimony.
2026-11-16 09:40:48 +01:00

103 lines
3.5 KiB
Go

package api
import (
"fmt"
"strings"
"testing"
"time"
"git.kmalloc.org/squid/parsimony/internal/store"
)
type postJSON struct {
ID int64 `json:"id"`
MessageID string `json:"message_id"`
Subject string `json:"subject"`
CreatedAt time.Time `json:"created_at"`
Recipients int `json:"recipients"`
Sent int `json:"sent"`
Failed int `json:"failed"`
Dropped int `json:"dropped"`
Pending int `json:"pending"`
}
func TestListPosts(t *testing.T) {
h, s := queueServer(t)
now := time.Unix(1_700_000_000, 0)
s.Now = func() time.Time { return now }
l, _ := s.CreateList("dev", "example.org", "Dev")
rcpts := []store.NewOutbound{{From: "b@x.org", Rcpt: "a@x.com"}, {From: "b@x.org", Rcpt: "b@x.com"}, {From: "b@x.org", Rcpt: "c@x.com"}}
s.EnqueuePost(l.ID, "mid:<1@x>", []byte("Message-ID: <1@x>\r\nSubject: first\r\n\r\nsecret body"), rcpts)
now = now.Add(time.Minute)
s.EnqueuePost(l.ID, "mid:<2@x>", []byte("Message-ID: <2@x>\r\nSubject: second\r\n\r\nsecret body"), rcpts[:1])
due, _ := s.DueOutbox(10)
s.CompleteOutbox(due[0].ID)
s.FailOutbox(due[1].ID, "no")
rec := do(t, h, "GET", fmt.Sprintf("/api/v1/lists/%d/posts", l.ID), "", true)
expect(t, rec, 200)
var ps []postJSON
decode(t, rec, &ps)
if len(ps) != 2 || ps[0].Subject != "second" || ps[1].Subject != "first" {
t.Fatalf("newest first: %+v", ps)
}
p := ps[1]
if p.ID == 0 || p.MessageID != "<1@x>" || !p.CreatedAt.Equal(now.Add(-time.Minute)) || p.Recipients != 3 ||
p.Sent != 1 || p.Failed != 1 || p.Dropped != 0 || p.Pending != 1 {
t.Fatalf("post: %+v", p)
}
if strings.Contains(rec.Body.String(), "secret body") {
t.Fatal("bodies are never listed")
}
decode(t, do(t, h, "GET", fmt.Sprintf("/api/v1/lists/%d/posts?limit=1", l.ID), "", true), &ps)
if len(ps) != 1 || ps[0].Subject != "second" {
t.Fatalf("limit=1: %+v", ps)
}
}
func TestListPostsEmptyIsAnArray(t *testing.T) {
h, s := queueServer(t)
l, _ := s.CreateList("dev", "example.org", "Dev")
rec := do(t, h, "GET", fmt.Sprintf("/api/v1/lists/%d/posts", l.ID), "", true)
expect(t, rec, 200)
if strings.TrimSpace(rec.Body.String()) != "[]" {
t.Fatalf("body %q", rec.Body.String())
}
}
func TestListPostsLimit(t *testing.T) {
h, s := queueServer(t)
l, _ := s.CreateList("dev", "example.org", "Dev")
for i := 0; i < 60; i++ {
s.EnqueuePost(l.ID, fmt.Sprintf("mid:%d", i), []byte("Subject: x\r\n\r\n"), nil)
}
path := fmt.Sprintf("/api/v1/lists/%d/posts", l.ID)
var ps []postJSON
decode(t, do(t, h, "GET", path, "", true), &ps)
if len(ps) != 50 {
t.Fatalf("default 50: %d", len(ps))
}
for _, bad := range []string{"-1", "x", "1.5"} {
expect(t, do(t, h, "GET", path+"?limit="+bad, "", true), 400)
}
}
func TestListPostsUnknownListAndAuth(t *testing.T) {
h, _ := queueServer(t)
expect(t, do(t, h, "GET", "/api/v1/lists/99/posts", "", true), 404)
expect(t, do(t, h, "GET", "/api/v1/lists/abc/posts", "", true), 404)
expect(t, do(t, h, "GET", "/api/v1/lists/99/posts", "", false), 401)
}
func TestDiscardingADeliveryOfAPostCountsAsDropped(t *testing.T) {
h, s := queueServer(t)
l, _ := s.CreateList("dev", "example.org", "Dev")
s.EnqueuePost(l.ID, "mid:<1@x>", []byte("Subject: x\r\n\r\n"), []store.NewOutbound{{From: "b@x.org", Rcpt: "a@x.com"}})
due, _ := s.DueOutbox(10)
expect(t, do(t, h, "DELETE", fmt.Sprintf("/api/v1/queue/%d", due[0].ID), "", true), 204)
ps, _ := s.ListPosts(l.ID, 0)
if len(ps) != 1 || ps[0].Dropped != 1 || ps[0].Sent != 0 || ps[0].Pending != 0 {
t.Fatalf("posts: %+v", ps)
}
}