droboticflow/internal/api/object_storage_browser_test.go

122 lines
4.7 KiB
Go

package api
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"droboticflow/internal/config"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"github.com/baidubce/bce-sdk-go/bce"
)
func TestExactObjectStorageKeyPreservesTrailingSlash(t *testing.T) {
if got := exactObjectStorageKey(" algo/nested/ "); got != "algo/nested/" {
t.Fatalf("exactObjectStorageKey preserved key = %q, want %q", got, "algo/nested/")
}
}
func TestIsBOSNoSuchKeyRecognizesServiceError(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{name: "explicit no such key", err: bce.NewBceServiceError("NoSuchKey", "The specified key does not exist.", "req-1", http.StatusNotFound), want: true},
{name: "empty body head response", err: bce.NewBceServiceError("NotFound", "404 Not Found", "req-2", http.StatusNotFound), want: true},
{name: "missing bucket remains an error", err: bce.NewBceServiceError("NoSuchBucket", "The specified bucket does not exist.", "req-3", http.StatusNotFound), want: false},
{name: "not found code requires 404", err: bce.NewBceServiceError("NotFound", "unexpected", "req-4", http.StatusInternalServerError), want: false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := isBOSNoSuchKey(test.err); got != test.want {
t.Fatalf("isBOSNoSuchKey() = %t, want %t", got, test.want)
}
})
}
}
func TestRetryObjectStorageSummaryRetriesTransientNotFound(t *testing.T) {
attempts := 0
want := domain.ObjectStorageObjectSummary{Bucket: "datastorage", Key: "large/file.bin"}
got, err := retryObjectStorageSummary(context.Background(), 4, 0, func() (domain.ObjectStorageObjectSummary, error) {
attempts++
if attempts < 3 {
return domain.ObjectStorageObjectSummary{}, bce.NewBceServiceError("NotFound", "404 Not Found", "req-retry", http.StatusNotFound)
}
return want, nil
})
if err != nil {
t.Fatalf("retryObjectStorageSummary() error = %v", err)
}
if attempts != 3 || got.Bucket != want.Bucket || got.Key != want.Key {
t.Fatalf("retryObjectStorageSummary() attempts = %d summary = %#v, want attempts=3 summary=%#v", attempts, got, want)
}
}
func TestRetryObjectStorageSummaryDoesNotRetryOtherErrors(t *testing.T) {
attempts := 0
wantErr := bce.NewBceServiceError("NoSuchBucket", "missing bucket", "req-bucket", http.StatusNotFound)
_, err := retryObjectStorageSummary(context.Background(), 4, 0, func() (domain.ObjectStorageObjectSummary, error) {
attempts++
return domain.ObjectStorageObjectSummary{}, wantErr
})
if !errors.Is(err, wantErr) || attempts != 1 {
t.Fatalf("retryObjectStorageSummary() error = %v attempts = %d, want original error and one attempt", err, attempts)
}
}
func TestRenderObjectStorageWriteErrorTreatsBOSNoSuchKeyAsNotFound(t *testing.T) {
recorder := httptest.NewRecorder()
err := bce.NewBceServiceError("NoSuchKey", "The specified key does not exist.", "req-2", http.StatusNotFound)
renderObjectStorageWriteError(recorder, err)
if recorder.Code != http.StatusNotFound {
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusNotFound)
}
if !strings.Contains(recorder.Body.String(), "NoSuchKey") {
t.Fatalf("expected response to include NoSuchKey, got %s", recorder.Body.String())
}
}
func TestObjectStorageJSONPutRejectsOversizedBody(t *testing.T) {
body := `{"key":"big.txt","content":"` + strings.Repeat("x", int(objectStorageJSONPutMaxBytes)) + `"}`
handler := newBusinessTestHandler(store.NewMemoryStore(), nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/api/object-storage/objects", strings.NewReader(body)))
if recorder.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("expected oversized JSON put to return 413, got %d body=%s", recorder.Code, recorder.Body.String())
}
}
func TestSyncReportEndpointsRequireSessionAuth(t *testing.T) {
appCfg := config.AppConfig{}
handler := NewHandlerWithConfig(store.NewMemoryStore(), nil, appCfg)
for _, path := range []string{"/api/object-storage/sync-report"} {
req := httptest.NewRequest(http.MethodGet, path, nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Fatalf("expected endpoint %s to require session auth", path)
}
}
}
func TestRetiredPlatformSyncReportRouteIsNotAvailableToSignedInUsers(t *testing.T) {
handler := newBusinessTestHandlerWithConfig(store.NewMemoryStore(), nil, config.AppConfig{})
for _, suffix := range []string{"", "?download=csv&path=/unused", "?download=xlsx"} {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/object-storage/sync-report"+suffix, nil))
if recorder.Code != http.StatusNotFound {
t.Fatalf("retired report route = %d, want 404", recorder.Code)
}
}
}