feat(router): RequestJoin for callers that are not an LMTP session
- RequestJoin(listAddress, addr) reuses join: the same open-policy check, the silence for active members, the confirmation mail and the store rate limits - it returns nil for an unknown or closed list, a member and a rate limited request, and an error only for internal failures - tests: confirmation mail and confirming it, nothing revealed, a bounce disabled member can rejoin, a database failure is reported
This commit is contained in:
parent
e772777a2a
commit
daea68f69d
2 changed files with 91 additions and 0 deletions
|
|
@ -94,6 +94,24 @@ func (r *Router) join(l store.List, sender string) error {
|
||||||
return r.askConfirmation(l, sender, store.ActionJoin)
|
return r.askConfirmation(l, sender, store.ActionJoin)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RequestJoin does what the +join command does for addr, for a caller that is not an LMTP session
|
||||||
|
// (the public subscribe form): there is no envelope sender to check, so the caller must have
|
||||||
|
// validated addr. It returns nil whenever nothing should be revealed: an unknown or closed list, an
|
||||||
|
// active member, a request the store rate limits. It returns an error only for internal failures.
|
||||||
|
func (r *Router) RequestJoin(listAddress, addr string) error {
|
||||||
|
l, err := r.store.FindList(listAddress)
|
||||||
|
if errors.Is(err, store.ErrNotFound) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := r.join(l, addr); err != nil && !errors.Is(err, ErrJoinClosed) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Router) leave(l store.List, sender string) error {
|
func (r *Router) leave(l store.List, sender string) error {
|
||||||
if ok, err := r.store.IsMember(l.ID, sender); err != nil || !ok {
|
if ok, err := r.store.IsMember(l.ID, sender); err != nil || !ok {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -628,3 +628,76 @@ func TestDroppingARCHeadersKeepsSimilarlyNamedOnes(t *testing.T) {
|
||||||
t.Errorf("only the ARC- prefix is dropped: %v", h)
|
t.Errorf("only the ARC- prefix is dropped: %v", h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRequestJoinQueuesTheConfirmationMail(t *testing.T) {
|
||||||
|
e := newEnv(t)
|
||||||
|
if err := e.r.RequestJoin("dev@example.org", "carol@x.com"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
e.flush()
|
||||||
|
m := lastMail(t, e.out)
|
||||||
|
if len(m.To) != 1 || m.To[0] != "carol@x.com" {
|
||||||
|
t.Fatalf("confirmation must go to the requester: %+v", m.To)
|
||||||
|
}
|
||||||
|
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); ok {
|
||||||
|
t.Fatal("not a member before confirming")
|
||||||
|
}
|
||||||
|
// The mail is the usual one: replying to it confirms.
|
||||||
|
if _, err := e.command("carol@x.com", "dev+confirm-"+token(t, m)+"@example.org", nil); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if ok, _ := e.s.IsMember(e.list.ID, "carol@x.com"); !ok {
|
||||||
|
t.Fatal("carol must be a member after confirming")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing may tell the caller whether the list exists, is closed, or the address is known.
|
||||||
|
func TestRequestJoinRevealsNothing(t *testing.T) {
|
||||||
|
e := newEnv(t)
|
||||||
|
closed, _ := e.s.CreateList("closed", "example.org", "Closed")
|
||||||
|
e.s.SetPolicies(closed.ID, store.PostMembers, store.JoinClosed, store.ArchivePrivate)
|
||||||
|
for _, c := range []struct{ name, list, addr string }{
|
||||||
|
{"unknown list", "nope@example.org", "carol@x.com"},
|
||||||
|
{"closed list", "closed@example.org", "carol@x.com"},
|
||||||
|
{"active member", "dev@example.org", "alice@example.com"},
|
||||||
|
} {
|
||||||
|
if err := e.r.RequestJoin(c.list, c.addr); err != nil {
|
||||||
|
t.Errorf("%s: want nil, got %v", c.name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.flush()
|
||||||
|
if len(e.out.Sent) != 0 {
|
||||||
|
t.Fatalf("no mail for these cases, sent %d", len(e.out.Sent))
|
||||||
|
}
|
||||||
|
// Asking twice is rate limited by the store: still nil, still one mail.
|
||||||
|
e.r.RequestJoin("dev@example.org", "carol@x.com")
|
||||||
|
if err := e.r.RequestJoin("dev@example.org", "carol@x.com"); err != nil {
|
||||||
|
t.Fatalf("rate limited: want nil, got %v", err)
|
||||||
|
}
|
||||||
|
e.flush()
|
||||||
|
if len(e.out.Sent) != 1 {
|
||||||
|
t.Fatalf("one mail expected, sent %d", len(e.out.Sent))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRequestJoinLetsABounceDisabledMemberRejoin(t *testing.T) {
|
||||||
|
e := newEnv(t)
|
||||||
|
for i := 0; i < store.BounceThreshold; i++ {
|
||||||
|
e.s.RecordHardBounce(e.alice, store.BounceThreshold)
|
||||||
|
}
|
||||||
|
if err := e.r.RequestJoin("dev@example.org", "alice@example.com"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
e.flush()
|
||||||
|
if m := lastMail(t, e.out); len(m.To) != 1 || m.To[0] != "alice@example.com" {
|
||||||
|
t.Fatalf("to = %v", m.To)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRequestJoinReportsAnInternalFailure(t *testing.T) {
|
||||||
|
e := newEnv(t)
|
||||||
|
e.s.Close()
|
||||||
|
if err := e.r.RequestJoin("dev@example.org", "carol@x.com"); err == nil {
|
||||||
|
t.Fatal("a database failure must be reported")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue