github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/model/reaction_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestReactionIsValid(t *testing.T) { 14 tests := []struct { 15 // reaction 16 reaction Reaction 17 // error message to print 18 errMsg string 19 // should there be an error 20 shouldErr bool 21 }{ 22 { 23 reaction: Reaction{ 24 UserId: NewId(), 25 PostId: NewId(), 26 EmojiName: "emoji", 27 CreateAt: GetMillis(), 28 UpdateAt: GetMillis(), 29 }, 30 errMsg: "", 31 shouldErr: false, 32 }, 33 { 34 reaction: Reaction{ 35 UserId: "", 36 PostId: NewId(), 37 EmojiName: "emoji", 38 CreateAt: GetMillis(), 39 UpdateAt: GetMillis(), 40 }, 41 errMsg: "user id should be invalid", 42 shouldErr: true, 43 }, 44 { 45 reaction: Reaction{ 46 UserId: "1234garbage", 47 PostId: NewId(), 48 EmojiName: "emoji", 49 CreateAt: GetMillis(), 50 UpdateAt: GetMillis(), 51 }, 52 errMsg: "user id should be invalid", 53 shouldErr: true, 54 }, 55 { 56 reaction: Reaction{ 57 UserId: NewId(), 58 PostId: "", 59 EmojiName: "emoji", 60 CreateAt: GetMillis(), 61 UpdateAt: GetMillis(), 62 }, 63 errMsg: "post id should be invalid", 64 shouldErr: true, 65 }, 66 { 67 reaction: Reaction{ 68 UserId: NewId(), 69 PostId: "1234garbage", 70 EmojiName: "emoji", 71 CreateAt: GetMillis(), 72 UpdateAt: GetMillis(), 73 }, 74 errMsg: "post id should be invalid", 75 shouldErr: true, 76 }, 77 { 78 reaction: Reaction{ 79 UserId: NewId(), 80 PostId: NewId(), 81 EmojiName: strings.Repeat("a", 64), 82 CreateAt: GetMillis(), 83 UpdateAt: GetMillis(), 84 }, 85 errMsg: "", 86 shouldErr: false, 87 }, 88 { 89 reaction: Reaction{ 90 UserId: NewId(), 91 PostId: NewId(), 92 EmojiName: "emoji-", 93 CreateAt: GetMillis(), 94 UpdateAt: GetMillis(), 95 }, 96 errMsg: "", 97 shouldErr: false, 98 }, 99 { 100 reaction: Reaction{ 101 UserId: NewId(), 102 PostId: NewId(), 103 EmojiName: "emoji_", 104 CreateAt: GetMillis(), 105 UpdateAt: GetMillis(), 106 }, 107 errMsg: "", 108 shouldErr: false, 109 }, 110 { 111 reaction: Reaction{ 112 UserId: NewId(), 113 PostId: NewId(), 114 EmojiName: "+1", 115 CreateAt: GetMillis(), 116 UpdateAt: GetMillis(), 117 }, 118 errMsg: "", 119 shouldErr: false, 120 }, 121 { 122 reaction: Reaction{ 123 UserId: NewId(), 124 PostId: NewId(), 125 EmojiName: "emoji:", 126 CreateAt: GetMillis(), 127 UpdateAt: GetMillis(), 128 }, 129 errMsg: "", 130 shouldErr: true, 131 }, 132 { 133 reaction: Reaction{ 134 UserId: NewId(), 135 PostId: NewId(), 136 EmojiName: "", 137 CreateAt: GetMillis(), 138 UpdateAt: GetMillis(), 139 }, 140 errMsg: "emoji name should be invalid", 141 shouldErr: true, 142 }, 143 { 144 reaction: Reaction{ 145 UserId: NewId(), 146 PostId: NewId(), 147 EmojiName: strings.Repeat("a", 65), 148 CreateAt: GetMillis(), 149 UpdateAt: GetMillis(), 150 }, 151 errMsg: "emoji name should be invalid", 152 shouldErr: true, 153 }, 154 { 155 reaction: Reaction{ 156 UserId: NewId(), 157 PostId: NewId(), 158 EmojiName: "emoji", 159 CreateAt: 0, 160 UpdateAt: GetMillis(), 161 }, 162 errMsg: "create at should be invalid", 163 shouldErr: true, 164 }, 165 { 166 reaction: Reaction{ 167 UserId: NewId(), 168 PostId: NewId(), 169 EmojiName: "emoji", 170 CreateAt: GetMillis(), 171 UpdateAt: 0, 172 }, 173 errMsg: "update at should be invalid", 174 shouldErr: true, 175 }, 176 } 177 178 for _, test := range tests { 179 err := test.reaction.IsValid() 180 if test.shouldErr { 181 // there should be an error here 182 require.NotNil(t, err, test.errMsg) 183 } else { 184 // err should be nil here 185 require.Nil(t, err, test.errMsg) 186 } 187 } 188 }