domain_test.go (4881B)
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 "time" 24 25 "github.com/stretchr/testify/suite" 26 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 27 ) 28 29 type DomainTestSuite struct { 30 BunDBStandardTestSuite 31 } 32 33 func (suite *DomainTestSuite) TestIsDomainBlocked() { 34 ctx := context.Background() 35 36 domainBlock := >smodel.DomainBlock{ 37 ID: "01G204214Y9TNJEBX39C7G88SW", 38 Domain: "some.bad.apples", 39 CreatedByAccountID: suite.testAccounts["admin_account"].ID, 40 CreatedByAccount: suite.testAccounts["admin_account"], 41 } 42 43 // no domain block exists for the given domain yet 44 blocked, err := suite.db.IsDomainBlocked(ctx, domainBlock.Domain) 45 suite.NoError(err) 46 suite.False(blocked) 47 48 err = suite.db.CreateDomainBlock(ctx, domainBlock) 49 suite.NoError(err) 50 51 // domain block now exists 52 blocked, err = suite.db.IsDomainBlocked(ctx, domainBlock.Domain) 53 suite.NoError(err) 54 suite.True(blocked) 55 suite.WithinDuration(time.Now(), domainBlock.CreatedAt, 10*time.Second) 56 } 57 58 func (suite *DomainTestSuite) TestIsDomainBlockedWildcard() { 59 ctx := context.Background() 60 61 domainBlock := >smodel.DomainBlock{ 62 ID: "01G204214Y9TNJEBX39C7G88SW", 63 Domain: "bad.apples", 64 CreatedByAccountID: suite.testAccounts["admin_account"].ID, 65 CreatedByAccount: suite.testAccounts["admin_account"], 66 } 67 68 // no domain block exists for the given domain yet 69 blocked, err := suite.db.IsDomainBlocked(ctx, domainBlock.Domain) 70 suite.NoError(err) 71 suite.False(blocked) 72 73 err = suite.db.CreateDomainBlock(ctx, domainBlock) 74 suite.NoError(err) 75 76 // Start with the base block domain 77 domain := domainBlock.Domain 78 79 for _, part := range []string{"extra", "domain", "parts"} { 80 // Prepend the next domain part 81 domain = part + "." + domain 82 83 // Check that domain block is wildcarded for this subdomain 84 blocked, err = suite.db.IsDomainBlocked(ctx, domainBlock.Domain) 85 suite.NoError(err) 86 suite.True(blocked) 87 } 88 } 89 90 func (suite *DomainTestSuite) TestIsDomainBlockedNonASCII() { 91 ctx := context.Background() 92 93 now := time.Now() 94 95 domainBlock := >smodel.DomainBlock{ 96 ID: "01G204214Y9TNJEBX39C7G88SW", 97 Domain: "xn--80aaa1bbb1h.com", 98 CreatedAt: now, 99 UpdatedAt: now, 100 CreatedByAccountID: suite.testAccounts["admin_account"].ID, 101 CreatedByAccount: suite.testAccounts["admin_account"], 102 } 103 104 // no domain block exists for the given domain yet 105 blocked, err := suite.db.IsDomainBlocked(ctx, "какашка.com") 106 suite.NoError(err) 107 suite.False(blocked) 108 109 blocked, err = suite.db.IsDomainBlocked(ctx, "xn--80aaa1bbb1h.com") 110 suite.NoError(err) 111 suite.False(blocked) 112 113 err = suite.db.CreateDomainBlock(ctx, domainBlock) 114 suite.NoError(err) 115 116 // domain block now exists 117 blocked, err = suite.db.IsDomainBlocked(ctx, "какашка.com") 118 suite.NoError(err) 119 suite.True(blocked) 120 121 blocked, err = suite.db.IsDomainBlocked(ctx, "xn--80aaa1bbb1h.com") 122 suite.NoError(err) 123 suite.True(blocked) 124 } 125 126 func (suite *DomainTestSuite) TestIsDomainBlockedNonASCII2() { 127 ctx := context.Background() 128 129 now := time.Now() 130 131 domainBlock := >smodel.DomainBlock{ 132 ID: "01G204214Y9TNJEBX39C7G88SW", 133 Domain: "какашка.com", 134 CreatedAt: now, 135 UpdatedAt: now, 136 CreatedByAccountID: suite.testAccounts["admin_account"].ID, 137 CreatedByAccount: suite.testAccounts["admin_account"], 138 } 139 140 // no domain block exists for the given domain yet 141 blocked, err := suite.db.IsDomainBlocked(ctx, "какашка.com") 142 suite.NoError(err) 143 suite.False(blocked) 144 145 blocked, err = suite.db.IsDomainBlocked(ctx, "xn--80aaa1bbb1h.com") 146 suite.NoError(err) 147 suite.False(blocked) 148 149 err = suite.db.CreateDomainBlock(ctx, domainBlock) 150 suite.NoError(err) 151 152 // domain block now exists 153 blocked, err = suite.db.IsDomainBlocked(ctx, "какашка.com") 154 suite.NoError(err) 155 suite.True(blocked) 156 157 blocked, err = suite.db.IsDomainBlocked(ctx, "xn--80aaa1bbb1h.com") 158 suite.NoError(err) 159 suite.True(blocked) 160 } 161 162 func TestDomainTestSuite(t *testing.T) { 163 suite.Run(t, new(DomainTestSuite)) 164 }