- Release removes the held message and queues its deliveries in one transaction - Of several simultaneous approvals one wins, the others get 404 - Extract the thread-root computation and test its linearity without the database (fast under -race)
140 lines
4.1 KiB
Go
140 lines
4.1 KiB
Go
package router
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"codeberg.org/squidd/parsimony/internal/store"
|
|
)
|
|
|
|
func postFrom(from string) []byte {
|
|
h := ""
|
|
if from != "" {
|
|
h = "From: " + from + "\r\n"
|
|
}
|
|
return []byte(h + "Subject: hi\r\nMessage-ID: <id1@x>\r\n\r\nbody\r\n")
|
|
}
|
|
|
|
func TestAMatchingFromHeaderIsDelivered(t *testing.T) {
|
|
e := newEnv(t)
|
|
for _, from := range []string{
|
|
"alice@example.com",
|
|
"Alice <alice@example.com>",
|
|
`"Alice S." <ALICE@Example.COM>`,
|
|
`"bob@example.com" <alice@example.com>`, // another address in the display name is just text
|
|
} {
|
|
if _, err := e.r.Route("alice@example.com", "dev@example.org", postFrom(from)); err != nil {
|
|
t.Errorf("From %q: %v", from, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestADifferentFromHeaderIsHeldForModeration(t *testing.T) {
|
|
e := newEnv(t)
|
|
cases := map[string]string{
|
|
"another member": "Bob <bob@example.com>",
|
|
"a stranger": "Carol <carol@example.com>",
|
|
"a plus-address": "alice+x@example.com",
|
|
"no From header": "",
|
|
"unparsable From": "not an address",
|
|
"several From address": "alice@example.com, bob@example.com",
|
|
"empty address": "<>",
|
|
}
|
|
for name, from := range cases {
|
|
_, err := e.r.Route("alice@example.com", "dev@example.org", postFrom(from))
|
|
if !errors.Is(err, ErrHeld) {
|
|
t.Errorf("%s: want ErrHeld, got %v", name, err)
|
|
}
|
|
}
|
|
held, _ := e.s.ListHeld(e.list.ID)
|
|
if len(held) != len(cases) {
|
|
t.Fatalf("held = %d, want %d", len(held), len(cases))
|
|
}
|
|
if held[0].Sender != "alice@example.com" {
|
|
t.Fatalf("the held message keeps the envelope sender: %+v", held[0])
|
|
}
|
|
if !strings.Contains(held[0].Reason, "From") || !strings.Contains(held[0].Reason, "sender") {
|
|
t.Fatalf("the admin is told why: %q", held[0].Reason)
|
|
}
|
|
if len(e.out.Sent) != 0 {
|
|
t.Fatal("nothing may be delivered")
|
|
}
|
|
if st, _ := e.s.OutboxStats(); st.Pending != 0 {
|
|
t.Fatal("nothing may be queued")
|
|
}
|
|
}
|
|
|
|
func TestNonMembersAreStillRefusedNotHeld(t *testing.T) {
|
|
e := newEnv(t)
|
|
_, err := e.r.Route("mallory@evil.com", "dev@example.org", postFrom("alice@example.com"))
|
|
if !errors.Is(err, ErrNotMember) {
|
|
t.Fatalf("a stranger is refused whatever From says: %v", err)
|
|
}
|
|
if held, _ := e.s.ListHeld(e.list.ID); len(held) != 0 {
|
|
t.Fatal("refused mail is not held")
|
|
}
|
|
}
|
|
|
|
func TestModeratedListsSayThatTheListIsModerated(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.SetPolicies(e.list.ID, "moderated", "open", "private")
|
|
e.r.Route("alice@example.com", "dev@example.org", postFrom("alice@example.com"))
|
|
held, _ := e.s.ListHeld(e.list.ID)
|
|
if len(held) != 1 || !strings.Contains(held[0].Reason, "moderated") {
|
|
t.Fatalf("held: %+v", held)
|
|
}
|
|
}
|
|
|
|
func TestAnApprovedIdentityMismatchIsDelivered(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.r.Route("alice@example.com", "dev@example.org", postFrom("Bob <bob@example.com>"))
|
|
held, _ := e.s.ListHeld(e.list.ID)
|
|
if err := e.release(held[0].ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(e.out.Sent) != 2 {
|
|
t.Fatalf("the admin approved it: sent %d", len(e.out.Sent))
|
|
}
|
|
}
|
|
|
|
func TestAFullModerationQueueRefusesTemporarily(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.SetPolicies(e.list.ID, "moderated", "open", "private")
|
|
for i := 0; i < 500; i++ {
|
|
if _, err := e.s.HoldMessage(e.list.ID, "x@y.com", []byte("Subject: s\r\n\r\nb"), "r"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
_, err := e.r.Route("alice@example.com", "dev@example.org", postFrom("alice@example.com"))
|
|
if !errors.Is(err, ErrHeldFull) {
|
|
t.Fatalf("want ErrHeldFull, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSimultaneousApprovalsDeliverOnce(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.s.SetPolicies(e.list.ID, "moderated", "open", "private")
|
|
e.r.Route("alice@example.com", "dev@example.org", postFrom("alice@example.com"))
|
|
held, _ := e.s.ListHeld(e.list.ID)
|
|
|
|
const n = 8
|
|
results := make(chan error, n)
|
|
for i := 0; i < n; i++ {
|
|
go func() { results <- e.r.Release(held[0].ID) }()
|
|
}
|
|
wins := 0
|
|
for i := 0; i < n; i++ {
|
|
if err := <-results; err == nil {
|
|
wins++
|
|
} else if !errors.Is(err, store.ErrNotFound) {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
if wins != 1 {
|
|
t.Fatalf("%d approvals succeeded, want exactly one", wins)
|
|
}
|
|
if st, _ := e.s.OutboxStats(); st.Pending != 2 {
|
|
t.Fatalf("2 members, one approval: pending = %d", st.Pending)
|
|
}
|
|
}
|