- Add web.listen config (default 127.0.0.1:8081, empty disables) - Start the public web in app, close it with the rest - Add end-to-end test: archive stays private, becomes public, hides addresses, one-click unsubscribe works - Cover mutation survivors: attachments, first text part, multibyte cap, member id 0, page clamp, API limit cap, closing connections - Update OpenAPI, README and example config
437 lines
13 KiB
Go
437 lines
13 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"codeberg.org/squidd/parsimony/internal/store"
|
|
)
|
|
|
|
const token = "s3cret"
|
|
|
|
func newServer(t *testing.T) http.Handler {
|
|
t.Helper()
|
|
s, err := store.Open(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { s.Close() })
|
|
return New(s, token, nil)
|
|
}
|
|
|
|
func do(t *testing.T, h http.Handler, method, path, body string, auth bool) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
req := httptest.NewRequest(method, path, strings.NewReader(body))
|
|
if auth {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
|
|
func decode(t *testing.T, rec *httptest.ResponseRecorder, v any) {
|
|
t.Helper()
|
|
if err := json.Unmarshal(rec.Body.Bytes(), v); err != nil {
|
|
t.Fatalf("decode %q: %v", rec.Body.String(), err)
|
|
}
|
|
}
|
|
|
|
func expect(t *testing.T, rec *httptest.ResponseRecorder, code int) {
|
|
t.Helper()
|
|
if rec.Code != code {
|
|
t.Fatalf("status = %d, want %d, body %s", rec.Code, code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHealthNoAuth(t *testing.T) {
|
|
h := newServer(t)
|
|
expect(t, do(t, h, "GET", "/api/v1/health", "", false), 200)
|
|
}
|
|
|
|
func TestAuthRequired(t *testing.T) {
|
|
h := newServer(t)
|
|
expect(t, do(t, h, "GET", "/api/v1/lists", "", false), 401)
|
|
req := httptest.NewRequest("GET", "/api/v1/lists", nil)
|
|
req.Header.Set("Authorization", "Bearer wrong")
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
expect(t, rec, 401)
|
|
if rec.Header().Get("WWW-Authenticate") == "" {
|
|
t.Error("missing WWW-Authenticate")
|
|
}
|
|
}
|
|
|
|
func TestEmptyTokenRejectsEverything(t *testing.T) {
|
|
s, _ := store.Open(":memory:")
|
|
defer s.Close()
|
|
h := New(s, "", nil)
|
|
req := httptest.NewRequest("GET", "/api/v1/lists", nil)
|
|
req.Header.Set("Authorization", "Bearer ")
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
expect(t, rec, 401)
|
|
}
|
|
|
|
type listJSON struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Domain string `json:"domain"`
|
|
Title string `json:"title"`
|
|
Address string `json:"address"`
|
|
PostPolicy string `json:"post_policy"`
|
|
JoinPolicy string `json:"join_policy"`
|
|
ArchivePolicy string `json:"archive_policy"`
|
|
}
|
|
|
|
func createList(t *testing.T, h http.Handler) listJSON {
|
|
t.Helper()
|
|
rec := do(t, h, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev"}`, true)
|
|
expect(t, rec, 201)
|
|
var l listJSON
|
|
decode(t, rec, &l)
|
|
return l
|
|
}
|
|
|
|
func TestListsCRUD(t *testing.T) {
|
|
h := newServer(t)
|
|
|
|
rec := do(t, h, "GET", "/api/v1/lists", "", true)
|
|
expect(t, rec, 200)
|
|
if strings.TrimSpace(rec.Body.String()) != "[]" {
|
|
t.Fatalf("empty list should be [], got %s", rec.Body.String())
|
|
}
|
|
|
|
l := createList(t, h)
|
|
if l.ID == 0 || l.Address != "dev@example.org" || l.Title != "Dev" {
|
|
t.Fatalf("created: %+v", l)
|
|
}
|
|
expect(t, do(t, h, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org"}`, true), 409)
|
|
|
|
rec = do(t, h, "GET", "/api/v1/lists", "", true)
|
|
var all []listJSON
|
|
decode(t, rec, &all)
|
|
if len(all) != 1 {
|
|
t.Fatalf("lists: %+v", all)
|
|
}
|
|
|
|
rec = do(t, h, "GET", "/api/v1/lists/1", "", true)
|
|
expect(t, rec, 200)
|
|
expect(t, do(t, h, "GET", "/api/v1/lists/99", "", true), 404)
|
|
expect(t, do(t, h, "GET", "/api/v1/lists/abc", "", true), 404)
|
|
|
|
rec = do(t, h, "PATCH", "/api/v1/lists/1", `{"title":"Renamed"}`, true)
|
|
expect(t, rec, 200)
|
|
var u listJSON
|
|
decode(t, rec, &u)
|
|
if u.Title != "Renamed" {
|
|
t.Fatalf("patched: %+v", u)
|
|
}
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/99", `{"title":"x"}`, true), 404)
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1", `{}`, true), 400)
|
|
|
|
expect(t, do(t, h, "DELETE", "/api/v1/lists/1", "", true), 204)
|
|
expect(t, do(t, h, "DELETE", "/api/v1/lists/1", "", true), 404)
|
|
}
|
|
|
|
func TestCreateListValidation(t *testing.T) {
|
|
h := newServer(t)
|
|
for _, body := range []string{
|
|
`not json`,
|
|
`{}`,
|
|
`{"name":"","domain":"example.org"}`,
|
|
`{"name":"a@b","domain":"example.org"}`,
|
|
`{"name":"a+b","domain":"example.org"}`,
|
|
`{"name":"dev","domain":""}`,
|
|
`{"name":"dev","domain":"example.org","bogus":1}`,
|
|
} {
|
|
rec := do(t, h, "POST", "/api/v1/lists", body, true)
|
|
if rec.Code != 400 {
|
|
t.Errorf("%q: status %d, want 400", body, rec.Code)
|
|
}
|
|
var e struct{ Error string }
|
|
decode(t, rec, &e)
|
|
if e.Error == "" {
|
|
t.Errorf("%q: missing error message", body)
|
|
}
|
|
}
|
|
}
|
|
|
|
type memberJSON struct {
|
|
Address string `json:"address"`
|
|
DisplayName string `json:"display_name"`
|
|
Disabled bool `json:"disabled"`
|
|
BounceCount int `json:"bounce_count"`
|
|
}
|
|
|
|
func TestMembers(t *testing.T) {
|
|
h := newServer(t)
|
|
createList(t, h)
|
|
|
|
rec := do(t, h, "GET", "/api/v1/lists/1/members", "", true)
|
|
expect(t, rec, 200)
|
|
if strings.TrimSpace(rec.Body.String()) != "[]" {
|
|
t.Fatalf("want [], got %s", rec.Body.String())
|
|
}
|
|
|
|
rec = do(t, h, "POST", "/api/v1/lists/1/members", `{"address":"Alice@Example.com","display_name":"Alice"}`, true)
|
|
expect(t, rec, 201)
|
|
var m memberJSON
|
|
decode(t, rec, &m)
|
|
if m.Address != "alice@example.com" || m.DisplayName != "Alice" {
|
|
t.Fatalf("member: %+v", m)
|
|
}
|
|
|
|
rec = do(t, h, "GET", "/api/v1/lists/1/members", "", true)
|
|
var ms []memberJSON
|
|
decode(t, rec, &ms)
|
|
if len(ms) != 1 {
|
|
t.Fatalf("members: %+v", ms)
|
|
}
|
|
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/1/members", `{"address":"not-an-address"}`, true), 400)
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/1/members", `{}`, true), 400)
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/99/members", `{"address":"a@b.com"}`, true), 404)
|
|
expect(t, do(t, h, "GET", "/api/v1/lists/99/members", "", true), 404)
|
|
|
|
expect(t, do(t, h, "DELETE", "/api/v1/lists/1/members/alice%40example.com", "", true), 204)
|
|
expect(t, do(t, h, "DELETE", "/api/v1/lists/1/members/alice%40example.com", "", true), 404)
|
|
}
|
|
|
|
func TestListPoliciesViaAPI(t *testing.T) {
|
|
h := newServer(t)
|
|
l := createList(t, h)
|
|
if l.PostPolicy != "members" || l.JoinPolicy != "open" {
|
|
t.Fatalf("defaults: %+v", l)
|
|
}
|
|
rec := do(t, h, "PATCH", "/api/v1/lists/1", `{"post_policy":"moderated"}`, true)
|
|
expect(t, rec, 200)
|
|
var u listJSON
|
|
decode(t, rec, &u)
|
|
if u.PostPolicy != "moderated" || u.JoinPolicy != "open" || u.Title != "Dev" {
|
|
t.Fatalf("patched: %+v", u)
|
|
}
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1", `{"join_policy":"closed","title":"New"}`, true), 200)
|
|
rec = do(t, h, "GET", "/api/v1/lists/1", "", true)
|
|
decode(t, rec, &u)
|
|
if u.JoinPolicy != "closed" || u.Title != "New" || u.PostPolicy != "moderated" {
|
|
t.Fatalf("after: %+v", u)
|
|
}
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1", `{"post_policy":"bogus"}`, true), 400)
|
|
}
|
|
|
|
func TestMemberBounceState(t *testing.T) {
|
|
s, _ := store.Open(":memory:")
|
|
defer s.Close()
|
|
h := New(s, token, nil)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
s.AddMember(l.ID, "a@x.com", "A")
|
|
ms, _ := s.Members(l.ID)
|
|
for i := 0; i < 3; i++ {
|
|
s.RecordHardBounce(ms[0].ID, 3)
|
|
}
|
|
rec := do(t, h, "GET", "/api/v1/lists/1/members", "", true)
|
|
var out []memberJSON
|
|
decode(t, rec, &out)
|
|
if len(out) != 1 || !out[0].Disabled || out[0].BounceCount != 3 {
|
|
t.Fatalf("members: %+v", out)
|
|
}
|
|
// Re-adding re-enables.
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/1/members", `{"address":"a@x.com"}`, true), 201)
|
|
rec = do(t, h, "GET", "/api/v1/lists/1/members", "", true)
|
|
decode(t, rec, &out)
|
|
if out[0].Disabled || out[0].BounceCount != 0 {
|
|
t.Fatalf("after re-add: %+v", out)
|
|
}
|
|
}
|
|
|
|
type fakeReleaser struct{ released []int64 }
|
|
|
|
func (f *fakeReleaser) Release(_ context.Context, id int64) error {
|
|
f.released = append(f.released, id)
|
|
return nil
|
|
}
|
|
|
|
func heldServer(t *testing.T) (http.Handler, *store.Store, *fakeReleaser) {
|
|
t.Helper()
|
|
s, _ := store.Open(":memory:")
|
|
t.Cleanup(func() { s.Close() })
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
s.HoldMessage(l.ID, "Bob@x.com", []byte("From: Bob <bob@x.com>\r\nSubject: Hello there\r\n\r\nbody"))
|
|
rel := &fakeReleaser{}
|
|
return New(s, token, rel), s, rel
|
|
}
|
|
|
|
type heldJSON struct {
|
|
ID int64 `json:"id"`
|
|
Sender string `json:"sender"`
|
|
Subject string `json:"subject"`
|
|
Size int `json:"size"`
|
|
}
|
|
|
|
func TestHeldQueueAPI(t *testing.T) {
|
|
h, _, rel := heldServer(t)
|
|
rec := do(t, h, "GET", "/api/v1/lists/1/held", "", true)
|
|
expect(t, rec, 200)
|
|
var hs []heldJSON
|
|
decode(t, rec, &hs)
|
|
if len(hs) != 1 || hs[0].Sender != "bob@x.com" || hs[0].Subject != "Hello there" || hs[0].Size == 0 {
|
|
t.Fatalf("held: %+v", hs)
|
|
}
|
|
expect(t, do(t, h, "GET", "/api/v1/lists/99/held", "", true), 404)
|
|
|
|
expect(t, do(t, h, "POST", "/api/v1/held/1/approve", "", true), 204)
|
|
if len(rel.released) != 1 || rel.released[0] != 1 {
|
|
t.Fatalf("released: %v", rel.released)
|
|
}
|
|
expect(t, do(t, h, "DELETE", "/api/v1/held/1", "", true), 204)
|
|
expect(t, do(t, h, "DELETE", "/api/v1/held/1", "", true), 404)
|
|
expect(t, do(t, h, "DELETE", "/api/v1/held/abc", "", true), 404)
|
|
}
|
|
|
|
func TestHeldRequiresAuth(t *testing.T) {
|
|
h, _, _ := heldServer(t)
|
|
expect(t, do(t, h, "POST", "/api/v1/held/1/approve", "", false), 401)
|
|
expect(t, do(t, h, "GET", "/api/v1/lists/1/held", "", false), 401)
|
|
}
|
|
|
|
func TestApproveWithoutReleaser(t *testing.T) {
|
|
s, _ := store.Open(":memory:")
|
|
defer s.Close()
|
|
h := New(s, token, nil)
|
|
expect(t, do(t, h, "POST", "/api/v1/held/1/approve", "", true), 501)
|
|
}
|
|
|
|
func TestApproveUnknownHeld(t *testing.T) {
|
|
s, _ := store.Open(":memory:")
|
|
defer s.Close()
|
|
h := New(s, token, errReleaser{store.ErrNotFound})
|
|
expect(t, do(t, h, "POST", "/api/v1/held/5/approve", "", true), 404)
|
|
}
|
|
|
|
type errReleaser struct{ err error }
|
|
|
|
func (e errReleaser) Release(context.Context, int64) error { return e.err }
|
|
|
|
func archiveServer(t *testing.T) http.Handler {
|
|
t.Helper()
|
|
s, _ := store.Open(":memory:")
|
|
t.Cleanup(func() { s.Close() })
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
for i, subj := range []string{"First post", "Second post", "Lunch"} {
|
|
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,
|
|
})
|
|
}
|
|
return New(s, token, nil)
|
|
}
|
|
|
|
type archiveJSON struct {
|
|
Total int `json:"total"`
|
|
Messages []struct {
|
|
ID int64 `json:"id"`
|
|
ThreadID int64 `json:"thread_id"`
|
|
Subject string `json:"subject"`
|
|
Sender string `json:"sender"`
|
|
Body string `json:"body"`
|
|
} `json:"messages"`
|
|
}
|
|
|
|
func TestArchivePolicyViaAPI(t *testing.T) {
|
|
h := newServer(t)
|
|
l := createList(t, h)
|
|
if l.ArchivePolicy != "private" {
|
|
t.Fatalf("default = %q", l.ArchivePolicy)
|
|
}
|
|
rec := do(t, h, "PATCH", "/api/v1/lists/1", `{"archive_policy":"public"}`, true)
|
|
expect(t, rec, 200)
|
|
var u listJSON
|
|
decode(t, rec, &u)
|
|
if u.ArchivePolicy != "public" || u.PostPolicy != "members" {
|
|
t.Fatalf("patched: %+v", u)
|
|
}
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1", `{"archive_policy":"bogus"}`, true), 400)
|
|
}
|
|
|
|
func TestArchiveList(t *testing.T) {
|
|
h := archiveServer(t)
|
|
rec := do(t, h, "GET", "/api/v1/lists/1/archive", "", true)
|
|
expect(t, rec, 200)
|
|
var out archiveJSON
|
|
decode(t, rec, &out)
|
|
if out.Total != 3 || len(out.Messages) != 3 || out.Messages[0].Subject != "Lunch" {
|
|
t.Fatalf("archive: %+v", out)
|
|
}
|
|
if out.Messages[0].Body != "" {
|
|
t.Fatal("listings must not include bodies")
|
|
}
|
|
|
|
rec = do(t, h, "GET", "/api/v1/lists/1/archive?limit=2&offset=2", "", true)
|
|
decode(t, rec, &out)
|
|
if out.Total != 3 || len(out.Messages) != 1 || out.Messages[0].Subject != "First post" {
|
|
t.Fatalf("page: %+v", out)
|
|
}
|
|
|
|
rec = do(t, h, "GET", "/api/v1/lists/1/archive?q=post", "", true)
|
|
decode(t, rec, &out)
|
|
if out.Total != 2 || len(out.Messages) != 2 {
|
|
t.Fatalf("search: %+v", out)
|
|
}
|
|
rec = do(t, h, "GET", "/api/v1/lists/1/archive?q=nothingmatches", "", true)
|
|
decode(t, rec, &out)
|
|
if out.Total != 0 || out.Messages == nil || len(out.Messages) != 0 {
|
|
t.Fatalf("empty search must be an empty array: %+v", out)
|
|
}
|
|
expect(t, do(t, h, "GET", "/api/v1/lists/1/archive?limit=abc", "", true), 400)
|
|
expect(t, do(t, h, "GET", "/api/v1/lists/1/archive?offset=-1", "", true), 400)
|
|
expect(t, do(t, h, "GET", "/api/v1/lists/99/archive", "", true), 404)
|
|
expect(t, do(t, h, "GET", "/api/v1/lists/1/archive", "", false), 401)
|
|
}
|
|
|
|
func TestArchiveLimitIsCapped(t *testing.T) {
|
|
s, _ := store.Open(":memory:")
|
|
t.Cleanup(func() { s.Close() })
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
for i := 0; i < maxPageSize+5; i++ {
|
|
s.ArchiveMessage(l.ID, store.Message{MessageID: fmt.Sprintf("<%d@x>", i), Subject: "s", Date: time.Unix(int64(i), 0)})
|
|
}
|
|
h := New(s, token, nil)
|
|
rec := do(t, h, "GET", "/api/v1/lists/1/archive?limit=100000", "", true)
|
|
expect(t, rec, 200)
|
|
var out archiveJSON
|
|
decode(t, rec, &out)
|
|
if len(out.Messages) != maxPageSize || out.Total != maxPageSize+5 {
|
|
t.Fatalf("got %d messages, total %d", len(out.Messages), out.Total)
|
|
}
|
|
rec = do(t, h, "GET", "/api/v1/lists/1/archive?limit=0", "", true)
|
|
decode(t, rec, &out)
|
|
if len(out.Messages) != 1 {
|
|
t.Fatalf("limit=0 must still return one message, got %d", len(out.Messages))
|
|
}
|
|
}
|
|
|
|
func TestArchiveGetAndDelete(t *testing.T) {
|
|
h := archiveServer(t)
|
|
rec := do(t, h, "GET", "/api/v1/archive/1", "", true)
|
|
expect(t, rec, 200)
|
|
var m struct {
|
|
Subject string `json:"subject"`
|
|
Body string `json:"body"`
|
|
}
|
|
decode(t, rec, &m)
|
|
if m.Subject != "First post" || m.Body != "body of First post" {
|
|
t.Fatalf("message: %+v", m)
|
|
}
|
|
expect(t, do(t, h, "GET", "/api/v1/archive/99", "", true), 404)
|
|
expect(t, do(t, h, "DELETE", "/api/v1/archive/1", "", true), 204)
|
|
expect(t, do(t, h, "GET", "/api/v1/archive/1", "", true), 404)
|
|
expect(t, do(t, h, "DELETE", "/api/v1/archive/1", "", true), 404)
|
|
expect(t, do(t, h, "DELETE", "/api/v1/archive/2", "", false), 401)
|
|
}
|