mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
test(server): cover tracked repo uninstall edge cases
Add handler tests for nested tracked repo uninstall, ambiguous basename rejection, and traversal rejection. Also isolate trash-backed server tests by setting XDG_DATA_HOME in test helpers.
This commit is contained in:
@@ -16,6 +16,7 @@ func newTestServer(t *testing.T) (*Server, string) {
|
||||
tmp := t.TempDir()
|
||||
sourceDir := filepath.Join(tmp, "skills")
|
||||
os.MkdirAll(sourceDir, 0755)
|
||||
t.Setenv("XDG_DATA_HOME", filepath.Join(tmp, "data"))
|
||||
t.Setenv("XDG_STATE_HOME", filepath.Join(tmp, "state"))
|
||||
|
||||
cfgPath := filepath.Join(tmp, "config", "config.yaml")
|
||||
@@ -40,6 +41,7 @@ func newTestServerWithTargets(t *testing.T, targets map[string]string) (*Server,
|
||||
tmp := t.TempDir()
|
||||
sourceDir := filepath.Join(tmp, "skills")
|
||||
os.MkdirAll(sourceDir, 0755)
|
||||
t.Setenv("XDG_DATA_HOME", filepath.Join(tmp, "data"))
|
||||
t.Setenv("XDG_STATE_HOME", filepath.Join(tmp, "state"))
|
||||
|
||||
cfgPath := filepath.Join(tmp, "config", "config.yaml")
|
||||
@@ -69,3 +71,14 @@ func addSkill(t *testing.T, sourceDir, name string) {
|
||||
os.MkdirAll(skillDir, 0755)
|
||||
os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte("---\nname: "+name+"\n---\n# "+name), 0644)
|
||||
}
|
||||
|
||||
func addTrackedRepo(t *testing.T, sourceDir, relPath string) {
|
||||
t.Helper()
|
||||
repoDir := filepath.Join(sourceDir, filepath.FromSlash(relPath))
|
||||
if err := os.MkdirAll(filepath.Join(repoDir, ".git"), 0755); err != nil {
|
||||
t.Fatalf("failed to create tracked repo: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(repoDir, "README.md"), []byte("tracked repo"), 0644); err != nil {
|
||||
t.Fatalf("failed to seed tracked repo: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,12 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"skillshare/internal/trash"
|
||||
)
|
||||
|
||||
func TestHandleListSkills_Empty(t *testing.T) {
|
||||
@@ -97,3 +102,63 @@ func TestHandleGetSkillFile_PathTraversal(t *testing.T) {
|
||||
t.Error("expected non-200 for path traversal attempt")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleUninstallRepo_NestedRepoPath(t *testing.T) {
|
||||
s, src := newTestServer(t)
|
||||
addTrackedRepo(t, src, filepath.Join("org", "_team-skills"))
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/api/repos/org/_team-skills", nil)
|
||||
req.SetPathValue("name", "org/_team-skills")
|
||||
rr := httptest.NewRecorder()
|
||||
s.handleUninstallRepo(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(src, "org", "_team-skills")); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected nested tracked repo to be removed from source, stat err=%v", err)
|
||||
}
|
||||
|
||||
entries, err := filepath.Glob(filepath.Join(trash.TrashDir(), "org", "_team-skills_*"))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to inspect trash: %v", err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("expected nested tracked repo to be moved to trash, got %d matches", len(entries))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleUninstallRepo_AmbiguousBasenameRequiresFullPath(t *testing.T) {
|
||||
s, src := newTestServer(t)
|
||||
addTrackedRepo(t, src, filepath.Join("org", "_team-skills"))
|
||||
addTrackedRepo(t, src, filepath.Join("dept", "_team-skills"))
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/api/repos/team-skills", nil)
|
||||
req.SetPathValue("name", "team-skills")
|
||||
rr := httptest.NewRecorder()
|
||||
s.handleUninstallRepo(rr, req)
|
||||
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
if !strings.Contains(rr.Body.String(), "multiple tracked repositories match") {
|
||||
t.Fatalf("expected ambiguous repo error, got %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleUninstallRepo_RejectsPathTraversal(t *testing.T) {
|
||||
s, _ := newTestServer(t)
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/api/repos/../../evil", nil)
|
||||
req.SetPathValue("name", "../evil")
|
||||
rr := httptest.NewRecorder()
|
||||
s.handleUninstallRepo(rr, req)
|
||||
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
if !strings.Contains(rr.Body.String(), "invalid or missing tracked repository name") {
|
||||
t.Fatalf("expected invalid name error, got %s", rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user