You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.9 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "encoding/json"
  7. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/routers/routes"
  15. "gitea.com/macaron/session"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func getSessionID(t *testing.T, resp *httptest.ResponseRecorder) string {
  19. cookies := resp.Result().Cookies()
  20. found := false
  21. sessionID := ""
  22. for _, cookie := range cookies {
  23. if cookie.Name == setting.SessionConfig.CookieName {
  24. sessionID = cookie.Value
  25. found = true
  26. }
  27. }
  28. assert.True(t, found)
  29. assert.NotEmpty(t, sessionID)
  30. return sessionID
  31. }
  32. func sessionFile(tmpDir, sessionID string) string {
  33. return filepath.Join(tmpDir, sessionID[0:1], sessionID[1:2], sessionID)
  34. }
  35. func sessionFileExist(t *testing.T, tmpDir, sessionID string) bool {
  36. sessionFile := sessionFile(tmpDir, sessionID)
  37. _, err := os.Lstat(sessionFile)
  38. if err != nil {
  39. if os.IsNotExist(err) {
  40. return false
  41. }
  42. assert.NoError(t, err)
  43. }
  44. return true
  45. }
  46. func TestSessionFileCreation(t *testing.T) {
  47. prepareTestEnv(t)
  48. oldSessionConfig := setting.SessionConfig.ProviderConfig
  49. defer func() {
  50. setting.SessionConfig.ProviderConfig = oldSessionConfig
  51. mac = routes.NewMacaron()
  52. routes.RegisterRoutes(mac)
  53. }()
  54. var config session.Options
  55. err := json.Unmarshal([]byte(oldSessionConfig), &config)
  56. assert.NoError(t, err)
  57. config.Provider = "file"
  58. // Now create a temporaryDirectory
  59. tmpDir, err := ioutil.TempDir("", "sessions")
  60. assert.NoError(t, err)
  61. defer func() {
  62. if _, err := os.Stat(tmpDir); !os.IsNotExist(err) {
  63. _ = os.RemoveAll(tmpDir)
  64. }
  65. }()
  66. config.ProviderConfig = tmpDir
  67. newConfigBytes, err := json.Marshal(config)
  68. assert.NoError(t, err)
  69. setting.SessionConfig.ProviderConfig = string(newConfigBytes)
  70. mac = routes.NewMacaron()
  71. routes.RegisterRoutes(mac)
  72. t.Run("NoSessionOnViewIssue", func(t *testing.T) {
  73. PrintCurrentTest(t)
  74. req := NewRequest(t, "GET", "/user2/repo1/issues/1")
  75. resp := MakeRequest(t, req, http.StatusOK)
  76. sessionID := getSessionID(t, resp)
  77. // We're not logged in so there should be no session
  78. assert.False(t, sessionFileExist(t, tmpDir, sessionID))
  79. })
  80. t.Run("CreateSessionOnLogin", func(t *testing.T) {
  81. PrintCurrentTest(t)
  82. req := NewRequest(t, "GET", "/user/login")
  83. resp := MakeRequest(t, req, http.StatusOK)
  84. sessionID := getSessionID(t, resp)
  85. // We're not logged in so there should be no session
  86. assert.False(t, sessionFileExist(t, tmpDir, sessionID))
  87. doc := NewHTMLParser(t, resp.Body)
  88. req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{
  89. "_csrf": doc.GetCSRF(),
  90. "user_name": "user2",
  91. "password": userPassword,
  92. })
  93. resp = MakeRequest(t, req, http.StatusFound)
  94. sessionID = getSessionID(t, resp)
  95. assert.FileExists(t, sessionFile(tmpDir, sessionID))
  96. })
  97. }