test: share signedAdminRequest between the admin tests

signedAdminRequest takes the signing region and a body, and always names
root in X-Vgw-Owner (admin create-bucket requires it, the other admin
routes ignore it), so the create-bucket test reuses it instead of its own
copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Christian Schmitt
2026-10-21 20:02:01 +02:00
co-authored by Claude Opus 5.5
parent c07ffdc315
commit 8137b7adae
2 changed files with 11 additions and 28 deletions
+1 -23
View File
@@ -15,18 +15,11 @@
package s3api
import (
"context"
"crypto/sha256"
"encoding/hex"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/gofiber/fiber/v3"
"github.com/versity/versitygw/backend"
"github.com/versity/versitygw/s3api/controllers"
@@ -84,7 +77,7 @@ func TestAdminCreateBucketLocationConstraint(t *testing.T) {
body = "<CreateBucketConfiguration><LocationConstraint>" + tt.constraint + "</LocationConstraint></CreateBucketConfiguration>"
}
resp, err := srv.newApp(t).Test(signedAdminCreateBucketRequest(t, "/test-bucket/create", region, body))
resp, err := srv.newApp(t).Test(signedAdminRequest(t, "/test-bucket/create", region, body))
if err != nil {
t.Fatalf("app.Test: %v", err)
}
@@ -101,18 +94,3 @@ func TestAdminCreateBucketLocationConstraint(t *testing.T) {
}
}
}
func signedAdminCreateBucketRequest(t *testing.T, path, region, body string) *http.Request {
t.Helper()
sum := sha256.Sum256([]byte(body))
payloadHash := hex.EncodeToString(sum[:])
req := httptest.NewRequest(http.MethodPatch, "http://localhost"+path, strings.NewReader(body))
req.Header.Set("X-Amz-Content-Sha256", payloadHash)
req.Header.Set("X-Vgw-Owner", testAdminRoot.Access)
creds := aws.Credentials{AccessKeyID: testAdminRoot.Access, SecretAccessKey: testAdminRoot.Secret}
if err := v4.NewSigner().SignHTTP(context.Background(), creds, req, payloadHash, "s3", region, time.Now()); err != nil {
t.Fatalf("sign request: %v", err)
}
return req
}
+10 -5
View File
@@ -20,6 +20,7 @@ import (
"encoding/hex"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
@@ -58,7 +59,7 @@ func TestAdminPathPrefix(t *testing.T) {
for _, tt := range tests {
t.Run(srv.name+"/"+tt.name, func(t *testing.T) {
app := srv.newApp(t, tt.prefix)
resp, err := app.Test(signedAdminRequest(t, tt.path))
resp, err := app.Test(signedAdminRequest(t, tt.path, "us-east-1", ""))
if err != nil {
t.Fatalf("app.Test: %v", err)
}
@@ -121,15 +122,19 @@ func newTestS3AdminApp(t *testing.T, prefix string) *fiber.App {
return srv.app
}
func signedAdminRequest(t *testing.T, path string) *http.Request {
// signedAdminRequest returns a PATCH to an admin route, signed with the root
// credentials for region. X-Vgw-Owner names root, which admin create-bucket
// requires and the other admin routes ignore.
func signedAdminRequest(t *testing.T, path, region, body string) *http.Request {
t.Helper()
sum := sha256.Sum256(nil)
sum := sha256.Sum256([]byte(body))
payloadHash := hex.EncodeToString(sum[:])
req := httptest.NewRequest(http.MethodPatch, "http://localhost"+path, nil)
req := httptest.NewRequest(http.MethodPatch, "http://localhost"+path, strings.NewReader(body))
req.Header.Set("X-Amz-Content-Sha256", payloadHash)
req.Header.Set("X-Vgw-Owner", testAdminRoot.Access)
creds := aws.Credentials{AccessKeyID: testAdminRoot.Access, SecretAccessKey: testAdminRoot.Secret}
if err := v4.NewSigner().SignHTTP(context.Background(), creds, req, payloadHash, "s3", "us-east-1", time.Now()); err != nil {
if err := v4.NewSigner().SignHTTP(context.Background(), creds, req, payloadHash, "s3", region, time.Now()); err != nil {
t.Fatalf("sign request: %v", err)
}
return req