check_auth_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package auth
  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_CheckAuthHandler(t *testing.T) {
  23. type req struct {
  24. method string
  25. route string
  26. contentType string
  27. }
  28. var (
  29. mc = minimock.NewController(t)
  30. username = gofakeit.Username()
  31. testError = errors.New(gofakeit.Phrase())
  32. correctReq = req{
  33. method: fiber.MethodGet,
  34. route: "/v1/auth/check",
  35. contentType: fiber.MIMEApplicationJSON,
  36. }
  37. claims = jwt.MapClaims{
  38. "name": username,
  39. }
  40. user = models.User{
  41. Username: username,
  42. }
  43. expectedRes = dto.UserResponse{Username: username}
  44. )
  45. tests := []struct {
  46. name string
  47. req req
  48. resCode int
  49. resBody interface{}
  50. userRepoMock func(mc *minimock.Controller) interfaces.UserRepository
  51. authServiceMock func(mc *minimock.Controller) interfaces.Auth
  52. }{
  53. {
  54. name: "positive case",
  55. req: correctReq,
  56. resCode: fiber.StatusOK,
  57. resBody: expectedRes,
  58. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  59. mock := repoMocks.NewUserRepositoryMock(mc)
  60. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  61. assert.Equal(mc, username, reqUsername)
  62. }).Return(&user, nil)
  63. return mock
  64. },
  65. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  66. mock := authMocks.NewAuthMock(mc)
  67. mock.GetClaimsMock.Return(claims)
  68. return mock
  69. },
  70. },
  71. {
  72. name: "negative case - user not found",
  73. req: correctReq,
  74. resCode: fiber.StatusForbidden,
  75. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  76. mock := repoMocks.NewUserRepositoryMock(mc)
  77. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  78. assert.Equal(mc, username, reqUsername)
  79. }).Return(nil, sql.ErrNoRows)
  80. return mock
  81. },
  82. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  83. mock := authMocks.NewAuthMock(mc)
  84. mock.GetClaimsMock.Return(claims)
  85. return mock
  86. },
  87. },
  88. {
  89. name: "negative case - repository error",
  90. req: correctReq,
  91. resCode: fiber.StatusInternalServerError,
  92. userRepoMock: func(mc *minimock.Controller) interfaces.UserRepository {
  93. mock := repoMocks.NewUserRepositoryMock(mc)
  94. mock.GetMock.Inspect(func(ctx context.Context, reqUsername string) {
  95. assert.Equal(mc, username, reqUsername)
  96. }).Return(nil, testError)
  97. return mock
  98. },
  99. authServiceMock: func(mc *minimock.Controller) interfaces.Auth {
  100. mock := authMocks.NewAuthMock(mc)
  101. mock.GetClaimsMock.Return(claims)
  102. return mock
  103. },
  104. },
  105. }
  106. for _, tt := range tests {
  107. t.Run(tt.name, func(t *testing.T) {
  108. fiberApp := fiber.New()
  109. serviceProvider := sp.InitMock(tt.userRepoMock(mc), tt.authServiceMock(mc))
  110. fiberApp.Get("/v1/auth/check", CheckAuthHandler(serviceProvider))
  111. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  112. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  113. if tt.resBody != nil {
  114. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  115. }
  116. })
  117. }
  118. }