Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add usecase unit test for s3 #24

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions app/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions app/domain/model/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ func (r Region) Prev() Region {
}

const (
// BucketMinLength is the minimum length of the bucket name.
BucketMinLength = 3
// BucketMaxLength is the maximum length of the bucket name.
BucketMaxLength = 63
// MinBucketNameLength is the minimum length of the bucket name.
MinBucketNameLength = 3
// MaxBucketNameLength is the maximum length of the bucket name.
MaxBucketNameLength = 63
// S3Protocol is the protocol of the S3 bucket.
S3Protocol = "s3://"
)
Expand Down Expand Up @@ -255,7 +255,7 @@ func (b Bucket) Validate() error {

// validateLength validates the length of the bucket name.
func (b Bucket) validateLength() error {
if len(b) < BucketMinLength || len(b) > BucketMaxLength {
if len(b) < MinBucketNameLength || len(b) > MaxBucketNameLength {
return fmt.Errorf("s3 bucket name must be between 3 and 63 characters long")
}
return nil
Expand Down
95 changes: 95 additions & 0 deletions app/external/mock/s3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package mock
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
ST1000: at least one file in a package should have a package comment (stylecheck)


import (
"context"

"github.com/nao1215/rainbow/app/domain/service"
)

// S3BucketCreator is a mock of the S3BucketCreator interface.
type S3BucketCreator func(ctx context.Context, input *service.S3BucketCreatorInput) (*service.S3BucketCreatorOutput, error)

// CreateS3Bucket calls the CreateS3BucketFunc.
func (m S3BucketCreator) CreateS3Bucket(ctx context.Context, input *service.S3BucketCreatorInput) (*service.S3BucketCreatorOutput, error) {
return m(ctx, input)
}

// S3BucketLister is a mock of the S3BucketLister interface.
type S3BucketLister func(ctx context.Context, input *service.S3BucketListerInput) (*service.S3BucketListerOutput, error)

// ListS3Buckets calls the ListS3BucketsFunc.
func (m S3BucketLister) ListS3Buckets(ctx context.Context, input *service.S3BucketListerInput) (*service.S3BucketListerOutput, error) {
return m(ctx, input)
}

// S3BucketLocationGetter is a mock of the S3BucketLocationGetter interface.
type S3BucketLocationGetter func(ctx context.Context, input *service.S3BucketLocationGetterInput) (*service.S3BucketLocationGetterOutput, error)

// GetS3BucketLocation calls the GetS3BucketLocationFunc.
func (m S3BucketLocationGetter) GetS3BucketLocation(ctx context.Context, input *service.S3BucketLocationGetterInput) (*service.S3BucketLocationGetterOutput, error) {
return m(ctx, input)
}

// S3ObjectsLister is a mock of the S3ObjectLister interface.
type S3ObjectsLister func(ctx context.Context, input *service.S3ObjectsListerInput) (*service.S3ObjectsListerOutput, error)

// ListS3Objects calls the ListS3ObjectsFunc.
func (m S3ObjectsLister) ListS3Objects(ctx context.Context, input *service.S3ObjectsListerInput) (*service.S3ObjectsListerOutput, error) {
return m(ctx, input)
}

// S3ObjectsDeleter is a mock of the S3ObjectsDeleter interface.
type S3ObjectsDeleter func(ctx context.Context, input *service.S3ObjectsDeleterInput) (*service.S3ObjectsDeleterOutput, error)

// DeleteS3Objects calls the DeleteS3ObjectsFunc.
func (m S3ObjectsDeleter) DeleteS3Objects(ctx context.Context, input *service.S3ObjectsDeleterInput) (*service.S3ObjectsDeleterOutput, error) {
return m(ctx, input)
}

// S3BucketDeleter is a mock of the S3BucketDeleter interface.
type S3BucketDeleter func(ctx context.Context, input *service.S3BucketDeleterInput) (*service.S3BucketDeleterOutput, error)

// DeleteS3Bucket calls the DeleteS3BucketFunc.
func (m S3BucketDeleter) DeleteS3Bucket(ctx context.Context, input *service.S3BucketDeleterInput) (*service.S3BucketDeleterOutput, error) {
return m(ctx, input)
}

// S3ObjectUploader is a mock of the S3ObjectUploader interface.
type S3ObjectUploader func(ctx context.Context, input *service.S3ObjectUploaderInput) (*service.S3ObjectUploaderOutput, error)

// UploadS3Object calls the UploadS3ObjectFunc.
func (m S3ObjectUploader) UploadS3Object(ctx context.Context, input *service.S3ObjectUploaderInput) (*service.S3ObjectUploaderOutput, error) {
return m(ctx, input)
}

// S3BucketPublicAccessBlocker is a mock of the S3BucketPublicAccessBlocker interface.
type S3BucketPublicAccessBlocker func(ctx context.Context, input *service.S3BucketPublicAccessBlockerInput) (*service.S3BucketPublicAccessBlockerOutput, error)

// BlockS3BucketPublicAccess calls the BlockS3BucketPublicAccessFunc.
func (m S3BucketPublicAccessBlocker) BlockS3BucketPublicAccess(ctx context.Context, input *service.S3BucketPublicAccessBlockerInput) (*service.S3BucketPublicAccessBlockerOutput, error) {
return m(ctx, input)
}

// S3BucketPolicySetter is a mock of the S3BucketPolicySetter interface.
type S3BucketPolicySetter func(ctx context.Context, input *service.S3BucketPolicySetterInput) (*service.S3BucketPolicySetterOutput, error)

// SetS3BucketPolicy calls the SetS3BucketPolicyFunc.
func (m S3BucketPolicySetter) SetS3BucketPolicy(ctx context.Context, input *service.S3BucketPolicySetterInput) (*service.S3BucketPolicySetterOutput, error) {
return m(ctx, input)
}

// S3ObjectDownloader is a mock of the S3ObjectDownloader interface.
type S3ObjectDownloader func(ctx context.Context, input *service.S3ObjectDownloaderInput) (*service.S3ObjectDownloaderOutput, error)

// DownloadS3Object calls the DownloadS3ObjectFunc.
func (m S3ObjectDownloader) DownloadS3Object(ctx context.Context, input *service.S3ObjectDownloaderInput) (*service.S3ObjectDownloaderOutput, error) {
return m(ctx, input)
}

// S3ObjectCopier is a mock of the S3ObjectCopier interface.
type S3ObjectCopier func(ctx context.Context, input *service.S3ObjectCopierInput) (*service.S3ObjectCopierOutput, error)

// CopyS3Object calls the CopyS3ObjectFunc.
func (m S3ObjectCopier) CopyS3Object(ctx context.Context, input *service.S3ObjectCopierInput) (*service.S3ObjectCopierOutput, error) {
return m(ctx, input)
}
35 changes: 23 additions & 12 deletions app/interactor/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,32 +239,33 @@ func (s *S3BucketDeleter) DeleteS3Bucket(ctx context.Context, input *usecase.S3B
//nolint:gochecknoglobals
var FileUploaderSet = wire.NewSet(
NewFileUploader,
wire.Struct(new(FileUploaderOptions), "*"),
wire.Bind(new(usecase.FileUploader), new(*FileUploader)),
)

var _ usecase.FileUploader = (*FileUploader)(nil)

// FileUploader is an implementation for FileUploader.
type FileUploader struct {
opts *FileUploaderOptions
}

// FileUploaderOptions is an option struct for FileUploader.
type FileUploaderOptions struct {
service.S3ObjectUploader
}

// NewFileUploader returns a new FileUploader struct.
func NewFileUploader(opts *FileUploaderOptions) *FileUploader {
func NewFileUploader(uploader service.S3ObjectUploader) *FileUploader {
return &FileUploader{
opts: opts,
S3ObjectUploader: uploader,
}
}

// UploadFile uploads a file to external storage.
func (u *FileUploader) UploadFile(ctx context.Context, input *usecase.UploadFileInput) (*usecase.UploadFileOutput, error) {
output, err := u.opts.S3ObjectUploader.UploadS3Object(ctx, &service.S3ObjectUploaderInput{
func (u *FileUploader) UploadFile(ctx context.Context, input *usecase.FileUploaderInput) (*usecase.FileUploaderOutput, error) {
if err := input.Bucket.Validate(); err != nil {
return nil, err
}
if err := input.Region.Validate(); err != nil {
return nil, err
}

output, err := u.S3ObjectUploader.UploadS3Object(ctx, &service.S3ObjectUploaderInput{
Bucket: input.Bucket,
Region: input.Region,
S3Key: input.Key,
Expand All @@ -273,7 +274,7 @@ func (u *FileUploader) UploadFile(ctx context.Context, input *usecase.UploadFile
if err != nil {
return nil, err
}
return &usecase.UploadFileOutput{
return &usecase.FileUploaderOutput{
ContentType: output.ContentType,
ContentLength: output.ContentLength,
}, nil
Expand Down Expand Up @@ -303,6 +304,13 @@ func NewS3BucketPublicAccessBlocker(b service.S3BucketPublicAccessBlocker) *S3Bu

// BlockS3BucketPublicAccess blocks public access to a bucket on S3.
func (s *S3BucketPublicAccessBlocker) BlockS3BucketPublicAccess(ctx context.Context, input *usecase.S3BucketPublicAccessBlockerInput) (*usecase.S3BucketPublicAccessBlockerOutput, error) {
if err := input.Bucket.Validate(); err != nil {
return nil, err
}
if err := input.Region.Validate(); err != nil {
return nil, err
}

if _, err := s.S3BucketPublicAccessBlocker.BlockS3BucketPublicAccess(ctx, &service.S3BucketPublicAccessBlockerInput{
Bucket: input.Bucket,
Region: input.Region,
Expand Down Expand Up @@ -336,6 +344,10 @@ func NewS3BucketPolicySetter(s service.S3BucketPolicySetter) *S3BucketPolicySett

// SetS3BucketPolicy sets a bucket policy on S3.
func (s *S3BucketPolicySetter) SetS3BucketPolicy(ctx context.Context, input *usecase.S3BucketPolicySetterInput) (*usecase.S3BucketPolicySetterOutput, error) {
if err := input.Bucket.Validate(); err != nil {
return nil, err
}

if _, err := s.S3BucketPolicySetter.SetS3BucketPolicy(ctx, &service.S3BucketPolicySetterInput{
Bucket: input.Bucket,
Policy: input.Policy,
Expand Down Expand Up @@ -389,7 +401,6 @@ func (s *S3ObjectDownloader) DownloadS3Object(ctx context.Context, input *usecas
}, nil
}


// S3ObjectCopierSet is a provider set for S3ObjectCopier.
//
//nolint:gochecknoglobals
Expand Down
Loading
Loading