Skip to content

Commit

Permalink
add testing storage service
Browse files Browse the repository at this point in the history
  • Loading branch information
febrihidayan committed Jan 27, 2024
1 parent a6381c4 commit f394f54
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 14 deletions.
2 changes: 1 addition & 1 deletion services/storage/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ func initHandler(
router *mux.Router,
cfg *config.StorageConfig) {

cloud_handler.CloudHttpHandler(router, cfg, mongoFactory, *awsService)
cloud_handler.NewCloudHttpHandler(router, cfg, mongoFactory, *awsService)
}
16 changes: 10 additions & 6 deletions services/storage/internal/delivery/http/delivery/cloud/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"

"github.com/febrihidayan/go-architecture-monorepo/pkg/exceptions"
"github.com/febrihidayan/go-architecture-monorepo/pkg/utils"
Expand All @@ -15,10 +17,10 @@ import (
"github.com/febrihidayan/go-architecture-monorepo/services/storage/internal/delivery/http/response"
)

func (x *cloudHttpHandler) Create(w http.ResponseWriter, r *http.Request) {
func (x *CloudHttpHandler) Create(w http.ResponseWriter, r *http.Request) {
var (
ctx = context.Background()
maxUpload = int64(x.cfg.MaxUpload) * 1024 * 1024
maxUpload = int64(x.Cfg.MaxUpload) * 1024 * 1024
)

jwtToken, errJwt := utils.DecodeJwtToken(r.Header.Get("Authorization"))
Expand All @@ -34,7 +36,7 @@ func (x *cloudHttpHandler) Create(w http.ResponseWriter, r *http.Request) {
}

if handler.Size > maxUpload {
errMessageSize := fmt.Sprintf("max file size %vMB", x.cfg.MaxUpload)
errMessageSize := fmt.Sprintf("max file size %vMB", x.Cfg.MaxUpload)
utils.RespondWithError(w, http.StatusBadRequest, []error{errors.New(errMessageSize)})
return
}
Expand All @@ -45,7 +47,7 @@ func (x *cloudHttpHandler) Create(w http.ResponseWriter, r *http.Request) {
return
}

tempFile, err := os.CreateTemp("temp-assets", "*."+mime.String())
tempFile, err := os.CreateTemp(filepath.Dir(handler.Filename)+"/temp-assets", "*."+mime.String())
if err != nil {
fmt.Println(err)
}
Expand All @@ -67,11 +69,13 @@ func (x *cloudHttpHandler) Create(w http.ResponseWriter, r *http.Request) {
Size: int(handler.Size),
MimeType: mime.String(),
ContentType: mime.Type(),
Directory: x.cfg.Aws.Directory,
Directory: x.Cfg.Aws.Directory,
},
}

cloud, errCreate := x.cloudUsecase.Create(ctx, payload)
log.Println("payload:", payload)

cloud, errCreate := x.CloudUsecase.Create(ctx, payload)
if err != nil {
utils.RespondWithError(w, exceptions.MapToHttpStatusCode(errCreate.Status), errCreate.Errors.Errors)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import (
"github.com/gorilla/mux"
)

type cloudHttpHandler struct {
cfg *config.StorageConfig
cloudUsecase usecases.CloudUsecase
type CloudHttpHandler struct {
Cfg *config.StorageConfig
CloudUsecase usecases.CloudUsecase
}

func CloudHttpHandler(
func NewCloudHttpHandler(
r *mux.Router,
config *config.StorageConfig,
mongoFactory *factories.MongoFactory,
awsService services.AwsService,
) {
handler := &cloudHttpHandler{
cfg: config,
cloudUsecase: cloud.NewCloudInteractor(
handler := &CloudHttpHandler{
Cfg: config,
CloudUsecase: cloud.NewCloudInteractor(
config,
mongoFactory,
&awsService,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cloud

import (
"bytes"
"encoding/json"
"errors"
"net/http/httptest"
"testing"

"github.com/febrihidayan/go-architecture-monorepo/pkg/common"
"github.com/febrihidayan/go-architecture-monorepo/pkg/exceptions"
"github.com/febrihidayan/go-architecture-monorepo/services/storage/internal/config"
cloud_http "github.com/febrihidayan/go-architecture-monorepo/services/storage/internal/delivery/http/delivery/cloud"
"github.com/febrihidayan/go-architecture-monorepo/services/storage/tests/mocks/usecases"
"github.com/gorilla/mux"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)

type CloudHandlerSuite struct {
suite.Suite
Route *mux.Router
Cfg *config.StorageConfig
Http *cloud_http.CloudHttpHandler
Response *httptest.ResponseRecorder
CloudUsecase *usecases.CloudUsecaseMock
Error *exceptions.CustomError
Token string
Id common.ID
}

func (x *CloudHandlerSuite) SetupTest() {
x.Cfg = &config.StorageConfig{
MaxUpload: 15,
}

x.CloudUsecase = new(usecases.CloudUsecaseMock)

x.Response = httptest.NewRecorder()
x.Route = mux.NewRouter()

x.Http = &cloud_http.CloudHttpHandler{
Cfg: x.Cfg,
CloudUsecase: x.CloudUsecase,
}

x.Error = &exceptions.CustomError{
Status: exceptions.ERRREPOSITORY,
Errors: multierror.Append(errors.New(mock.Anything)),
}

x.Token = "Bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InNpbTIifQ.eyJleHAiOjE3MDM4NjI3NTgsImp0aSI6Im1uYjIzdmNzcnQ3NTZ5dWlvbW5idmN4OThlcnR5dWlvcCIsInJvbGVzIjpbInN1cGVyYWRtaW5pc3RyYXRvciJdLCJzdWIiOiIzYzkyYzE4MC1mOGE0LTQ0ZGMtYWJlZS00ZDg2ODhmNDBjYWUifQ.aMKIC5uTqAxEEAhZ-aqvuhDYfc56M_6ZukUGImoxdLs"
x.Id = common.NewID()

}

func TestAclHandler(t *testing.T) {
suite.Run(t, new(CloudHandlerSuite))
}

type Any []interface{}

func (a Any) Get(i int) interface{} {
return a[i]
}

type HandlerParams struct {
method string
path string
payload Any
expected int
}

func BodyHelper(v any) *bytes.Buffer {
b, _ := json.Marshal(v)
return bytes.NewBuffer(b)
}
57 changes: 57 additions & 0 deletions services/storage/tests/mocks/usecases/cloud_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package usecases

import (
"context"

"github.com/febrihidayan/go-architecture-monorepo/pkg/exceptions"
"github.com/febrihidayan/go-architecture-monorepo/services/storage/domain/entities"
"github.com/stretchr/testify/mock"
)

type CloudUsecaseMock struct {
mock.Mock
}

func (x *CloudUsecaseMock) Create(ctx context.Context, payload entities.CloudDto) (result *entities.Cloud, err *exceptions.CustomError) {
args := x.Called(payload)

if n, ok := args.Get(0).(*entities.Cloud); ok {
result = n
}

if n, ok := args.Get(1).(*exceptions.CustomError); ok {
err = n
}

return
}

func (x *CloudUsecaseMock) DeleteAllStatusJob(ctx context.Context) (err *exceptions.CustomError) {
args := x.Called()

if n, ok := args.Get(0).(*exceptions.CustomError); ok {
err = n
}

return
}

func (x *CloudUsecaseMock) Deletes(ctx context.Context, payloads []*entities.Cloud) (err *exceptions.CustomError) {
args := x.Called(payloads)

if n, ok := args.Get(0).(*exceptions.CustomError); ok {
err = n
}

return
}

func (x *CloudUsecaseMock) UpdateStatus(ctx context.Context, payloads []*entities.Cloud) (err *exceptions.CustomError) {
args := x.Called(payloads)

if n, ok := args.Get(0).(*exceptions.CustomError); ok {
err = n
}

return
}

0 comments on commit f394f54

Please sign in to comment.