code.gitea.io/gitea@v1.21.7/tests/integration/create_no_session_test.go (about)

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