go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/gsutil/gsutil_test.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package gsutil 16 17 import ( 18 "context" 19 "fmt" 20 "testing" 21 22 "cloud.google.com/go/storage" 23 24 "go.chromium.org/luci/common/clock" 25 "go.chromium.org/luci/server/auth" 26 "go.chromium.org/luci/server/auth/authtest" 27 "time" 28 29 "go.chromium.org/luci/resultdb/internal/testutil" 30 31 . "github.com/smartystreets/goconvey/convey" 32 ) 33 34 func TestGenerateSignedURL(t *testing.T) { 35 t.Parallel() 36 37 Convey(`Generate Signed URL`, t, func() { 38 Convey(`Valid`, func() { 39 ctx := auth.WithState(context.Background(), &authtest.FakeState{ 40 Identity: "user:user@example.com", 41 }) 42 bucket := "testBucket" 43 object := "object.txt" 44 expiration := clock.Now(ctx).UTC().Add(7 * 24 * time.Hour) 45 opts := testutil.GetSignedURLOptions(ctx) 46 47 gsClient, err := storage.NewClient(ctx) 48 So(err, ShouldBeNil) 49 50 url, err := GenerateSignedURL(ctx, gsClient, bucket, object, expiration, opts) 51 So(err, ShouldBeNil) 52 So(url, ShouldStartWith, "https://storage.googleapis.com/testBucket/object.txt?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=") 53 }) 54 }) 55 } 56 57 func TestSplit(t *testing.T) { 58 t.Parallel() 59 60 Convey(`Split`, t, func() { 61 Convey(`Valid`, func() { 62 bucket := "testBucket" 63 object := "object.txt" 64 b, o := Split(fmt.Sprintf("gs://%s/%s", bucket, object)) 65 So(b, ShouldEqual, bucket) 66 So(o, ShouldEqual, object) 67 }) 68 69 Convey(`With no object`, func() { 70 bucket := "testBucket" 71 b, o := Split(fmt.Sprintf("gs://%s", bucket)) 72 So(b, ShouldEqual, bucket) 73 So(o, ShouldBeEmpty) 74 }) 75 76 Convey(`With invalid prefix`, func() { 77 bucket := "testBucket" 78 object := "object.txt" 79 path := fmt.Sprintf("xyz://%s/%s", bucket, object) 80 b, o := Split(path) 81 So(b, ShouldBeEmpty) 82 So(o, ShouldEqual, path) 83 }) 84 }) 85 }