package main import ( "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "sourcecraft.dev/bigbes/sr-ht-ecore/apimeta" "sourcecraft.dev/bigbes/sr-ht-dolt/authn" ) // The scope this service advertises and the scope it enforces are the same fact // written twice: meta.sr.ht prefixes the service name to what it reads from // api-meta.json, and authn.DatabaseScope is what a presented token is checked // against. A drift would let a user mint a token meta calls valid and the clone // path does not honour, which is a support ticket rather than an error. func TestTheAdvertisedScopeIsTheEnforcedOne(t *testing.T) { assert.Equal(t, authn.DatabaseScope, serviceName+"/"+databaseScopeName) } // The name is upper case because meta.sr.ht compares it verbatim — no case // folding — and every other service on the instance names its scopes that way // (git.sr.ht/REPOSITORIES, todo.sr.ht/TRACKERS, paste.sr.ht/PASTES). A lower // case scope is accepted by meta and works, which is exactly why the drift // survived: it breaks only the user who spells the grant by hand, by analogy // with every neighbouring service. func TestTheScopeIsSpelledLikeEveryOtherServices(t *testing.T) { assert.Equal(t, strings.ToUpper(databaseScopeName), databaseScopeName) } // The wiring, not the package: ecore's apimeta owns the never-null rule, and // this asserts dolt.sr.ht actually declares the grant it enforces rather than // serving an empty list that would leave meta with no checkbox to offer. func TestAPIMetaAdvertisesTheDatabaseScope(t *testing.T) { rec := httptest.NewRecorder() apimeta.Handler(databaseScopeName).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, apimeta.Path, nil)) require.Equal(t, http.StatusOK, rec.Code) var got apimeta.Meta require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got)) assert.Equal(t, []string{"DATABASES"}, got.Scopes) assert.NotContains(t, rec.Body.String(), "null") }