From 3bdc8b6eb6d1dd015bf62339f4476cd9dd8131e1 Mon Sep 17 00:00:00 2001 From: Shion Ichikawa Date: Sat, 11 May 2024 10:21:28 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20(pkg/gcs)=20test=20for=20IssueDownl?= =?UTF-8?q?oadSignedURL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/gcs/object_test.go | 102 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 pkg/gcs/object_test.go diff --git a/pkg/gcs/object_test.go b/pkg/gcs/object_test.go new file mode 100644 index 0000000..c6cec2d --- /dev/null +++ b/pkg/gcs/object_test.go @@ -0,0 +1,102 @@ +package gcs + +import ( + "cloud.google.com/go/storage" + "context" + "fmt" + "google.golang.org/api/option" + "io" + "net/http" + "net/url" + "testing" + "time" + "ynufes-mypage-backend/pkg/setting" +) + +func TestObjectRef_IssueDownloadSignedURL(t *testing.T) { + ctx := context.Background() + conf := setting.Get() + certPath := conf.Infrastructure.Firebase.JsonCredentialFile + c, err := storage.NewClient(ctx, option.WithCredentialsFile(certPath)) + if err != nil { + t.Fatal(err) + } + b := c.Bucket("ynufes-mypage-staging-bucket") + ref := NewFolderRef(b, "some-folder") + uploadRef, err := ref.Upload(ctx, "some-file", []byte("some-content")) + if err != nil { + t.Fatal(err) + return + } + issueOpt := IssueLinkOptions{ + ExpiresIn: 5 * time.Second, + AuthHeaders: map[string]string{ + "user": "shion-test", + }, + AuthMetaHeaders: map[string]string{ + "user": "shion-meta", + }, + AuthQueries: map[string]string{ + "user-query": "shion-query", + }, + } + targetUrl, err := uploadRef.IssueDownloadSignedURL(issueOpt, true) + if err != nil { + t.Fatal(err) + } + if err := verifySignedURL(targetUrl, issueOpt, true); err != nil { + t.Fatal(err) + } +} + +//func uploadTestFile( +// ctx context.Context, +// bucket *storage.BucketHandle, +// fileName string, +// content []byte, +//) (ObjectRef, error) { +// ref := NewFolderRef(bucket, "some-folder") +// return ref.Upload(ctx, fileName, content) +//} + +func verifySignedURL( + target SignedObjectLink, + opt IssueLinkOptions, + addQuery bool, +) error { + targetURL, err := url.Parse(string(target)) + if err != nil { + return fmt.Errorf("failed to parse signed URL: %w", err) + } + if addQuery { + q := targetURL.Query() + for k, v := range opt.AuthQueries { + q.Set(k, v) + } + targetURL.RawQuery = q.Encode() + } + fmt.Println("targetURL: ", targetURL.String()) + req, err := http.NewRequest("GET", targetURL.String(), nil) + if err != nil { + return fmt.Errorf("failed to create request: %w", err) + } + for k, v := range opt.AuthHeaders { + req.Header.Set(k, v) + } + for k, v := range opt.AuthMetaHeaders { + req.Header.Set("x-goog-meta-"+k, v) + } + client := http.DefaultClient + resp, err := client.Do(req) + if err != nil { + return fmt.Errorf("failed to send request: %w", err) + } + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + payload, _ := io.ReadAll(resp.Body) + if string(payload) != "some-content" { + return fmt.Errorf("unexpected payload: %s", string(payload)) + } + return nil +}