Skip to content

Commit

Permalink
mod: updated Expiry in PreSignUrlOptions to be a time.Duration instea…
Browse files Browse the repository at this point in the history
…d of an int | removed test case for PreSignUrl expiry validation
  • Loading branch information
denilbhatt0814 committed Jun 11, 2024
1 parent 07157bf commit 21db5e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 47 deletions.
12 changes: 6 additions & 6 deletions api/storage/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ type File interface {
// Delete - Delete this object
Delete(ctx context.Context) error
// UploadUrl - Creates a signed Url for uploading this file reference
UploadUrl(ctx context.Context, expiry int) (string, error)
UploadUrl(ctx context.Context, expiry time.Duration) (string, error)
// DownloadUrl - Creates a signed Url for downloading this file reference
DownloadUrl(ctx context.Context, expiry int) (string, error)
DownloadUrl(ctx context.Context, expiry time.Duration) (string, error)
}

type fileImpl struct {
Expand Down Expand Up @@ -96,7 +96,7 @@ func (o *fileImpl) Delete(ctx context.Context) error {

type PresignUrlOptions struct {
Mode Mode
Expiry int
Expiry time.Duration
}

func (p PresignUrlOptions) isValid() error {
Expand All @@ -107,11 +107,11 @@ func (p PresignUrlOptions) isValid() error {
return nil
}

func (o *fileImpl) UploadUrl(ctx context.Context, expiry int) (string, error) {
func (o *fileImpl) UploadUrl(ctx context.Context, expiry time.Duration) (string, error) {
return o.signUrl(ctx, PresignUrlOptions{Expiry: expiry, Mode: ModeWrite})
}

func (o *fileImpl) DownloadUrl(ctx context.Context, expiry int) (string, error) {
func (o *fileImpl) DownloadUrl(ctx context.Context, expiry time.Duration) (string, error) {
return o.signUrl(ctx, PresignUrlOptions{Expiry: expiry, Mode: ModeRead})
}

Expand All @@ -130,7 +130,7 @@ func (o *fileImpl) signUrl(ctx context.Context, opts PresignUrlOptions) (string,
BucketName: o.bucket,
Key: o.key,
Operation: op,
Expiry: durationpb.New(time.Duration(opts.Expiry) * time.Second),
Expiry: durationpb.New(opts.Expiry),
})
if err != nil {
return "", errors.FromGrpcError(err)
Expand Down
54 changes: 13 additions & 41 deletions api/storage/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ var _ = Describe("File", func() {
})

Describe("UploadUrl()", func() {
var expiry int
var expiry time.Duration

BeforeEach(func() {
expiry = 1 * 24 * 60 * 60 // 1 Day
expiry = 1 * time.Hour // 1 hour
})

When("the PreSignUrl gRPC operation is successful", func() {
Expand All @@ -226,7 +226,7 @@ var _ = Describe("File", func() {
BucketName: bucketName,
Key: fileName,
Operation: v1.StoragePreSignUrlRequest_WRITE,
Expiry: durationpb.New(time.Duration(expiry) * time.Second),
Expiry: durationpb.New(expiry),
}).Return(&v1.StoragePreSignUrlResponse{
Url: url,
}, nil).Times(1)
Expand Down Expand Up @@ -270,10 +270,10 @@ var _ = Describe("File", func() {
})

Describe("DownloadUrl()", func() {
var expiry int
var expiry time.Duration

BeforeEach(func() {
expiry = 1 * 24 * 60 * 60 // 1 Day
expiry = 1 * time.Hour // 1 hour
})

When("the PreSignUrl gRPC operation is successful", func() {
Expand All @@ -286,7 +286,7 @@ var _ = Describe("File", func() {
BucketName: bucketName,
Key: fileName,
Operation: v1.StoragePreSignUrlRequest_READ,
Expiry: durationpb.New(time.Duration(expiry) * time.Second),
Expiry: durationpb.New(expiry),
}).Return(&v1.StoragePreSignUrlResponse{
Url: url,
}, nil).Times(1)
Expand Down Expand Up @@ -368,10 +368,10 @@ var _ = Describe("fileImpl", func() {
})

Describe("signUrl()", func() {
var expiry int
var expiry time.Duration

BeforeEach(func() {
expiry = 1 * 24 * 60 * 60 // 1 day
expiry = 1 * time.Hour // 1 hour
})

When("invalid mode is provided", func() {
Expand All @@ -396,7 +396,7 @@ var _ = Describe("fileImpl", func() {
BucketName: bucketName,
Key: fileName,
Operation: v1.StoragePreSignUrlRequest_READ,
Expiry: durationpb.New(time.Duration(expiry) * time.Second),
Expiry: durationpb.New(expiry),
}).Return(nil, errors.New(errorMsg)).Times(1)
})

Expand Down Expand Up @@ -427,7 +427,7 @@ var _ = Describe("fileImpl", func() {
BucketName: bucketName,
Key: fileName,
Operation: v1.StoragePreSignUrlRequest_WRITE,
Expiry: durationpb.New(time.Duration(expiry) * time.Second),
Expiry: durationpb.New(expiry),
}).Return(&v1.StoragePreSignUrlResponse{
Url: url,
}, nil)
Expand All @@ -448,13 +448,13 @@ var _ = Describe("fileImpl", func() {

var _ = Describe("PresignUrlOptions", func() {
var mode Mode
var expiry int
var expiry time.Duration
var p *PresignUrlOptions

Describe("isValid()", func() {
When("valid mode and expiry are passed", func() {
BeforeEach(func() {
expiry = 1 * 24 * 60 * 60 // 1 day
expiry = 1 * time.Hour // 1 hour
mode = ModeRead

p = &PresignUrlOptions{
Expand All @@ -474,7 +474,7 @@ var _ = Describe("PresignUrlOptions", func() {

BeforeEach(func() {
errorMsg = "invalid mode"
expiry = 1 * 24 * 60 * 60 // 1 day
expiry = 1 * time.Hour // 1 hour

p = &PresignUrlOptions{
Mode: 7,
Expand All @@ -495,33 +495,5 @@ var _ = Describe("PresignUrlOptions", func() {
).To(BeTrue())
})
})

When("invalid expiry is passed", func() {
var errorMsg string

BeforeEach(func() {
errorMsg = "invalid expiry"
expiry = 9999 * 24 * 60 * 60 // 9999 days
mode = ModeRead

p = &PresignUrlOptions{
Mode: mode,
Expiry: expiry,
}
})

It("should return an error", func() {
err := p.isValid()
By("occurance of error")
Expect(err).To(HaveOccurred())

By("containing appropriate error message")
Expect(strings.Contains(
strings.ToLower(err.Error()),
strings.ToLower(errorMsg),
),
).To(BeTrue())
})
})
})
})

0 comments on commit 21db5e3

Please sign in to comment.