notification_test.go (3215B)
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 happyNotification() *gtsmodel.Notification { 30 return >smodel.Notification{ 31 ID: "01FE91RJR88PSEEE30EV35QR8N", 32 CreatedAt: time.Now(), 33 NotificationType: gtsmodel.NotificationFave, 34 OriginAccountID: "01FE96MAE58MXCE5C4SSMEMCEK", 35 OriginAccount: nil, 36 TargetAccountID: "01FE96MXRHWZHKC0WH5FT82H1A", 37 TargetAccount: nil, 38 StatusID: "01FE96NBPNJNY26730FT6GZTFE", 39 Status: nil, 40 } 41 } 42 43 type NotificationValidateTestSuite struct { 44 suite.Suite 45 } 46 47 func (suite *NotificationValidateTestSuite) TestValidateNotificationHappyPath() { 48 // no problem here 49 n := happyNotification() 50 err := validate.Struct(n) 51 suite.NoError(err) 52 } 53 54 func (suite *NotificationValidateTestSuite) TestValidateNotificationBadID() { 55 n := happyNotification() 56 57 n.ID = "" 58 err := validate.Struct(n) 59 suite.EqualError(err, "Key: 'Notification.ID' Error:Field validation for 'ID' failed on the 'required' tag") 60 61 n.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB" 62 err = validate.Struct(n) 63 suite.EqualError(err, "Key: 'Notification.ID' Error:Field validation for 'ID' failed on the 'ulid' tag") 64 } 65 66 func (suite *NotificationValidateTestSuite) TestValidateNotificationStatusID() { 67 n := happyNotification() 68 69 n.StatusID = "" 70 err := validate.Struct(n) 71 suite.EqualError(err, "Key: 'Notification.StatusID' Error:Field validation for 'StatusID' failed on the 'required_if' tag") 72 73 n.StatusID = "9HZJ76B6VXSKF" 74 err = validate.Struct(n) 75 suite.EqualError(err, "Key: 'Notification.StatusID' Error:Field validation for 'StatusID' failed on the 'ulid' tag") 76 77 n.StatusID = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!!!!!!!!!!!!" 78 err = validate.Struct(n) 79 suite.EqualError(err, "Key: 'Notification.StatusID' Error:Field validation for 'StatusID' failed on the 'ulid' tag") 80 81 n.StatusID = "" 82 n.NotificationType = gtsmodel.NotificationFollowRequest 83 err = validate.Struct(n) 84 suite.NoError(err) 85 } 86 87 func (suite *NotificationValidateTestSuite) TestValidateNotificationNoCreatedAt() { 88 n := happyNotification() 89 90 n.CreatedAt = time.Time{} 91 err := validate.Struct(n) 92 suite.NoError(err) 93 } 94 95 func TestNotificationValidateTestSuite(t *testing.T) { 96 suite.Run(t, new(NotificationValidateTestSuite)) 97 }