login_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package auth
  2. import (
  3. "context"
  4. "database/sql"
  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. "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/auth/mocks"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers/test"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  15. )
  16. func TestLoginHandler(t *testing.T) {
  17. t.Parallel()
  18. type req struct {
  19. method string
  20. route string
  21. contentType string
  22. body *dto.LoginRequest
  23. }
  24. var (
  25. id = gofakeit.Uint64()
  26. username = gofakeit.Username()
  27. password = gofakeit.Word()
  28. passwordHash = gofakeit.Word()
  29. token = gofakeit.Word()
  30. testError = gofakeit.Error()
  31. correctReq = req{
  32. method: fiber.MethodPost,
  33. route: "/v1/auth/login",
  34. body: &dto.LoginRequest{
  35. Username: username,
  36. Password: password,
  37. },
  38. contentType: fiber.MIMEApplicationJSON,
  39. }
  40. user = models.User{
  41. ID: id,
  42. Username: username,
  43. Password: passwordHash,
  44. }
  45. )
  46. tests := []struct {
  47. name string
  48. req req
  49. resCode int
  50. resBody interface{}
  51. userRepoMock func(mc *minimock.Controller) UserRepository
  52. authServiceMock func(mc *minimock.Controller) AuthService
  53. }{
  54. {
  55. name: "positive case",
  56. req: correctReq,
  57. resCode: fiber.StatusOK,
  58. resBody: dto.LoginResponse{Token: token},
  59. userRepoMock: func(mc *minimock.Controller) UserRepository {
  60. mock := mocks.NewUserRepositoryMock(mc)
  61. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  62. assert.Equal(mc, username, reqUsername)
  63. }).Return(&user, nil)
  64. return mock
  65. },
  66. authServiceMock: func(mc *minimock.Controller) AuthService {
  67. mock := mocks.NewAuthServiceMock(mc)
  68. mock.IsCorrectPasswordMock.Return(true)
  69. mock.GenerateTokenMock.Expect(user).Return(token, nil)
  70. return mock
  71. },
  72. },
  73. {
  74. name: "negative case - body parse error",
  75. req: req{
  76. method: fiber.MethodPost,
  77. route: "/v1/auth/login",
  78. },
  79. resCode: fiber.StatusBadRequest,
  80. userRepoMock: func(mc *minimock.Controller) UserRepository {
  81. return mocks.NewUserRepositoryMock(mc)
  82. },
  83. authServiceMock: func(mc *minimock.Controller) AuthService {
  84. return mocks.NewAuthServiceMock(mc)
  85. },
  86. },
  87. {
  88. name: "negative case - bad request",
  89. req: req{
  90. method: fiber.MethodPost,
  91. route: "/v1/auth/login",
  92. body: &dto.LoginRequest{
  93. Password: password,
  94. },
  95. contentType: fiber.MIMEApplicationJSON,
  96. },
  97. resCode: fiber.StatusBadRequest,
  98. userRepoMock: func(mc *minimock.Controller) UserRepository {
  99. return mocks.NewUserRepositoryMock(mc)
  100. },
  101. authServiceMock: func(mc *minimock.Controller) AuthService {
  102. return mocks.NewAuthServiceMock(mc)
  103. },
  104. },
  105. {
  106. name: "negative case - generate token error",
  107. req: correctReq,
  108. resCode: fiber.StatusInternalServerError,
  109. userRepoMock: func(mc *minimock.Controller) UserRepository {
  110. mock := mocks.NewUserRepositoryMock(mc)
  111. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  112. assert.Equal(mc, username, reqUsername)
  113. }).Return(&user, nil)
  114. return mock
  115. },
  116. authServiceMock: func(mc *minimock.Controller) AuthService {
  117. mock := mocks.NewAuthServiceMock(mc)
  118. mock.IsCorrectPasswordMock.Expect(password, passwordHash).Return(true)
  119. mock.GenerateTokenMock.Expect(user).Return("", testError)
  120. return mock
  121. },
  122. },
  123. {
  124. name: "negative case - incorrect password",
  125. req: correctReq,
  126. resCode: fiber.StatusForbidden,
  127. userRepoMock: func(mc *minimock.Controller) UserRepository {
  128. mock := mocks.NewUserRepositoryMock(mc)
  129. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  130. assert.Equal(mc, username, reqUsername)
  131. }).Return(&user, nil)
  132. return mock
  133. },
  134. authServiceMock: func(mc *minimock.Controller) AuthService {
  135. mock := mocks.NewAuthServiceMock(mc)
  136. mock.IsCorrectPasswordMock.Expect(password, passwordHash).Return(false)
  137. return mock
  138. },
  139. },
  140. {
  141. name: "negative case - repository error (get user)",
  142. req: correctReq,
  143. resCode: fiber.StatusInternalServerError,
  144. userRepoMock: func(mc *minimock.Controller) UserRepository {
  145. mock := mocks.NewUserRepositoryMock(mc)
  146. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  147. assert.Equal(mc, username, reqUsername)
  148. }).Return(nil, testError)
  149. return mock
  150. },
  151. authServiceMock: func(mc *minimock.Controller) AuthService {
  152. return mocks.NewAuthServiceMock(mc)
  153. },
  154. },
  155. {
  156. name: "negative case - user not found",
  157. req: correctReq,
  158. resCode: fiber.StatusForbidden,
  159. userRepoMock: func(mc *minimock.Controller) UserRepository {
  160. mock := mocks.NewUserRepositoryMock(mc)
  161. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  162. assert.Equal(mc, username, reqUsername)
  163. }).Return(nil, sql.ErrNoRows)
  164. return mock
  165. },
  166. authServiceMock: func(mc *minimock.Controller) AuthService {
  167. return mocks.NewAuthServiceMock(mc)
  168. },
  169. },
  170. }
  171. for _, tt := range tests {
  172. t.Run(tt.name, func(t *testing.T) {
  173. t.Parallel()
  174. mc := minimock.NewController(t)
  175. fiberApp := fiber.New()
  176. fiberApp.Post("/v1/auth/login", LoginHandler(tt.authServiceMock(mc), tt.userRepoMock(mc)))
  177. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, test.ConvertDataToIOReader(tt.req.body))
  178. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  179. fiberRes, _ := fiberApp.Test(fiberReq, test.TestTimeout)
  180. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  181. if tt.resBody != nil {
  182. assert.Equal(t, test.MarshalResponse(tt.resBody), test.ConvertBodyToString(fiberRes.Body))
  183. }
  184. })
  185. }
  186. }