gotosocial/internal/validate/block_test.go

117 lines
3.6 KiB
Go
Raw Normal View History

2021-08-31 19:27:02 +02:00
/*
GoToSocial
2021-12-20 18:42:19 +01:00
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
2021-08-31 19:27:02 +02:00
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 18:29:25 +02:00
package validate_test
2021-08-31 19:27:02 +02:00
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
2021-09-01 18:29:25 +02:00
"github.com/superseriousbusiness/gotosocial/internal/validate"
2021-08-31 19:27:02 +02: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
2021-09-03 10:30:40 +02:00
b := happyBlock()
err := validate.Struct(b)
2021-08-31 19:27:02 +02:00
suite.NoError(err)
}
func (suite *BlockValidateTestSuite) TestValidateBlockBadID() {
2021-09-03 10:30:40 +02:00
b := happyBlock()
2021-08-31 19:27:02 +02:00
2021-09-03 10:30:40 +02:00
b.ID = ""
err := validate.Struct(b)
2021-08-31 19:27:02 +02:00
suite.EqualError(err, "Key: 'Block.ID' Error:Field validation for 'ID' failed on the 'required' tag")
2021-09-03 10:30:40 +02:00
b.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB"
err = validate.Struct(b)
2021-08-31 19:27:02 +02:00
suite.EqualError(err, "Key: 'Block.ID' Error:Field validation for 'ID' failed on the 'ulid' tag")
}
func (suite *BlockValidateTestSuite) TestValidateBlockNoCreatedAt() {
2021-09-03 10:30:40 +02:00
b := happyBlock()
2021-08-31 19:27:02 +02:00
2021-09-03 10:30:40 +02:00
b.CreatedAt = time.Time{}
err := validate.Struct(b)
2021-08-31 19:27:02 +02:00
suite.NoError(err)
}
func (suite *BlockValidateTestSuite) TestValidateBlockCreatedByAccountID() {
2021-09-03 10:30:40 +02:00
b := happyBlock()
2021-08-31 19:27:02 +02:00
2021-09-03 10:30:40 +02:00
b.AccountID = ""
err := validate.Struct(b)
2021-08-31 19:27:02 +02:00
suite.EqualError(err, "Key: 'Block.AccountID' Error:Field validation for 'AccountID' failed on the 'required' tag")
2021-09-03 10:30:40 +02:00
b.AccountID = "this-is-not-a-valid-ulid"
err = validate.Struct(b)
2021-08-31 19:27:02 +02:00
suite.EqualError(err, "Key: 'Block.AccountID' Error:Field validation for 'AccountID' failed on the 'ulid' tag")
}
func (suite *BlockValidateTestSuite) TestValidateBlockTargetAccountID() {
2021-09-03 10:30:40 +02:00
b := happyBlock()
2021-08-31 19:27:02 +02:00
2021-09-03 10:30:40 +02:00
b.TargetAccountID = "invalid-ulid"
err := validate.Struct(b)
2021-08-31 19:27:02 +02:00
suite.EqualError(err, "Key: 'Block.TargetAccountID' Error:Field validation for 'TargetAccountID' failed on the 'ulid' tag")
2021-09-03 10:30:40 +02:00
b.TargetAccountID = "01FEEDHX4G7EGHF5GD9E82Y51Q"
err = validate.Struct(b)
2021-08-31 19:27:02 +02:00
suite.NoError(err)
2021-09-03 10:30:40 +02:00
b.TargetAccountID = ""
err = validate.Struct(b)
2021-08-31 19:27:02 +02:00
suite.EqualError(err, "Key: 'Block.TargetAccountID' Error:Field validation for 'TargetAccountID' failed on the 'required' tag")
}
func (suite *BlockValidateTestSuite) TestValidateBlockURI() {
2021-09-03 10:30:40 +02:00
b := happyBlock()
2021-08-31 19:27:02 +02:00
2021-09-03 10:30:40 +02:00
b.URI = "invalid-uri"
err := validate.Struct(b)
2021-08-31 19:27:02 +02:00
suite.EqualError(err, "Key: 'Block.URI' Error:Field validation for 'URI' failed on the 'url' tag")
2021-09-03 10:30:40 +02:00
b.URI = ""
err = validate.Struct(b)
2021-08-31 19:27:02 +02: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))
}