forked from longtao.wu/droboticflow
108 lines
4.5 KiB
Go
108 lines
4.5 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func TestBrowserAuthenticationCannotBeDisabledByRetiredEnvironment(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "false")
|
|
handler := NewHandler(store.NewMemoryStore(), nil)
|
|
for _, path := range []string{"/api/projects", "/api/users", "/api/object-storage/sync-report", "/worker-api/metrics"} {
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, path, nil))
|
|
if recorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("anonymous %s = %d", path, recorder.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRetiredCredentialTokenFieldsDoNotAuthenticate(t *testing.T) {
|
|
for _, field := range []string{"tokenHash", "tokenHashes"} {
|
|
t.Run(field, func(t *testing.T) {
|
|
handler, data := objectSyncTestHandler(t, true)
|
|
token := "retired-fixture-token"
|
|
var value any = sessionTokenHash(token)
|
|
if field == "tokenHashes" {
|
|
value = []string{sessionTokenHash(token)}
|
|
}
|
|
raw, _ := json.Marshal(map[string]any{"id": "old-credential", "organizationId": domain.DefaultOrganizationID, "enabled": true, field: value, "deviceId": "old-device"})
|
|
var credential domain.ObjectSyncCredentialRecord
|
|
if err := json.Unmarshal(raw, &credential); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := data.UpsertObjectSyncCredential(context.Background(), credential); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := httptest.NewRequest(http.MethodGet, "/api/external/object-sync/capabilities", nil)
|
|
request.Header.Set("Authorization", "Bearer "+token)
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusUnauthorized {
|
|
t.Fatalf("retired %s authenticated: %d", field, recorder.Code)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStructuredArtifactsRequireCurrentPayload(t *testing.T) {
|
|
old := domain.ExecutionArtifact{Type: domain.PortTypeJSON, Data: map[string]any{"version": "old"}}
|
|
if artifactStructuredData(&old) != nil {
|
|
t.Fatal("historical data field was interpreted as a payload")
|
|
}
|
|
old.Payload = &domain.ArtifactPayload{Mode: domain.ArtifactTransferModeInline, Type: domain.PortTypeJSON, Inline: map[string]any{"version": "current"}}
|
|
if artifactStructuredData(&old)["version"] != "current" {
|
|
t.Fatal("explicit payload did not take precedence")
|
|
}
|
|
}
|
|
|
|
type currentJSONFixtureBrowser struct {
|
|
objectStorageBrowser
|
|
content []byte
|
|
}
|
|
|
|
func (browser currentJSONFixtureBrowser) GetObjectSummary(context.Context, string, string) (domain.ObjectStorageObjectSummary, error) {
|
|
return domain.ObjectStorageObjectSummary{SizeBytes: int64(len(browser.content))}, nil
|
|
}
|
|
|
|
func (browser currentJSONFixtureBrowser) ReadObject(context.Context, string, string) ([]byte, string, error) {
|
|
return browser.content, "application/json", nil
|
|
}
|
|
|
|
func TestCurrentJSONReferencesSupportObjectsAndArrays(t *testing.T) {
|
|
ctx := context.Background()
|
|
data := store.NewMemoryStore()
|
|
connection, err := data.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{OrganizationID: domain.DefaultOrganizationID, Name: "JSON fixture", Provider: domain.StorageProviderBOS, Endpoint: "https://example.test"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
original := storageConnectionBrowserResolver
|
|
t.Cleanup(func() { storageConnectionBrowserResolver = original })
|
|
for _, content := range []string{`{"count":2}`, `[1,2]`} {
|
|
storageConnectionBrowserResolver = func(context.Context, *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
return currentJSONFixtureBrowser{content: []byte(content)}, nil, nil
|
|
}
|
|
artifact := domain.ExecutionArtifact{ID: "current-json", Type: domain.PortTypeJSON, Payload: &domain.ArtifactPayload{
|
|
Mode: domain.ArtifactTransferModeRef, Type: domain.PortTypeJSON,
|
|
Ref: &domain.ArtifactReference{Provider: domain.StorageProviderBOS, Kind: domain.ArtifactRefKindObject, ConnectionID: connection.ID, Bucket: "fixtures", Key: "data.json"},
|
|
}}
|
|
server := &server{store: data}
|
|
runs, err := server.resolveStructuredRunArtifacts(ctx, []domain.RunRecord{{OrganizationID: domain.DefaultOrganizationID, Artifacts: []domain.ExecutionArtifact{artifact, artifact}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if runs[0].Artifacts[0].Payload.Mode != domain.ArtifactTransferModeRef {
|
|
t.Fatal("view hydration rewrote the persisted reference contract")
|
|
}
|
|
}
|
|
if artifactStorageIdentity(domain.ExecutionArtifact{Path: "old/path", Name: "old"}) != "" {
|
|
t.Fatal("path/name identity fallback remains")
|
|
}
|
|
}
|