gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

session_test.go (2790B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package middleware_test
     19 
     20 import (
     21 	"testing"
     22 
     23 	"github.com/stretchr/testify/suite"
     24 	"github.com/superseriousbusiness/gotosocial/internal/config"
     25 	"github.com/superseriousbusiness/gotosocial/internal/middleware"
     26 	"github.com/superseriousbusiness/gotosocial/testrig"
     27 )
     28 
     29 type SessionTestSuite struct {
     30 	suite.Suite
     31 }
     32 
     33 func (suite *SessionTestSuite) SetupTest() {
     34 	testrig.InitTestConfig()
     35 }
     36 
     37 func (suite *SessionTestSuite) TestDeriveSessionNameLocalhostWithPort() {
     38 	config.SetProtocol("http")
     39 	config.SetHost("localhost:8080")
     40 
     41 	sessionName, err := middleware.SessionName()
     42 	suite.NoError(err)
     43 	suite.Equal("gotosocial-localhost", sessionName)
     44 }
     45 
     46 func (suite *SessionTestSuite) TestDeriveSessionNameLocalhost() {
     47 	config.SetProtocol("http")
     48 	config.SetHost("localhost")
     49 
     50 	sessionName, err := middleware.SessionName()
     51 	suite.NoError(err)
     52 	suite.Equal("gotosocial-localhost", sessionName)
     53 }
     54 
     55 func (suite *SessionTestSuite) TestDeriveSessionNoProtocol() {
     56 	config.SetProtocol("")
     57 	config.SetHost("localhost")
     58 
     59 	sessionName, err := middleware.SessionName()
     60 	suite.EqualError(err, "parse \"://localhost\": missing protocol scheme")
     61 	suite.Equal("", sessionName)
     62 }
     63 
     64 func (suite *SessionTestSuite) TestDeriveSessionNoHost() {
     65 	config.SetProtocol("https")
     66 	config.SetHost("")
     67 	config.SetPort(0)
     68 
     69 	sessionName, err := middleware.SessionName()
     70 	suite.EqualError(err, "could not derive hostname without port from https://")
     71 	suite.Equal("", sessionName)
     72 }
     73 
     74 func (suite *SessionTestSuite) TestDeriveSessionOK() {
     75 	config.SetProtocol("https")
     76 	config.SetHost("example.org")
     77 
     78 	sessionName, err := middleware.SessionName()
     79 	suite.NoError(err)
     80 	suite.Equal("gotosocial-example.org", sessionName)
     81 }
     82 
     83 func (suite *SessionTestSuite) TestDeriveSessionIDNOK() {
     84 	config.SetProtocol("https")
     85 	config.SetHost("fóid.org")
     86 
     87 	sessionName, err := middleware.SessionName()
     88 	suite.NoError(err)
     89 	suite.Equal("gotosocial-xn--fid-gna.org", sessionName)
     90 }
     91 
     92 func TestSessionTestSuite(t *testing.T) {
     93 	suite.Run(t, &SessionTestSuite{})
     94 }