parsimony/internal/api/api_test.go
tgiachi 7ce6a1c98e feat(api): REST API for lists and members
- Add /api/v1 JSON endpoints: lists CRUD, members add/list/remove, health
- Protect routes with a constant-time bearer token check
- Reject unknown JSON fields and invalid names or addresses
- Map store errors to 404 and 409
2026-11-09 14:57:43 +01:00

196 lines
5.3 KiB
Go

package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"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)
}
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, "")
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"`
}
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":"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"`
}
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)
}