test(api): the route walk reports a handler it reached instead of crashing (#8295)
release / web (push) Successful in 1m40s
release / go (push) Failing after 2m3s
release / govulncheck (push) Successful in 21s
release / integration (push) Failing after 3m32s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Publish the release image to GHCR (dispatch only) (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 2m48s
release / android (push) Canceled after 3m28s

With the admin check removed, the walk reached a handler that panicked on
its bare wiring, and the panic ended the whole api test binary, hiding
every other failure. The walk now recovers and names the route that ran
its handler. The deliberate break above stays in place for one more run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 10:51:39 -05:00
co-authored by Claude Opus 5.5
parent 6f89c3c212
commit 22f801d8f1
+30 -8
View File
@@ -8,6 +8,7 @@ import (
"log/slog"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
@@ -92,6 +93,31 @@ func sessionCookie(t *testing.T, pool *pgxpool.Pool, user dbq.User) *http.Cookie
return &http.Cookie{Name: auth.SessionCookieName, Value: token}
}
// reachedHandler stands for a request that got past the access checks into a
// handler, which then panicked on the walk's bare wiring.
const reachedHandler = -1
// serveGuarded serves one request and returns its status, or reachedHandler
// when a handler panicked. Without the recover, one route mounted outside its
// group would take down the whole test binary and hide every other failure.
func serveGuarded(r http.Handler, req *http.Request) (code int) {
defer func() {
if recover() != nil {
code = reachedHandler
}
}()
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w.Code
}
func describeCode(code int) string {
if code == reachedHandler {
return "from inside its handler (it ran, and panicked)"
}
return strconv.Itoa(code)
}
// Every API route but the public ones answers 401 without a session, and
// every admin route answers 403 to a signed-in user who is not an admin.
// The routes come from the production router, so a route registered outside
@@ -109,10 +135,8 @@ func TestMountedRoutes_RequireASessionAndAdminRoutesAnAdmin(t *testing.T) {
public[key] = true
continue
}
anon := httptest.NewRecorder()
r.ServeHTTP(anon, httptest.NewRequest(rt.method, rt.path, nil))
if anon.Code != http.StatusUnauthorized {
t.Errorf("%s answered %d without a session, want 401", key, anon.Code)
if code := serveGuarded(r, httptest.NewRequest(rt.method, rt.path, nil)); code != http.StatusUnauthorized {
t.Errorf("%s answered %s without a session, want 401", key, describeCode(code))
}
if !strings.HasPrefix(rt.pattern, "/api/admin/") {
continue
@@ -120,10 +144,8 @@ func TestMountedRoutes_RequireASessionAndAdminRoutesAnAdmin(t *testing.T) {
admin++
req := httptest.NewRequest(rt.method, rt.path, nil)
req.AddCookie(cookie)
signedIn := httptest.NewRecorder()
r.ServeHTTP(signedIn, req)
if signedIn.Code != http.StatusForbidden {
t.Errorf("%s answered %d to a non-admin, want 403", key, signedIn.Code)
if code := serveGuarded(r, req); code != http.StatusForbidden {
t.Errorf("%s answered %s to a non-admin, want 403", key, describeCode(code))
}
}
if admin < 50 {