Files

36 lines
1.2 KiB
Go
Raw Permalink Normal View History

package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestRegisterRoutes(t *testing.T) {
// Auth is disabled and there is no database, so these checks only touch
// routes that answer before any query runs.
server := &Server{auth: newAuthService(nil, "", false, true)}
mux := http.NewServeMux()
server.registerRoutes(mux, webFS)
cases := []struct {
name string
method string
path string
want int
}{
{name: "health is public", method: http.MethodGet, path: "/health", want: http.StatusOK},
{name: "sync needs a bearer token", method: http.MethodPost, path: "/api/sync", want: http.StatusUnauthorized},
{name: "lists need a bearer token", method: http.MethodGet, path: "/api/lists", want: http.StatusUnauthorized},
{name: "managed users pass the role gate when auth is disabled", method: http.MethodGet, path: "/api/auth/users", want: http.StatusOK},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
recorder := httptest.NewRecorder()
mux.ServeHTTP(recorder, httptest.NewRequest(tc.method, tc.path, nil))
if recorder.Code != tc.want {
t.Fatalf("%s %s status = %d, want %d", tc.method, tc.path, recorder.Code, tc.want)
}
})
}
}