mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-02-18 23:41:48 +01:00
New test files with expanded coverage: API tests: - ai_handler_test.go: AI handler unit tests with mocking - agent_profiles_tools_test.go: Profile management tests - alerts_endpoints_test.go: Alert API endpoint tests - alerts_test.go: Updated for interface changes - audit_handlers_test.go: Audit handler tests - frontend_embed_test.go: Frontend embedding tests - metadata_handlers_test.go, metadata_provider_test.go: Metadata tests - notifications_test.go: Updated for interface changes - profile_suggestions_test.go: Profile suggestion tests - saml_service_test.go: SAML authentication tests - sensor_proxy_gate_test.go: Sensor proxy tests - updates_test.go: Updated for interface changes Agent tests: - dockeragent/signature_test.go: Docker agent signature tests - hostagent/agent_metrics_test.go: Host agent metrics tests - hostagent/commands_test.go: Command execution tests - hostagent/network_helpers_test.go: Network helper tests - hostagent/proxmox_setup_test.go: Updated setup tests - kubernetesagent/*_test.go: Kubernetes agent tests Core package tests: - monitoring/kubernetes_agents_test.go, reload_test.go - remoteconfig/client_test.go, signature_test.go - sensors/collector_test.go - updates/adapter_installsh_*_test.go: Install adapter tests - updates/manager_*_test.go: Update manager tests - websocket/hub_*_test.go: WebSocket hub tests Library tests: - pkg/audit/export_test.go: Audit export tests - pkg/metrics/store_test.go: Metrics store tests - pkg/proxmox/*_test.go: Proxmox client tests - pkg/reporting/reporting_test.go: Reporting tests - pkg/server/*_test.go: Server tests - pkg/tlsutil/extra_test.go: TLS utility tests Total: ~8000 lines of new test code
98 lines
2.4 KiB
Go
98 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func resetDevProxy() {
|
|
devProxyOnce = sync.Once{}
|
|
devProxy = nil
|
|
devProxyErr = nil
|
|
}
|
|
|
|
func writeFile(t *testing.T, dir, name, content string) {
|
|
t.Helper()
|
|
path := filepath.Join(dir, name)
|
|
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
|
t.Fatalf("write file: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestGetFrontendFSOverride(t *testing.T) {
|
|
resetDevProxy()
|
|
dir := t.TempDir()
|
|
writeFile(t, dir, "index.html", "<html>ok</html>")
|
|
t.Setenv("PULSE_FRONTEND_DIR", dir)
|
|
|
|
fsys, err := getFrontendFS()
|
|
if err != nil {
|
|
t.Fatalf("getFrontendFS error: %v", err)
|
|
}
|
|
|
|
f, err := fsys.Open("index.html")
|
|
if err != nil {
|
|
t.Fatalf("open index: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
data, err := io.ReadAll(f)
|
|
if err != nil {
|
|
t.Fatalf("read index: %v", err)
|
|
}
|
|
if string(data) != "<html>ok</html>" {
|
|
t.Fatalf("unexpected content: %s", string(data))
|
|
}
|
|
}
|
|
|
|
func TestServeFrontendHandler(t *testing.T) {
|
|
resetDevProxy()
|
|
dir := t.TempDir()
|
|
writeFile(t, dir, "index.html", "<html>index</html>")
|
|
writeFile(t, dir, "app-123.js", "console.log('ok');")
|
|
t.Setenv("PULSE_FRONTEND_DIR", dir)
|
|
t.Setenv("FRONTEND_DEV_SERVER", "")
|
|
|
|
handler := serveFrontendHandler()
|
|
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
handler(rec, req)
|
|
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "index") {
|
|
t.Fatalf("unexpected root response: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
if rec.Header().Get("Cache-Control") == "" {
|
|
t.Fatal("expected cache headers for index")
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodGet, "/app-123.js", nil)
|
|
handler(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("unexpected asset response: %d", rec.Code)
|
|
}
|
|
if !strings.Contains(rec.Header().Get("Cache-Control"), "immutable") {
|
|
t.Fatalf("expected immutable cache header, got %s", rec.Header().Get("Cache-Control"))
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodGet, "/missing", nil)
|
|
handler(rec, req)
|
|
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "index") {
|
|
t.Fatalf("expected SPA fallback, got %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodGet, "/api/test", nil)
|
|
handler(rec, req)
|
|
if rec.Code != http.StatusNotFound {
|
|
t.Fatalf("expected 404 for api path, got %d", rec.Code)
|
|
}
|
|
}
|