From bf933971dd8d04e60077593895461271f0191362 Mon Sep 17 00:00:00 2001 From: David Young Date: Sun, 22 Nov 2026 08:31:37 -0700 Subject: [PATCH] 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 --- internal/web/changes.go | 9 ++++++--- internal/web/changes_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/internal/web/changes.go b/internal/web/changes.go index ab012ce..64b492f 100644 --- a/internal/web/changes.go +++ b/internal/web/changes.go @@ -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) diff --git a/internal/web/changes_test.go b/internal/web/changes_test.go index ae4c7db..198c98c 100644 --- a/internal/web/changes_test.go +++ b/internal/web/changes_test.go @@ -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) + } +}