gtsocial-umbx

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

clientstore_test.go (4088B)


      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 oauth_test
     19 
     20 import (
     21 	"context"
     22 	"testing"
     23 
     24 	"github.com/stretchr/testify/suite"
     25 	"github.com/superseriousbusiness/gotosocial/internal/db"
     26 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     27 	"github.com/superseriousbusiness/gotosocial/internal/state"
     28 	"github.com/superseriousbusiness/gotosocial/testrig"
     29 	"github.com/superseriousbusiness/oauth2/v4/models"
     30 )
     31 
     32 type PgClientStoreTestSuite struct {
     33 	suite.Suite
     34 	db               db.DB
     35 	state            state.State
     36 	testClientID     string
     37 	testClientSecret string
     38 	testClientDomain string
     39 	testClientUserID string
     40 }
     41 
     42 // SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout
     43 func (suite *PgClientStoreTestSuite) SetupSuite() {
     44 	suite.testClientID = "01FCVB74EW6YBYAEY7QG9CQQF6"
     45 	suite.testClientSecret = "4cc87402-259b-4a35-9485-2c8bf54f3763"
     46 	suite.testClientDomain = "https://example.org"
     47 	suite.testClientUserID = "01FEGYXKVCDB731QF9MVFXA4F5"
     48 }
     49 
     50 // SetupTest creates a postgres connection and creates the oauth_clients table before each test
     51 func (suite *PgClientStoreTestSuite) SetupTest() {
     52 	suite.state.Caches.Init()
     53 	testrig.InitTestLog()
     54 	testrig.InitTestConfig()
     55 	suite.db = testrig.NewTestDB(&suite.state)
     56 	suite.state.DB = suite.db
     57 	testrig.StandardDBSetup(suite.db, nil)
     58 }
     59 
     60 // TearDownTest drops the oauth_clients table and closes the pg connection after each test
     61 func (suite *PgClientStoreTestSuite) TearDownTest() {
     62 	testrig.StandardDBTeardown(suite.db)
     63 }
     64 
     65 func (suite *PgClientStoreTestSuite) TestClientStoreSetAndGet() {
     66 	// set a new client in the store
     67 	cs := oauth.NewClientStore(suite.db)
     68 	if err := cs.Set(context.Background(), suite.testClientID, models.New(suite.testClientID, suite.testClientSecret, suite.testClientDomain, suite.testClientUserID)); err != nil {
     69 		suite.FailNow(err.Error())
     70 	}
     71 
     72 	// fetch that client from the store
     73 	client, err := cs.GetByID(context.Background(), suite.testClientID)
     74 	if err != nil {
     75 		suite.FailNow(err.Error())
     76 	}
     77 
     78 	// check that the values are the same
     79 	suite.NotNil(client)
     80 	suite.EqualValues(models.New(suite.testClientID, suite.testClientSecret, suite.testClientDomain, suite.testClientUserID), client)
     81 }
     82 
     83 func (suite *PgClientStoreTestSuite) TestClientSetAndDelete() {
     84 	// set a new client in the store
     85 	cs := oauth.NewClientStore(suite.db)
     86 	if err := cs.Set(context.Background(), suite.testClientID, models.New(suite.testClientID, suite.testClientSecret, suite.testClientDomain, suite.testClientUserID)); err != nil {
     87 		suite.FailNow(err.Error())
     88 	}
     89 
     90 	// fetch the client from the store
     91 	client, err := cs.GetByID(context.Background(), suite.testClientID)
     92 	if err != nil {
     93 		suite.FailNow(err.Error())
     94 	}
     95 
     96 	// check that the values are the same
     97 	suite.NotNil(client)
     98 	suite.EqualValues(models.New(suite.testClientID, suite.testClientSecret, suite.testClientDomain, suite.testClientUserID), client)
     99 	if err := cs.Delete(context.Background(), suite.testClientID); err != nil {
    100 		suite.FailNow(err.Error())
    101 	}
    102 
    103 	// try to get the deleted client; we should get an error
    104 	deletedClient, err := cs.GetByID(context.Background(), suite.testClientID)
    105 	suite.Assert().Nil(deletedClient)
    106 	suite.Assert().EqualValues(db.ErrNoEntries, err)
    107 }
    108 
    109 func TestPgClientStoreTestSuite(t *testing.T) {
    110 	suite.Run(t, new(PgClientStoreTestSuite))
    111 }