- the member DTO carries publisher
- POST /lists/{id}/members takes an optional publisher: absent leaves the flag
of an existing member alone, true or false sets it
- PATCH /lists/{id}/members/{address} with {"publisher": bool}: 404 for a
non-member, 400 when the field is missing
- the list PATCH accepts post_policy publishers; the validation message says so
- openapi updated
112 lines
3.8 KiB
Go
112 lines
3.8 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type publisherJSON struct {
|
|
Address string `json:"address"`
|
|
Publisher bool `json:"publisher"`
|
|
}
|
|
|
|
// publishers returns the publisher flag of every member of list 1, by address.
|
|
func publishers(t *testing.T, h http.Handler) map[string]bool {
|
|
t.Helper()
|
|
rec := do(t, h, "GET", "/api/v1/lists/1/members", "", true)
|
|
expect(t, rec, 200)
|
|
var ms []publisherJSON
|
|
decode(t, rec, &ms)
|
|
out := map[string]bool{}
|
|
for _, m := range ms {
|
|
out[m.Address] = m.Publisher
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestAddMemberWithThePublisherFlag(t *testing.T) {
|
|
h := newServer(t)
|
|
createList(t, h)
|
|
rec := do(t, h, "POST", "/api/v1/lists/1/members", `{"address":"a@x.com","publisher":true}`, true)
|
|
expect(t, rec, 201)
|
|
var m publisherJSON
|
|
decode(t, rec, &m)
|
|
if !m.Publisher {
|
|
t.Fatalf("created: %+v", m)
|
|
}
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/1/members", `{"address":"b@x.com"}`, true), 201)
|
|
if got := publishers(t, h); !got["a@x.com"] || got["b@x.com"] {
|
|
t.Fatalf("publishers: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestAddMemberLeavesThePublisherFlagAloneWhenAbsent(t *testing.T) {
|
|
h := newServer(t)
|
|
createList(t, h)
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/1/members", `{"address":"a@x.com","publisher":true}`, true), 201)
|
|
rec := do(t, h, "POST", "/api/v1/lists/1/members", `{"address":"a@x.com","display_name":"A"}`, true)
|
|
expect(t, rec, 201)
|
|
var m publisherJSON
|
|
decode(t, rec, &m)
|
|
if !m.Publisher || !publishers(t, h)["a@x.com"] {
|
|
t.Fatalf("re-adding keeps the flag: %+v %v", m, publishers(t, h))
|
|
}
|
|
// An explicit false demotes an existing member.
|
|
rec = do(t, h, "POST", "/api/v1/lists/1/members", `{"address":"a@x.com","publisher":false}`, true)
|
|
expect(t, rec, 201)
|
|
decode(t, rec, &m)
|
|
if m.Publisher || publishers(t, h)["a@x.com"] {
|
|
t.Fatalf("demoted: %+v %v", m, publishers(t, h))
|
|
}
|
|
}
|
|
|
|
func TestPatchMemberSetsThePublisherFlag(t *testing.T) {
|
|
h := newServer(t)
|
|
createList(t, h)
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/1/members", `{"address":"a@x.com"}`, true), 201)
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1/members/A%40X.com", `{"publisher":true}`, true), 204)
|
|
if !publishers(t, h)["a@x.com"] {
|
|
t.Fatal("promoted")
|
|
}
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1/members/a%40x.com", `{"publisher":false}`, true), 204)
|
|
if publishers(t, h)["a@x.com"] {
|
|
t.Fatal("demoted")
|
|
}
|
|
}
|
|
|
|
func TestPatchMemberErrors(t *testing.T) {
|
|
h := newServer(t)
|
|
createList(t, h)
|
|
expect(t, do(t, h, "POST", "/api/v1/lists/1/members", `{"address":"a@x.com"}`, true), 201)
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1/members/b%40x.com", `{"publisher":true}`, true), 404)
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/99/members/a%40x.com", `{"publisher":true}`, true), 404)
|
|
rec := do(t, h, "PATCH", "/api/v1/lists/1/members/a%40x.com", `{}`, true)
|
|
expect(t, rec, 400)
|
|
if !strings.Contains(rec.Body.String(), "publisher") {
|
|
t.Fatalf("the error names the field: %s", rec.Body.String())
|
|
}
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1/members/a%40x.com", `{"publisher":"yes"}`, true), 400)
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1/members/a%40x.com", `{"publisher":true,"x":1}`, true), 400)
|
|
expect(t, do(t, h, "PATCH", "/api/v1/lists/1/members/a%40x.com", `{"publisher":true}`, false), 401)
|
|
if publishers(t, h)["a@x.com"] {
|
|
t.Fatal("nothing changed")
|
|
}
|
|
}
|
|
|
|
func TestThePublishersPostPolicyViaAPI(t *testing.T) {
|
|
h := newServer(t)
|
|
createList(t, h)
|
|
rec := do(t, h, "PATCH", "/api/v1/lists/1", `{"post_policy":"publishers"}`, true)
|
|
expect(t, rec, 200)
|
|
var l listJSON
|
|
decode(t, rec, &l)
|
|
if l.PostPolicy != "publishers" {
|
|
t.Fatalf("patched: %+v", l)
|
|
}
|
|
rec = do(t, h, "PATCH", "/api/v1/lists/1", `{"post_policy":"bogus"}`, true)
|
|
expect(t, rec, 400)
|
|
if !strings.Contains(rec.Body.String(), "members, moderated or publishers") {
|
|
t.Fatalf("the message lists the policies: %s", rec.Body.String())
|
|
}
|
|
}
|