droboticflow/internal/api/delivery_package_contents_test.go
2026-08-22 00:01:30 +08:00

76 lines
3.6 KiB
Go

package api
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
func TestDeliveryPackageContentsExposeOnlyFinalArtifactsToReviewer(t *testing.T) {
ctx := context.Background()
dataStore := store.NewMemoryStore()
videoPath := filepath.Join(t.TempDir(), "final-001.mp4")
if err := os.WriteFile(videoPath, []byte("final-video"), 0o600); err != nil {
t.Fatal(err)
}
manifestID, videoID := "artifact-manifest", "artifact-video"
run := domain.RunRecord{
ID: "run-final", OrganizationID: domain.DefaultOrganizationID, WorkflowID: "workflow-final", ProjectID: "project-final", Status: domain.RunStatusCompleted,
Artifacts: []domain.ExecutionArtifact{
{ID: manifestID, RunID: "run-final", NodeID: "package", Name: "packages-manifest.json", Type: domain.PortTypeJSON, Data: map[string]any{"packageCount": 1, "trainingEnabled": false, "items": []any{map[string]any{"packageName": "slice-package", "clipId": "slice", "clipName": "final-001.mp4", "startSecond": 0.0, "endSecond": 10.2, "duration": 10.2}}}},
{ID: videoID, RunID: "run-final", NodeID: "package", Name: "final-001.mp4", Type: domain.PortTypeVideo, Path: videoPath, Size: "11 B", Metadata: &domain.ArtifactMetadata{MimeType: "video/mp4"}},
{ID: "artifact-upstream", RunID: "run-parent", NodeID: "human", Name: "annotations.json", Type: domain.PortTypeJSON},
},
}
if err := dataStore.UpsertRun(ctx, run); err != nil {
t.Fatal(err)
}
item := domain.DeliveryPackageRecord{
ID: "delivery-final", OrganizationID: domain.DefaultOrganizationID, ProjectID: run.ProjectID, WorkflowID: run.WorkflowID,
ChildRunID: run.ID, Status: domain.DeliveryPackageAccepted, FinalExportArtifactIDs: []string{manifestID, videoID}, Version: 1,
}
if err := dataStore.UpsertDeliveryPackage(ctx, item); err != nil {
t.Fatal(err)
}
principal := &authenticatedPrincipal{User: domain.UserRecord{ID: "admin", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin}, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID}}
handler := NewHandler(dataStore, nil)
request := httptest.NewRequest(http.MethodGet, "/api/delivery-packages/"+item.ID+"/contents", nil)
request = request.WithContext(context.WithValue(request.Context(), authContextKey{}, principal))
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("contents status=%d body=%s", recorder.Code, recorder.Body.String())
}
var payload struct {
Artifacts []domain.ExecutionArtifact `json:"artifacts"`
Manifest map[string]any `json:"manifest"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
t.Fatal(err)
}
if len(payload.Artifacts) != 2 || payload.Manifest["packageCount"] != float64(1) {
t.Fatalf("unexpected delivery contents: %#v", payload)
}
for _, artifact := range payload.Artifacts {
if artifact.ID == "artifact-upstream" || artifact.Data["contentURL"] == "" {
t.Fatalf("contents leaked an upstream artifact or omitted content URL: %#v", artifact)
}
}
contentRequest := httptest.NewRequest(http.MethodGet, "/api/delivery-packages/"+item.ID+"/artifacts/"+videoID+"/content", nil)
contentRequest = contentRequest.WithContext(context.WithValue(contentRequest.Context(), authContextKey{}, principal))
contentRecorder := httptest.NewRecorder()
handler.ServeHTTP(contentRecorder, contentRequest)
if contentRecorder.Code != http.StatusOK || contentRecorder.Body.String() != "final-video" {
t.Fatalf("video content status=%d body=%q", contentRecorder.Code, contentRecorder.Body.String())
}
}