check_auth_test.go 3.5 KB

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