Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

create_no_session_test.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. "code.gitea.io/gitea/modules/json"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/util"
  14. "code.gitea.io/gitea/routers"
  15. "gitea.com/go-chi/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. defer prepareTestEnv(t)()
  48. oldSessionConfig := setting.SessionConfig.ProviderConfig
  49. defer func() {
  50. setting.SessionConfig.ProviderConfig = oldSessionConfig
  51. c = routers.NormalRoutes()
  52. }()
  53. var config session.Options
  54. err := json.Unmarshal([]byte(oldSessionConfig), &config)
  55. assert.NoError(t, err)
  56. config.Provider = "file"
  57. // Now create a temporaryDirectory
  58. tmpDir, err := os.MkdirTemp("", "sessions")
  59. assert.NoError(t, err)
  60. defer func() {
  61. if _, err := os.Stat(tmpDir); !os.IsNotExist(err) {
  62. _ = util.RemoveAll(tmpDir)
  63. }
  64. }()
  65. config.ProviderConfig = tmpDir
  66. newConfigBytes, err := json.Marshal(config)
  67. assert.NoError(t, err)
  68. setting.SessionConfig.ProviderConfig = string(newConfigBytes)
  69. c = routers.NormalRoutes()
  70. t.Run("NoSessionOnViewIssue", func(t *testing.T) {
  71. defer PrintCurrentTest(t)()
  72. req := NewRequest(t, "GET", "/user2/repo1/issues/1")
  73. resp := MakeRequest(t, req, http.StatusOK)
  74. sessionID := getSessionID(t, resp)
  75. // We're not logged in so there should be no session
  76. assert.False(t, sessionFileExist(t, tmpDir, sessionID))
  77. })
  78. t.Run("CreateSessionOnLogin", func(t *testing.T) {
  79. defer PrintCurrentTest(t)()
  80. req := NewRequest(t, "GET", "/user/login")
  81. resp := MakeRequest(t, req, http.StatusOK)
  82. sessionID := getSessionID(t, resp)
  83. // We're not logged in so there should be no session
  84. assert.False(t, sessionFileExist(t, tmpDir, sessionID))
  85. doc := NewHTMLParser(t, resp.Body)
  86. req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{
  87. "_csrf": doc.GetCSRF(),
  88. "user_name": "user2",
  89. "password": userPassword,
  90. })
  91. resp = MakeRequest(t, req, http.StatusFound)
  92. sessionID = getSessionID(t, resp)
  93. assert.FileExists(t, sessionFile(tmpDir, sessionID))
  94. })
  95. }