gtsocial-umbx

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

instance_test.go (4574B)


      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 bundb_test
     19 
     20 import (
     21 	"context"
     22 	"testing"
     23 
     24 	"github.com/stretchr/testify/suite"
     25 	"github.com/superseriousbusiness/gotosocial/internal/config"
     26 	"github.com/superseriousbusiness/gotosocial/internal/db"
     27 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     28 	"github.com/superseriousbusiness/gotosocial/testrig"
     29 )
     30 
     31 type InstanceTestSuite struct {
     32 	BunDBStandardTestSuite
     33 }
     34 
     35 func (suite *InstanceTestSuite) TestCountInstanceUsers() {
     36 	count, err := suite.db.CountInstanceUsers(context.Background(), config.GetHost())
     37 	suite.NoError(err)
     38 	suite.Equal(4, count)
     39 }
     40 
     41 func (suite *InstanceTestSuite) TestCountInstanceUsersRemote() {
     42 	count, err := suite.db.CountInstanceUsers(context.Background(), "fossbros-anonymous.io")
     43 	suite.NoError(err)
     44 	suite.Equal(1, count)
     45 }
     46 
     47 func (suite *InstanceTestSuite) TestCountInstanceStatuses() {
     48 	count, err := suite.db.CountInstanceStatuses(context.Background(), config.GetHost())
     49 	suite.NoError(err)
     50 	suite.Equal(16, count)
     51 }
     52 
     53 func (suite *InstanceTestSuite) TestCountInstanceStatusesRemote() {
     54 	count, err := suite.db.CountInstanceStatuses(context.Background(), "fossbros-anonymous.io")
     55 	suite.NoError(err)
     56 	suite.Equal(1, count)
     57 }
     58 
     59 func (suite *InstanceTestSuite) TestCountInstanceDomains() {
     60 	count, err := suite.db.CountInstanceDomains(context.Background(), config.GetHost())
     61 	suite.NoError(err)
     62 	suite.Equal(2, count)
     63 }
     64 
     65 func (suite *InstanceTestSuite) TestGetInstanceOK() {
     66 	instance, err := suite.db.GetInstance(context.Background(), "localhost:8080")
     67 	suite.NoError(err)
     68 	suite.NotNil(instance)
     69 }
     70 
     71 func (suite *InstanceTestSuite) TestGetInstanceNonexistent() {
     72 	instance, err := suite.db.GetInstance(context.Background(), "doesnt.exist.com")
     73 	suite.ErrorIs(err, db.ErrNoEntries)
     74 	suite.Nil(instance)
     75 }
     76 
     77 func (suite *InstanceTestSuite) TestGetInstancePeers() {
     78 	peers, err := suite.db.GetInstancePeers(context.Background(), false)
     79 	suite.NoError(err)
     80 	suite.Len(peers, 2)
     81 }
     82 
     83 func (suite *InstanceTestSuite) TestGetInstancePeersIncludeSuspended() {
     84 	peers, err := suite.db.GetInstancePeers(context.Background(), true)
     85 	suite.NoError(err)
     86 	suite.Len(peers, 2)
     87 }
     88 
     89 func (suite *InstanceTestSuite) TestGetInstanceAccounts() {
     90 	accounts, err := suite.db.GetInstanceAccounts(context.Background(), "fossbros-anonymous.io", "", 10)
     91 	suite.NoError(err)
     92 	suite.Len(accounts, 1)
     93 }
     94 
     95 func (suite *InstanceTestSuite) TestGetInstanceModeratorAddressesOK() {
     96 	// We have one admin user by default.
     97 	addresses, err := suite.db.GetInstanceModeratorAddresses(context.Background())
     98 	suite.NoError(err)
     99 	suite.EqualValues([]string{"admin@example.org"}, addresses)
    100 }
    101 
    102 func (suite *InstanceTestSuite) TestGetInstanceModeratorAddressesZorkAsModerator() {
    103 	// Promote zork to moderator role.
    104 	testUser := &gtsmodel.User{}
    105 	*testUser = *suite.testUsers["local_account_1"]
    106 	testUser.Moderator = testrig.TrueBool()
    107 	if err := suite.db.UpdateUser(context.Background(), testUser, "moderator"); err != nil {
    108 		suite.FailNow(err.Error())
    109 	}
    110 
    111 	addresses, err := suite.db.GetInstanceModeratorAddresses(context.Background())
    112 	suite.NoError(err)
    113 	suite.EqualValues([]string{"admin@example.org", "zork@example.org"}, addresses)
    114 }
    115 
    116 func (suite *InstanceTestSuite) TestGetInstanceModeratorAddressesNoAdmin() {
    117 	// Demote admin from admin + moderator roles.
    118 	testUser := &gtsmodel.User{}
    119 	*testUser = *suite.testUsers["admin_account"]
    120 	testUser.Admin = testrig.FalseBool()
    121 	testUser.Moderator = testrig.FalseBool()
    122 	if err := suite.db.UpdateUser(context.Background(), testUser, "admin", "moderator"); err != nil {
    123 		suite.FailNow(err.Error())
    124 	}
    125 
    126 	addresses, err := suite.db.GetInstanceModeratorAddresses(context.Background())
    127 	suite.ErrorIs(err, db.ErrNoEntries)
    128 	suite.Empty(addresses)
    129 }
    130 
    131 func TestInstanceTestSuite(t *testing.T) {
    132 	suite.Run(t, new(InstanceTestSuite))
    133 }