Skip to content

Commit

Permalink
Merge pull request #28 from nao1215/add/unit-test-for-cloudfront-usecase
Browse files Browse the repository at this point in the history
Add unit test for cloudfront interactor
  • Loading branch information
nao1215 authored Jan 7, 2024
2 parents 24fabc8 + b58235d commit e553eac
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 75 deletions.
9 changes: 0 additions & 9 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
---
name: Pull Request
about: Create a pull request to help us improve
title: "[Pull Request] XXX"
labels: ""
assignees: ''

---

## Summary

[Provide a brief overview and purpose of the PR]
Expand Down
23 changes: 23 additions & 0 deletions app/external/mock/cloudfront.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mock

import (
"context"

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

// CloudFrontCreator is a mock of the CloudFrontCreator interface.
type CloudFrontCreator func(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error)

// CreateCloudFront calls the CreateCloudFrontFunc.
func (m CloudFrontCreator) CreateCloudFront(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) {
return m(ctx, input)
}

// OAICreator is a mock of the OAICreator interface.
type OAICreator func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error)

// CreateOAI calls the CreateOAIFunc.
func (m OAICreator) CreateOAI(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) {
return m(ctx, input)
}
4 changes: 4 additions & 0 deletions app/interactor/cloudfront.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func NewCloudFrontCreator(opts *CloudFrontCreatorOptions) *CloudFrontCreator {

// CreateCloudFront creates a CDN.
func (c *CloudFrontCreator) CreateCloudFront(ctx context.Context, input *usecase.CreateCloudFrontInput) (*usecase.CreateCloudFrontOutput, error) {
if err := input.Bucket.Validate(); err != nil {
return nil, err
}

oaiOutput, err := c.opts.OAICreator.CreateOAI(ctx, &service.OAICreatorInput{})
if err != nil {
return nil, err
Expand Down
127 changes: 127 additions & 0 deletions app/interactor/cloudfront_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package interactor

import (
"context"
"errors"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/google/go-cmp/cmp"
"github.com/nao1215/rainbow/app/domain/model"
"github.com/nao1215/rainbow/app/domain/service"
"github.com/nao1215/rainbow/app/external/mock"
"github.com/nao1215/rainbow/app/usecase"
)

func TestCloudFrontCreator_CreateCloudFront(t *testing.T) {
t.Parallel()

t.Run("success to create CloudFront", func(t *testing.T) {
t.Parallel()

oaiCreator := mock.OAICreator(func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) {
return &service.OAICreatorOutput{
ID: aws.String("test-oai-id"),
}, nil
})

cloudFrontCreator := mock.CloudFrontCreator(func(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) {
want := &service.CloudFrontCreatorInput{
Bucket: model.Bucket("test-bucket"),
OAIID: aws.String("test-oai-id"),
}
if diff := cmp.Diff(want, input); diff != "" {
t.Errorf("differs: (-want +got)\n%s", diff)
}
return &service.CloudFrontCreatorOutput{
Domain: model.Domain("test.cloudfront.net"),
}, nil
})

creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{
OAICreator: oaiCreator,
CloudFrontCreator: cloudFrontCreator,
})

got, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{
Bucket: model.Bucket("test-bucket"),
})
if err != nil {
t.Errorf("unexpected error: %v", err)
}

want := &usecase.CreateCloudFrontOutput{
Domain: model.Domain("test.cloudfront.net"),
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("differs: (-want +got)\n%s", diff)
}
})

t.Run("fail to create CloudFront", func(t *testing.T) {
t.Parallel()

oaiCreator := mock.OAICreator(func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) {
return &service.OAICreatorOutput{
ID: aws.String("test-oai-id"),
}, nil
})

cloudFrontCreator := mock.CloudFrontCreator(func(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) {
want := &service.CloudFrontCreatorInput{
Bucket: model.Bucket("test-bucket"),
OAIID: aws.String("test-oai-id"),
}
if diff := cmp.Diff(want, input); diff != "" {
t.Errorf("differs: (-want +got)\n%s", diff)
}
return nil, errors.New("some error")
})

creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{
OAICreator: oaiCreator,
CloudFrontCreator: cloudFrontCreator,
})

_, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{
Bucket: model.Bucket("test-bucket"),
})
if err == nil {
t.Error("expected error")
}
})

t.Run("fail to create OAI", func(t *testing.T) {
t.Parallel()

oaiCreator := mock.OAICreator(func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) {
return nil, errors.New("some error")
})

creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{
OAICreator: oaiCreator,
CloudFrontCreator: nil,
})

if _, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{
Bucket: model.Bucket("test-bucket"),
}); err == nil {
t.Error("expected error")
}
})

t.Run("fail to validate s3 bucket name", func(t *testing.T) {
t.Parallel()

creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{
OAICreator: nil,
CloudFrontCreator: nil,
})

if _, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{
Bucket: model.Bucket(""),
}); err == nil {
t.Error("expected error")
}
})
}
Loading

0 comments on commit e553eac

Please sign in to comment.