A GitHub token in Settings → Network and sharing that GitHub refused (401, expired or revoked) failed every plugin and skill check, so a stale token was worse than none. Now it is treated as an environment token already was: the request is asked again without it, the token isn't sent again while magpie runs or until the field changes, and the anonymous answer (60 an hour) is the one taken. This overturns the package comment and TestSettingsTokenRefusedStaysAnError: the token being the user's choice is a reason to tell them, not to fail the check. The page stays honest about it: - Settings › Network and sharing still shows the token, masked, with Remove, and says in red that GitHub refused it and requests go without one until it is replaced (/api/settings githubTokenRefused). Before, a refused token simply read as none being set. - After a Settings token's refusal requests go anonymous, not with GITHUB_TOKEN/GH_TOKEN: the user put the Settings one ahead of those, and the page says "without one". Patch by MYCF711 (fix/settings-token-fallback, 7d93f1b5): the ghtoken change and its tests. Changed from it: the notice is in Settings' row, where the token lives, instead of the patch's Plugins page note, which cost ~630 bytes of core script the budget (28 bytes of room atc54478904) doesn't have; the Library's skill checks reach Settings' row too. Token() doesn't fall through to the environment; the library's skill check test (the sibling the patch left red) expects the anonymous answer; the unreachable new Refused() message is dropped (Refused's "where" now holds for a refused Settings token). app.js stays in its budget by shortening renderGitHubToken's comment. Run at this tree (basec54478904), on mote: - go test -tags nogui ./internal/ghtoken ./internal/plugin ./internal/library ./internal/gui: ok (TestCoreScriptBudget included; no budget raised). GOOS=linux and windows go vet, go build ./...: clean. - github-token-refused.test.cjs (new; chromium and webkit, en and zh, 1000 and 440px): red 8 of 8 against origin/main's assets, green with the change. With github-token and i18n-keys: 21 of 22; the red is i18n-keys "a plugin's words are its own" ("Runs as"), red at origin/main74dcf40aaalone too (#3897). - Every GUI file that opens Settings › Network and sharing (allow-ips, api-key-theme, bare-ids, browser-access, cors-origins, gateway-port, lan-docker, proxy-test, settings-proxy-auth, settings-proxy-remember): 126 of 126. - Broken by hand: Token() falling through to GITHUB_TOKEN after the Settings token's refusal turns TestSettingsTokenRefusedGoesWithoutEnvToken red; MYCF711 showed TestSettingsTokenRefusedAskedAgainWithout red with from == "settings" back in Do. Not tried: a real revoked token against api.github.com. The Plugins and Library pages have no notice of their own (Settings' row says it).
107 lines
4.1 KiB
Go
107 lines
4.1 KiB
Go
package gui
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/ghtoken"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// The GitHub token is set and removed on its own, and the page is told it
|
|
// masked, with where it is from: Settings, or GITHUB_TOKEN / GH_TOKEN.
|
|
func TestGitHubTokenSetting(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("GITHUB_TOKEN", "")
|
|
t.Setenv("GH_TOKEN", "")
|
|
call := func(method, path, body string) (int, string) {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
|
|
return rec.Code, rec.Body.String()
|
|
}
|
|
const tok = "ghp_abcdefghijklmnopqrstuvwxyz0123"
|
|
code, body := call("POST", "/api/settings/github-token", `{"token":" `+tok+` "}`)
|
|
if code != http.StatusOK || settings.Load().GitHubToken != tok {
|
|
t.Fatalf("set: %d %s, saved %q", code, body, settings.Load().GitHubToken)
|
|
}
|
|
for _, path := range []string{"/api/settings/github-token", "/api/settings"} {
|
|
if path == "/api/settings" {
|
|
code, body = call("GET", path, "")
|
|
}
|
|
if code != http.StatusOK || strings.Contains(body, tok) || !strings.Contains(body, `"githubTokenMask":"ghp_…0123"`) || !strings.Contains(body, `"githubTokenFrom":"settings"`) {
|
|
t.Errorf("%s answered %d and told the page %s", path, code, body)
|
|
}
|
|
}
|
|
// a save of the Settings page keeps it
|
|
if code, body = call("POST", "/api/settings", `{"theme":"dark","githubToken":""}`); code != http.StatusOK || settings.Load().GitHubToken != tok {
|
|
t.Fatalf("save: %d %s, token %q", code, body, settings.Load().GitHubToken)
|
|
}
|
|
if code, _ = call("POST", "/api/settings/github-token", `{"token":"two words"}`); code == http.StatusOK || settings.Load().GitHubToken != tok {
|
|
t.Errorf("a token with a space was taken: %d", code)
|
|
}
|
|
// removed, the environment's is used, and said so
|
|
t.Setenv("GH_TOKEN", "gho_fromtheenvironment99")
|
|
code, body = call("POST", "/api/settings/github-token", `{"token":""}`)
|
|
if code != http.StatusOK || settings.Load().GitHubToken != "" {
|
|
t.Fatalf("remove: %d %s", code, body)
|
|
}
|
|
if strings.Contains(body, "gho_fromtheenvironment99") || !strings.Contains(body, `"githubTokenFrom":"GH_TOKEN"`) || !strings.Contains(body, `"githubTokenMask":"gho_…nt99"`) {
|
|
t.Errorf("from the environment: %s", body)
|
|
}
|
|
}
|
|
|
|
// A Settings token GitHub refused is still shown in Settings, masked and
|
|
// said to be refused, not as if none were set, while requests go without
|
|
// one (#3926).
|
|
func TestGitHubTokenRefusedShown(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("GITHUB_TOKEN", "")
|
|
t.Setenv("GH_TOKEN", "")
|
|
state := func() string {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("GET", "/api/settings", nil))
|
|
return rec.Body.String()
|
|
}
|
|
const tok = "ghp_refusedrefusedrefusedrefused4567"
|
|
s := settings.Load()
|
|
s.GitHubToken = tok
|
|
if err := settings.Save(s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if body := state(); strings.Contains(body, "githubTokenRefused") {
|
|
t.Fatalf("refused before GitHub was asked: %s", body)
|
|
}
|
|
gh := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Authorization") != "" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
}
|
|
}))
|
|
defer gh.Close()
|
|
req, _ := http.NewRequest("GET", gh.URL+"/repos/o/r", nil)
|
|
resp, _, err := ghtoken.Do(req, gh.URL, http.DefaultClient.Do)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("the request without the token got %d", resp.StatusCode)
|
|
}
|
|
body := state()
|
|
if strings.Contains(body, tok) || !strings.Contains(body, `"githubTokenRefused":true`) || !strings.Contains(body, `"githubTokenFrom":"settings"`) || !strings.Contains(body, `"githubTokenMask":"ghp_…4567"`) {
|
|
t.Errorf("after the refusal the page was told %s", body)
|
|
}
|
|
}
|