Skip to content

Commit 1e8c91d

Browse files
committed
Add unit test for cloudfront interactor
1 parent 24fabc8 commit 1e8c91d

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

app/external/mock/cloudfront.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package mock
2+
3+
import (
4+
"context"
5+
6+
"github.com/nao1215/rainbow/app/domain/service"
7+
)
8+
9+
// CloudFrontCreator is a mock of the CloudFrontCreator interface.
10+
type CloudFrontCreator func(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error)
11+
12+
// CreateCloudFront calls the CreateCloudFrontFunc.
13+
func (m CloudFrontCreator) CreateCloudFront(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) {
14+
return m(ctx, input)
15+
}
16+
17+
// OAICreator is a mock of the OAICreator interface.
18+
type OAICreator func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error)
19+
20+
// CreateOAI calls the CreateOAIFunc.
21+
func (m OAICreator) CreateOAI(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) {
22+
return m(ctx, input)
23+
}

app/interactor/cloudfront.go

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ func NewCloudFrontCreator(opts *CloudFrontCreatorOptions) *CloudFrontCreator {
4040

4141
// CreateCloudFront creates a CDN.
4242
func (c *CloudFrontCreator) CreateCloudFront(ctx context.Context, input *usecase.CreateCloudFrontInput) (*usecase.CreateCloudFrontOutput, error) {
43+
if err := input.Bucket.Validate(); err != nil {
44+
return nil, err
45+
}
46+
4347
oaiOutput, err := c.opts.OAICreator.CreateOAI(ctx, &service.OAICreatorInput{})
4448
if err != nil {
4549
return nil, err

app/interactor/cloudfront_test.go

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package interactor
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
8+
"github.com/aws/aws-sdk-go-v2/aws"
9+
"github.com/google/go-cmp/cmp"
10+
"github.com/nao1215/rainbow/app/domain/model"
11+
"github.com/nao1215/rainbow/app/domain/service"
12+
"github.com/nao1215/rainbow/app/external/mock"
13+
"github.com/nao1215/rainbow/app/usecase"
14+
)
15+
16+
func TestCloudFrontCreator_CreateCloudFront(t *testing.T) {
17+
t.Parallel()
18+
19+
t.Run("success to create CloudFront", func(t *testing.T) {
20+
t.Parallel()
21+
22+
oaiCreator := mock.OAICreator(func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) {
23+
return &service.OAICreatorOutput{
24+
ID: aws.String("test-oai-id"),
25+
}, nil
26+
})
27+
28+
cloudFrontCreator := mock.CloudFrontCreator(func(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) {
29+
want := &service.CloudFrontCreatorInput{
30+
Bucket: model.Bucket("test-bucket"),
31+
OAIID: aws.String("test-oai-id"),
32+
}
33+
if diff := cmp.Diff(want, input); diff != "" {
34+
t.Errorf("differs: (-want +got)\n%s", diff)
35+
}
36+
return &service.CloudFrontCreatorOutput{
37+
Domain: model.Domain("test.cloudfront.net"),
38+
}, nil
39+
})
40+
41+
creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{
42+
OAICreator: oaiCreator,
43+
CloudFrontCreator: cloudFrontCreator,
44+
})
45+
46+
got, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{
47+
Bucket: model.Bucket("test-bucket"),
48+
})
49+
if err != nil {
50+
t.Errorf("unexpected error: %v", err)
51+
}
52+
53+
want := &usecase.CreateCloudFrontOutput{
54+
Domain: model.Domain("test.cloudfront.net"),
55+
}
56+
if diff := cmp.Diff(want, got); diff != "" {
57+
t.Errorf("differs: (-want +got)\n%s", diff)
58+
}
59+
})
60+
61+
t.Run("fail to create CloudFront", func(t *testing.T) {
62+
t.Parallel()
63+
64+
oaiCreator := mock.OAICreator(func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) {
65+
return &service.OAICreatorOutput{
66+
ID: aws.String("test-oai-id"),
67+
}, nil
68+
})
69+
70+
cloudFrontCreator := mock.CloudFrontCreator(func(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) {
71+
want := &service.CloudFrontCreatorInput{
72+
Bucket: model.Bucket("test-bucket"),
73+
OAIID: aws.String("test-oai-id"),
74+
}
75+
if diff := cmp.Diff(want, input); diff != "" {
76+
t.Errorf("differs: (-want +got)\n%s", diff)
77+
}
78+
return nil, errors.New("some error")
79+
})
80+
81+
creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{
82+
OAICreator: oaiCreator,
83+
CloudFrontCreator: cloudFrontCreator,
84+
})
85+
86+
_, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{
87+
Bucket: model.Bucket("test-bucket"),
88+
})
89+
if err == nil {
90+
t.Error("expected error")
91+
}
92+
})
93+
94+
t.Run("fail to create OAI", func(t *testing.T) {
95+
t.Parallel()
96+
97+
oaiCreator := mock.OAICreator(func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) {
98+
return nil, errors.New("some error")
99+
})
100+
101+
creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{
102+
OAICreator: oaiCreator,
103+
CloudFrontCreator: nil,
104+
})
105+
106+
if _, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{
107+
Bucket: model.Bucket("test-bucket"),
108+
}); err == nil {
109+
t.Error("expected error")
110+
}
111+
})
112+
113+
t.Run("fail to validate s3 bucket name", func(t *testing.T) {
114+
t.Parallel()
115+
116+
creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{
117+
OAICreator: nil,
118+
CloudFrontCreator: nil,
119+
})
120+
121+
if _, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{
122+
Bucket: model.Bucket(""),
123+
}); err == nil {
124+
t.Error("expected error")
125+
}
126+
})
127+
}

0 commit comments

Comments
 (0)