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
+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)
}
}