test(api): every route but health answers 401 without the token

routes() is now the single table New registers, so a route added later is
covered by TestEveryRouteNeedsTheToken without editing it.

- api: declare the routes in one table (routes) and register from it
- api: table test walks it, 401 without a token, not 401 with one
This commit is contained in:
tgiachi 2026-11-10 11:49:32 +01:00
commit 28b73fa24a
2 changed files with 82 additions and 20 deletions

View file

@ -40,29 +40,44 @@ func New(s *store.Store, token string, rel Releaser) http.Handler {
func newHandler(s *store.Store, token string, rel Releaser, now func() time.Time) http.Handler {
srv := &server{store: s, token: token, releaser: rel, lim: newLimiter(now)}
mux := http.NewServeMux()
mux.HandleFunc("GET /api/v1/health", func(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
})
mux.Handle("GET /api/v1/lists", srv.auth(srv.listLists))
mux.Handle("POST /api/v1/lists", srv.auth(srv.createList))
mux.Handle("GET /api/v1/lists/{id}", srv.auth(srv.getList))
mux.Handle("PATCH /api/v1/lists/{id}", srv.auth(srv.updateList))
mux.Handle("DELETE /api/v1/lists/{id}", srv.auth(srv.deleteList))
mux.Handle("GET /api/v1/lists/{id}/members", srv.auth(srv.listMembers))
mux.Handle("POST /api/v1/lists/{id}/members", srv.auth(srv.addMember))
mux.Handle("DELETE /api/v1/lists/{id}/members/{address}", srv.auth(srv.removeMember))
mux.Handle("GET /api/v1/lists/{id}/archive", srv.auth(srv.listArchive))
mux.Handle("POST /api/v1/lists/{id}/archive/import", srv.auth(srv.importArchive))
mux.Handle("GET /api/v1/archive/{id}", srv.auth(srv.getArchived))
mux.Handle("DELETE /api/v1/archive/{id}", srv.auth(srv.deleteArchived))
mux.Handle("GET /api/v1/queue", srv.auth(srv.getQueue))
mux.Handle("DELETE /api/v1/queue/{id}", srv.auth(srv.discardQueued))
mux.Handle("GET /api/v1/lists/{id}/held", srv.auth(srv.listHeld))
mux.Handle("POST /api/v1/held/{id}/approve", srv.auth(srv.approveHeld))
mux.Handle("DELETE /api/v1/held/{id}", srv.auth(srv.discardHeld))
for _, rt := range srv.routes() {
mux.Handle(rt.pattern, rt.handler)
}
return mux
}
type route struct {
pattern string
handler http.Handler
}
// routes is the only place routes are declared. Everything but the health check goes through auth.
func (s *server) routes() []route {
public := func(h http.HandlerFunc) http.Handler { return h }
return []route{
{"GET /api/v1/health", public(func(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
})},
{"GET /api/v1/lists", s.auth(s.listLists)},
{"POST /api/v1/lists", s.auth(s.createList)},
{"GET /api/v1/lists/{id}", s.auth(s.getList)},
{"PATCH /api/v1/lists/{id}", s.auth(s.updateList)},
{"DELETE /api/v1/lists/{id}", s.auth(s.deleteList)},
{"GET /api/v1/lists/{id}/members", s.auth(s.listMembers)},
{"POST /api/v1/lists/{id}/members", s.auth(s.addMember)},
{"DELETE /api/v1/lists/{id}/members/{address}", s.auth(s.removeMember)},
{"GET /api/v1/lists/{id}/archive", s.auth(s.listArchive)},
{"POST /api/v1/lists/{id}/archive/import", s.auth(s.importArchive)},
{"GET /api/v1/archive/{id}", s.auth(s.getArchived)},
{"DELETE /api/v1/archive/{id}", s.auth(s.deleteArchived)},
{"GET /api/v1/queue", s.auth(s.getQueue)},
{"DELETE /api/v1/queue/{id}", s.auth(s.discardQueued)},
{"GET /api/v1/lists/{id}/held", s.auth(s.listHeld)},
{"POST /api/v1/held/{id}/approve", s.auth(s.approveHeld)},
{"DELETE /api/v1/held/{id}", s.auth(s.discardHeld)},
}
}
func (s *server) auth(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip := clientIP(r)

View file

@ -685,3 +685,50 @@ func TestQueueShowsFailedDeliveriesApartFromPendingOnes(t *testing.T) {
}
expect(t, do(t, h, "DELETE", "/api/v1/queue/"+strconv.FormatInt(due[2].ID, 10), "", true), 204)
}
// TestEveryRouteNeedsTheToken walks the route table New registers, so a route added later is
// covered without touching this test: all of them but the health check answer 401 without a token
// and something else with one.
func TestEveryRouteNeedsTheToken(t *testing.T) {
s, err := store.Open(":memory:")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { s.Close() })
h := New(s, token, nil)
rs := (&server{}).routes()
if len(rs) < 17 {
t.Fatalf("only %d routes in the table", len(rs))
}
for i, rt := range rs {
method, pattern, _ := strings.Cut(rt.pattern, " ")
path := strings.NewReplacer("{id}", "1", "{address}", "a@example.com").Replace(pattern)
t.Run(rt.pattern, func(t *testing.T) {
// Each request comes from its own address, so the lockout never gets in the way.
addr := fmt.Sprintf("192.0.2.%d:4000", i+1)
call := func(auth bool) int {
req := httptest.NewRequest(method, path, strings.NewReader("{}"))
req.RemoteAddr = addr
if auth {
req.Header.Set("Authorization", "Bearer "+token)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec.Code
}
if pattern == "/api/v1/health" {
if got := call(false); got != 200 {
t.Fatalf("health without a token: %d", got)
}
return
}
if got := call(false); got != 401 {
t.Errorf("no token: status %d, want 401", got)
}
if got := call(true); got == 401 {
t.Errorf("good token: status %d", got)
}
})
}
}