update_user_test.go 6.6 KB

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