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