mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Boost unit test coverage across three core packages: - internal/sync: 11.6% → 54.6% (discovery, status, checksum, manifest, pull, merge/prune) - internal/install: 40.7% → 60.0% (meta, ignore, copy, updatable, local install, options, gitignore, source) - internal/server: 15.1% → 40.0% (health, overview, skills, targets, config, log, trash, sync, collect, check, backup, diff, audit, hub)
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestHandleGetAuditRules_NotExist(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/audit/rules", 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 {
|
|
Exists bool `json:"exists"`
|
|
Raw string `json:"raw"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
if resp.Exists {
|
|
t.Error("expected exists false for non-existent rules")
|
|
}
|
|
}
|
|
|
|
func TestHandleInitAuditRules(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
req := httptest.NewRequest(http.MethodPost, "/api/audit/rules", 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 {
|
|
Success bool `json:"success"`
|
|
}
|
|
json.Unmarshal(rr.Body.Bytes(), &resp)
|
|
if !resp.Success {
|
|
t.Error("expected success true")
|
|
}
|
|
|
|
// Verify rules now exist
|
|
req2 := httptest.NewRequest(http.MethodGet, "/api/audit/rules", nil)
|
|
rr2 := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr2, req2)
|
|
|
|
var resp2 struct {
|
|
Exists bool `json:"exists"`
|
|
}
|
|
json.Unmarshal(rr2.Body.Bytes(), &resp2)
|
|
if !resp2.Exists {
|
|
t.Error("expected rules to exist after init")
|
|
}
|
|
}
|
|
|
|
func TestHandleInitAuditRules_AlreadyExists(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
// Init once
|
|
req := httptest.NewRequest(http.MethodPost, "/api/audit/rules", nil)
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("first init failed: %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
// Init again — should conflict
|
|
req2 := httptest.NewRequest(http.MethodPost, "/api/audit/rules", nil)
|
|
rr2 := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr2, req2)
|
|
|
|
if rr2.Code != http.StatusConflict {
|
|
t.Errorf("expected 409 conflict, got %d: %s", rr2.Code, rr2.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandlePutAuditRules_InvalidYAML(t *testing.T) {
|
|
s, _ := newTestServer(t)
|
|
body := `{"raw":"not: [valid yaml rules"}`
|
|
req := httptest.NewRequest(http.MethodPut, "/api/audit/rules", strings.NewReader(body))
|
|
rr := httptest.NewRecorder()
|
|
s.handler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusBadRequest {
|
|
t.Errorf("expected 400, got %d: %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|