package objectstorage import ( "context" "net/http" "net/http/httptest" "testing" "time" "github.com/geo-platform/tenant-api/internal/shared/config" ) func TestAliyunPutObjectOptionsAcceptsSupportedObjectACL(t *testing.T) { t.Parallel() for _, acl := range []string{"", "default", "private", "public-read", "public-read-write"} { acl := acl t.Run(acl, func(t *testing.T) { t.Parallel() options, err := aliyunPutObjectOptions(config.ObjectStorageConfig{ObjectACL: acl}, "application/zip") if err != nil { t.Fatalf("aliyunPutObjectOptions(%q) error = %v", acl, err) } if len(options) == 0 { t.Fatalf("expected at least content type option") } }) } } func TestAliyunPutObjectOptionsRejectsUnsupportedObjectACL(t *testing.T) { t.Parallel() _, err := aliyunPutObjectOptions(config.ObjectStorageConfig{ObjectACL: "authenticated-read"}, "application/zip") if err == nil { t.Fatal("expected unsupported object acl error") } } func TestAliyunExistsHonorsContextCancellation(t *testing.T) { t.Parallel() requestStarted := make(chan struct{}) blockRequest := make(chan struct{}) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { select { case <-requestStarted: default: close(requestStarted) } <-blockRequest w.WriteHeader(http.StatusOK) })) defer func() { close(blockRequest) server.Close() }() client := NewAliyunClient(config.ObjectStorageConfig{ Provider: "aliyun", Endpoint: server.URL, AccessKey: "test-access-key", SecretKey: "test-secret-key", Bucket: "test-bucket", Region: "cn-test", }, nil) ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) defer cancel() started := make(chan struct{}) done := make(chan error, 1) go func() { close(started) _, err := client.Exists(ctx, "desktop-client/releases/test.dmg") done <- err }() <-started select { case <-requestStarted: case <-time.After(time.Second): t.Fatal("expected aliyun request to reach the test server") } select { case err := <-done: if err == nil { t.Fatal("expected context cancellation error") } case <-time.After(2 * time.Second): t.Fatal("aliyun Exists did not honor context cancellation") } } func TestIsPublicReadACL(t *testing.T) { t.Parallel() for _, tc := range []struct { acl string want bool }{ {acl: "", want: false}, {acl: "default", want: false}, {acl: "private", want: false}, {acl: "public-read", want: true}, {acl: "public-read-write", want: true}, {acl: " PUBLIC-READ ", want: true}, } { got := isPublicReadACL(tc.acl) if got != tc.want { t.Fatalf("isPublicReadACL(%q) = %v, want %v", tc.acl, got, tc.want) } } }