package media import ( "net/http" "net/http/httptest" "strings" "testing" "github.com/go-chi/chi/v5" sharedauth "repos.gfranz.ch/Gabriel20xx/shared/backend/auth" ) // TestMediaRoutesRefuseAnonymousRequests walks the media route table: every route mounted behind // Require answers 401 to a caller with no session, before any handler runs. The Service has no // database or CarrierNode client, so a request that got past the gate would panic. func TestMediaRoutesRefuseAnonymousRequests(t *testing.T) { auth := sharedauth.NewWithStore(loginRequiredStore{}, "session", false, "", "testproject") router := chi.NewRouter() New(Config{Auth: auth}).Mount(router) for _, route := range []struct{ method, target string }{ {http.MethodPost, "/media/like"}, {http.MethodPost, "/media/save"}, {http.MethodGet, "/media/liked"}, {http.MethodPost, "/media/7/tags"}, {http.MethodPost, "/media/7/tags/funny/vote"}, {http.MethodGet, "/playlists"}, {http.MethodPost, "/playlists"}, {http.MethodDelete, "/playlists/3"}, {http.MethodPost, "/playlists/3/items"}, {http.MethodDelete, "/playlists/3/items"}, {http.MethodPatch, "/playlists/3/items/reorder"}, } { response := httptest.NewRecorder() router.ServeHTTP(response, httptest.NewRequest(route.method, route.target, strings.NewReader(`{}`))) if response.Code != http.StatusUnauthorized { t.Errorf("%s %s status = %d, want 401", route.method, route.target, response.Code) } } }