emaildomainblock_test.go (3387B)
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 happyEmailDomainBlock() *gtsmodel.EmailDomainBlock { 30 return >smodel.EmailDomainBlock{ 31 ID: "01FE91RJR88PSEEE30EV35QR8N", 32 CreatedAt: time.Now(), 33 UpdatedAt: time.Now(), 34 Domain: "baddudes.suck", 35 CreatedByAccountID: "01FEED79PRMVWPRMFHFQM8MJQN", 36 } 37 } 38 39 type EmailDomainBlockValidateTestSuite struct { 40 suite.Suite 41 } 42 43 func (suite *EmailDomainBlockValidateTestSuite) TestValidateEmailDomainBlockHappyPath() { 44 // no problem here 45 e := happyEmailDomainBlock() 46 err := validate.Struct(e) 47 suite.NoError(err) 48 } 49 50 func (suite *EmailDomainBlockValidateTestSuite) TestValidateEmailDomainBlockBadID() { 51 e := happyEmailDomainBlock() 52 53 e.ID = "" 54 err := validate.Struct(e) 55 suite.EqualError(err, "Key: 'EmailDomainBlock.ID' Error:Field validation for 'ID' failed on the 'required' tag") 56 57 e.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB" 58 err = validate.Struct(e) 59 suite.EqualError(err, "Key: 'EmailDomainBlock.ID' Error:Field validation for 'ID' failed on the 'ulid' tag") 60 } 61 62 func (suite *EmailDomainBlockValidateTestSuite) TestValidateEmailDomainBlockNoCreatedAt() { 63 e := happyEmailDomainBlock() 64 65 e.CreatedAt = time.Time{} 66 err := validate.Struct(e) 67 suite.NoError(err) 68 } 69 70 func (suite *EmailDomainBlockValidateTestSuite) TestValidateEmailDomainBlockBadDomain() { 71 e := happyEmailDomainBlock() 72 73 e.Domain = "" 74 err := validate.Struct(e) 75 suite.EqualError(err, "Key: 'EmailDomainBlock.Domain' Error:Field validation for 'Domain' failed on the 'required' tag") 76 77 e.Domain = "this-is-not-a-valid-domain" 78 err = validate.Struct(e) 79 suite.EqualError(err, "Key: 'EmailDomainBlock.Domain' Error:Field validation for 'Domain' failed on the 'fqdn' tag") 80 } 81 82 func (suite *EmailDomainBlockValidateTestSuite) TestValidateEmailDomainBlockCreatedByAccountID() { 83 e := happyEmailDomainBlock() 84 85 e.CreatedByAccountID = "" 86 err := validate.Struct(e) 87 suite.EqualError(err, "Key: 'EmailDomainBlock.CreatedByAccountID' Error:Field validation for 'CreatedByAccountID' failed on the 'required' tag") 88 89 e.CreatedByAccountID = "this-is-not-a-valid-ulid" 90 err = validate.Struct(e) 91 suite.EqualError(err, "Key: 'EmailDomainBlock.CreatedByAccountID' Error:Field validation for 'CreatedByAccountID' failed on the 'ulid' tag") 92 } 93 94 func TestEmailDomainBlockValidateTestSuite(t *testing.T) { 95 suite.Run(t, new(EmailDomainBlockValidateTestSuite)) 96 }