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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "context"
  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/routers"
  14. "code.gitea.io/gitea/tests"
  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 tests.PrepareTestEnv(t)()
  48. oldSessionConfig := setting.SessionConfig.ProviderConfig
  49. defer func() {
  50. setting.SessionConfig.ProviderConfig = oldSessionConfig
  51. c = routers.NormalRoutes(context.TODO())
  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 := t.TempDir()
  59. config.ProviderConfig = tmpDir
  60. newConfigBytes, err := json.Marshal(config)
  61. assert.NoError(t, err)
  62. setting.SessionConfig.ProviderConfig = string(newConfigBytes)
  63. c = routers.NormalRoutes(context.TODO())
  64. t.Run("NoSessionOnViewIssue", func(t *testing.T) {
  65. defer tests.PrintCurrentTest(t)()
  66. req := NewRequest(t, "GET", "/user2/repo1/issues/1")
  67. resp := MakeRequest(t, req, http.StatusOK)
  68. sessionID := getSessionID(t, resp)
  69. // We're not logged in so there should be no session
  70. assert.False(t, sessionFileExist(t, tmpDir, sessionID))
  71. })
  72. t.Run("CreateSessionOnLogin", func(t *testing.T) {
  73. defer tests.PrintCurrentTest(t)()
  74. req := NewRequest(t, "GET", "/user/login")
  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. doc := NewHTMLParser(t, resp.Body)
  80. req = NewRequestWithValues(t, "POST", "/user/login", map[string]string{
  81. "_csrf": doc.GetCSRF(),
  82. "user_name": "user2",
  83. "password": userPassword,
  84. })
  85. resp = MakeRequest(t, req, http.StatusSeeOther)
  86. sessionID = getSessionID(t, resp)
  87. assert.FileExists(t, sessionFile(tmpDir, sessionID))
  88. })
  89. }