103 lines
2.5 KiB
Go
103 lines
2.5 KiB
Go
package webui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"bee/audit/internal/app"
|
|
)
|
|
|
|
func TestXrandrCommandAddsDefaultX11Env(t *testing.T) {
|
|
t.Setenv("DISPLAY", "")
|
|
t.Setenv("XAUTHORITY", "")
|
|
|
|
cmd := xrandrCommand("--query")
|
|
|
|
var hasDisplay bool
|
|
var hasXAuthority bool
|
|
for _, kv := range cmd.Env {
|
|
if kv == "DISPLAY=:0" {
|
|
hasDisplay = true
|
|
}
|
|
if kv == "XAUTHORITY=/home/bee/.Xauthority" {
|
|
hasXAuthority = true
|
|
}
|
|
}
|
|
if !hasDisplay {
|
|
t.Fatalf("DISPLAY not injected: %v", cmd.Env)
|
|
}
|
|
if !hasXAuthority {
|
|
t.Fatalf("XAUTHORITY not injected: %v", cmd.Env)
|
|
}
|
|
}
|
|
|
|
func TestHandleAPISATRunDecodesBodyWithoutContentLength(t *testing.T) {
|
|
globalQueue.mu.Lock()
|
|
originalTasks := globalQueue.tasks
|
|
globalQueue.tasks = nil
|
|
globalQueue.mu.Unlock()
|
|
t.Cleanup(func() {
|
|
globalQueue.mu.Lock()
|
|
globalQueue.tasks = originalTasks
|
|
globalQueue.mu.Unlock()
|
|
})
|
|
|
|
h := &handler{opts: HandlerOptions{App: &app.App{}}}
|
|
req := httptest.NewRequest("POST", "/api/sat/cpu/run", strings.NewReader(`{"profile":"smoke"}`))
|
|
req.ContentLength = -1
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.handleAPISATRun("cpu").ServeHTTP(rec, req)
|
|
|
|
if rec.Code != 200 {
|
|
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
globalQueue.mu.Lock()
|
|
defer globalQueue.mu.Unlock()
|
|
if len(globalQueue.tasks) != 1 {
|
|
t.Fatalf("tasks=%d want 1", len(globalQueue.tasks))
|
|
}
|
|
if got := globalQueue.tasks[0].params.BurnProfile; got != "smoke" {
|
|
t.Fatalf("burn profile=%q want smoke", got)
|
|
}
|
|
}
|
|
|
|
func TestHandleAPIExportBundleQueuesTask(t *testing.T) {
|
|
globalQueue.mu.Lock()
|
|
originalTasks := globalQueue.tasks
|
|
globalQueue.tasks = nil
|
|
globalQueue.mu.Unlock()
|
|
t.Cleanup(func() {
|
|
globalQueue.mu.Lock()
|
|
globalQueue.tasks = originalTasks
|
|
globalQueue.mu.Unlock()
|
|
})
|
|
|
|
h := &handler{opts: HandlerOptions{ExportDir: t.TempDir()}}
|
|
req := httptest.NewRequest("POST", "/api/export/bundle", nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.handleAPIExportBundle(rec, req)
|
|
|
|
if rec.Code != 200 {
|
|
t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
var body map[string]string
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
if body["task_id"] == "" {
|
|
t.Fatalf("missing task_id in response: %v", body)
|
|
}
|
|
globalQueue.mu.Lock()
|
|
defer globalQueue.mu.Unlock()
|
|
if len(globalQueue.tasks) != 1 {
|
|
t.Fatalf("tasks=%d want 1", len(globalQueue.tasks))
|
|
}
|
|
if got := globalQueue.tasks[0].Target; got != "support-bundle" {
|
|
t.Fatalf("target=%q want support-bundle", got)
|
|
}
|
|
}
|