user_test.go (4556B)
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 "net" 22 "testing" 23 "time" 24 25 "github.com/stretchr/testify/suite" 26 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 27 "github.com/superseriousbusiness/gotosocial/internal/validate" 28 "github.com/superseriousbusiness/gotosocial/testrig" 29 ) 30 31 func happyUser() *gtsmodel.User { 32 return >smodel.User{ 33 ID: "01FE8TTK9F34BR0KG7639AJQTX", 34 Email: "whatever@example.org", 35 AccountID: "01FE8TWA7CN8J7237K5DFS1RY5", 36 Account: nil, 37 EncryptedPassword: "$2y$10$tkRapNGW.RWkEuCMWdgArunABFvsPGRvFQY3OibfSJo0RDL3z8WfC", 38 CreatedAt: time.Now(), 39 UpdatedAt: time.Now(), 40 SignUpIP: net.ParseIP("128.64.32.16"), 41 CurrentSignInAt: time.Now(), 42 CurrentSignInIP: net.ParseIP("128.64.32.16"), 43 LastSignInAt: time.Now(), 44 LastSignInIP: net.ParseIP("128.64.32.16"), 45 SignInCount: 0, 46 InviteID: "", 47 ChosenLanguages: []string{}, 48 FilteredLanguages: []string{}, 49 Locale: "en", 50 CreatedByApplicationID: "01FE8Y5EHMWCA1MHMTNHRVZ1X4", 51 CreatedByApplication: nil, 52 LastEmailedAt: time.Now(), 53 ConfirmationToken: "", 54 ConfirmedAt: time.Now(), 55 ConfirmationSentAt: time.Time{}, 56 UnconfirmedEmail: "", 57 Moderator: testrig.FalseBool(), 58 Admin: testrig.FalseBool(), 59 Disabled: testrig.FalseBool(), 60 Approved: testrig.TrueBool(), 61 } 62 } 63 64 type UserValidateTestSuite struct { 65 suite.Suite 66 } 67 68 func (suite *UserValidateTestSuite) TestValidateUserHappyPath() { 69 // no problem here 70 u := happyUser() 71 err := validate.Struct(u) 72 suite.NoError(err) 73 } 74 75 func (suite *UserValidateTestSuite) TestValidateUserNoID() { 76 // user has no id set 77 u := happyUser() 78 u.ID = "" 79 80 err := validate.Struct(u) 81 suite.EqualError(err, "Key: 'User.ID' Error:Field validation for 'ID' failed on the 'required' tag") 82 } 83 84 func (suite *UserValidateTestSuite) TestValidateUserNoEmail() { 85 // user has no email or unconfirmed email set 86 u := happyUser() 87 u.Email = "" 88 89 err := validate.Struct(u) 90 suite.EqualError(err, "Key: 'User.Email' Error:Field validation for 'Email' failed on the 'required_with' tag\nKey: 'User.UnconfirmedEmail' Error:Field validation for 'UnconfirmedEmail' failed on the 'required_without' tag") 91 } 92 93 func (suite *UserValidateTestSuite) TestValidateUserOnlyUnconfirmedEmail() { 94 // user has only UnconfirmedEmail but ConfirmedAt is set 95 u := happyUser() 96 u.Email = "" 97 u.UnconfirmedEmail = "whatever@example.org" 98 99 err := validate.Struct(u) 100 suite.EqualError(err, "Key: 'User.Email' Error:Field validation for 'Email' failed on the 'required_with' tag") 101 } 102 103 func (suite *UserValidateTestSuite) TestValidateUserOnlyUnconfirmedEmailOK() { 104 // user has only UnconfirmedEmail and ConfirmedAt is not set 105 u := happyUser() 106 u.Email = "" 107 u.UnconfirmedEmail = "whatever@example.org" 108 u.ConfirmedAt = time.Time{} 109 110 err := validate.Struct(u) 111 suite.NoError(err) 112 } 113 114 func (suite *UserValidateTestSuite) TestValidateUserNoConfirmedAt() { 115 // user has Email but no ConfirmedAt 116 u := happyUser() 117 u.ConfirmedAt = time.Time{} 118 119 err := validate.Struct(u) 120 suite.EqualError(err, "Key: 'User.ConfirmedAt' Error:Field validation for 'ConfirmedAt' failed on the 'required_with' tag") 121 } 122 123 func (suite *UserValidateTestSuite) TestValidateUserUnlikelySignInCount() { 124 // user has Email but no ConfirmedAt 125 u := happyUser() 126 u.SignInCount = -69 127 128 err := validate.Struct(u) 129 suite.EqualError(err, "Key: 'User.SignInCount' Error:Field validation for 'SignInCount' failed on the 'min' tag") 130 } 131 132 func TestUserValidateTestSuite(t *testing.T) { 133 suite.Run(t, new(UserValidateTestSuite)) 134 }