instance_test.go (5008B)
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/gtsmodel" 26 "github.com/superseriousbusiness/gotosocial/internal/validate" 27 ) 28 29 func happyInstance() *gtsmodel.Instance { 30 return >smodel.Instance{ 31 ID: "01FE91RJR88PSEEE30EV35QR8N", 32 CreatedAt: time.Now(), 33 UpdatedAt: time.Now(), 34 Domain: "example.org", 35 Title: "Example Instance", 36 URI: "https://example.org", 37 SuspendedAt: time.Time{}, 38 DomainBlockID: "", 39 DomainBlock: nil, 40 ShortDescription: "This is a description for the example/testing instance.", 41 Description: "This is a way longer description for the example/testing instance!", 42 Terms: "Don't be a knobhead.", 43 ContactEmail: "admin@example.org", 44 ContactAccountUsername: "admin", 45 ContactAccountID: "01FEE20H5QWHJDEXAEE9G96PR0", 46 ContactAccount: nil, 47 Reputation: 420, 48 Version: "gotosocial 0.1.0", 49 } 50 } 51 52 type InstanceValidateTestSuite struct { 53 suite.Suite 54 } 55 56 func (suite *InstanceValidateTestSuite) TestValidateInstanceHappyPath() { 57 // no problem here 58 m := happyInstance() 59 err := validate.Struct(*m) 60 suite.NoError(err) 61 } 62 63 func (suite *InstanceValidateTestSuite) TestValidateInstanceBadID() { 64 m := happyInstance() 65 66 m.ID = "" 67 err := validate.Struct(*m) 68 suite.EqualError(err, "Key: 'Instance.ID' Error:Field validation for 'ID' failed on the 'required' tag") 69 70 m.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB" 71 err = validate.Struct(*m) 72 suite.EqualError(err, "Key: 'Instance.ID' Error:Field validation for 'ID' failed on the 'ulid' tag") 73 } 74 75 func (suite *InstanceValidateTestSuite) TestValidateInstanceAccountURI() { 76 i := happyInstance() 77 78 i.URI = "" 79 err := validate.Struct(i) 80 suite.EqualError(err, "Key: 'Instance.URI' Error:Field validation for 'URI' failed on the 'required' tag") 81 82 i.URI = "---------------------------" 83 err = validate.Struct(i) 84 suite.EqualError(err, "Key: 'Instance.URI' Error:Field validation for 'URI' failed on the 'url' tag") 85 } 86 87 func (suite *InstanceValidateTestSuite) TestValidateInstanceDodgyAccountID() { 88 i := happyInstance() 89 90 i.ContactAccountID = "9HZJ76B6VXSKF" 91 err := validate.Struct(i) 92 suite.EqualError(err, "Key: 'Instance.ContactAccountID' Error:Field validation for 'ContactAccountID' failed on the 'ulid' tag") 93 94 i.ContactAccountID = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!!!!!!!!!!!!" 95 err = validate.Struct(i) 96 suite.EqualError(err, "Key: 'Instance.ContactAccountID' Error:Field validation for 'ContactAccountID' failed on the 'ulid' tag") 97 98 i.ContactAccountID = "" 99 err = validate.Struct(i) 100 suite.EqualError(err, "Key: 'Instance.ContactAccountID' Error:Field validation for 'ContactAccountID' failed on the 'required_with' tag") 101 102 i.ContactAccountUsername = "" 103 err = validate.Struct(i) 104 suite.NoError(err) 105 } 106 107 func (suite *InstanceValidateTestSuite) TestValidateInstanceDomain() { 108 i := happyInstance() 109 110 i.Domain = "poopoo" 111 err := validate.Struct(i) 112 suite.EqualError(err, "Key: 'Instance.Domain' Error:Field validation for 'Domain' failed on the 'fqdn' tag") 113 114 i.Domain = "" 115 err = validate.Struct(i) 116 suite.EqualError(err, "Key: 'Instance.Domain' Error:Field validation for 'Domain' failed on the 'required' tag") 117 118 i.Domain = "https://aaaaaaaaaaaaah.org" 119 err = validate.Struct(i) 120 suite.EqualError(err, "Key: 'Instance.Domain' Error:Field validation for 'Domain' failed on the 'fqdn' tag") 121 } 122 123 func (suite *InstanceValidateTestSuite) TestValidateInstanceContactEmail() { 124 i := happyInstance() 125 126 i.ContactEmail = "poopoo" 127 err := validate.Struct(i) 128 suite.EqualError(err, "Key: 'Instance.ContactEmail' Error:Field validation for 'ContactEmail' failed on the 'email' tag") 129 130 i.ContactEmail = "" 131 err = validate.Struct(i) 132 suite.NoError(err) 133 } 134 135 func (suite *InstanceValidateTestSuite) TestValidateInstanceNoCreatedAt() { 136 i := happyInstance() 137 138 i.CreatedAt = time.Time{} 139 err := validate.Struct(i) 140 suite.NoError(err) 141 } 142 143 func TestInstanceValidateTestSuite(t *testing.T) { 144 suite.Run(t, new(InstanceValidateTestSuite)) 145 }