update_user_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package user
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  7. "testing"
  8. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  9. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  10. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  13. repoMocks "git.dmitriygnatenko.ru/dima/homethings/internal/repositories/mocks"
  14. sp "git.dmitriygnatenko.ru/dima/homethings/internal/service_provider"
  15. authMocks "git.dmitriygnatenko.ru/dima/homethings/internal/services/auth/mocks"
  16. "github.com/brianvoe/gofakeit/v6"
  17. "github.com/gofiber/fiber/v2"
  18. "github.com/gojuno/minimock/v3"
  19. "github.com/golang-jwt/jwt/v4"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. func Test_UpdateUserHandler(t *testing.T) {
  23. type req struct {
  24. method string
  25. route string
  26. contentType string
  27. body *dto.UpdateUserRequest
  28. }
  29. var (
  30. mc = minimock.NewController(t)
  31. id = gofakeit.Number(1, 1000)
  32. username = gofakeit.Username()
  33. password = gofakeit.Word()
  34. newUsername = gofakeit.Username()
  35. newPassword = gofakeit.Word()
  36. testError = errors.New(gofakeit.Phrase())
  37. correctReq = req{
  38. method: fiber.MethodPut,
  39. route: "/v1/users",
  40. body: &dto.UpdateUserRequest{
  41. Username: &newUsername,
  42. Password: &newPassword,
  43. },
  44. contentType: fiber.MIMEApplicationJSON,
  45. }
  46. claims = jwt.MapClaims{
  47. "name": username,
  48. }
  49. user = models.User{
  50. ID: id,
  51. Username: username,
  52. Password: password,
  53. }
  54. )
  55. tests := []struct {
  56. name string
  57. req req
  58. resCode int
  59. resBody interface{}
  60. userRepoMock func(mc *minimock.Controller) interfaces.UserRepository
  61. authServiceMock func(mc *minimock.Controller) interfaces.Auth
  62. }{
  63. {
  64. name: "positive case",
  65. req: correctReq,
  66. resCode: fiber.StatusOK,
  67. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  68. mock := repoMocks.NewUserRepositoryMock(mc)
  69. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  70. assert.Equal(mc, username, reqUsername)
  71. }).Return(&user, nil)
  72. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateUserRequest) {
  73. assert.Equal(mc, id, req.ID)
  74. assert.Equal(mc, newUsername, req.Username.String)
  75. assert.Equal(mc, newPassword, req.Password.String)
  76. }).Return(nil)
  77. return mock
  78. },
  79. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  80. mock := authMocks.NewAuthMock(mc)
  81. mock.GeneratePasswordHashMock.Expect(newPassword).Return(newPassword, nil)
  82. mock.GetClaimsMock.Return(claims)
  83. return mock
  84. },
  85. },
  86. {
  87. name: "negative case - body parse error",
  88. req: req{
  89. method: fiber.MethodPut,
  90. route: "/v1/users",
  91. },
  92. resCode: fiber.StatusBadRequest,
  93. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  94. return repoMocks.NewUserRepositoryMock(mc)
  95. },
  96. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  97. return authMocks.NewAuthMock(mc)
  98. },
  99. },
  100. {
  101. name: "negative case - bad request",
  102. req: req{
  103. method: fiber.MethodPut,
  104. route: "/v1/users",
  105. body: &dto.UpdateUserRequest{},
  106. contentType: fiber.MIMEApplicationJSON,
  107. },
  108. resCode: fiber.StatusBadRequest,
  109. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  110. return repoMocks.NewUserRepositoryMock(mc)
  111. },
  112. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  113. return authMocks.NewAuthMock(mc)
  114. },
  115. },
  116. {
  117. name: "negative case - auth service error",
  118. req: correctReq,
  119. resCode: fiber.StatusInternalServerError,
  120. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  121. mock := repoMocks.NewUserRepositoryMock(mc)
  122. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  123. assert.Equal(mc, username, reqUsername)
  124. }).Return(&user, nil)
  125. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateUserRequest) {
  126. assert.Equal(mc, id, req.ID)
  127. assert.Equal(mc, newUsername, req.Username.String)
  128. assert.Equal(mc, newPassword, req.Password.String)
  129. }).Return(nil)
  130. return mock
  131. },
  132. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  133. mock := authMocks.NewAuthMock(mc)
  134. mock.GeneratePasswordHashMock.Expect(newPassword).Return("", testError)
  135. return mock
  136. },
  137. },
  138. {
  139. name: "negative case - repository error (update)",
  140. req: correctReq,
  141. resCode: fiber.StatusInternalServerError,
  142. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  143. mock := repoMocks.NewUserRepositoryMock(mc)
  144. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  145. assert.Equal(mc, username, reqUsername)
  146. }).Return(&user, nil)
  147. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateUserRequest) {
  148. assert.Equal(mc, id, req.ID)
  149. assert.Equal(mc, newUsername, req.Username.String)
  150. assert.Equal(mc, newPassword, req.Password.String)
  151. }).Return(testError)
  152. return mock
  153. },
  154. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  155. mock := authMocks.NewAuthMock(mc)
  156. mock.GeneratePasswordHashMock.Expect(newPassword).Return(newPassword, nil)
  157. mock.GetClaimsMock.Return(claims)
  158. return mock
  159. },
  160. },
  161. {
  162. name: "negative case - repository error (get user)",
  163. req: correctReq,
  164. resCode: fiber.StatusInternalServerError,
  165. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  166. mock := repoMocks.NewUserRepositoryMock(mc)
  167. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  168. assert.Equal(mc, username, reqUsername)
  169. }).Return(nil, testError)
  170. return mock
  171. },
  172. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  173. mock := authMocks.NewAuthMock(mc)
  174. mock.GeneratePasswordHashMock.Expect(newPassword).Return(newPassword, nil)
  175. mock.GetClaimsMock.Return(claims)
  176. return mock
  177. },
  178. },
  179. {
  180. name: "negative case - repository error (user not found)",
  181. req: correctReq,
  182. resCode: fiber.StatusBadRequest,
  183. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  184. mock := repoMocks.NewUserRepositoryMock(mc)
  185. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  186. assert.Equal(mc, username, reqUsername)
  187. }).Return(nil, sql.ErrNoRows)
  188. return mock
  189. },
  190. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  191. mock := authMocks.NewAuthMock(mc)
  192. mock.GeneratePasswordHashMock.Expect(newPassword).Return(newPassword, nil)
  193. mock.GetClaimsMock.Return(claims)
  194. return mock
  195. },
  196. },
  197. }
  198. for _, tt := range tests {
  199. t.Run(tt.name, func(t *testing.T) {
  200. fiberApp := fiber.New()
  201. serviceProvider := sp.InitMock(tt.userRepoMock(mc), tt.authServiceMock(mc))
  202. fiberApp.Put("/v1/users", UpdateUserHandler(serviceProvider))
  203. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, helpers.ConvertDataToIOReader(tt.req.body))
  204. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  205. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  206. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  207. if tt.resBody != nil {
  208. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  209. }
  210. })
  211. }
  212. }