add_user_test.go 4.4 KB

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