test own domain excluded

This commit is contained in:
tobi 2024-11-21 13:29:00 +01:00
parent e338a26d3b
commit 2c2ff90913
3 changed files with 88 additions and 10 deletions

View file

@ -22,7 +22,6 @@
"errors"
"slices"
"github.com/miekg/dns"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
@ -65,14 +64,6 @@ func (d *domainDB) IsDomainPermissionExcluded(ctx context.Context, domain string
return false, err
}
// Check if our host and given domain are equal
// or part of the same second-level domain; we
// always exclude such perms as creating blocks
// or allows in such cases may break things.
if dns.CompareDomainName(domain, config.GetHost()) >= 2 {
return true, nil
}
// Func to scan list of all
// excluded domain perms from DB.
loadF := func() ([]string, error) {
@ -80,12 +71,16 @@ func (d *domainDB) IsDomainPermissionExcluded(ctx context.Context, domain string
if err := d.db.
NewSelect().
Table("domain_excludes").
Table("domain_permission_excludes").
Column("domain").
Scan(ctx, &domains); err != nil {
return nil, err
}
// Exclude our own domain as creating blocks
// or allows for self will likely break things.
domains = append(domains, config.GetHost())
return domains, nil
}

View file

@ -23,6 +23,7 @@
"testing"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
@ -100,6 +101,85 @@ func (suite *DomainPermissionExcludeTestSuite) TestPermExcludeCreateGetDelete()
}
}
func (suite *DomainPermissionExcludeTestSuite) TestExcluded() {
var (
ctx = context.Background()
createdByAccountID = suite.testAccounts["admin_account"].ID
)
// Insert some excludes into the db.
for _, exclude := range []*gtsmodel.DomainPermissionExclude{
{
ID: "01JD7AFFBBZSPY8R2M0JCGQGPW",
Domain: "example.org",
CreatedByAccountID: createdByAccountID,
},
{
ID: "01JD7AMK98E2QX78KXEZJ1RF5Z",
Domain: "boobs.com",
CreatedByAccountID: createdByAccountID,
},
{
ID: "01JD7AMXW3R3W98E91R62ACDA0",
Domain: "rad.boobs.com",
CreatedByAccountID: createdByAccountID,
},
{
ID: "01JD7AYYN5TXQVASB30PT08CE1",
Domain: "honkers.org",
CreatedByAccountID: createdByAccountID,
},
} {
if err := suite.state.DB.PutDomainPermissionExclude(ctx, exclude); err != nil {
suite.FailNow(err.Error())
}
}
type testCase struct {
domain string
excluded bool
}
for i, testCase := range []testCase{
{
domain: config.GetHost(),
excluded: true,
},
{
domain: "test.example.org",
excluded: true,
},
{
domain: "example.org",
excluded: true,
},
{
domain: "boobs.com",
excluded: true,
},
{
domain: "rad.boobs.com",
excluded: true,
},
{
domain: "sir.not.appearing.in.this.list",
excluded: false,
},
} {
excluded, err := suite.state.DB.IsDomainPermissionExcluded(ctx, testCase.domain)
if err != nil {
suite.FailNow(err.Error())
}
if excluded != testCase.excluded {
suite.Failf("",
"test %d: %s excluded should be %t",
i, testCase.domain, testCase.excluded,
)
}
}
}
func TestDomainPermissionExcludeTestSuite(t *testing.T) {
suite.Run(t, new(DomainPermissionExcludeTestSuite))
}

View file

@ -129,4 +129,7 @@ type Domain interface {
// DeleteDomainPermissionExclude deletes one DomainPermissionExclude with the given id.
DeleteDomainPermissionExclude(ctx context.Context, id string) error
// IsDomainPermissionExcluded returns true if the given domain matches in the list of excluded domains.
IsDomainPermissionExcluded(ctx context.Context, domain string) (bool, error)
}