Files
pawsql/internal/adminui/handler_test.go
Shaun Campbell 7d7133d6cc
All checks were successful
Build and Push Image / docker-build-and-push (push) Successful in 6m32s
Test and Release PawSQL / test (push) Successful in 50s
Test and Release PawSQL / release (push) Successful in 8s
feat: ship Barkstack UI plugin
2026-09-16 13:47:07 -04:00

63 lines
2.1 KiB
Go

package adminui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestManifestEndpointDescribesPawSQLPlugin(t *testing.T) {
response := httptest.NewRecorder()
Handler(nil).ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/barkstack/ui/manifest.json", nil))
if response.Code != http.StatusOK {
t.Fatalf("status = %d", response.Code)
}
if got := response.Header().Get("Content-Type"); got != "application/json" {
t.Fatalf("content type = %q", got)
}
var got manifest
if err := json.Unmarshal(response.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if got.APIVersion != "barkstack.dev/ui/v1" || got.ID != "pawsql" || got.Entry != entryPath || got.Element != "barkstack-pawsql" {
t.Fatalf("manifest = %+v", got)
}
}
func TestEmbeddedEntryAssetIsServedWithJavaScriptContentType(t *testing.T) {
response := httptest.NewRecorder()
Handler(nil).ServeHTTP(response, httptest.NewRequest(http.MethodGet, entryPath, nil))
if response.Code != http.StatusOK {
t.Fatalf("status = %d", response.Code)
}
if got := response.Header().Get("Content-Type"); !strings.Contains(got, "javascript") {
t.Fatalf("content type = %q", got)
}
if response.Body.Len() == 0 {
t.Fatal("embedded entry asset is empty")
}
}
func TestStatusEndpointReportsConfiguredRoutes(t *testing.T) {
routes := []RouteSummary{{Name: "reporting", Hostname: "reports.example.com", Managed: true}}
response := httptest.NewRecorder()
Handler(routes).ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/barkstack/ui/status.json", nil))
var got statusResponse
if err := json.Unmarshal(response.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if got.Status != "running" || len(got.Routes) != 1 || got.Routes[0] != routes[0] {
t.Fatalf("status response = %+v", got)
}
}
func TestUnknownAssetReturnsNotFound(t *testing.T) {
response := httptest.NewRecorder()
Handler(nil).ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/barkstack/ui/assets/missing.js", nil))
if response.Code != http.StatusNotFound {
t.Fatalf("status = %d, want 404", response.Code)
}
}