gotosocial/internal/validate/status_test.go

164 lines
6.1 KiB
Go
Raw Normal View History

2021-08-30 11:38:06 +00: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 16:29:25 +00:00
package validate_test
2021-08-30 11:38:06 +00:00
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
2021-08-31 13:59:12 +00:00
"github.com/superseriousbusiness/gotosocial/internal/ap"
2021-08-30 11:38:06 +00:00
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
2021-09-01 16:29:25 +00:00
"github.com/superseriousbusiness/gotosocial/internal/validate"
2021-08-30 11:38:06 +00:00
)
func happyStatus() *gtsmodel.Status {
return &gtsmodel.Status{
ID: "01FEBBH6NYDG87NK6A6EC543ED",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
URI: "https://example.org/users/test_user/statuses/01FEBBH6NYDG87NK6A6EC543ED",
URL: "https://example.org/@test_user/01FEBBH6NYDG87NK6A6EC543ED",
Content: "<p>Test status! #hello</p>",
AttachmentIDs: []string{"01FEBBKZBY9H5FEP3PHVVAAGN1", "01FEBBM7S2R4WT6WWW22KN1PWE"},
Attachments: nil,
TagIDs: []string{"01FEBBNBMBSN1FESMZ1TCXNWYP"},
Tags: nil,
MentionIDs: nil,
Mentions: nil,
EmojiIDs: nil,
Emojis: nil,
Local: true,
AccountID: "01FEBBQ4KEP3824WW61MF52638",
Account: nil,
AccountURI: "https://example.org/users/test_user",
InReplyToID: "",
InReplyToURI: "",
InReplyToAccountID: "",
InReplyTo: nil,
InReplyToAccount: nil,
BoostOfID: "",
BoostOfAccountID: "",
BoostOf: nil,
BoostOfAccount: nil,
ContentWarning: "hello world test post",
Visibility: gtsmodel.VisibilityPublic,
Sensitive: false,
Language: "en",
CreatedWithApplicationID: "01FEBBZHF4GFVRXSJVXD0JTZZ2",
CreatedWithApplication: nil,
VisibilityAdvanced: gtsmodel.VisibilityAdvanced{
Federated: true,
Boostable: true,
Replyable: true,
Likeable: true,
},
2021-08-31 13:59:12 +00:00
ActivityStreamsType: ap.ObjectNote,
2021-08-30 11:38:06 +00:00
Text: "Test status! #hello",
Pinned: false,
}
}
type StatusValidateTestSuite struct {
suite.Suite
}
func (suite *StatusValidateTestSuite) TestValidateStatusHappyPath() {
// no problem here
s := happyStatus()
2021-09-01 16:29:25 +00:00
err := validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.NoError(err)
}
func (suite *StatusValidateTestSuite) TestValidateStatusBadID() {
s := happyStatus()
s.ID = ""
2021-09-01 16:29:25 +00:00
err := validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.EqualError(err, "Key: 'Status.ID' Error:Field validation for 'ID' failed on the 'required' tag")
s.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB"
2021-09-01 16:29:25 +00:00
err = validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.EqualError(err, "Key: 'Status.ID' Error:Field validation for 'ID' failed on the 'ulid' tag")
}
func (suite *StatusValidateTestSuite) TestValidateStatusAttachmentIDs() {
s := happyStatus()
s.AttachmentIDs[0] = ""
2021-09-01 16:29:25 +00:00
err := validate.Struct(*s)
2021-08-30 18:20:27 +00:00
suite.EqualError(err, "Key: 'Status.AttachmentIDs[0]' Error:Field validation for 'AttachmentIDs[0]' failed on the 'ulid' tag")
2021-08-30 11:38:06 +00:00
s.AttachmentIDs[0] = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB"
2021-09-01 16:29:25 +00:00
err = validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.EqualError(err, "Key: 'Status.AttachmentIDs[0]' Error:Field validation for 'AttachmentIDs[0]' failed on the 'ulid' tag")
s.AttachmentIDs[1] = ""
2021-09-01 16:29:25 +00:00
err = validate.Struct(*s)
2021-08-30 18:20:27 +00:00
suite.EqualError(err, "Key: 'Status.AttachmentIDs[0]' Error:Field validation for 'AttachmentIDs[0]' failed on the 'ulid' tag\nKey: 'Status.AttachmentIDs[1]' Error:Field validation for 'AttachmentIDs[1]' failed on the 'ulid' tag")
2021-08-30 11:38:06 +00:00
s.AttachmentIDs = []string{}
2021-09-01 16:29:25 +00:00
err = validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.NoError(err)
s.AttachmentIDs = nil
2021-09-01 16:29:25 +00:00
err = validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.NoError(err)
}
func (suite *StatusValidateTestSuite) TestStatusApplicationID() {
s := happyStatus()
s.CreatedWithApplicationID = ""
2021-09-01 16:29:25 +00:00
err := validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.EqualError(err, "Key: 'Status.CreatedWithApplicationID' Error:Field validation for 'CreatedWithApplicationID' failed on the 'required_if' tag")
s.Local = false
2021-09-01 16:29:25 +00:00
err = validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.NoError(err)
}
func (suite *StatusValidateTestSuite) TestValidateStatusReplyFields() {
s := happyStatus()
s.InReplyToAccountID = "01FEBCTP6DN7961PN81C3DVM4N "
2021-09-01 16:29:25 +00:00
err := validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.EqualError(err, "Key: 'Status.InReplyToID' Error:Field validation for 'InReplyToID' failed on the 'required_with' tag\nKey: 'Status.InReplyToURI' Error:Field validation for 'InReplyToURI' failed on the 'required_with' tag\nKey: 'Status.InReplyToAccountID' Error:Field validation for 'InReplyToAccountID' failed on the 'ulid' tag")
s.InReplyToAccountID = "01FEBCTP6DN7961PN81C3DVM4N"
2021-09-01 16:29:25 +00:00
err = validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.EqualError(err, "Key: 'Status.InReplyToID' Error:Field validation for 'InReplyToID' failed on the 'required_with' tag\nKey: 'Status.InReplyToURI' Error:Field validation for 'InReplyToURI' failed on the 'required_with' tag")
s.InReplyToURI = "https://example.org/users/mmbop/statuses/aaaaaaaa"
2021-09-01 16:29:25 +00:00
err = validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.EqualError(err, "Key: 'Status.InReplyToID' Error:Field validation for 'InReplyToID' failed on the 'required_with' tag")
s.InReplyToID = "not a valid ulid"
2021-09-01 16:29:25 +00:00
err = validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.EqualError(err, "Key: 'Status.InReplyToID' Error:Field validation for 'InReplyToID' failed on the 'ulid' tag")
s.InReplyToID = "01FEBD07E72DEY6YB9K10ZA6ST"
2021-09-01 16:29:25 +00:00
err = validate.Struct(*s)
2021-08-30 11:38:06 +00:00
suite.NoError(err)
}
func TestStatusValidateTestSuite(t *testing.T) {
suite.Run(t, new(StatusValidateTestSuite))
}