github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/cmd/mattermost/commands/group_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package commands 5 6 import ( 7 "testing" 8 9 "github.com/mattermost/mattermost-server/v5/model" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestChannelGroupEnable(t *testing.T) { 14 th := Setup(t).InitBasic() 15 defer th.TearDown() 16 17 // create public channel 18 channel := th.CreatePublicChannel() 19 20 // try to enable, should fail it is private 21 require.Error(t, th.RunCommand(t, "group", "channel", "enable", th.BasicTeam.Name+":"+channel.Name)) 22 23 channel = th.CreatePrivateChannel() 24 25 // try to enable, should fail because channel has no groups 26 require.Error(t, th.RunCommand(t, "group", "channel", "enable", th.BasicTeam.Name+":"+channel.Name)) 27 28 // add group 29 id := model.NewId() 30 group, err := th.App.CreateGroup(&model.Group{ 31 DisplayName: "dn_" + id, 32 Name: model.NewString("name" + id), 33 Source: model.GroupSourceLdap, 34 Description: "description_" + id, 35 RemoteId: model.NewId(), 36 }) 37 require.Nil(t, err) 38 39 _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ 40 AutoAdd: true, 41 SyncableId: channel.Id, 42 Type: model.GroupSyncableTypeChannel, 43 GroupId: group.Id, 44 }) 45 require.Nil(t, err) 46 47 // enabling should succeed now 48 th.CheckCommand(t, "group", "channel", "enable", th.BasicTeam.Name+":"+channel.Name) 49 channel, appErr := th.App.GetChannelByName(channel.Name, th.BasicTeam.Id, false) 50 require.Nil(t, appErr) 51 require.NotNil(t, channel.GroupConstrained) 52 require.True(t, *channel.GroupConstrained) 53 54 // try to enable nonexistent channel, should fail 55 require.Error(t, th.RunCommand(t, "group", "channel", "enable", th.BasicTeam.Name+":"+channel.Name+"asdf")) 56 } 57 58 func TestChannelGroupDisable(t *testing.T) { 59 th := Setup(t).InitBasic() 60 defer th.TearDown() 61 62 // create private channel 63 channel := th.CreatePrivateChannel() 64 65 // try to disable, should work 66 th.CheckCommand(t, "group", "channel", "disable", th.BasicTeam.Name+":"+channel.Name) 67 channel, appErr := th.App.GetChannelByName(channel.Name, th.BasicTeam.Id, false) 68 require.Nil(t, appErr) 69 require.NotNil(t, channel.GroupConstrained) 70 require.False(t, *channel.GroupConstrained) 71 72 // add group and enable 73 id := model.NewId() 74 group, err := th.App.CreateGroup(&model.Group{ 75 DisplayName: "dn_" + id, 76 Name: model.NewString("name" + id), 77 Source: model.GroupSourceLdap, 78 Description: "description_" + id, 79 RemoteId: model.NewId(), 80 }) 81 require.Nil(t, err) 82 83 _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ 84 AutoAdd: true, 85 SyncableId: channel.Id, 86 Type: model.GroupSyncableTypeChannel, 87 GroupId: group.Id, 88 }) 89 require.Nil(t, err) 90 91 th.CheckCommand(t, "group", "channel", "enable", th.BasicTeam.Name+":"+channel.Name) 92 channel, appErr = th.App.GetChannelByName(channel.Name, th.BasicTeam.Id, false) 93 require.Nil(t, appErr) 94 require.NotNil(t, channel.GroupConstrained) 95 require.True(t, *channel.GroupConstrained) 96 97 // try to disable, should work 98 th.CheckCommand(t, "group", "channel", "disable", th.BasicTeam.Name+":"+channel.Name) 99 channel, appErr = th.App.GetChannelByName(channel.Name, th.BasicTeam.Id, false) 100 require.Nil(t, appErr) 101 require.NotNil(t, channel.GroupConstrained) 102 require.False(t, *channel.GroupConstrained) 103 104 // try to disable nonexistent channel, should fail 105 require.Error(t, th.RunCommand(t, "group", "channel", "disable", th.BasicTeam.Name+":"+channel.Name+"asdf")) 106 } 107 108 func TestChannelGroupStatus(t *testing.T) { 109 th := Setup(t).InitBasic() 110 defer th.TearDown() 111 112 // create private channel 113 channel := th.CreatePrivateChannel() 114 115 // get status, should be Disabled 116 output := th.CheckCommand(t, "group", "channel", "status", th.BasicTeam.Name+":"+channel.Name) 117 require.Contains(t, output, "Disabled") 118 119 // add group and enable 120 id := model.NewId() 121 group, err := th.App.CreateGroup(&model.Group{ 122 DisplayName: "dn_" + id, 123 Name: model.NewString("name" + id), 124 Source: model.GroupSourceLdap, 125 Description: "description_" + id, 126 RemoteId: model.NewId(), 127 }) 128 require.Nil(t, err) 129 130 _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ 131 AutoAdd: true, 132 SyncableId: channel.Id, 133 Type: model.GroupSyncableTypeChannel, 134 GroupId: group.Id, 135 }) 136 require.Nil(t, err) 137 138 th.CheckCommand(t, "group", "channel", "enable", th.BasicTeam.Name+":"+channel.Name) 139 channel, appErr := th.App.GetChannelByName(channel.Name, th.BasicTeam.Id, false) 140 require.Nil(t, appErr) 141 require.NotNil(t, channel.GroupConstrained) 142 require.True(t, *channel.GroupConstrained) 143 144 // get status, should be enabled 145 output = th.CheckCommand(t, "group", "channel", "status", th.BasicTeam.Name+":"+channel.Name) 146 require.Contains(t, output, "Enabled") 147 148 // try to get status of nonexistent channel, should fail 149 require.Error(t, th.RunCommand(t, "group", "channel", "status", th.BasicTeam.Name+":"+channel.Name+"asdf")) 150 } 151 152 func TestChannelGroupList(t *testing.T) { 153 th := Setup(t).InitBasic() 154 defer th.TearDown() 155 156 // create private channel 157 channel := th.CreatePrivateChannel() 158 159 // list groups for a channel with none, should work 160 th.CheckCommand(t, "group", "channel", "list", th.BasicTeam.Name+":"+channel.Name) 161 162 // add groups and enable 163 id1 := model.NewId() 164 g1, err := th.App.CreateGroup(&model.Group{ 165 DisplayName: "dn_" + id1, 166 Name: model.NewString("name" + id1), 167 Source: model.GroupSourceLdap, 168 Description: "description_" + id1, 169 RemoteId: model.NewId(), 170 }) 171 require.Nil(t, err) 172 173 _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ 174 AutoAdd: true, 175 SyncableId: channel.Id, 176 Type: model.GroupSyncableTypeChannel, 177 GroupId: g1.Id, 178 }) 179 require.Nil(t, err) 180 181 id2 := model.NewId() 182 g2, err := th.App.CreateGroup(&model.Group{ 183 DisplayName: "dn_" + id2, 184 Name: model.NewString("name" + id2), 185 Source: model.GroupSourceLdap, 186 Description: "description_" + id2, 187 RemoteId: model.NewId(), 188 }) 189 require.Nil(t, err) 190 191 _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ 192 AutoAdd: true, 193 SyncableId: channel.Id, 194 Type: model.GroupSyncableTypeChannel, 195 GroupId: g2.Id, 196 }) 197 require.Nil(t, err) 198 199 th.CheckCommand(t, "group", "channel", "enable", th.BasicTeam.Name+":"+channel.Name) 200 channel, appErr := th.App.GetChannelByName(channel.Name, th.BasicTeam.Id, false) 201 require.Nil(t, appErr) 202 require.NotNil(t, channel.GroupConstrained) 203 require.True(t, *channel.GroupConstrained) 204 205 // list groups 206 output := th.CheckCommand(t, "group", "channel", "list", th.BasicTeam.Name+":"+channel.Name) 207 require.Contains(t, output, g1.DisplayName) 208 require.Contains(t, output, g2.DisplayName) 209 210 // try to get list of nonexistent channel, should fail 211 require.Error(t, th.RunCommand(t, "group", "channel", "list", th.BasicTeam.Name+":"+channel.Name+"asdf")) 212 } 213 214 func TestTeamGroupEnable(t *testing.T) { 215 th := Setup(t).InitBasic() 216 defer th.TearDown() 217 218 // try to enable, should fail because team has no groups 219 require.Error(t, th.RunCommand(t, "group", "team", "enable", th.BasicTeam.Name)) 220 221 // add group 222 id := model.NewId() 223 group, err := th.App.CreateGroup(&model.Group{ 224 DisplayName: "dn_" + id, 225 Name: model.NewString("name" + id), 226 Source: model.GroupSourceLdap, 227 Description: "description_" + id, 228 RemoteId: model.NewId(), 229 }) 230 require.Nil(t, err) 231 232 _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ 233 AutoAdd: true, 234 SyncableId: th.BasicTeam.Id, 235 Type: model.GroupSyncableTypeTeam, 236 GroupId: group.Id, 237 }) 238 require.Nil(t, err) 239 240 // enabling should succeed now 241 th.CheckCommand(t, "group", "team", "enable", th.BasicTeam.Name) 242 team, appErr := th.App.GetTeamByName(th.BasicTeam.Name) 243 require.Nil(t, appErr) 244 require.NotNil(t, team.GroupConstrained) 245 require.True(t, *team.GroupConstrained) 246 247 // try to enable nonexistent team, should fail 248 require.Error(t, th.RunCommand(t, "group", "team", "enable", th.BasicTeam.Name+"asdf")) 249 } 250 251 func TestTeamGroupDisable(t *testing.T) { 252 th := Setup(t).InitBasic() 253 defer th.TearDown() 254 255 // try to disable, should work 256 th.CheckCommand(t, "group", "team", "disable", th.BasicTeam.Name) 257 team, appErr := th.App.GetTeamByName(th.BasicTeam.Name) 258 require.Nil(t, appErr) 259 require.NotNil(t, team.GroupConstrained) 260 require.False(t, *team.GroupConstrained) 261 262 // add group and enable 263 id := model.NewId() 264 group, err := th.App.CreateGroup(&model.Group{ 265 DisplayName: "dn_" + id, 266 Name: model.NewString("name" + id), 267 Source: model.GroupSourceLdap, 268 Description: "description_" + id, 269 RemoteId: model.NewId(), 270 }) 271 require.Nil(t, err) 272 273 _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ 274 AutoAdd: true, 275 SyncableId: team.Id, 276 Type: model.GroupSyncableTypeTeam, 277 GroupId: group.Id, 278 }) 279 require.Nil(t, err) 280 281 th.CheckCommand(t, "group", "team", "enable", th.BasicTeam.Name) 282 team, appErr = th.App.GetTeamByName(th.BasicTeam.Name) 283 require.Nil(t, appErr) 284 require.NotNil(t, team.GroupConstrained) 285 require.True(t, *team.GroupConstrained) 286 287 // try to disable, should work 288 th.CheckCommand(t, "group", "team", "disable", th.BasicTeam.Name) 289 team, appErr = th.App.GetTeamByName(th.BasicTeam.Name) 290 require.Nil(t, appErr) 291 require.NotNil(t, team.GroupConstrained) 292 require.False(t, *team.GroupConstrained) 293 294 // try to disable nonexistent team, should fail 295 require.Error(t, th.RunCommand(t, "group", "team", "disable", th.BasicTeam.Name+"asdf")) 296 } 297 298 func TestTeamGroupStatus(t *testing.T) { 299 th := Setup(t).InitBasic() 300 defer th.TearDown() 301 302 // get status, should be Disabled 303 output := th.CheckCommand(t, "group", "team", "status", th.BasicTeam.Name) 304 require.Contains(t, output, "Disabled") 305 306 // add group and enable 307 id := model.NewId() 308 group, err := th.App.CreateGroup(&model.Group{ 309 DisplayName: "dn_" + id, 310 Name: model.NewString("name" + id), 311 Source: model.GroupSourceLdap, 312 Description: "description_" + id, 313 RemoteId: model.NewId(), 314 }) 315 require.Nil(t, err) 316 317 _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ 318 AutoAdd: true, 319 SyncableId: th.BasicTeam.Id, 320 Type: model.GroupSyncableTypeTeam, 321 GroupId: group.Id, 322 }) 323 require.Nil(t, err) 324 325 th.CheckCommand(t, "group", "team", "enable", th.BasicTeam.Name) 326 team, appErr := th.App.GetTeamByName(th.BasicTeam.Name) 327 require.Nil(t, appErr) 328 require.NotNil(t, team.GroupConstrained) 329 require.True(t, *team.GroupConstrained) 330 331 // get status, should be enabled 332 output = th.CheckCommand(t, "group", "team", "status", th.BasicTeam.Name) 333 require.Contains(t, output, "Enabled") 334 335 // try to get status of nonexistent channel, should fail 336 require.Error(t, th.RunCommand(t, "group", "team", "status", th.BasicTeam.Name+"asdf")) 337 } 338 339 func TestTeamGroupList(t *testing.T) { 340 th := Setup(t).InitBasic() 341 defer th.TearDown() 342 343 // list groups for a team with none, should work 344 th.CheckCommand(t, "group", "team", "list", th.BasicTeam.Name) 345 346 // add groups and enable 347 id1 := model.NewId() 348 g1, err := th.App.CreateGroup(&model.Group{ 349 DisplayName: "dn_" + id1, 350 Name: model.NewString("name" + id1), 351 Source: model.GroupSourceLdap, 352 Description: "description_" + id1, 353 RemoteId: model.NewId(), 354 }) 355 require.Nil(t, err) 356 357 _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ 358 AutoAdd: true, 359 SyncableId: th.BasicTeam.Id, 360 Type: model.GroupSyncableTypeTeam, 361 GroupId: g1.Id, 362 }) 363 require.Nil(t, err) 364 365 id2 := model.NewId() 366 g2, err := th.App.CreateGroup(&model.Group{ 367 DisplayName: "dn_" + id2, 368 Name: model.NewString("name" + id2), 369 Source: model.GroupSourceLdap, 370 Description: "description_" + id2, 371 RemoteId: model.NewId(), 372 }) 373 require.Nil(t, err) 374 375 _, err = th.App.UpsertGroupSyncable(&model.GroupSyncable{ 376 AutoAdd: true, 377 SyncableId: th.BasicTeam.Id, 378 Type: model.GroupSyncableTypeTeam, 379 GroupId: g2.Id, 380 }) 381 require.Nil(t, err) 382 383 th.CheckCommand(t, "group", "team", "enable", th.BasicTeam.Name) 384 team, appErr := th.App.GetTeamByName(th.BasicTeam.Name) 385 require.Nil(t, appErr) 386 require.NotNil(t, team.GroupConstrained) 387 require.True(t, *team.GroupConstrained) 388 389 // list groups 390 output := th.CheckCommand(t, "group", "team", "list", th.BasicTeam.Name) 391 require.Contains(t, output, g1.DisplayName) 392 require.Contains(t, output, g2.DisplayName) 393 394 // try to get list of nonexistent team, should fail 395 require.Error(t, th.RunCommand(t, "group", "team", "list", th.BasicTeam.Name+"asdf")) 396 }