gtsocial-umbx

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

auth_test.go (4850B)


      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 auth_test
     19 
     20 import (
     21 	"bytes"
     22 	"fmt"
     23 	"net/http/httptest"
     24 
     25 	"github.com/gin-contrib/sessions"
     26 	"github.com/gin-contrib/sessions/memstore"
     27 	"github.com/gin-gonic/gin"
     28 	"github.com/stretchr/testify/suite"
     29 	"github.com/superseriousbusiness/gotosocial/internal/api/auth"
     30 	"github.com/superseriousbusiness/gotosocial/internal/config"
     31 	"github.com/superseriousbusiness/gotosocial/internal/db"
     32 	"github.com/superseriousbusiness/gotosocial/internal/email"
     33 	"github.com/superseriousbusiness/gotosocial/internal/federation"
     34 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     35 	"github.com/superseriousbusiness/gotosocial/internal/media"
     36 	"github.com/superseriousbusiness/gotosocial/internal/middleware"
     37 	"github.com/superseriousbusiness/gotosocial/internal/oidc"
     38 	"github.com/superseriousbusiness/gotosocial/internal/processing"
     39 	"github.com/superseriousbusiness/gotosocial/internal/state"
     40 	"github.com/superseriousbusiness/gotosocial/internal/storage"
     41 	"github.com/superseriousbusiness/gotosocial/testrig"
     42 )
     43 
     44 type AuthStandardTestSuite struct {
     45 	suite.Suite
     46 	db           db.DB
     47 	storage      *storage.Driver
     48 	state        state.State
     49 	mediaManager *media.Manager
     50 	federator    federation.Federator
     51 	processor    *processing.Processor
     52 	emailSender  email.Sender
     53 	idp          oidc.IDP
     54 
     55 	// standard suite models
     56 	testTokens       map[string]*gtsmodel.Token
     57 	testClients      map[string]*gtsmodel.Client
     58 	testApplications map[string]*gtsmodel.Application
     59 	testUsers        map[string]*gtsmodel.User
     60 	testAccounts     map[string]*gtsmodel.Account
     61 
     62 	// module being tested
     63 	authModule *auth.Module
     64 }
     65 
     66 const (
     67 	sessionUserID   = "userid"
     68 	sessionClientID = "client_id"
     69 )
     70 
     71 func (suite *AuthStandardTestSuite) SetupSuite() {
     72 	suite.testTokens = testrig.NewTestTokens()
     73 	suite.testClients = testrig.NewTestClients()
     74 	suite.testApplications = testrig.NewTestApplications()
     75 	suite.testUsers = testrig.NewTestUsers()
     76 	suite.testAccounts = testrig.NewTestAccounts()
     77 }
     78 
     79 func (suite *AuthStandardTestSuite) SetupTest() {
     80 	suite.state.Caches.Init()
     81 
     82 	testrig.InitTestConfig()
     83 	testrig.InitTestLog()
     84 
     85 	suite.db = testrig.NewTestDB(&suite.state)
     86 	suite.state.DB = suite.db
     87 	suite.storage = testrig.NewInMemoryStorage()
     88 	suite.state.Storage = suite.storage
     89 	suite.mediaManager = testrig.NewTestMediaManager(&suite.state)
     90 	suite.federator = testrig.NewTestFederator(&suite.state, testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../testrig/media")), suite.mediaManager)
     91 	suite.emailSender = testrig.NewEmailSender("../../../web/template/", nil)
     92 	suite.processor = testrig.NewTestProcessor(&suite.state, suite.federator, suite.emailSender, suite.mediaManager)
     93 	suite.authModule = auth.New(suite.db, suite.processor, suite.idp)
     94 	testrig.StandardDBSetup(suite.db, suite.testAccounts)
     95 }
     96 
     97 func (suite *AuthStandardTestSuite) TearDownTest() {
     98 	testrig.StandardDBTeardown(suite.db)
     99 }
    100 
    101 func (suite *AuthStandardTestSuite) newContext(requestMethod string, requestPath string, requestBody []byte, bodyContentType string) (*gin.Context, *httptest.ResponseRecorder) {
    102 	// create the recorder and gin test context
    103 	recorder := httptest.NewRecorder()
    104 	ctx, engine := testrig.CreateGinTestContext(recorder, nil)
    105 
    106 	// load templates into the engine
    107 	testrig.ConfigureTemplatesWithGin(engine, "../../../web/template")
    108 
    109 	// create the request
    110 	protocol := config.GetProtocol()
    111 	host := config.GetHost()
    112 	baseURI := fmt.Sprintf("%s://%s", protocol, host)
    113 	requestURI := fmt.Sprintf("%s/%s", baseURI, requestPath)
    114 
    115 	ctx.Request = httptest.NewRequest(requestMethod, requestURI, bytes.NewReader(requestBody)) // the endpoint we're hitting
    116 	ctx.Request.Header.Set("accept", "text/html")
    117 
    118 	if bodyContentType != "" {
    119 		ctx.Request.Header.Set("Content-Type", bodyContentType)
    120 	}
    121 
    122 	// trigger the session middleware on the context
    123 	store := memstore.NewStore(make([]byte, 32), make([]byte, 32))
    124 	store.Options(middleware.SessionOptions())
    125 	sessionMiddleware := sessions.Sessions("gotosocial-localhost", store)
    126 	sessionMiddleware(ctx)
    127 
    128 	return ctx, recorder
    129 }