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.8KB

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