2
0

handler_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/apitest"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  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, true)
  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 TestUnauthenticatedUserWithCookieRedirect(t *testing.T) {
  41. _, handler, next := loadHandlersWithRedirect(t)
  42. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. rr := httptest.NewRecorder()
  47. // make the request without a cookie set
  48. handler.ServeHTTP(rr, req)
  49. assert.Equal(t, http.StatusFound, rr.Result().StatusCode)
  50. gotLoc, err := rr.Result().Location()
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. assert.Equal(t, "/dashboard", gotLoc.Path)
  55. assert.False(t, next.WasCalled, "next handler should not have been called")
  56. }
  57. func TestAuthenticatedUserWithToken(t *testing.T) {
  58. config, handler, next := loadHandlers(t)
  59. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. rr := httptest.NewRecorder()
  64. // create a new user for the token to reference
  65. user := apitest.CreateTestUser(t, config, true)
  66. tokenStr := apitest.AuthenticateUserWithToken(t, config, user.ID)
  67. req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tokenStr))
  68. handler.ServeHTTP(rr, req)
  69. assertNextHandlerCalled(t, next, rr, user)
  70. }
  71. func TestUnauthenticatedUserWithToken(t *testing.T) {
  72. _, handler, next := loadHandlers(t)
  73. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. rr := httptest.NewRecorder()
  78. // create a new user and a cookie for them
  79. tokenStr := "badtokenstring"
  80. req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tokenStr))
  81. handler.ServeHTTP(rr, req)
  82. assertForbiddenError(t, next, rr)
  83. }
  84. func TestAuthBadDatabaseRead(t *testing.T) {
  85. config, handler, next := loadHandlers(t)
  86. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. rr := httptest.NewRecorder()
  91. // create a new user and a cookie for them
  92. user := apitest.CreateTestUser(t, config, true)
  93. cookie := apitest.AuthenticateUserWithCookie(t, config, user, false)
  94. req.AddCookie(cookie)
  95. // set the repository interface to one that can't query from the db
  96. configLoader := apitest.NewTestConfigLoader(false)
  97. config, err = configLoader.LoadConfig()
  98. factory := authn.NewAuthNFactory(config)
  99. handler = factory.NewAuthenticated(next)
  100. handler.ServeHTTP(rr, req)
  101. assertForbiddenError(t, next, rr)
  102. }
  103. func TestAuthBadSessionUserWrite(t *testing.T) {
  104. config, handler, next := loadHandlers(t)
  105. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. rr := httptest.NewRecorder()
  110. // create a new user and a cookie for them
  111. apitest.CreateTestUser(t, config, true)
  112. // create cookie where session values are incorrect
  113. // i.e. written for a user that doesn't exist (id 500)
  114. cookie := apitest.AuthenticateUserWithCookie(t, config, &models.User{
  115. Model: gorm.Model{
  116. ID: 500,
  117. },
  118. }, false)
  119. req.AddCookie(cookie)
  120. handler.ServeHTTP(rr, req)
  121. assertForbiddenError(t, next, rr)
  122. }
  123. func TestAuthBadSessionUserIDType(t *testing.T) {
  124. config, handler, next := loadHandlers(t)
  125. req, err := http.NewRequest("GET", "/auth-endpoint", nil)
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. rr := httptest.NewRecorder()
  130. // create a new user and a cookie for them
  131. user := apitest.CreateTestUser(t, config, true)
  132. // create cookie where session values are incorrect
  133. // i.e. written for a user that doesn't exist (id 500)
  134. cookie := apitest.AuthenticateUserWithCookie(t, config, user, true)
  135. req.AddCookie(cookie)
  136. handler.ServeHTTP(rr, req)
  137. assertForbiddenError(t, next, rr)
  138. }
  139. type testHandler struct {
  140. WasCalled bool
  141. User *models.User
  142. }
  143. func (t *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  144. t.WasCalled = true
  145. user, _ := r.Context().Value(types.UserScope).(*models.User)
  146. t.User = user
  147. }
  148. func loadHandlers(t *testing.T) (*config.Config, http.Handler, *testHandler) {
  149. config := apitest.LoadConfig(t)
  150. factory := authn.NewAuthNFactory(config)
  151. next := &testHandler{}
  152. handler := factory.NewAuthenticated(next)
  153. return config, handler, next
  154. }
  155. func loadHandlersWithRedirect(t *testing.T) (*config.Config, http.Handler, *testHandler) {
  156. config := apitest.LoadConfig(t)
  157. factory := authn.NewAuthNFactory(config)
  158. next := &testHandler{}
  159. handler := factory.NewAuthenticatedWithRedirect(next)
  160. return config, handler, next
  161. }
  162. func assertForbiddenError(t *testing.T, next *testHandler, rr *httptest.ResponseRecorder) {
  163. assert := assert.New(t)
  164. // first assert that that the next middleware was not called
  165. assert.False(next.WasCalled, "next handler should not have been called")
  166. apitest.AssertResponseForbidden(t, rr)
  167. }
  168. func assertNextHandlerCalled(
  169. t *testing.T,
  170. next *testHandler,
  171. rr *httptest.ResponseRecorder,
  172. expUser *models.User,
  173. ) {
  174. // make sure the handler was called with the expected user, and resulted in 200 OK
  175. assert := assert.New(t)
  176. assert.True(next.WasCalled, "next handler should have been called")
  177. assert.Equal(expUser, next.User, "user should be equal")
  178. assert.Equal(http.StatusOK, rr.Result().StatusCode, "status code should be ok")
  179. }