656 lines
21 KiB
Go
656 lines
21 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}`,
|
|
} {
|
|
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"`
|
|
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 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)
|
|
}
|