status_test.go (6406B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package validate_test 19 20 import ( 21 "testing" 22 "time" 23 24 "github.com/stretchr/testify/suite" 25 "github.com/superseriousbusiness/gotosocial/internal/ap" 26 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 27 "github.com/superseriousbusiness/gotosocial/internal/validate" 28 "github.com/superseriousbusiness/gotosocial/testrig" 29 ) 30 31 func happyStatus() *gtsmodel.Status { 32 return >smodel.Status{ 33 ID: "01FEBBH6NYDG87NK6A6EC543ED", 34 CreatedAt: time.Now(), 35 UpdatedAt: time.Now(), 36 URI: "https://example.org/users/test_user/statuses/01FEBBH6NYDG87NK6A6EC543ED", 37 URL: "https://example.org/@test_user/01FEBBH6NYDG87NK6A6EC543ED", 38 Content: "<p>Test status! #hello</p>", 39 AttachmentIDs: []string{"01FEBBKZBY9H5FEP3PHVVAAGN1", "01FEBBM7S2R4WT6WWW22KN1PWE"}, 40 Attachments: nil, 41 TagIDs: []string{"01FEBBNBMBSN1FESMZ1TCXNWYP"}, 42 Tags: nil, 43 MentionIDs: nil, 44 Mentions: nil, 45 EmojiIDs: nil, 46 Emojis: nil, 47 Local: testrig.TrueBool(), 48 AccountID: "01FEBBQ4KEP3824WW61MF52638", 49 Account: nil, 50 AccountURI: "https://example.org/users/test_user", 51 InReplyToID: "", 52 InReplyToURI: "", 53 InReplyToAccountID: "", 54 InReplyTo: nil, 55 InReplyToAccount: nil, 56 BoostOfID: "", 57 BoostOfAccountID: "", 58 BoostOf: nil, 59 BoostOfAccount: nil, 60 ContentWarning: "hello world test post", 61 Visibility: gtsmodel.VisibilityPublic, 62 Sensitive: testrig.FalseBool(), 63 Language: "en", 64 CreatedWithApplicationID: "01FEBBZHF4GFVRXSJVXD0JTZZ2", 65 CreatedWithApplication: nil, 66 Federated: testrig.TrueBool(), 67 Boostable: testrig.TrueBool(), 68 Replyable: testrig.TrueBool(), 69 Likeable: testrig.TrueBool(), 70 ActivityStreamsType: ap.ObjectNote, 71 Text: "Test status! #hello", 72 } 73 } 74 75 type StatusValidateTestSuite struct { 76 suite.Suite 77 } 78 79 func (suite *StatusValidateTestSuite) TestValidateStatusHappyPath() { 80 // no problem here 81 s := happyStatus() 82 err := validate.Struct(s) 83 suite.NoError(err) 84 } 85 86 func (suite *StatusValidateTestSuite) TestValidateStatusBadID() { 87 s := happyStatus() 88 89 s.ID = "" 90 err := validate.Struct(s) 91 suite.EqualError(err, "Key: 'Status.ID' Error:Field validation for 'ID' failed on the 'required' tag") 92 93 s.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB" 94 err = validate.Struct(s) 95 suite.EqualError(err, "Key: 'Status.ID' Error:Field validation for 'ID' failed on the 'ulid' tag") 96 } 97 98 func (suite *StatusValidateTestSuite) TestValidateStatusAttachmentIDs() { 99 s := happyStatus() 100 101 s.AttachmentIDs[0] = "" 102 err := validate.Struct(s) 103 suite.EqualError(err, "Key: 'Status.AttachmentIDs[0]' Error:Field validation for 'AttachmentIDs[0]' failed on the 'ulid' tag") 104 105 s.AttachmentIDs[0] = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB" 106 err = validate.Struct(s) 107 suite.EqualError(err, "Key: 'Status.AttachmentIDs[0]' Error:Field validation for 'AttachmentIDs[0]' failed on the 'ulid' tag") 108 109 s.AttachmentIDs[1] = "" 110 err = validate.Struct(s) 111 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") 112 113 s.AttachmentIDs = []string{} 114 err = validate.Struct(s) 115 suite.NoError(err) 116 117 s.AttachmentIDs = nil 118 err = validate.Struct(s) 119 suite.NoError(err) 120 } 121 122 func (suite *StatusValidateTestSuite) TestStatusApplicationID() { 123 s := happyStatus() 124 125 s.CreatedWithApplicationID = "" 126 err := validate.Struct(s) 127 suite.EqualError(err, "Key: 'Status.CreatedWithApplicationID' Error:Field validation for 'CreatedWithApplicationID' failed on the 'required_if' tag") 128 129 s.Local = testrig.FalseBool() 130 err = validate.Struct(s) 131 suite.NoError(err) 132 } 133 134 func (suite *StatusValidateTestSuite) TestValidateStatusReplyFields() { 135 s := happyStatus() 136 137 s.InReplyToAccountID = "01FEBCTP6DN7961PN81C3DVM4N " 138 err := validate.Struct(s) 139 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") 140 141 s.InReplyToAccountID = "01FEBCTP6DN7961PN81C3DVM4N" 142 err = validate.Struct(s) 143 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") 144 145 s.InReplyToURI = "https://example.org/users/mmbop/statuses/aaaaaaaa" 146 err = validate.Struct(s) 147 suite.EqualError(err, "Key: 'Status.InReplyToID' Error:Field validation for 'InReplyToID' failed on the 'required_with' tag") 148 149 s.InReplyToID = "not a valid ulid" 150 err = validate.Struct(s) 151 suite.EqualError(err, "Key: 'Status.InReplyToID' Error:Field validation for 'InReplyToID' failed on the 'ulid' tag") 152 153 s.InReplyToID = "01FEBD07E72DEY6YB9K10ZA6ST" 154 err = validate.Struct(s) 155 suite.NoError(err) 156 } 157 158 func TestStatusValidateTestSuite(t *testing.T) { 159 suite.Run(t, new(StatusValidateTestSuite)) 160 }