gotosocial/internal/validate/block_test.go

117 lines
3.6 KiB
Go
Raw Normal View History

2021-08-31 18:27:02 +01:00
/*
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/>.
*/
2021-09-01 17:29:25 +01:00
package validate_test
2021-08-31 18:27:02 +01:00
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
2021-09-01 17:29:25 +01:00
"github.com/superseriousbusiness/gotosocial/internal/validate"
2021-08-31 18:27:02 +01:00
)
func happyBlock() *gtsmodel.Block {
return &gtsmodel.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()
2021-09-01 17:29:25 +01:00
err := validate.Struct(*d)
2021-08-31 18:27:02 +01:00
suite.NoError(err)
}
func (suite *BlockValidateTestSuite) TestValidateBlockBadID() {
d := happyBlock()
d.ID = ""
2021-09-01 17:29:25 +01:00
err := validate.Struct(*d)
2021-08-31 18:27:02 +01:00
suite.EqualError(err, "Key: 'Block.ID' Error:Field validation for 'ID' failed on the 'required' tag")
d.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB"
2021-09-01 17:29:25 +01:00
err = validate.Struct(*d)
2021-08-31 18:27:02 +01:00
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{}
2021-09-01 17:29:25 +01:00
err := validate.Struct(*d)
2021-08-31 18:27:02 +01:00
suite.NoError(err)
}
func (suite *BlockValidateTestSuite) TestValidateBlockCreatedByAccountID() {
d := happyBlock()
d.AccountID = ""
2021-09-01 17:29:25 +01:00
err := validate.Struct(*d)
2021-08-31 18:27:02 +01:00
suite.EqualError(err, "Key: 'Block.AccountID' Error:Field validation for 'AccountID' failed on the 'required' tag")
d.AccountID = "this-is-not-a-valid-ulid"
2021-09-01 17:29:25 +01:00
err = validate.Struct(*d)
2021-08-31 18:27:02 +01:00
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"
2021-09-01 17:29:25 +01:00
err := validate.Struct(*d)
2021-08-31 18:27:02 +01:00
suite.EqualError(err, "Key: 'Block.TargetAccountID' Error:Field validation for 'TargetAccountID' failed on the 'ulid' tag")
d.TargetAccountID = "01FEEDHX4G7EGHF5GD9E82Y51Q"
2021-09-01 17:29:25 +01:00
err = validate.Struct(*d)
2021-08-31 18:27:02 +01:00
suite.NoError(err)
d.TargetAccountID = ""
2021-09-01 17:29:25 +01:00
err = validate.Struct(*d)
2021-08-31 18:27:02 +01:00
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"
2021-09-01 17:29:25 +01:00
err := validate.Struct(*d)
2021-08-31 18:27:02 +01:00
suite.EqualError(err, "Key: 'Block.URI' Error:Field validation for 'URI' failed on the 'url' tag")
d.URI = ""
2021-09-01 17:29:25 +01:00
err = validate.Struct(*d)
2021-08-31 18:27:02 +01:00
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))
}