Files
skillshare/internal/server/handler_check_test.go
Willie 115236a6fb fix(server): use token auth when checking default-branch skill remotes
The dashboard check fetched the remote HEAD without credentials when a
skill had no pinned branch. Private repos therefore reported an error in
the dashboard while the CLI check, which uses
GetRemoteHeadHashWithAuth, succeeded. The pinned-branch path already
used auth, and the default-branch path now matches it.
2026-09-30 01:44:50 +08:00

101 lines
3.3 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestHandleCheck_EmptySource(t *testing.T) {
s, _ := newTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/api/check", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
TrackedRepos []any `json:"tracked_repos"`
Skills []any `json:"skills"`
}
json.Unmarshal(rr.Body.Bytes(), &resp)
if len(resp.TrackedRepos) != 0 {
t.Errorf("expected 0 tracked repos, got %d", len(resp.TrackedRepos))
}
if len(resp.Skills) != 0 {
t.Errorf("expected 0 skills, got %d", len(resp.Skills))
}
}
// addTrackedRepoWithBrokenIndex clones a tracked repo from a local origin and
// corrupts its index, so `git status` fails while `git fetch` still succeeds.
func addTrackedRepoWithBrokenIndex(t *testing.T, sourceDir, name string) string {
t.Helper()
seed := t.TempDir()
initGitRepo(t, seed)
origin := filepath.Join(t.TempDir(), "origin.git")
runGit(t, seed, "clone", "--bare", seed, origin)
runGit(t, sourceDir, "clone", origin, name)
repoDir := filepath.Join(sourceDir, name)
// fetch reads the index for submodule recursion unless it is disabled.
runGit(t, repoDir, "config", "fetch.recurseSubmodules", "false")
if err := os.WriteFile(filepath.Join(repoDir, ".git", "index"), []byte("corrupt"), 0644); err != nil {
t.Fatalf("corrupt index: %v", err)
}
return repoDir
}
func TestHandleCheck_DirtyCheckErrorReported(t *testing.T) {
s, src := newTestServer(t)
addTrackedRepoWithBrokenIndex(t, src, "_team")
req := httptest.NewRequest(http.MethodGet, "/api/check", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
TrackedRepos []repoCheckResult `json:"tracked_repos"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if len(resp.TrackedRepos) != 1 || resp.TrackedRepos[0].Status != "error" {
t.Fatalf("expected one repo with status error, got %+v", resp.TrackedRepos)
}
}
// TestURLBranchGroupRemoteHash_DefaultBranchUsesAuth verifies that a skill
// without a pinned branch still probes the remote HEAD with token auth, as the
// CLI check does. The fake git answers ls-remote only when the auth env
// carries the token.
func TestURLBranchGroupRemoteHash_DefaultBranchUsesAuth(t *testing.T) {
const token = "ghp_check_default_branch_token"
fakeBin := t.TempDir()
script := "#!/bin/sh\n" +
"case \"$GIT_CONFIG_KEY_0\" in *" + token + "*) ;; *) echo 'fatal: Authentication failed' >&2; exit 128;; esac\n" +
"printf '0123456789abcdef0123456789abcdef01234567\\tHEAD\\n'\n"
if err := os.WriteFile(filepath.Join(fakeBin, "git"), []byte(script), 0755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", fakeBin+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("GIT_CONFIG_COUNT", "")
t.Setenv("GITHUB_TOKEN", token)
hash, err := urlBranchGroup{url: "https://github.com/org/private-skills.git"}.remoteHash()
if err != nil {
t.Fatalf("remoteHash without branch failed: %v", err)
}
if !strings.HasPrefix("0123456789abcdef0123456789abcdef01234567", hash) || hash == "" {
t.Fatalf("unexpected hash %q", hash)
}
}