package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"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.RepoScope 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.RepoScope, serviceName+"/"+repoScopeName)
}
// 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 TestAPIMetaAdvertisesTheRepoScope(t *testing.T) {
rec := httptest.NewRecorder()
apimeta.Handler(repoScopeName).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{"repos"}, got.Scopes)
assert.NotContains(t, rec.Body.String(), "null")
}