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.

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