diff options
author | 2021-08-31 19:27:02 +0200 | |
---|---|---|
committer | 2021-09-01 11:12:10 +0200 | |
commit | 7b01304dac37524643eca389af07337928d2b815 (patch) | |
tree | 6c77b6cfe17f312350531627f10d4471f2efe4df /internal | |
parent | change muchos things (diff) | |
download | gotosocial-7b01304dac37524643eca389af07337928d2b815.tar.xz |
more updates
Diffstat (limited to 'internal')
-rw-r--r-- | internal/db/bundb/bundb.go | 28 | ||||
-rw-r--r-- | internal/db/bundb/migrations/20210816411877_struct_validation.go | 54 | ||||
-rw-r--r-- | internal/db/bundb/migrations/README.md | 21 | ||||
-rw-r--r-- | internal/db/bundb/migrations/main.go | 28 | ||||
-rw-r--r-- | internal/gtsmodel/account.go | 158 | ||||
-rw-r--r-- | internal/gtsmodel/block.go | 16 | ||||
-rw-r--r-- | internal/gtsmodel/block_test.go | 115 | ||||
-rw-r--r-- | internal/gtsmodel/domainblock_test.go | 121 | ||||
-rw-r--r-- | internal/gtsmodel/emaildomainblock_test.go | 96 | ||||
-rw-r--r-- | internal/gtsmodel/follow.go | 6 | ||||
-rw-r--r-- | internal/gtsmodel/followrequest.go | 2 |
11 files changed, 520 insertions, 125 deletions
diff --git a/internal/db/bundb/bundb.go b/internal/db/bundb/bundb.go index 6fcc56e51..2a622020b 100644 --- a/internal/db/bundb/bundb.go +++ b/internal/db/bundb/bundb.go @@ -37,11 +37,13 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/cache" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/uptrace/bun" "github.com/uptrace/bun/dialect/pgdialect" "github.com/uptrace/bun/dialect/sqlitedialect" + "github.com/uptrace/bun/migrate" _ "modernc.org/sqlite" ) @@ -73,6 +75,29 @@ type bunDBService struct { conn *DBConn } +func doMigration(ctx context.Context, db *bun.DB, log *logrus.Logger) error { + l := log.WithField("func", "doMigration") + + migrator := migrate.NewMigrator(db, migrations.Migrations) + + if err := migrator.Init(ctx); err != nil { + return err + } + + group, err := migrator.Migrate(ctx) + if err != nil { + return err + } + + if group.ID == 0 { + l.Info("there are no new migrations to run") + return nil + } + + l.Infof("MIGRATED DATABASE TO %s", group) + return nil +} + // NewBunDBService returns a bunDB derived from the provided config, which implements the go-fed DB interface. // Under the hood, it uses https://github.com/uptrace/bun to create and maintain a database connection. func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger) (db.DB, error) { @@ -131,6 +156,9 @@ func NewBunDBService(ctx context.Context, c *config.Config, log *logrus.Logger) } accounts := &accountDB{config: c, conn: conn, cache: cache.NewAccountCache()} + if err := doMigration(ctx, conn.DB, log); err != nil { + return nil, fmt.Errorf("db migration error: %s", err) + } ps := &bunDBService{ Account: accounts, diff --git a/internal/db/bundb/migrations/20210816411877_struct_validation.go b/internal/db/bundb/migrations/20210816411877_struct_validation.go new file mode 100644 index 000000000..660d94517 --- /dev/null +++ b/internal/db/bundb/migrations/20210816411877_struct_validation.go @@ -0,0 +1,54 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package migrations + +import ( + "context" + + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/uptrace/bun" +) + +func init() { + up := func(ctx context.Context, db *bun.DB) error { + return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + _, err := tx.NewCreateTable().Model(>smodel.Account{}).IfNotExists().Exec(ctx) + if err != nil { + return err + } + + return nil + }) + } + + down := func(ctx context.Context, db *bun.DB) error { + return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + _, err := tx.NewDropTable().Model(>smodel.Account{}).Exec(ctx) + if err != nil { + return err + } + + return nil + }) + } + + if err := Migrations.Register(up, down); err != nil { + panic(err) + } +} diff --git a/internal/db/bundb/migrations/README.md b/internal/db/bundb/migrations/README.md new file mode 100644 index 000000000..e293d43c7 --- /dev/null +++ b/internal/db/bundb/migrations/README.md @@ -0,0 +1,21 @@ +# Migrations + +## How do I write a migration file? + +[See here](https://bun.uptrace.dev/guide/migrations.html#migration-names) + +As a template, take one of the existing migration files and modify it. It will be automatically loaded and handled by Bun. + +## File format + +Bun requires a very specific format: 14 digits, then letters or underscores. + +You can use the following bash command on your branch to generate a suitable migration filename. + +```bash +echo "$(date --utc +%Y%m%H%M%S%N | head -c 14)_$(git rev-parse --abbrev-ref HEAD).go" +``` + +## Rules of thumb + +1. **DON'T DROP TABLES**!!!!!!!! diff --git a/internal/db/bundb/migrations/main.go b/internal/db/bundb/migrations/main.go new file mode 100644 index 000000000..7f4e76027 --- /dev/null +++ b/internal/db/bundb/migrations/main.go @@ -0,0 +1,28 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package migrations + +import ( + "github.com/uptrace/bun/migrate" +) + +var ( + // Migrations provides migration logic for bun + Migrations = migrate.NewMigrations() +) diff --git a/internal/gtsmodel/account.go b/internal/gtsmodel/account.go index a59746be5..75ea02b4f 100644 --- a/internal/gtsmodel/account.go +++ b/internal/gtsmodel/account.go @@ -27,124 +27,56 @@ import ( "time" ) -// Account represents either a local or a remote fediverse account, gotosocial or otherwise (mastodon, pleroma, etc) +// Account represents either a local or a remote fediverse account, gotosocial or otherwise (mastodon, pleroma, etc). type Account struct { - /* - BASIC INFO - */ - - // id of this account in the local database - ID string `bun:"type:CHAR(26),pk,nullzero,notnull,unique"` - // Username of the account, should just be a string of [a-z0-9_]. Can be added to domain to create the full username in the form ``[username]@[domain]`` eg., ``user_96@example.org`` - Username string `bun:",notnull,unique:userdomain,nullzero"` // username and domain should be unique *with* each other - // Domain of the account, will be null if this is a local account, otherwise something like ``example.org`` or ``mastodon.social``. Should be unique with username. - Domain string `bun:",unique:userdomain,nullzero"` // username and domain should be unique *with* each other - - /* - ACCOUNT METADATA - */ - - // ID of the avatar as a media attachment - AvatarMediaAttachmentID string `bun:"type:CHAR(26),nullzero"` - AvatarMediaAttachment *MediaAttachment `bun:"rel:belongs-to"` - // For a non-local account, where can the header be fetched? - AvatarRemoteURL string `bun:",nullzero"` - // ID of the header as a media attachment - HeaderMediaAttachmentID string `bun:"type:CHAR(26),nullzero"` - HeaderMediaAttachment *MediaAttachment `bun:"rel:belongs-to"` - // For a non-local account, where can the header be fetched? - HeaderRemoteURL string `bun:",nullzero"` - // DisplayName for this account. Can be empty, then just the Username will be used for display purposes. - DisplayName string `bun:",nullzero"` - // a key/value map of fields that this account has added to their profile - Fields []Field - // A note that this account has on their profile (ie., the account's bio/description of themselves) - Note string `bun:",nullzero"` - // Is this a memorial account, ie., has the user passed away? - Memorial bool `bun:",nullzero"` - // This account has moved this account id in the database - MovedToAccountID string `bun:"type:CHAR(26),nullzero"` - // When was this account created? - CreatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp"` - // When was this account last updated? - UpdatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp"` - // Does this account identify itself as a bot? - Bot bool - // What reason was given for signing up when this account was created? - Reason string `bun:",nullzero"` - - /* - USER AND PRIVACY PREFERENCES - */ - - // Does this account need an approval for new followers? - Locked bool `bun:",default:true"` - // Should this account be shown in the instance's profile directory? - Discoverable bool `bun:",default:false"` - // Default post privacy for this account - Privacy Visibility `bun:",default:'public'"` - // Set posts from this account to sensitive by default? - Sensitive bool `bun:",default:false"` - // What language does this account post in? - Language string `bun:",default:'en'"` - - /* - ACTIVITYPUB THINGS - */ - - // What is the activitypub URI for this account discovered by webfinger? - URI string `bun:",unique,nullzero"` - // At which URL can we see the user account in a web browser? - URL string `bun:",unique,nullzero"` - // Last time this account was located using the webfinger API. - LastWebfingeredAt time.Time `bun:",nullzero"` - // Address of this account's activitypub inbox, for sending activity to - InboxURI string `bun:",unique,nullzero"` - // Address of this account's activitypub outbox - OutboxURI string `bun:",unique,nullzero"` - // URI for getting the following list of this account - FollowingURI string `bun:",unique,nullzero"` - // URI for getting the followers list of this account - FollowersURI string `bun:",unique,nullzero"` - // URL for getting the featured collection list of this account - FeaturedCollectionURI string `bun:",unique,nullzero"` - // What type of activitypub actor is this account? - ActorType string `bun:",nullzero"` - // This account is associated with x account id - AlsoKnownAs string `bun:",nullzero"` - - /* - CRYPTO FIELDS - */ - - // Privatekey for validating activitypub requests, will only be defined for local accounts - PrivateKey *rsa.PrivateKey - // Publickey for encoding activitypub requests, will be defined for both local and remote accounts - PublicKey *rsa.PublicKey - // Web-reachable location of this account's public key - PublicKeyURI string `bun:",nullzero"` - - /* - ADMIN FIELDS - */ - - // When was this account set to have all its media shown as sensitive? - SensitizedAt time.Time `bun:",nullzero"` - // When was this account silenced (eg., statuses only visible to followers, not public)? - SilencedAt time.Time `bun:",nullzero"` - // When was this account suspended (eg., don't allow it to log in/post, don't accept media/posts from this account) - SuspendedAt time.Time `bun:",nullzero"` - // Should we hide this account's collections? - HideCollections bool - // id of the database entry that caused this account to become suspended -- can be an account ID or a domain block ID - SuspensionOrigin string `bun:"type:CHAR(26),nullzero"` + ID string `validate:"required,ulid" bun:"type:CHAR(26),pk,nullzero,notnull,unique"` // id of this item in the database + CreatedAt time.Time `validate:"-" bun:",nullzero,notnull,default:current_timestamp"` // when was item created + UpdatedAt time.Time `validate:"-" bun:",nullzero,notnull,default:current_timestamp"` // when was item last updated + Username string `validate:"required" bun:",nullzero,notnull,unique:userdomain"` // Username of the account, should just be a string of [a-zA-Z0-9_]. Can be added to domain to create the full username in the form ``[username]@[domain]`` eg., ``user_96@example.org``. Username and domain should be unique *with* each other + Domain string `validate:"omitempty,fqdn" bun:",nullzero,unique:userdomain"` // Domain of the account, will be null if this is a local account, otherwise something like ``example.org`` or ``mastodon.social``. Should be unique with username. + AvatarMediaAttachmentID string `validate:"omitempty,ulid" bun:"type:CHAR(26),nullzero"` // Database ID of the media attachment, if present + AvatarMediaAttachment *MediaAttachment `validate:"-" bun:"rel:belongs-to"` // MediaAttachment corresponding to avatarMediaAttachmentID + AvatarRemoteURL string `validate:"omitempty,url" bun:",nullzero"` // For a non-local account, where can the header be fetched? + HeaderMediaAttachmentID string `validate:"omitempty,ulid" bun:"type:CHAR(26),nullzero"` // Database ID of the media attachment, if present + HeaderMediaAttachment *MediaAttachment `validate:"-" bun:"rel:belongs-to"` // MediaAttachment corresponding to headerMediaAttachmentID + HeaderRemoteURL string `validate:"omitempty,url" bun:",nullzero"` // For a non-local account, where can the header be fetched? + DisplayName string `validate:"-" bun:",nullzero"` // DisplayName for this account. Can be empty, then just the Username will be used for display purposes. + Fields []Field `validate:"-"` // a key/value map of fields that this account has added to their profile + Note string `validate:"-" bun:",nullzero"` // A note that this account has on their profile (ie., the account's bio/description of themselves) + Memorial bool `validate:"-" bun:",nullzero,default:false"` // Is this a memorial account, ie., has the user passed away? + AlsoKnownAs string `validate:"omitempty,ulid" bun:"type:CHAR(26),nullzero"` // This account is associated with x account id + MovedToAccountID string `validate:"omitempty,ulid" bun:"type:CHAR(26),nullzero"` // This account has moved this account id in the database + Bot bool `validate:"-" bun:",nullzero,default:false"` // Does this account identify itself as a bot? + Reason string `validate:"-" bun:",nullzero"` // What reason was given for signing up when this account was created? + Locked bool `validate:"-" bun:",nullzero,default:true"` // Does this account need an approval for new followers? + Discoverable bool `validate:"-" bun:",nullzero,default:false"` // Should this account be shown in the instance's profile directory? + Privacy Visibility `validate:"oneof=public unlocked followers_only mutuals_only direct" bun:",nullzero,notnull,default:'public'"` // Default post privacy for this account + Sensitive bool `validate:"-" bun:",nullzero,default:false"` // Set posts from this account to sensitive by default? + Language string `validate:"-" bun:",nullzero,notnull,default:'en'"` // What language does this account post in? + URI string `validate:"required,url" bun:",nullzero,notnull,unique"` // ActivityPub URI for this account. + URL string `validate:"omitempty,url" bun:",unique,nullzero"` // Web URL for this account's profile + LastWebfingeredAt time.Time `validate:"-" bun:",nullzero,notnull,default:current_timestamp"` // Last time this account was refreshed/located with webfinger. + InboxURI string `validate:"omitempty,url" bun:",nullzero,unique"` // Address of this account's ActivityPub inbox, for sending activity to + OutboxURI string `validate:"omitempty,url" bun:",nullzero,unique"` // Address of this account's activitypub outbox + FollowingURI string `validate:"omitempty,url" bun:",nullzero,unique"` // URI for getting the following list of this account + FollowersURI string `validate:"omitempty,url" bun:",nullzero,unique"` // URI for getting the followers list of this account + FeaturedCollectionURI string `validate:"omitempty,url" bun:",nullzero,unique"` // URL for getting the featured collection list of this account + ActorType string `validate:"oneof=Application Group Organization Person Service " bun:",nullzero,notnull"` // What type of activitypub actor is this account? + PrivateKey *rsa.PrivateKey `validate:"required_without=Domain"` // Privatekey for validating activitypub requests, will only be defined for local accounts + PublicKey *rsa.PublicKey `validate:"required"` // Publickey for encoding activitypub requests, will be defined for both local and remote accounts + PublicKeyURI string `validate:"required" bun:",nullzero,notnull"` // Web-reachable location of this account's public key + SensitizedAt time.Time `validate:"-" bun:",nullzero"` // When was this account set to have all its media shown as sensitive? + SilencedAt time.Time `validate:"-" bun:",nullzero"` // When was this account silenced (eg., statuses only visible to followers, not public)? + SuspendedAt time.Time `validate:"-" bun:",nullzero"` // When was this account suspended (eg., don't allow it to log in/post, don't accept media/posts from this account) + HideCollections bool `validate:"-" bun:",nullzero,default:false"` // Hide this account's collections + SuspensionOrigin string `validate:"omitempty,ulid" bun:"type:CHAR(26),nullzero"` // id of the database entry that caused this account to become suspended -- can be an account ID or a domain block ID } // Field represents a key value field on an account, for things like pronouns, website, etc. // VerifiedAt is optional, to be used only if Value is a URL to a webpage that contains the // username of the user. type Field struct { - Name string - Value string - VerifiedAt time.Time `bun:",nullzero"` + Name string `validate:"required"` // Name of this field. + Value string `validate:"required"` // Value of this field. + VerifiedAt time.Time `validate:"-" bun:",nullzero"` // This field was verified at (optional). } diff --git a/internal/gtsmodel/block.go b/internal/gtsmodel/block.go index 989dd8193..61595c12d 100644 --- a/internal/gtsmodel/block.go +++ b/internal/gtsmodel/block.go @@ -4,12 +4,12 @@ import "time" // Block refers to the blocking of one account by another. type Block struct { - ID string `validate:"required,ulid" bun:"type:CHAR(26),pk,nullzero,notnull,unique"` // id of this item in the database - CreatedAt time.Time `validate:"-" bun:",nullzero,notnull,default:current_timestamp"` // when was item created - UpdatedAt time.Time `validate:"-" bun:",nullzero,notnull,default:current_timestamp"` // when was item last updated - URI string `validate:"required,url" bun:",notnull,nullzero,unique"` // ActivityPub uri of this block. - AccountID string `validate:"required,ulid" bun:"type:CHAR(26),unique:frsrctarget,notnull"` // Who does this block originate from? - Account *Account `validate:"-" bun:"rel:belongs-to"` // Account corresponding to accountID - TargetAccountID string `validate:"required,ulid" bun:"type:CHAR(26),unique:frsrctarget,notnull"` // Who is the target of this block ? - TargetAccount *Account `validate:"-" bun:"rel:belongs-to"` // Account corresponding to targetAccountID + ID string `validate:"required,ulid" bun:"type:CHAR(26),pk,nullzero,notnull,unique"` // id of this item in the database + CreatedAt time.Time `validate:"-" bun:",nullzero,notnull,default:current_timestamp"` // when was item created + UpdatedAt time.Time `validate:"-" bun:",nullzero,notnull,default:current_timestamp"` // when was item last updated + URI string `validate:"required,url" bun:",notnull,nullzero,unique"` // ActivityPub uri of this block. + AccountID string `validate:"required,ulid" bun:"type:CHAR(26),unique:blocksrctarget,notnull"` // Who does this block originate from? + Account *Account `validate:"-" bun:"rel:belongs-to"` // Account corresponding to accountID + TargetAccountID string `validate:"required,ulid" bun:"type:CHAR(26),unique:blocksrctarget,notnull"` // Who is the target of this block ? + TargetAccount *Account `validate:"-" bun:"rel:belongs-to"` // Account corresponding to targetAccountID } diff --git a/internal/gtsmodel/block_test.go b/internal/gtsmodel/block_test.go new file mode 100644 index 000000000..307f26cd9 --- /dev/null +++ b/internal/gtsmodel/block_test.go @@ -0,0 +1,115 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package gtsmodel_test + +import ( + "testing" + "time" + + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + +func happyBlock() *gtsmodel.Block { + return >smodel.Block{ + ID: "01FE91RJR88PSEEE30EV35QR8N", + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + URI: "https://example.org/accounts/someone/blocks/01FE91RJR88PSEEE30EV35QR8N", + AccountID: "01FEED79PRMVWPRMFHFQM8MJQN", + Account: nil, + TargetAccountID: "01FEEDMF6C0QD589MRK7919Z0R", + TargetAccount: nil, + } +} + +type BlockValidateTestSuite struct { + suite.Suite +} + +func (suite *BlockValidateTestSuite) TestValidateBlockHappyPath() { + // no problem here + d := happyBlock() + err := gtsmodel.ValidateStruct(*d) + suite.NoError(err) +} + +func (suite *BlockValidateTestSuite) TestValidateBlockBadID() { + d := happyBlock() + + d.ID = "" + err := gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'Block.ID' Error:Field validation for 'ID' failed on the 'required' tag") + + d.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB" + err = gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'Block.ID' Error:Field validation for 'ID' failed on the 'ulid' tag") +} + +func (suite *BlockValidateTestSuite) TestValidateBlockNoCreatedAt() { + d := happyBlock() + + d.CreatedAt = time.Time{} + err := gtsmodel.ValidateStruct(*d) + suite.NoError(err) +} + +func (suite *BlockValidateTestSuite) TestValidateBlockCreatedByAccountID() { + d := happyBlock() + + d.AccountID = "" + err := gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'Block.AccountID' Error:Field validation for 'AccountID' failed on the 'required' tag") + + d.AccountID = "this-is-not-a-valid-ulid" + err = gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'Block.AccountID' Error:Field validation for 'AccountID' failed on the 'ulid' tag") +} + +func (suite *BlockValidateTestSuite) TestValidateBlockTargetAccountID() { + d := happyBlock() + + d.TargetAccountID = "invalid-ulid" + err := gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'Block.TargetAccountID' Error:Field validation for 'TargetAccountID' failed on the 'ulid' tag") + + d.TargetAccountID = "01FEEDHX4G7EGHF5GD9E82Y51Q" + err = gtsmodel.ValidateStruct(*d) + suite.NoError(err) + + d.TargetAccountID = "" + err = gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'Block.TargetAccountID' Error:Field validation for 'TargetAccountID' failed on the 'required' tag") +} + +func (suite *BlockValidateTestSuite) TestValidateBlockURI() { + d := happyBlock() + + d.URI = "invalid-uri" + err := gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'Block.URI' Error:Field validation for 'URI' failed on the 'url' tag") + + d.URI = "" + err = gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'Block.URI' Error:Field validation for 'URI' failed on the 'required' tag") +} + +func TestBlockValidateTestSuite(t *testing.T) { + suite.Run(t, new(BlockValidateTestSuite)) +} diff --git a/internal/gtsmodel/domainblock_test.go b/internal/gtsmodel/domainblock_test.go new file mode 100644 index 000000000..ced29ab31 --- /dev/null +++ b/internal/gtsmodel/domainblock_test.go @@ -0,0 +1,121 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package gtsmodel_test + +import ( + "testing" + "time" + + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + +func happyDomainBlock() *gtsmodel.DomainBlock { + return >smodel.DomainBlock{ + ID: "01FE91RJR88PSEEE30EV35QR8N", + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Domain: "baddudes.suck", + CreatedByAccountID: "01FEED79PRMVWPRMFHFQM8MJQN", + PrivateComment: "we don't like em", + PublicComment: "poo poo dudes", + Obfuscate: false, + SubscriptionID: "", + } +} + +type DomainBlockValidateTestSuite struct { + suite.Suite +} + +func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockHappyPath() { + // no problem here + d := happyDomainBlock() + err := gtsmodel.ValidateStruct(*d) + suite.NoError(err) +} + +func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockBadID() { + d := happyDomainBlock() + + d.ID = "" + err := gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'DomainBlock.ID' Error:Field validation for 'ID' failed on the 'required' tag") + + d.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB" + err = gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'DomainBlock.ID' Error:Field validation for 'ID' failed on the 'ulid' tag") +} + +func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockNoCreatedAt() { + d := happyDomainBlock() + + d.CreatedAt = time.Time{} + err := gtsmodel.ValidateStruct(*d) + suite.NoError(err) +} + +func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockBadDomain() { + d := happyDomainBlock() + + d.Domain = "" + err := gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'DomainBlock.Domain' Error:Field validation for 'Domain' failed on the 'required' tag") + + d.Domain = "this-is-not-a-valid-domain" + err = gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'DomainBlock.Domain' Error:Field validation for 'Domain' failed on the 'fqdn' tag") +} + +func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockCreatedByAccountID() { + d := happyDomainBlock() + + d.CreatedByAccountID = "" + err := gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'DomainBlock.CreatedByAccountID' Error:Field validation for 'CreatedByAccountID' failed on the 'required' tag") + + d.CreatedByAccountID = "this-is-not-a-valid-ulid" + err = gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'DomainBlock.CreatedByAccountID' Error:Field validation for 'CreatedByAccountID' failed on the 'ulid' tag") +} + +func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockComments() { + d := happyDomainBlock() + + d.PrivateComment = "" + d.PublicComment = "" + err := gtsmodel.ValidateStruct(*d) + suite.NoError(err) +} + +func (suite *DomainBlockValidateTestSuite) TestValidateDomainSubscriptionID() { + d := happyDomainBlock() + + d.SubscriptionID = "invalid-ulid" + err := gtsmodel.ValidateStruct(*d) + suite.EqualError(err, "Key: 'DomainBlock.SubscriptionID' Error:Field validation for 'SubscriptionID' failed on the 'ulid' tag") + + d.SubscriptionID = "01FEEDHX4G7EGHF5GD9E82Y51Q" + err = gtsmodel.ValidateStruct(*d) + suite.NoError(err) +} + +func TestDomainBlockValidateTestSuite(t *testing.T) { + suite.Run(t, new(DomainBlockValidateTestSuite)) +} diff --git a/internal/gtsmodel/emaildomainblock_test.go b/internal/gtsmodel/emaildomainblock_test.go new file mode 100644 index 000000000..83d7c4c6a --- /dev/null +++ b/internal/gtsmodel/emaildomainblock_test.go @@ -0,0 +1,96 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package gtsmodel_test + +import ( + "testing" + "time" + + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + +func happyEmailDomainBlock() *gtsmodel.EmailDomainBlock { + return >smodel.EmailDomainBlock{ + ID: "01FE91RJR88PSEEE30EV35QR8N", + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Domain: "baddudes.suck", + CreatedByAccountID: "01FEED79PRMVWPRMFHFQM8MJQN", + } +} + +type EmailDomainBlockValidateTestSuite struct { + suite.Suite +} + +func (suite *EmailDomainBlockValidateTestSuite) TestValidateEmailDomainBlockHappyPath() { + // no problem here + e := happyEmailDomainBlock() + err := gtsmodel.ValidateStruct(*e) + suite.NoError(err) +} + +func (suite *EmailDomainBlockValidateTestSuite) TestValidateEmailDomainBlockBadID() { + e := happyEmailDomainBlock() + + e.ID = "" + err := gtsmodel.ValidateStruct(*e) + suite.EqualError(err, "Key: 'EmailDomainBlock.ID' Error:Field validation for 'ID' failed on the 'required' tag") + + e.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB" + err = gtsmodel.ValidateStruct(*e) + suite.EqualError(err, "Key: 'EmailDomainBlock.ID' Error:Field validation for 'ID' failed on the 'ulid' tag") +} + +func (suite *EmailDomainBlockValidateTestSuite) TestValidateEmailDomainBlockNoCreatedAt() { + e := happyEmailDomainBlock() + + e.CreatedAt = time.Time{} + err := gtsmodel.ValidateStruct(*e) + suite.NoError(err) +} + +func (suite *EmailDomainBlockValidateTestSuite) TestValidateEmailDomainBlockBadDomain() { + e := happyEmailDomainBlock() + + e.Domain = "" + err := gtsmodel.ValidateStruct(*e) + suite.EqualError(err, "Key: 'EmailDomainBlock.Domain' Error:Field validation for 'Domain' failed on the 'required' tag") + + e.Domain = "this-is-not-a-valid-domain" + err = gtsmodel.ValidateStruct(*e) + suite.EqualError(err, "Key: 'EmailDomainBlock.Domain' Error:Field validation for 'Domain' failed on the 'fqdn' tag") +} + +func (suite *EmailDomainBlockValidateTestSuite) TestValidateEmailDomainBlockCreatedByAccountID() { + e := happyEmailDomainBlock() + + e.CreatedByAccountID = "" + err := gtsmodel.ValidateStruct(*e) + suite.EqualError(err, "Key: 'EmailDomainBlock.CreatedByAccountID' Error:Field validation for 'CreatedByAccountID' failed on the 'required' tag") + + e.CreatedByAccountID = "this-is-not-a-valid-ulid" + err = gtsmodel.ValidateStruct(*e) + suite.EqualError(err, "Key: 'EmailDomainBlock.CreatedByAccountID' Error:Field validation for 'CreatedByAccountID' failed on the 'ulid' tag") +} + +func TestEmailDomainBlockValidateTestSuite(t *testing.T) { + suite.Run(t, new(EmailDomainBlockValidateTestSuite)) +} diff --git a/internal/gtsmodel/follow.go b/internal/gtsmodel/follow.go index 8b03db56a..c2b2633b9 100644 --- a/internal/gtsmodel/follow.go +++ b/internal/gtsmodel/follow.go @@ -26,10 +26,10 @@ type Follow struct { CreatedAt time.Time `validate:"-" bun:",nullzero,notnull,default:current_timestamp"` // when was item created UpdatedAt time.Time `validate:"-" bun:",nullzero,notnull,default:current_timestamp"` // when was item last updated URI string `validate:"required,url" bun:",notnull,nullzero,unique"` // ActivityPub uri of this follow. - AccountID string `validate:"required,ulid" bun:"type:CHAR(26),unique:srctarget,notnull"` // Who does this follow originate from? + AccountID string `validate:"required,ulid" bun:"type:CHAR(26),unique:srctarget,notnull"` // Who does this follow originate from? Account *Account `validate:"-" bun:"rel:belongs-to"` // Account corresponding to accountID - TargetAccountID string `validate:"required,ulid" bun:"type:CHAR(26),unique:srctarget,notnull"` // Who is the target of this follow ? + TargetAccountID string `validate:"required,ulid" bun:"type:CHAR(26),unique:srctarget,notnull"` // Who is the target of this follow ? TargetAccount *Account `validate:"-" bun:"rel:belongs-to"` // Account corresponding to targetAccountID - ShowReblogs bool `validate:"-" bun:",nullzero,default:true"` // Does this follow also want to see reblogs and not just posts? + ShowReblogs bool `validate:"-" bun:",nullzero,default:true"` // Does this follow also want to see reblogs and not just posts? Notify bool `validate:"-" bun:",nullzero,default:false"` // does the following account want to be notified when the followed account posts? } diff --git a/internal/gtsmodel/followrequest.go b/internal/gtsmodel/followrequest.go index c4b3c9997..ae22f6487 100644 --- a/internal/gtsmodel/followrequest.go +++ b/internal/gtsmodel/followrequest.go @@ -30,6 +30,6 @@ type FollowRequest struct { Account *Account `validate:"-" bun:"rel:belongs-to"` // Account corresponding to accountID TargetAccountID string `validate:"required,ulid" bun:"type:CHAR(26),unique:frsrctarget,notnull"` // Who is the target of this follow request? TargetAccount *Account `validate:"-" bun:"rel:belongs-to"` // Account corresponding to targetAccountID - ShowReblogs bool `validate:"-" bun:",nullzero,default:true"` // Does this follow also want to see reblogs and not just posts? + ShowReblogs bool `validate:"-" bun:",nullzero,default:true"` // Does this follow also want to see reblogs and not just posts? Notify bool `validate:"-" bun:",nullzero,default:false"` // does the following account want to be notified when the followed account posts? } |