forked from longtao.wu/droboticflow
396 lines
14 KiB
Go
396 lines
14 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
// ============================================================================
|
|
// Challenger M5 Empirical & Adversarial Verification Test Suite
|
|
// ============================================================================
|
|
|
|
func TestChallengerM5_BackendModeFilteringAndPagination(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
s := &server{store: dataStore}
|
|
|
|
adminPrincipal := &authenticatedPrincipal{
|
|
User: domain.UserRecord{
|
|
ID: "admin-m5",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
Role: domain.UserRoleAdmin,
|
|
Name: "Admin User",
|
|
},
|
|
Organization: domain.OrganizationRecord{
|
|
ID: domain.DefaultOrganizationID,
|
|
Status: domain.OrganizationStatusActive,
|
|
},
|
|
}
|
|
_ = dataStore.UpsertUser(ctx, adminPrincipal.User)
|
|
|
|
baseTime := time.Date(2026, 8, 20, 8, 0, 0, 0, time.UTC)
|
|
|
|
// Seed 10 runs across 2 projects in default organization:
|
|
// 4 production runs, 4 debug runs, 2 legacy runs (mode "")
|
|
runs := []domain.RunRecord{
|
|
// Project A
|
|
{
|
|
ID: "run-prod-a1",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-a",
|
|
WorkflowID: "wf-a",
|
|
Mode: domain.RunModeProduction,
|
|
ReleaseID: "rel-1",
|
|
WorkflowVersion: 1,
|
|
Status: domain.RunStatusCompleted,
|
|
CreatedAt: baseTime.Add(1 * time.Minute).Format(time.RFC3339Nano),
|
|
},
|
|
{
|
|
ID: "run-prod-a2",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-a",
|
|
WorkflowID: "wf-a",
|
|
Mode: domain.RunModeProduction,
|
|
ReleaseID: "rel-1",
|
|
WorkflowVersion: 1,
|
|
Status: domain.RunStatusRunning,
|
|
CreatedAt: baseTime.Add(2 * time.Minute).Format(time.RFC3339Nano),
|
|
},
|
|
{
|
|
ID: "run-debug-a1",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-a",
|
|
WorkflowID: "wf-a",
|
|
Mode: domain.RunModeDebug,
|
|
Status: domain.RunStatusRunning,
|
|
CreatedAt: baseTime.Add(3 * time.Minute).Format(time.RFC3339Nano),
|
|
},
|
|
{
|
|
ID: "run-debug-a2",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-a",
|
|
WorkflowID: "wf-a",
|
|
Mode: domain.RunModeDebug,
|
|
Status: domain.RunStatusFailed,
|
|
CreatedAt: baseTime.Add(4 * time.Minute).Format(time.RFC3339Nano),
|
|
},
|
|
{
|
|
ID: "run-legacy-a1",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-a",
|
|
WorkflowID: "wf-a",
|
|
Mode: "",
|
|
Status: domain.RunStatusCompleted,
|
|
CreatedAt: baseTime.Add(5 * time.Minute).Format(time.RFC3339Nano),
|
|
},
|
|
|
|
// Project B
|
|
{
|
|
ID: "run-prod-b1",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-b",
|
|
WorkflowID: "wf-b",
|
|
Mode: domain.RunModeProduction,
|
|
ReleaseID: "rel-2",
|
|
WorkflowVersion: 2,
|
|
Status: domain.RunStatusCompleted,
|
|
CreatedAt: baseTime.Add(6 * time.Minute).Format(time.RFC3339Nano),
|
|
},
|
|
{
|
|
ID: "run-prod-b2",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-b",
|
|
WorkflowID: "wf-b",
|
|
Mode: domain.RunModeProduction,
|
|
ReleaseID: "rel-2",
|
|
WorkflowVersion: 2,
|
|
Status: domain.RunStatusFailed,
|
|
CreatedAt: baseTime.Add(7 * time.Minute).Format(time.RFC3339Nano),
|
|
},
|
|
{
|
|
ID: "run-debug-b1",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-b",
|
|
WorkflowID: "wf-b",
|
|
Mode: domain.RunModeDebug,
|
|
Status: domain.RunStatusCompleted,
|
|
CreatedAt: baseTime.Add(8 * time.Minute).Format(time.RFC3339Nano),
|
|
},
|
|
{
|
|
ID: "run-debug-b2",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-b",
|
|
WorkflowID: "wf-b",
|
|
Mode: domain.RunModeDebug,
|
|
Status: domain.RunStatusRunning,
|
|
CreatedAt: baseTime.Add(9 * time.Minute).Format(time.RFC3339Nano),
|
|
},
|
|
{
|
|
ID: "run-legacy-b1",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: "proj-b",
|
|
WorkflowID: "wf-b",
|
|
Mode: "",
|
|
Status: domain.RunStatusCancelled,
|
|
CreatedAt: baseTime.Add(10 * time.Minute).Format(time.RFC3339Nano),
|
|
},
|
|
}
|
|
|
|
for _, r := range runs {
|
|
if err := dataStore.UpsertRun(ctx, r); err != nil {
|
|
t.Fatalf("failed to seed run %s: %v", r.ID, err)
|
|
}
|
|
}
|
|
|
|
type pageResponse struct {
|
|
Items []domain.RunSummary `json:"items"`
|
|
Total int `json:"total"`
|
|
StatusCounts map[domain.RunStatus]int `json:"statusCounts"`
|
|
NextCursor string `json:"nextCursor"`
|
|
HasMore bool `json:"hasMore"`
|
|
}
|
|
|
|
type errResponse struct {
|
|
Error string `json:"error"`
|
|
Issues []validationIssue `json:"issues"`
|
|
}
|
|
|
|
doGetRuns := func(t *testing.T, queryString string, expectedStatusCode int) (*pageResponse, *errResponse) {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/runs"+queryString, nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), authContextKey{}, adminPrincipal))
|
|
rec := httptest.NewRecorder()
|
|
|
|
s.handleRunsList(rec, req)
|
|
|
|
if rec.Code != expectedStatusCode {
|
|
t.Fatalf("GET /api/runs%s expected status %d, got %d. Body: %s", queryString, expectedStatusCode, rec.Code, rec.Body.String())
|
|
}
|
|
|
|
if expectedStatusCode == http.StatusOK {
|
|
var resp pageResponse
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("failed to decode response JSON: %v", err)
|
|
}
|
|
return &resp, nil
|
|
} else {
|
|
var errResp errResponse
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &errResp); err != nil {
|
|
t.Fatalf("failed to decode error JSON: %v", err)
|
|
}
|
|
return nil, &errResp
|
|
}
|
|
}
|
|
|
|
// 1. Filter mode=production -> returns 4 explicit production runs + 2 legacy production runs = 6 runs
|
|
t.Run("Filter mode=production", func(t *testing.T) {
|
|
page, _ := doGetRuns(t, "?mode=production", http.StatusOK)
|
|
if page.Total != 6 {
|
|
t.Errorf("expected total 6 production runs (including legacy), got %d", page.Total)
|
|
}
|
|
if len(page.Items) != 6 {
|
|
t.Fatalf("expected 6 items, got %d", len(page.Items))
|
|
}
|
|
for _, item := range page.Items {
|
|
if item.Mode != domain.RunModeProduction && item.Mode != "" {
|
|
t.Errorf("run %s has unexpected mode %q, expected production or legacy", item.ID, item.Mode)
|
|
}
|
|
}
|
|
// Status counts:
|
|
// Completed: prod-a1, legacy-a1, prod-b1 -> 3
|
|
// Running: prod-a2 -> 1
|
|
// Failed: prod-b2 -> 1
|
|
// Cancelled: legacy-b1 -> 1
|
|
if page.StatusCounts[domain.RunStatusCompleted] != 3 {
|
|
t.Errorf("expected 3 completed production runs in statusCounts, got %d", page.StatusCounts[domain.RunStatusCompleted])
|
|
}
|
|
if page.StatusCounts[domain.RunStatusRunning] != 1 {
|
|
t.Errorf("expected 1 running production run in statusCounts, got %d", page.StatusCounts[domain.RunStatusRunning])
|
|
}
|
|
if page.StatusCounts[domain.RunStatusFailed] != 1 {
|
|
t.Errorf("expected 1 failed production run in statusCounts, got %d", page.StatusCounts[domain.RunStatusFailed])
|
|
}
|
|
})
|
|
|
|
// 2. Filter mode=debug -> must return strictly the 4 debug runs
|
|
t.Run("Filter mode=debug", func(t *testing.T) {
|
|
page, _ := doGetRuns(t, "?mode=debug", http.StatusOK)
|
|
if page.Total != 4 {
|
|
t.Errorf("expected total 4 debug runs, got %d", page.Total)
|
|
}
|
|
if len(page.Items) != 4 {
|
|
t.Fatalf("expected 4 items, got %d", len(page.Items))
|
|
}
|
|
for _, item := range page.Items {
|
|
if item.Mode != domain.RunModeDebug {
|
|
t.Errorf("run %s has unexpected mode %q, expected %q", item.ID, item.Mode, domain.RunModeDebug)
|
|
}
|
|
}
|
|
if page.StatusCounts[domain.RunStatusRunning] != 2 {
|
|
t.Errorf("expected 2 running debug runs in statusCounts, got %d", page.StatusCounts[domain.RunStatusRunning])
|
|
}
|
|
if page.StatusCounts[domain.RunStatusCompleted] != 1 {
|
|
t.Errorf("expected 1 completed debug run in statusCounts, got %d", page.StatusCounts[domain.RunStatusCompleted])
|
|
}
|
|
if page.StatusCounts[domain.RunStatusFailed] != 1 {
|
|
t.Errorf("expected 1 failed debug run in statusCounts, got %d", page.StatusCounts[domain.RunStatusFailed])
|
|
}
|
|
})
|
|
|
|
// 3. Filter mode=all -> must return all 10 runs
|
|
t.Run("Filter mode=all", func(t *testing.T) {
|
|
page, _ := doGetRuns(t, "?mode=all", http.StatusOK)
|
|
if page.Total != 10 {
|
|
t.Errorf("expected total 10 runs under mode=all, got %d", page.Total)
|
|
}
|
|
if len(page.Items) != 10 {
|
|
t.Fatalf("expected 10 items, got %d", len(page.Items))
|
|
}
|
|
})
|
|
|
|
// 4. Combined filter: mode=production & projectId=proj-a (2 prod + 1 legacy = 3)
|
|
t.Run("Combined mode=production & projectId=proj-a", func(t *testing.T) {
|
|
page, _ := doGetRuns(t, "?mode=production&projectId=proj-a", http.StatusOK)
|
|
if page.Total != 3 {
|
|
t.Errorf("expected total 3 production runs for proj-a, got %d", page.Total)
|
|
}
|
|
if len(page.Items) != 3 {
|
|
t.Fatalf("expected 3 items, got %d", len(page.Items))
|
|
}
|
|
for _, item := range page.Items {
|
|
if item.ProjectID != "proj-a" || (item.Mode != domain.RunModeProduction && item.Mode != "") {
|
|
t.Errorf("unexpected run %s (project: %s, mode: %s)", item.ID, item.ProjectID, item.Mode)
|
|
}
|
|
}
|
|
})
|
|
|
|
// 5. Combined filter: mode=debug & projectId=proj-a (2 debug)
|
|
t.Run("Combined mode=debug & projectId=proj-a", func(t *testing.T) {
|
|
page, _ := doGetRuns(t, "?mode=debug&projectId=proj-a", http.StatusOK)
|
|
if page.Total != 2 {
|
|
t.Errorf("expected total 2 debug runs for proj-a, got %d", page.Total)
|
|
}
|
|
if len(page.Items) != 2 {
|
|
t.Fatalf("expected 2 items, got %d", len(page.Items))
|
|
}
|
|
for _, item := range page.Items {
|
|
if item.ProjectID != "proj-a" || item.Mode != domain.RunModeDebug {
|
|
t.Errorf("unexpected run %s (project: %s, mode: %s)", item.ID, item.ProjectID, item.Mode)
|
|
}
|
|
}
|
|
})
|
|
|
|
// 6. Invalid mode string -> must return HTTP 400 with validation issue
|
|
t.Run("Invalid mode parameter rejects with 400", func(t *testing.T) {
|
|
_, errResp := doGetRuns(t, "?mode=invalid_mode_val", http.StatusBadRequest)
|
|
foundModeIssue := false
|
|
for _, issue := range errResp.Issues {
|
|
if issue.Field == "mode" {
|
|
foundModeIssue = true
|
|
}
|
|
}
|
|
if !foundModeIssue {
|
|
t.Errorf("expected validation issue on 'mode' field, got %v", errResp.Issues)
|
|
}
|
|
})
|
|
|
|
// 7. Pagination under mode=debug (limit=2, total=4, traversing nextCursor across 2 pages)
|
|
t.Run("Pagination traversal under mode=debug", func(t *testing.T) {
|
|
page1, _ := doGetRuns(t, "?mode=debug&limit=2", http.StatusOK)
|
|
if len(page1.Items) != 2 {
|
|
t.Fatalf("page 1 expected 2 items, got %d", len(page1.Items))
|
|
}
|
|
if !page1.HasMore || page1.NextCursor == "" {
|
|
t.Fatalf("page 1 expected hasMore=true and nextCursor, got hasMore=%v cursor=%q", page1.HasMore, page1.NextCursor)
|
|
}
|
|
|
|
page2, _ := doGetRuns(t, fmt.Sprintf("?mode=debug&limit=2&cursor=%s", page1.NextCursor), http.StatusOK)
|
|
if len(page2.Items) != 2 {
|
|
t.Fatalf("page 2 expected 2 items, got %d", len(page2.Items))
|
|
}
|
|
if page2.HasMore {
|
|
t.Errorf("page 2 expected hasMore=false, got true")
|
|
}
|
|
|
|
// Ensure all 4 retrieved items are distinct and debug runs
|
|
seen := make(map[string]bool)
|
|
for _, item := range append(page1.Items, page2.Items...) {
|
|
if seen[item.ID] {
|
|
t.Errorf("duplicate item %s found across pages", item.ID)
|
|
}
|
|
seen[item.ID] = true
|
|
if item.Mode != domain.RunModeDebug {
|
|
t.Errorf("run %s has unexpected mode %q", item.ID, item.Mode)
|
|
}
|
|
}
|
|
if len(seen) != 4 {
|
|
t.Errorf("expected 4 unique items traversed, got %d", len(seen))
|
|
}
|
|
})
|
|
|
|
// 8. Multi-tenant isolation under mode query
|
|
t.Run("Tenant isolation under mode=production and mode=debug", func(t *testing.T) {
|
|
tenantBPrincipal := &authenticatedPrincipal{
|
|
User: domain.UserRecord{
|
|
ID: "user-org-beta",
|
|
OrganizationID: "org-beta",
|
|
Role: domain.UserRoleAdmin,
|
|
Name: "Beta User",
|
|
},
|
|
Organization: domain.OrganizationRecord{
|
|
ID: "org-beta",
|
|
Status: domain.OrganizationStatusActive,
|
|
},
|
|
}
|
|
_ = dataStore.UpsertUser(ctx, tenantBPrincipal.User)
|
|
|
|
// Seed 1 production run and 1 debug run in org-beta
|
|
_ = dataStore.UpsertRun(ctx, domain.RunRecord{
|
|
ID: "run-beta-prod",
|
|
OrganizationID: "org-beta",
|
|
ProjectID: "proj-beta",
|
|
WorkflowID: "wf-beta",
|
|
Mode: domain.RunModeProduction,
|
|
Status: domain.RunStatusRunning,
|
|
CreatedAt: baseTime.Add(15 * time.Minute).Format(time.RFC3339Nano),
|
|
})
|
|
_ = dataStore.UpsertRun(ctx, domain.RunRecord{
|
|
ID: "run-beta-debug",
|
|
OrganizationID: "org-beta",
|
|
ProjectID: "proj-beta",
|
|
WorkflowID: "wf-beta",
|
|
Mode: domain.RunModeDebug,
|
|
Status: domain.RunStatusCompleted,
|
|
CreatedAt: baseTime.Add(16 * time.Minute).Format(time.RFC3339Nano),
|
|
})
|
|
|
|
// Query org-beta as tenantB:
|
|
req := httptest.NewRequest(http.MethodGet, "/api/runs?mode=production", nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), authContextKey{}, tenantBPrincipal))
|
|
rec := httptest.NewRecorder()
|
|
s.handleRunsList(rec, req)
|
|
|
|
var betaPage pageResponse
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &betaPage)
|
|
if betaPage.Total != 1 || len(betaPage.Items) != 1 || betaPage.Items[0].ID != "run-beta-prod" {
|
|
t.Errorf("expected exactly 1 production run for org-beta, got total=%d items=%v", betaPage.Total, betaPage.Items)
|
|
}
|
|
|
|
// Query default organization again to ensure org-beta runs are NOT present in default org's results
|
|
pageDef, _ := doGetRuns(t, "?mode=production", http.StatusOK)
|
|
for _, item := range pageDef.Items {
|
|
if item.OrganizationID == "org-beta" {
|
|
t.Errorf("tenant isolation leak: found org-beta run %s in default org query", item.ID)
|
|
}
|
|
}
|
|
})
|
|
}
|