forked from longtao.wu/droboticflow
7169 lines
274 KiB
Go
7169 lines
274 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/config"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
|
|
bosapi "github.com/baidubce/bce-sdk-go/services/bos/api"
|
|
)
|
|
|
|
func TestListStorageObjectsFromConnectionNormalizesDirectoryPrefixes(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
prefix string
|
|
expected string
|
|
}{
|
|
{name: "empty", prefix: "", expected: ""},
|
|
{name: "trimmed root child", prefix: "algo", expected: "algo/"},
|
|
{name: "preserve nested directory", prefix: "algo/X-platform-test", expected: "algo/X-platform-test/"},
|
|
{name: "trim slashes", prefix: "/algo/X-platform-test//", expected: "algo/X-platform-test/"},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
requestPrefix := storageObjectRequestPrefix(test.prefix)
|
|
if requestPrefix != test.expected {
|
|
t.Fatalf("request prefix = %q, want %q", requestPrefix, test.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAppendStorageObjectItemsSkipsCurrentPrefixAndDedupesPages(t *testing.T) {
|
|
collected := make(map[string]struct{})
|
|
appendStorageObjectItems(
|
|
collected,
|
|
&bosapi.ListObjectsResult{
|
|
CommonPrefixes: []bosapi.PrefixType{
|
|
{Prefix: "algo/"},
|
|
{Prefix: "algo/X-platform-test/"},
|
|
},
|
|
Contents: []bosapi.ObjectSummaryType{
|
|
{Key: "algo/"},
|
|
{Key: "algo/manifest.json"},
|
|
},
|
|
},
|
|
"algo",
|
|
"algo/",
|
|
)
|
|
appendStorageObjectItems(
|
|
collected,
|
|
&bosapi.ListObjectsResult{
|
|
CommonPrefixes: []bosapi.PrefixType{
|
|
{Prefix: "algo/X-platform-test/"},
|
|
{Prefix: "algo/raw/"},
|
|
},
|
|
Contents: []bosapi.ObjectSummaryType{
|
|
{Key: "algo/manifest.json"},
|
|
{Key: "algo/video.mp4"},
|
|
},
|
|
},
|
|
"algo",
|
|
"algo/",
|
|
)
|
|
|
|
items := make([]string, 0, len(collected))
|
|
for item := range collected {
|
|
items = append(items, item)
|
|
}
|
|
sort.Strings(items)
|
|
|
|
expected := []string{
|
|
"algo/X-platform-test/",
|
|
"algo/manifest.json",
|
|
"algo/raw/",
|
|
"algo/video.mp4",
|
|
}
|
|
if strings.Join(items, "|") != strings.Join(expected, "|") {
|
|
t.Fatalf("items = %#v, want %#v", items, expected)
|
|
}
|
|
}
|
|
|
|
func TestAPITokenAuthProtectsBusinessEndpointsWhenConfigured(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_API_TOKEN", "test-token")
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
|
|
healthRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(healthRecorder, httptest.NewRequest(http.MethodGet, "/api/health", nil))
|
|
if healthRecorder.Code != http.StatusOK {
|
|
t.Fatalf("expected health endpoint to remain public, got %d", healthRecorder.Code)
|
|
}
|
|
|
|
unauthorizedRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(unauthorizedRecorder, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil))
|
|
if unauthorizedRecorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected dashboard without token to be rejected, got %d", unauthorizedRecorder.Code)
|
|
}
|
|
|
|
authorizedRequest := httptest.NewRequest(http.MethodGet, "/api/dashboard", nil)
|
|
authorizedRequest.Header.Set("Authorization", "Bearer test-token")
|
|
authorizedRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(authorizedRecorder, authorizedRequest)
|
|
if authorizedRecorder.Code != http.StatusOK {
|
|
t.Fatalf("expected dashboard with token to succeed, got %d body=%s", authorizedRecorder.Code, authorizedRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestNewHandlerReportsInvalidConfiguration(t *testing.T) {
|
|
t.Setenv("PORT", "invalid")
|
|
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/health", nil))
|
|
|
|
if recorder.Code != http.StatusInternalServerError {
|
|
t.Fatalf("expected invalid configuration to return 500, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "PORT must be an integer") {
|
|
t.Fatalf("expected invalid PORT error, got %s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func sessionCookieFromRecorder(t *testing.T, recorder *httptest.ResponseRecorder) *http.Cookie {
|
|
t.Helper()
|
|
cookies := recorder.Result().Cookies()
|
|
for _, cookie := range cookies {
|
|
if cookie.Name == sessionCookieName {
|
|
if !cookie.HttpOnly {
|
|
t.Fatalf("expected HttpOnly session cookie, got %#v", cookie)
|
|
}
|
|
return cookie
|
|
}
|
|
}
|
|
t.Fatalf("expected session cookie, got %#v", cookies)
|
|
return nil
|
|
}
|
|
|
|
func serveJSONWithCookie(handler http.Handler, method string, path string, body []byte, cookie *http.Cookie) *httptest.ResponseRecorder {
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(method, path, bytes.NewReader(body))
|
|
if cookie != nil {
|
|
request.AddCookie(cookie)
|
|
if isCSRFProtectedMethod(method) && !isCSRFExemptEndpoint(request) {
|
|
request.AddCookie(&http.Cookie{Name: csrfCookieName, Value: "test-csrf-token"})
|
|
request.Header.Set(csrfHeaderName, "test-csrf-token")
|
|
}
|
|
}
|
|
handler.ServeHTTP(recorder, request)
|
|
return recorder
|
|
}
|
|
|
|
func newHandlerWithWorkerAPIBaseURL(t *testing.T, workerURL string) http.Handler {
|
|
t.Helper()
|
|
appConfig, err := config.LoadValidated()
|
|
if err != nil {
|
|
t.Fatalf("load test config: %v", err)
|
|
}
|
|
appConfig.WorkerAPIBaseURL = workerURL
|
|
return NewHandlerWithConfig(store.NewMemoryStore(), nil, appConfig)
|
|
}
|
|
|
|
func TestResolveScanConfigUsesConfiguredGlobalBOSEndpointForConnectionWithoutEndpoint(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 全局 endpoint 连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
AccessKeyID: "ak-api-default",
|
|
SecretAccessKey: "sk-api-default",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
server := &server{
|
|
store: dataStore,
|
|
globalBOSEndpoint: "https://gz.bcebos.com",
|
|
}
|
|
|
|
resolved, err := server.resolveScanConfig(ctx, &domain.ScanConfig{
|
|
DataSource: "BOS",
|
|
ConnectionID: connection.ID,
|
|
Bucket: "datastorage",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("resolve scan config: %v", err)
|
|
}
|
|
if resolved == nil {
|
|
t.Fatalf("expected resolved scan config")
|
|
}
|
|
if resolved.Endpoint != "https://gz.bcebos.com" {
|
|
t.Fatalf("expected configured global bos endpoint, got %q", resolved.Endpoint)
|
|
}
|
|
if resolved.AccessKeyID != "ak-api-default" || resolved.SecretAccessKey != "sk-api-default" {
|
|
t.Fatalf("expected resolved connection secrets, got %+v", resolved)
|
|
}
|
|
}
|
|
|
|
func TestResolveScanConfigKeepsEmptyEndpointWithoutGlobalBOSFallback(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 无 endpoint 连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
AccessKeyID: "ak-api-empty-endpoint",
|
|
SecretAccessKey: "sk-api-empty-endpoint",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
server := &server{store: dataStore}
|
|
|
|
resolved, err := server.resolveScanConfig(ctx, &domain.ScanConfig{
|
|
DataSource: "BOS",
|
|
ConnectionID: connection.ID,
|
|
Bucket: "datastorage",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("resolve scan config: %v", err)
|
|
}
|
|
if resolved == nil {
|
|
t.Fatalf("expected resolved scan config")
|
|
}
|
|
if resolved.Endpoint != "" {
|
|
t.Fatalf("expected empty endpoint without global fallback, got %q", resolved.Endpoint)
|
|
}
|
|
}
|
|
|
|
func bootstrapSuperAdminSession(t *testing.T, handler http.Handler) *http.Cookie {
|
|
t.Helper()
|
|
body := []byte(`{"name":"Root Admin","email":"root@example.com","password":"correct-horse-battery-staple"}`)
|
|
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/auth/bootstrap-super-admin", body, nil)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("bootstrap super admin = %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
return sessionCookieFromRecorder(t, recorder)
|
|
}
|
|
|
|
func loginSession(t *testing.T, handler http.Handler, email string, password string) *http.Cookie {
|
|
t.Helper()
|
|
body, err := json.Marshal(map[string]string{"email": email, "password": password})
|
|
if err != nil {
|
|
t.Fatalf("marshal login payload: %v", err)
|
|
}
|
|
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/auth/login", body, nil)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("login %s = %d body=%s", email, recorder.Code, recorder.Body.String())
|
|
}
|
|
return sessionCookieFromRecorder(t, recorder)
|
|
}
|
|
|
|
func adminCreateOrganization(t *testing.T, handler http.Handler, cookie *http.Cookie, name string, slug string) domain.OrganizationRecord {
|
|
t.Helper()
|
|
body, err := json.Marshal(map[string]string{"name": name, "slug": slug})
|
|
if err != nil {
|
|
t.Fatalf("marshal organization payload: %v", err)
|
|
}
|
|
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/admin/organizations", body, cookie)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("create organization %s = %d body=%s", name, recorder.Code, recorder.Body.String())
|
|
}
|
|
var item domain.OrganizationRecord
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &item); err != nil {
|
|
t.Fatalf("decode organization: %v", err)
|
|
}
|
|
return item
|
|
}
|
|
|
|
func adminCreateUser(t *testing.T, handler http.Handler, cookie *http.Cookie, organizationID string, name string, email string, role domain.UserRole, password string) domain.UserRecord {
|
|
t.Helper()
|
|
body, err := json.Marshal(map[string]any{
|
|
"name": name,
|
|
"email": email,
|
|
"role": role,
|
|
"password": password,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal user payload: %v", err)
|
|
}
|
|
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/admin/organizations/"+organizationID+"/users", body, cookie)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("create user %s = %d body=%s", email, recorder.Code, recorder.Body.String())
|
|
}
|
|
var item domain.UserRecord
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &item); err != nil {
|
|
t.Fatalf("decode user: %v", err)
|
|
}
|
|
if item.PasswordHash != "" {
|
|
t.Fatalf("admin user response leaked password hash: %s", recorder.Body.String())
|
|
}
|
|
return item
|
|
}
|
|
|
|
func TestSessionAuthBootstrapLoginAndBusinessGuard(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
|
|
unauthorizedRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(unauthorizedRecorder, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil))
|
|
if unauthorizedRecorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected dashboard without session to be rejected, got %d", unauthorizedRecorder.Code)
|
|
}
|
|
|
|
statusRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(statusRecorder, httptest.NewRequest(http.MethodGet, "/api/auth/bootstrap-status", nil))
|
|
if statusRecorder.Code != http.StatusOK {
|
|
t.Fatalf("bootstrap status = %d body=%s", statusRecorder.Code, statusRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(statusRecorder.Body.String(), `"bootstrapRequired":true`) {
|
|
t.Fatalf("expected bootstrap to be required, got %s", statusRecorder.Body.String())
|
|
}
|
|
|
|
blankPasswordBootstrapRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
blankPasswordBootstrapRecorder,
|
|
httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/auth/bootstrap-super-admin",
|
|
bytes.NewReader([]byte(`{"name":"Blank Password","email":"blank@example.com","password":" "}`)),
|
|
),
|
|
)
|
|
if blankPasswordBootstrapRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected whitespace bootstrap password to fail, got %d body=%s", blankPasswordBootstrapRecorder.Code, blankPasswordBootstrapRecorder.Body.String())
|
|
}
|
|
|
|
bootstrapBody := []byte(`{"name":"Root Admin","email":"root@example.com","password":"correct-horse-battery-staple"}`)
|
|
bootstrapRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(bootstrapRecorder, httptest.NewRequest(http.MethodPost, "/api/auth/bootstrap-super-admin", bytes.NewReader(bootstrapBody)))
|
|
if bootstrapRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("bootstrap super admin = %d body=%s", bootstrapRecorder.Code, bootstrapRecorder.Body.String())
|
|
}
|
|
if strings.Contains(bootstrapRecorder.Body.String(), "passwordHash") {
|
|
t.Fatalf("bootstrap response leaked password hash: %s", bootstrapRecorder.Body.String())
|
|
}
|
|
bootstrapCookie := sessionCookieFromRecorder(t, bootstrapRecorder)
|
|
|
|
bootstrapMeRequest := httptest.NewRequest(http.MethodGet, "/api/auth/me", nil)
|
|
bootstrapMeRequest.AddCookie(bootstrapCookie)
|
|
bootstrapMeRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(bootstrapMeRecorder, bootstrapMeRequest)
|
|
if bootstrapMeRecorder.Code != http.StatusOK {
|
|
t.Fatalf("me after bootstrap = %d body=%s", bootstrapMeRecorder.Code, bootstrapMeRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(bootstrapMeRecorder.Body.String(), `"isSuperAdmin":true`) {
|
|
t.Fatalf("expected super admin in bootstrap me response, got %s", bootstrapMeRecorder.Body.String())
|
|
}
|
|
|
|
loginBody := []byte(`{"email":"root@example.com","password":"correct-horse-battery-staple"}`)
|
|
loginRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(loginRecorder, httptest.NewRequest(http.MethodPost, "/api/auth/login", bytes.NewReader(loginBody)))
|
|
if loginRecorder.Code != http.StatusOK {
|
|
t.Fatalf("login = %d body=%s", loginRecorder.Code, loginRecorder.Body.String())
|
|
}
|
|
sessionCookie := sessionCookieFromRecorder(t, loginRecorder)
|
|
|
|
meRequest := httptest.NewRequest(http.MethodGet, "/api/auth/me", nil)
|
|
meRequest.AddCookie(sessionCookie)
|
|
meRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(meRecorder, meRequest)
|
|
if meRecorder.Code != http.StatusOK {
|
|
t.Fatalf("me = %d body=%s", meRecorder.Code, meRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(meRecorder.Body.String(), `"isSuperAdmin":true`) {
|
|
t.Fatalf("expected super admin in me response, got %s", meRecorder.Body.String())
|
|
}
|
|
|
|
businessRequest := httptest.NewRequest(http.MethodGet, "/api/dashboard", nil)
|
|
businessRequest.AddCookie(sessionCookie)
|
|
businessRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(businessRecorder, businessRequest)
|
|
if businessRecorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected super admin business access to be forbidden, got %d body=%s", businessRecorder.Code, businessRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestAuthMeUsesStoredUserContextInPublicPreviewMode(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
organization, err := dataStore.EnsureDefaultOrganization(ctx)
|
|
if err != nil {
|
|
t.Fatalf("ensure default organization: %v", err)
|
|
}
|
|
if _, err := dataStore.CreateUser(ctx, domain.CreateUserInput{
|
|
OrganizationID: organization.ID,
|
|
Name: "Root Admin",
|
|
Email: "root-public@example.com",
|
|
Role: domain.UserRoleAdmin,
|
|
IsSuperAdmin: true,
|
|
Status: domain.UserStatusActive,
|
|
}); err != nil {
|
|
t.Fatalf("create super admin: %v", err)
|
|
}
|
|
if _, err := dataStore.CreateUser(ctx, domain.CreateUserInput{
|
|
OrganizationID: organization.ID,
|
|
Name: "POC Operator",
|
|
Email: "poc-operator@example.com",
|
|
Role: domain.UserRoleDataOperator,
|
|
Status: domain.UserStatusActive,
|
|
}); err != nil {
|
|
t.Fatalf("create operator: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/auth/me", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("public preview me = %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), `"email":"poc-operator@example.com"`) {
|
|
t.Fatalf("expected public preview to prefer business user, got %s", recorder.Body.String())
|
|
}
|
|
if strings.Contains(recorder.Body.String(), "passwordHash") {
|
|
t.Fatalf("public preview me leaked password hash: %s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestAuthMeStillRequiresCookieWhenSessionAuthIsEnabled(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
organization, err := dataStore.EnsureDefaultOrganization(ctx)
|
|
if err != nil {
|
|
t.Fatalf("ensure default organization: %v", err)
|
|
}
|
|
if _, err := dataStore.CreateUser(ctx, domain.CreateUserInput{
|
|
OrganizationID: organization.ID,
|
|
Name: "POC Operator",
|
|
Email: "poc-auth-required@example.com",
|
|
Role: domain.UserRoleDataOperator,
|
|
Status: domain.UserStatusActive,
|
|
}); err != nil {
|
|
t.Fatalf("create operator: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/auth/me", nil))
|
|
if recorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected auth me to require login with session auth enabled, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSecureSessionCookieAndSharedTokenBrowserBypass(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-token")
|
|
t.Setenv("DROBOTICFLOW_SESSION_COOKIE_SECURE", "true")
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
if !rootCookie.Secure {
|
|
t.Fatalf("expected secure bootstrap session cookie, got %#v", rootCookie)
|
|
}
|
|
org := adminCreateOrganization(t, handler, rootCookie, "Browser Tenant", "browser-tenant")
|
|
admin := adminCreateUser(t, handler, rootCookie, org.ID, "Browser Admin", "browser-admin@example.com", domain.UserRoleAdmin, "browser-pass")
|
|
|
|
adminCookie := loginSession(t, handler, admin.Email, "browser-pass")
|
|
if !adminCookie.Secure {
|
|
t.Fatalf("expected secure login session cookie, got %#v", adminCookie)
|
|
}
|
|
missingCSRFRecorder := httptest.NewRecorder()
|
|
missingCSRFRequest := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/projects",
|
|
bytes.NewReader([]byte(`{"name":"Missing CSRF","description":"should fail"}`)),
|
|
)
|
|
missingCSRFRequest.AddCookie(adminCookie)
|
|
handler.ServeHTTP(missingCSRFRecorder, missingCSRFRequest)
|
|
if missingCSRFRecorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected missing csrf token to be rejected, got %d body=%s", missingCSRFRecorder.Code, missingCSRFRecorder.Body.String())
|
|
}
|
|
projectRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/projects",
|
|
[]byte(`{"name":"Cookie Project","description":"created without embedded API token"}`),
|
|
adminCookie,
|
|
)
|
|
if projectRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("expected session cookie to bypass shared API token, got %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestAdminOrganizationSlugValidation(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
|
|
invalidCreateSlugs := []string{
|
|
"bad slug",
|
|
"bad/slug",
|
|
"-tenant",
|
|
"tenant-",
|
|
strings.Repeat("a", 64),
|
|
"租户",
|
|
}
|
|
for _, slug := range invalidCreateSlugs {
|
|
recorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/admin/organizations",
|
|
[]byte(fmt.Sprintf(`{"name":"Invalid Tenant","slug":%q}`, slug)),
|
|
rootCookie,
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid organization slug %q rejected, got %d body=%s", slug, recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "slug") {
|
|
t.Fatalf("expected slug validation issue for %q, body=%s", slug, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
org := adminCreateOrganization(t, handler, rootCookie, "Slug Tenant", "Slug-Tenant")
|
|
if org.Slug != "slug-tenant" {
|
|
t.Fatalf("expected organization slug normalized to lowercase, got %#v", org)
|
|
}
|
|
|
|
duplicateCreateRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/admin/organizations",
|
|
[]byte(`{"name":"Duplicate Slug","slug":"slug-tenant"}`),
|
|
rootCookie,
|
|
)
|
|
if duplicateCreateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected duplicate organization slug rejected, got %d body=%s", duplicateCreateRecorder.Code, duplicateCreateRecorder.Body.String())
|
|
}
|
|
|
|
invalidUpdateRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/admin/organizations/"+org.ID,
|
|
[]byte(`{"slug":"invalid/tenant"}`),
|
|
rootCookie,
|
|
)
|
|
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid organization slug update rejected, got %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
|
}
|
|
stored, err := dataStore.GetOrganization(context.Background(), org.ID)
|
|
if err != nil {
|
|
t.Fatalf("reload organization after invalid slug update: %v", err)
|
|
}
|
|
if stored == nil || stored.Slug != "slug-tenant" {
|
|
t.Fatalf("expected invalid slug update not to persist, got %#v", stored)
|
|
}
|
|
|
|
other := adminCreateOrganization(t, handler, rootCookie, "Other Tenant", "other-tenant")
|
|
duplicateUpdateRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/admin/organizations/"+other.ID,
|
|
[]byte(`{"slug":"slug-tenant"}`),
|
|
rootCookie,
|
|
)
|
|
if duplicateUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected duplicate organization slug update rejected, got %d body=%s", duplicateUpdateRecorder.Code, duplicateUpdateRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestUserEmailUniquenessValidation(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
org := adminCreateOrganization(t, handler, rootCookie, "Email Tenant", "email-tenant")
|
|
|
|
admin := adminCreateUser(t, handler, rootCookie, org.ID, "Email Admin", "email-admin@example.com", domain.UserRoleAdmin, "admin-pass")
|
|
operator := adminCreateUser(t, handler, rootCookie, org.ID, "Email Operator", "email-operator@example.com", domain.UserRoleOperator, "operator-pass")
|
|
|
|
duplicateAdminCreateRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/admin/organizations/"+org.ID+"/users",
|
|
[]byte(`{"name":"Duplicate Admin","email":"EMAIL-ADMIN@example.com","role":"operator","password":"operator-pass"}`),
|
|
rootCookie,
|
|
)
|
|
if duplicateAdminCreateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected duplicate admin user create email rejected, got %d body=%s", duplicateAdminCreateRecorder.Code, duplicateAdminCreateRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(duplicateAdminCreateRecorder.Body.String(), "email") {
|
|
t.Fatalf("expected email validation issue, body=%s", duplicateAdminCreateRecorder.Body.String())
|
|
}
|
|
|
|
duplicateAdminUpdateRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/admin/users/"+operator.ID,
|
|
[]byte(`{"email":"email-admin@example.com"}`),
|
|
rootCookie,
|
|
)
|
|
if duplicateAdminUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected duplicate admin user update email rejected, got %d body=%s", duplicateAdminUpdateRecorder.Code, duplicateAdminUpdateRecorder.Body.String())
|
|
}
|
|
storedOperator, err := dataStore.GetUser(context.Background(), operator.ID)
|
|
if err != nil {
|
|
t.Fatalf("reload operator after invalid email update: %v", err)
|
|
}
|
|
if storedOperator == nil || storedOperator.Email != "email-operator@example.com" {
|
|
t.Fatalf("expected duplicate email update not to persist, got %#v", storedOperator)
|
|
}
|
|
|
|
operatorCookie := loginSession(t, handler, operator.Email, "operator-pass")
|
|
duplicateProfileRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/users/me",
|
|
[]byte(`{"email":"email-admin@example.com"}`),
|
|
operatorCookie,
|
|
)
|
|
if duplicateProfileRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected duplicate profile email rejected, got %d body=%s", duplicateProfileRecorder.Code, duplicateProfileRecorder.Body.String())
|
|
}
|
|
|
|
adminCookie := loginSession(t, handler, admin.Email, "admin-pass")
|
|
duplicateBusinessCreateRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/users",
|
|
[]byte(`{"name":"Duplicate Business User","email":"email-operator@example.com","role":"operator","password":"business-pass"}`),
|
|
adminCookie,
|
|
)
|
|
if duplicateBusinessCreateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected duplicate business user create email rejected, got %d body=%s", duplicateBusinessCreateRecorder.Code, duplicateBusinessCreateRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestStoreDuplicateMutationErrorsRenderValidationResponses(t *testing.T) {
|
|
emailRecorder := httptest.NewRecorder()
|
|
renderUserMutationError(emailRecorder, store.ErrDuplicateUserEmail)
|
|
if emailRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected duplicate email error to render 400, got %d body=%s", emailRecorder.Code, emailRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(emailRecorder.Body.String(), `"field":"email"`) || !strings.Contains(emailRecorder.Body.String(), "email must be unique") {
|
|
t.Fatalf("expected duplicate email validation issue, got %s", emailRecorder.Body.String())
|
|
}
|
|
|
|
slugRecorder := httptest.NewRecorder()
|
|
renderOrganizationMutationError(slugRecorder, store.ErrDuplicateOrganizationSlug)
|
|
if slugRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected duplicate slug error to render 400, got %d body=%s", slugRecorder.Code, slugRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(slugRecorder.Body.String(), `"field":"slug"`) || !strings.Contains(slugRecorder.Body.String(), "slug must be unique") {
|
|
t.Fatalf("expected duplicate slug validation issue, got %s", slugRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSessionLogoutRequiresCSRF(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
sessionCookie := bootstrapSuperAdminSession(t, handler)
|
|
|
|
missingCSRFRecorder := httptest.NewRecorder()
|
|
missingCSRFRequest := httptest.NewRequest(http.MethodPost, "/api/auth/logout", nil)
|
|
missingCSRFRequest.AddCookie(sessionCookie)
|
|
handler.ServeHTTP(missingCSRFRecorder, missingCSRFRequest)
|
|
if missingCSRFRecorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected logout without csrf token to be rejected, got %d body=%s", missingCSRFRecorder.Code, missingCSRFRecorder.Body.String())
|
|
}
|
|
|
|
meRequest := httptest.NewRequest(http.MethodGet, "/api/auth/me", nil)
|
|
meRequest.AddCookie(sessionCookie)
|
|
meRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(meRecorder, meRequest)
|
|
if meRecorder.Code != http.StatusOK {
|
|
t.Fatalf("expected session to survive rejected logout, got %d body=%s", meRecorder.Code, meRecorder.Body.String())
|
|
}
|
|
|
|
logoutRecorder := serveJSONWithCookie(handler, http.MethodPost, "/api/auth/logout", nil, sessionCookie)
|
|
if logoutRecorder.Code != http.StatusNoContent {
|
|
t.Fatalf("logout with csrf = %d body=%s", logoutRecorder.Code, logoutRecorder.Body.String())
|
|
}
|
|
|
|
loggedOutMeRequest := httptest.NewRequest(http.MethodGet, "/api/auth/me", nil)
|
|
loggedOutMeRequest.AddCookie(sessionCookie)
|
|
loggedOutMeRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(loggedOutMeRecorder, loggedOutMeRequest)
|
|
if loggedOutMeRecorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected logged-out session to be rejected, got %d body=%s", loggedOutMeRecorder.Code, loggedOutMeRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestExternalBusinessWriteBypassesSessionLoginRequirement(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-token")
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/external/business-db/documents", bytes.NewReader([]byte(`{}`))))
|
|
if recorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected external write auth to reject missing X-API-Key, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if strings.Contains(recorder.Body.String(), "login is required") {
|
|
t.Fatalf("external write endpoint should not be blocked by session auth: %s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSessionAuthTenantIsolationAndRolePermissions(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
|
|
orgA := adminCreateOrganization(t, handler, rootCookie, "Tenant A", "tenant-a")
|
|
orgB := adminCreateOrganization(t, handler, rootCookie, "Tenant B", "tenant-b")
|
|
blankPasswordAdminUserRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/admin/organizations/"+orgA.ID+"/users",
|
|
[]byte(`{"name":"Blank Password User","email":"blank-password-user@example.com","role":"operator","password":" "}`),
|
|
rootCookie,
|
|
)
|
|
if blankPasswordAdminUserRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected admin user create with whitespace password to fail, got %d body=%s", blankPasswordAdminUserRecorder.Code, blankPasswordAdminUserRecorder.Body.String())
|
|
}
|
|
adminA := adminCreateUser(t, handler, rootCookie, orgA.ID, "Admin A", "admin-a@example.com", domain.UserRoleAdmin, "admin-a-pass")
|
|
adminB := adminCreateUser(t, handler, rootCookie, orgB.ID, "Admin B", "admin-b@example.com", domain.UserRoleAdmin, "admin-b-pass")
|
|
operatorA := adminCreateUser(t, handler, rootCookie, orgA.ID, "Operator A", "operator-a@example.com", domain.UserRoleOperator, "operator-a-pass")
|
|
auditorA := adminCreateUser(t, handler, rootCookie, orgA.ID, "Auditor A", "auditor-a@example.com", domain.UserRoleAuditor, "auditor-a-pass")
|
|
|
|
if adminA.OrganizationID != orgA.ID || adminB.OrganizationID != orgB.ID || operatorA.OrganizationID != orgA.ID || auditorA.OrganizationID != orgA.ID {
|
|
t.Fatalf("created users have wrong organizations: adminA=%s adminB=%s auditorA=%s", adminA.OrganizationID, adminB.OrganizationID, auditorA.OrganizationID)
|
|
}
|
|
|
|
adminACookie := loginSession(t, handler, adminA.Email, "admin-a-pass")
|
|
projectRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/projects",
|
|
[]byte(`{"name":"Tenant A Project","description":"owned by tenant A","organizationId":"org_wrong"}`),
|
|
adminACookie,
|
|
)
|
|
if projectRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("tenant A project create = %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
|
}
|
|
var createdProject domain.ProjectRecord
|
|
if err := json.Unmarshal(projectRecorder.Body.Bytes(), &createdProject); err != nil {
|
|
t.Fatalf("decode project: %v", err)
|
|
}
|
|
if createdProject.OrganizationID != orgA.ID {
|
|
t.Fatalf("expected created project tenant %s, got %s", orgA.ID, createdProject.OrganizationID)
|
|
}
|
|
blankProjectNameRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/projects/"+createdProject.ID,
|
|
[]byte(`{"name":" "}`),
|
|
adminACookie,
|
|
)
|
|
if blankProjectNameRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected project update with whitespace name to fail, got %d body=%s", blankProjectNameRecorder.Code, blankProjectNameRecorder.Body.String())
|
|
}
|
|
invalidProjectStatusRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/projects/"+createdProject.ID,
|
|
[]byte(`{"status":"paused"}`),
|
|
adminACookie,
|
|
)
|
|
if invalidProjectStatusRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected project update with invalid status to fail, got %d body=%s", invalidProjectStatusRecorder.Code, invalidProjectStatusRecorder.Body.String())
|
|
}
|
|
storedProject, err := dataStore.GetProject(context.Background(), createdProject.ID)
|
|
if err != nil {
|
|
t.Fatalf("reload project after invalid updates: %v", err)
|
|
}
|
|
if storedProject == nil || storedProject.Name != createdProject.Name || storedProject.Status != createdProject.Status {
|
|
t.Fatalf("expected invalid project updates to preserve fields, got %#v", storedProject)
|
|
}
|
|
workflowA, err := dataStore.CreateWorkflow(context.Background(), domain.CreateWorkflowInput{
|
|
OrganizationID: createdProject.OrganizationID,
|
|
ProjectID: createdProject.ID,
|
|
Name: "Tenant A Workflow",
|
|
Description: "owned by tenant A",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("seed tenant A workflow: %v", err)
|
|
}
|
|
runA, err := dataStore.CreateRun(context.Background(), workflowA.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("seed tenant A run: %v", err)
|
|
}
|
|
|
|
adminBCookie := loginSession(t, handler, adminB.Email, "admin-b-pass")
|
|
foreignGetRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/projects/"+createdProject.ID, nil, adminBCookie)
|
|
if foreignGetRecorder.Code != http.StatusNotFound {
|
|
t.Fatalf("expected tenant B project get to hide tenant A record, got %d body=%s", foreignGetRecorder.Code, foreignGetRecorder.Body.String())
|
|
}
|
|
|
|
foreignListRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/projects", nil, adminBCookie)
|
|
if foreignListRecorder.Code != http.StatusOK {
|
|
t.Fatalf("tenant B project list = %d body=%s", foreignListRecorder.Code, foreignListRecorder.Body.String())
|
|
}
|
|
var projectList struct {
|
|
Items []domain.ProjectRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(foreignListRecorder.Body.Bytes(), &projectList); err != nil {
|
|
t.Fatalf("decode project list: %v", err)
|
|
}
|
|
for _, item := range projectList.Items {
|
|
if item.ID == createdProject.ID {
|
|
t.Fatalf("tenant B project list leaked tenant A project: %#v", projectList.Items)
|
|
}
|
|
}
|
|
|
|
dashboardRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/dashboard", nil, adminBCookie)
|
|
if dashboardRecorder.Code != http.StatusOK {
|
|
t.Fatalf("tenant B dashboard = %d body=%s", dashboardRecorder.Code, dashboardRecorder.Body.String())
|
|
}
|
|
var dashboard map[string]map[string]float64
|
|
if err := json.Unmarshal(dashboardRecorder.Body.Bytes(), &dashboard); err != nil {
|
|
t.Fatalf("decode dashboard: %v", err)
|
|
}
|
|
if dashboard["projects"]["active"] != 0 {
|
|
t.Fatalf("tenant B dashboard leaked tenant A active project count: %s", dashboardRecorder.Body.String())
|
|
}
|
|
|
|
foreignRunsRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/runs", nil, adminBCookie)
|
|
if foreignRunsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("tenant B run summary list = %d body=%s", foreignRunsRecorder.Code, foreignRunsRecorder.Body.String())
|
|
}
|
|
var runList struct {
|
|
Items []domain.RunSummary `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(foreignRunsRecorder.Body.Bytes(), &runList); err != nil {
|
|
t.Fatalf("decode run summary list: %v", err)
|
|
}
|
|
for _, item := range runList.Items {
|
|
if item.ID == runA.ID {
|
|
t.Fatalf("tenant B run summary list leaked tenant A run: %#v", runList.Items)
|
|
}
|
|
}
|
|
|
|
auditorCookie := loginSession(t, handler, auditorA.Email, "auditor-a-pass")
|
|
auditorProjectRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/projects",
|
|
[]byte(`{"name":"Auditor Project","description":"should fail"}`),
|
|
auditorCookie,
|
|
)
|
|
if auditorProjectRecorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected auditor project create to be forbidden, got %d body=%s", auditorProjectRecorder.Code, auditorProjectRecorder.Body.String())
|
|
}
|
|
for _, path := range []string{"/api/ai/operators/generate", "/api/ai/workflows/generate"} {
|
|
recorder := serveJSONWithCookie(handler, http.MethodPost, path, []byte(`{"description":"generate test"}`), auditorCookie)
|
|
if recorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected auditor POST %s to be forbidden, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
for _, path := range []string{
|
|
"/api/storage/connections",
|
|
"/api/mongo/connections",
|
|
"/api/storage/buckets",
|
|
"/api/storage/objects",
|
|
} {
|
|
recorder := serveJSONWithCookie(handler, http.MethodGet, path, nil, auditorCookie)
|
|
if recorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected auditor GET %s to be forbidden, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
publicSettingsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(publicSettingsRecorder, httptest.NewRequest(http.MethodGet, "/api/public/settings", nil))
|
|
if publicSettingsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("public settings = %d body=%s", publicSettingsRecorder.Code, publicSettingsRecorder.Body.String())
|
|
}
|
|
var publicSettings map[string]string
|
|
if err := json.Unmarshal(publicSettingsRecorder.Body.Bytes(), &publicSettings); err != nil {
|
|
t.Fatalf("decode public settings: %v", err)
|
|
}
|
|
if len(publicSettings) != 1 || publicSettings["timezone"] != domain.DefaultWorkflowScheduleTimezone {
|
|
t.Fatalf("expected public settings to expose only default timezone, got %#v", publicSettings)
|
|
}
|
|
|
|
adminUsersRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/users", nil, adminACookie)
|
|
if adminUsersRecorder.Code != http.StatusOK {
|
|
t.Fatalf("admin user list = %d body=%s", adminUsersRecorder.Code, adminUsersRecorder.Body.String())
|
|
}
|
|
|
|
adminSettingsRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/settings", nil, adminACookie)
|
|
if adminSettingsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("admin settings get = %d body=%s", adminSettingsRecorder.Code, adminSettingsRecorder.Body.String())
|
|
}
|
|
|
|
userWithoutPasswordRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/users",
|
|
[]byte(`{"name":"No Password","email":"no-password@example.com","role":"operator"}`),
|
|
adminACookie,
|
|
)
|
|
if userWithoutPasswordRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected session user create without password to fail, got %d body=%s", userWithoutPasswordRecorder.Code, userWithoutPasswordRecorder.Body.String())
|
|
}
|
|
|
|
userWithInvalidStatusRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/users",
|
|
[]byte(`{"name":"Invalid Status","email":"invalid-status@example.com","role":"operator","status":"pending","password":"invalid-status-pass"}`),
|
|
adminACookie,
|
|
)
|
|
if userWithInvalidStatusRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected session user create with invalid status to fail, got %d body=%s", userWithInvalidStatusRecorder.Code, userWithInvalidStatusRecorder.Body.String())
|
|
}
|
|
|
|
userWithPasswordRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/users",
|
|
[]byte(`{"name":"Created Operator","email":"created-operator@example.com","role":"operator","password":"created-operator-pass"}`),
|
|
adminACookie,
|
|
)
|
|
if userWithPasswordRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("session user create with password = %d body=%s", userWithPasswordRecorder.Code, userWithPasswordRecorder.Body.String())
|
|
}
|
|
var createdSessionUser domain.UserRecord
|
|
if err := json.Unmarshal(userWithPasswordRecorder.Body.Bytes(), &createdSessionUser); err != nil {
|
|
t.Fatalf("decode created session user: %v", err)
|
|
}
|
|
if createdSessionUser.PasswordHash != "" {
|
|
t.Fatalf("session user create leaked password hash: %s", userWithPasswordRecorder.Body.String())
|
|
}
|
|
storedSessionUser, err := dataStore.GetUser(context.Background(), createdSessionUser.ID)
|
|
if err != nil {
|
|
t.Fatalf("load created session user: %v", err)
|
|
}
|
|
if storedSessionUser == nil || strings.TrimSpace(storedSessionUser.PasswordHash) == "" {
|
|
t.Fatalf("expected created session user to store password hash, got %#v", storedSessionUser)
|
|
}
|
|
originalCreatedUserPasswordHash := storedSessionUser.PasswordHash
|
|
blankPasswordUpdateRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/users/"+createdSessionUser.ID,
|
|
[]byte(`{"password":" "}`),
|
|
adminACookie,
|
|
)
|
|
if blankPasswordUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected user update with whitespace password to fail, got %d body=%s", blankPasswordUpdateRecorder.Code, blankPasswordUpdateRecorder.Body.String())
|
|
}
|
|
storedSessionUser, err = dataStore.GetUser(context.Background(), createdSessionUser.ID)
|
|
if err != nil {
|
|
t.Fatalf("reload created session user after blank password update: %v", err)
|
|
}
|
|
if storedSessionUser == nil || storedSessionUser.PasswordHash != originalCreatedUserPasswordHash {
|
|
t.Fatalf("expected blank password update to preserve hash, got %#v", storedSessionUser)
|
|
}
|
|
blankNameUpdateRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/users/"+createdSessionUser.ID,
|
|
[]byte(`{"name":" "}`),
|
|
adminACookie,
|
|
)
|
|
if blankNameUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected user update with whitespace name to fail, got %d body=%s", blankNameUpdateRecorder.Code, blankNameUpdateRecorder.Body.String())
|
|
}
|
|
invalidStatusUpdateRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/users/"+createdSessionUser.ID,
|
|
[]byte(`{"status":"pending"}`),
|
|
adminACookie,
|
|
)
|
|
if invalidStatusUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected user update with invalid status to fail, got %d body=%s", invalidStatusUpdateRecorder.Code, invalidStatusUpdateRecorder.Body.String())
|
|
}
|
|
storedSessionUser, err = dataStore.GetUser(context.Background(), createdSessionUser.ID)
|
|
if err != nil {
|
|
t.Fatalf("reload created session user after invalid updates: %v", err)
|
|
}
|
|
if storedSessionUser == nil || storedSessionUser.Name != createdSessionUser.Name || storedSessionUser.Status != createdSessionUser.Status {
|
|
t.Fatalf("expected invalid updates to preserve user fields, got %#v", storedSessionUser)
|
|
}
|
|
_ = loginSession(t, handler, createdSessionUser.Email, "created-operator-pass")
|
|
|
|
operatorCookie := loginSession(t, handler, operatorA.Email, "operator-a-pass")
|
|
for _, path := range []string{"/api/storage/connections", "/api/mongo/connections"} {
|
|
recorder := serveJSONWithCookie(handler, http.MethodGet, path, nil, operatorCookie)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("expected operator GET %s to be allowed, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
for _, path := range []string{"/api/storage/buckets", "/api/storage/objects"} {
|
|
recorder := serveJSONWithCookie(handler, http.MethodGet, path, nil, operatorCookie)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected operator GET %s to reach storage browse validation, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
adminOnlyPaths := []string{
|
|
"/api/users",
|
|
"/api/database",
|
|
"/api/object-storage",
|
|
"/api/object-storage/objects",
|
|
"/api/object-storage/content",
|
|
"/api/settings",
|
|
"/api/external/business-db/tokens",
|
|
}
|
|
for _, path := range adminOnlyPaths {
|
|
recorder := serveJSONWithCookie(handler, http.MethodGet, path, nil, operatorCookie)
|
|
if recorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected operator GET %s to be forbidden, got %d body=%s", path, recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
mongoTestRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/mongo/connections/test",
|
|
[]byte(`{"uri":"mongodb://example.invalid:27017","database":"business"}`),
|
|
operatorCookie,
|
|
)
|
|
if mongoTestRecorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected operator mongo connection test to be forbidden, got %d body=%s", mongoTestRecorder.Code, mongoTestRecorder.Body.String())
|
|
}
|
|
|
|
objectStorageCreateRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPost,
|
|
"/api/object-storage/objects",
|
|
[]byte(`{"bucket":"datastorage","key":"blocked.json","content":"{}"}`),
|
|
operatorCookie,
|
|
)
|
|
if objectStorageCreateRecorder.Code != http.StatusForbidden {
|
|
t.Fatalf("expected operator object storage create to be forbidden, got %d body=%s", objectStorageCreateRecorder.Code, objectStorageCreateRecorder.Body.String())
|
|
}
|
|
|
|
profileRecorder := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/users/me",
|
|
[]byte(`{"name":"Renamed Operator","email":"RENAMED-OPERATOR@example.com","role":"admin"}`),
|
|
operatorCookie,
|
|
)
|
|
if profileRecorder.Code != http.StatusOK {
|
|
t.Fatalf("operator profile update = %d body=%s", profileRecorder.Code, profileRecorder.Body.String())
|
|
}
|
|
var updatedOperator domain.UserRecord
|
|
if err := json.Unmarshal(profileRecorder.Body.Bytes(), &updatedOperator); err != nil {
|
|
t.Fatalf("decode updated operator: %v", err)
|
|
}
|
|
if updatedOperator.Name != "Renamed Operator" || updatedOperator.Email != "renamed-operator@example.com" {
|
|
t.Fatalf("expected profile fields to update, got %#v", updatedOperator)
|
|
}
|
|
if updatedOperator.Role != domain.UserRoleOperator {
|
|
t.Fatalf("expected profile update to preserve operator role, got %s", updatedOperator.Role)
|
|
}
|
|
storedOperator, err := dataStore.GetUser(context.Background(), operatorA.ID)
|
|
if err != nil {
|
|
t.Fatalf("load stored operator: %v", err)
|
|
}
|
|
if storedOperator == nil || storedOperator.Role != domain.UserRoleOperator {
|
|
t.Fatalf("expected stored operator role to remain operator, got %#v", storedOperator)
|
|
}
|
|
}
|
|
|
|
func TestSessionAuthShowsGlobalBuiltinOperatorsForEachTenant(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
|
|
orgA := adminCreateOrganization(t, handler, rootCookie, "Operator Tenant A", "operator-tenant-a")
|
|
orgB := adminCreateOrganization(t, handler, rootCookie, "Operator Tenant B", "operator-tenant-b")
|
|
adminA := adminCreateUser(t, handler, rootCookie, orgA.ID, "Operator Admin A", "operator-admin-a@example.com", domain.UserRoleAdmin, "operator-admin-a-pass")
|
|
adminB := adminCreateUser(t, handler, rootCookie, orgB.ID, "Operator Admin B", "operator-admin-b@example.com", domain.UserRoleAdmin, "operator-admin-b-pass")
|
|
adminACookie := loginSession(t, handler, adminA.Email, "operator-admin-a-pass")
|
|
adminBCookie := loginSession(t, handler, adminB.Email, "operator-admin-b-pass")
|
|
|
|
catalog, err := builtinOperatorRecords("2026-06-23T00:00:00Z", "runner:test")
|
|
if err != nil {
|
|
t.Fatalf("load builtin catalog: %v", err)
|
|
}
|
|
|
|
listARecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/operators", nil, adminACookie)
|
|
if listARecorder.Code != http.StatusOK {
|
|
t.Fatalf("tenant A operators list = %d body=%s", listARecorder.Code, listARecorder.Body.String())
|
|
}
|
|
var listA struct {
|
|
Items []domain.OperatorRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(listARecorder.Body.Bytes(), &listA); err != nil {
|
|
t.Fatalf("decode tenant A operators: %v", err)
|
|
}
|
|
if got := countOperatorsByType(listA.Items, "builtin"); got != len(catalog) {
|
|
t.Fatalf("tenant A builtin count = %d, want %d", got, len(catalog))
|
|
}
|
|
|
|
customA := createCustomOperatorForTest(t, handler, adminACookie, "Tenant A Custom Operator", "registry.example/operators/a:v1")
|
|
customB := createCustomOperatorForTest(t, handler, adminBCookie, "Tenant B Custom Operator", "registry.example/operators/b:v1")
|
|
|
|
listBRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/operators", nil, adminBCookie)
|
|
if listBRecorder.Code != http.StatusOK {
|
|
t.Fatalf("tenant B operators list = %d body=%s", listBRecorder.Code, listBRecorder.Body.String())
|
|
}
|
|
var listB struct {
|
|
Items []domain.OperatorRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(listBRecorder.Body.Bytes(), &listB); err != nil {
|
|
t.Fatalf("decode tenant B operators: %v", err)
|
|
}
|
|
if got := countOperatorsByType(listB.Items, "builtin"); got != len(catalog) {
|
|
t.Fatalf("tenant B builtin count = %d, want %d", got, len(catalog))
|
|
}
|
|
if !containsOperatorID(listB.Items, customB.ID) {
|
|
t.Fatalf("tenant B custom operator missing from tenant B list")
|
|
}
|
|
if containsOperatorID(listB.Items, customA.ID) {
|
|
t.Fatalf("tenant B operators leaked tenant A custom operator")
|
|
}
|
|
|
|
builtinGetRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/operators/"+catalog[0].ID, nil, adminBCookie)
|
|
if builtinGetRecorder.Code != http.StatusOK {
|
|
t.Fatalf("tenant B builtin get = %d body=%s", builtinGetRecorder.Code, builtinGetRecorder.Body.String())
|
|
}
|
|
foreignCustomGetRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/operators/"+customA.ID, nil, adminBCookie)
|
|
if foreignCustomGetRecorder.Code != http.StatusNotFound {
|
|
t.Fatalf("expected tenant B custom get to hide tenant A operator, got %d body=%s", foreignCustomGetRecorder.Code, foreignCustomGetRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func createCustomOperatorForTest(t *testing.T, handler http.Handler, cookie *http.Cookie, name string, image string) domain.OperatorRecord {
|
|
t.Helper()
|
|
payload, err := json.Marshal(map[string]any{
|
|
"name": name,
|
|
"type": "custom",
|
|
"category": "DATA",
|
|
"description": "tenant scoped custom operator",
|
|
"inputs": []map[string]string{
|
|
{"id": "input", "name": "Input", "type": "json"},
|
|
},
|
|
"outputs": []map[string]string{
|
|
{"id": "output", "name": "Output", "type": "json"},
|
|
},
|
|
"image": image,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal custom operator: %v", err)
|
|
}
|
|
recorder := serveJSONWithCookie(handler, http.MethodPost, "/api/operators", payload, cookie)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("create custom operator %s = %d body=%s", name, recorder.Code, recorder.Body.String())
|
|
}
|
|
var item domain.OperatorRecord
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &item); err != nil {
|
|
t.Fatalf("decode custom operator: %v", err)
|
|
}
|
|
return item
|
|
}
|
|
|
|
func countOperatorsByType(items []domain.OperatorRecord, operatorType string) int {
|
|
count := 0
|
|
for _, item := range items {
|
|
if strings.EqualFold(strings.TrimSpace(item.Type), operatorType) {
|
|
count++
|
|
}
|
|
}
|
|
return count
|
|
}
|
|
|
|
func containsOperatorID(items []domain.OperatorRecord, id string) bool {
|
|
for _, item := range items {
|
|
if item.ID == id {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func TestWorkerAPIHealthIsProxiedThroughAPI(t *testing.T) {
|
|
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
if request.URL.Path != "/worker-api/health" {
|
|
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
|
}
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
_, _ = writer.Write([]byte(`{"status":"ok","service":"droboticflow-worker"}`))
|
|
}))
|
|
defer worker.Close()
|
|
|
|
handler := newHandlerWithWorkerAPIBaseURL(t, worker.URL)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/worker-api/health", nil))
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if got := strings.TrimSpace(recorder.Body.String()); got != `{"status":"ok","service":"droboticflow-worker"}` {
|
|
t.Fatalf("unexpected worker health payload: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestWorkerAPIBaseURLUsesConfiguredEnv(t *testing.T) {
|
|
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
if request.URL.Path != "/worker-api/health" {
|
|
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
|
}
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
_, _ = writer.Write([]byte(`{"status":"configured"}`))
|
|
}))
|
|
defer worker.Close()
|
|
|
|
t.Setenv("WORKER_API_BASE_URL", worker.URL)
|
|
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/worker-api/health", nil))
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if got := strings.TrimSpace(recorder.Body.String()); got != `{"status":"configured"}` {
|
|
t.Fatalf("unexpected configured worker payload: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestWorkerAPIProxyTimesOutSlowHealthChecks(t *testing.T) {
|
|
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
time.Sleep(100 * time.Millisecond)
|
|
writer.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer worker.Close()
|
|
|
|
originalTimeout := workerAPIShortProxyTimeout
|
|
workerAPIShortProxyTimeout = 10 * time.Millisecond
|
|
t.Cleanup(func() {
|
|
workerAPIShortProxyTimeout = originalTimeout
|
|
})
|
|
|
|
handler := newHandlerWithWorkerAPIBaseURL(t, worker.URL)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/worker-api/health", nil))
|
|
|
|
if recorder.Code != http.StatusBadGateway {
|
|
t.Fatalf("expected proxy timeout to return 502, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestWorkerAPIProcessOnceIsProxiedThroughAPI(t *testing.T) {
|
|
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
if request.Method != http.MethodPost {
|
|
t.Fatalf("unexpected upstream method: %s", request.Method)
|
|
}
|
|
if request.URL.Path != "/worker-api/process-once" {
|
|
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
|
}
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
writer.WriteHeader(http.StatusOK)
|
|
_, _ = writer.Write([]byte(`{"run":{"id":"run-1","status":"running"}}`))
|
|
}))
|
|
defer worker.Close()
|
|
|
|
handler := newHandlerWithWorkerAPIBaseURL(t, worker.URL)
|
|
unauthorizedRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(unauthorizedRecorder, httptest.NewRequest(http.MethodPost, "/worker-api/process-once", nil))
|
|
if unauthorizedRecorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected unauthenticated process-once to be rejected, got %d body=%s", unauthorizedRecorder.Code, unauthorizedRecorder.Body.String())
|
|
}
|
|
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
org := adminCreateOrganization(t, handler, rootCookie, "Worker Tenant", "worker-tenant")
|
|
admin := adminCreateUser(t, handler, rootCookie, org.ID, "Worker Admin", "worker-admin@example.com", domain.UserRoleAdmin, "worker-pass")
|
|
adminCookie := loginSession(t, handler, admin.Email, "worker-pass")
|
|
|
|
recorder := serveJSONWithCookie(handler, http.MethodPost, "/worker-api/process-once", nil, adminCookie)
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if got := strings.TrimSpace(recorder.Body.String()); got != `{"run":{"id":"run-1","status":"running"}}` {
|
|
t.Fatalf("unexpected worker process-once payload: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestWorkerAPIMetricsIsProxiedThroughAPI(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_API_TOKEN", "worker-metrics-token")
|
|
|
|
worker := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
if request.Method != http.MethodGet {
|
|
t.Fatalf("unexpected upstream method: %s", request.Method)
|
|
}
|
|
if request.URL.Path != "/worker-api/metrics" {
|
|
t.Fatalf("unexpected upstream path: %s", request.URL.Path)
|
|
}
|
|
writer.Header().Set("Content-Type", "text/plain; version=0.0.4")
|
|
writer.WriteHeader(http.StatusOK)
|
|
_, _ = writer.Write([]byte("droboticflow_worker_runs{status=\"queued\"} 1\n"))
|
|
}))
|
|
defer worker.Close()
|
|
|
|
handler := newHandlerWithWorkerAPIBaseURL(t, worker.URL)
|
|
unauthorizedRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(unauthorizedRecorder, httptest.NewRequest(http.MethodGet, "/worker-api/metrics", nil))
|
|
if unauthorizedRecorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected metrics without API token to be rejected, got %d body=%s", unauthorizedRecorder.Code, unauthorizedRecorder.Body.String())
|
|
}
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/worker-api/metrics", nil)
|
|
request.Header.Set("Authorization", "Bearer worker-metrics-token")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "droboticflow_worker_runs") {
|
|
t.Fatalf("unexpected worker metrics payload: %s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestAPIHandlerSupportsDashboardCRUDAndRunQueueing(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
storageRoot := t.TempDir()
|
|
for _, bucket := range []string{"incoming", "processed"} {
|
|
if err := os.Mkdir(filepath.Join(storageRoot, bucket), 0o755); err != nil {
|
|
t.Fatalf("create bucket %s: %v", bucket, err)
|
|
}
|
|
}
|
|
settings, err := dataStore.GetSettings(ctx)
|
|
if err != nil {
|
|
t.Fatalf("get settings: %v", err)
|
|
}
|
|
settings.K8s.Kubeconfig = "apiVersion: v1"
|
|
if err := dataStore.UpsertSettings(ctx, *settings); err != nil {
|
|
t.Fatalf("update settings: %v", err)
|
|
}
|
|
if _, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "Dashboard 项目",
|
|
Description: "用于 dashboard 验证",
|
|
}); err != nil {
|
|
t.Fatalf("create dashboard project: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
dashboardRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(dashboardRecorder, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil))
|
|
if dashboardRecorder.Code != http.StatusOK {
|
|
t.Fatalf("dashboard status: %d", dashboardRecorder.Code)
|
|
}
|
|
var dashboardBody map[string]any
|
|
if err := json.Unmarshal(dashboardRecorder.Body.Bytes(), &dashboardBody); err != nil {
|
|
t.Fatalf("decode dashboard: %v", err)
|
|
}
|
|
projects := dashboardBody["projects"].(map[string]any)
|
|
if projects["active"].(float64) <= 0 {
|
|
t.Fatalf("expected active projects")
|
|
}
|
|
|
|
projectPayload := []byte(`{"name":"API 项目","description":"通过 API 创建"}`)
|
|
projectRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(projectRecorder, httptest.NewRequest(http.MethodPost, "/api/projects", bytes.NewReader(projectPayload)))
|
|
if projectRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("project create status: %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
|
}
|
|
var createdProject map[string]any
|
|
if err := json.Unmarshal(projectRecorder.Body.Bytes(), &createdProject); err != nil {
|
|
t.Fatalf("decode created project: %v", err)
|
|
}
|
|
projectID := createdProject["id"].(string)
|
|
if projectID == "" {
|
|
t.Fatalf("expected project id")
|
|
}
|
|
|
|
workflowPayload := []byte(`{"name":"API 工作流","description":"workflow","nodes":[{"id":"node-source","type":"operator","data":{"label":"真实数据源","category":"IO","description":"source","inputs":[],"outputs":[{"id":"out-1","name":"file","type":"file"}],"hyperparams":[{"key":"SOURCE_TEXT","value":"api-test"}]},"position":{"x":0,"y":0}}],"edges":[]}`)
|
|
workflowRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(workflowRecorder, httptest.NewRequest(http.MethodPost, "/api/projects/"+projectID+"/workflows", bytes.NewReader(workflowPayload)))
|
|
if workflowRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("workflow create status: %d body=%s", workflowRecorder.Code, workflowRecorder.Body.String())
|
|
}
|
|
var createdWorkflow map[string]any
|
|
if err := json.Unmarshal(workflowRecorder.Body.Bytes(), &createdWorkflow); err != nil {
|
|
t.Fatalf("decode workflow: %v", err)
|
|
}
|
|
if createdWorkflow["type"].(string) != "batch" {
|
|
t.Fatalf("expected workflow type to default to batch, got %v", createdWorkflow["type"])
|
|
}
|
|
|
|
projectsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(projectsRecorder, httptest.NewRequest(http.MethodGet, "/api/projects", nil))
|
|
if projectsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("projects list status: %d body=%s", projectsRecorder.Code, projectsRecorder.Body.String())
|
|
}
|
|
var projectsBody struct {
|
|
Items []map[string]any `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(projectsRecorder.Body.Bytes(), &projectsBody); err != nil {
|
|
t.Fatalf("decode projects list: %v", err)
|
|
}
|
|
var countedProject map[string]any
|
|
for _, item := range projectsBody.Items {
|
|
if item["id"] == projectID {
|
|
countedProject = item
|
|
break
|
|
}
|
|
}
|
|
if countedProject == nil {
|
|
t.Fatalf("expected created project in projects list")
|
|
}
|
|
if countedProject["workflowCount"].(float64) != 1 {
|
|
t.Fatalf("expected workflowCount=1 in projects list, got %v", countedProject["workflowCount"])
|
|
}
|
|
|
|
projectGetRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(projectGetRecorder, httptest.NewRequest(http.MethodGet, "/api/projects/"+projectID, nil))
|
|
if projectGetRecorder.Code != http.StatusOK {
|
|
t.Fatalf("project get status: %d body=%s", projectGetRecorder.Code, projectGetRecorder.Body.String())
|
|
}
|
|
var projectGetBody map[string]any
|
|
if err := json.Unmarshal(projectGetRecorder.Body.Bytes(), &projectGetBody); err != nil {
|
|
t.Fatalf("decode project get: %v", err)
|
|
}
|
|
if projectGetBody["workflowCount"].(float64) != 1 {
|
|
t.Fatalf("expected workflowCount=1 in project get, got %v", projectGetBody["workflowCount"])
|
|
}
|
|
|
|
runPayload := []byte(`{"trigger":"manual"}`)
|
|
runRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(runRecorder, httptest.NewRequest(http.MethodPost, "/api/workflows/"+createdWorkflow["id"].(string)+"/run", bytes.NewReader(runPayload)))
|
|
if runRecorder.Code != http.StatusAccepted {
|
|
t.Fatalf("run create status: %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
|
}
|
|
var queuedRun map[string]any
|
|
if err := json.Unmarshal(runRecorder.Body.Bytes(), &queuedRun); err != nil {
|
|
t.Fatalf("decode queued run: %v", err)
|
|
}
|
|
if queuedRun["status"].(string) != "queued" {
|
|
t.Fatalf("expected queued status, got %v", queuedRun["status"])
|
|
}
|
|
|
|
userPayload := []byte(`{"name":"API User","email":"api-user@example.com","role":"admin"}`)
|
|
userRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(userRecorder, httptest.NewRequest(http.MethodPost, "/api/users", bytes.NewReader(userPayload)))
|
|
if userRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("user create status: %d body=%s", userRecorder.Code, userRecorder.Body.String())
|
|
}
|
|
|
|
runsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(runsRecorder, httptest.NewRequest(http.MethodGet, "/api/runs", nil))
|
|
if runsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("runs list status: %d", runsRecorder.Code)
|
|
}
|
|
var runsBody struct {
|
|
Items []map[string]any `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(runsRecorder.Body.Bytes(), &runsBody); err != nil {
|
|
t.Fatalf("decode runs: %v", err)
|
|
}
|
|
foundRun := false
|
|
for _, item := range runsBody.Items {
|
|
if item["id"] == queuedRun["id"] {
|
|
foundRun = true
|
|
break
|
|
}
|
|
}
|
|
if !foundRun {
|
|
t.Fatalf("expected queued run in listing")
|
|
}
|
|
|
|
bucketsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(bucketsRecorder, httptest.NewRequest(http.MethodGet, "/api/storage/buckets?sourcePath="+storageRoot, nil))
|
|
if bucketsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("buckets status: %d", bucketsRecorder.Code)
|
|
}
|
|
var bucketsBody struct {
|
|
Items []string `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(bucketsRecorder.Body.Bytes(), &bucketsBody); err != nil {
|
|
t.Fatalf("decode buckets: %v", err)
|
|
}
|
|
if len(bucketsBody.Items) == 0 {
|
|
t.Fatalf("expected storage buckets")
|
|
}
|
|
|
|
objectsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
objectsRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/storage/objects?sourcePath="+storageRoot+"&bucket=incoming&prefix=", nil),
|
|
)
|
|
if objectsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("objects status: %d", objectsRecorder.Code)
|
|
}
|
|
|
|
missingSourcePathRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(missingSourcePathRecorder, httptest.NewRequest(http.MethodGet, "/api/storage/buckets", nil))
|
|
if missingSourcePathRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("missing sourcePath status: %d", missingSourcePathRecorder.Code)
|
|
}
|
|
|
|
aiRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(aiRecorder, httptest.NewRequest(http.MethodGet, "/api/ai/status", nil))
|
|
if aiRecorder.Code != http.StatusOK {
|
|
t.Fatalf("ai status: %d", aiRecorder.Code)
|
|
}
|
|
}
|
|
|
|
func TestOperatorsListEnsuresMongoWriteBuiltin(t *testing.T) {
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("operators list status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
var response struct {
|
|
Items []domain.OperatorRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
t.Fatalf("decode operators list: %v", err)
|
|
}
|
|
|
|
var mongoWrite *domain.OperatorRecord
|
|
for index := range response.Items {
|
|
item := response.Items[index]
|
|
if item.Name == "MongoDB 写入" {
|
|
mongoWrite = &item
|
|
break
|
|
}
|
|
}
|
|
if mongoWrite == nil {
|
|
t.Fatalf("expected MongoDB 写入 builtin operator to be present")
|
|
}
|
|
if mongoWrite.Category != "DATA" {
|
|
t.Fatalf("expected MongoDB 写入 category DATA, got %q", mongoWrite.Category)
|
|
}
|
|
if len(mongoWrite.Inputs) == 0 || len(mongoWrite.Outputs) == 0 {
|
|
t.Fatalf("expected MongoDB 写入 ports to be defined")
|
|
}
|
|
foundTarget := false
|
|
for _, param := range mongoWrite.Hyperparams {
|
|
if param.Key == "MONGO_TARGET" && param.Value == "business" {
|
|
foundTarget = true
|
|
break
|
|
}
|
|
}
|
|
if !foundTarget {
|
|
t.Fatalf("expected MongoDB 写入 to default MONGO_TARGET=business")
|
|
}
|
|
}
|
|
|
|
func TestOperatorsListEnsuresRegressionBuiltinCatalog(t *testing.T) {
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("operators list status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
var response struct {
|
|
Items []domain.OperatorRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
t.Fatalf("decode operators list: %v", err)
|
|
}
|
|
|
|
byName := make(map[string]domain.OperatorRecord, len(response.Items))
|
|
for _, item := range response.Items {
|
|
byName[item.Name] = item
|
|
}
|
|
|
|
for _, name := range []string{
|
|
"真实数据源",
|
|
"BOS 增量扫描",
|
|
"目录扫描",
|
|
"模型推理",
|
|
"X 平台数据集索引",
|
|
"采集目录校验",
|
|
"JSON 字段提取",
|
|
"文本模板渲染",
|
|
"JSON 过滤",
|
|
"JSON 映射重命名",
|
|
"JSON 转 CSV",
|
|
"CSV 转 JSON",
|
|
"条件分流",
|
|
"数据合并",
|
|
"视频质检",
|
|
"数据过滤",
|
|
"视频切片",
|
|
"视频标注",
|
|
"成品打包",
|
|
"抽检采样",
|
|
"人工审核",
|
|
"MongoDB 写入",
|
|
"LeRobot 数据集概览",
|
|
"ROSBag 数据概览",
|
|
"结果上传",
|
|
"Shell 命令执行",
|
|
} {
|
|
if _, ok := byName[name]; !ok {
|
|
t.Fatalf("expected builtin operator %q to be present", name)
|
|
}
|
|
}
|
|
|
|
sampling := byName["抽检采样"]
|
|
foundRules := false
|
|
for _, param := range sampling.Hyperparams {
|
|
if param.Key == "RULES_JSON" {
|
|
foundRules = true
|
|
break
|
|
}
|
|
}
|
|
if !foundRules {
|
|
t.Fatalf("expected 抽检采样 to expose RULES_JSON")
|
|
}
|
|
}
|
|
|
|
func TestBuiltinOperatorCatalogLoadsDeclarativeRecords(t *testing.T) {
|
|
records, err := builtinOperatorRecords("2026-06-16T00:00:00Z", "runner:test")
|
|
if err != nil {
|
|
t.Fatalf("load builtin operator catalog: %v", err)
|
|
}
|
|
if len(records) < 20 {
|
|
t.Fatalf("expected builtin catalog to have real operator coverage, got %d", len(records))
|
|
}
|
|
|
|
seenIDs := map[string]struct{}{}
|
|
seenNames := map[string]struct{}{}
|
|
foundMongoWrite := false
|
|
for _, item := range records {
|
|
if item.ID == "" || item.Name == "" || item.Category == "" || item.Description == "" {
|
|
t.Fatalf("expected required catalog fields to be populated: %+v", item)
|
|
}
|
|
if item.Type != "builtin" {
|
|
t.Fatalf("expected catalog item %s to be builtin, got %q", item.ID, item.Type)
|
|
}
|
|
if item.Image != "runner:test" {
|
|
t.Fatalf("expected image injection for %s, got %q", item.ID, item.Image)
|
|
}
|
|
if item.CreatedAt != "2026-06-16T00:00:00Z" || item.UpdatedAt != "2026-06-16T00:00:00Z" {
|
|
t.Fatalf("expected timestamp injection for %s", item.ID)
|
|
}
|
|
if _, exists := seenIDs[item.ID]; exists {
|
|
t.Fatalf("duplicate builtin id %q", item.ID)
|
|
}
|
|
seenIDs[item.ID] = struct{}{}
|
|
if _, exists := seenNames[item.Name]; exists {
|
|
t.Fatalf("duplicate builtin name %q", item.Name)
|
|
}
|
|
seenNames[item.Name] = struct{}{}
|
|
if item.Name == "MongoDB 写入" {
|
|
foundMongoWrite = true
|
|
}
|
|
}
|
|
if !foundMongoWrite {
|
|
t.Fatalf("expected declarative catalog to include MongoDB 写入")
|
|
}
|
|
}
|
|
|
|
func TestOperatorsListRefreshesBuiltinOperatorImages(t *testing.T) {
|
|
t.Setenv("OPERATOR_RUNNER_IMAGE", "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:20260416-workspacefix-amd64")
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
item, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
|
Name: "BOS 增量扫描",
|
|
Type: "builtin",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Image: "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:old",
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create operator: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("operators list status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
stored, err := dataStore.GetOperator(ctx, item.ID)
|
|
if err != nil {
|
|
t.Fatalf("get operator: %v", err)
|
|
}
|
|
if stored == nil {
|
|
t.Fatalf("expected operator to exist")
|
|
}
|
|
if stored.Image != "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:20260416-workspacefix-amd64" {
|
|
t.Fatalf("expected builtin operator image to refresh, got %q", stored.Image)
|
|
}
|
|
}
|
|
|
|
func TestOperatorsDeleteKeepsBuiltinOperatorDeleted(t *testing.T) {
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
|
|
listRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
|
if listRecorder.Code != http.StatusOK {
|
|
t.Fatalf("operators list status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
|
}
|
|
|
|
var listResponse struct {
|
|
Items []domain.OperatorRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(listRecorder.Body.Bytes(), &listResponse); err != nil {
|
|
t.Fatalf("decode operators list: %v", err)
|
|
}
|
|
|
|
var builtin *domain.OperatorRecord
|
|
for index := range listResponse.Items {
|
|
item := listResponse.Items[index]
|
|
if item.Type == "builtin" && item.Name == "真实数据源" {
|
|
builtin = &item
|
|
break
|
|
}
|
|
}
|
|
if builtin == nil {
|
|
t.Fatalf("expected builtin operator to exist before delete")
|
|
}
|
|
|
|
deleteRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
deleteRecorder,
|
|
httptest.NewRequest(http.MethodDelete, "/api/operators/"+builtin.ID, nil),
|
|
)
|
|
if deleteRecorder.Code != http.StatusNoContent {
|
|
t.Fatalf("delete builtin status: %d body=%s", deleteRecorder.Code, deleteRecorder.Body.String())
|
|
}
|
|
|
|
listAfterDeleteRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(listAfterDeleteRecorder, httptest.NewRequest(http.MethodGet, "/api/operators", nil))
|
|
if listAfterDeleteRecorder.Code != http.StatusOK {
|
|
t.Fatalf("operators list after delete status: %d body=%s", listAfterDeleteRecorder.Code, listAfterDeleteRecorder.Body.String())
|
|
}
|
|
|
|
var listAfterDeleteResponse struct {
|
|
Items []domain.OperatorRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(listAfterDeleteRecorder.Body.Bytes(), &listAfterDeleteResponse); err != nil {
|
|
t.Fatalf("decode operators list after delete: %v", err)
|
|
}
|
|
for _, item := range listAfterDeleteResponse.Items {
|
|
if item.Name == builtin.Name {
|
|
t.Fatalf("expected builtin operator %q to stay deleted", builtin.Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserListsCollectionsAndQueriesDocuments(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
if _, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "数据库浏览项目",
|
|
Description: "用于数据库浏览测试",
|
|
}); err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
|
Name: "JSON 测试算子",
|
|
Type: "builtin",
|
|
Category: "DATA",
|
|
Description: "用于数据库过滤查询测试",
|
|
Inputs: []domain.PortDefinition{{ID: "input", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "output", Name: "output", Type: domain.PortTypeJSON}},
|
|
}); err != nil {
|
|
t.Fatalf("create operator: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
overviewRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(overviewRecorder, httptest.NewRequest(http.MethodGet, "/api/database?target=platform", nil))
|
|
if overviewRecorder.Code != http.StatusOK {
|
|
t.Fatalf("database overview status: %d body=%s", overviewRecorder.Code, overviewRecorder.Body.String())
|
|
}
|
|
|
|
var overview struct {
|
|
Database string `json:"database"`
|
|
Collections []struct {
|
|
Name string `json:"name"`
|
|
DocumentCount int64 `json:"documentCount"`
|
|
} `json:"collections"`
|
|
}
|
|
if err := json.Unmarshal(overviewRecorder.Body.Bytes(), &overview); err != nil {
|
|
t.Fatalf("decode database overview: %v", err)
|
|
}
|
|
if overview.Database == "" {
|
|
t.Fatalf("expected database name in overview response")
|
|
}
|
|
if len(overview.Collections) == 0 {
|
|
t.Fatalf("expected collections in overview response")
|
|
}
|
|
|
|
foundOperatorsCollection := false
|
|
for _, item := range overview.Collections {
|
|
if item.Name == "operators" {
|
|
foundOperatorsCollection = true
|
|
if item.DocumentCount < 1 {
|
|
t.Fatalf("expected operators collection to have documents, got %d", item.DocumentCount)
|
|
}
|
|
}
|
|
}
|
|
if !foundOperatorsCollection {
|
|
t.Fatalf("expected operators collection in overview")
|
|
}
|
|
|
|
filter := url.QueryEscape(`{"category":"DATA"}`)
|
|
queryRecorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodGet, "/api/database/collections/operators/documents?target=platform&filter="+filter+"&limit=10", nil)
|
|
handler.ServeHTTP(queryRecorder, request)
|
|
if queryRecorder.Code != http.StatusOK {
|
|
t.Fatalf("database documents status: %d body=%s", queryRecorder.Code, queryRecorder.Body.String())
|
|
}
|
|
|
|
var result struct {
|
|
Collection string `json:"collection"`
|
|
Documents []map[string]any `json:"documents"`
|
|
Limit int `json:"limit"`
|
|
Total int64 `json:"total"`
|
|
Filter map[string]any `json:"filter"`
|
|
}
|
|
if err := json.Unmarshal(queryRecorder.Body.Bytes(), &result); err != nil {
|
|
t.Fatalf("decode database documents: %v", err)
|
|
}
|
|
if result.Collection != "operators" {
|
|
t.Fatalf("expected operators collection, got %q", result.Collection)
|
|
}
|
|
if result.Total < 1 || len(result.Documents) < 1 {
|
|
t.Fatalf("expected at least one operator document, total=%d len=%d", result.Total, len(result.Documents))
|
|
}
|
|
if result.Filter["category"] != "DATA" {
|
|
t.Fatalf("expected filter to round-trip, got %#v", result.Filter)
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserRejectsInvalidDocumentPagination(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
|
Name: "分页测试算子",
|
|
Type: "builtin",
|
|
Category: "DATA",
|
|
Description: "用于数据库分页参数测试",
|
|
}); err != nil {
|
|
t.Fatalf("create operator: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
request := httptest.NewRequest(http.MethodGet, "/api/database/collections/operators/documents?target=platform&limit=0&skip=-1", nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("invalid pagination status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, `"field":"limit"`) || !strings.Contains(body, "limit must be greater than or equal to 1") {
|
|
t.Fatalf("expected limit validation issue, got %s", body)
|
|
}
|
|
if !strings.Contains(body, `"field":"skip"`) || !strings.Contains(body, "skip must be zero or greater") {
|
|
t.Fatalf("expected skip validation issue, got %s", body)
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserRejectsNonIntegerDocumentLimit(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
|
Name: "分页非整数测试算子",
|
|
Type: "builtin",
|
|
Category: "DATA",
|
|
Description: "用于数据库分页非整数参数测试",
|
|
}); err != nil {
|
|
t.Fatalf("create operator: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
request := httptest.NewRequest(http.MethodGet, "/api/database/collections/operators/documents?target=platform&limit=1.5", nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("non-integer pagination status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, `"field":"limit"`) || !strings.Contains(body, "limit must be an integer") {
|
|
t.Fatalf("expected limit integer validation issue, got %s", body)
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserRejectsNegativeDocumentLimitAsPositiveRequired(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
|
Name: "分页负数测试算子",
|
|
Type: "builtin",
|
|
Category: "DATA",
|
|
Description: "用于数据库分页负数参数测试",
|
|
}); err != nil {
|
|
t.Fatalf("create operator: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
request := httptest.NewRequest(http.MethodGet, "/api/database/collections/operators/documents?target=platform&limit=-1", nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("negative limit status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, `"field":"limit"`) || !strings.Contains(body, "limit must be greater than or equal to 1") {
|
|
t.Fatalf("expected positive limit validation issue, got %s", body)
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserClampsDocumentPaginationBounds(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
|
Name: "分页上限测试算子",
|
|
Type: "builtin",
|
|
Category: "DATA",
|
|
Description: "用于数据库分页上限测试",
|
|
}); err != nil {
|
|
t.Fatalf("create operator: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
request := httptest.NewRequest(http.MethodGet, "/api/database/collections/operators/documents?target=platform&limit=999&skip=9999", nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("clamped pagination status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var result struct {
|
|
Limit int `json:"limit"`
|
|
Skip int `json:"skip"`
|
|
Total int `json:"total"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &result); err != nil {
|
|
t.Fatalf("decode clamped pagination response: %v", err)
|
|
}
|
|
if result.Limit != 200 {
|
|
t.Fatalf("expected limit to clamp to 200, got %d", result.Limit)
|
|
}
|
|
if result.Skip != result.Total {
|
|
t.Fatalf("expected skip to clamp to available total after store query, got skip=%d total=%d", result.Skip, result.Total)
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserDefaultsToBusinessTargetWhenUnspecified(t *testing.T) {
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
|
|
overviewRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(overviewRecorder, httptest.NewRequest(http.MethodGet, "/api/database", nil))
|
|
if overviewRecorder.Code != http.StatusOK {
|
|
t.Fatalf("database overview status: %d body=%s", overviewRecorder.Code, overviewRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(overviewRecorder.Body.String(), `"target":"business"`) {
|
|
t.Fatalf("expected default target=business, got %s", overviewRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(overviewRecorder.Body.String(), `"configured":false`) {
|
|
t.Fatalf("expected configured=false for unconfigured business mongo, got %s", overviewRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserUsesMongoConnectionWhenProvided(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
|
Name: "业务库",
|
|
URI: "mongodb://business.example:27017",
|
|
Database: "business",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create mongo connection: %v", err)
|
|
}
|
|
|
|
originalResolver := mongoConnectionStoreResolver
|
|
t.Cleanup(func() {
|
|
mongoConnectionStoreResolver = originalResolver
|
|
})
|
|
businessStore := store.NewMemoryStore()
|
|
if _, err := businessStore.CreateProject(ctx, domain.CreateProjectInput{Name: "业务项目", Description: "业务库"}); err != nil {
|
|
t.Fatalf("create business project: %v", err)
|
|
}
|
|
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
|
if item == nil || item.ID != connection.ID || item.URI != "mongodb://business.example:27017" || item.Database != "business" {
|
|
t.Fatalf("unexpected mongo connection: %#v", item)
|
|
}
|
|
return businessStore, func() {}, nil
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
overviewRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(overviewRecorder, httptest.NewRequest(http.MethodGet, "/api/database?connectionId="+connection.ID, nil))
|
|
if overviewRecorder.Code != http.StatusOK {
|
|
t.Fatalf("business database overview status: %d body=%s", overviewRecorder.Code, overviewRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(overviewRecorder.Body.String(), `"target":"business"`) {
|
|
t.Fatalf("expected business target in overview, got %s", overviewRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(overviewRecorder.Body.String(), `"connectionId":"`+connection.ID+`"`) {
|
|
t.Fatalf("expected connection id in overview, got %s", overviewRecorder.Body.String())
|
|
}
|
|
|
|
queryRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(queryRecorder, httptest.NewRequest(http.MethodGet, "/api/database/collections/projects/documents?connectionId="+connection.ID+"&limit=5", nil))
|
|
if queryRecorder.Code != http.StatusOK {
|
|
t.Fatalf("business database documents status: %d body=%s", queryRecorder.Code, queryRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(queryRecorder.Body.String(), `"configured":true`) {
|
|
t.Fatalf("expected configured=true, got %s", queryRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(queryRecorder.Body.String(), `"target":"business"`) {
|
|
t.Fatalf("expected business target in query result, got %s", queryRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserBusinessCollectionAndDocumentCRUD(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
|
Name: "业务写入库",
|
|
URI: "mongodb://business.example:27017",
|
|
Database: "business",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create mongo connection: %v", err)
|
|
}
|
|
|
|
originalResolver := mongoConnectionStoreResolver
|
|
t.Cleanup(func() {
|
|
mongoConnectionStoreResolver = originalResolver
|
|
})
|
|
businessStore := store.NewMemoryStore()
|
|
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
|
if item == nil || item.ID != connection.ID {
|
|
t.Fatalf("unexpected mongo connection: %#v", item)
|
|
}
|
|
return businessStore, func() {}, nil
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
connectionQuery := "?connectionId=" + connection.ID
|
|
|
|
createCollectionRecorder := httptest.NewRecorder()
|
|
createCollectionBody := bytes.NewBufferString(`{"name":"orders"}`)
|
|
handler.ServeHTTP(
|
|
createCollectionRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/database/collections"+connectionQuery, createCollectionBody),
|
|
)
|
|
if createCollectionRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("create collection status: %d body=%s", createCollectionRecorder.Code, createCollectionRecorder.Body.String())
|
|
}
|
|
|
|
createDocumentRecorder := httptest.NewRecorder()
|
|
createDocumentBody := bytes.NewBufferString(`{"document":{"orderNo":"SO-001","status":"new","amount":12}}`)
|
|
handler.ServeHTTP(
|
|
createDocumentRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/database/collections/orders/documents"+connectionQuery, createDocumentBody),
|
|
)
|
|
if createDocumentRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("create document status: %d body=%s", createDocumentRecorder.Code, createDocumentRecorder.Body.String())
|
|
}
|
|
var createdDocument struct {
|
|
Collection string `json:"collection"`
|
|
Document map[string]any `json:"document"`
|
|
}
|
|
if err := json.Unmarshal(createDocumentRecorder.Body.Bytes(), &createdDocument); err != nil {
|
|
t.Fatalf("decode created document: %v", err)
|
|
}
|
|
documentID, _ := createdDocument.Document["_id"].(string)
|
|
if documentID == "" {
|
|
t.Fatalf("expected created document id, got %#v", createdDocument.Document["_id"])
|
|
}
|
|
|
|
queryRecorder := httptest.NewRecorder()
|
|
filter := url.QueryEscape(`{"orderNo":"SO-001"}`)
|
|
handler.ServeHTTP(
|
|
queryRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/database/collections/orders/documents"+connectionQuery+"&filter="+filter, nil),
|
|
)
|
|
if queryRecorder.Code != http.StatusOK {
|
|
t.Fatalf("query documents status: %d body=%s", queryRecorder.Code, queryRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(queryRecorder.Body.String(), `"total":1`) {
|
|
t.Fatalf("expected one query result, got %s", queryRecorder.Body.String())
|
|
}
|
|
|
|
updateDocumentRecorder := httptest.NewRecorder()
|
|
updateDocumentBody := bytes.NewBufferString(`{"document":{"orderNo":"SO-001","status":"done","amount":18}}`)
|
|
handler.ServeHTTP(
|
|
updateDocumentRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/database/collections/orders/documents/"+documentID+connectionQuery, updateDocumentBody),
|
|
)
|
|
if updateDocumentRecorder.Code != http.StatusOK {
|
|
t.Fatalf("update document status: %d body=%s", updateDocumentRecorder.Code, updateDocumentRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(updateDocumentRecorder.Body.String(), `"status":"done"`) {
|
|
t.Fatalf("expected updated status in response, got %s", updateDocumentRecorder.Body.String())
|
|
}
|
|
|
|
renameCollectionRecorder := httptest.NewRecorder()
|
|
renameCollectionBody := bytes.NewBufferString(`{"newName":"ordersArchive"}`)
|
|
handler.ServeHTTP(
|
|
renameCollectionRecorder,
|
|
httptest.NewRequest(http.MethodPatch, "/api/database/collections/orders"+connectionQuery, renameCollectionBody),
|
|
)
|
|
if renameCollectionRecorder.Code != http.StatusOK {
|
|
t.Fatalf("rename collection status: %d body=%s", renameCollectionRecorder.Code, renameCollectionRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(renameCollectionRecorder.Body.String(), `"collection":"ordersArchive"`) {
|
|
t.Fatalf("expected renamed collection in response, got %s", renameCollectionRecorder.Body.String())
|
|
}
|
|
|
|
deleteDocumentRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
deleteDocumentRecorder,
|
|
httptest.NewRequest(http.MethodDelete, "/api/database/collections/ordersArchive/documents/"+documentID+connectionQuery, nil),
|
|
)
|
|
if deleteDocumentRecorder.Code != http.StatusOK {
|
|
t.Fatalf("delete document status: %d body=%s", deleteDocumentRecorder.Code, deleteDocumentRecorder.Body.String())
|
|
}
|
|
|
|
deleteCollectionRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
deleteCollectionRecorder,
|
|
httptest.NewRequest(http.MethodDelete, "/api/database/collections/ordersArchive"+connectionQuery, nil),
|
|
)
|
|
if deleteCollectionRecorder.Code != http.StatusOK {
|
|
t.Fatalf("delete collection status: %d body=%s", deleteCollectionRecorder.Code, deleteCollectionRecorder.Body.String())
|
|
}
|
|
|
|
overviewRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(overviewRecorder, httptest.NewRequest(http.MethodGet, "/api/database"+connectionQuery, nil))
|
|
if overviewRecorder.Code != http.StatusOK {
|
|
t.Fatalf("overview status: %d body=%s", overviewRecorder.Code, overviewRecorder.Body.String())
|
|
}
|
|
if strings.Contains(overviewRecorder.Body.String(), "ordersArchive") {
|
|
t.Fatalf("expected deleted collection to be absent, got %s", overviewRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserBusinessIndexCRUD(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
|
Name: "业务索引库",
|
|
URI: "mongodb://business.example:27017",
|
|
Database: "business",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create mongo connection: %v", err)
|
|
}
|
|
|
|
originalResolver := mongoConnectionStoreResolver
|
|
t.Cleanup(func() {
|
|
mongoConnectionStoreResolver = originalResolver
|
|
})
|
|
businessStore := store.NewMemoryStore()
|
|
if _, err := businessStore.CreateDatabaseCollection(ctx, "events"); err != nil {
|
|
t.Fatalf("create collection: %v", err)
|
|
}
|
|
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
|
if item == nil || item.ID != connection.ID {
|
|
t.Fatalf("unexpected mongo connection: %#v", item)
|
|
}
|
|
return businessStore, func() {}, nil
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
connectionQuery := "?connectionId=" + connection.ID
|
|
|
|
createIndexRecorder := httptest.NewRecorder()
|
|
createIndexBody := bytes.NewBufferString(`{"name":"idx-record-key","keys":{"recordKey":1},"unique":true}`)
|
|
handler.ServeHTTP(
|
|
createIndexRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/database/collections/events/indexes"+connectionQuery, createIndexBody),
|
|
)
|
|
if createIndexRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("create index status: %d body=%s", createIndexRecorder.Code, createIndexRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(createIndexRecorder.Body.String(), `"name":"idx-record-key"`) {
|
|
t.Fatalf("expected created index name, got %s", createIndexRecorder.Body.String())
|
|
}
|
|
|
|
listIndexesRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
listIndexesRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/database/collections/events/indexes"+connectionQuery, nil),
|
|
)
|
|
if listIndexesRecorder.Code != http.StatusOK {
|
|
t.Fatalf("list indexes status: %d body=%s", listIndexesRecorder.Code, listIndexesRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(listIndexesRecorder.Body.String(), `"idx-record-key"`) {
|
|
t.Fatalf("expected created index in list, got %s", listIndexesRecorder.Body.String())
|
|
}
|
|
|
|
deleteIndexRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
deleteIndexRecorder,
|
|
httptest.NewRequest(http.MethodDelete, "/api/database/collections/events/indexes/idx-record-key"+connectionQuery, nil),
|
|
)
|
|
if deleteIndexRecorder.Code != http.StatusOK {
|
|
t.Fatalf("delete index status: %d body=%s", deleteIndexRecorder.Code, deleteIndexRecorder.Body.String())
|
|
}
|
|
if strings.Contains(deleteIndexRecorder.Body.String(), `"deleted":false`) {
|
|
t.Fatalf("expected index deletion success, got %s", deleteIndexRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserRejectsInvalidBusinessWriteInputsBeforeStoreAccess(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
|
Name: "业务校验库",
|
|
URI: "mongodb://business.example:27017",
|
|
Database: "business",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create mongo connection: %v", err)
|
|
}
|
|
|
|
originalResolver := mongoConnectionStoreResolver
|
|
t.Cleanup(func() {
|
|
mongoConnectionStoreResolver = originalResolver
|
|
})
|
|
businessStore := store.NewMemoryStore()
|
|
if _, err := businessStore.CreateDatabaseCollection(ctx, "events"); err != nil {
|
|
t.Fatalf("create collection: %v", err)
|
|
}
|
|
resolverCalls := 0
|
|
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
|
resolverCalls++
|
|
if item == nil || item.ID != connection.ID {
|
|
t.Fatalf("unexpected mongo connection: %#v", item)
|
|
}
|
|
return businessStore, func() {}, nil
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
connectionQuery := "?connectionId=" + connection.ID
|
|
|
|
invalidCollectionRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidCollectionRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/database/collections"+connectionQuery, bytes.NewBufferString(`{"name":"bad/name"}`)),
|
|
)
|
|
if invalidCollectionRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid collection name rejected, got %d body=%s", invalidCollectionRecorder.Code, invalidCollectionRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidCollectionRecorder.Body.String(), `"field":"name"`) || !strings.Contains(invalidCollectionRecorder.Body.String(), "path separators") {
|
|
t.Fatalf("expected collection name validation issue, got %s", invalidCollectionRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 0 {
|
|
t.Fatalf("expected invalid collection name to be rejected before store access, resolver calls=%d", resolverCalls)
|
|
}
|
|
|
|
invalidIndexRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidIndexRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/database/collections/events/indexes"+connectionQuery, bytes.NewBufferString(`{"keys":{"$where":1}}`)),
|
|
)
|
|
if invalidIndexRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid index keys rejected, got %d body=%s", invalidIndexRecorder.Code, invalidIndexRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidIndexRecorder.Body.String(), `"field":"keys.$where"`) || !strings.Contains(invalidIndexRecorder.Body.String(), "Mongo operator") {
|
|
t.Fatalf("expected index key validation issue, got %s", invalidIndexRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 0 {
|
|
t.Fatalf("expected invalid index keys to be rejected before store access, resolver calls=%d", resolverCalls)
|
|
}
|
|
|
|
invalidDirectionRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidDirectionRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/database/collections/events/indexes"+connectionQuery, bytes.NewBufferString(`{"keys":{"recordKey":"ascending"}}`)),
|
|
)
|
|
if invalidDirectionRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid index direction rejected, got %d body=%s", invalidDirectionRecorder.Code, invalidDirectionRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidDirectionRecorder.Body.String(), `"field":"keys.recordKey"`) || !strings.Contains(invalidDirectionRecorder.Body.String(), "index key direction") {
|
|
t.Fatalf("expected index direction validation issue, got %s", invalidDirectionRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 0 {
|
|
t.Fatalf("expected invalid index direction to be rejected before store access, resolver calls=%d", resolverCalls)
|
|
}
|
|
|
|
invalidDocumentRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidDocumentRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/database/collections/events/documents"+connectionQuery, bytes.NewBufferString(`{"document":{"profile":{"$set":"admin"}}}`)),
|
|
)
|
|
if invalidDocumentRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid document rejected, got %d body=%s", invalidDocumentRecorder.Code, invalidDocumentRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidDocumentRecorder.Body.String(), `"field":"document.profile.$set"`) || !strings.Contains(invalidDocumentRecorder.Body.String(), "Mongo operator") {
|
|
t.Fatalf("expected document Mongo operator validation issue, got %s", invalidDocumentRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 0 {
|
|
t.Fatalf("expected invalid document to be rejected before store access, resolver calls=%d", resolverCalls)
|
|
}
|
|
|
|
invalidNULDocumentRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidNULDocumentRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/database/collections/events/documents"+connectionQuery, bytes.NewBufferString(`{"document":{"profile":{"bad\u0000key":"admin"}}}`)),
|
|
)
|
|
if invalidNULDocumentRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected NUL document key rejected, got %d body=%s", invalidNULDocumentRecorder.Code, invalidNULDocumentRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidNULDocumentRecorder.Body.String(), "NUL bytes") {
|
|
t.Fatalf("expected document NUL validation issue, got %s", invalidNULDocumentRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 0 {
|
|
t.Fatalf("expected NUL document key to be rejected before store access, resolver calls=%d", resolverCalls)
|
|
}
|
|
}
|
|
|
|
func TestDatabaseBrowserRejectsInvalidReadInputsBeforeStoreAccess(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
|
Name: "业务读取校验库",
|
|
URI: "mongodb://business.example:27017",
|
|
Database: "business",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create mongo connection: %v", err)
|
|
}
|
|
|
|
originalResolver := mongoConnectionStoreResolver
|
|
t.Cleanup(func() {
|
|
mongoConnectionStoreResolver = originalResolver
|
|
})
|
|
businessStore := store.NewMemoryStore()
|
|
if _, err := businessStore.CreateDatabaseCollection(ctx, "events"); err != nil {
|
|
t.Fatalf("create collection: %v", err)
|
|
}
|
|
resolverCalls := 0
|
|
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
|
resolverCalls++
|
|
if item == nil || item.ID != connection.ID {
|
|
t.Fatalf("unexpected mongo connection: %#v", item)
|
|
}
|
|
return businessStore, func() {}, nil
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
connectionQuery := "?connectionId=" + connection.ID
|
|
|
|
invalidIndexesRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidIndexesRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/database/collections/system.profile/indexes"+connectionQuery, nil),
|
|
)
|
|
if invalidIndexesRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid index collection rejected, got %d body=%s", invalidIndexesRecorder.Code, invalidIndexesRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidIndexesRecorder.Body.String(), `"field":"name"`) || !strings.Contains(invalidIndexesRecorder.Body.String(), "reserved system prefix") {
|
|
t.Fatalf("expected collection validation issue, got %s", invalidIndexesRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 0 {
|
|
t.Fatalf("expected invalid index collection to be rejected before store access, resolver calls=%d", resolverCalls)
|
|
}
|
|
|
|
invalidCollectionRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidCollectionRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/database/collections/bad$name/documents"+connectionQuery, nil),
|
|
)
|
|
if invalidCollectionRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid document collection rejected, got %d body=%s", invalidCollectionRecorder.Code, invalidCollectionRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidCollectionRecorder.Body.String(), `"field":"name"`) || !strings.Contains(invalidCollectionRecorder.Body.String(), "cannot contain $") {
|
|
t.Fatalf("expected document collection validation issue, got %s", invalidCollectionRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 0 {
|
|
t.Fatalf("expected invalid document collection to be rejected before store access, resolver calls=%d", resolverCalls)
|
|
}
|
|
|
|
invalidFilter := url.QueryEscape(`{"profile":{"$where":"return true"}}`)
|
|
invalidFilterRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidFilterRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/database/collections/events/documents"+connectionQuery+"&filter="+invalidFilter, nil),
|
|
)
|
|
if invalidFilterRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid filter rejected, got %d body=%s", invalidFilterRecorder.Code, invalidFilterRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidFilterRecorder.Body.String(), `"field":"filter.profile.$where"`) || !strings.Contains(invalidFilterRecorder.Body.String(), "$where") {
|
|
t.Fatalf("expected filter $where validation issue, got %s", invalidFilterRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 0 {
|
|
t.Fatalf("expected invalid filter to be rejected before store access, resolver calls=%d", resolverCalls)
|
|
}
|
|
}
|
|
|
|
func TestMongoConnectionsTestEndpointUsesDraftOrStoredConnection(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
storedConnection, err := dataStore.CreateMongoConnection(ctx, domain.CreateMongoConnectionInput{
|
|
Name: "业务测试库",
|
|
URI: "mongodb://business.example:27017",
|
|
Database: "business",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create mongo connection: %v", err)
|
|
}
|
|
|
|
originalResolver := mongoConnectionStoreResolver
|
|
t.Cleanup(func() {
|
|
mongoConnectionStoreResolver = originalResolver
|
|
})
|
|
probeStore := store.NewMemoryStore()
|
|
if _, err := probeStore.CreateDatabaseCollection(ctx, "probeCollection"); err != nil {
|
|
t.Fatalf("create probe collection: %v", err)
|
|
}
|
|
resolverCalls := 0
|
|
mongoConnectionStoreResolver = func(_ context.Context, item *domain.MongoConnectionRecord) (databaseBrowserStore, func(), error) {
|
|
resolverCalls++
|
|
if item == nil {
|
|
t.Fatalf("expected mongo connection")
|
|
}
|
|
return probeStore, func() {}, nil
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
invalidDraftRecorder := httptest.NewRecorder()
|
|
invalidDraftBody := bytes.NewBufferString(`{"uri":"mongodb://<user>:<password>@draft.example:27017","database":"draft-db"}`)
|
|
handler.ServeHTTP(invalidDraftRecorder, httptest.NewRequest(http.MethodPost, "/api/mongo/connections/test", invalidDraftBody))
|
|
if invalidDraftRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid draft connection test rejected, got %d body=%s", invalidDraftRecorder.Code, invalidDraftRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidDraftRecorder.Body.String(), "placeholder markers") {
|
|
t.Fatalf("expected draft URI validation issue, got %s", invalidDraftRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 0 {
|
|
t.Fatalf("expected invalid draft URI to be rejected before resolver, resolver calls=%d", resolverCalls)
|
|
}
|
|
|
|
draftRecorder := httptest.NewRecorder()
|
|
draftBody := bytes.NewBufferString(`{"uri":"mongodb://draft.example:27017","database":"draft-db"}`)
|
|
handler.ServeHTTP(draftRecorder, httptest.NewRequest(http.MethodPost, "/api/mongo/connections/test", draftBody))
|
|
if draftRecorder.Code != http.StatusOK {
|
|
t.Fatalf("draft connection test status: %d body=%s", draftRecorder.Code, draftRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(draftRecorder.Body.String(), `"probeCollection"`) {
|
|
t.Fatalf("expected probe collection in test response, got %s", draftRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 1 {
|
|
t.Fatalf("expected valid draft to call resolver once, got %d", resolverCalls)
|
|
}
|
|
|
|
storedRecorder := httptest.NewRecorder()
|
|
storedBody := bytes.NewBufferString(`{"id":"` + storedConnection.ID + `"}`)
|
|
handler.ServeHTTP(storedRecorder, httptest.NewRequest(http.MethodPost, "/api/mongo/connections/test", storedBody))
|
|
if storedRecorder.Code != http.StatusOK {
|
|
t.Fatalf("stored connection test status: %d body=%s", storedRecorder.Code, storedRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(storedRecorder.Body.String(), `"connectionId":"`+storedConnection.ID+`"`) {
|
|
t.Fatalf("expected stored connection id in response, got %s", storedRecorder.Body.String())
|
|
}
|
|
if resolverCalls != 2 {
|
|
t.Fatalf("expected stored connection test to call resolver, got %d", resolverCalls)
|
|
}
|
|
}
|
|
|
|
func TestRunsListReturnsSummaryPayload(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
run := domain.RunRecord{
|
|
ID: "run-summary-1",
|
|
WorkflowID: "workflow-1",
|
|
WorkflowName: "Summary Workflow",
|
|
ProjectID: "project-1",
|
|
ProjectName: "Summary Project",
|
|
Trigger: domain.RunTriggerScheduled,
|
|
Status: domain.RunStatusRunning,
|
|
CurrentNodeID: "node-quality",
|
|
NodeExecutions: []domain.NodeExecutionRecord{
|
|
{
|
|
NodeID: "node-quality",
|
|
NodeLabel: "视频质检",
|
|
Status: domain.NodeExecutionStatusRunning,
|
|
Logs: []domain.LogEntryRecord{
|
|
{Timestamp: "2026-04-13T01:00:00Z", Level: "INFO", Message: "heavy node log"},
|
|
},
|
|
Artifacts: []domain.ExecutionArtifact{
|
|
{ID: "artifact-1", RunID: "run-summary-1", NodeID: "node-quality", Name: "quality-report.json", Type: domain.PortTypeJSON, Size: "2 KB", Path: "/tmp/quality-report.json"},
|
|
},
|
|
ResourceSeries: []domain.ResourceSample{
|
|
{Timestamp: "2026-04-13T01:00:00Z", CPU: 90, Memory: 1024},
|
|
},
|
|
},
|
|
},
|
|
Logs: []domain.LogEntryRecord{
|
|
{Timestamp: "2026-04-13T01:00:00Z", Level: "INFO", Message: "top level log"},
|
|
},
|
|
ResourceSeries: []domain.ResourceSample{
|
|
{Timestamp: "2026-04-13T01:00:00Z", CPU: 80, Memory: 2048},
|
|
},
|
|
Artifacts: []domain.ExecutionArtifact{
|
|
{ID: "artifact-2", RunID: "run-summary-1", NodeID: "node-quality", Name: "clip.mp4", Type: domain.PortTypeVideo, Size: "120 MB", Path: "/tmp/clip.mp4"},
|
|
},
|
|
K8sRefs: []domain.K8sRef{
|
|
{NodeID: "node-quality", JobName: "quality-job", Namespace: "droboticflow"},
|
|
},
|
|
CreatedAt: "2026-04-13T01:00:00Z",
|
|
UpdatedAt: "2026-04-13T01:00:00Z",
|
|
}
|
|
if err := dataStore.UpsertRun(ctx, run); err != nil {
|
|
t.Fatalf("upsert run: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/runs", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("runs list status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
body := recorder.Body.String()
|
|
for _, forbidden := range []string{"\"logs\"", "\"artifacts\"", "\"resourceSeries\"", "\"nodeExecutions\"", "\"k8sRefs\""} {
|
|
if strings.Contains(body, forbidden) {
|
|
t.Fatalf("runs list should not include %s in summary payload: %s", forbidden, body)
|
|
}
|
|
}
|
|
|
|
var response struct {
|
|
Items []map[string]any `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
t.Fatalf("decode runs response: %v", err)
|
|
}
|
|
if len(response.Items) != 1 {
|
|
t.Fatalf("expected 1 run summary, got %d", len(response.Items))
|
|
}
|
|
if response.Items[0]["id"] != run.ID {
|
|
t.Fatalf("summary id = %v, want %s", response.Items[0]["id"], run.ID)
|
|
}
|
|
if response.Items[0]["currentNodeId"] != run.CurrentNodeID {
|
|
t.Fatalf("summary currentNodeId = %v, want %s", response.Items[0]["currentNodeId"], run.CurrentNodeID)
|
|
}
|
|
}
|
|
|
|
func TestPocAcceptanceImportCreatesPlatformRunArtifact(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
payload := map[string]any{
|
|
"result": map[string]any{
|
|
"runId": "poc-operator-acceptance-20260623010101",
|
|
"status": "passed",
|
|
"workspace": "/workspace/acceptance/poc-operator-acceptance-20260623010101",
|
|
"acceptanceScope": "dependency-isolation",
|
|
"fullExternalAcceptance": false,
|
|
"operatorCount": 28,
|
|
"operatorPassedCount": 26,
|
|
"coveredOperatorCount": 26,
|
|
"allOperatorCoverageComplete": false,
|
|
"blockingOperatorCoverageComplete": true,
|
|
"missingCoverage": []string{"dcloud_data_generation", "dcloud_embodied_annotation"},
|
|
"blockingMissingCoverage": []string{},
|
|
"requiredExternals": []string{"bos", "mongo"},
|
|
"statusCounts": map[string]any{"passed": 28, "skipped": 2},
|
|
"failedCount": 0,
|
|
"skippedCount": 2,
|
|
"optionalSkippedCount": 2,
|
|
"requiredSkippedCount": 0,
|
|
"externalVerificationPassed": []string{"bos", "mongo"},
|
|
"externalVerificationMissing": []string{},
|
|
"externalVerificationPassedCount": 2,
|
|
"externalVerificationRequiredCount": 2,
|
|
},
|
|
"source": map[string]any{
|
|
"environment": "poc",
|
|
"namespace": "droboticflow",
|
|
"jobName": "droboticflow-operator-acceptance-20260623010101",
|
|
"image": "ccr.example/droboticflow-operator-runner:tag",
|
|
"imageId": "ccr.example/droboticflow-operator-runner@sha256:abc",
|
|
},
|
|
}
|
|
rawPayload, err := json.Marshal(payload)
|
|
if err != nil {
|
|
t.Fatalf("marshal payload: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("import status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var imported struct {
|
|
Run domain.RunRecord `json:"run"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &imported); err != nil {
|
|
t.Fatalf("decode import response: %v", err)
|
|
}
|
|
if imported.Run.Status != domain.RunStatusCompleted {
|
|
t.Fatalf("run status = %s, want completed", imported.Run.Status)
|
|
}
|
|
if imported.Run.WorkflowID != pocAcceptanceWorkflowID || imported.Run.ProjectID != pocAcceptanceProjectID {
|
|
t.Fatalf("unexpected platform linkage: workflow=%s project=%s", imported.Run.WorkflowID, imported.Run.ProjectID)
|
|
}
|
|
|
|
listRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/runs", nil))
|
|
if listRecorder.Code != http.StatusOK {
|
|
t.Fatalf("runs list status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(listRecorder.Body.String(), imported.Run.ID) {
|
|
t.Fatalf("runs list should contain imported run: %s", listRecorder.Body.String())
|
|
}
|
|
|
|
artifactRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(artifactRecorder, httptest.NewRequest(http.MethodGet, "/api/runs/"+imported.Run.ID+"/artifacts", nil))
|
|
if artifactRecorder.Code != http.StatusOK {
|
|
t.Fatalf("artifacts status: %d body=%s", artifactRecorder.Code, artifactRecorder.Body.String())
|
|
}
|
|
var artifactResponse struct {
|
|
Items []domain.ExecutionArtifact `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(artifactRecorder.Body.Bytes(), &artifactResponse); err != nil {
|
|
t.Fatalf("decode artifacts: %v", err)
|
|
}
|
|
if len(artifactResponse.Items) != 1 || artifactResponse.Items[0].Name != "poc-operator-full-acceptance-result.json" {
|
|
t.Fatalf("unexpected artifacts: %+v", artifactResponse.Items)
|
|
}
|
|
if artifactResponse.Items[0].Data["acceptanceScope"] != "dependency-isolation" {
|
|
t.Fatalf("artifact data missing acceptance summary: %+v", artifactResponse.Items[0].Data)
|
|
}
|
|
|
|
secondRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(secondRecorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
|
if secondRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("second import status: %d body=%s", secondRecorder.Code, secondRecorder.Body.String())
|
|
}
|
|
listAgainRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(listAgainRecorder, httptest.NewRequest(http.MethodGet, "/api/runs", nil))
|
|
var listAgain struct {
|
|
Items []domain.RunSummary `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(listAgainRecorder.Body.Bytes(), &listAgain); err != nil {
|
|
t.Fatalf("decode runs after second import: %v", err)
|
|
}
|
|
if len(listAgain.Items) != 1 {
|
|
t.Fatalf("reimport should update the same run, got %d runs", len(listAgain.Items))
|
|
}
|
|
}
|
|
|
|
func TestPocAcceptanceImportAllowsSharedAPITokenWhenSessionAuthIsEnabled(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-acceptance-token")
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
payload := map[string]any{
|
|
"result": map[string]any{
|
|
"runId": "poc-strict-preflight-token",
|
|
"status": "failed",
|
|
"acceptanceScope": "full",
|
|
"fullExternalAcceptance": false,
|
|
"operatorCount": 28,
|
|
"operatorPassedCount": 0,
|
|
"coveredOperatorCount": 0,
|
|
"allOperatorCoverageComplete": false,
|
|
"blockingOperatorCoverageComplete": false,
|
|
"missingCoverage": []string{"dcloud_data_generation", "dcloud_embodied_annotation"},
|
|
"blockingMissingCoverage": []string{"dcloud_data_generation", "dcloud_embodied_annotation"},
|
|
"failedCount": 1,
|
|
},
|
|
}
|
|
rawPayload, err := json.Marshal(payload)
|
|
if err != nil {
|
|
t.Fatalf("marshal payload: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload))
|
|
request.Header.Set("Authorization", "Bearer shared-acceptance-token")
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("import status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
projectRecorder := httptest.NewRecorder()
|
|
projectRequest := httptest.NewRequest(http.MethodPost, "/api/projects", bytes.NewReader([]byte(`{"name":"Token Should Not Create Project"}`)))
|
|
projectRequest.Header.Set("Authorization", "Bearer shared-acceptance-token")
|
|
handler.ServeHTTP(projectRecorder, projectRequest)
|
|
if projectRecorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("expected regular business writes to still require login, got %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestPocAcceptanceImportRequiresLoginOrAPITokenWhenSessionAuthIsEnabled(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
t.Setenv("DROBOTICFLOW_API_TOKEN", "shared-acceptance-token")
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader([]byte(`{"result":{"status":"passed"}}`))),
|
|
)
|
|
if recorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "API token is required") {
|
|
t.Fatalf("expected API token error, got %s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestPocAcceptanceImportValidatesRequiredSummaryFields(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader([]byte(`{"result":{"status":"passed"}}`))),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
for _, field := range []string{"result.runId", "result.acceptanceScope", "result.fullExternalAcceptance", "result.operatorCount", "result.operatorPassedCount"} {
|
|
if !strings.Contains(body, field) {
|
|
t.Fatalf("expected validation field %s in %s", field, body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPocAcceptanceImportRejectsInconsistentFullAcceptance(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
payload := map[string]any{
|
|
"result": map[string]any{
|
|
"runId": "poc-operator-acceptance-fake-full",
|
|
"status": "passed",
|
|
"acceptanceScope": "dependency-isolation",
|
|
"fullExternalAcceptance": true,
|
|
"operatorCount": 28,
|
|
"operatorPassedCount": 26,
|
|
"coveredOperatorCount": 26,
|
|
"allOperatorCoverageComplete": false,
|
|
"blockingOperatorCoverageComplete": true,
|
|
"missingCoverage": []string{"dcloud_data_generation", "dcloud_embodied_annotation"},
|
|
"blockingMissingCoverage": []string{},
|
|
"failedCount": 0,
|
|
"requiredSkippedCount": 0,
|
|
},
|
|
}
|
|
rawPayload, err := json.Marshal(payload)
|
|
if err != nil {
|
|
t.Fatalf("marshal payload: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
for _, field := range []string{
|
|
"result.acceptanceScope",
|
|
"result.requiredExternals",
|
|
"result.externalVerificationPassed",
|
|
"result.externalVerificationPassedCount",
|
|
"result.externalVerificationRequiredCount",
|
|
"result.operatorPassedCount",
|
|
"result.coveredOperatorCount",
|
|
"result.allOperatorCoverageComplete",
|
|
"result.missingCoverage",
|
|
} {
|
|
if !strings.Contains(body, field) {
|
|
t.Fatalf("expected validation field %s in %s", field, body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPocAcceptanceImportRejectsIncompleteExternalVerification(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
payload := map[string]any{
|
|
"result": map[string]any{
|
|
"runId": "poc-operator-acceptance-missing-external-proof",
|
|
"status": "passed",
|
|
"acceptanceScope": "full",
|
|
"fullExternalAcceptance": true,
|
|
"operatorCount": 28,
|
|
"operatorPassedCount": 28,
|
|
"coveredOperatorCount": 28,
|
|
"allOperatorCoverageComplete": true,
|
|
"blockingOperatorCoverageComplete": true,
|
|
"missingCoverage": []string{},
|
|
"blockingMissingCoverage": []string{},
|
|
"requiredExternals": []string{"bos", "dcloud", "mongo"},
|
|
"externalVerificationPassed": []string{"bos", "mongo"},
|
|
"externalVerificationMissing": []string{"dcloud"},
|
|
"externalVerificationPassedCount": 2,
|
|
"externalVerificationRequiredCount": 3,
|
|
"failedCount": 0,
|
|
"requiredSkippedCount": 0,
|
|
"generatedFixtureCount": 0,
|
|
"realFixtureCount": 6,
|
|
"allFixturesReal": true,
|
|
"fixtureSources": map[string]any{
|
|
"allowGeneratedFixtures": false,
|
|
"items": map[string]any{
|
|
"xplatform": map[string]any{"path": "/workspace/fixtures/xplatform", "generated": false},
|
|
"csv": map[string]any{"path": "/workspace/fixtures/records.csv", "generated": false},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
rawPayload, err := json.Marshal(payload)
|
|
if err != nil {
|
|
t.Fatalf("marshal payload: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
for _, field := range []string{
|
|
"result.externalVerificationPassed",
|
|
"result.externalVerificationMissing",
|
|
"result.externalVerificationPassedCount",
|
|
} {
|
|
if !strings.Contains(body, field) {
|
|
t.Fatalf("expected validation field %s in %s", field, body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPocAcceptanceImportRejectsGeneratedFixtureFullAcceptance(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
payload := map[string]any{
|
|
"result": map[string]any{
|
|
"runId": "poc-operator-acceptance-generated-fixtures",
|
|
"status": "passed",
|
|
"acceptanceScope": "full",
|
|
"fullExternalAcceptance": true,
|
|
"operatorCount": 28,
|
|
"operatorPassedCount": 28,
|
|
"coveredOperatorCount": 28,
|
|
"allOperatorCoverageComplete": true,
|
|
"blockingOperatorCoverageComplete": true,
|
|
"missingCoverage": []string{},
|
|
"blockingMissingCoverage": []string{},
|
|
"requiredExternals": []string{"bos", "dcloud", "mongo"},
|
|
"externalVerificationPassed": []string{"bos", "dcloud", "mongo"},
|
|
"externalVerificationMissing": []string{},
|
|
"externalVerificationPassedCount": 3,
|
|
"externalVerificationRequiredCount": 3,
|
|
"failedCount": 0,
|
|
"requiredSkippedCount": 0,
|
|
"generatedFixtureCount": 1,
|
|
"realFixtureCount": 5,
|
|
"allFixturesReal": false,
|
|
"fixtureSources": map[string]any{
|
|
"allowGeneratedFixtures": true,
|
|
"items": map[string]any{
|
|
"xplatform": map[string]any{"path": "/workspace/acceptance/generated/xplatform", "generated": true},
|
|
"csv": map[string]any{"path": "/workspace/fixtures/records.csv", "generated": false},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
rawPayload, err := json.Marshal(payload)
|
|
if err != nil {
|
|
t.Fatalf("marshal payload: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/acceptance/poc-operator-result", bytes.NewReader(rawPayload)))
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
for _, field := range []string{
|
|
"result.generatedFixtureCount",
|
|
"result.allFixturesReal",
|
|
"result.fixtureSources.allowGeneratedFixtures",
|
|
"result.fixtureSources.items",
|
|
} {
|
|
if !strings.Contains(body, field) {
|
|
t.Fatalf("expected validation field %s in %s", field, body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunLogsEndpointPaginatesAndDefaultsToTail(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
logs := make([]domain.LogEntryRecord, 0, 1100)
|
|
for index := 0; index < 1100; index++ {
|
|
logs = append(logs, domain.LogEntryRecord{Timestamp: "2026-04-13T01:00:00Z", Level: "INFO", Message: fmt.Sprintf("line-%04d", index)})
|
|
}
|
|
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
|
ID: "run-logs-page",
|
|
WorkflowID: "workflow-logs",
|
|
WorkflowName: "Logs",
|
|
ProjectID: "project-logs",
|
|
ProjectName: "Logs",
|
|
Trigger: domain.RunTriggerManual,
|
|
Status: domain.RunStatusCompleted,
|
|
Logs: logs,
|
|
NodeExecutions: []domain.NodeExecutionRecord{},
|
|
ResourceSeries: []domain.ResourceSample{},
|
|
Artifacts: []domain.ExecutionArtifact{},
|
|
K8sRefs: []domain.K8sRef{},
|
|
CreatedAt: "2026-04-13T01:00:00Z",
|
|
UpdatedAt: "2026-04-13T01:00:00Z",
|
|
}); err != nil {
|
|
t.Fatalf("upsert run: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/runs/run-logs-page/logs", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("logs status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var response struct {
|
|
Items []domain.LogEntryRecord `json:"items"`
|
|
Total int `json:"total"`
|
|
Limit int `json:"limit"`
|
|
Offset int `json:"offset"`
|
|
Truncated bool `json:"truncated"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
t.Fatalf("decode logs response: %v", err)
|
|
}
|
|
if response.Total != 1100 || response.Limit != 1000 || response.Offset != 100 || !response.Truncated {
|
|
t.Fatalf("unexpected default page metadata: %+v", response)
|
|
}
|
|
if len(response.Items) != 1000 || response.Items[0].Message != "line-0100" {
|
|
t.Fatalf("expected tail page starting at line-0100, got len=%d first=%q", len(response.Items), response.Items[0].Message)
|
|
}
|
|
|
|
pageRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(pageRecorder, httptest.NewRequest(http.MethodGet, "/api/runs/run-logs-page/logs?limit=2&offset=3", nil))
|
|
if pageRecorder.Code != http.StatusOK {
|
|
t.Fatalf("explicit page status: %d body=%s", pageRecorder.Code, pageRecorder.Body.String())
|
|
}
|
|
var explicitPage struct {
|
|
Items []domain.LogEntryRecord `json:"items"`
|
|
Total int `json:"total"`
|
|
Limit int `json:"limit"`
|
|
Offset int `json:"offset"`
|
|
}
|
|
if err := json.Unmarshal(pageRecorder.Body.Bytes(), &explicitPage); err != nil {
|
|
t.Fatalf("decode explicit page: %v", err)
|
|
}
|
|
if explicitPage.Total != 1100 || explicitPage.Limit != 2 || explicitPage.Offset != 3 || len(explicitPage.Items) != 2 || explicitPage.Items[0].Message != "line-0003" {
|
|
t.Fatalf("unexpected explicit page: %+v", explicitPage)
|
|
}
|
|
|
|
zeroLimitRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(zeroLimitRecorder, httptest.NewRequest(http.MethodGet, "/api/runs/run-logs-page/logs?limit=0", nil))
|
|
if zeroLimitRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("zero limit status: %d body=%s", zeroLimitRecorder.Code, zeroLimitRecorder.Body.String())
|
|
}
|
|
if body := zeroLimitRecorder.Body.String(); !strings.Contains(body, `"field":"limit"`) || !strings.Contains(body, "limit must be greater than or equal to 1") {
|
|
t.Fatalf("expected limit validation error, got %s", body)
|
|
}
|
|
}
|
|
|
|
func TestRunPagedEndpointsRejectInvalidPagination(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
|
ID: "run-invalid-pages",
|
|
WorkflowID: "workflow-pages",
|
|
WorkflowName: "Pages",
|
|
ProjectID: "project-pages",
|
|
ProjectName: "Pages",
|
|
Trigger: domain.RunTriggerManual,
|
|
Status: domain.RunStatusCompleted,
|
|
Logs: []domain.LogEntryRecord{{Timestamp: "2026-04-13T01:00:00Z", Level: "INFO", Message: "hello"}},
|
|
NodeExecutions: []domain.NodeExecutionRecord{},
|
|
ResourceSeries: []domain.ResourceSample{{Timestamp: "2026-04-13T01:00:00Z", CPU: 1, Memory: 64}},
|
|
Artifacts: []domain.ExecutionArtifact{{ID: "artifact-1", Name: "result.json", Type: domain.PortTypeJSON}},
|
|
K8sRefs: []domain.K8sRef{},
|
|
CreatedAt: "2026-04-13T01:00:00Z",
|
|
UpdatedAt: "2026-04-13T01:00:00Z",
|
|
}); err != nil {
|
|
t.Fatalf("upsert run: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
cases := []struct {
|
|
name string
|
|
path string
|
|
field string
|
|
messagePart string
|
|
}{
|
|
{
|
|
name: "logs non integer limit",
|
|
path: "/api/runs/run-invalid-pages/logs?limit=abc",
|
|
field: "limit",
|
|
messagePart: "limit must be an integer",
|
|
},
|
|
{
|
|
name: "resources negative offset",
|
|
path: "/api/runs/run-invalid-pages/resources?offset=-1",
|
|
field: "offset",
|
|
messagePart: "offset must be zero or greater",
|
|
},
|
|
{
|
|
name: "artifacts decimal skip alias",
|
|
path: "/api/runs/run-invalid-pages/artifacts?skip=1.5",
|
|
field: "skip",
|
|
messagePart: "skip must be an integer",
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, tc.path, nil))
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, fmt.Sprintf(`"field":"%s"`, tc.field)) || !strings.Contains(body, tc.messagePart) {
|
|
t.Fatalf("expected %s validation issue containing %q, got %s", tc.field, tc.messagePart, body)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWorkflowRunsListReturnsWorkflowOnlySummaries(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "历史项目", Description: "history"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflowA, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "周期工作流 A",
|
|
Description: "a",
|
|
Type: domain.WorkflowTypeStreaming,
|
|
Schedule: &domain.WorkflowSchedule{IntervalHours: 2, Mode: domain.WorkflowScheduleModeEveryHours},
|
|
Nodes: []domain.WorkflowNode{},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflowA: %v", err)
|
|
}
|
|
workflowB, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "周期工作流 B",
|
|
Description: "b",
|
|
Type: domain.WorkflowTypeStreaming,
|
|
Schedule: &domain.WorkflowSchedule{IntervalHours: 3, Mode: domain.WorkflowScheduleModeEveryHours},
|
|
Nodes: []domain.WorkflowNode{},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflowB: %v", err)
|
|
}
|
|
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
|
ID: "run-a1",
|
|
WorkflowID: workflowA.ID,
|
|
WorkflowName: workflowA.Name,
|
|
ProjectID: project.ID,
|
|
ProjectName: project.Name,
|
|
Trigger: domain.RunTriggerScheduled,
|
|
Status: domain.RunStatusCompleted,
|
|
CreatedAt: "2026-04-22T01:00:00Z",
|
|
UpdatedAt: "2026-04-22T01:00:00Z",
|
|
NodeExecutions: []domain.NodeExecutionRecord{},
|
|
Logs: []domain.LogEntryRecord{},
|
|
ResourceSeries: []domain.ResourceSample{},
|
|
Artifacts: []domain.ExecutionArtifact{},
|
|
K8sRefs: []domain.K8sRef{},
|
|
}); err != nil {
|
|
t.Fatalf("upsert workflowA run: %v", err)
|
|
}
|
|
if err := dataStore.UpsertRun(ctx, domain.RunRecord{
|
|
ID: "run-b1",
|
|
WorkflowID: workflowB.ID,
|
|
WorkflowName: workflowB.Name,
|
|
ProjectID: project.ID,
|
|
ProjectName: project.Name,
|
|
Trigger: domain.RunTriggerScheduled,
|
|
Status: domain.RunStatusFailed,
|
|
CreatedAt: "2026-04-22T02:00:00Z",
|
|
UpdatedAt: "2026-04-22T02:00:00Z",
|
|
NodeExecutions: []domain.NodeExecutionRecord{},
|
|
Logs: []domain.LogEntryRecord{},
|
|
ResourceSeries: []domain.ResourceSample{},
|
|
Artifacts: []domain.ExecutionArtifact{},
|
|
K8sRefs: []domain.K8sRef{},
|
|
}); err != nil {
|
|
t.Fatalf("upsert workflowB run: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/workflows/"+workflowA.ID+"/runs", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("workflow runs list status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var response struct {
|
|
Items []domain.RunSummary `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
t.Fatalf("decode workflow runs: %v", err)
|
|
}
|
|
if len(response.Items) != 1 {
|
|
t.Fatalf("expected 1 workflow run summary, got %d", len(response.Items))
|
|
}
|
|
if response.Items[0].ID != "run-a1" {
|
|
t.Fatalf("expected workflowA run summary, got %#v", response.Items[0])
|
|
}
|
|
}
|
|
|
|
func TestRunsRerunSubtreeCreatesQueuedChildRunWithBaseline(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "补跑项目", Description: "rerun"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "失败补跑工作流",
|
|
Description: "rerun subtree",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{ID: "a", Type: "operator", Data: domain.WorkflowNodeData{Label: "A", Category: "DATA", Description: "a"}},
|
|
{ID: "b", Type: "operator", Data: domain.WorkflowNodeData{Label: "B", Category: "DATA", Description: "b"}},
|
|
{ID: "c", Type: "operator", Data: domain.WorkflowNodeData{Label: "C", Category: "DATA", Description: "c"}},
|
|
{ID: "d", Type: "operator", Data: domain.WorkflowNodeData{Label: "D", Category: "DATA", Description: "d"}},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "a-b", Source: "a", Target: "b"},
|
|
{ID: "b-d", Source: "b", Target: "d"},
|
|
{ID: "c-d", Source: "c", Target: "d"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
sourceRun := domain.RunRecord{
|
|
ID: "run-failed-1",
|
|
WorkflowID: workflow.ID,
|
|
WorkflowName: workflow.Name,
|
|
ProjectID: project.ID,
|
|
ProjectName: project.Name,
|
|
Trigger: domain.RunTriggerScheduled,
|
|
Status: domain.RunStatusFailed,
|
|
CurrentNodeID: "b",
|
|
NodeExecutions: []domain.NodeExecutionRecord{
|
|
{NodeID: "a", NodeLabel: "A", Status: domain.NodeExecutionStatusCompleted, Logs: []domain.LogEntryRecord{{Message: "a ok"}}, Artifacts: []domain.ExecutionArtifact{{ID: "artifact-a", Name: "a.json"}}},
|
|
{NodeID: "c", NodeLabel: "C", Status: domain.NodeExecutionStatusCompleted, Logs: []domain.LogEntryRecord{{Message: "c ok"}}, Artifacts: []domain.ExecutionArtifact{{ID: "artifact-c", Name: "c.json"}}},
|
|
{NodeID: "b", NodeLabel: "B", Status: domain.NodeExecutionStatusFailed, Error: "boom"},
|
|
},
|
|
CreatedAt: "2026-04-22T03:00:00Z",
|
|
UpdatedAt: "2026-04-22T03:05:00Z",
|
|
}
|
|
if err := dataStore.UpsertRun(ctx, sourceRun); err != nil {
|
|
t.Fatalf("upsert source run: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
body := bytes.NewBufferString(`{"nodeId":"b"}`)
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/runs/"+sourceRun.ID+"/rerun-subtree", body))
|
|
if recorder.Code != http.StatusAccepted {
|
|
t.Fatalf("rerun subtree status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var child domain.RunRecord
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &child); err != nil {
|
|
t.Fatalf("decode child run: %v", err)
|
|
}
|
|
if child.Trigger != domain.RunTriggerRerunSubtree {
|
|
t.Fatalf("expected rerun_subtree trigger, got %s", child.Trigger)
|
|
}
|
|
if child.ParentRunID != sourceRun.ID {
|
|
t.Fatalf("expected parent run %s, got %s", sourceRun.ID, child.ParentRunID)
|
|
}
|
|
if child.RestartFromNodeID != "b" {
|
|
t.Fatalf("expected restartFromNodeId=b, got %s", child.RestartFromNodeID)
|
|
}
|
|
if child.Status != domain.RunStatusQueued {
|
|
t.Fatalf("expected queued child run, got %s", child.Status)
|
|
}
|
|
if len(child.NodeExecutions) != 2 {
|
|
t.Fatalf("expected 2 baseline executions, got %#v", child.NodeExecutions)
|
|
}
|
|
nodeIDs := []string{child.NodeExecutions[0].NodeID, child.NodeExecutions[1].NodeID}
|
|
sort.Strings(nodeIDs)
|
|
if strings.Join(nodeIDs, ",") != "a,c" {
|
|
t.Fatalf("expected baseline nodes a,c, got %v", nodeIDs)
|
|
}
|
|
}
|
|
|
|
func TestRunsRerunSubtreeRejectsMissingCompletedBaseline(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "补跑项目", Description: "rerun"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "失败补跑工作流",
|
|
Description: "rerun subtree",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{ID: "a", Type: "operator", Data: domain.WorkflowNodeData{Label: "A", Category: "DATA", Description: "a"}},
|
|
{ID: "b", Type: "operator", Data: domain.WorkflowNodeData{Label: "B", Category: "DATA", Description: "b"}},
|
|
{ID: "c", Type: "operator", Data: domain.WorkflowNodeData{Label: "C", Category: "DATA", Description: "c"}},
|
|
{ID: "d", Type: "operator", Data: domain.WorkflowNodeData{Label: "D", Category: "DATA", Description: "d"}},
|
|
},
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "a-b", Source: "a", Target: "b"},
|
|
{ID: "b-d", Source: "b", Target: "d"},
|
|
{ID: "c-d", Source: "c", Target: "d"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
sourceRun := domain.RunRecord{
|
|
ID: "run-failed-2",
|
|
WorkflowID: workflow.ID,
|
|
WorkflowName: workflow.Name,
|
|
ProjectID: project.ID,
|
|
ProjectName: project.Name,
|
|
Trigger: domain.RunTriggerScheduled,
|
|
Status: domain.RunStatusFailed,
|
|
CurrentNodeID: "b",
|
|
NodeExecutions: []domain.NodeExecutionRecord{
|
|
{NodeID: "a", NodeLabel: "A", Status: domain.NodeExecutionStatusCompleted},
|
|
{NodeID: "c", NodeLabel: "C", Status: domain.NodeExecutionStatusFailed, Error: "side branch failed"},
|
|
{NodeID: "b", NodeLabel: "B", Status: domain.NodeExecutionStatusFailed, Error: "boom"},
|
|
},
|
|
CreatedAt: "2026-04-22T03:00:00Z",
|
|
UpdatedAt: "2026-04-22T03:05:00Z",
|
|
}
|
|
if err := dataStore.UpsertRun(ctx, sourceRun); err != nil {
|
|
t.Fatalf("upsert source run: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
body := bytes.NewBufferString(`{"nodeId":"b"}`)
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/runs/"+sourceRun.ID+"/rerun-subtree", body))
|
|
if recorder.Code != http.StatusConflict {
|
|
t.Fatalf("expected 409 for invalid baseline, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestWorkflowNodeSourceStateEndpointsExposeAndResetCommittedState(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
Name: "扫描状态工作流",
|
|
Description: "scan source state",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Kind: domain.WorkflowKindScan,
|
|
ScanConfig: &domain.ScanConfig{
|
|
DataSource: "BOS",
|
|
Endpoint: "https://bos.example.test",
|
|
SourcePath: "bos://datastorage/algo/X-platform-test/source-a",
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test/source-a",
|
|
ScanMode: "INCREMENTAL",
|
|
},
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "scan-source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "BOS 增量扫描",
|
|
Category: "IO",
|
|
Description: "source",
|
|
},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
currentFingerprint := domain.ComputeSourceFingerprint(domain.SourceFingerprintInput{
|
|
ScanMode: "INCREMENTAL",
|
|
IncrementalUnit: "OBJECT",
|
|
DirectoryDepth: 1,
|
|
DataSource: "BOS",
|
|
SourcePath: "bos://datastorage/algo/X-platform-test/source-a",
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test/source-a",
|
|
Endpoint: "https://bos.example.test",
|
|
})
|
|
legacyFingerprint := domain.ComputeSourceFingerprint(domain.SourceFingerprintInput{
|
|
ScanMode: "INCREMENTAL",
|
|
IncrementalUnit: "OBJECT",
|
|
DirectoryDepth: 1,
|
|
DataSource: "BOS",
|
|
SourcePath: "bos://datastorage/algo/X-platform-test/source-legacy",
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test/source-legacy",
|
|
Endpoint: "https://bos.example.test",
|
|
})
|
|
|
|
if err := dataStore.UpsertSourceState(ctx, domain.SourceStateRecord{
|
|
ID: workflow.ID + ":scan-source:" + legacyFingerprint,
|
|
WorkflowID: workflow.ID,
|
|
NodeID: "scan-source",
|
|
SourceFingerprint: legacyFingerprint,
|
|
DataSource: "BOS",
|
|
SourcePath: "bos://datastorage/algo/X-platform-test/source-legacy",
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test/source-legacy",
|
|
ScanMode: "INCREMENTAL",
|
|
IncrementalUnit: "OBJECT",
|
|
DirectoryDepth: 1,
|
|
SeenFingerprints: []string{"legacy-1"},
|
|
LastRunID: "run-legacy",
|
|
LastListedCount: 3,
|
|
LastNewCount: 1,
|
|
LastScanCompletedAt: "2026-04-12T00:00:00Z",
|
|
UpdatedAt: "2026-04-12T00:00:01Z",
|
|
}); err != nil {
|
|
t.Fatalf("seed legacy source state: %v", err)
|
|
}
|
|
if err := dataStore.UpsertSourceState(ctx, domain.SourceStateRecord{
|
|
ID: workflow.ID + ":scan-source:" + currentFingerprint,
|
|
WorkflowID: workflow.ID,
|
|
NodeID: "scan-source",
|
|
SourceFingerprint: currentFingerprint,
|
|
DataSource: "BOS",
|
|
SourcePath: "bos://datastorage/algo/X-platform-test/source-a",
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test/source-a",
|
|
ScanMode: "INCREMENTAL",
|
|
IncrementalUnit: "OBJECT",
|
|
DirectoryDepth: 1,
|
|
SeenFingerprints: []string{"entry-1", "entry-2"},
|
|
LastRunID: "run-current",
|
|
LastListedCount: 8,
|
|
LastNewCount: 2,
|
|
LastScanCompletedAt: "2026-04-12T01:00:00Z",
|
|
UpdatedAt: "2026-04-12T01:00:01Z",
|
|
}); err != nil {
|
|
t.Fatalf("seed current source state: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
getRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(getRecorder, httptest.NewRequest(http.MethodGet, "/api/workflows/"+workflow.ID+"/nodes/scan-source/source-state", nil))
|
|
if getRecorder.Code != http.StatusOK {
|
|
t.Fatalf("source state get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
|
}
|
|
|
|
var body workflowNodeSourceStateResponse
|
|
if err := json.Unmarshal(getRecorder.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("decode source state response: %v", err)
|
|
}
|
|
if !body.Supported || !body.Stateful {
|
|
t.Fatalf("expected supported stateful response, got %+v", body)
|
|
}
|
|
if body.Current == nil || body.Current.LastRunID != "run-current" {
|
|
t.Fatalf("expected current source state, got %+v", body.Current)
|
|
}
|
|
if body.CurrentFingerprint != currentFingerprint {
|
|
t.Fatalf("expected current fingerprint %s, got %s", currentFingerprint, body.CurrentFingerprint)
|
|
}
|
|
if len(body.Versions) != 2 {
|
|
t.Fatalf("expected 2 source state versions, got %+v", body.Versions)
|
|
}
|
|
if body.Versions[0].LastRunID != "run-current" {
|
|
t.Fatalf("expected latest source state first, got %+v", body.Versions)
|
|
}
|
|
|
|
deleteRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(deleteRecorder, httptest.NewRequest(http.MethodDelete, "/api/workflows/"+workflow.ID+"/nodes/scan-source/source-state", nil))
|
|
if deleteRecorder.Code != http.StatusNoContent {
|
|
t.Fatalf("source state delete status: %d body=%s", deleteRecorder.Code, deleteRecorder.Body.String())
|
|
}
|
|
|
|
stored, err := dataStore.GetSourceState(ctx, workflow.ID, "scan-source", currentFingerprint)
|
|
if err != nil {
|
|
t.Fatalf("get deleted current state: %v", err)
|
|
}
|
|
if stored != nil {
|
|
t.Fatalf("expected current source state removed, got %+v", stored)
|
|
}
|
|
legacyStored, err := dataStore.GetSourceState(ctx, workflow.ID, "scan-source", legacyFingerprint)
|
|
if err != nil {
|
|
t.Fatalf("get legacy source state: %v", err)
|
|
}
|
|
if legacyStored == nil {
|
|
t.Fatalf("expected legacy source state to remain")
|
|
}
|
|
}
|
|
|
|
func TestWorkflowNodeSourceStateEndpointsSupportDedicatedBosScanOperatorNodes(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
Name: "BOS 扫描状态工作流",
|
|
Description: "dedicated bos scan state",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Kind: domain.WorkflowKindScan,
|
|
ScanConfig: &domain.ScanConfig{
|
|
DataSource: "BOS",
|
|
Endpoint: "https://bos.example.test",
|
|
SourcePath: "bos://datastorage/algo/X-platform-test",
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test",
|
|
ScanMode: "INCREMENTAL",
|
|
},
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "node-bos-scan",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "BOS 增量扫描",
|
|
Category: "IO",
|
|
Description: "source",
|
|
},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
currentFingerprint := domain.ComputeSourceFingerprint(domain.SourceFingerprintInput{
|
|
ScanMode: "INCREMENTAL",
|
|
IncrementalUnit: "OBJECT",
|
|
DirectoryDepth: 1,
|
|
DataSource: "BOS",
|
|
SourcePath: "bos://datastorage/algo/X-platform-test",
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test",
|
|
Endpoint: "https://bos.example.test",
|
|
})
|
|
|
|
if err := dataStore.UpsertSourceState(ctx, domain.SourceStateRecord{
|
|
ID: workflow.ID + ":node-bos-scan:" + currentFingerprint,
|
|
WorkflowID: workflow.ID,
|
|
NodeID: "node-bos-scan",
|
|
SourceFingerprint: currentFingerprint,
|
|
DataSource: "BOS",
|
|
SourcePath: "bos://datastorage/algo/X-platform-test",
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test",
|
|
ScanMode: "INCREMENTAL",
|
|
IncrementalUnit: "OBJECT",
|
|
DirectoryDepth: 1,
|
|
SeenFingerprints: []string{"entry-1"},
|
|
LastRunID: "run-bos-current",
|
|
LastListedCount: 6,
|
|
LastNewCount: 1,
|
|
LastScanCompletedAt: "2026-04-12T02:00:00Z",
|
|
UpdatedAt: "2026-04-12T02:00:01Z",
|
|
}); err != nil {
|
|
t.Fatalf("seed bos source state: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
getRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(getRecorder, httptest.NewRequest(http.MethodGet, "/api/workflows/"+workflow.ID+"/nodes/node-bos-scan/source-state", nil))
|
|
if getRecorder.Code != http.StatusOK {
|
|
t.Fatalf("source state get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
|
}
|
|
|
|
var body workflowNodeSourceStateResponse
|
|
if err := json.Unmarshal(getRecorder.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("decode source state response: %v", err)
|
|
}
|
|
if !body.Supported || !body.Stateful {
|
|
t.Fatalf("expected supported stateful response, got %+v", body)
|
|
}
|
|
if body.Current == nil || body.Current.LastRunID != "run-bos-current" {
|
|
t.Fatalf("expected current bos source state, got %+v", body.Current)
|
|
}
|
|
if body.CurrentFingerprint != currentFingerprint {
|
|
t.Fatalf("expected current fingerprint %s, got %s", currentFingerprint, body.CurrentFingerprint)
|
|
}
|
|
}
|
|
|
|
func TestUserStoragePathFavoritesAPIStoresNormalizedFavoritesWithoutLeakingToUsersList(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
user, err := dataStore.CreateUser(ctx, domain.CreateUserInput{
|
|
Name: "收藏用户",
|
|
Email: "favorite-user@example.com",
|
|
Role: domain.UserRoleOperator,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create user: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
updatePayload := []byte(`{
|
|
"items": [
|
|
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"algo/X-platform-test","label":" "},
|
|
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"algo/X-platform-test/","label":"重复"},
|
|
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"processed"},
|
|
{"scopeKey":"","bucket":"datastorage","prefix":"invalid"},
|
|
{"scopeKey":"source:bos://datastorage/root","bucket":"archive","prefix":" clips "}
|
|
]
|
|
}`)
|
|
updateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
updateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/users/"+user.ID+"/storage-path-favorites", bytes.NewReader(updatePayload)),
|
|
)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("favorites update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
var updateBody struct {
|
|
Items []domain.StoragePathFavoriteRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updateBody); err != nil {
|
|
t.Fatalf("decode favorites update: %v", err)
|
|
}
|
|
expected := []domain.StoragePathFavoriteRecord{
|
|
{
|
|
ScopeKey: "connection:storage-a",
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test",
|
|
Label: "datastorage/algo/X-platform-test",
|
|
},
|
|
{
|
|
ScopeKey: "connection:storage-a",
|
|
Bucket: "datastorage",
|
|
Prefix: "processed",
|
|
Label: "datastorage/processed",
|
|
},
|
|
{
|
|
ScopeKey: "source:bos://datastorage/root",
|
|
Bucket: "archive",
|
|
Prefix: "clips",
|
|
Label: "archive/clips",
|
|
},
|
|
}
|
|
if got, want := len(updateBody.Items), len(expected); got != want {
|
|
t.Fatalf("favorites count = %d, want %d", got, want)
|
|
}
|
|
for index := range expected {
|
|
if updateBody.Items[index] != expected[index] {
|
|
t.Fatalf("favorite[%d] = %#v, want %#v", index, updateBody.Items[index], expected[index])
|
|
}
|
|
}
|
|
|
|
getRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
getRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/users/"+user.ID+"/storage-path-favorites", nil),
|
|
)
|
|
if getRecorder.Code != http.StatusOK {
|
|
t.Fatalf("favorites get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
|
}
|
|
var getBody struct {
|
|
Items []domain.StoragePathFavoriteRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(getRecorder.Body.Bytes(), &getBody); err != nil {
|
|
t.Fatalf("decode favorites get: %v", err)
|
|
}
|
|
if strings.Join(func() []string {
|
|
values := make([]string, 0, len(getBody.Items))
|
|
for _, item := range getBody.Items {
|
|
values = append(values, item.ScopeKey+"|"+item.Bucket+"|"+item.Prefix+"|"+item.Label)
|
|
}
|
|
return values
|
|
}(), "\n") != strings.Join(func() []string {
|
|
values := make([]string, 0, len(expected))
|
|
for _, item := range expected {
|
|
values = append(values, item.ScopeKey+"|"+item.Bucket+"|"+item.Prefix+"|"+item.Label)
|
|
}
|
|
return values
|
|
}(), "\n") {
|
|
t.Fatalf("favorites get body = %#v, want %#v", getBody.Items, expected)
|
|
}
|
|
|
|
usersRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(usersRecorder, httptest.NewRequest(http.MethodGet, "/api/users", nil))
|
|
if usersRecorder.Code != http.StatusOK {
|
|
t.Fatalf("users list status: %d body=%s", usersRecorder.Code, usersRecorder.Body.String())
|
|
}
|
|
if strings.Contains(usersRecorder.Body.String(), "storagePathFavorites") {
|
|
t.Fatalf("expected users list to omit storagePathFavorites, got %s", usersRecorder.Body.String())
|
|
}
|
|
|
|
storedUser, err := dataStore.GetUser(ctx, user.ID)
|
|
if err != nil {
|
|
t.Fatalf("get stored user: %v", err)
|
|
}
|
|
if storedUser == nil || len(storedUser.StoragePathFavorites) != len(expected) {
|
|
t.Fatalf("stored favorites = %#v, want %#v", storedUser, expected)
|
|
}
|
|
}
|
|
|
|
func TestUserStoragePathFavoritesSessionAuthRequiresSelfOrAdmin(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
organization := adminCreateOrganization(t, handler, rootCookie, "Favorites Tenant", "favorites-tenant")
|
|
admin := adminCreateUser(t, handler, rootCookie, organization.ID, "Favorites Admin", "favorites-admin@example.com", domain.UserRoleAdmin, "admin-pass")
|
|
operatorA := adminCreateUser(t, handler, rootCookie, organization.ID, "Favorites Operator A", "favorites-a@example.com", domain.UserRoleOperator, "operator-a-pass")
|
|
operatorB := adminCreateUser(t, handler, rootCookie, organization.ID, "Favorites Operator B", "favorites-b@example.com", domain.UserRoleOperator, "operator-b-pass")
|
|
adminCookie := loginSession(t, handler, admin.Email, "admin-pass")
|
|
operatorCookie := loginSession(t, handler, operatorA.Email, "operator-a-pass")
|
|
|
|
payload := []byte(`{"items":[{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"owned"}]}`)
|
|
forbiddenUpdate := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/users/"+operatorB.ID+"/storage-path-favorites",
|
|
payload,
|
|
operatorCookie,
|
|
)
|
|
if forbiddenUpdate.Code != http.StatusForbidden {
|
|
t.Fatalf("expected operator update of another user's favorites to be forbidden, got %d body=%s", forbiddenUpdate.Code, forbiddenUpdate.Body.String())
|
|
}
|
|
|
|
forbiddenGet := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodGet,
|
|
"/api/users/"+operatorB.ID+"/storage-path-favorites",
|
|
nil,
|
|
operatorCookie,
|
|
)
|
|
if forbiddenGet.Code != http.StatusForbidden {
|
|
t.Fatalf("expected operator read of another user's favorites to be forbidden, got %d body=%s", forbiddenGet.Code, forbiddenGet.Body.String())
|
|
}
|
|
|
|
selfUpdate := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/users/"+operatorA.ID+"/storage-path-favorites",
|
|
payload,
|
|
operatorCookie,
|
|
)
|
|
if selfUpdate.Code != http.StatusOK {
|
|
t.Fatalf("expected self favorites update to succeed, got %d body=%s", selfUpdate.Code, selfUpdate.Body.String())
|
|
}
|
|
|
|
adminUpdate := serveJSONWithCookie(
|
|
handler,
|
|
http.MethodPut,
|
|
"/api/users/"+operatorB.ID+"/storage-path-favorites",
|
|
payload,
|
|
adminCookie,
|
|
)
|
|
if adminUpdate.Code != http.StatusOK {
|
|
t.Fatalf("expected admin favorites update to succeed, got %d body=%s", adminUpdate.Code, adminUpdate.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestProjectStoragePathFavoritesAPIStoresNormalizedFavoritesWithoutLeakingToProjectResponses(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "共享收藏项目",
|
|
Description: "project favorites",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
updatePayload := []byte(`{
|
|
"items": [
|
|
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"algo/X-platform-test"},
|
|
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"algo/X-platform-test/"},
|
|
{"scopeKey":"connection:storage-a","bucket":"datastorage","prefix":"processed"},
|
|
{"scopeKey":"source:bos://datastorage/root","bucket":"archive","prefix":" clips "}
|
|
]
|
|
}`)
|
|
updateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
updateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/projects/"+project.ID+"/storage-path-favorites", bytes.NewReader(updatePayload)),
|
|
)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("project favorites update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
var updateBody struct {
|
|
Items []domain.StoragePathFavoriteRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updateBody); err != nil {
|
|
t.Fatalf("decode project favorites update: %v", err)
|
|
}
|
|
if len(updateBody.Items) != 3 {
|
|
t.Fatalf("expected 3 normalized project favorites, got %#v", updateBody.Items)
|
|
}
|
|
|
|
getRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
getRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/projects/"+project.ID+"/storage-path-favorites", nil),
|
|
)
|
|
if getRecorder.Code != http.StatusOK {
|
|
t.Fatalf("project favorites get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
|
}
|
|
if strings.Contains(getRecorder.Body.String(), "\"prefix\":\"clips \"") {
|
|
t.Fatalf("expected project favorites prefix to be normalized, got %s", getRecorder.Body.String())
|
|
}
|
|
|
|
projectGetRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(projectGetRecorder, httptest.NewRequest(http.MethodGet, "/api/projects/"+project.ID, nil))
|
|
if projectGetRecorder.Code != http.StatusOK {
|
|
t.Fatalf("project get status: %d body=%s", projectGetRecorder.Code, projectGetRecorder.Body.String())
|
|
}
|
|
if strings.Contains(projectGetRecorder.Body.String(), "storagePathFavorites") {
|
|
t.Fatalf("expected project get to omit storagePathFavorites, got %s", projectGetRecorder.Body.String())
|
|
}
|
|
|
|
projectsListRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(projectsListRecorder, httptest.NewRequest(http.MethodGet, "/api/projects", nil))
|
|
if projectsListRecorder.Code != http.StatusOK {
|
|
t.Fatalf("projects list status: %d body=%s", projectsListRecorder.Code, projectsListRecorder.Body.String())
|
|
}
|
|
if strings.Contains(projectsListRecorder.Body.String(), "storagePathFavorites") {
|
|
t.Fatalf("expected projects list to omit storagePathFavorites, got %s", projectsListRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSettingsAIConfigurationDrivesAIStatus(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
settingsPayload := []byte(`{
|
|
"k8s": {
|
|
"endpoint": "https://192.0.2.10:6443",
|
|
"namespace": "droboticflow",
|
|
"workspacePVCName": "droboticflow-workspace",
|
|
"workspaceMountPath": "/workspace",
|
|
"kubeconfig": "apiVersion: v1\nkind: Config"
|
|
},
|
|
"ai": {
|
|
"provider": "openai-compatible",
|
|
"baseURL": "https://ai.example/v1",
|
|
"model": "model-x",
|
|
"apiKey": "test-key"
|
|
},
|
|
"businessMongo": {
|
|
"uri": "mongodb://business-user:business-pass@business.example:27017/?authSource=admin",
|
|
"database": "business_data"
|
|
},
|
|
"timezone": "Asia/Tokyo"
|
|
}`)
|
|
|
|
updateRecorder := httptest.NewRecorder()
|
|
updateRequest := httptest.NewRequest(http.MethodPut, "/api/settings", bytes.NewReader(settingsPayload))
|
|
handler.ServeHTTP(updateRecorder, updateRequest)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("settings update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
|
|
statusRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(statusRecorder, httptest.NewRequest(http.MethodGet, "/api/ai/status", nil))
|
|
if statusRecorder.Code != http.StatusOK {
|
|
t.Fatalf("ai status code: %d body=%s", statusRecorder.Code, statusRecorder.Body.String())
|
|
}
|
|
|
|
var statusBody struct {
|
|
Enabled bool `json:"enabled"`
|
|
BaseURL string `json:"baseURL"`
|
|
Model string `json:"model"`
|
|
Provider string `json:"provider"`
|
|
}
|
|
if err := json.Unmarshal(statusRecorder.Body.Bytes(), &statusBody); err != nil {
|
|
t.Fatalf("decode ai status: %v", err)
|
|
}
|
|
if !statusBody.Enabled {
|
|
t.Fatalf("expected ai status enabled")
|
|
}
|
|
if statusBody.Model != "model-x" {
|
|
t.Fatalf("unexpected model: %s", statusBody.Model)
|
|
}
|
|
if statusBody.BaseURL != "https://ai.example/v1" {
|
|
t.Fatalf("unexpected baseURL: %s", statusBody.BaseURL)
|
|
}
|
|
|
|
settingsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(settingsRecorder, httptest.NewRequest(http.MethodGet, "/api/settings", nil))
|
|
if settingsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("settings get status: %d body=%s", settingsRecorder.Code, settingsRecorder.Body.String())
|
|
}
|
|
var settingsBody struct {
|
|
K8s struct {
|
|
WorkspacePVCName string `json:"workspacePVCName"`
|
|
WorkspaceMountPath string `json:"workspaceMountPath"`
|
|
Kubeconfig string `json:"kubeconfig"`
|
|
KubeconfigPreview string `json:"kubeconfigPreview"`
|
|
HasKubeconfig bool `json:"hasKubeconfig"`
|
|
} `json:"k8s"`
|
|
AI struct {
|
|
APIKey string `json:"apiKey"`
|
|
APIKeyPreview string `json:"apiKeyPreview"`
|
|
HasAPIKey bool `json:"hasAPIKey"`
|
|
} `json:"ai"`
|
|
BusinessMongo struct {
|
|
URI string `json:"uri"`
|
|
URIPreview string `json:"uriPreview"`
|
|
HasURI bool `json:"hasUri"`
|
|
Database string `json:"database"`
|
|
} `json:"businessMongo"`
|
|
Timezone string `json:"timezone"`
|
|
Storage any `json:"storage"`
|
|
}
|
|
if err := json.Unmarshal(settingsRecorder.Body.Bytes(), &settingsBody); err != nil {
|
|
t.Fatalf("decode settings: %v", err)
|
|
}
|
|
if settingsBody.K8s.Kubeconfig != "" {
|
|
t.Fatalf("expected settings kubeconfig to be sanitized, got %q", settingsBody.K8s.Kubeconfig)
|
|
}
|
|
if !settingsBody.K8s.HasKubeconfig {
|
|
t.Fatalf("expected hasKubeconfig to be true")
|
|
}
|
|
if settingsBody.K8s.KubeconfigPreview == "" {
|
|
t.Fatalf("expected kubeconfig preview to be returned")
|
|
}
|
|
if settingsBody.K8s.WorkspacePVCName != "droboticflow-workspace" {
|
|
t.Fatalf("expected workspace pvc name to roundtrip, got %q", settingsBody.K8s.WorkspacePVCName)
|
|
}
|
|
if settingsBody.K8s.WorkspaceMountPath != "/workspace" {
|
|
t.Fatalf("expected workspace mount path to roundtrip, got %q", settingsBody.K8s.WorkspaceMountPath)
|
|
}
|
|
if settingsBody.AI.APIKey != "" {
|
|
t.Fatalf("expected settings apiKey to be sanitized")
|
|
}
|
|
if !settingsBody.AI.HasAPIKey {
|
|
t.Fatalf("expected hasAPIKey to be true")
|
|
}
|
|
if settingsBody.AI.APIKeyPreview != "te****ey" {
|
|
t.Fatalf("expected apiKey preview to be masked, got %q", settingsBody.AI.APIKeyPreview)
|
|
}
|
|
if settingsBody.BusinessMongo.URI != "" {
|
|
t.Fatalf("expected business mongo uri to be sanitized")
|
|
}
|
|
if !settingsBody.BusinessMongo.HasURI {
|
|
t.Fatalf("expected hasUri to be true for business mongo")
|
|
}
|
|
if settingsBody.BusinessMongo.URIPreview == "" {
|
|
t.Fatalf("expected business mongo uri preview to be returned")
|
|
}
|
|
if settingsBody.BusinessMongo.Database != "business_data" {
|
|
t.Fatalf("expected business mongo database to roundtrip, got %q", settingsBody.BusinessMongo.Database)
|
|
}
|
|
if settingsBody.Timezone != "Asia/Tokyo" {
|
|
t.Fatalf("expected timezone to roundtrip, got %q", settingsBody.Timezone)
|
|
}
|
|
if settingsBody.Storage != nil {
|
|
t.Fatalf("expected storage to be omitted from settings response")
|
|
}
|
|
}
|
|
|
|
func TestSettingsRejectsAIKeyWithoutExplicitRuntimeConfig(t *testing.T) {
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
aiPatch string
|
|
field string
|
|
message string
|
|
}{
|
|
{
|
|
name: "missing base url",
|
|
aiPatch: `"baseURL":"","model":"model-x","apiKey":"test-key"`,
|
|
field: "ai.baseURL",
|
|
message: "baseURL is required when apiKey is configured",
|
|
},
|
|
{
|
|
name: "missing model",
|
|
aiPatch: `"baseURL":"https://ai.example/v1","model":"","apiKey":"test-key"`,
|
|
field: "ai.model",
|
|
message: "model is required when apiKey is configured",
|
|
},
|
|
{
|
|
name: "invalid base url",
|
|
aiPatch: `"baseURL":"ai.example/v1","model":"model-x"`,
|
|
field: "ai.baseURL",
|
|
message: "baseURL must be an absolute http(s) URL without whitespace",
|
|
},
|
|
{
|
|
name: "invalid model",
|
|
aiPatch: `"baseURL":"https://ai.example/v1","model":"model x"`,
|
|
field: "ai.model",
|
|
message: "model must not contain whitespace",
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
payload := `{"k8s":{"workspacePVCName":"droboticflow-workspace","workspaceMountPath":"/workspace"},"ai":{` + tc.aiPatch + `}}`
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPut, "/api/settings", strings.NewReader(payload)))
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), tc.field) || !strings.Contains(recorder.Body.String(), tc.message) {
|
|
t.Fatalf("expected validation error for %s/%q, got %s", tc.field, tc.message, recorder.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSettingsUpdateRejectsInvalidRuntimeDefaults(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
payload string
|
|
field string
|
|
messagePart string
|
|
}{
|
|
{
|
|
name: "worker poll interval must be positive",
|
|
payload: `{"k8s":{"workspacePVCName":"workspace","workspaceMountPath":"/workspace"},"worker":{"pollIntervalMs":0}}`,
|
|
field: "worker.pollIntervalMs",
|
|
messagePart: "greater than zero",
|
|
},
|
|
{
|
|
name: "worker concurrency must be positive",
|
|
payload: `{"k8s":{"workspacePVCName":"workspace","workspaceMountPath":"/workspace"},"worker":{"maxConcurrentRuns":-1}}`,
|
|
field: "worker.maxConcurrentRuns",
|
|
messagePart: "greater than zero",
|
|
},
|
|
{
|
|
name: "cpu request must be a kubernetes quantity",
|
|
payload: `{"k8s":{"workspacePVCName":"workspace","workspaceMountPath":"/workspace"},"defaults":{"cpuRequest":"one-core"}}`,
|
|
field: "defaults.cpuRequest",
|
|
messagePart: "valid Kubernetes resource quantity",
|
|
},
|
|
{
|
|
name: "memory limit must be positive",
|
|
payload: `{"k8s":{"workspacePVCName":"workspace","workspaceMountPath":"/workspace"},"defaults":{"memoryLimit":"0"}}`,
|
|
field: "defaults.memoryLimit",
|
|
messagePart: "greater than zero",
|
|
},
|
|
{
|
|
name: "gpu limit must be positive integer",
|
|
payload: `{"k8s":{"workspacePVCName":"workspace","workspaceMountPath":"/workspace"},"defaults":{"gpuLimit":"half"}}`,
|
|
field: "defaults.gpuLimit",
|
|
messagePart: "positive integer",
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodPut, "/api/settings", strings.NewReader(tc.payload))
|
|
|
|
handler.ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, `"field":"`+tc.field+`"`) || !strings.Contains(body, tc.messagePart) {
|
|
t.Fatalf("expected %s validation issue containing %q, got %s", tc.field, tc.messagePart, body)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTemplateExecutionConfigValidationAndPersistence(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
createPayload := []byte(`{
|
|
"name":"执行保障模板",
|
|
"description":"template execution config",
|
|
"type":"streaming",
|
|
"hyperparams":[{"key":"GLOBAL_ENV","value":"template-create"}],
|
|
"maxConcurrentRuns":2,
|
|
"nodeTimeoutSeconds":12,
|
|
"runTimeoutSeconds":34,
|
|
"maxRetryAttempts":1,
|
|
"retryBackoffSeconds":56,
|
|
"schedule":{
|
|
"interval":90,
|
|
"sourcePath":"oss://incoming"
|
|
},
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`)
|
|
|
|
createRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(createPayload)),
|
|
)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("template create status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var created map[string]any
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created template: %v", err)
|
|
}
|
|
if created["type"].(string) != "streaming" {
|
|
t.Fatalf("expected streaming type, got %v", created["type"])
|
|
}
|
|
if created["retryBackoffSeconds"].(float64) != 56 {
|
|
t.Fatalf("expected retryBackoffSeconds=56, got %v", created["retryBackoffSeconds"])
|
|
}
|
|
createdHyperparams := created["hyperparams"].([]any)
|
|
if len(createdHyperparams) != 1 || createdHyperparams[0].(map[string]any)["value"].(string) != "template-create" {
|
|
t.Fatalf("expected template hyperparams to persist on create, got %#v", created["hyperparams"])
|
|
}
|
|
|
|
templateID := created["id"].(string)
|
|
getRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(getRecorder, httptest.NewRequest(http.MethodGet, "/api/templates/"+templateID, nil))
|
|
if getRecorder.Code != http.StatusOK {
|
|
t.Fatalf("template get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
|
}
|
|
|
|
var loaded map[string]any
|
|
if err := json.Unmarshal(getRecorder.Body.Bytes(), &loaded); err != nil {
|
|
t.Fatalf("decode loaded template: %v", err)
|
|
}
|
|
if loaded["nodeTimeoutSeconds"].(float64) != 12 {
|
|
t.Fatalf("expected nodeTimeoutSeconds=12, got %v", loaded["nodeTimeoutSeconds"])
|
|
}
|
|
schedule := loaded["schedule"].(map[string]any)
|
|
if schedule["interval"].(float64) != 90 {
|
|
t.Fatalf("expected interval=90, got %v", schedule["interval"])
|
|
}
|
|
loadedHyperparams := loaded["hyperparams"].([]any)
|
|
if len(loadedHyperparams) != 1 || loadedHyperparams[0].(map[string]any)["key"].(string) != "GLOBAL_ENV" {
|
|
t.Fatalf("expected template hyperparams to survive load, got %#v", loaded["hyperparams"])
|
|
}
|
|
|
|
updatePayload := []byte(`{
|
|
"type":"batch",
|
|
"hyperparams":[{"key":"GLOBAL_ENV","value":"template-update"},{"key":"SHARED_MODE","value":"true"}],
|
|
"maxConcurrentRuns":3,
|
|
"nodeTimeoutSeconds":6,
|
|
"runTimeoutSeconds":18,
|
|
"maxRetryAttempts":2,
|
|
"retryBackoffSeconds":9
|
|
}`)
|
|
updateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
updateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/templates/"+templateID, bytes.NewReader(updatePayload)),
|
|
)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("template update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
|
|
var updated map[string]any
|
|
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updated); err != nil {
|
|
t.Fatalf("decode updated template: %v", err)
|
|
}
|
|
if updated["type"].(string) != "batch" {
|
|
t.Fatalf("expected batch type after update, got %v", updated["type"])
|
|
}
|
|
if updated["maxConcurrentRuns"].(float64) != 3 {
|
|
t.Fatalf("expected maxConcurrentRuns=3 after update, got %v", updated["maxConcurrentRuns"])
|
|
}
|
|
if updated["retryBackoffSeconds"].(float64) != 9 {
|
|
t.Fatalf("expected retryBackoffSeconds=9 after update, got %v", updated["retryBackoffSeconds"])
|
|
}
|
|
updatedHyperparams := updated["hyperparams"].([]any)
|
|
if len(updatedHyperparams) != 2 || updatedHyperparams[1].(map[string]any)["key"].(string) != "SHARED_MODE" {
|
|
t.Fatalf("expected template hyperparams to update, got %#v", updated["hyperparams"])
|
|
}
|
|
|
|
invalidPayload := []byte(`{"maxRetryAttempts":-1}`)
|
|
invalidRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/templates/"+templateID, bytes.NewReader(invalidPayload)),
|
|
)
|
|
if invalidRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("invalid template update status: %d body=%s", invalidRecorder.Code, invalidRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestWorkflowExecutionConfigValidationAndPersistence(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "执行配置项目",
|
|
Description: "workflow execution settings",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
createPayload := []byte(`{
|
|
"name":"执行配置工作流",
|
|
"description":"workflow",
|
|
"type":"batch",
|
|
"hyperparams":[{"key":"GLOBAL_ENV","value":"workflow-create"}],
|
|
"maxConcurrentRuns":2,
|
|
"nodeTimeoutSeconds":30,
|
|
"runTimeoutSeconds":120,
|
|
"maxRetryAttempts":3,
|
|
"retryBackoffSeconds":15,
|
|
"nodes":[{"id":"node-source","type":"operator","data":{"label":"真实数据源","category":"IO","description":"source","inputs":[],"outputs":[{"id":"out-1","name":"file","type":"file"}],"hyperparams":[{"key":"SOURCE_TEXT","value":"api-test"}]},"position":{"x":0,"y":0}}],
|
|
"edges":[]
|
|
}`)
|
|
|
|
createRecorder := httptest.NewRecorder()
|
|
createRequest := httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload))
|
|
handler.ServeHTTP(createRecorder, createRequest)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("workflow create status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var created domain.WorkflowRecord
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created workflow: %v", err)
|
|
}
|
|
if created.RunTimeoutSeconds != 120 {
|
|
t.Fatalf("expected runTimeoutSeconds=120, got %d", created.RunTimeoutSeconds)
|
|
}
|
|
if created.NodeTimeoutSeconds != 30 {
|
|
t.Fatalf("expected nodeTimeoutSeconds=30, got %d", created.NodeTimeoutSeconds)
|
|
}
|
|
if created.MaxRetryAttempts != 3 {
|
|
t.Fatalf("expected maxRetryAttempts=3, got %d", created.MaxRetryAttempts)
|
|
}
|
|
if created.RetryBackoffSeconds != 15 {
|
|
t.Fatalf("expected retryBackoffSeconds=15, got %d", created.RetryBackoffSeconds)
|
|
}
|
|
if len(created.Hyperparams) != 1 || created.Hyperparams[0].Value != "workflow-create" {
|
|
t.Fatalf("expected workflow hyperparams persisted on create, got %#v", created.Hyperparams)
|
|
}
|
|
|
|
updatePayload := []byte(`{
|
|
"hyperparams":[{"key":"GLOBAL_ENV","value":"workflow-update"},{"key":"SHARED_MODE","value":"true"}],
|
|
"nodeTimeoutSeconds":12,
|
|
"runTimeoutSeconds":45,
|
|
"maxRetryAttempts":1,
|
|
"retryBackoffSeconds":9
|
|
}`)
|
|
updateRecorder := httptest.NewRecorder()
|
|
updateRequest := httptest.NewRequest(http.MethodPut, "/api/workflows/"+created.ID, bytes.NewReader(updatePayload))
|
|
handler.ServeHTTP(updateRecorder, updateRequest)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("workflow update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
|
|
var updated domain.WorkflowRecord
|
|
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updated); err != nil {
|
|
t.Fatalf("decode updated workflow: %v", err)
|
|
}
|
|
if updated.RunTimeoutSeconds != 45 {
|
|
t.Fatalf("expected updated runTimeoutSeconds=45, got %d", updated.RunTimeoutSeconds)
|
|
}
|
|
if updated.NodeTimeoutSeconds != 12 {
|
|
t.Fatalf("expected updated nodeTimeoutSeconds=12, got %d", updated.NodeTimeoutSeconds)
|
|
}
|
|
if updated.MaxRetryAttempts != 1 {
|
|
t.Fatalf("expected updated maxRetryAttempts=1, got %d", updated.MaxRetryAttempts)
|
|
}
|
|
if updated.RetryBackoffSeconds != 9 {
|
|
t.Fatalf("expected updated retryBackoffSeconds=9, got %d", updated.RetryBackoffSeconds)
|
|
}
|
|
if len(updated.Hyperparams) != 2 || updated.Hyperparams[1].Key != "SHARED_MODE" {
|
|
t.Fatalf("expected workflow hyperparams updated, got %#v", updated.Hyperparams)
|
|
}
|
|
|
|
invalidCreatePayload := []byte(`{
|
|
"name":"非法工作流",
|
|
"description":"workflow",
|
|
"type":"batch",
|
|
"nodeTimeoutSeconds":-1,
|
|
"runTimeoutSeconds":-1,
|
|
"maxRetryAttempts":-2,
|
|
"retryBackoffSeconds":-3,
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`)
|
|
invalidCreateRecorder := httptest.NewRecorder()
|
|
invalidCreateRequest := httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(invalidCreatePayload))
|
|
handler.ServeHTTP(invalidCreateRecorder, invalidCreateRequest)
|
|
if invalidCreateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("invalid create status: %d body=%s", invalidCreateRecorder.Code, invalidCreateRecorder.Body.String())
|
|
}
|
|
|
|
invalidUpdatePayload := []byte(`{"nodeTimeoutSeconds":-1,"runTimeoutSeconds":-1,"maxRetryAttempts":-1,"retryBackoffSeconds":-1}`)
|
|
invalidUpdateRecorder := httptest.NewRecorder()
|
|
invalidUpdateRequest := httptest.NewRequest(http.MethodPut, "/api/workflows/"+created.ID, bytes.NewReader(invalidUpdatePayload))
|
|
handler.ServeHTTP(invalidUpdateRecorder, invalidUpdateRequest)
|
|
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("invalid update status: %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestWorkflowAndTemplateMetadataValidationAndNormalization(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "元数据校验项目",
|
|
Description: "metadata validation",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflowCreateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
workflowCreateRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader([]byte(`{
|
|
"name":" Trimmed Workflow ",
|
|
"description":"workflow metadata",
|
|
"type":" batch ",
|
|
"kind":" standard ",
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`))),
|
|
)
|
|
if workflowCreateRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("workflow create status: %d body=%s", workflowCreateRecorder.Code, workflowCreateRecorder.Body.String())
|
|
}
|
|
var createdWorkflow domain.WorkflowRecord
|
|
if err := json.Unmarshal(workflowCreateRecorder.Body.Bytes(), &createdWorkflow); err != nil {
|
|
t.Fatalf("decode workflow: %v", err)
|
|
}
|
|
if createdWorkflow.Name != "Trimmed Workflow" || createdWorkflow.Type != domain.WorkflowTypeBatch || createdWorkflow.Kind != domain.WorkflowKindStandard {
|
|
t.Fatalf("expected normalized workflow metadata, got %#v", createdWorkflow)
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
payload string
|
|
}{
|
|
{name: "blank name", payload: `{"name":" "}`},
|
|
{name: "invalid type", payload: `{"type":"realtime"}`},
|
|
{name: "invalid kind", payload: `{"kind":"custom"}`},
|
|
{name: "invalid status", payload: `{"status":"paused"}`},
|
|
} {
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/workflows/"+createdWorkflow.ID, bytes.NewReader([]byte(tc.payload))),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected %s to fail, got %d body=%s", tc.name, recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
storedWorkflow, err := dataStore.GetWorkflow(ctx, createdWorkflow.ID)
|
|
if err != nil {
|
|
t.Fatalf("reload workflow: %v", err)
|
|
}
|
|
if storedWorkflow == nil || storedWorkflow.Name != createdWorkflow.Name || storedWorkflow.Type != createdWorkflow.Type || storedWorkflow.Kind != createdWorkflow.Kind || storedWorkflow.Status != createdWorkflow.Status {
|
|
t.Fatalf("expected invalid workflow updates to preserve metadata, got %#v", storedWorkflow)
|
|
}
|
|
|
|
workflowUpdateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
workflowUpdateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/workflows/"+createdWorkflow.ID, bytes.NewReader([]byte(`{
|
|
"name":" Ready Workflow ",
|
|
"type":" streaming ",
|
|
"kind":" standard ",
|
|
"status":" ready "
|
|
}`))),
|
|
)
|
|
if workflowUpdateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("workflow update status: %d body=%s", workflowUpdateRecorder.Code, workflowUpdateRecorder.Body.String())
|
|
}
|
|
var updatedWorkflow domain.WorkflowRecord
|
|
if err := json.Unmarshal(workflowUpdateRecorder.Body.Bytes(), &updatedWorkflow); err != nil {
|
|
t.Fatalf("decode updated workflow: %v", err)
|
|
}
|
|
if updatedWorkflow.Name != "Ready Workflow" || updatedWorkflow.Type != domain.WorkflowTypeStreaming || updatedWorkflow.Kind != domain.WorkflowKindStandard || updatedWorkflow.Status != domain.WorkflowStatusReady {
|
|
t.Fatalf("expected normalized workflow update, got %#v", updatedWorkflow)
|
|
}
|
|
|
|
workflowInvalidCreateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
workflowInvalidCreateRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader([]byte(`{
|
|
"name":"Invalid Kind Workflow",
|
|
"description":"workflow metadata",
|
|
"type":"batch",
|
|
"kind":"custom",
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`))),
|
|
)
|
|
if workflowInvalidCreateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("invalid workflow create status: %d body=%s", workflowInvalidCreateRecorder.Code, workflowInvalidCreateRecorder.Body.String())
|
|
}
|
|
|
|
templateCreateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
templateCreateRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader([]byte(`{
|
|
"name":" Trimmed Template ",
|
|
"description":"template metadata",
|
|
"type":" batch ",
|
|
"kind":" standard ",
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`))),
|
|
)
|
|
if templateCreateRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("template create status: %d body=%s", templateCreateRecorder.Code, templateCreateRecorder.Body.String())
|
|
}
|
|
var createdTemplate domain.TemplateRecord
|
|
if err := json.Unmarshal(templateCreateRecorder.Body.Bytes(), &createdTemplate); err != nil {
|
|
t.Fatalf("decode template: %v", err)
|
|
}
|
|
if createdTemplate.Name != "Trimmed Template" || createdTemplate.Type != domain.WorkflowTypeBatch || createdTemplate.Kind != domain.WorkflowKindStandard {
|
|
t.Fatalf("expected normalized template metadata, got %#v", createdTemplate)
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
payload string
|
|
}{
|
|
{name: "blank name", payload: `{"name":" "}`},
|
|
{name: "invalid type", payload: `{"type":"realtime"}`},
|
|
{name: "invalid kind", payload: `{"kind":"custom"}`},
|
|
} {
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/templates/"+createdTemplate.ID, bytes.NewReader([]byte(tc.payload))),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected template %s to fail, got %d body=%s", tc.name, recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
templateUpdateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
templateUpdateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/templates/"+createdTemplate.ID, bytes.NewReader([]byte(`{
|
|
"name":" Streaming Template ",
|
|
"type":" streaming ",
|
|
"kind":" standard "
|
|
}`))),
|
|
)
|
|
if templateUpdateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("template update status: %d body=%s", templateUpdateRecorder.Code, templateUpdateRecorder.Body.String())
|
|
}
|
|
var updatedTemplate domain.TemplateRecord
|
|
if err := json.Unmarshal(templateUpdateRecorder.Body.Bytes(), &updatedTemplate); err != nil {
|
|
t.Fatalf("decode updated template: %v", err)
|
|
}
|
|
if updatedTemplate.Name != "Streaming Template" || updatedTemplate.Type != domain.WorkflowTypeStreaming || updatedTemplate.Kind != domain.WorkflowKindStandard {
|
|
t.Fatalf("expected normalized template update, got %#v", updatedTemplate)
|
|
}
|
|
}
|
|
|
|
func TestOperatorMetadataValidationAndNormalization(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
createRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/operators", bytes.NewReader([]byte(`{
|
|
"name":" Custom Operator ",
|
|
"category":" CV ",
|
|
"description":" Normalize metadata ",
|
|
"image":" registry.example.com/custom/operator:v1 ",
|
|
"inputs":[{"id":" in-1 ","name":" input ","type":" JSON ","description":" source ","transmission":" BUS "}],
|
|
"outputs":[{"id":"out-1","name":" result ","type":" FILE ","transmission":" OSS "}],
|
|
"resources":{"cpuLimit":" 500m ","memoryLimit":" 1Gi ","gpuLimit":" 1 "}
|
|
}`))),
|
|
)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("operator create status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var created domain.OperatorRecord
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode operator: %v", err)
|
|
}
|
|
if created.Name != "Custom Operator" || created.Type != "custom" || created.Category != "CV" || created.Description != "Normalize metadata" {
|
|
t.Fatalf("expected normalized operator metadata, got %#v", created)
|
|
}
|
|
if created.Image != "registry.example.com/custom/operator:v1" {
|
|
t.Fatalf("expected trimmed image, got %q", created.Image)
|
|
}
|
|
if len(created.Inputs) != 1 || created.Inputs[0].ID != "in-1" || created.Inputs[0].Name != "input" || created.Inputs[0].Type != domain.PortTypeJSON || created.Inputs[0].Transmission != "bus" {
|
|
t.Fatalf("expected normalized input port, got %#v", created.Inputs)
|
|
}
|
|
if len(created.Outputs) != 1 || created.Outputs[0].Type != domain.PortTypeFile || created.Outputs[0].Transmission != "oss" {
|
|
t.Fatalf("expected normalized output port, got %#v", created.Outputs)
|
|
}
|
|
if created.Resources == nil || created.Resources.CPULimit != "500m" || created.Resources.MemoryLimit != "1Gi" || created.Resources.GPULimit != "1" {
|
|
t.Fatalf("expected normalized resources, got %#v", created.Resources)
|
|
}
|
|
|
|
builtinCreateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
builtinCreateRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/operators", bytes.NewReader([]byte(`{
|
|
"name":"伪内置算子",
|
|
"type":"builtin",
|
|
"category":"IO",
|
|
"description":"should fail",
|
|
"inputs":[],
|
|
"outputs":[]
|
|
}`))),
|
|
)
|
|
if builtinCreateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected builtin operator registration to fail, got %d body=%s", builtinCreateRecorder.Code, builtinCreateRecorder.Body.String())
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
payload string
|
|
}{
|
|
{name: "blank name", payload: `{"name":" "}`},
|
|
{name: "blank category", payload: `{"category":" "}`},
|
|
{name: "blank description", payload: `{"description":" "}`},
|
|
{name: "type change", payload: `{"type":"builtin"}`},
|
|
{name: "invalid type", payload: `{"type":"system"}`},
|
|
{name: "invalid input type", payload: `{"inputs":[{"id":"in-1","name":"input","type":"audio"}]}`},
|
|
{name: "invalid output transmission", payload: `{"outputs":[{"id":"out-1","name":"result","type":"json","transmission":"mailbox"}]}`},
|
|
{name: "duplicate output ids", payload: `{"outputs":[{"id":"dup","name":"one","type":"json"},{"id":"dup","name":"two","type":"json"}]}`},
|
|
{name: "invalid resources", payload: `{"resources":{"gpuLimit":"half"}}`},
|
|
} {
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/operators/"+created.ID, bytes.NewReader([]byte(tc.payload))),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected %s to fail, got %d body=%s", tc.name, recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
stored, err := dataStore.GetOperator(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("reload operator: %v", err)
|
|
}
|
|
if stored == nil || stored.Name != created.Name || stored.Type != created.Type || stored.Category != created.Category || stored.Description != created.Description {
|
|
t.Fatalf("expected invalid operator updates to preserve metadata, got %#v", stored)
|
|
}
|
|
|
|
updateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
updateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/operators/"+created.ID, bytes.NewReader([]byte(`{
|
|
"name":" Updated Operator ",
|
|
"type":" CUSTOM ",
|
|
"category":" AI ",
|
|
"description":" Updated description ",
|
|
"outputs":[{"id":"out-2","name":" tensor result ","type":" TENSOR ","transmission":" STREAM "}],
|
|
"resources":{"cpuLimit":" 750m ","memoryLimit":" 2Gi "}
|
|
}`))),
|
|
)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("operator update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
var updated domain.OperatorRecord
|
|
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updated); err != nil {
|
|
t.Fatalf("decode updated operator: %v", err)
|
|
}
|
|
if updated.Name != "Updated Operator" || updated.Type != "custom" || updated.Category != "AI" || updated.Description != "Updated description" {
|
|
t.Fatalf("expected normalized operator update, got %#v", updated)
|
|
}
|
|
if len(updated.Outputs) != 1 || updated.Outputs[0].ID != "out-2" || updated.Outputs[0].Name != "tensor result" || updated.Outputs[0].Type != domain.PortTypeTensor || updated.Outputs[0].Transmission != "stream" {
|
|
t.Fatalf("expected normalized updated output port, got %#v", updated.Outputs)
|
|
}
|
|
if updated.Resources == nil || updated.Resources.CPULimit != "750m" || updated.Resources.MemoryLimit != "2Gi" || updated.Resources.GPULimit != "" {
|
|
t.Fatalf("expected normalized updated resources, got %#v", updated.Resources)
|
|
}
|
|
}
|
|
|
|
func TestCreateAndUpdatePreserveOperatorRunnerImage(t *testing.T) {
|
|
t.Setenv("OPERATOR_RUNNER_IMAGE", "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:20260412151842-amd64")
|
|
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
oldRunnerImage := "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:20260410181157-amd64"
|
|
customImage := "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/custom-operator:1"
|
|
|
|
operatorCreatePayload := []byte(`{
|
|
"name":"镜像归一化算子",
|
|
"type":"custom",
|
|
"category":"IO",
|
|
"description":"normalize operator image",
|
|
"image":"` + oldRunnerImage + `",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
|
}`)
|
|
operatorCreateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(operatorCreateRecorder, httptest.NewRequest(http.MethodPost, "/api/operators", bytes.NewReader(operatorCreatePayload)))
|
|
if operatorCreateRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("operator create status: %d body=%s", operatorCreateRecorder.Code, operatorCreateRecorder.Body.String())
|
|
}
|
|
|
|
var createdOperator domain.OperatorRecord
|
|
if err := json.Unmarshal(operatorCreateRecorder.Body.Bytes(), &createdOperator); err != nil {
|
|
t.Fatalf("decode created operator: %v", err)
|
|
}
|
|
if createdOperator.Image != oldRunnerImage {
|
|
t.Fatalf("expected created operator image %q, got %q", oldRunnerImage, createdOperator.Image)
|
|
}
|
|
|
|
storedOperator, err := dataStore.GetOperator(ctx, createdOperator.ID)
|
|
if err != nil {
|
|
t.Fatalf("get stored operator: %v", err)
|
|
}
|
|
if storedOperator == nil || storedOperator.Image != oldRunnerImage {
|
|
t.Fatalf("expected stored operator image %q, got %+v", oldRunnerImage, storedOperator)
|
|
}
|
|
|
|
operatorUpdatePayload := []byte(`{"image":"` + oldRunnerImage + `"}`)
|
|
operatorUpdateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(operatorUpdateRecorder, httptest.NewRequest(http.MethodPut, "/api/operators/"+createdOperator.ID, bytes.NewReader(operatorUpdatePayload)))
|
|
if operatorUpdateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("operator update status: %d body=%s", operatorUpdateRecorder.Code, operatorUpdateRecorder.Body.String())
|
|
}
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "镜像归一化项目",
|
|
Description: "normalize workflow images",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflowCreatePayload := []byte(`{
|
|
"name":"镜像归一化工作流",
|
|
"description":"normalize workflow node image",
|
|
"type":"batch",
|
|
"nodes":[
|
|
{
|
|
"id":"runner-node",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"Runner Node",
|
|
"category":"IO",
|
|
"description":"runner node",
|
|
"image":"` + oldRunnerImage + `",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
|
},
|
|
"position":{"x":0,"y":0}
|
|
},
|
|
{
|
|
"id":"custom-node",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"Custom Node",
|
|
"category":"IO",
|
|
"description":"custom node",
|
|
"image":"` + customImage + `",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-2","name":"output","type":"json"}]
|
|
},
|
|
"position":{"x":180,"y":0}
|
|
}
|
|
],
|
|
"edges":[]
|
|
}`)
|
|
workflowCreateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(workflowCreateRecorder, httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(workflowCreatePayload)))
|
|
if workflowCreateRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("workflow create status: %d body=%s", workflowCreateRecorder.Code, workflowCreateRecorder.Body.String())
|
|
}
|
|
|
|
var createdWorkflow domain.WorkflowRecord
|
|
if err := json.Unmarshal(workflowCreateRecorder.Body.Bytes(), &createdWorkflow); err != nil {
|
|
t.Fatalf("decode created workflow: %v", err)
|
|
}
|
|
if createdWorkflow.Nodes[0].Data.Image != oldRunnerImage {
|
|
t.Fatalf("expected created workflow runner image %q, got %q", oldRunnerImage, createdWorkflow.Nodes[0].Data.Image)
|
|
}
|
|
if createdWorkflow.Nodes[1].Data.Image != customImage {
|
|
t.Fatalf("expected custom workflow image to remain %q, got %q", customImage, createdWorkflow.Nodes[1].Data.Image)
|
|
}
|
|
|
|
storedWorkflow, err := dataStore.GetWorkflow(ctx, createdWorkflow.ID)
|
|
if err != nil {
|
|
t.Fatalf("get stored workflow: %v", err)
|
|
}
|
|
if storedWorkflow == nil || storedWorkflow.Nodes[0].Data.Image != oldRunnerImage {
|
|
t.Fatalf("expected stored workflow runner image %q, got %+v", oldRunnerImage, storedWorkflow)
|
|
}
|
|
if storedWorkflow.Nodes[1].Data.Image != customImage {
|
|
t.Fatalf("expected stored custom workflow image to remain %q, got %q", customImage, storedWorkflow.Nodes[1].Data.Image)
|
|
}
|
|
|
|
workflowUpdatePayload := []byte(`{
|
|
"nodes":[
|
|
{
|
|
"id":"runner-node",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"Runner Node",
|
|
"category":"IO",
|
|
"description":"runner node",
|
|
"image":"` + oldRunnerImage + `",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
|
},
|
|
"position":{"x":0,"y":0}
|
|
},
|
|
{
|
|
"id":"custom-node",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"Custom Node",
|
|
"category":"IO",
|
|
"description":"custom node",
|
|
"image":"` + customImage + `",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-2","name":"output","type":"json"}]
|
|
},
|
|
"position":{"x":180,"y":0}
|
|
}
|
|
]
|
|
}`)
|
|
workflowUpdateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(workflowUpdateRecorder, httptest.NewRequest(http.MethodPut, "/api/workflows/"+createdWorkflow.ID, bytes.NewReader(workflowUpdatePayload)))
|
|
if workflowUpdateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("workflow update status: %d body=%s", workflowUpdateRecorder.Code, workflowUpdateRecorder.Body.String())
|
|
}
|
|
|
|
templateCreatePayload := []byte(`{
|
|
"name":"镜像归一化模板",
|
|
"description":"normalize template node image",
|
|
"type":"batch",
|
|
"nodes":[
|
|
{
|
|
"id":"runner-node",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"Runner Node",
|
|
"category":"IO",
|
|
"description":"runner node",
|
|
"image":"` + oldRunnerImage + `",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
|
},
|
|
"position":{"x":0,"y":0}
|
|
},
|
|
{
|
|
"id":"custom-node",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"Custom Node",
|
|
"category":"IO",
|
|
"description":"custom node",
|
|
"image":"` + customImage + `",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-2","name":"output","type":"json"}]
|
|
},
|
|
"position":{"x":180,"y":0}
|
|
}
|
|
],
|
|
"edges":[]
|
|
}`)
|
|
templateCreateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(templateCreateRecorder, httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(templateCreatePayload)))
|
|
if templateCreateRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("template create status: %d body=%s", templateCreateRecorder.Code, templateCreateRecorder.Body.String())
|
|
}
|
|
|
|
var createdTemplate domain.TemplateRecord
|
|
if err := json.Unmarshal(templateCreateRecorder.Body.Bytes(), &createdTemplate); err != nil {
|
|
t.Fatalf("decode created template: %v", err)
|
|
}
|
|
if createdTemplate.Nodes[0].Data.Image != oldRunnerImage {
|
|
t.Fatalf("expected created template runner image %q, got %q", oldRunnerImage, createdTemplate.Nodes[0].Data.Image)
|
|
}
|
|
if createdTemplate.Nodes[1].Data.Image != customImage {
|
|
t.Fatalf("expected custom template image to remain %q, got %q", customImage, createdTemplate.Nodes[1].Data.Image)
|
|
}
|
|
|
|
storedTemplate, err := dataStore.GetTemplate(ctx, createdTemplate.ID)
|
|
if err != nil {
|
|
t.Fatalf("get stored template: %v", err)
|
|
}
|
|
if storedTemplate == nil || storedTemplate.Nodes[0].Data.Image != oldRunnerImage {
|
|
t.Fatalf("expected stored template runner image %q, got %+v", oldRunnerImage, storedTemplate)
|
|
}
|
|
if storedTemplate.Nodes[1].Data.Image != customImage {
|
|
t.Fatalf("expected stored custom template image to remain %q, got %q", customImage, storedTemplate.Nodes[1].Data.Image)
|
|
}
|
|
|
|
templateUpdatePayload := []byte(`{
|
|
"nodes":[
|
|
{
|
|
"id":"runner-node",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"Runner Node",
|
|
"category":"IO",
|
|
"description":"runner node",
|
|
"image":"` + oldRunnerImage + `",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-1","name":"output","type":"json"}]
|
|
},
|
|
"position":{"x":0,"y":0}
|
|
},
|
|
{
|
|
"id":"custom-node",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"Custom Node",
|
|
"category":"IO",
|
|
"description":"custom node",
|
|
"image":"` + customImage + `",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-2","name":"output","type":"json"}]
|
|
},
|
|
"position":{"x":180,"y":0}
|
|
}
|
|
]
|
|
}`)
|
|
templateUpdateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(templateUpdateRecorder, httptest.NewRequest(http.MethodPut, "/api/templates/"+createdTemplate.ID, bytes.NewReader(templateUpdatePayload)))
|
|
if templateUpdateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("template update status: %d body=%s", templateUpdateRecorder.Code, templateUpdateRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCompletedWorkflowCannotBeRunAgainManually(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "已完成项目",
|
|
Description: "禁止重复运行",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "已完成工作流",
|
|
Description: "completed",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
workflow.Status = domain.WorkflowStatusCompleted
|
|
if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil {
|
|
t.Fatalf("mark workflow completed: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
runRecorder := httptest.NewRecorder()
|
|
runRequest := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/workflows/"+workflow.ID+"/run",
|
|
bytes.NewReader([]byte(`{"trigger":"manual"}`)),
|
|
)
|
|
handler.ServeHTTP(runRecorder, runRequest)
|
|
if runRecorder.Code != http.StatusConflict {
|
|
t.Fatalf("expected 409 for completed workflow rerun, got %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
|
}
|
|
|
|
var body map[string]string
|
|
if err := json.Unmarshal(runRecorder.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("decode rerun response: %v", err)
|
|
}
|
|
if body["error"] == "" {
|
|
t.Fatalf("expected rerun error message")
|
|
}
|
|
}
|
|
|
|
func TestProjectlessScanWorkflowCanRun(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
Name: "无项目扫描工作流",
|
|
Description: "projectless scan workflow",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Kind: domain.WorkflowKindScan,
|
|
ScanConfig: &domain.ScanConfig{
|
|
DataSource: "BOS",
|
|
SourcePath: "bos://datastorage/algo/X-platform-test",
|
|
Bucket: "datastorage",
|
|
Prefix: "algo/X-platform-test",
|
|
Endpoint: "https://bos.example.test",
|
|
AccessKeyID: "ak-projectless-1234",
|
|
SecretAccessKey: "sk-projectless-5678",
|
|
Enabled: false,
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
runRecorder := httptest.NewRecorder()
|
|
runRequest := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/workflows/"+workflow.ID+"/run",
|
|
bytes.NewReader([]byte(`{"trigger":"manual"}`)),
|
|
)
|
|
handler.ServeHTTP(runRecorder, runRequest)
|
|
if runRecorder.Code != http.StatusAccepted {
|
|
t.Fatalf("expected 202 for projectless scan workflow run, got %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
|
}
|
|
|
|
var run domain.RunRecord
|
|
if err := json.Unmarshal(runRecorder.Body.Bytes(), &run); err != nil {
|
|
t.Fatalf("decode run response: %v", err)
|
|
}
|
|
if run.ProjectID != "" || run.ProjectName != "" {
|
|
t.Fatalf("expected blank project scope for projectless run, got %+v", run)
|
|
}
|
|
}
|
|
|
|
func TestRunningWorkflowCanBeStoppedGracefully(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "停止项目",
|
|
Description: "graceful stop",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "停止工作流",
|
|
Description: "stoppable",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
run.Status = domain.RunStatusRunning
|
|
run.StartedAt = nowISO()
|
|
run.UpdatedAt = nowISO()
|
|
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
|
t.Fatalf("upsert running run: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodPost, "/api/runs/"+run.ID+"/stop", nil)
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("expected 200 from graceful stop, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
stored, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get stored run: %v", err)
|
|
}
|
|
if stored == nil || stored.Status != domain.RunStatus("stopping") {
|
|
t.Fatalf("expected run to enter stopping state, got %#v", stored)
|
|
}
|
|
}
|
|
|
|
func TestRunningWorkflowCanBeForceStopped(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
originalFactory := runJobTerminatorFactory
|
|
runJobTerminatorFactory = func(namespace string, imagePullSecretName string, workspacePVCName string, workspaceMountPath string) (runJobTerminator, error) {
|
|
return testRunJobTerminator{}, nil
|
|
}
|
|
defer func() {
|
|
runJobTerminatorFactory = originalFactory
|
|
}()
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "强制停止项目",
|
|
Description: "force stop",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "强制停止工作流",
|
|
Description: "force stoppable",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "数据源",
|
|
Category: "IO",
|
|
Description: "source",
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual)
|
|
if err != nil {
|
|
t.Fatalf("create run: %v", err)
|
|
}
|
|
run.Status = domain.RunStatusRunning
|
|
run.StartedAt = nowISO()
|
|
run.UpdatedAt = nowISO()
|
|
run.K8sRefs = []domain.K8sRef{{NodeID: "source", JobName: "run-source-1", Namespace: "droboticflow"}}
|
|
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
|
t.Fatalf("upsert running run: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodPost, "/api/runs/"+run.ID+"/force-stop", nil)
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("expected 200 from force stop, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
stored, err := dataStore.GetRun(ctx, run.ID)
|
|
if err != nil {
|
|
t.Fatalf("get stored run: %v", err)
|
|
}
|
|
if stored == nil || stored.Status != domain.RunStatusCancelled {
|
|
t.Fatalf("expected run to be cancelled, got %#v", stored)
|
|
}
|
|
if stored.EndedAt == "" {
|
|
t.Fatalf("expected cancelled run to be ended")
|
|
}
|
|
}
|
|
|
|
type testRunJobTerminator struct{}
|
|
|
|
func (testRunJobTerminator) TerminateRun(context.Context, string, string) error { return nil }
|
|
|
|
func (testRunJobTerminator) TerminateJob(context.Context, string, string) error { return nil }
|
|
|
|
func TestProjectWorkflowRejectsInvalidScanSourceIncrementalUnit(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "非法增量单位项目",
|
|
Description: "invalid incremental unit",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
handler := NewHandler(dataStore, nil)
|
|
payload := []byte(`{
|
|
"name":"非法增量单位工作流",
|
|
"description":"invalid incremental unit",
|
|
"type":"batch",
|
|
"nodes":[{
|
|
"id":"scan-source",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"BOS 增量扫描",
|
|
"hyperparams":[
|
|
{"key":"DATA_SOURCE","value":"BOS"},
|
|
{"key":"SOURCE_PATH","value":"/data/source"},
|
|
{"key":"SCAN_MODE","value":"INCREMENTAL"},
|
|
{"key":"INCREMENTAL_UNIT","value":"ROW"}
|
|
]
|
|
}
|
|
}],
|
|
"edges":[]
|
|
}`)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, `"field":"incrementalUnit"`) || !strings.Contains(body, "INCREMENTAL_UNIT must be OBJECT or DIRECTORY") {
|
|
t.Fatalf("expected incrementalUnit validation issue, got %s", body)
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowRejectsInvalidScanSourceScanMode(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "非法扫描模式项目",
|
|
Description: "invalid scan mode",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
handler := NewHandler(dataStore, nil)
|
|
payload := []byte(`{
|
|
"name":"非法扫描模式工作流",
|
|
"description":"invalid scan mode",
|
|
"type":"batch",
|
|
"nodes":[{
|
|
"id":"scan-source",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"BOS 增量扫描",
|
|
"hyperparams":[
|
|
{"key":"DATA_SOURCE","value":"BOS"},
|
|
{"key":"SOURCE_PATH","value":"/data/source"},
|
|
{"key":"SCAN_MODE","value":"ROW"}
|
|
]
|
|
}
|
|
}],
|
|
"edges":[]
|
|
}`)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, `"field":"scanMode"`) || !strings.Contains(body, "SCAN_MODE must be FULL or INCREMENTAL") {
|
|
t.Fatalf("expected scanMode validation issue, got %s", body)
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowRejectsInvalidScanSourceDataSource(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "非法扫描数据源项目",
|
|
Description: "invalid scan data source",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
handler := NewHandler(dataStore, nil)
|
|
payload := []byte(`{
|
|
"name":"非法扫描数据源工作流",
|
|
"description":"invalid scan data source",
|
|
"type":"batch",
|
|
"nodes":[{
|
|
"id":"scan-source",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"BOS 增量扫描",
|
|
"hyperparams":[
|
|
{"key":"DATA_SOURCE","value":"ROW"},
|
|
{"key":"SOURCE_PATH","value":"bos://datastorage/algo/X-platform-test"},
|
|
{"key":"SCAN_MODE","value":"INCREMENTAL"}
|
|
]
|
|
}
|
|
}],
|
|
"edges":[]
|
|
}`)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
body := recorder.Body.String()
|
|
if !strings.Contains(body, `"field":"dataSource"`) || !strings.Contains(body, "DATA_SOURCE must be BOS for scan workflows") {
|
|
t.Fatalf("expected dataSource validation issue, got %s", body)
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowAPISupportsNativeScanWorkflows(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
for _, name := range []string{"BOS 增量扫描", "X 平台数据集索引", "采集目录校验"} {
|
|
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
|
Name: name,
|
|
Type: "builtin",
|
|
Category: "IO",
|
|
Description: name,
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "output", Type: domain.PortTypeJSON}},
|
|
}); err != nil {
|
|
t.Fatalf("create operator %s: %v", name, err)
|
|
}
|
|
}
|
|
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "扫描项目",
|
|
Description: "native workflow scan api",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
createPayload := []byte(`{
|
|
"name":"X-platform BOS 扫描",
|
|
"description":"扫描 workflow 原生创建",
|
|
"type":"batch",
|
|
"kind":"scan",
|
|
"schedule":{"interval":900},
|
|
"scanConfig":{
|
|
"dataSource":"BOS",
|
|
"bucket":"datastorage",
|
|
"prefix":"algo/X-platform-test/",
|
|
"endpoint":"https://bos.example.test",
|
|
"accessKeyId":"ak-native-1234",
|
|
"secretAccessKey":"sk-native-5678",
|
|
"scanMode":"INCREMENTAL",
|
|
"incrementalUnit":"DIRECTORY",
|
|
"directoryDepth":2,
|
|
"pattern":"*.mp4",
|
|
"enabled":true
|
|
},
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`)
|
|
invalidCreatePayload := []byte(strings.Replace(string(createPayload), `"endpoint":"https://bos.example.test"`, `"endpoint":"ftp://bos.example.test"`, 1))
|
|
invalidCreateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidCreateRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(invalidCreatePayload)),
|
|
)
|
|
if invalidCreateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid scan endpoint create failure, got %d body=%s", invalidCreateRecorder.Code, invalidCreateRecorder.Body.String())
|
|
}
|
|
invalidCreateBody := invalidCreateRecorder.Body.String()
|
|
if !strings.Contains(invalidCreateBody, `"field":"endpoint"`) || !strings.Contains(invalidCreateBody, "absolute http(s) URL") {
|
|
t.Fatalf("expected endpoint validation issue, got %s", invalidCreateBody)
|
|
}
|
|
|
|
createRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
|
)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("create scan workflow status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var created domain.WorkflowRecord
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created workflow: %v", err)
|
|
}
|
|
if created.Kind != domain.WorkflowKindScan {
|
|
t.Fatalf("expected scan workflow kind, got %s", created.Kind)
|
|
}
|
|
if created.Type != domain.WorkflowTypeStreaming {
|
|
t.Fatalf("expected streaming scan workflow, got %s", created.Type)
|
|
}
|
|
if created.ScanConfig == nil {
|
|
t.Fatalf("expected scan config in response")
|
|
}
|
|
if created.ScanConfig.AccessKeyID != "" || created.ScanConfig.SecretAccessKey != "" {
|
|
t.Fatalf("expected masked secrets in response: %+v", created.ScanConfig)
|
|
}
|
|
if !created.ScanConfig.HasAccessKeyID || !created.ScanConfig.HasSecretAccessKey {
|
|
t.Fatalf("expected secret presence flags in response: %+v", created.ScanConfig)
|
|
}
|
|
if created.ScanConfig.AccessKeyPreview == "" || created.ScanConfig.SecretAccessKeyPreview == "" {
|
|
t.Fatalf("expected secret previews in response: %+v", created.ScanConfig)
|
|
}
|
|
if created.ScanConfig.ScanMode != "INCREMENTAL" {
|
|
t.Fatalf("expected incremental scan mode, got %s", created.ScanConfig.ScanMode)
|
|
}
|
|
if created.ScanConfig.IncrementalUnit != "DIRECTORY" || created.ScanConfig.DirectoryDepth != 2 {
|
|
t.Fatalf("expected directory incremental config, got %+v", created.ScanConfig)
|
|
}
|
|
if len(created.Nodes) != 3 || len(created.Edges) != 2 {
|
|
t.Fatalf("expected default scan graph scaffold, got %d nodes and %d edges", len(created.Nodes), len(created.Edges))
|
|
}
|
|
|
|
stored, err := dataStore.GetWorkflow(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("get stored workflow: %v", err)
|
|
}
|
|
if stored == nil || stored.ScanConfig == nil {
|
|
t.Fatalf("expected stored workflow scan config")
|
|
}
|
|
if stored.ScanConfig.AccessKeyID != "ak-native-1234" || stored.ScanConfig.SecretAccessKey != "sk-native-5678" {
|
|
t.Fatalf("expected secrets preserved in store: %+v", stored.ScanConfig)
|
|
}
|
|
if stored.ScanConfig.IncrementalUnit != "DIRECTORY" || stored.ScanConfig.DirectoryDepth != 2 {
|
|
t.Fatalf("expected stored directory incremental config, got %+v", stored.ScanConfig)
|
|
}
|
|
if stored.Schedule == nil || stored.Schedule.Interval != 900 {
|
|
t.Fatalf("expected schedule interval persisted, got %+v", stored.Schedule)
|
|
}
|
|
if stored.Schedule.SourcePath != "bos://datastorage/algo/X-platform-test" {
|
|
t.Fatalf("unexpected stored source path: %+v", stored.Schedule)
|
|
}
|
|
|
|
invalidUpdatePayload := []byte(`{
|
|
"scanConfig":{
|
|
"dataSource":"BOS",
|
|
"bucket":"datastorage",
|
|
"prefix":"algo/X-platform-test",
|
|
"endpoint":"https://bos.example.test/bad path",
|
|
"accessKeyId":"",
|
|
"secretAccessKey":"",
|
|
"scanMode":"INCREMENTAL",
|
|
"incrementalUnit":"DIRECTORY",
|
|
"directoryDepth":2,
|
|
"pattern":"*.mp4",
|
|
"enabled":true
|
|
}
|
|
}`)
|
|
invalidUpdateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidUpdateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/workflows/"+created.ID, bytes.NewReader(invalidUpdatePayload)),
|
|
)
|
|
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid scan endpoint update failure, got %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
|
}
|
|
invalidUpdateBody := invalidUpdateRecorder.Body.String()
|
|
if !strings.Contains(invalidUpdateBody, `"field":"endpoint"`) || !strings.Contains(invalidUpdateBody, "without whitespace") {
|
|
t.Fatalf("expected endpoint whitespace validation issue, got %s", invalidUpdateBody)
|
|
}
|
|
afterInvalidUpdate, err := dataStore.GetWorkflow(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("get workflow after invalid update: %v", err)
|
|
}
|
|
if afterInvalidUpdate == nil || afterInvalidUpdate.ScanConfig == nil || afterInvalidUpdate.ScanConfig.Endpoint != "https://bos.example.test" {
|
|
t.Fatalf("expected invalid update to leave scan endpoint unchanged, got %+v", afterInvalidUpdate)
|
|
}
|
|
|
|
updatePayload := []byte(`{
|
|
"schedule":{"interval":0},
|
|
"scanConfig":{
|
|
"dataSource":"BOS",
|
|
"bucket":"datastorage",
|
|
"prefix":"algo/X-platform-test",
|
|
"endpoint":"https://bos.example.test",
|
|
"accessKeyId":"",
|
|
"secretAccessKey":"",
|
|
"scanMode":"FULL",
|
|
"incrementalUnit":"OBJECT",
|
|
"directoryDepth":1,
|
|
"pattern":"*.json",
|
|
"enabled":false
|
|
}
|
|
}`)
|
|
updateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
updateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/workflows/"+created.ID, bytes.NewReader(updatePayload)),
|
|
)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("update scan workflow status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
|
|
updated, err := dataStore.GetWorkflow(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("get updated workflow: %v", err)
|
|
}
|
|
if updated == nil || updated.ScanConfig == nil {
|
|
t.Fatalf("expected updated scan config")
|
|
}
|
|
if updated.Type != domain.WorkflowTypeBatch {
|
|
t.Fatalf("expected batch after disabling scan schedule, got %s", updated.Type)
|
|
}
|
|
if updated.ScanConfig.AccessKeyID != "ak-native-1234" || updated.ScanConfig.SecretAccessKey != "sk-native-5678" {
|
|
t.Fatalf("expected secrets preserved on generic workflow update: %+v", updated.ScanConfig)
|
|
}
|
|
if updated.ScanConfig.ScanMode != "FULL" {
|
|
t.Fatalf("expected full scan mode after update, got %s", updated.ScanConfig.ScanMode)
|
|
}
|
|
if updated.ScanConfig.IncrementalUnit != "OBJECT" || updated.ScanConfig.DirectoryDepth != 1 {
|
|
t.Fatalf("expected updated incremental config, got %+v", updated.ScanConfig)
|
|
}
|
|
if updated.Schedule == nil || updated.Schedule.Interval != 0 {
|
|
t.Fatalf("expected schedule interval reset to 0, got %+v", updated.Schedule)
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowCreateInfersScanConfigFromIncrementalSourceNode(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(context.Background(), domain.CreateProjectInput{
|
|
Name: "增量项目",
|
|
Description: "generic incremental dag",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
createPayload := []byte(`{
|
|
"name":"增量全链路",
|
|
"description":"generic incremental dag",
|
|
"type":"streaming",
|
|
"schedule":{"interval":45},
|
|
"nodes":[
|
|
{
|
|
"id":"source",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"BOS 增量扫描",
|
|
"category":"IO",
|
|
"description":"source",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-1","name":"manifest","type":"json"}],
|
|
"bucket":"wjzh-data-migration",
|
|
"prefix":"wjzh-0330-Data-migration",
|
|
"pattern":"*.mp4",
|
|
"hyperparams":[
|
|
{"key":"DATA_SOURCE","value":"BOS"},
|
|
{"key":"SOURCE_PATH","value":"bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
|
{"key":"BOS_ENDPOINT","value":"https://bos.example.test"},
|
|
{"key":"BOS_ACCESS_KEY_ID","value":"ak-node-123"},
|
|
{"key":"BOS_SECRET_ACCESS_KEY","value":"sk-node-456"},
|
|
{"key":"SCAN_MODE","value":"INCREMENTAL"}
|
|
]
|
|
},
|
|
"position":{"x":0,"y":0}
|
|
},
|
|
{
|
|
"id":"quality",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"视频质检",
|
|
"category":"VIDEO",
|
|
"description":"quality",
|
|
"inputs":[{"id":"in-1","name":"input","type":"json"}],
|
|
"outputs":[{"id":"out-1","name":"quality","type":"json"}]
|
|
},
|
|
"position":{"x":200,"y":0}
|
|
}
|
|
],
|
|
"edges":[
|
|
{"id":"source-quality","source":"source","sourceHandle":"out-1","target":"quality","targetHandle":"in-1"}
|
|
]
|
|
}`)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
|
)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("create incremental workflow status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
var created domain.WorkflowRecord
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created workflow: %v", err)
|
|
}
|
|
if created.Kind != domain.WorkflowKindScan {
|
|
t.Fatalf("expected scan kind, got %s", created.Kind)
|
|
}
|
|
if created.Type != domain.WorkflowTypeStreaming {
|
|
t.Fatalf("expected streaming workflow, got %s", created.Type)
|
|
}
|
|
if created.ScanConfig == nil {
|
|
t.Fatalf("expected inferred scan config")
|
|
}
|
|
if created.ScanConfig.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
|
t.Fatalf("unexpected inferred source path: %+v", created.ScanConfig)
|
|
}
|
|
if created.ScanConfig.Bucket != "wjzh-data-migration" || created.ScanConfig.Prefix != "wjzh-0330-Data-migration" {
|
|
t.Fatalf("unexpected inferred bucket/prefix: %+v", created.ScanConfig)
|
|
}
|
|
if created.ScanConfig.AccessKeyID != "" || created.ScanConfig.SecretAccessKey != "" {
|
|
t.Fatalf("expected inferred secrets to be masked, got %+v", created.ScanConfig)
|
|
}
|
|
if created.ScanConfig.ScanMode != "INCREMENTAL" {
|
|
t.Fatalf("expected inferred incremental scan mode, got %+v", created.ScanConfig)
|
|
}
|
|
if created.MaxConcurrentRuns != 1 {
|
|
t.Fatalf("expected inferred scan workflow maxConcurrentRuns=1, got %d", created.MaxConcurrentRuns)
|
|
}
|
|
if created.Schedule == nil || created.Schedule.Interval != 45 || created.Schedule.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
|
t.Fatalf("unexpected inferred schedule: %+v", created.Schedule)
|
|
}
|
|
if len(created.Nodes) != 2 || len(created.Edges) != 1 {
|
|
t.Fatalf("expected original graph to be preserved, got %d nodes %d edges", len(created.Nodes), len(created.Edges))
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowCreateRejectsInvalidIncrementalDirectoryDepthHyperparam(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(context.Background(), domain.CreateProjectInput{
|
|
Name: "非法增量项目",
|
|
Description: "invalid incremental depth",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
createPayload := []byte(`{
|
|
"name":"非法目录深度工作流",
|
|
"description":"invalid incremental depth",
|
|
"type":"streaming",
|
|
"schedule":{"interval":45},
|
|
"nodes":[
|
|
{
|
|
"id":"source",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"BOS 增量扫描",
|
|
"category":"IO",
|
|
"description":"source",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-1","name":"manifest","type":"json"}],
|
|
"bucket":"wjzh-data-migration",
|
|
"prefix":"wjzh-0330-Data-migration",
|
|
"hyperparams":[
|
|
{"key":"DATA_SOURCE","value":"BOS"},
|
|
{"key":"SOURCE_PATH","value":"bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
|
{"key":"BOS_ENDPOINT","value":"https://bos.example.test"},
|
|
{"key":"BOS_ACCESS_KEY_ID","value":"ak-node-123"},
|
|
{"key":"BOS_SECRET_ACCESS_KEY","value":"sk-node-456"},
|
|
{"key":"SCAN_MODE","value":"INCREMENTAL"},
|
|
{"key":"INCREMENTAL_DIRECTORY_DEPTH","value":"abc"}
|
|
]
|
|
},
|
|
"position":{"x":0,"y":0}
|
|
}
|
|
],
|
|
"edges":[]
|
|
}`)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid depth to be rejected, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "INCREMENTAL_DIRECTORY_DEPTH must be a positive integer") {
|
|
t.Fatalf("expected directory depth validation error, got %s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowCreatePreservesBOSSourcePathWhenUsingConnectionReference(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "连接引用扫描项目",
|
|
Description: "connection + sourcePath",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "WJZH Connection",
|
|
Provider: domain.StorageProviderBOS,
|
|
Endpoint: "https://bos.example.test",
|
|
Bucket: "wjzh-data-migration",
|
|
AccessKeyID: "ak-conn",
|
|
SecretAccessKey: "sk-conn",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
createPayload := []byte(fmt.Sprintf(`{
|
|
"name":"连接引用增量烟测",
|
|
"description":"scan config should keep explicit sourcePath",
|
|
"type":"batch",
|
|
"nodes":[
|
|
{
|
|
"id":"source",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"BOS 增量扫描",
|
|
"category":"IO",
|
|
"description":"source",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-1","name":"manifest","type":"json"}],
|
|
"connectionId":"%s",
|
|
"hyperparams":[
|
|
{"key":"DATA_SOURCE","value":"BOS"},
|
|
{"key":"SOURCE_PATH","value":"bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
|
{"key":"SCAN_MODE","value":"FULL"}
|
|
]
|
|
},
|
|
"position":{"x":0,"y":0}
|
|
}
|
|
],
|
|
"edges":[]
|
|
}`, connection.ID))
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
|
)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("create workflow status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
var created domain.WorkflowRecord
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created workflow: %v", err)
|
|
}
|
|
if created.ScanConfig == nil {
|
|
t.Fatalf("expected scan config")
|
|
}
|
|
if created.ScanConfig.ConnectionID != connection.ID {
|
|
t.Fatalf("expected connection reference to be preserved, got %+v", created.ScanConfig)
|
|
}
|
|
if created.ScanConfig.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
|
t.Fatalf("expected explicit bos sourcePath to be preserved, got %+v", created.ScanConfig)
|
|
}
|
|
|
|
runRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
runRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/workflows/"+created.ID+"/run", bytes.NewReader([]byte(`{"trigger":"manual"}`))),
|
|
)
|
|
if runRecorder.Code != http.StatusCreated && runRecorder.Code != http.StatusAccepted {
|
|
t.Fatalf("expected connectionId + sourcePath workflow to run, got %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestWorkflowUpdateInfersScanConfigFromIncrementalSourceNode(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "增量更新项目",
|
|
Description: "generic incremental dag",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "待升级增量工作流",
|
|
Description: "incremental update",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Schedule: &domain.WorkflowSchedule{Interval: 0},
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "BOS 增量扫描",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Bucket: "wjzh-data-migration",
|
|
Prefix: "wjzh-0330-Data-migration",
|
|
Pattern: "*.json",
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "BOS"},
|
|
{Key: "SOURCE_PATH", Value: "bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
|
{Key: "BOS_ENDPOINT", Value: "https://bos.example.test"},
|
|
{Key: "BOS_ACCESS_KEY_ID", Value: "ak-update-123"},
|
|
{Key: "BOS_SECRET_ACCESS_KEY", Value: "sk-update-456"},
|
|
{Key: "SCAN_MODE", Value: "FULL"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
updatePayload := []byte(`{
|
|
"type":"streaming",
|
|
"schedule":{"interval":75}
|
|
}`)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/workflows/"+workflow.ID, bytes.NewReader(updatePayload)),
|
|
)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("update incremental workflow status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
updated, err := dataStore.GetWorkflow(ctx, workflow.ID)
|
|
if err != nil {
|
|
t.Fatalf("get updated workflow: %v", err)
|
|
}
|
|
if updated == nil || updated.ScanConfig == nil {
|
|
t.Fatalf("expected inferred scan config after update")
|
|
}
|
|
if updated.Kind != domain.WorkflowKindScan {
|
|
t.Fatalf("expected scan kind after update, got %s", updated.Kind)
|
|
}
|
|
if updated.Type != domain.WorkflowTypeStreaming {
|
|
t.Fatalf("expected streaming type after update, got %s", updated.Type)
|
|
}
|
|
if updated.ScanConfig.AccessKeyID != "ak-update-123" || updated.ScanConfig.SecretAccessKey != "sk-update-456" {
|
|
t.Fatalf("expected secrets derived from node hyperparams, got %+v", updated.ScanConfig)
|
|
}
|
|
if updated.ScanConfig.ScanMode != "FULL" {
|
|
t.Fatalf("expected scan mode derived from node hyperparams, got %+v", updated.ScanConfig)
|
|
}
|
|
if updated.MaxConcurrentRuns != 1 {
|
|
t.Fatalf("expected inferred scan workflow maxConcurrentRuns=1 after update, got %d", updated.MaxConcurrentRuns)
|
|
}
|
|
if updated.Schedule == nil || updated.Schedule.Interval != 75 || updated.Schedule.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
|
t.Fatalf("unexpected updated schedule: %+v", updated.Schedule)
|
|
}
|
|
}
|
|
|
|
func TestTemplateCreateInfersScanConfigFromIncrementalSourceNode(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
createPayload := []byte(`{
|
|
"name":"增量模板",
|
|
"description":"template incremental dag",
|
|
"type":"streaming",
|
|
"schedule":{"interval":30},
|
|
"nodes":[
|
|
{
|
|
"id":"source",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"BOS 增量扫描",
|
|
"category":"IO",
|
|
"description":"source",
|
|
"inputs":[],
|
|
"outputs":[{"id":"out-1","name":"manifest","type":"json"}],
|
|
"bucket":"wjzh-data-migration",
|
|
"prefix":"wjzh-0330-Data-migration",
|
|
"pattern":"*.mp4",
|
|
"hyperparams":[
|
|
{"key":"DATA_SOURCE","value":"BOS"},
|
|
{"key":"SOURCE_PATH","value":"bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
|
{"key":"BOS_ENDPOINT","value":"https://bos.example.test"},
|
|
{"key":"BOS_ACCESS_KEY_ID","value":"ak-template-123"},
|
|
{"key":"BOS_SECRET_ACCESS_KEY","value":"sk-template-456"},
|
|
{"key":"SCAN_MODE","value":"INCREMENTAL"}
|
|
]
|
|
},
|
|
"position":{"x":0,"y":0}
|
|
}
|
|
],
|
|
"edges":[]
|
|
}`)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(createPayload)),
|
|
)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("create incremental template status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
var created domain.TemplateRecord
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created template: %v", err)
|
|
}
|
|
if created.Kind != domain.WorkflowKindScan {
|
|
t.Fatalf("expected scan template kind, got %s", created.Kind)
|
|
}
|
|
if created.Type != domain.WorkflowTypeStreaming {
|
|
t.Fatalf("expected streaming template type, got %s", created.Type)
|
|
}
|
|
if created.ScanConfig == nil {
|
|
t.Fatalf("expected inferred template scan config")
|
|
}
|
|
if created.ScanConfig.AccessKeyID != "" || created.ScanConfig.SecretAccessKey != "" {
|
|
t.Fatalf("expected inferred template secrets to be masked, got %+v", created.ScanConfig)
|
|
}
|
|
if created.MaxConcurrentRuns != 1 {
|
|
t.Fatalf("expected inferred scan template maxConcurrentRuns=1, got %d", created.MaxConcurrentRuns)
|
|
}
|
|
if created.Schedule == nil || created.Schedule.Interval != 30 || created.Schedule.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
|
t.Fatalf("unexpected template schedule: %+v", created.Schedule)
|
|
}
|
|
}
|
|
|
|
func TestTemplateUpdateInfersScanConfigFromIncrementalSourceNode(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
template, err := dataStore.CreateTemplate(ctx, domain.CreateTemplateInput{
|
|
Name: "待升级增量模板",
|
|
Description: "incremental template update",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Schedule: &domain.WorkflowSchedule{Interval: 0},
|
|
Nodes: []domain.WorkflowNode{
|
|
{
|
|
ID: "source",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "BOS 增量扫描",
|
|
Category: "IO",
|
|
Description: "source",
|
|
Bucket: "wjzh-data-migration",
|
|
Prefix: "wjzh-0330-Data-migration",
|
|
Pattern: "*.json",
|
|
Hyperparams: []domain.HyperParam{
|
|
{Key: "DATA_SOURCE", Value: "BOS"},
|
|
{Key: "SOURCE_PATH", Value: "bos://wjzh-data-migration/wjzh-0330-Data-migration"},
|
|
{Key: "BOS_ENDPOINT", Value: "https://bos.example.test"},
|
|
{Key: "BOS_ACCESS_KEY_ID", Value: "ak-template-update-123"},
|
|
{Key: "BOS_SECRET_ACCESS_KEY", Value: "sk-template-update-456"},
|
|
{Key: "SCAN_MODE", Value: "INCREMENTAL"},
|
|
},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
},
|
|
Edges: []domain.WorkflowEdge{},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create template: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
updatePayload := []byte(`{
|
|
"type":"streaming",
|
|
"schedule":{"interval":90}
|
|
}`)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/templates/"+template.ID, bytes.NewReader(updatePayload)),
|
|
)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("update incremental template status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
updated, err := dataStore.GetTemplate(ctx, template.ID)
|
|
if err != nil {
|
|
t.Fatalf("get updated template: %v", err)
|
|
}
|
|
if updated == nil || updated.ScanConfig == nil {
|
|
t.Fatalf("expected inferred template scan config after update")
|
|
}
|
|
if updated.Kind != domain.WorkflowKindScan {
|
|
t.Fatalf("expected scan template kind after update, got %s", updated.Kind)
|
|
}
|
|
if updated.Type != domain.WorkflowTypeStreaming {
|
|
t.Fatalf("expected streaming template type after update, got %s", updated.Type)
|
|
}
|
|
if updated.ScanConfig.AccessKeyID != "ak-template-update-123" || updated.ScanConfig.SecretAccessKey != "sk-template-update-456" {
|
|
t.Fatalf("expected template secrets derived from node hyperparams, got %+v", updated.ScanConfig)
|
|
}
|
|
if updated.ScanConfig.ScanMode != "INCREMENTAL" {
|
|
t.Fatalf("expected template scan mode derived from node hyperparams, got %+v", updated.ScanConfig)
|
|
}
|
|
if updated.MaxConcurrentRuns != 1 {
|
|
t.Fatalf("expected inferred scan template maxConcurrentRuns=1 after update, got %d", updated.MaxConcurrentRuns)
|
|
}
|
|
if updated.Schedule == nil || updated.Schedule.Interval != 90 || updated.Schedule.SourcePath != "bos://wjzh-data-migration/wjzh-0330-Data-migration" {
|
|
t.Fatalf("unexpected updated template schedule: %+v", updated.Schedule)
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowCreatePersistsEveryHoursSchedule(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(context.Background(), domain.CreateProjectInput{
|
|
Name: "小时调度项目",
|
|
Description: "hourly schedule",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
payload := []byte(`{
|
|
"name":"小时调度工作流",
|
|
"description":"hourly schedule",
|
|
"type":"streaming",
|
|
"schedule":{
|
|
"mode":"every_hours",
|
|
"intervalHours":4
|
|
},
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
|
)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("create hourly workflow status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
var created domain.WorkflowRecord
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created workflow: %v", err)
|
|
}
|
|
if created.Schedule == nil {
|
|
t.Fatalf("expected schedule persisted")
|
|
}
|
|
if created.Schedule.Mode != domain.WorkflowScheduleModeEveryHours {
|
|
t.Fatalf("expected every_hours mode, got %+v", created.Schedule)
|
|
}
|
|
if created.Schedule.IntervalHours != 4 {
|
|
t.Fatalf("expected intervalHours=4, got %+v", created.Schedule)
|
|
}
|
|
if strings.TrimSpace(created.Schedule.AnchorAt) == "" {
|
|
t.Fatalf("expected anchorAt populated, got %+v", created.Schedule)
|
|
}
|
|
}
|
|
|
|
func TestTemplateCreatePersistsEveryDaysScheduleDefaultsTimezone(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
payload := []byte(`{
|
|
"name":"按天模板",
|
|
"description":"daily schedule",
|
|
"type":"streaming",
|
|
"schedule":{
|
|
"mode":"every_days_at_time",
|
|
"dayInterval":2,
|
|
"timeOfDay":"18:30"
|
|
},
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(payload)),
|
|
)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("create daily template status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
|
|
var created domain.TemplateRecord
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created template: %v", err)
|
|
}
|
|
if created.Schedule == nil {
|
|
t.Fatalf("expected schedule persisted")
|
|
}
|
|
if created.Schedule.Mode != domain.WorkflowScheduleModeEveryDaysAtTime {
|
|
t.Fatalf("expected every_days_at_time mode, got %+v", created.Schedule)
|
|
}
|
|
if created.Schedule.DayInterval != 2 || created.Schedule.TimeOfDay != "18:30" {
|
|
t.Fatalf("unexpected daily schedule payload: %+v", created.Schedule)
|
|
}
|
|
if created.Schedule.Timezone != domain.DefaultWorkflowScheduleTimezone {
|
|
t.Fatalf("expected default timezone %q, got %+v", domain.DefaultWorkflowScheduleTimezone, created.Schedule)
|
|
}
|
|
if strings.TrimSpace(created.Schedule.AnchorAt) == "" {
|
|
t.Fatalf("expected anchorAt populated, got %+v", created.Schedule)
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowCreateRejectsInvalidEveryDaysSchedule(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(context.Background(), domain.CreateProjectInput{
|
|
Name: "非法调度项目",
|
|
Description: "invalid schedule",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
payload := []byte(`{
|
|
"name":"非法按天工作流",
|
|
"description":"invalid daily schedule",
|
|
"type":"streaming",
|
|
"schedule":{
|
|
"mode":"every_days_at_time",
|
|
"dayInterval":1,
|
|
"timeOfDay":"25:61"
|
|
},
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid daily schedule rejected, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "schedule.timeOfDay") {
|
|
t.Fatalf("expected timeOfDay validation issue, body=%s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestStorageConnectionsAPIStoresSecretsMasked(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
appConfig, err := config.LoadValidated()
|
|
if err != nil {
|
|
t.Fatalf("load test config: %v", err)
|
|
}
|
|
appConfig.GlobalBOSEndpoint = ""
|
|
handler := NewHandlerWithConfig(dataStore, nil, appConfig)
|
|
|
|
invalidCreateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidCreateRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/storage/connections", bytes.NewReader([]byte(`{
|
|
"name":"无效 BOS",
|
|
"provider":"BOS",
|
|
"endpoint":"bos.example.test",
|
|
"accessKeyId":"ak-invalid",
|
|
"secretAccessKey":"sk-invalid"
|
|
}`))),
|
|
)
|
|
if invalidCreateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid storage endpoint rejected, got %d body=%s", invalidCreateRecorder.Code, invalidCreateRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidCreateRecorder.Body.String(), "endpoint") {
|
|
t.Fatalf("expected endpoint validation issue, body=%s", invalidCreateRecorder.Body.String())
|
|
}
|
|
|
|
createPayload := []byte(`{
|
|
"name":"BOS 主连接",
|
|
"description":"扫描用 BOS 连接",
|
|
"provider":"BOS",
|
|
"endpoint":"https://bos.example.test",
|
|
"bucket":"datastorage",
|
|
"accessKeyId":"ak-connection-1234",
|
|
"secretAccessKey":"sk-connection-5678"
|
|
}`)
|
|
createRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/storage/connections", bytes.NewReader(createPayload)),
|
|
)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("create storage connection status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var created map[string]any
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created connection: %v", err)
|
|
}
|
|
if _, ok := created["accessKeyId"]; ok {
|
|
t.Fatalf("expected masked accessKeyId to be omitted: %#v", created)
|
|
}
|
|
if _, ok := created["secretAccessKey"]; ok {
|
|
t.Fatalf("expected masked secretAccessKey to be omitted: %#v", created)
|
|
}
|
|
if created["hasAccessKeyId"] != true || created["hasSecretAccessKey"] != true {
|
|
t.Fatalf("expected secret presence flags in response: %#v", created)
|
|
}
|
|
if created["accessKeyPreview"] == "" || created["secretAccessKeyPreview"] == "" {
|
|
t.Fatalf("expected secret previews in response: %#v", created)
|
|
}
|
|
|
|
stored, err := dataStore.GetStorageConnection(ctx, created["id"].(string))
|
|
if err != nil {
|
|
t.Fatalf("get stored storage connection: %v", err)
|
|
}
|
|
if stored == nil {
|
|
t.Fatalf("expected stored connection")
|
|
}
|
|
if stored.Bucket != "datastorage" {
|
|
t.Fatalf("expected bucket preserved in store: %#v", stored)
|
|
}
|
|
if stored.AccessKeyID != "ak-connection-1234" || stored.SecretAccessKey != "sk-connection-5678" {
|
|
t.Fatalf("expected real secrets preserved in store: %#v", stored)
|
|
}
|
|
|
|
updatePayload := []byte(`{
|
|
"description":"更新后的连接描述",
|
|
"endpoint":"https://gz.bcebos.com",
|
|
"bucket":"archive",
|
|
"accessKeyId":"",
|
|
"secretAccessKey":""
|
|
}`)
|
|
updateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
updateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/storage/connections/"+created["id"].(string), bytes.NewReader(updatePayload)),
|
|
)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("update storage connection status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
|
|
updated, err := dataStore.GetStorageConnection(ctx, created["id"].(string))
|
|
if err != nil {
|
|
t.Fatalf("get updated storage connection: %v", err)
|
|
}
|
|
if updated == nil {
|
|
t.Fatalf("expected updated connection")
|
|
}
|
|
if updated.Description != "更新后的连接描述" || updated.Endpoint != "https://gz.bcebos.com" || updated.Bucket != "archive" {
|
|
t.Fatalf("unexpected updated connection: %#v", updated)
|
|
}
|
|
if updated.AccessKeyID != "ak-connection-1234" || updated.SecretAccessKey != "sk-connection-5678" {
|
|
t.Fatalf("expected blank update payload to preserve secrets: %#v", updated)
|
|
}
|
|
|
|
invalidUpdateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidUpdateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/storage/connections/"+created["id"].(string), bytes.NewReader([]byte(`{"endpoint":"ftp://gz.bcebos.com"}`))),
|
|
)
|
|
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid storage endpoint update rejected, got %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidUpdateRecorder.Body.String(), "endpoint") {
|
|
t.Fatalf("expected endpoint validation issue, body=%s", invalidUpdateRecorder.Body.String())
|
|
}
|
|
|
|
clearEndpointRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
clearEndpointRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/storage/connections/"+created["id"].(string), bytes.NewReader([]byte(`{"endpoint":""}`))),
|
|
)
|
|
if clearEndpointRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected clearing endpoint without global fallback rejected, got %d body=%s", clearEndpointRecorder.Code, clearEndpointRecorder.Body.String())
|
|
}
|
|
|
|
persisted, err := dataStore.GetStorageConnection(ctx, created["id"].(string))
|
|
if err != nil {
|
|
t.Fatalf("get storage connection after invalid updates: %v", err)
|
|
}
|
|
if persisted == nil || persisted.Endpoint != "https://gz.bcebos.com" {
|
|
t.Fatalf("expected invalid updates not to persist endpoint changes: %#v", persisted)
|
|
}
|
|
|
|
listRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/storage/connections", nil))
|
|
if listRecorder.Code != http.StatusOK {
|
|
t.Fatalf("list storage connections status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
|
}
|
|
var listBody struct {
|
|
Items []map[string]any `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(listRecorder.Body.Bytes(), &listBody); err != nil {
|
|
t.Fatalf("decode storage connection list: %v", err)
|
|
}
|
|
if len(listBody.Items) != 1 {
|
|
t.Fatalf("expected 1 storage connection, got %d", len(listBody.Items))
|
|
}
|
|
}
|
|
|
|
func TestMongoConnectionsAPIStoresSecretsMasked(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
invalidCreateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidCreateRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/mongo/connections", bytes.NewReader([]byte(`{
|
|
"name":"占位符 MongoDB",
|
|
"uri":"mongodb://<user>:<password>@business.example:27017",
|
|
"database":"business_data"
|
|
}`))),
|
|
)
|
|
if invalidCreateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected placeholder mongo URI rejected, got %d body=%s", invalidCreateRecorder.Code, invalidCreateRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidCreateRecorder.Body.String(), "uri") {
|
|
t.Fatalf("expected uri validation issue, body=%s", invalidCreateRecorder.Body.String())
|
|
}
|
|
|
|
createPayload := []byte(`{
|
|
"name":"业务 MongoDB",
|
|
"description":"测试业务库",
|
|
"uri":"mongodb://business-user:business-pass@business.example:27017",
|
|
"database":"business_data"
|
|
}`)
|
|
createRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/mongo/connections", bytes.NewReader(createPayload)),
|
|
)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("create mongo connection status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var created map[string]any
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created mongo connection: %v", err)
|
|
}
|
|
if _, ok := created["uri"]; ok {
|
|
t.Fatalf("expected masked uri to be omitted: %#v", created)
|
|
}
|
|
if created["hasUri"] != true || created["uriPreview"] == "" {
|
|
t.Fatalf("expected uri presence metadata: %#v", created)
|
|
}
|
|
|
|
stored, err := dataStore.GetMongoConnection(ctx, created["id"].(string))
|
|
if err != nil {
|
|
t.Fatalf("get stored mongo connection: %v", err)
|
|
}
|
|
if stored == nil {
|
|
t.Fatalf("expected stored mongo connection")
|
|
}
|
|
if stored.URI != "mongodb://business-user:business-pass@business.example:27017" {
|
|
t.Fatalf("expected real uri preserved in store: %#v", stored)
|
|
}
|
|
|
|
updatePayload := []byte(`{
|
|
"description":"更新后的测试业务库",
|
|
"uri":"",
|
|
"database":"business_data_v2"
|
|
}`)
|
|
updateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
updateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/mongo/connections/"+created["id"].(string), bytes.NewReader(updatePayload)),
|
|
)
|
|
if updateRecorder.Code != http.StatusOK {
|
|
t.Fatalf("update mongo connection status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
|
|
updated, err := dataStore.GetMongoConnection(ctx, created["id"].(string))
|
|
if err != nil {
|
|
t.Fatalf("get updated mongo connection: %v", err)
|
|
}
|
|
if updated == nil {
|
|
t.Fatalf("expected updated mongo connection")
|
|
}
|
|
if updated.Description != "更新后的测试业务库" || updated.Database != "business_data_v2" {
|
|
t.Fatalf("unexpected updated mongo connection: %#v", updated)
|
|
}
|
|
if updated.URI != "mongodb://business-user:business-pass@business.example:27017" {
|
|
t.Fatalf("expected blank update payload to preserve uri: %#v", updated)
|
|
}
|
|
|
|
invalidUpdateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
invalidUpdateRecorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/mongo/connections/"+created["id"].(string), bytes.NewReader([]byte(`{"uri":"http://business.example:27017"}`))),
|
|
)
|
|
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid mongo URI update rejected, got %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(invalidUpdateRecorder.Body.String(), "uri") {
|
|
t.Fatalf("expected uri validation issue, body=%s", invalidUpdateRecorder.Body.String())
|
|
}
|
|
|
|
persisted, err := dataStore.GetMongoConnection(ctx, created["id"].(string))
|
|
if err != nil {
|
|
t.Fatalf("get mongo connection after invalid update: %v", err)
|
|
}
|
|
if persisted == nil || persisted.URI != "mongodb://business-user:business-pass@business.example:27017" || persisted.Database != "business_data_v2" {
|
|
t.Fatalf("expected invalid update not to persist mongo changes: %#v", persisted)
|
|
}
|
|
|
|
listRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(listRecorder, httptest.NewRequest(http.MethodGet, "/api/mongo/connections", nil))
|
|
if listRecorder.Code != http.StatusOK {
|
|
t.Fatalf("list mongo connections status: %d body=%s", listRecorder.Code, listRecorder.Body.String())
|
|
}
|
|
var listBody struct {
|
|
Items []map[string]any `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(listRecorder.Body.Bytes(), &listBody); err != nil {
|
|
t.Fatalf("decode mongo connection list: %v", err)
|
|
}
|
|
if len(listBody.Items) != 1 {
|
|
t.Fatalf("expected 1 mongo connection, got %d", len(listBody.Items))
|
|
}
|
|
}
|
|
|
|
func TestStorageBrowserSupportsConnectionID(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 主连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
Endpoint: "https://bos.example.test",
|
|
AccessKeyID: "ak-browser-1234",
|
|
SecretAccessKey: "sk-browser-5678",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
|
|
originalBucketsResolver := storageConnectionBucketsResolver
|
|
originalObjectsResolver := storageConnectionObjectsResolver
|
|
storageConnectionBucketsResolver = func(ctx context.Context, item *domain.StorageConnectionRecord) ([]string, error) {
|
|
if item == nil || item.ID != connection.ID {
|
|
t.Fatalf("unexpected storage connection for buckets: %#v", item)
|
|
}
|
|
return []string{"datastorage", "incoming"}, nil
|
|
}
|
|
storageConnectionObjectsResolver = func(ctx context.Context, item *domain.StorageConnectionRecord, bucket string, prefix string) ([]string, error) {
|
|
if item == nil || item.ID != connection.ID {
|
|
t.Fatalf("unexpected storage connection for objects: %#v", item)
|
|
}
|
|
if bucket != "datastorage" || prefix != "algo/X-platform-test" {
|
|
t.Fatalf("unexpected bucket/prefix: %s %s", bucket, prefix)
|
|
}
|
|
return []string{"algo/X-platform-test/BJ_001/", "algo/X-platform-test/meta.json"}, nil
|
|
}
|
|
t.Cleanup(func() {
|
|
storageConnectionBucketsResolver = originalBucketsResolver
|
|
storageConnectionObjectsResolver = originalObjectsResolver
|
|
})
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
bucketsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
bucketsRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/storage/buckets?connectionId="+connection.ID, nil),
|
|
)
|
|
if bucketsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("connection buckets status: %d body=%s", bucketsRecorder.Code, bucketsRecorder.Body.String())
|
|
}
|
|
|
|
objectsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
objectsRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/storage/objects?connectionId="+connection.ID+"&bucket=datastorage&prefix=algo/X-platform-test", nil),
|
|
)
|
|
if objectsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("connection objects status: %d body=%s", objectsRecorder.Code, objectsRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
type fakeObjectStorageBrowser struct {
|
|
buckets map[string]struct{}
|
|
objects map[string]domain.ObjectStorageObjectSummary
|
|
content map[string][]byte
|
|
readErr map[string]error
|
|
readCalls map[string]int
|
|
readPreviewCalls map[string]int
|
|
lastPreviewMaxLen map[string]int64
|
|
}
|
|
|
|
func newFakeObjectStorageBrowser() *fakeObjectStorageBrowser {
|
|
return &fakeObjectStorageBrowser{
|
|
buckets: map[string]struct{}{
|
|
"datastorage": {},
|
|
},
|
|
objects: map[string]domain.ObjectStorageObjectSummary{
|
|
"datastorage|algo/demo.json": {
|
|
Bucket: "datastorage",
|
|
Key: "algo/demo.json",
|
|
Name: "demo.json",
|
|
Kind: "json",
|
|
SizeBytes: 13,
|
|
ContentType: "application/json",
|
|
},
|
|
},
|
|
content: map[string][]byte{
|
|
"datastorage|algo/demo.json": []byte(`{"demo":true}`),
|
|
},
|
|
readErr: map[string]error{},
|
|
readCalls: map[string]int{},
|
|
readPreviewCalls: map[string]int{},
|
|
lastPreviewMaxLen: map[string]int64{},
|
|
}
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) ListBuckets(context.Context) ([]domain.ObjectStorageBucketSummary, error) {
|
|
items := make([]domain.ObjectStorageBucketSummary, 0, len(browser.buckets))
|
|
for bucket := range browser.buckets {
|
|
items = append(items, domain.ObjectStorageBucketSummary{Name: bucket})
|
|
}
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].Name < items[right].Name })
|
|
return items, nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) ListObjects(_ context.Context, bucket string, prefix string) ([]domain.ObjectStorageObjectSummary, error) {
|
|
items := make([]domain.ObjectStorageObjectSummary, 0)
|
|
normalizedPrefix := strings.Trim(strings.TrimSpace(prefix), "/")
|
|
for _, item := range browser.objects {
|
|
if item.Bucket != bucket {
|
|
continue
|
|
}
|
|
if normalizedPrefix != "" && !strings.HasPrefix(item.Key, normalizedPrefix) {
|
|
continue
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
sort.Slice(items, func(left int, right int) bool { return items[left].Key < items[right].Key })
|
|
return items, nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) CreateBucket(_ context.Context, bucket string) (domain.ObjectStorageBucketSummary, error) {
|
|
browser.buckets[bucket] = struct{}{}
|
|
return domain.ObjectStorageBucketSummary{Name: bucket}, nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) DeleteBucket(_ context.Context, bucket string) error {
|
|
delete(browser.buckets, bucket)
|
|
return nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) PutObject(_ context.Context, bucket string, key string, payload []byte, contentType string) (domain.ObjectStorageObjectSummary, error) {
|
|
browser.buckets[bucket] = struct{}{}
|
|
item := domain.ObjectStorageObjectSummary{
|
|
Bucket: bucket,
|
|
Key: key,
|
|
Name: filepath.Base(key),
|
|
Kind: "file",
|
|
SizeBytes: int64(len(payload)),
|
|
ContentType: contentType,
|
|
}
|
|
if strings.HasSuffix(strings.ToLower(key), ".json") {
|
|
item.Kind = "json"
|
|
}
|
|
browser.objects[bucket+"|"+key] = item
|
|
browser.content[bucket+"|"+key] = append([]byte(nil), payload...)
|
|
return item, nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) PutObjectStream(ctx context.Context, bucket string, key string, reader io.Reader, contentType string) (domain.ObjectStorageObjectSummary, error) {
|
|
payload, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
return domain.ObjectStorageObjectSummary{}, err
|
|
}
|
|
return browser.PutObject(ctx, bucket, key, payload, contentType)
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) RenameObject(_ context.Context, bucket string, key string, newKey string) (domain.ObjectStorageObjectSummary, error) {
|
|
item := browser.objects[bucket+"|"+key]
|
|
delete(browser.objects, bucket+"|"+key)
|
|
item.Key = newKey
|
|
item.Name = filepath.Base(newKey)
|
|
browser.objects[bucket+"|"+newKey] = item
|
|
browser.content[bucket+"|"+newKey] = browser.content[bucket+"|"+key]
|
|
delete(browser.content, bucket+"|"+key)
|
|
return item, nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) DeleteObject(_ context.Context, bucket string, key string) error {
|
|
delete(browser.objects, bucket+"|"+key)
|
|
delete(browser.content, bucket+"|"+key)
|
|
return nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) DeletePrefix(_ context.Context, bucket string, prefix string) error {
|
|
normalized := strings.Trim(strings.TrimSpace(prefix), "/")
|
|
for objectKey := range browser.objects {
|
|
if !strings.HasPrefix(objectKey, bucket+"|") {
|
|
continue
|
|
}
|
|
key := strings.TrimPrefix(objectKey, bucket+"|")
|
|
if key == normalized || strings.HasPrefix(key, normalized+"/") {
|
|
delete(browser.objects, objectKey)
|
|
delete(browser.content, objectKey)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) GetObjectSummary(_ context.Context, bucket string, key string) (domain.ObjectStorageObjectSummary, error) {
|
|
item, ok := browser.objects[bucket+"|"+key]
|
|
if !ok {
|
|
return domain.ObjectStorageObjectSummary{}, os.ErrNotExist
|
|
}
|
|
return item, nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) ReadObject(_ context.Context, bucket string, key string) ([]byte, string, error) {
|
|
browser.readCalls[bucket+"|"+key]++
|
|
if err, ok := browser.readErr[bucket+"|"+key]; ok && err != nil {
|
|
return nil, "", err
|
|
}
|
|
item, ok := browser.objects[bucket+"|"+key]
|
|
if !ok {
|
|
return nil, "", os.ErrNotExist
|
|
}
|
|
payload, ok := browser.content[bucket+"|"+key]
|
|
if !ok {
|
|
return nil, "", os.ErrNotExist
|
|
}
|
|
return append([]byte(nil), payload...), item.ContentType, nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) ReadObjectPreview(_ context.Context, bucket string, key string, maxBytes int64) ([]byte, string, error) {
|
|
browser.readPreviewCalls[bucket+"|"+key]++
|
|
browser.lastPreviewMaxLen[bucket+"|"+key] = maxBytes
|
|
if err, ok := browser.readErr[bucket+"|"+key]; ok && err != nil {
|
|
return nil, "", err
|
|
}
|
|
item, ok := browser.objects[bucket+"|"+key]
|
|
if !ok {
|
|
return nil, "", os.ErrNotExist
|
|
}
|
|
payload, ok := browser.content[bucket+"|"+key]
|
|
if !ok {
|
|
return nil, "", os.ErrNotExist
|
|
}
|
|
if maxBytes > 0 && int64(len(payload)) > maxBytes {
|
|
payload = payload[:maxBytes]
|
|
}
|
|
return append([]byte(nil), payload...), item.ContentType, nil
|
|
}
|
|
|
|
func (browser *fakeObjectStorageBrowser) BuildPreviewURL(_ context.Context, bucket string, key string) (string, error) {
|
|
if _, ok := browser.objects[bucket+"|"+key]; !ok {
|
|
return "", os.ErrNotExist
|
|
}
|
|
return "https://example.com/preview/" + url.PathEscape(bucket) + "/" + url.PathEscape(key), nil
|
|
}
|
|
|
|
func TestObjectStorageBrowserSupportsConnectionCRUDAndPreview(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 管理连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
Endpoint: "https://bos.example.test",
|
|
Bucket: "datastorage",
|
|
AccessKeyID: "ak-storage-browser",
|
|
SecretAccessKey: "sk-storage-browser",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
|
|
fakeBrowser := newFakeObjectStorageBrowser()
|
|
originalResolver := storageConnectionBrowserResolver
|
|
storageConnectionBrowserResolver = func(_ context.Context, item *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
if item == nil || item.ID != connection.ID {
|
|
t.Fatalf("unexpected storage connection: %#v", item)
|
|
}
|
|
return fakeBrowser, func() {}, nil
|
|
}
|
|
t.Cleanup(func() {
|
|
storageConnectionBrowserResolver = originalResolver
|
|
})
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
overviewRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
overviewRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/object-storage?connectionId="+connection.ID, nil),
|
|
)
|
|
if overviewRecorder.Code != http.StatusOK {
|
|
t.Fatalf("object storage overview status: %d body=%s", overviewRecorder.Code, overviewRecorder.Body.String())
|
|
}
|
|
|
|
createObjectRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createObjectRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/object-storage/objects?connectionId="+connection.ID, strings.NewReader(`{"key":"algo/created.json","content":"{\"ok\":true}","contentType":"application/json"}`)),
|
|
)
|
|
if createObjectRecorder.Code != http.StatusOK {
|
|
t.Fatalf("create object status: %d body=%s", createObjectRecorder.Code, createObjectRecorder.Body.String())
|
|
}
|
|
|
|
renameRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
renameRecorder,
|
|
httptest.NewRequest(http.MethodPatch, "/api/object-storage/objects?connectionId="+connection.ID, strings.NewReader(`{"key":"algo/created.json","newKey":"algo/renamed.json"}`)),
|
|
)
|
|
if renameRecorder.Code != http.StatusOK {
|
|
t.Fatalf("rename object status: %d body=%s", renameRecorder.Code, renameRecorder.Body.String())
|
|
}
|
|
|
|
objectsRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
objectsRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/object-storage/objects?connectionId="+connection.ID+"&prefix=algo", nil),
|
|
)
|
|
if objectsRecorder.Code != http.StatusOK {
|
|
t.Fatalf("list objects status: %d body=%s", objectsRecorder.Code, objectsRecorder.Body.String())
|
|
}
|
|
|
|
contentRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
contentRecorder,
|
|
httptest.NewRequest(http.MethodGet, "/api/object-storage/content?connectionId="+connection.ID+"&key=algo/renamed.json", nil),
|
|
)
|
|
if contentRecorder.Code != http.StatusOK {
|
|
t.Fatalf("object content status: %d body=%s", contentRecorder.Code, contentRecorder.Body.String())
|
|
}
|
|
if got := strings.TrimSpace(contentRecorder.Header().Get("Content-Type")); !strings.HasPrefix(got, "application/json") {
|
|
t.Fatalf("unexpected content type: %s", got)
|
|
}
|
|
|
|
var multipartBody bytes.Buffer
|
|
multipartWriter := multipart.NewWriter(&multipartBody)
|
|
fileWriter, err := multipartWriter.CreateFormFile("file", "hello.txt")
|
|
if err != nil {
|
|
t.Fatalf("create multipart file: %v", err)
|
|
}
|
|
if _, err := io.WriteString(fileWriter, "hello object storage"); err != nil {
|
|
t.Fatalf("write multipart file: %v", err)
|
|
}
|
|
if err := multipartWriter.Close(); err != nil {
|
|
t.Fatalf("close multipart writer: %v", err)
|
|
}
|
|
uploadRequest := httptest.NewRequest(http.MethodPost, "/api/object-storage/objects/upload?connectionId="+connection.ID+"&key=algo/nested/uploaded.txt", &multipartBody)
|
|
uploadRequest.Header.Set("Content-Type", multipartWriter.FormDataContentType())
|
|
uploadRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(uploadRecorder, uploadRequest)
|
|
if uploadRecorder.Code != http.StatusOK {
|
|
t.Fatalf("upload object status: %d body=%s", uploadRecorder.Code, uploadRecorder.Body.String())
|
|
}
|
|
|
|
deleteObjectRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
deleteObjectRecorder,
|
|
httptest.NewRequest(http.MethodDelete, "/api/object-storage/objects?connectionId="+connection.ID+"&key=algo/renamed.json", nil),
|
|
)
|
|
if deleteObjectRecorder.Code != http.StatusOK {
|
|
t.Fatalf("delete object status: %d body=%s", deleteObjectRecorder.Code, deleteObjectRecorder.Body.String())
|
|
}
|
|
|
|
deleteDirectoryRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
deleteDirectoryRecorder,
|
|
httptest.NewRequest(http.MethodDelete, "/api/object-storage/objects?connectionId="+connection.ID+"&key=algo/nested&recursive=true", nil),
|
|
)
|
|
if deleteDirectoryRecorder.Code != http.StatusOK {
|
|
t.Fatalf("delete directory status: %d body=%s", deleteDirectoryRecorder.Code, deleteDirectoryRecorder.Body.String())
|
|
}
|
|
if _, ok := fakeBrowser.objects["datastorage|algo/nested/uploaded.txt"]; ok {
|
|
t.Fatalf("expected recursive directory delete to remove nested object")
|
|
}
|
|
}
|
|
|
|
func TestObjectStorageContentStreamsVideoPreviewWithoutReadingWholeObject(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 管理连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
Endpoint: "https://bos.example.test",
|
|
Bucket: "datastorage",
|
|
AccessKeyID: "ak-storage-browser",
|
|
SecretAccessKey: "sk-storage-browser",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
|
|
fakeBrowser := newFakeObjectStorageBrowser()
|
|
videoKey := "algo/demo.mp4"
|
|
fakeBrowser.objects["datastorage|"+videoKey] = domain.ObjectStorageObjectSummary{
|
|
Bucket: "datastorage",
|
|
Key: videoKey,
|
|
Name: "demo.mp4",
|
|
Kind: "video",
|
|
SizeBytes: 7 << 30,
|
|
ContentType: "video/mp4",
|
|
}
|
|
fakeBrowser.readErr["datastorage|"+videoKey] = fmt.Errorf("read should not be called for redirected video preview")
|
|
|
|
originalResolver := storageConnectionBrowserResolver
|
|
storageConnectionBrowserResolver = func(ctx context.Context, item *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
if item == nil || item.ID != connection.ID {
|
|
t.Fatalf("unexpected storage connection: %#v", item)
|
|
}
|
|
return fakeBrowser, func() {}, nil
|
|
}
|
|
defer func() { storageConnectionBrowserResolver = originalResolver }()
|
|
|
|
previewFile, err := os.CreateTemp(t.TempDir(), "object-storage-preview-*.mp4")
|
|
if err != nil {
|
|
t.Fatalf("create preview file: %v", err)
|
|
}
|
|
if _, err := previewFile.Write([]byte("preview-video")); err != nil {
|
|
t.Fatalf("write preview file: %v", err)
|
|
}
|
|
if err := previewFile.Close(); err != nil {
|
|
t.Fatalf("close preview file: %v", err)
|
|
}
|
|
|
|
originalPreviewResolver := sourceBrowserVideoPreviewPath
|
|
sourceBrowserVideoPreviewPath = func(ctx context.Context, request sourceBrowserPreviewRequest) (string, error) {
|
|
if request.RedirectURL == "" {
|
|
t.Fatalf("expected redirect url for video preview request")
|
|
}
|
|
if len(request.Payload) != 0 {
|
|
t.Fatalf("expected video preview to avoid in-memory payload reads")
|
|
}
|
|
return previewFile.Name(), nil
|
|
}
|
|
defer func() { sourceBrowserVideoPreviewPath = originalPreviewResolver }()
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(
|
|
http.MethodGet,
|
|
"/api/object-storage/content?connectionId="+url.QueryEscape(connection.ID)+"&bucket=datastorage&key="+url.QueryEscape(videoKey)+"&transcode=browser",
|
|
nil,
|
|
)
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("video preview status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if contentType := recorder.Header().Get("Content-Type"); contentType != "video/mp4" {
|
|
t.Fatalf("unexpected content type: %s", contentType)
|
|
}
|
|
if body := recorder.Body.String(); body != "preview-video" {
|
|
t.Fatalf("unexpected preview body: %q", body)
|
|
}
|
|
}
|
|
|
|
func TestObjectStorageContentPreviewTruncatesLargeJSONObjects(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 管理连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
Endpoint: "https://bos.example.test",
|
|
Bucket: "datastorage",
|
|
AccessKeyID: "ak-storage-browser",
|
|
SecretAccessKey: "sk-storage-browser",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
|
|
fakeBrowser := newFakeObjectStorageBrowser()
|
|
largeKey := "algo/large.json"
|
|
largePayload := []byte(strings.Repeat(`{"imu":1}`+"\n", 40000))
|
|
fakeBrowser.objects["datastorage|"+largeKey] = domain.ObjectStorageObjectSummary{
|
|
Bucket: "datastorage",
|
|
Key: largeKey,
|
|
Name: "large.json",
|
|
Kind: "json",
|
|
SizeBytes: int64(len(largePayload)),
|
|
ContentType: "application/json",
|
|
}
|
|
fakeBrowser.content["datastorage|"+largeKey] = largePayload
|
|
|
|
originalResolver := storageConnectionBrowserResolver
|
|
storageConnectionBrowserResolver = func(_ context.Context, item *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
if item == nil || item.ID != connection.ID {
|
|
t.Fatalf("unexpected storage connection: %#v", item)
|
|
}
|
|
return fakeBrowser, func() {}, nil
|
|
}
|
|
defer func() { storageConnectionBrowserResolver = originalResolver }()
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(
|
|
http.MethodGet,
|
|
"/api/object-storage/content?connectionId="+url.QueryEscape(connection.ID)+"&bucket=datastorage&key="+url.QueryEscape(largeKey)+"&preview=true",
|
|
nil,
|
|
)
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("content preview status: %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if fakeBrowser.readCalls["datastorage|"+largeKey] != 0 {
|
|
t.Fatalf("expected no full-object reads, got %d", fakeBrowser.readCalls["datastorage|"+largeKey])
|
|
}
|
|
if fakeBrowser.readPreviewCalls["datastorage|"+largeKey] != 1 {
|
|
t.Fatalf("expected one preview read, got %d", fakeBrowser.readPreviewCalls["datastorage|"+largeKey])
|
|
}
|
|
if got := fakeBrowser.lastPreviewMaxLen["datastorage|"+largeKey]; got != objectStoragePartialPreviewBytes {
|
|
t.Fatalf("preview max bytes = %d, want %d", got, objectStoragePartialPreviewBytes)
|
|
}
|
|
if recorder.Header().Get(objectStoragePreviewTruncatedHeader) != "true" {
|
|
t.Fatalf("expected truncated header true, got %q", recorder.Header().Get(objectStoragePreviewTruncatedHeader))
|
|
}
|
|
if got := recorder.Header().Get(objectStoragePreviewOriginalSizeHeader); got != fmt.Sprintf("%d", len(largePayload)) {
|
|
t.Fatalf("unexpected original size header: %q", got)
|
|
}
|
|
if recorder.Body.Len() != int(objectStoragePartialPreviewBytes) {
|
|
t.Fatalf("preview body length = %d, want %d", recorder.Body.Len(), objectStoragePartialPreviewBytes)
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowAPICreatesConnectionBackedScanWorkflow(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
for _, name := range []string{"BOS 增量扫描", "X 平台数据集索引", "采集目录校验"} {
|
|
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
|
Name: name,
|
|
Type: "builtin",
|
|
Category: "IO",
|
|
Description: name,
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "output", Type: domain.PortTypeJSON}},
|
|
}); err != nil {
|
|
t.Fatalf("create operator %s: %v", name, err)
|
|
}
|
|
}
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "连接型扫描项目",
|
|
Description: "scan with storage connection",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
Name: "BOS 主连接",
|
|
Provider: domain.StorageProviderBOS,
|
|
Endpoint: "https://bos.example.test",
|
|
AccessKeyID: "ak-conn-1234",
|
|
SecretAccessKey: "sk-conn-5678",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
createPayload := []byte(`{
|
|
"name":"连接型 BOS 扫描",
|
|
"description":"使用 connectionId 的扫描 workflow",
|
|
"type":"batch",
|
|
"kind":"scan",
|
|
"schedule":{"interval":600},
|
|
"scanConfig":{
|
|
"dataSource":"BOS",
|
|
"connectionId":"` + connection.ID + `",
|
|
"bucket":"datastorage",
|
|
"prefix":"algo/X-platform-test/",
|
|
"scanMode":"INCREMENTAL",
|
|
"incrementalUnit":"DIRECTORY",
|
|
"directoryDepth":3,
|
|
"enabled":true
|
|
},
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`)
|
|
createRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
|
)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("create connection-backed scan workflow status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var created domain.WorkflowRecord
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode created workflow: %v", err)
|
|
}
|
|
if created.ScanConfig == nil || created.ScanConfig.ConnectionID != connection.ID {
|
|
t.Fatalf("expected connection-backed scan config, got %+v", created.ScanConfig)
|
|
}
|
|
if created.ScanConfig.IncrementalUnit != "DIRECTORY" || created.ScanConfig.DirectoryDepth != 3 {
|
|
t.Fatalf("expected connection-backed directory incremental config, got %+v", created.ScanConfig)
|
|
}
|
|
if created.ScanConfig.AccessKeyID != "" || created.ScanConfig.SecretAccessKey != "" {
|
|
t.Fatalf("expected workflow response not to expose secrets: %+v", created.ScanConfig)
|
|
}
|
|
if created.Type != domain.WorkflowTypeStreaming {
|
|
t.Fatalf("expected enabled connection-backed scan workflow to be streaming, got %s", created.Type)
|
|
}
|
|
|
|
stored, err := dataStore.GetWorkflow(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("get stored workflow: %v", err)
|
|
}
|
|
if stored == nil || stored.ScanConfig == nil {
|
|
t.Fatalf("expected stored scan config")
|
|
}
|
|
if stored.ScanConfig.ConnectionID != connection.ID {
|
|
t.Fatalf("expected stored connection id, got %+v", stored.ScanConfig)
|
|
}
|
|
if stored.ScanConfig.IncrementalUnit != "DIRECTORY" || stored.ScanConfig.DirectoryDepth != 3 {
|
|
t.Fatalf("expected stored connection-backed directory incremental config, got %+v", stored.ScanConfig)
|
|
}
|
|
if stored.ScanConfig.AccessKeyID != "" || stored.ScanConfig.SecretAccessKey != "" {
|
|
t.Fatalf("expected workflow store to keep connection ref instead of duplicating secrets: %+v", stored.ScanConfig)
|
|
}
|
|
|
|
runRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
runRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/workflows/"+created.ID+"/run", bytes.NewReader([]byte(`{"trigger":"manual"}`))),
|
|
)
|
|
if runRecorder.Code != http.StatusAccepted {
|
|
t.Fatalf("run connection-backed scan workflow status: %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowAPIRejectsUnknownNodeStorageConnection(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "连接校验项目",
|
|
Description: "reject invalid node connection",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
createPayload := []byte(`{
|
|
"name":"无效连接工作流",
|
|
"description":"节点 connectionId 应该校验",
|
|
"type":"batch",
|
|
"nodes":[
|
|
{
|
|
"id":"node-oss",
|
|
"type":"operator",
|
|
"data":{
|
|
"label":"OSS 数据源",
|
|
"category":"IO",
|
|
"description":"source",
|
|
"connectionId":"connection-missing",
|
|
"inputs":[],
|
|
"outputs":[],
|
|
"hyperparams":[]
|
|
},
|
|
"position":{"x":0,"y":0}
|
|
}
|
|
],
|
|
"edges":[]
|
|
}`)
|
|
createRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
|
)
|
|
if createRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected validation failure, got %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(createRecorder.Body.String(), "connectionId") {
|
|
t.Fatalf("expected connectionId issue, body=%s", createRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func duplicateInputEdgeWorkflowNodes() []domain.WorkflowNode {
|
|
return []domain.WorkflowNode{
|
|
{
|
|
ID: "source-a",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "上游 A",
|
|
Category: "IO",
|
|
Description: "source a",
|
|
Inputs: []domain.PortDefinition{},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
|
},
|
|
Position: domain.Position{X: 0, Y: 0},
|
|
},
|
|
{
|
|
ID: "source-b",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "上游 B",
|
|
Category: "IO",
|
|
Description: "source b",
|
|
Inputs: []domain.PortDefinition{},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
|
},
|
|
Position: domain.Position{X: 120, Y: 0},
|
|
},
|
|
{
|
|
ID: "upload",
|
|
Type: "operator",
|
|
Data: domain.WorkflowNodeData{
|
|
Label: "结果上传",
|
|
Category: "IO",
|
|
Description: "upload",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "result", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "uploaded", Type: domain.PortTypeJSON}},
|
|
},
|
|
Position: domain.Position{X: 240, Y: 0},
|
|
},
|
|
}
|
|
}
|
|
|
|
func duplicateInputEdgeWorkflowEdges() []domain.WorkflowEdge {
|
|
return []domain.WorkflowEdge{
|
|
{ID: "edge-a", Source: "source-a", SourceHandle: "out-1", Target: "upload", TargetHandle: "in-1"},
|
|
{ID: "edge-b", Source: "source-b", SourceHandle: "out-1", Target: "upload", TargetHandle: "in-1"},
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowAPIRejectsDuplicateIncomingEdgesPerInputPort(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "重复输入端口项目",
|
|
Description: "reject duplicate input edges",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
payload, err := json.Marshal(domain.CreateWorkflowInput{
|
|
Name: "非法上传工作流",
|
|
Description: "duplicate input edges",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: duplicateInputEdgeWorkflowNodes(),
|
|
Edges: duplicateInputEdgeWorkflowEdges(),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal payload: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
NewHandler(dataStore, nil).ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(payload)),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "每个输入端口只能连接一条上游连线") {
|
|
t.Fatalf("expected duplicate input edge validation, body=%s", recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "上传算子") {
|
|
t.Fatalf("expected upload node guidance, body=%s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestWorkflowUpdateRejectsDuplicateIncomingEdgesPerInputPort(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "更新重复输入端口项目",
|
|
Description: "reject duplicate input edges on update",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "合法工作流",
|
|
Description: "valid before update",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: duplicateInputEdgeWorkflowNodes(),
|
|
Edges: []domain.WorkflowEdge{
|
|
{ID: "edge-a", Source: "source-a", SourceHandle: "out-1", Target: "upload", TargetHandle: "in-1"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
payload, err := json.Marshal(map[string]any{
|
|
"nodes": duplicateInputEdgeWorkflowNodes(),
|
|
"edges": duplicateInputEdgeWorkflowEdges(),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal payload: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
NewHandler(dataStore, nil).ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPut, "/api/workflows/"+workflow.ID, bytes.NewReader(payload)),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected update validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "每个输入端口只能连接一条上游连线") {
|
|
t.Fatalf("expected duplicate input edge validation, body=%s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestWorkflowRunRejectsDuplicateIncomingEdgesPerInputPort(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "运行重复输入端口项目",
|
|
Description: "reject duplicate input edges on run",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
ProjectID: project.ID,
|
|
Name: "非法运行工作流",
|
|
Description: "invalid graph",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: duplicateInputEdgeWorkflowNodes(),
|
|
Edges: duplicateInputEdgeWorkflowEdges(),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
NewHandler(dataStore, nil).ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/run", bytes.NewReader([]byte(`{"trigger":"manual"}`))),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected run validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "每个输入端口只能连接一条上游连线") {
|
|
t.Fatalf("expected duplicate input edge validation, body=%s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestTemplateCreateRejectsDuplicateIncomingEdgesPerInputPort(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
|
|
payload, err := json.Marshal(domain.CreateTemplateInput{
|
|
Name: "非法模板",
|
|
Description: "duplicate input edges",
|
|
Type: domain.WorkflowTypeBatch,
|
|
Nodes: duplicateInputEdgeWorkflowNodes(),
|
|
Edges: duplicateInputEdgeWorkflowEdges(),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal payload: %v", err)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
NewHandler(dataStore, nil).ServeHTTP(
|
|
recorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(payload)),
|
|
)
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected template validation failure, got %d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if !strings.Contains(recorder.Body.String(), "每个输入端口只能连接一条上游连线") {
|
|
t.Fatalf("expected duplicate input edge validation, body=%s", recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestProjectWorkflowAPICreatesScanDraftAndBlocksRunUntilConfigured(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
for _, name := range []string{"BOS 增量扫描", "X 平台数据集索引", "采集目录校验"} {
|
|
if _, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
|
Name: name,
|
|
Type: "builtin",
|
|
Category: "IO",
|
|
Description: name,
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "output", Type: domain.PortTypeJSON}},
|
|
}); err != nil {
|
|
t.Fatalf("create operator %s: %v", name, err)
|
|
}
|
|
}
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
|
Name: "扫描草稿项目",
|
|
Description: "scan draft",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
createPayload := []byte(`{
|
|
"name":"扫描草稿",
|
|
"description":"先创建后配置",
|
|
"type":"batch",
|
|
"kind":"scan",
|
|
"schedule":{"interval":0},
|
|
"scanConfig":{
|
|
"dataSource":"BOS",
|
|
"endpoint":"https://bos.example.test",
|
|
"scanMode":"INCREMENTAL",
|
|
"enabled":false
|
|
},
|
|
"nodes":[],
|
|
"edges":[]
|
|
}`)
|
|
createRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
createRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload)),
|
|
)
|
|
if createRecorder.Code != http.StatusCreated {
|
|
t.Fatalf("create scan draft status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
|
}
|
|
|
|
var created domain.WorkflowRecord
|
|
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("decode scan draft: %v", err)
|
|
}
|
|
if created.Kind != domain.WorkflowKindScan {
|
|
t.Fatalf("expected scan kind, got %s", created.Kind)
|
|
}
|
|
if created.ScanConfig == nil {
|
|
t.Fatalf("expected scan config")
|
|
}
|
|
if created.ScanConfig.Bucket != "" || created.ScanConfig.Prefix != "" {
|
|
t.Fatalf("expected incomplete draft scan target, got %+v", created.ScanConfig)
|
|
}
|
|
if created.Type != domain.WorkflowTypeBatch {
|
|
t.Fatalf("expected draft scan workflow to stay batch, got %s", created.Type)
|
|
}
|
|
if len(created.Nodes) != 3 {
|
|
t.Fatalf("expected scaffolded scan nodes, got %d", len(created.Nodes))
|
|
}
|
|
|
|
runRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(
|
|
runRecorder,
|
|
httptest.NewRequest(http.MethodPost, "/api/workflows/"+created.ID+"/run", bytes.NewReader([]byte(`{"trigger":"manual"}`))),
|
|
)
|
|
if runRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected scan draft run to be rejected, got %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
|
}
|
|
if !strings.Contains(runRecorder.Body.String(), "扫描源配置未完成") {
|
|
t.Fatalf("expected missing scan config error, got %s", runRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSettingsRejectsInvalidTimezone(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
|
|
updateRecorder := httptest.NewRecorder()
|
|
updateRequest := httptest.NewRequest(
|
|
http.MethodPut,
|
|
"/api/settings",
|
|
bytes.NewReader([]byte(`{"timezone":"Mars/Base"}`)),
|
|
)
|
|
handler.ServeHTTP(updateRecorder, updateRequest)
|
|
if updateRecorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected invalid timezone to fail, got %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
|
}
|
|
}
|