Revision tests (8): - List, get, create, update status/labels, compare, rollback - Not-found paths for missing items and revisions Schema tests (4): - List schemas, get by name, form descriptor, not-found Audit tests (4): - Completeness summary (empty + with items), item detail, not-found Auth tests (4): - Get current user (authenticated + unauthenticated) - Auth config response - Token lifecycle: create, list, revoke
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func newAuditRouter(s *Server) http.Handler {
|
|
r := chi.NewRouter()
|
|
r.Get("/api/audit/completeness", s.HandleAuditCompleteness)
|
|
r.Get("/api/audit/completeness/{partNumber}", s.HandleAuditItemDetail)
|
|
return r
|
|
}
|
|
|
|
func TestHandleAuditCompletenessEmpty(t *testing.T) {
|
|
s := newTestServerWithSchemas(t)
|
|
router := newAuditRouter(s)
|
|
|
|
req := httptest.NewRequest("GET", "/api/audit/completeness", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status: got %d, want %d; body: %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandleAuditCompleteness(t *testing.T) {
|
|
s := newTestServerWithSchemas(t)
|
|
router := newAuditRouter(s)
|
|
|
|
createItemDirect(t, s, "AUD-001", "audit item 1", nil)
|
|
createItemDirect(t, s, "AUD-002", "audit item 2", nil)
|
|
|
|
req := httptest.NewRequest("GET", "/api/audit/completeness", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status: got %d, want %d; body: %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
|
|
var resp map[string]any
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decoding response: %v", err)
|
|
}
|
|
// Should have items array
|
|
items, ok := resp["items"]
|
|
if !ok {
|
|
t.Fatal("response missing 'items' key")
|
|
}
|
|
itemList, ok := items.([]any)
|
|
if !ok {
|
|
t.Fatal("'items' is not an array")
|
|
}
|
|
if len(itemList) < 2 {
|
|
t.Errorf("expected at least 2 audit items, got %d", len(itemList))
|
|
}
|
|
}
|
|
|
|
func TestHandleAuditItemDetail(t *testing.T) {
|
|
s := newTestServerWithSchemas(t)
|
|
router := newAuditRouter(s)
|
|
|
|
cost := 50.0
|
|
createItemDirect(t, s, "AUDDET-001", "audit detail item", &cost)
|
|
|
|
req := httptest.NewRequest("GET", "/api/audit/completeness/AUDDET-001", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status: got %d, want %d; body: %s", w.Code, http.StatusOK, w.Body.String())
|
|
}
|
|
|
|
var resp map[string]any
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decoding response: %v", err)
|
|
}
|
|
if resp["part_number"] != "AUDDET-001" {
|
|
t.Errorf("part_number: got %v, want %q", resp["part_number"], "AUDDET-001")
|
|
}
|
|
if _, ok := resp["score"]; !ok {
|
|
t.Error("response missing 'score' field")
|
|
}
|
|
if _, ok := resp["tier"]; !ok {
|
|
t.Error("response missing 'tier' field")
|
|
}
|
|
}
|
|
|
|
func TestHandleAuditItemDetailNotFound(t *testing.T) {
|
|
s := newTestServerWithSchemas(t)
|
|
router := newAuditRouter(s)
|
|
|
|
req := httptest.NewRequest("GET", "/api/audit/completeness/NOPE-999", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
t.Errorf("status: got %d, want %d", w.Code, http.StatusNotFound)
|
|
}
|
|
}
|