|
| 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