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.

create_no_session_test.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "github.com/go-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. }