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

create_no_session_test.go 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. c = routes.NewChi()
  53. c.Mount("/", routes.NormalRoutes())
  54. routes.DelegateToMacaron(c)
  55. }()
  56. var config session.Options
  57. err := json.Unmarshal([]byte(oldSessionConfig), &config)
  58. assert.NoError(t, err)
  59. config.Provider = "file"
  60. // Now create a temporaryDirectory
  61. tmpDir, err := ioutil.TempDir("", "sessions")
  62. assert.NoError(t, err)
  63. defer func() {
  64. if _, err := os.Stat(tmpDir); !os.IsNotExist(err) {
  65. _ = util.RemoveAll(tmpDir)
  66. }
  67. }()
  68. config.ProviderConfig = tmpDir
  69. newConfigBytes, err := json.Marshal(config)
  70. assert.NoError(t, err)
  71. setting.SessionConfig.ProviderConfig = string(newConfigBytes)
  72. c = routes.NewChi()
  73. c.Mount("/", routes.NormalRoutes())
  74. routes.DelegateToMacaron(c)
  75. t.Run("NoSessionOnViewIssue", func(t *testing.T) {
  76. defer PrintCurrentTest(t)()
  77. req := NewRequest(t, "GET", "/user2/repo1/issues/1")
  78. resp := MakeRequest(t, req, http.StatusOK)
  79. sessionID := getSessionID(t, resp)
  80. // We're not logged in so there should be no session
  81. assert.False(t, sessionFileExist(t, tmpDir, sessionID))
  82. })
  83. t.Run("CreateSessionOnLogin", func(t *testing.T) {
  84. defer PrintCurrentTest(t)()
  85. req := NewRequest(t, "GET", "/user/login")
  86. resp := MakeRequest(t, req, http.StatusOK)
  87. sessionID := getSessionID(t, resp)
  88. // We're not logged in so there should be no session
  89. assert.False(t, sessionFileExist(t, tmpDir, sessionID))
  90. doc := NewHTMLParser(t, resp.Body)
  91. req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{
  92. "_csrf": doc.GetCSRF(),
  93. "user_name": "user2",
  94. "password": userPassword,
  95. })
  96. resp = MakeRequest(t, req, http.StatusFound)
  97. sessionID = getSessionID(t, resp)
  98. assert.FileExists(t, sessionFile(tmpDir, sessionID))
  99. })
  100. }