add_user_test.go 4.4 KB

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