github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/app/slashcommands/command_invite_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package slashcommands 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/mattermost/mattermost-server/v5/model" 13 ) 14 15 func TestInviteProvider(t *testing.T) { 16 th := setup(t).initBasic() 17 defer th.tearDown() 18 19 channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN) 20 privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE) 21 dmChannel := th.createDmChannel(th.BasicUser2) 22 privateChannel2 := th.createChannelWithAnotherUser(th.BasicTeam, model.CHANNEL_PRIVATE, th.BasicUser2.Id) 23 24 basicUser3 := th.createUser() 25 th.linkUserToTeam(basicUser3, th.BasicTeam) 26 basicUser4 := th.createUser() 27 deactivatedUser := th.createUser() 28 th.App.UpdateActive(deactivatedUser, false) 29 30 var err *model.AppError 31 _, err = th.App.CreateBot(&model.Bot{ 32 Username: "bot1", 33 OwnerId: basicUser3.Id, 34 Description: "a test bot", 35 }) 36 require.Nil(t, err) 37 38 bot2, err := th.App.CreateBot(&model.Bot{ 39 Username: "bot2", 40 OwnerId: basicUser3.Id, 41 Description: "a test bot", 42 }) 43 require.Nil(t, err) 44 _, err = th.App.AddUserToTeam(th.BasicTeam.Id, bot2.UserId, basicUser3.Id) 45 require.Nil(t, err) 46 47 bot3, err := th.App.CreateBot(&model.Bot{ 48 Username: "bot3", 49 OwnerId: basicUser3.Id, 50 Description: "a test bot", 51 }) 52 require.Nil(t, err) 53 _, err = th.App.AddUserToTeam(th.BasicTeam.Id, bot3.UserId, basicUser3.Id) 54 require.Nil(t, err) 55 err = th.App.RemoveUserFromTeam(th.BasicTeam.Id, bot3.UserId, basicUser3.Id) 56 require.Nil(t, err) 57 58 InviteP := InviteProvider{} 59 args := &model.CommandArgs{ 60 T: func(s string, args ...interface{}) string { return s }, 61 ChannelId: th.BasicChannel.Id, 62 TeamId: th.BasicTeam.Id, 63 UserId: th.BasicUser.Id, 64 } 65 66 userAndWrongChannel := "@" + th.BasicUser2.Username + " wrongchannel1" 67 userAndChannel := "@" + th.BasicUser2.Username + " ~" + channel.Name + " " 68 userAndDisplayChannel := "@" + th.BasicUser2.Username + " ~" + channel.DisplayName + " " 69 userAndPrivateChannel := "@" + th.BasicUser2.Username + " ~" + privateChannel.Name 70 userAndDMChannel := "@" + basicUser3.Username + " ~" + dmChannel.Name 71 userAndInvalidPrivate := "@" + basicUser3.Username + " ~" + privateChannel2.Name 72 deactivatedUserPublicChannel := "@" + deactivatedUser.Username + " ~" + channel.Name 73 74 groupChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE) 75 _, err = th.App.AddChannelMember(th.BasicUser.Id, groupChannel, "", "") 76 require.Nil(t, err) 77 groupChannel.GroupConstrained = model.NewBool(true) 78 groupChannel, _ = th.App.UpdateChannel(groupChannel) 79 80 groupChannelNonUser := "@" + th.BasicUser2.Username + " ~" + groupChannel.Name 81 82 tests := []struct { 83 desc string 84 expected string 85 msg string 86 }{ 87 { 88 desc: "Missing user and channel in the command", 89 expected: "api.command_invite.missing_message.app_error", 90 msg: "", 91 }, 92 { 93 desc: "User added in the current channel", 94 expected: "", 95 msg: th.BasicUser2.Username, 96 }, 97 { 98 desc: "Add user to another channel not the current", 99 expected: "api.command_invite.success", 100 msg: userAndChannel, 101 }, 102 { 103 desc: "try to add a user to a direct channel", 104 expected: "api.command_invite.directchannel.app_error", 105 msg: userAndDMChannel, 106 }, 107 { 108 desc: "Try to add a user to a invalid channel", 109 expected: "api.command_invite.channel.error", 110 msg: userAndWrongChannel, 111 }, 112 { 113 desc: "Try to add a user to an private channel", 114 expected: "api.command_invite.success", 115 msg: userAndPrivateChannel, 116 }, 117 { 118 desc: "Using display channel name which is different form Channel name", 119 expected: "api.command_invite.channel.error", 120 msg: userAndDisplayChannel, 121 }, 122 { 123 desc: "Invalid user to current channel", 124 expected: "api.command_invite.missing_user.app_error", 125 msg: "@invalidUser123", 126 }, 127 { 128 desc: "Invalid user to current channel without @", 129 expected: "api.command_invite.missing_user.app_error", 130 msg: "invalidUser321", 131 }, 132 { 133 desc: "try to add a user which is not part of the team", 134 expected: "api.command_invite.user_not_in_team.app_error", 135 msg: basicUser4.Username, 136 }, 137 { 138 desc: "try to add a user not part of the group to a group channel", 139 expected: "api.command_invite.group_constrained_user_denied", 140 msg: groupChannelNonUser, 141 }, 142 { 143 desc: "try to add a user to a private channel with no permission", 144 expected: "api.command_invite.private_channel.app_error", 145 msg: userAndInvalidPrivate, 146 }, 147 { 148 desc: "try to add a deleted user to a public channel", 149 expected: "api.command_invite.missing_user.app_error", 150 msg: deactivatedUserPublicChannel, 151 }, 152 { 153 desc: "try to add bot to a public channel", 154 expected: "api.command_invite.user_not_in_team.app_error", 155 msg: "@bot1", 156 }, 157 { 158 desc: "add bot to a public channel", 159 expected: "", 160 msg: "@bot2", 161 }, 162 { 163 desc: "try to add bot removed from a team to a public channel", 164 expected: "api.command_invite.user_not_in_team.app_error", 165 msg: "@bot3", 166 }, 167 } 168 169 for _, test := range tests { 170 t.Run(test.desc, func(t *testing.T) { 171 actual := InviteP.DoCommand(th.App, args, test.msg).Text 172 assert.Equal(t, test.expected, actual) 173 }) 174 } 175 } 176 177 func TestInviteGroup(t *testing.T) { 178 th := setup(t).initBasic() 179 defer th.tearDown() 180 181 th.BasicTeam.GroupConstrained = model.NewBool(true) 182 var err *model.AppError 183 _, _ = th.App.AddTeamMember(th.BasicTeam.Id, th.BasicUser.Id) 184 _, err = th.App.AddTeamMember(th.BasicTeam.Id, th.BasicUser2.Id) 185 require.Nil(t, err) 186 th.BasicTeam, _ = th.App.UpdateTeam(th.BasicTeam) 187 188 privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE) 189 190 groupChannelUser1 := "@" + th.BasicUser.Username + " ~" + privateChannel.Name 191 groupChannelUser2 := "@" + th.BasicUser2.Username + " ~" + privateChannel.Name 192 basicUser3 := th.createUser() 193 groupChannelUser3 := "@" + basicUser3.Username + " ~" + privateChannel.Name 194 195 InviteP := InviteProvider{} 196 args := &model.CommandArgs{ 197 T: func(s string, args ...interface{}) string { return s }, 198 ChannelId: th.BasicChannel.Id, 199 TeamId: th.BasicTeam.Id, 200 UserId: th.BasicUser.Id, 201 } 202 203 tests := []struct { 204 desc string 205 expected string 206 msg string 207 }{ 208 { 209 desc: "try to add an existing user part of the group to a group channel", 210 expected: "api.command_invite.user_already_in_channel.app_error", 211 msg: groupChannelUser1, 212 }, 213 { 214 desc: "try to add a user part of the group to a group channel", 215 expected: "api.command_invite.success", 216 msg: groupChannelUser2, 217 }, 218 { 219 desc: "try to add a user NOT part of the group to a group channel", 220 expected: "api.command_invite.user_not_in_team.app_error", 221 msg: groupChannelUser3, 222 }, 223 } 224 225 for _, test := range tests { 226 t.Run(test.desc, func(t *testing.T) { 227 actual := InviteP.DoCommand(th.App, args, test.msg).Text 228 assert.Equal(t, test.expected, actual) 229 }) 230 } 231 }