Gitea test works with a repository-only token

The test asked Gitea for the token's user, which needs read:user; a token
scoped to write:repository (all Supermarket needs) failed. It now checks
the websites' repositories and only names the user when it can.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 08:31:37 -07:00
co-authored by Claude Opus 5.5
parent 68c224f51b
commit bf933971dd
2 changed files with 31 additions and 3 deletions
+6 -3
View File
@@ -255,11 +255,14 @@ func (s *Server) testGitea(ctx context.Context) map[string]any {
if g == nil {
return map[string]any{"Error": "Save the token first."}
}
who, err := g.Me(ctx)
if err != nil {
// Only the repositories matter: a token scoped to write:repository can't
// read its own user, and doesn't need to.
out := "The token works."
if who, err := g.Me(ctx); err == nil && who != "" {
out = "The token works, as " + who + "."
} else if errors.Is(err, gitea.ErrUnauthorized) {
return map[string]any{"Error": err.Error()}
}
out := "The token works, as " + who + "."
var bad []string
for _, st := range s.seoSites() {
p, _ := profile.Load(s.DB)
+25
View File
@@ -65,3 +65,28 @@ func TestChangesPage(t *testing.T) {
t.Errorf("gitea settings: %q", s.giteaURL())
}
}
// A token scoped only to repositories can't read /user; the test still
// works from the repository.
func TestGiteaTestWithRepositoryScope(t *testing.T) {
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/user":
w.WriteHeader(http.StatusForbidden)
fmt.Fprint(w, `{"message":"token does not have at least one of required scope(s), required=[read:user], token scope=write:repository"}`)
case "/api/v1/repos/Federated_Computer/website":
fmt.Fprint(w, `{"full_name":"Federated_Computer/website","default_branch":"main","permissions":{"push":true}}`)
default:
http.NotFound(w, r)
}
}))
defer g.Close()
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: "https://us.example",
Repo: "https://gitea.example.com/Federated_Computer/website.git"}}})
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
body := postForm(h, "/settings/search/test/gitea", nil).Body.String()
if !strings.Contains(body, "The token works.") || !strings.Contains(body, "Federated_Computer/website is reachable") || strings.Contains(body, "error") {
t.Errorf("test: %s", body)
}
}