gtsocial-umbx

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

admin_test.go (3160B)


      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 	gtsmodel "github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations/20211113114307_init"
     26 	"github.com/superseriousbusiness/gotosocial/testrig"
     27 )
     28 
     29 type AdminTestSuite struct {
     30 	BunDBStandardTestSuite
     31 }
     32 
     33 func (suite *AdminTestSuite) TestIsUsernameAvailableNo() {
     34 	available, err := suite.db.IsUsernameAvailable(context.Background(), "the_mighty_zork")
     35 	suite.NoError(err)
     36 	suite.False(available)
     37 }
     38 
     39 func (suite *AdminTestSuite) TestIsUsernameAvailableYes() {
     40 	available, err := suite.db.IsUsernameAvailable(context.Background(), "someone_completely_different")
     41 	suite.NoError(err)
     42 	suite.True(available)
     43 }
     44 
     45 func (suite *AdminTestSuite) TestIsEmailAvailableNo() {
     46 	available, err := suite.db.IsEmailAvailable(context.Background(), "zork@example.org")
     47 	suite.NoError(err)
     48 	suite.False(available)
     49 }
     50 
     51 func (suite *AdminTestSuite) TestIsEmailAvailableYes() {
     52 	available, err := suite.db.IsEmailAvailable(context.Background(), "someone@somewhere.com")
     53 	suite.NoError(err)
     54 	suite.True(available)
     55 }
     56 
     57 func (suite *AdminTestSuite) TestIsEmailAvailableDomainBlocked() {
     58 	if err := suite.db.Put(context.Background(), &gtsmodel.EmailDomainBlock{
     59 		ID:                 "01GEEV2R2YC5GRSN96761YJE47",
     60 		Domain:             "somewhere.com",
     61 		CreatedByAccountID: suite.testAccounts["admin_account"].ID,
     62 	}); err != nil {
     63 		suite.FailNow(err.Error())
     64 	}
     65 
     66 	available, err := suite.db.IsEmailAvailable(context.Background(), "someone@somewhere.com")
     67 	suite.EqualError(err, "email domain somewhere.com is blocked")
     68 	suite.False(available)
     69 }
     70 
     71 func (suite *AdminTestSuite) TestCreateInstanceAccount() {
     72 	// reinitialize db caches to clear
     73 	suite.state.Caches.Init()
     74 	// we need to take an empty db for this...
     75 	testrig.StandardDBTeardown(suite.db)
     76 	// ...with tables created but no data
     77 	testrig.CreateTestTables(suite.db)
     78 
     79 	// make sure there's no instance account in the db yet
     80 	acct, err := suite.db.GetInstanceAccount(context.Background(), "")
     81 	suite.Error(err)
     82 	suite.Nil(acct)
     83 
     84 	// create it
     85 	err = suite.db.CreateInstanceAccount(context.Background())
     86 	suite.NoError(err)
     87 
     88 	// and now check it exists
     89 	acct, err = suite.db.GetInstanceAccount(context.Background(), "")
     90 	suite.NoError(err)
     91 	suite.NotNil(acct)
     92 }
     93 
     94 func TestAdminTestSuite(t *testing.T) {
     95 	suite.Run(t, new(AdminTestSuite))
     96 }