check_auth_test.go 3.4 KB

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