handler_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package authn_test
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/porter-dev/porter/api/server/authn"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apitest"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. "github.com/stretchr/testify/assert"
  13. "gorm.io/gorm"
  14. )
  15. func TestAuthenticatedUserWithCookie(t *testing.T) {
  16. config, handler, next := loadHandlers(t)
  17. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. rr := httptest.NewRecorder()
  22. // create a new user and a cookie for them
  23. user := apitest.CreateTestUser(t, config)
  24. cookie := apitest.AuthenticateUserWithCookie(t, config, user, false)
  25. req.AddCookie(cookie)
  26. handler.ServeHTTP(rr, req)
  27. assertNextHandlerCalled(t, next, rr, user)
  28. }
  29. func TestUnauthenticatedUserWithCookie(t *testing.T) {
  30. _, handler, next := loadHandlers(t)
  31. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. rr := httptest.NewRecorder()
  36. // make the request without a cookie set
  37. handler.ServeHTTP(rr, req)
  38. assertForbiddenError(t, next, rr)
  39. }
  40. func TestAuthenticatedUserWithToken(t *testing.T) {
  41. config, handler, next := loadHandlers(t)
  42. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. rr := httptest.NewRecorder()
  47. // create a new user for the token to reference
  48. user := apitest.CreateTestUser(t, config)
  49. tokenStr := apitest.AuthenticateUserWithToken(t, config, user.ID)
  50. req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tokenStr))
  51. handler.ServeHTTP(rr, req)
  52. assertNextHandlerCalled(t, next, rr, user)
  53. }
  54. func TestUnauthenticatedUserWithToken(t *testing.T) {
  55. _, handler, next := loadHandlers(t)
  56. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. rr := httptest.NewRecorder()
  61. // create a new user and a cookie for them
  62. tokenStr := "badtokenstring"
  63. req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tokenStr))
  64. handler.ServeHTTP(rr, req)
  65. assertForbiddenError(t, next, rr)
  66. }
  67. func TestAuthBadDatabaseRead(t *testing.T) {
  68. config, handler, next := loadHandlers(t)
  69. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. rr := httptest.NewRecorder()
  74. // create a new user and a cookie for them
  75. user := apitest.CreateTestUser(t, config)
  76. cookie := apitest.AuthenticateUserWithCookie(t, config, user, false)
  77. req.AddCookie(cookie)
  78. // set the repository interface to one that can't query from the db
  79. configLoader := apitest.NewTestConfigLoader(false)
  80. config, err = configLoader.LoadConfig()
  81. factory := authn.NewAuthNFactory(config)
  82. handler = factory.NewAuthenticated(next)
  83. handler.ServeHTTP(rr, req)
  84. assertForbiddenError(t, next, rr)
  85. }
  86. func TestAuthBadSessionUserWrite(t *testing.T) {
  87. config, handler, next := loadHandlers(t)
  88. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. rr := httptest.NewRecorder()
  93. // create a new user and a cookie for them
  94. apitest.CreateTestUser(t, config)
  95. // create cookie where session values are incorrect
  96. // i.e. written for a user that doesn't exist (id 500)
  97. cookie := apitest.AuthenticateUserWithCookie(t, config, &models.User{
  98. Model: gorm.Model{
  99. ID: 500,
  100. },
  101. }, false)
  102. req.AddCookie(cookie)
  103. handler.ServeHTTP(rr, req)
  104. assertForbiddenError(t, next, rr)
  105. }
  106. func TestAuthBadSessionUserIDType(t *testing.T) {
  107. config, handler, next := loadHandlers(t)
  108. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. rr := httptest.NewRecorder()
  113. // create a new user and a cookie for them
  114. user := apitest.CreateTestUser(t, config)
  115. // create cookie where session values are incorrect
  116. // i.e. written for a user that doesn't exist (id 500)
  117. cookie := apitest.AuthenticateUserWithCookie(t, config, user, true)
  118. req.AddCookie(cookie)
  119. handler.ServeHTTP(rr, req)
  120. assertForbiddenError(t, next, rr)
  121. }
  122. type testHandler struct {
  123. WasCalled bool
  124. User *models.User
  125. }
  126. func (t *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  127. t.WasCalled = true
  128. user, _ := r.Context().Value(types.UserScope).(*models.User)
  129. t.User = user
  130. }
  131. func loadHandlers(t *testing.T) (*shared.Config, http.Handler, *testHandler) {
  132. config := apitest.LoadConfig(t)
  133. factory := authn.NewAuthNFactory(config)
  134. next := &testHandler{}
  135. handler := factory.NewAuthenticated(next)
  136. return config, handler, next
  137. }
  138. func assertForbiddenError(t *testing.T, next *testHandler, rr *httptest.ResponseRecorder) {
  139. assert := assert.New(t)
  140. // first assert that that the next middleware was not called
  141. assert.False(next.WasCalled, "next handler should not have been called")
  142. apitest.AssertResponseForbidden(t, rr)
  143. }
  144. func assertNextHandlerCalled(
  145. t *testing.T,
  146. next *testHandler,
  147. rr *httptest.ResponseRecorder,
  148. expUser *models.User,
  149. ) {
  150. // make sure the handler was called with the expected user, and resulted in 200 OK
  151. assert := assert.New(t)
  152. assert.True(next.WasCalled, "next handler should have been called")
  153. assert.Equal(expUser, next.User, "user should be equal")
  154. assert.Equal(http.StatusOK, rr.Result().StatusCode, "status code should be ok")
  155. }