add_user_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package user
  2. import (
  3. "context"
  4. "errors"
  5. "net/http/httptest"
  6. "testing"
  7. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  8. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  9. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  10. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  11. repoMocks "git.dmitriygnatenko.ru/dima/homethings/internal/repositories/mocks"
  12. sp "git.dmitriygnatenko.ru/dima/homethings/internal/service_provider"
  13. authMocks "git.dmitriygnatenko.ru/dima/homethings/internal/services/auth/mocks"
  14. "github.com/brianvoe/gofakeit/v6"
  15. "github.com/gofiber/fiber/v2"
  16. "github.com/gojuno/minimock/v3"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func Test_AddUserHandler(t *testing.T) {
  20. type req struct {
  21. method string
  22. route string
  23. contentType string
  24. body *dto.AddUserRequest
  25. }
  26. var (
  27. mc = minimock.NewController(t)
  28. id = gofakeit.Number(1, 1000)
  29. username = gofakeit.Username()
  30. password = gofakeit.Word()
  31. testError = errors.New(gofakeit.Phrase())
  32. correctReq = req{
  33. method: fiber.MethodPost,
  34. route: "/v1/users",
  35. body: &dto.AddUserRequest{
  36. Username: username,
  37. Password: password,
  38. },
  39. contentType: fiber.MIMEApplicationJSON,
  40. }
  41. )
  42. tests := []struct {
  43. name string
  44. req req
  45. resCode int
  46. resBody interface{}
  47. userRepoMock func(mc *minimock.Controller) interfaces.UserRepository
  48. authServiceMock func(mc *minimock.Controller) interfaces.Auth
  49. }{
  50. {
  51. name: "positive case",
  52. req: correctReq,
  53. resCode: fiber.StatusOK,
  54. resBody: dto.EmptyResponse{},
  55. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  56. mock := repoMocks.NewUserRepositoryMock(mc)
  57. mock.AddMock.Inspect(func(ctx context.Context, reqUsername string, reqPassword string) {
  58. assert.Equal(mc, username, reqUsername)
  59. assert.NotEmpty(mc, reqPassword)
  60. }).Return(id, nil)
  61. return mock
  62. },
  63. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  64. mock := authMocks.NewAuthMock(mc)
  65. mock.GeneratePasswordHashMock.Return(password, nil)
  66. return mock
  67. },
  68. },
  69. {
  70. name: "negative case - body parse error",
  71. req: req{
  72. method: fiber.MethodPost,
  73. route: "/v1/users",
  74. },
  75. resCode: fiber.StatusBadRequest,
  76. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  77. return repoMocks.NewUserRepositoryMock(mc)
  78. },
  79. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  80. return authMocks.NewAuthMock(mc)
  81. },
  82. },
  83. {
  84. name: "negative case - bad request",
  85. req: req{
  86. method: fiber.MethodPost,
  87. route: "/v1/users",
  88. body: &dto.AddUserRequest{
  89. Password: password,
  90. },
  91. contentType: fiber.MIMEApplicationJSON,
  92. },
  93. resCode: fiber.StatusBadRequest,
  94. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  95. return repoMocks.NewUserRepositoryMock(mc)
  96. },
  97. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  98. return authMocks.NewAuthMock(mc)
  99. },
  100. },
  101. {
  102. name: "negative case - repository error",
  103. req: correctReq,
  104. resCode: fiber.StatusInternalServerError,
  105. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  106. mock := repoMocks.NewUserRepositoryMock(mc)
  107. mock.AddMock.Inspect(func(ctx context.Context, reqUsername string, reqPassword string) {
  108. assert.Equal(mc, username, reqUsername)
  109. assert.NotEmpty(mc, reqPassword)
  110. }).Return(0, testError)
  111. return mock
  112. },
  113. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  114. mock := authMocks.NewAuthMock(mc)
  115. mock.GeneratePasswordHashMock.Return(password, nil)
  116. return mock
  117. },
  118. },
  119. {
  120. name: "negative case - auth service error",
  121. req: correctReq,
  122. resCode: fiber.StatusInternalServerError,
  123. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  124. return repoMocks.NewUserRepositoryMock(mc)
  125. },
  126. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  127. mock := authMocks.NewAuthMock(mc)
  128. mock.GeneratePasswordHashMock.Return("", testError)
  129. return mock
  130. },
  131. },
  132. }
  133. for _, tt := range tests {
  134. t.Run(tt.name, func(t *testing.T) {
  135. fiberApp := fiber.New()
  136. serviceProvider := sp.InitMock(tt.userRepoMock(mc), tt.authServiceMock(mc))
  137. fiberApp.Post("/v1/users", AddUserHandler(serviceProvider))
  138. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, helpers.ConvertDataToIOReader(tt.req.body))
  139. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  140. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  141. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  142. if tt.resBody != nil {
  143. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  144. }
  145. })
  146. }
  147. }