- handleAuthLogin now authenticates via store.FindUserByPhone instead of FindUserByEmail; the login/bootstrap-super-admin request payload field is now `phone`. Old email addresses are no longer valid login credentials. - bootstrap-super-admin now validates phone format (isValidChinaMobilePhone) instead of net/mail.ParseAddress; net/mail is fully removed from internal/api. - Same-class gap fix: handleAdminOrganizationUsersCreate (the legacy per-organization admin endpoint, not touched by ticket 02) still created users by email only, which would have left them unable to log in under the new phone-only login. Switched it to phone validation/uniqueness too, and removed the now-dead normalizeUserInput/validateUserEmailAvailable helpers. - Frontend: authApi.login, UserContext's login signature, and UserBootstrapPanel's submit handler now send `phone` instead of `email` (the form/labels were already phone-based from ticket 04). - Updated ~20 Go test files whose fixtures/helpers logged in via user.Email; adminCreateUser/loginSession/bootstrapSuperAdminSession now work in terms of phone (deterministically derived from existing email-shaped test identities via testPhoneFromSeed, to avoid rewriting every call site). Added internal/api/auth_phone_login_test.go covering phone login success/failure and old-email rejection. go test ./internal/api/... and npm run test:frontend match their pre-ticket baselines exactly (75 Go failures / 599 frontend tests with 586 pass, 13 fail) by failing test name, not just count.
1057 lines
43 KiB
Go
1057 lines
43 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/config"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func objectSyncTestHandler(t *testing.T, enabled bool) (http.Handler, *store.MemoryStore) {
|
|
t.Helper()
|
|
dataStore := store.NewMemoryStore()
|
|
if err := dataStore.UpsertStorageConnection(t.Context(), domain.StorageConnectionRecord{
|
|
ID: "bos-connection",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
Name: "BOS",
|
|
Provider: domain.StorageProviderBOS,
|
|
Endpoint: "https://bj.bcebos.com",
|
|
AccessKeyID: "masked-in-api",
|
|
SecretAccessKey: "masked-in-api",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return NewHandlerWithConfig(dataStore, nil, config.AppConfig{
|
|
ObjectSyncEnabled: enabled,
|
|
WorkspaceMountPath: "/workspace",
|
|
}), dataStore
|
|
}
|
|
|
|
func objectSyncRequest(t *testing.T, handler http.Handler, method, target string, payload any, idempotencyKey string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
var body bytes.Buffer
|
|
if payload != nil {
|
|
if err := json.NewEncoder(&body).Encode(payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
request := httptest.NewRequest(method, target, &body)
|
|
request.Header.Set("Content-Type", "application/json")
|
|
if idempotencyKey != "" {
|
|
request.Header.Set("Idempotency-Key", idempotencyKey)
|
|
}
|
|
writer := httptest.NewRecorder()
|
|
handler.ServeHTTP(writer, request)
|
|
return writer
|
|
}
|
|
|
|
func TestObjectSyncFeatureFlagBlocksAllRoutes(t *testing.T) {
|
|
handler, _ := objectSyncTestHandler(t, false)
|
|
writer := objectSyncRequest(t, handler, http.MethodGet, "/api/object-sync/spaces", nil, "")
|
|
if writer.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status = %d, want %d; body=%s", writer.Code, http.StatusServiceUnavailable, writer.Body.String())
|
|
}
|
|
var response objectSyncAPIError
|
|
if err := json.Unmarshal(writer.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.Code != "object_sync_disabled" || response.RequestID == "" {
|
|
t.Fatalf("unexpected error envelope: %#v", response)
|
|
}
|
|
}
|
|
|
|
func TestExternalObjectSyncUsesDeviceCredentialWithoutSharedAPIToken(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandlerWithConfig(dataStore, nil, config.AppConfig{
|
|
APIToken: "shared-platform-token",
|
|
RequireSessionAuth: true,
|
|
ObjectSyncEnabled: true,
|
|
})
|
|
writer := objectSyncRequest(t, handler, http.MethodGet, "/api/external/object-sync/capabilities", nil, "")
|
|
if writer.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status = %d, want %d; body=%s", writer.Code, http.StatusUnauthorized, writer.Body.String())
|
|
}
|
|
var response objectSyncAPIError
|
|
if err := json.Unmarshal(writer.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.Code != "credential_required" {
|
|
t.Fatalf("external object sync was blocked by the shared API token gate: %#v", response)
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncScopedCredentialPlansSpaceButRestrictsManifestPaths(t *testing.T) {
|
|
scope := domain.ObjectSyncCredentialScope{
|
|
AllowedRelativePrefixes: []string{"camera one"},
|
|
Actions: []string{"plan", "create"},
|
|
}
|
|
if !objectSyncScopeAllowsAction(scope, "plan") {
|
|
t.Fatal("a prefix-scoped credential must be able to create a Space session")
|
|
}
|
|
if !objectSyncScopeAllows(scope, "create", "camera one/frame.mcap") {
|
|
t.Fatal("an in-prefix manifest path was denied")
|
|
}
|
|
if objectSyncScopeAllows(scope, "create", "camera two/frame.mcap") {
|
|
t.Fatal("an out-of-prefix manifest path was allowed")
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncSpaceStartsBlockedAndCannotBypassCapabilityGate(t *testing.T) {
|
|
handler, _ := objectSyncTestHandler(t, true)
|
|
create := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/spaces", map[string]any{
|
|
"name": "Robot uploads",
|
|
"connectionId": "bos-connection",
|
|
"bucket": "robot-data",
|
|
"rootPrefix": "incoming",
|
|
}, "space-create")
|
|
if create.Code != http.StatusCreated {
|
|
t.Fatalf("status = %d; body=%s", create.Code, create.Body.String())
|
|
}
|
|
var space domain.ObjectSyncSpaceRecord
|
|
if err := json.Unmarshal(create.Body.Bytes(), &space); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if space.Enabled || space.Mode != "blocked" || space.Capability.State != domain.ObjectSyncCapabilityUnverified {
|
|
t.Fatalf("space bypassed initial gate: %#v", space)
|
|
}
|
|
enable := objectSyncRequest(t, handler, http.MethodPut, "/api/object-sync/spaces/"+space.ID, map[string]any{
|
|
"enabled": true,
|
|
}, "space-enable")
|
|
if enable.Code != http.StatusConflict {
|
|
t.Fatalf("status = %d, want conflict; body=%s", enable.Code, enable.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncDriftStateCannotBeClearedBySettingsUpdate(t *testing.T) {
|
|
handler, dataStore := objectSyncTestHandler(t, true)
|
|
target, _ := store.AsObjectSyncStore(dataStore)
|
|
policy := domain.DefaultObjectSyncSpacePolicy()
|
|
policy.AllowConditionalUpdate = true
|
|
policy.AllowForceOverwrite = true
|
|
space := domain.ObjectSyncSpaceRecord{
|
|
ID: "space-drifted",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ConnectionID: "bos-connection",
|
|
Bucket: "robot-data",
|
|
Enabled: false,
|
|
Mode: "blocked",
|
|
Policy: policy,
|
|
Capability: domain.ObjectSyncCapabilityRecord{
|
|
State: domain.ObjectSyncCapabilityDrifted,
|
|
ConditionalCreate: true,
|
|
ConditionalUpdate: true,
|
|
ConditionalMultipart: true,
|
|
ExclusivePrefixConfirmed: true,
|
|
},
|
|
CreatedAt: "2026-07-28T08:00:00Z",
|
|
UpdatedAt: "2026-07-28T08:00:00Z",
|
|
}
|
|
if err := target.UpsertObjectSyncSpace(t.Context(), space); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
writer := objectSyncRequest(t, handler, http.MethodPut, "/api/object-sync/spaces/"+space.ID, map[string]any{
|
|
"name": "renamed but still drifted",
|
|
"enabled": true,
|
|
}, "drift-settings-update")
|
|
if writer.Code != http.StatusConflict {
|
|
t.Fatalf("status = %d; body=%s", writer.Code, writer.Body.String())
|
|
}
|
|
stored, err := target.GetObjectSyncSpace(t.Context(), space.ID)
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("stored space: %#v, %v", stored, err)
|
|
}
|
|
if stored.Capability.State != domain.ObjectSyncCapabilityDrifted || stored.Enabled {
|
|
t.Fatalf("drift gate was bypassed: %#v", stored)
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncWritesRequireIdempotencyKey(t *testing.T) {
|
|
handler, _ := objectSyncTestHandler(t, true)
|
|
writer := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/spaces", map[string]any{
|
|
"name": "Robot uploads",
|
|
"connectionId": "bos-connection",
|
|
"bucket": "robot-data",
|
|
}, "")
|
|
if writer.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d; body=%s", writer.Code, writer.Body.String())
|
|
}
|
|
var response objectSyncAPIError
|
|
if err := json.Unmarshal(writer.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.Code != "idempotency_key_required" {
|
|
t.Fatalf("code = %q", response.Code)
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncIdempotencyReplaysCompletedResponse(t *testing.T) {
|
|
handler, _ := objectSyncTestHandler(t, true)
|
|
payload := map[string]any{
|
|
"name": "Robot uploads",
|
|
"connectionId": "bos-connection",
|
|
"bucket": "robot-data",
|
|
}
|
|
first := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/spaces", payload, "replay-space")
|
|
second := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/spaces", payload, "replay-space")
|
|
if first.Code != http.StatusCreated || second.Code != first.Code {
|
|
t.Fatalf("statuses = %d, %d; first=%s second=%s", first.Code, second.Code, first.Body.String(), second.Body.String())
|
|
}
|
|
if first.Body.String() != second.Body.String() {
|
|
t.Fatalf("replayed response changed:\nfirst=%s\nsecond=%s", first.Body.String(), second.Body.String())
|
|
}
|
|
if second.Header().Get("Idempotency-Replayed") != "true" {
|
|
t.Fatalf("missing replay header: %#v", second.Header())
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncIdempotencyRejectsKeyReuseWithDifferentPayload(t *testing.T) {
|
|
handler, _ := objectSyncTestHandler(t, true)
|
|
first := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/spaces", map[string]any{
|
|
"name": "Robot uploads",
|
|
"connectionId": "bos-connection",
|
|
"bucket": "robot-data",
|
|
}, "reused-space-key")
|
|
if first.Code != http.StatusCreated {
|
|
t.Fatalf("first status = %d; body=%s", first.Code, first.Body.String())
|
|
}
|
|
second := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/spaces", map[string]any{
|
|
"name": "Different uploads",
|
|
"connectionId": "bos-connection",
|
|
"bucket": "robot-data",
|
|
}, "reused-space-key")
|
|
if second.Code != http.StatusConflict {
|
|
t.Fatalf("second status = %d; body=%s", second.Code, second.Body.String())
|
|
}
|
|
var response objectSyncAPIError
|
|
if err := json.Unmarshal(second.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.Code != "idempotency_key_reused" {
|
|
t.Fatalf("code = %q", response.Code)
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncClientReleaseArtifactsAreImmutable(t *testing.T) {
|
|
handler, dataStore := objectSyncTestHandler(t, true)
|
|
target, _ := store.AsObjectSyncStore(dataStore)
|
|
release := map[string]any{
|
|
"id": "drosync-1.0.0",
|
|
"version": "1.0.0",
|
|
"channel": "canary",
|
|
"protocolVersion": "v1",
|
|
"rolloutPercent": 5,
|
|
"paused": false,
|
|
"artifacts": map[string]string{"linux-x86_64": "https://downloads.example/drosync.deb"},
|
|
"sha256": map[string]string{"linux-x86_64": strings.Repeat("a", 64)},
|
|
"signature": "signed-manifest",
|
|
}
|
|
created := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/client-releases", release, "release-create")
|
|
if created.Code != http.StatusOK {
|
|
t.Fatalf("status = %d; body=%s", created.Code, created.Body.String())
|
|
}
|
|
items, err := target.ListObjectSyncClientReleases(t.Context())
|
|
if err != nil || len(items) != 1 {
|
|
t.Fatalf("stored releases = %#v, %v", items, err)
|
|
}
|
|
items[0].StageChangedAt = time.Now().UTC().Add(-25 * time.Hour).Format(time.RFC3339Nano)
|
|
if err := target.UpsertObjectSyncClientRelease(t.Context(), items[0]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
release["channel"] = "pilot"
|
|
release["rolloutPercent"] = 20
|
|
advanced := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/client-releases", release, "release-pilot")
|
|
if advanced.Code != http.StatusOK {
|
|
t.Fatalf("advance status = %d; body=%s", advanced.Code, advanced.Body.String())
|
|
}
|
|
release["artifacts"] = map[string]string{"linux-x86_64": "https://attacker.example/replaced.deb"}
|
|
rejected := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/client-releases", release, "release-mutate")
|
|
if rejected.Code != http.StatusConflict {
|
|
t.Fatalf("mutation status = %d; body=%s", rejected.Code, rejected.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncClientReleasePromotionRequiresObservationWindow(t *testing.T) {
|
|
handler, _ := objectSyncTestHandler(t, true)
|
|
release := map[string]any{
|
|
"id": "drosync-2.0.0",
|
|
"version": "2.0.0",
|
|
"channel": "canary",
|
|
"protocolVersion": "v1",
|
|
"rolloutPercent": 5,
|
|
"paused": false,
|
|
"artifacts": map[string]string{"linux-x86_64": "https://downloads.example/drosync.deb"},
|
|
"sha256": map[string]string{"linux-x86_64": strings.Repeat("b", 64)},
|
|
"signature": "signed-manifest",
|
|
}
|
|
created := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/client-releases", release, "release-observe-create")
|
|
if created.Code != http.StatusOK {
|
|
t.Fatalf("create status = %d; body=%s", created.Code, created.Body.String())
|
|
}
|
|
release["channel"] = "pilot"
|
|
release["rolloutPercent"] = 20
|
|
promoted := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/client-releases", release, "release-observe-promote")
|
|
if promoted.Code != http.StatusConflict {
|
|
t.Fatalf("promotion status = %d; body=%s", promoted.Code, promoted.Body.String())
|
|
}
|
|
var response objectSyncAPIError
|
|
if err := json.Unmarshal(promoted.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.Code != "rollout_observation_incomplete" {
|
|
t.Fatalf("code = %q", response.Code)
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncPreviewReleaseIsDownloadOnly(t *testing.T) {
|
|
handler, _ := objectSyncTestHandler(t, true)
|
|
artifacts := map[string]string{}
|
|
digests := map[string]string{}
|
|
for _, platform := range objectSyncClientPlatforms {
|
|
artifacts[platform] = "bos://bos-connection/robot-data/.drosync-internal/client-releases/" + platform
|
|
digests[platform] = strings.Repeat("d", 64)
|
|
}
|
|
release := map[string]any{
|
|
"id": "drosync-preview-1.0.0-poc",
|
|
"version": "1.0.0-poc",
|
|
"kind": "preview",
|
|
"channel": "preview",
|
|
"protocolVersion": "v1",
|
|
"rolloutPercent": 100,
|
|
"paused": false,
|
|
"artifacts": artifacts,
|
|
"sha256": digests,
|
|
"signature": "",
|
|
}
|
|
created := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/client-releases", release, "preview-create")
|
|
if created.Code != http.StatusOK {
|
|
t.Fatalf("status = %d; body=%s", created.Code, created.Body.String())
|
|
}
|
|
if strings.Contains(created.Body.String(), "bos://") ||
|
|
!strings.Contains(created.Body.String(), "/api/object-sync/client-releases/drosync-preview-1.0.0-poc/artifacts/macos-arm64") {
|
|
t.Fatalf("preview response did not expose guarded download routes: %s", created.Body.String())
|
|
}
|
|
item := domain.ObjectSyncClientReleaseRecord{
|
|
Kind: objectSyncReleasePreview,
|
|
ProtocolVersion: domain.ObjectSyncProtocolVersion,
|
|
RolloutPercent: 100,
|
|
}
|
|
if objectSyncClientReleaseCanAutoUpdate(item) {
|
|
t.Fatal("preview releases must never enter automatic updates")
|
|
}
|
|
item.Kind = objectSyncReleaseSigned
|
|
if !objectSyncClientReleaseCanAutoUpdate(item) {
|
|
t.Fatal("active signed release should remain eligible for automatic updates")
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncPreviewPlatformsIncludeLinuxArm64ForS100(t *testing.T) {
|
|
found := false
|
|
for _, platform := range objectSyncClientPlatforms {
|
|
if platform == "linux-arm64" {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("linux-arm64 must be a required preview-release platform: %#v", objectSyncClientPlatforms)
|
|
}
|
|
artifacts := map[string]string{}
|
|
digests := map[string]string{}
|
|
for _, platform := range objectSyncClientPlatforms {
|
|
artifacts[platform] = "bos://bos-connection/robot-data/.drosync-internal/client-releases/" + platform
|
|
digests[platform] = strings.Repeat("a", 64)
|
|
}
|
|
if err := validateObjectSyncPreviewArtifacts(domain.ObjectSyncClientReleaseRecord{Artifacts: artifacts, SHA256: digests}); err != nil {
|
|
t.Fatalf("linux-arm64 preview release must validate: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncPreviewReleaseRequiresEveryPlatform(t *testing.T) {
|
|
handler, _ := objectSyncTestHandler(t, true)
|
|
release := map[string]any{
|
|
"id": "drosync-preview-incomplete",
|
|
"version": "1.0.0-poc",
|
|
"kind": "preview",
|
|
"channel": "preview",
|
|
"protocolVersion": "v1",
|
|
"rolloutPercent": 100,
|
|
"paused": false,
|
|
"artifacts": map[string]string{
|
|
"macos-arm64": "bos://bos-connection/robot-data/client.pkg",
|
|
},
|
|
"sha256": map[string]string{
|
|
"macos-arm64": strings.Repeat("e", 64),
|
|
},
|
|
"signature": "",
|
|
}
|
|
response := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/client-releases", release, "preview-incomplete")
|
|
if response.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d; body=%s", response.Code, response.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncDeviceApprovalRoutesAreRemoved(t *testing.T) {
|
|
handler, _ := objectSyncTestHandler(t, true)
|
|
writer := objectSyncRequest(t, handler, http.MethodPost, "/api/external/object-sync/device-authorizations", map[string]any{
|
|
"deviceId": "device-01",
|
|
"deviceName": "capture-station",
|
|
"platform": "linux",
|
|
"architecture": "x86_64",
|
|
"clientVersion": "1.0.0",
|
|
}, "pair-device")
|
|
if writer.Code != http.StatusGone {
|
|
t.Fatalf("status = %d; body=%s", writer.Code, writer.Body.String())
|
|
}
|
|
var response objectSyncAPIError
|
|
if err := json.Unmarshal(writer.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.Code != "device_approval_removed" {
|
|
t.Fatalf("unexpected response: %#v", response)
|
|
}
|
|
lookup := objectSyncRequest(t, handler, http.MethodGet, "/api/object-sync/device-authorizations/by-code?userCode=ABCDEF", nil, "removed-lookup")
|
|
if lookup.Code != http.StatusGone {
|
|
t.Fatalf("lookup status = %d; body=%s", lookup.Code, lookup.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncRemovedPairingStillUsesDistributedRateLimit(t *testing.T) {
|
|
handler, _ := objectSyncTestHandler(t, true)
|
|
for index := 0; index < 30; index++ {
|
|
writer := objectSyncRequest(t, handler, http.MethodPost, "/api/external/object-sync/device-authorizations", map[string]any{
|
|
"deviceId": "rate-device",
|
|
"deviceName": "rate-test",
|
|
"platform": "linux",
|
|
"architecture": "x86_64",
|
|
}, "rate-pair-"+strconv.Itoa(index))
|
|
if writer.Code != http.StatusGone {
|
|
t.Fatalf("request %d status = %d; body=%s", index+1, writer.Code, writer.Body.String())
|
|
}
|
|
}
|
|
blocked := objectSyncRequest(t, handler, http.MethodPost, "/api/external/object-sync/device-authorizations", map[string]any{
|
|
"deviceId": "rate-device",
|
|
"deviceName": "rate-test",
|
|
"platform": "linux",
|
|
"architecture": "x86_64",
|
|
}, "rate-pair-blocked")
|
|
if blocked.Code != http.StatusTooManyRequests {
|
|
t.Fatalf("status = %d, want %d; body=%s", blocked.Code, http.StatusTooManyRequests, blocked.Body.String())
|
|
}
|
|
if blocked.Header().Get("Retry-After") == "" || blocked.Header().Get("RateLimit-Remaining") != "0" {
|
|
t.Fatalf("missing rate limit headers: %#v", blocked.Header())
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncAccessKeySecretLifecycle(t *testing.T) {
|
|
handler, dataStore := objectSyncTestHandler(t, true)
|
|
spaceResponse := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/spaces", map[string]any{
|
|
"name": "Scoped key space",
|
|
"connectionId": "bos-connection",
|
|
"bucket": "robot-data",
|
|
"rootPrefix": "acceptance/keys",
|
|
}, "key-space-create")
|
|
if spaceResponse.Code != http.StatusCreated {
|
|
t.Fatalf("space status = %d; body=%s", spaceResponse.Code, spaceResponse.Body.String())
|
|
}
|
|
var space domain.ObjectSyncSpaceRecord
|
|
if err := json.Unmarshal(spaceResponse.Body.Bytes(), &space); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
created := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/credentials", map[string]any{
|
|
"name": "capture station key",
|
|
"scopes": []map[string]any{{
|
|
"syncSpaceId": space.ID,
|
|
"allowedRelativePrefixes": []string{"camera one"},
|
|
"actions": []string{"plan", "create"},
|
|
}},
|
|
}, "credential-create")
|
|
if created.Code != http.StatusCreated {
|
|
t.Fatalf("credential status = %d; body=%s", created.Code, created.Body.String())
|
|
}
|
|
var issued struct {
|
|
Credential domain.ObjectSyncCredentialRecord `json:"credential"`
|
|
Secret string `json:"secret"`
|
|
}
|
|
if err := json.Unmarshal(created.Body.Bytes(), &issued); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.HasPrefix(issued.Credential.AccessKeyID, "dsk_") || !strings.HasPrefix(issued.Secret, "dss_") {
|
|
t.Fatalf("unexpected issued key: %#v", issued)
|
|
}
|
|
if issued.Credential.SecretHash != "" || issued.Credential.TokenHash != "" || len(issued.Credential.TokenHashes) != 0 {
|
|
t.Fatalf("credential response leaked hashes: %#v", issued.Credential)
|
|
}
|
|
target, _ := store.AsObjectSyncStore(dataStore)
|
|
storedCredential, err := target.GetObjectSyncCredential(t.Context(), issued.Credential.ID)
|
|
if err != nil || storedCredential == nil {
|
|
t.Fatalf("stored credential = %#v, %v", storedCredential, err)
|
|
}
|
|
if !strings.HasPrefix(storedCredential.SecretHash, "$argon2id$") || strings.Contains(storedCredential.SecretHash, issued.Secret) {
|
|
t.Fatalf("credential Secret was not stored as an Argon2id hash: %#v", storedCredential)
|
|
}
|
|
|
|
wrong := objectSyncRequest(t, handler, http.MethodPost, "/api/external/object-sync/auth/token", map[string]any{
|
|
"accessKeyId": issued.Credential.AccessKeyID,
|
|
"secret": "dss_wrong",
|
|
"deviceId": "device-key-test",
|
|
}, "credential-auth-wrong")
|
|
if wrong.Code != http.StatusUnauthorized {
|
|
t.Fatalf("wrong secret status = %d; body=%s", wrong.Code, wrong.Body.String())
|
|
}
|
|
|
|
authenticated := objectSyncRequest(t, handler, http.MethodPost, "/api/external/object-sync/auth/token", map[string]any{
|
|
"accessKeyId": issued.Credential.AccessKeyID,
|
|
"secret": issued.Secret,
|
|
"deviceId": "device-key-test",
|
|
"deviceName": "acceptance mac",
|
|
"platform": "macos",
|
|
"architecture": "arm64",
|
|
"clientVersion": "1.0.0",
|
|
}, "credential-auth-correct")
|
|
if authenticated.Code != http.StatusOK {
|
|
t.Fatalf("auth status = %d; body=%s", authenticated.Code, authenticated.Body.String())
|
|
}
|
|
var tokenResponse struct {
|
|
AccessToken string `json:"accessToken"`
|
|
}
|
|
if err := json.Unmarshal(authenticated.Body.Bytes(), &tokenResponse); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tokenResponse.AccessToken == "" {
|
|
t.Fatal("access token was not returned")
|
|
}
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/api/external/object-sync/capabilities", nil)
|
|
request.Header.Set("Idempotency-Key", "credential-capabilities")
|
|
request.Header.Set(objectSyncCredentialHeader, tokenResponse.AccessToken)
|
|
capabilities := httptest.NewRecorder()
|
|
handler.ServeHTTP(capabilities, request)
|
|
if capabilities.Code != http.StatusOK {
|
|
t.Fatalf("capabilities status = %d; body=%s", capabilities.Code, capabilities.Body.String())
|
|
}
|
|
|
|
storedSpace, err := target.GetObjectSyncSpace(t.Context(), space.ID)
|
|
if err != nil || storedSpace == nil {
|
|
t.Fatalf("stored space = %#v, %v", storedSpace, err)
|
|
}
|
|
storedSpace.Enabled = true
|
|
storedSpace.Mode = "create_only"
|
|
storedSpace.Capability.State = domain.ObjectSyncCapabilityCreateOnly
|
|
storedSpace.Capability.ConditionalCreate = true
|
|
if err := target.UpsertObjectSyncSpace(t.Context(), *storedSpace); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
secondDeviceAuth := objectSyncRequest(t, handler, http.MethodPost, "/api/external/object-sync/auth/token", map[string]any{
|
|
"accessKeyId": issued.Credential.AccessKeyID,
|
|
"secret": issued.Secret,
|
|
"deviceId": "device-key-test-two",
|
|
"deviceName": "second acceptance mac",
|
|
"platform": "macos",
|
|
"architecture": "arm64",
|
|
"clientVersion": "1.0.0",
|
|
}, "credential-auth-second-device")
|
|
if secondDeviceAuth.Code != http.StatusOK {
|
|
t.Fatalf("second device auth status = %d; body=%s", secondDeviceAuth.Code, secondDeviceAuth.Body.String())
|
|
}
|
|
var secondTokenResponse struct {
|
|
AccessToken string `json:"accessToken"`
|
|
}
|
|
if err := json.Unmarshal(secondDeviceAuth.Body.Bytes(), &secondTokenResponse); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
secondSessionRequest := httptest.NewRequest(
|
|
http.MethodPost,
|
|
"/api/external/object-sync/sessions",
|
|
strings.NewReader(`{"syncSpaceId":"`+space.ID+`","mappingId":"second-device","clientVersion":"1.0.0","protocolVersion":"v1"}`),
|
|
)
|
|
secondSessionRequest.Header.Set("Content-Type", "application/json")
|
|
secondSessionRequest.Header.Set("Idempotency-Key", "second-device-session")
|
|
secondSessionRequest.Header.Set(objectSyncCredentialHeader, secondTokenResponse.AccessToken)
|
|
secondSessionResponse := httptest.NewRecorder()
|
|
handler.ServeHTTP(secondSessionResponse, secondSessionRequest)
|
|
if secondSessionResponse.Code != http.StatusCreated {
|
|
t.Fatalf("second device session status = %d; body=%s", secondSessionResponse.Code, secondSessionResponse.Body.String())
|
|
}
|
|
var secondSession domain.ObjectSyncSessionRecord
|
|
if err := json.Unmarshal(secondSessionResponse.Body.Bytes(), &secondSession); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if secondSession.DeviceID != "device-key-test-two" {
|
|
t.Fatalf("bearer token was associated with the wrong device: %#v", secondSession)
|
|
}
|
|
|
|
listed := objectSyncRequest(t, handler, http.MethodGet, "/api/object-sync/credentials", nil, "credential-list")
|
|
if listed.Code != http.StatusOK {
|
|
t.Fatalf("list status = %d; body=%s", listed.Code, listed.Body.String())
|
|
}
|
|
if strings.Contains(listed.Body.String(), issued.Secret) || strings.Contains(listed.Body.String(), "$argon2id$") {
|
|
t.Fatalf("credential list leaked a secret or hash: %s", listed.Body.String())
|
|
}
|
|
|
|
rotated := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/credentials/"+issued.Credential.ID+"/rotate", map[string]any{}, "credential-rotate")
|
|
if rotated.Code != http.StatusOK {
|
|
t.Fatalf("rotate status = %d; body=%s", rotated.Code, rotated.Body.String())
|
|
}
|
|
var rotatedResponse struct {
|
|
Secret string `json:"secret"`
|
|
}
|
|
if err := json.Unmarshal(rotated.Body.Bytes(), &rotatedResponse); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rotatedResponse.Secret == "" || rotatedResponse.Secret == issued.Secret {
|
|
t.Fatalf("rotation did not return a new secret: %#v", rotatedResponse)
|
|
}
|
|
|
|
oldTokenRequest := httptest.NewRequest(http.MethodGet, "/api/external/object-sync/capabilities", nil)
|
|
oldTokenRequest.Header.Set("Idempotency-Key", "credential-old-token")
|
|
oldTokenRequest.Header.Set(objectSyncCredentialHeader, tokenResponse.AccessToken)
|
|
oldTokenResponse := httptest.NewRecorder()
|
|
handler.ServeHTTP(oldTokenResponse, oldTokenRequest)
|
|
if oldTokenResponse.Code != http.StatusUnauthorized {
|
|
t.Fatalf("old bearer token remained valid after rotation: %d; body=%s", oldTokenResponse.Code, oldTokenResponse.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncDataOperatorOnlySeesAssignedSpacesAndSessions(t *testing.T) {
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandlerWithConfig(dataStore, nil, config.AppConfig{
|
|
RequireSessionAuth: true,
|
|
ObjectSyncEnabled: true,
|
|
})
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
organization := adminCreateOrganization(t, handler, rootCookie, "Drosync tenant", "drosync-tenant")
|
|
operatorA := adminCreateUser(t, handler, rootCookie, organization.ID, "Operator A", "drosync-a@example.com", domain.UserRoleAnnotator, "operator-a-password")
|
|
operatorB := adminCreateUser(t, handler, rootCookie, organization.ID, "Operator B", "drosync-b@example.com", domain.UserRoleAnnotator, "operator-b-password")
|
|
target, _ := store.AsObjectSyncStore(dataStore)
|
|
now := nowISO()
|
|
if err := target.UpsertObjectSyncClientRelease(t.Context(), domain.ObjectSyncClientReleaseRecord{
|
|
ID: "drosync-stable-download",
|
|
Version: "1.0.0",
|
|
Channel: "stable",
|
|
ProtocolVersion: domain.ObjectSyncProtocolVersion,
|
|
Artifacts: map[string]string{"macos-arm64": "https://downloads.example/drosync.pkg"},
|
|
SHA256: map[string]string{"macos-arm64": strings.Repeat("c", 64)},
|
|
Signature: "signed-manifest",
|
|
RolloutPercent: 100,
|
|
CreatedAt: now,
|
|
StageChangedAt: now,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, space := range []domain.ObjectSyncSpaceRecord{
|
|
{
|
|
ID: "space-assigned-a",
|
|
OrganizationID: organization.ID,
|
|
OperatorUserIDs: []string{operatorA.ID},
|
|
Name: "A only",
|
|
ConnectionID: "connection-a",
|
|
Bucket: "bucket-a",
|
|
RootPrefix: "a",
|
|
Mode: "create_only",
|
|
Policy: domain.DefaultObjectSyncSpacePolicy(),
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
},
|
|
{
|
|
ID: "space-assigned-b",
|
|
OrganizationID: organization.ID,
|
|
OperatorUserIDs: []string{operatorB.ID},
|
|
Name: "B only",
|
|
ConnectionID: "connection-b",
|
|
Bucket: "bucket-b",
|
|
RootPrefix: "b",
|
|
Mode: "create_only",
|
|
Policy: domain.DefaultObjectSyncSpacePolicy(),
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
},
|
|
} {
|
|
if err := target.UpsertObjectSyncSpace(t.Context(), space); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sessionCreatedAt := now
|
|
if space.ID == "space-assigned-b" {
|
|
sessionCreatedAt = time.Now().UTC().Add(time.Hour).Format(time.RFC3339Nano)
|
|
}
|
|
if err := target.UpsertObjectSyncSession(t.Context(), domain.ObjectSyncSessionRecord{
|
|
ID: "session-" + space.ID,
|
|
OrganizationID: organization.ID,
|
|
SyncSpaceID: space.ID,
|
|
Status: domain.ObjectSyncSessionCompleted,
|
|
CreatedAt: sessionCreatedAt,
|
|
UpdatedAt: sessionCreatedAt,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
operatorCookie := loginSession(t, handler, operatorA.Phone, "operator-a-password")
|
|
spacesResponse := serveJSONWithCookie(handler, http.MethodGet, "/api/object-sync/spaces", nil, operatorCookie)
|
|
if spacesResponse.Code != http.StatusOK {
|
|
t.Fatalf("spaces status = %d; body=%s", spacesResponse.Code, spacesResponse.Body.String())
|
|
}
|
|
if !strings.Contains(spacesResponse.Body.String(), "space-assigned-a") || strings.Contains(spacesResponse.Body.String(), "space-assigned-b") {
|
|
t.Fatalf("space assignment was not enforced: %s", spacesResponse.Body.String())
|
|
}
|
|
sessionsResponse := serveJSONWithCookie(handler, http.MethodGet, "/api/object-sync/sessions?limit=1", nil, operatorCookie)
|
|
if sessionsResponse.Code != http.StatusOK {
|
|
t.Fatalf("sessions status = %d; body=%s", sessionsResponse.Code, sessionsResponse.Body.String())
|
|
}
|
|
if !strings.Contains(sessionsResponse.Body.String(), "session-space-assigned-a") || strings.Contains(sessionsResponse.Body.String(), "session-space-assigned-b") {
|
|
t.Fatalf("session assignment was not inherited from the space: %s", sessionsResponse.Body.String())
|
|
}
|
|
clientReleasesResponse := serveJSONWithCookie(handler, http.MethodGet, "/api/object-sync/client-releases", nil, operatorCookie)
|
|
if clientReleasesResponse.Code != http.StatusOK {
|
|
t.Fatalf("client releases status = %d; body=%s", clientReleasesResponse.Code, clientReleasesResponse.Body.String())
|
|
}
|
|
if !strings.Contains(clientReleasesResponse.Body.String(), "drosync-stable-download") {
|
|
t.Fatalf("stable client release was not available to data operator: %s", clientReleasesResponse.Body.String())
|
|
}
|
|
}
|
|
|
|
type resumableObjectSyncBrowser struct {
|
|
*fakeObjectStorageBrowser
|
|
initiateCalls int
|
|
}
|
|
|
|
func (browser *resumableObjectSyncBrowser) PutObjectConditional(ctx context.Context, bucket, key string, payload []byte, contentType, _, _ string, _ map[string]string) (domain.ObjectStorageObjectSummary, error) {
|
|
return browser.PutObject(ctx, bucket, key, payload, contentType)
|
|
}
|
|
|
|
func (browser *resumableObjectSyncBrowser) PublishMultipartCopyConditional(_ context.Context, bucket, _, targetKey string, sizeBytes int64, _, _, _ string, _ map[string]string) (domain.ObjectStorageObjectSummary, error) {
|
|
return domain.ObjectStorageObjectSummary{Bucket: bucket, Key: targetKey, SizeBytes: sizeBytes}, nil
|
|
}
|
|
|
|
func (browser *resumableObjectSyncBrowser) GetBucketVersioning(context.Context, string) (string, error) {
|
|
return "", nil
|
|
}
|
|
|
|
func (browser *resumableObjectSyncBrowser) PresignPutObjectBound(ctx context.Context, bucket, key, contentType, _ string, _ int64, _ map[string]string, expires int) (domain.ObjectStoragePresignedUploadResult, error) {
|
|
return browser.PresignPutObject(ctx, bucket, key, contentType, expires)
|
|
}
|
|
|
|
func (browser *resumableObjectSyncBrowser) InitiateMultipartUploadBound(ctx context.Context, bucket, key, contentType string, _ map[string]string, expires int) (domain.ObjectStorageMultipartUploadCreateResult, error) {
|
|
browser.initiateCalls++
|
|
return browser.InitiateMultipartUpload(ctx, bucket, key, contentType, expires)
|
|
}
|
|
|
|
func (browser *resumableObjectSyncBrowser) PresignMultipartUploadPartBound(ctx context.Context, bucket, key, uploadID string, partNumber int, _ string, _ int64, expires int) (domain.ObjectStorageMultipartUploadPartURLResult, error) {
|
|
return browser.PresignMultipartUploadPart(ctx, bucket, key, uploadID, partNumber, expires)
|
|
}
|
|
|
|
func (browser *resumableObjectSyncBrowser) CompleteMultipartUploadBound(ctx context.Context, bucket, key, uploadID string, parts []domain.ObjectStorageMultipartUploadPart, _ map[string]string) (domain.ObjectStorageObjectSummary, error) {
|
|
return browser.CompleteMultipartUpload(ctx, bucket, key, uploadID, parts)
|
|
}
|
|
|
|
func TestObjectSyncMultipartInitiateResumesAndCheckpointsParts(t *testing.T) {
|
|
handler, dataStore := objectSyncTestHandler(t, true)
|
|
target, _ := store.AsObjectSyncStore(dataStore)
|
|
browser := &resumableObjectSyncBrowser{fakeObjectStorageBrowser: newFakeObjectStorageBrowser()}
|
|
originalResolver := storageConnectionBrowserResolver
|
|
storageConnectionBrowserResolver = func(context.Context, *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
return browser, func() {}, nil
|
|
}
|
|
t.Cleanup(func() { storageConnectionBrowserResolver = originalResolver })
|
|
|
|
now := nowISO()
|
|
token := "multipart-device-token"
|
|
space := domain.ObjectSyncSpaceRecord{
|
|
ID: "space-multipart-resume",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
Name: "Multipart resume",
|
|
ConnectionID: "bos-connection",
|
|
Bucket: "datastorage",
|
|
RootPrefix: "resume",
|
|
Mode: "create_only",
|
|
Enabled: true,
|
|
Policy: domain.DefaultObjectSyncSpacePolicy(),
|
|
Capability: domain.ObjectSyncCapabilityRecord{
|
|
State: domain.ObjectSyncCapabilityCreateOnly,
|
|
ConditionalCreate: true,
|
|
},
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
credential := domain.ObjectSyncCredentialRecord{
|
|
ID: "credential-multipart-resume",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
DeviceID: "device-multipart-resume",
|
|
Name: "multipart test",
|
|
Enabled: true,
|
|
TokenHash: sessionTokenHash(token),
|
|
Scopes: []domain.ObjectSyncCredentialScope{{
|
|
SyncSpaceID: space.ID,
|
|
AllowedRelativePrefixes: []string{""},
|
|
Actions: []string{"plan", "create", "keep_both"},
|
|
}},
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
device := domain.ObjectSyncDeviceRecord{
|
|
ID: credential.DeviceID,
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
Name: "multipart device",
|
|
Status: "active",
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
session := domain.ObjectSyncSessionRecord{
|
|
ID: "session-multipart-resume",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
SyncSpaceID: space.ID,
|
|
CredentialID: credential.ID,
|
|
DeviceID: device.ID,
|
|
Status: domain.ObjectSyncSessionRunning,
|
|
PolicySnapshot: space.Policy,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
item := domain.ObjectSyncItemRecord{
|
|
ID: "item-multipart-resume",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
SessionID: session.ID,
|
|
RelativePath: "large.bin",
|
|
TargetKey: "resume/large.bin",
|
|
SizeBytes: 600 * 1024 * 1024,
|
|
SHA256: strings.Repeat("a", 64),
|
|
Status: domain.ObjectSyncItemReserved,
|
|
StagingKey: ".drosync-internal/staging/resume/large.bin",
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := target.UpsertObjectSyncSpace(t.Context(), space); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := target.UpsertObjectSyncCredential(t.Context(), credential); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := target.UpsertObjectSyncDevice(t.Context(), device); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := target.UpsertObjectSyncSession(t.Context(), session); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := target.UpsertObjectSyncItems(t.Context(), []domain.ObjectSyncItemRecord{item}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
request := func(path string, payload any, idempotencyKey string) *httptest.ResponseRecorder {
|
|
var body bytes.Buffer
|
|
if payload != nil {
|
|
if err := json.NewEncoder(&body).Encode(payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
httpRequest := httptest.NewRequest(http.MethodPost, path, &body)
|
|
httpRequest.Header.Set("Content-Type", "application/json")
|
|
httpRequest.Header.Set("Idempotency-Key", idempotencyKey)
|
|
httpRequest.Header.Set(objectSyncCredentialHeader, token)
|
|
response := httptest.NewRecorder()
|
|
handler.ServeHTTP(response, httpRequest)
|
|
return response
|
|
}
|
|
|
|
initiated := request("/api/external/object-sync/items/"+item.ID+"/uploads/multipart", map[string]any{}, "multipart-initiate-first")
|
|
if initiated.Code != http.StatusCreated {
|
|
t.Fatalf("initiate status = %d; body=%s", initiated.Code, initiated.Body.String())
|
|
}
|
|
var first struct {
|
|
UploadID string `json:"uploadId"`
|
|
}
|
|
if err := json.Unmarshal(initiated.Body.Bytes(), &first); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if first.UploadID == "" || browser.initiateCalls != 1 {
|
|
t.Fatalf("unexpected first initiate: %#v calls=%d", first, browser.initiateCalls)
|
|
}
|
|
|
|
for _, part := range []map[string]any{
|
|
{"partNumber": 2, "eTag": "etag-two", "sizeBytes": 256 * 1024 * 1024},
|
|
{"partNumber": 1, "eTag": "etag-one", "sizeBytes": 256 * 1024 * 1024},
|
|
} {
|
|
partNumber := part["partNumber"].(int)
|
|
checkpoint := request("/api/external/object-sync/items/"+item.ID+"/uploads/multipart/parts", part, "multipart-checkpoint-"+strconv.Itoa(partNumber))
|
|
if checkpoint.Code != http.StatusOK {
|
|
t.Fatalf("checkpoint %d status = %d; body=%s", partNumber, checkpoint.Code, checkpoint.Body.String())
|
|
}
|
|
}
|
|
|
|
resumed := request("/api/external/object-sync/items/"+item.ID+"/uploads/multipart", map[string]any{}, "multipart-initiate-resume")
|
|
if resumed.Code != http.StatusOK {
|
|
t.Fatalf("resume status = %d; body=%s", resumed.Code, resumed.Body.String())
|
|
}
|
|
var resumeResponse struct {
|
|
UploadID string `json:"uploadId"`
|
|
UploadedParts []domain.ObjectStorageMultipartUploadPart `json:"uploadedParts"`
|
|
Resumed bool `json:"resumed"`
|
|
}
|
|
if err := json.Unmarshal(resumed.Body.Bytes(), &resumeResponse); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !resumeResponse.Resumed || resumeResponse.UploadID != first.UploadID || browser.initiateCalls != 1 {
|
|
t.Fatalf("multipart upload was not resumed: %#v calls=%d", resumeResponse, browser.initiateCalls)
|
|
}
|
|
if len(resumeResponse.UploadedParts) != 2 || resumeResponse.UploadedParts[0].PartNumber != 1 || resumeResponse.UploadedParts[1].PartNumber != 2 {
|
|
t.Fatalf("checkpointed parts were not persisted in order: %#v", resumeResponse.UploadedParts)
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncSessionsUseOpaqueCursorPagination(t *testing.T) {
|
|
handler, dataStore := objectSyncTestHandler(t, true)
|
|
target, _ := store.AsObjectSyncStore(dataStore)
|
|
for _, id := range []string{"session-a", "session-b", "session-c"} {
|
|
if err := target.UpsertObjectSyncSession(t.Context(), domain.ObjectSyncSessionRecord{
|
|
ID: id,
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
Status: domain.ObjectSyncSessionRunning,
|
|
CreatedAt: "2026-07-28T08:00:00Z",
|
|
UpdatedAt: "2026-07-28T08:00:00Z",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
first := objectSyncRequest(t, handler, http.MethodGet, "/api/object-sync/sessions?limit=2", nil, "")
|
|
if first.Code != http.StatusOK {
|
|
t.Fatalf("first status = %d; body=%s", first.Code, first.Body.String())
|
|
}
|
|
var firstPage domain.ObjectSyncSessionPage
|
|
if err := json.Unmarshal(first.Body.Bytes(), &firstPage); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(firstPage.Items) != 2 || firstPage.NextCursor == "" || strings.Contains(firstPage.NextCursor, "session-") {
|
|
t.Fatalf("unexpected first page: %#v", firstPage)
|
|
}
|
|
second := objectSyncRequest(t, handler, http.MethodGet, "/api/object-sync/sessions?limit=2&cursor="+firstPage.NextCursor, nil, "")
|
|
if second.Code != http.StatusOK {
|
|
t.Fatalf("second status = %d; body=%s", second.Code, second.Body.String())
|
|
}
|
|
var secondPage domain.ObjectSyncSessionPage
|
|
if err := json.Unmarshal(second.Body.Bytes(), &secondPage); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(secondPage.Items) != 1 || secondPage.NextCursor != "" {
|
|
t.Fatalf("unexpected second page: %#v", secondPage)
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncForceOverwriteQueuesSeparateAuditedItem(t *testing.T) {
|
|
handler, dataStore := objectSyncTestHandler(t, true)
|
|
target, _ := store.AsObjectSyncStore(dataStore)
|
|
policy := domain.DefaultObjectSyncSpacePolicy()
|
|
policy.AllowConditionalUpdate = true
|
|
policy.AllowForceOverwrite = true
|
|
space := domain.ObjectSyncSpaceRecord{
|
|
ID: "space-force",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ConnectionID: "bos-connection",
|
|
Bucket: "robot-data",
|
|
Enabled: true,
|
|
Mode: "update_enabled",
|
|
Policy: policy,
|
|
Capability: domain.ObjectSyncCapabilityRecord{
|
|
State: domain.ObjectSyncCapabilityUpdateEnabled,
|
|
ConditionalCreate: true,
|
|
ConditionalUpdate: true,
|
|
ConditionalMultipart: true,
|
|
ExclusivePrefixConfirmed: true,
|
|
},
|
|
CreatedAt: "2026-07-28T08:00:00Z",
|
|
UpdatedAt: "2026-07-28T08:00:00Z",
|
|
}
|
|
if err := target.UpsertObjectSyncSpace(t.Context(), space); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
session := domain.ObjectSyncSessionRecord{
|
|
ID: "session-force",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
SyncSpaceID: space.ID,
|
|
Status: domain.ObjectSyncSessionConflicted,
|
|
CreatedAt: "2026-07-28T08:00:00Z",
|
|
UpdatedAt: "2026-07-28T08:00:00Z",
|
|
}
|
|
if err := target.UpsertObjectSyncSession(t.Context(), session); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conflict := domain.ObjectSyncItemRecord{
|
|
ID: "item-conflict",
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
SessionID: session.ID,
|
|
ClientItemID: "client-conflict",
|
|
TargetKey: "incoming/file.bin.__conflict__20260728T000000Z_source",
|
|
LogicalTargetKey: "incoming/file.bin",
|
|
SizeBytes: 4,
|
|
SHA256: "abcd",
|
|
Status: domain.ObjectSyncItemKeptBoth,
|
|
CreatedAt: "2026-07-28T08:00:00Z",
|
|
UpdatedAt: "2026-07-28T08:00:00Z",
|
|
}
|
|
if err := target.UpsertObjectSyncItems(t.Context(), []domain.ObjectSyncItemRecord{conflict}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
writer := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/items/"+conflict.ID+"/force-overwrite", map[string]any{}, "force-overwrite")
|
|
if writer.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d; body=%s", writer.Code, writer.Body.String())
|
|
}
|
|
page, err := target.ListObjectSyncItems(t.Context(), domain.ObjectSyncListQuery{SessionID: session.ID, Limit: 10})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(page.Items) != 2 {
|
|
t.Fatalf("items = %#v", page.Items)
|
|
}
|
|
original, err := target.GetObjectSyncItem(t.Context(), conflict.ID)
|
|
if err != nil || original == nil || original.Status != domain.ObjectSyncItemKeptBoth {
|
|
t.Fatalf("original conflict was mutated: %#v, %v", original, err)
|
|
}
|
|
var queued *domain.ObjectSyncItemRecord
|
|
for index := range page.Items {
|
|
if page.Items[index].ID != conflict.ID {
|
|
queued = &page.Items[index]
|
|
}
|
|
}
|
|
if queued == nil || queued.Action != "force_overwrite" || queued.ForceSourceKey != conflict.TargetKey || queued.Status != domain.ObjectSyncItemCommitting {
|
|
t.Fatalf("queued force item = %#v", queued)
|
|
}
|
|
}
|
|
|
|
func TestObjectSyncReleaseRejectsSessionsAcrossSpaces(t *testing.T) {
|
|
handler, dataStore := objectSyncTestHandler(t, true)
|
|
target, _ := store.AsObjectSyncStore(dataStore)
|
|
for index, spaceID := range []string{"space-a", "space-b"} {
|
|
if err := target.UpsertObjectSyncSession(t.Context(), domain.ObjectSyncSessionRecord{
|
|
ID: "release-session-" + strconv.Itoa(index),
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
SyncSpaceID: spaceID,
|
|
Status: domain.ObjectSyncSessionCompleted,
|
|
CreatedAt: "2026-07-28T08:00:00Z",
|
|
UpdatedAt: "2026-07-28T08:00:00Z",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
writer := objectSyncRequest(t, handler, http.MethodPost, "/api/object-sync/releases", map[string]any{
|
|
"name": "ambiguous-release",
|
|
"sessionIds": []string{"release-session-0", "release-session-1"},
|
|
}, "release-cross-space")
|
|
if writer.Code != http.StatusConflict {
|
|
t.Fatalf("status = %d; body=%s", writer.Code, writer.Body.String())
|
|
}
|
|
var response objectSyncAPIError
|
|
if err := json.Unmarshal(writer.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.Code != "release_space_mismatch" {
|
|
t.Fatalf("code = %q", response.Code)
|
|
}
|
|
}
|