- api: empty names, '{' in a name, 253/254-character domains, title trimming and the name fallback
- config: an empty query or fragment in base_url
- router: leaving works on a list closed to new members
- store: an expired token is removed when presented
842 lines
28 KiB
Go
842 lines
28 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"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}`,
|
|
// Names that break an address: separators, brackets, whitespace, control characters, dots at the ends.
|
|
`{"name":"a/b","domain":"example.org"}`,
|
|
`{"name":"a<b","domain":"example.org"}`,
|
|
`{"name":"a>b","domain":"example.org"}`,
|
|
`{"name":"a\tb","domain":"example.org"}`,
|
|
`{"name":"a b","domain":"example.org"}`,
|
|
`{"name":"a\"b","domain":"example.org"}`,
|
|
`{"name":"caffè","domain":"example.org"}`,
|
|
`{"name":".dev","domain":"example.org"}`,
|
|
`{"name":"dev.","domain":"example.org"}`,
|
|
`{"name":"a..b","domain":"example.org"}`,
|
|
`{"name":"a{b","domain":"example.org"}`,
|
|
`{"name":"","domain":"example.org","title":"Dev"}`,
|
|
`{"name":" ","domain":"example.org","title":"Dev"}`,
|
|
`{"name":"` + strings.Repeat("a", 31) + `","domain":"example.org"}`,
|
|
// Domains that are not host names.
|
|
`{"name":"dev","domain":"exa mple.org"}`,
|
|
`{"name":"dev","domain":"example..org"}`,
|
|
`{"name":"dev","domain":"-example.org"}`,
|
|
`{"name":"dev","domain":"example-.org"}`,
|
|
`{"name":"dev","domain":"example.org."}`,
|
|
`{"name":"dev","domain":"ex_ample.org"}`,
|
|
`{"name":"dev","domain":"exämple.org"}`,
|
|
`{"name":"dev","domain":"a/b.org"}`,
|
|
`{"name":"dev","domain":"` + strings.Repeat("a", 64) + `.org"}`,
|
|
// Titles that are not one clean line.
|
|
`{"name":"dev","domain":"example.org","title":"Dev\r\nBcc: x@y.com"}`,
|
|
`{"name":"dev","domain":"example.org","title":"Dev\nlist"}`,
|
|
`{"name":"dev","domain":"example.org","title":"Dev\tlist"}`,
|
|
`{"name":"dev","domain":"example.org","title":"Dev\u0000list"}`,
|
|
`{"name":"dev","domain":"example.org","title":"` + strings.Repeat("x", 201) + `"}`,
|
|
} {
|
|
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"), "the list is moderated")
|
|
rel := &fakeReleaser{}
|
|
return New(s, token, rel), s, rel
|
|
}
|
|
|
|
type heldJSON struct {
|
|
ID int64 `json:"id"`
|
|
Sender string `json:"sender"`
|
|
From string `json:"from"`
|
|
Mismatch bool `json:"mismatch"`
|
|
Subject string `json:"subject"`
|
|
Size int `json:"size"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
|
|
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 || hs[0].Reason != "the list is moderated" {
|
|
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 TestHeldListingShowsWhoReallyWrote(t *testing.T) {
|
|
s, _ := store.Open(":memory:")
|
|
defer s.Close()
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
s.HoldMessage(l.ID, "bob@x.com", []byte("From: Bob <BOB@x.com>\r\nSubject: honest\r\n\r\nb"), "")
|
|
s.HoldMessage(l.ID, "bob@x.com", []byte("From: Eve <eve@x.com>\r\nSubject: passing as Eve\r\n\r\nb"), "")
|
|
s.HoldMessage(l.ID, "bob@x.com", []byte("Subject: no From at all\r\n\r\nb"), "")
|
|
s.HoldMessage(l.ID, "bob@x.com", []byte("From: not an address\r\nSubject: garbled\r\n\r\nb"), "")
|
|
h := New(s, token, nil)
|
|
|
|
var hs []heldJSON
|
|
decode(t, do(t, h, "GET", "/api/v1/lists/1/held", "", true), &hs)
|
|
if len(hs) != 4 {
|
|
t.Fatalf("%+v", hs)
|
|
}
|
|
want := []struct {
|
|
from string
|
|
mismatch bool
|
|
}{{"Bob <BOB@x.com>", false}, {"Eve <eve@x.com>", true}, {"", true}, {"not an address", true}}
|
|
for i, w := range want {
|
|
if hs[i].From != w.from || hs[i].Mismatch != w.mismatch {
|
|
t.Errorf("held %d: from %q mismatch %v, want %q %v", i+1, hs[i].From, hs[i].Mismatch, w.from, w.mismatch)
|
|
}
|
|
if hs[i].Sender != "bob@x.com" {
|
|
t.Errorf("the envelope sender stays visible: %q", hs[i].Sender)
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func queueServer(t *testing.T) (http.Handler, *store.Store) {
|
|
t.Helper()
|
|
s, _ := store.Open(":memory:")
|
|
t.Cleanup(func() { s.Close() })
|
|
return New(s, token, nil), s
|
|
}
|
|
|
|
type queueJSON struct {
|
|
Pending int `json:"pending"`
|
|
Retrying int `json:"retrying"`
|
|
Failed int `json:"failed"`
|
|
Oldest *time.Time `json:"oldest"`
|
|
Items []struct {
|
|
ID int64 `json:"id"`
|
|
Recipient string `json:"recipient"`
|
|
Attempts int `json:"attempts"`
|
|
LastError string `json:"last_error"`
|
|
NextAttempt time.Time `json:"next_attempt"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
Failed bool `json:"failed"`
|
|
} `json:"items"`
|
|
}
|
|
|
|
func TestQueueEmpty(t *testing.T) {
|
|
h, _ := queueServer(t)
|
|
rec := do(t, h, "GET", "/api/v1/queue", "", true)
|
|
expect(t, rec, 200)
|
|
var q queueJSON
|
|
decode(t, rec, &q)
|
|
if q.Pending != 0 || q.Retrying != 0 || q.Oldest != nil || q.Items == nil || len(q.Items) != 0 {
|
|
t.Fatalf("empty queue: %+v (items must be [] not null)", q)
|
|
}
|
|
}
|
|
|
|
func TestQueueShowsPendingAndFailingDeliveries(t *testing.T) {
|
|
h, s := queueServer(t)
|
|
s.Enqueue([]byte("Subject: x\r\n\r\nx"), []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"},
|
|
})
|
|
due, _ := s.DueOutbox(10)
|
|
s.RetryOutbox(due[1].ID, time.Now().Add(time.Hour), "connection refused")
|
|
|
|
rec := do(t, h, "GET", "/api/v1/queue", "", true)
|
|
expect(t, rec, 200)
|
|
var q queueJSON
|
|
decode(t, rec, &q)
|
|
if q.Pending != 3 || q.Retrying != 1 || q.Oldest == nil || len(q.Items) != 3 {
|
|
t.Fatalf("queue: %+v", q)
|
|
}
|
|
if q.Items[0].Recipient != "b@x.com" || q.Items[0].Attempts != 1 || q.Items[0].LastError != "connection refused" {
|
|
t.Fatalf("failing deliveries come first: %+v", q.Items[0])
|
|
}
|
|
if strings.Contains(rec.Body.String(), "Subject") {
|
|
t.Fatal("message contents must not be exposed")
|
|
}
|
|
}
|
|
|
|
func TestQueueListIsCapped(t *testing.T) {
|
|
h, s := queueServer(t)
|
|
var items []store.NewOutbound
|
|
for i := 0; i < 120; i++ {
|
|
items = append(items, store.NewOutbound{From: "b@x.org", Rcpt: fmt.Sprintf("u%d@x.com", i)})
|
|
}
|
|
s.Enqueue([]byte("x"), items)
|
|
var q queueJSON
|
|
decode(t, do(t, h, "GET", "/api/v1/queue", "", true), &q)
|
|
if q.Pending != 120 || len(q.Items) != 100 {
|
|
t.Fatalf("pending=%d items=%d", q.Pending, len(q.Items))
|
|
}
|
|
}
|
|
|
|
func TestQueueDiscard(t *testing.T) {
|
|
h, s := queueServer(t)
|
|
s.Enqueue([]byte("x"), []store.NewOutbound{{From: "b@x.org", Rcpt: "a@x.com"}, {From: "b@x.org", Rcpt: "b@x.com"}})
|
|
due, _ := s.DueOutbox(10)
|
|
expect(t, do(t, h, "DELETE", fmt.Sprintf("/api/v1/queue/%d", due[0].ID), "", true), 204)
|
|
expect(t, do(t, h, "DELETE", fmt.Sprintf("/api/v1/queue/%d", due[0].ID), "", true), 404)
|
|
expect(t, do(t, h, "DELETE", "/api/v1/queue/abc", "", true), 404)
|
|
if st, _ := s.OutboxStats(); st.Pending != 1 {
|
|
t.Fatalf("pending = %d", st.Pending)
|
|
}
|
|
}
|
|
|
|
func TestQueueRequiresAuth(t *testing.T) {
|
|
h, _ := queueServer(t)
|
|
expect(t, do(t, h, "GET", "/api/v1/queue", "", false), 401)
|
|
expect(t, do(t, h, "DELETE", "/api/v1/queue/1", "", false), 401)
|
|
}
|
|
|
|
const importMbox = "From alice@example.com Tue Jan 2 15:04:05 2024\nFrom: Alice <alice@example.com>\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 <bob@example.com>\nSubject: Re: Kickoff\nMessage-ID: <2@x>\nIn-Reply-To: <1@x>\nDate: Mon, 01 Jan 2024 11:00:00 +0000\n\nagreed\n"
|
|
|
|
type importJSON struct {
|
|
Imported int `json:"imported"`
|
|
Duplicates int `json:"duplicates"`
|
|
Failed int `json:"failed"`
|
|
Errors []string `json:"errors"`
|
|
}
|
|
|
|
func TestArchiveImport(t *testing.T) {
|
|
h, s := queueServer(t)
|
|
s.CreateList("dev", "example.org", "Dev")
|
|
|
|
rec := do(t, h, "POST", "/api/v1/lists/1/archive/import", importMbox, true)
|
|
expect(t, rec, 200)
|
|
var sum importJSON
|
|
decode(t, rec, &sum)
|
|
if sum.Imported != 2 || sum.Duplicates != 0 || sum.Failed != 0 || sum.Errors == nil || len(sum.Errors) != 0 {
|
|
t.Fatalf("summary: %+v (errors must be [] not null)", sum)
|
|
}
|
|
var page archiveJSON
|
|
decode(t, do(t, h, "GET", "/api/v1/lists/1/archive", "", true), &page)
|
|
if page.Total != 2 || page.Messages[0].Subject != "Re: Kickoff" {
|
|
t.Fatalf("archive: %+v", page)
|
|
}
|
|
if page.Messages[0].ThreadID != page.Messages[1].ThreadID {
|
|
t.Fatal("imported replies must be threaded")
|
|
}
|
|
|
|
rec = do(t, h, "POST", "/api/v1/lists/1/archive/import", importMbox, true)
|
|
expect(t, rec, 200)
|
|
decode(t, rec, &sum)
|
|
if sum.Imported != 0 || sum.Duplicates != 2 {
|
|
t.Fatalf("second import: %+v", sum)
|
|
}
|
|
}
|
|
|
|
func TestArchiveImportReportsFailuresWithoutFailingTheRequest(t *testing.T) {
|
|
h, s := queueServer(t)
|
|
s.CreateList("dev", "example.org", "Dev")
|
|
body := importMbox + "\nFrom x@y.z Tue Jan 2 15:04:05 2024\n\nno headers\n"
|
|
rec := do(t, h, "POST", "/api/v1/lists/1/archive/import", body, true)
|
|
expect(t, rec, 200)
|
|
var sum importJSON
|
|
decode(t, rec, &sum)
|
|
if sum.Imported != 2 || sum.Failed != 1 || len(sum.Errors) != 1 || !strings.Contains(sum.Errors[0], "message 3") {
|
|
t.Fatalf("summary: %+v", sum)
|
|
}
|
|
}
|
|
|
|
func TestArchiveImportEdgeCases(t *testing.T) {
|
|
h, s := queueServer(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
s.SetPolicies(l.ID, store.PostMembers, store.JoinOpen, store.ArchiveOff)
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/99/archive/import", importMbox, true), 404)
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/abc/archive/import", importMbox, true), 404)
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/1/archive/import", importMbox, false), 401)
|
|
|
|
rec := do(t, h, "POST", "/api/v1/lists/1/archive/import", "", true)
|
|
expect(t, rec, 200)
|
|
var sum importJSON
|
|
decode(t, rec, &sum)
|
|
if sum.Imported != 0 || sum.Failed != 0 {
|
|
t.Fatalf("empty body: %+v", sum)
|
|
}
|
|
rec = do(t, h, "POST", "/api/v1/lists/1/archive/import", importMbox, true)
|
|
expect(t, rec, 200)
|
|
decode(t, rec, &sum)
|
|
if sum.Imported != 2 {
|
|
t.Fatalf("an import works even when the archive policy is off: %+v", sum)
|
|
}
|
|
}
|
|
|
|
type failingBody struct {
|
|
data string
|
|
done bool
|
|
}
|
|
|
|
func (f *failingBody) Read(p []byte) (int, error) {
|
|
if !f.done {
|
|
f.done = true
|
|
return copy(p, f.data), nil
|
|
}
|
|
return 0, errors.New("connection reset by peer")
|
|
}
|
|
|
|
func TestArchiveImportStoppedByAReadErrorIsAnErrorResponse(t *testing.T) {
|
|
h, s := queueServer(t)
|
|
s.CreateList("dev", "example.org", "Dev")
|
|
req := httptest.NewRequest("POST", "/api/v1/lists/1/archive/import", &failingBody{data: importMbox})
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
expect(t, rec, 400)
|
|
var e struct{ Error string }
|
|
decode(t, rec, &e)
|
|
if !strings.Contains(e.Error, "connection reset") || !strings.Contains(e.Error, "1 message") {
|
|
t.Fatalf("the error says what happened and how far it got: %q", e.Error)
|
|
}
|
|
l, _ := s.FindList("dev@example.org")
|
|
if _, total, _ := s.ListMessages(l.ID, 10, 0); total != 1 {
|
|
t.Fatalf("the first message was complete and is kept, got %d", total)
|
|
}
|
|
}
|
|
|
|
func TestQueueShowsFailedDeliveriesApartFromPendingOnes(t *testing.T) {
|
|
h, s := queueServer(t)
|
|
s.Enqueue([]byte("x"), []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"}})
|
|
due, _ := s.DueOutbox(10)
|
|
s.FailOutbox(due[2].ID, "refused by the relay: 554 no")
|
|
s.RetryOutbox(due[1].ID, time.Now().Add(time.Hour), "down")
|
|
|
|
var q queueJSON
|
|
decode(t, do(t, h, "GET", "/api/v1/queue", "", true), &q)
|
|
if q.Pending != 2 || q.Retrying != 1 || q.Failed != 1 || len(q.Items) != 3 {
|
|
t.Fatalf("queue: %+v", q)
|
|
}
|
|
if !q.Items[0].Failed || q.Items[0].Recipient != "c@x.com" || q.Items[0].LastError != "refused by the relay: 554 no" {
|
|
t.Fatalf("failed deliveries come first, with the reason: %+v", q.Items[0])
|
|
}
|
|
if q.Items[1].Failed || q.Items[2].Failed {
|
|
t.Fatalf("only one failed: %+v", q.Items)
|
|
}
|
|
expect(t, do(t, h, "DELETE", "/api/v1/queue/"+strconv.FormatInt(due[2].ID, 10), "", true), 204)
|
|
}
|
|
|
|
// TestEveryRouteNeedsTheToken walks the route table New registers, so a route added later is
|
|
// covered without touching this test: all of them but the health check answer 401 without a token
|
|
// and something else with one.
|
|
func TestEveryRouteNeedsTheToken(t *testing.T) {
|
|
s, err := store.Open(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { s.Close() })
|
|
h := New(s, token, nil)
|
|
|
|
rs := (&server{}).routes()
|
|
if len(rs) < 17 {
|
|
t.Fatalf("only %d routes in the table", len(rs))
|
|
}
|
|
for i, rt := range rs {
|
|
method, pattern, _ := strings.Cut(rt.pattern, " ")
|
|
path := strings.NewReplacer("{id}", "1", "{address}", "a@example.com").Replace(pattern)
|
|
t.Run(rt.pattern, func(t *testing.T) {
|
|
// Each request comes from its own address, so the lockout never gets in the way.
|
|
addr := fmt.Sprintf("192.0.2.%d:4000", i+1)
|
|
call := func(auth bool) int {
|
|
req := httptest.NewRequest(method, path, strings.NewReader("{}"))
|
|
req.RemoteAddr = addr
|
|
if auth {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
return rec.Code
|
|
}
|
|
if pattern == "/api/v1/health" {
|
|
if got := call(false); got != 200 {
|
|
t.Fatalf("health without a token: %d", got)
|
|
}
|
|
return
|
|
}
|
|
if got := call(false); got != 401 {
|
|
t.Errorf("no token: status %d, want 401", got)
|
|
}
|
|
if got := call(true); got == 401 {
|
|
t.Errorf("good token: status %d", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCreateListAcceptsTheLimits(t *testing.T) {
|
|
h := newServer(t)
|
|
for _, body := range []string{
|
|
`{"name":"` + strings.Repeat("a", 30) + `","domain":"example.org","title":"` + strings.Repeat("è", 200) + `"}`,
|
|
`{"name":"dev-team_1.eu","domain":"lists.sub-domain.example.org"}`,
|
|
`{"name":"Dev2","domain":"localhost","title":"Développeurs ☕"}`,
|
|
} {
|
|
if rec := do(t, h, "POST", "/api/v1/lists", body, true); rec.Code != 201 {
|
|
t.Errorf("%.80q: status %d, body %s", body, rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
// A title with a line break would make every later post to the list fail to rewrite, so the list
|
|
// must not come into existence at all.
|
|
func TestCreateListWithABadTitleLeavesNothingBehind(t *testing.T) {
|
|
h := newServer(t)
|
|
expect(t, do(t, h, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev\r\nX: y"}`, true), 400)
|
|
rec := do(t, h, "GET", "/api/v1/lists", "", true)
|
|
if strings.TrimSpace(rec.Body.String()) != "[]" {
|
|
t.Fatalf("the refused list was created: %s", rec.Body.String())
|
|
}
|
|
// The name is free for a good request.
|
|
expect(t, do(t, h, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev"}`, true), 201)
|
|
}
|
|
|
|
func TestCreateListLowercasesNameAndDomain(t *testing.T) {
|
|
h := newServer(t)
|
|
rec := do(t, h, "POST", "/api/v1/lists", `{"name":" Dev ","domain":"Example.ORG"}`, true)
|
|
expect(t, rec, 201)
|
|
var l listJSON
|
|
decode(t, rec, &l)
|
|
if l.Address != "dev@example.org" || l.Title != "Dev" {
|
|
t.Fatalf("%+v", l)
|
|
}
|
|
}
|
|
|
|
func TestUpdateListTitleUsesTheSameRules(t *testing.T) {
|
|
h := newServer(t)
|
|
createList(t, h)
|
|
for _, title := range []string{`"a\tb"`, `"` + strings.Repeat("x", 201) + `"`, `" "`, `"a\r\nb"`} {
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1", `{"title":`+title+`}`, true), 400)
|
|
}
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1", `{"title":"`+strings.Repeat("x", 200)+`"}`, true), 200)
|
|
}
|
|
|
|
func TestValidDomainLengthLimits(t *testing.T) {
|
|
l63 := strings.Repeat("a", 63)
|
|
d253 := l63 + "." + l63 + "." + l63 + "." + strings.Repeat("a", 61)
|
|
if len(d253) != 253 || !validDomain(d253) {
|
|
t.Fatalf("a 253-character domain is valid (%d)", len(d253))
|
|
}
|
|
if validDomain(d253 + "a") {
|
|
t.Fatal("254 characters is too long")
|
|
}
|
|
if !validDomain(l63+".org") || validDomain("") || validName("") {
|
|
t.Fatal("63-character labels are valid, empty names and domains are not")
|
|
}
|
|
}
|
|
|
|
func TestCreateListTrimsTheTitle(t *testing.T) {
|
|
h := newServer(t)
|
|
rec := do(t, h, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":" Dev team \t"}`, true)
|
|
expect(t, rec, 201)
|
|
var l listJSON
|
|
decode(t, rec, &l)
|
|
if l.Title != "Dev team" {
|
|
t.Fatalf("title = %q", l.Title)
|
|
}
|
|
rec = do(t, h, "POST", "/api/v1/lists", `{"name":"ops","domain":"example.org","title":" "}`, true)
|
|
expect(t, rec, 201)
|
|
decode(t, rec, &l)
|
|
if l.Title != "ops" {
|
|
t.Fatalf("a blank title falls back to the name: %q", l.Title)
|
|
}
|
|
}
|