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>
93 lines
4.0 KiB
Go
93 lines
4.0 KiB
Go
package web
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"supermarket/internal/ai"
|
|
"supermarket/internal/profile"
|
|
"supermarket/internal/secrets"
|
|
"supermarket/internal/seo"
|
|
)
|
|
|
|
func TestChangesPage(t *testing.T) {
|
|
newAIClient = func(ai.Config, *secrets.Store) (ai.Client, error) {
|
|
return cannedAI{`{"title":"Plans and prices","description":"Every plan, what it costs, and what's included, per month."}`}, nil
|
|
}
|
|
defer func() { newAIClient = ai.New }()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
fmt.Fprint(w, `<html><head><title>Pricing · Us</title></head><body><h1>Pricing</h1><p>Plans and prices.</p></body></html>`)
|
|
}))
|
|
defer srv.Close()
|
|
s, h := newTestServer(t, nil)
|
|
profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: srv.URL}}})
|
|
|
|
body := do(h, http.MethodGet, "/seo/changes", nil, nil).Body.String()
|
|
for _, want := range []string{"Nothing waiting", "save a Gitea token", "set this website's Hugo repository"} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("empty page lacks %q", want)
|
|
}
|
|
}
|
|
rec := postForm(h, "/seo/changes/page?site=0", url.Values{"page": {srv.URL + "/pricing/"}, "why": {"Title too long."}})
|
|
if rec.Code != http.StatusSeeOther {
|
|
t.Fatalf("propose: %d %s", rec.Code, rec.Body)
|
|
}
|
|
body = do(h, http.MethodGet, "/seo/changes", nil, nil).Body.String()
|
|
for _, want := range []string{`value="Plans and prices · Us"`, "now: “Pricing · Us”", "Title too long.", "Yes: open a pull request"} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("proposal lacks %q", want)
|
|
}
|
|
}
|
|
cs, _ := seo.Changes(s.DB, srv.URL, 10)
|
|
id := fmt.Sprint(cs[0].ID)
|
|
postForm(h, "/seo/changes/"+id+"/save", url.Values{"new_title": {"Plans and prices from $5 · Us"}, "new_description": {"Edited."}})
|
|
if c, _ := seo.GetChange(s.DB, cs[0].ID); c.NewTitle != "Plans and prices from $5 · Us" || c.NewDescription != "Edited." {
|
|
t.Errorf("edit: %+v", c)
|
|
}
|
|
if rec := postForm(h, "/seo/changes/"+id+"/yes", nil); !strings.Contains(rec.Body.String(), "Save a Gitea token") {
|
|
t.Errorf("yes without Gitea: %d", rec.Code)
|
|
}
|
|
postForm(h, "/seo/changes/"+id+"/no", nil)
|
|
if c, _ := seo.GetChange(s.DB, cs[0].ID); c.Status != seo.ChangeDeclined {
|
|
t.Errorf("no: %+v", c.Status)
|
|
}
|
|
if body := do(h, http.MethodGet, "/seo/changes", nil, nil).Body.String(); !strings.Contains(body, "You said no") {
|
|
t.Error("declined change isn't listed")
|
|
}
|
|
// Gitea settings.
|
|
postForm(h, "/settings/search", url.Values{"gitea_url": {"https://gitea.example.com/"}, secrets.GiteaToken: {"tok"}})
|
|
if s.giteaURL() != "https://gitea.example.com" || s.gitea() == nil {
|
|
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)
|
|
}
|
|
}
|