channel_test.go 82.4 KB
Newer Older
1
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
=Corey Hulen's avatar
=Corey Hulen committed
2 3 4 5 6 7
// See License.txt for license information.

package api

import (
	"net/http"
hmhealey's avatar
hmhealey committed
8
	"strings"
=Corey Hulen's avatar
=Corey Hulen committed
9
	"testing"
=Corey Hulen's avatar
=Corey Hulen committed
10
	"time"
11 12

	"github.com/mattermost/platform/model"
13
	"github.com/mattermost/platform/store"
14
	"github.com/mattermost/platform/utils"
=Corey Hulen's avatar
=Corey Hulen committed
15 16 17
)

func TestCreateChannel(t *testing.T) {
18
	th := Setup().InitBasic().InitSystemAdmin()
19
	Client := th.BasicClient
20
	SystemAdminClient := th.SystemAdminClient
21
	team := th.BasicTeam
22
	th.LoginBasic2()
23 24 25
	team2 := th.CreateTeam(th.BasicClient)
	th.LoginBasic()
	th.BasicClient.SetTeamId(team.Id)
=Corey Hulen's avatar
=Corey Hulen committed
26

27
	channel := model.Channel{DisplayName: "Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
=Corey Hulen's avatar
=Corey Hulen committed
28 29 30 31 32 33 34 35 36 37
	rchannel, err := Client.CreateChannel(&channel)
	if err != nil {
		t.Fatal(err)
	}

	if rchannel.Data.(*model.Channel).Name != channel.Name {
		t.Fatal("full name didn't match")
	}

	rget := Client.Must(Client.GetChannels("")).Data.(*model.ChannelList)
38
	nameMatch := false
39
	for _, c := range *rget {
40 41 42 43 44 45 46
		if c.Name == channel.Name {
			nameMatch = true
		}
	}

	if !nameMatch {
		t.Fatal("Did not create channel with correct name")
=Corey Hulen's avatar
=Corey Hulen committed
47 48 49 50 51 52
	}

	if _, err := Client.CreateChannel(rchannel.Data.(*model.Channel)); err == nil {
		t.Fatal("Cannot create an existing")
	}

53 54
	savedId := rchannel.Data.(*model.Channel).Id

=Corey Hulen's avatar
=Corey Hulen committed
55 56
	rchannel.Data.(*model.Channel).Id = ""
	if _, err := Client.CreateChannel(rchannel.Data.(*model.Channel)); err != nil {
57
		if err.Id != "store.sql_channel.save_channel.exists.app_error" {
=Corey Hulen's avatar
=Corey Hulen committed
58 59 60 61
			t.Fatal(err)
		}
	}

62
	if _, err := Client.DoApiPost(Client.GetTeamRoute()+"/channels/create", "garbage"); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
63 64 65
		t.Fatal("should have been an error")
	}

66 67
	Client.DeleteChannel(savedId)
	if _, err := Client.CreateChannel(rchannel.Data.(*model.Channel)); err != nil {
68
		if err.Message != "A channel with that URL was previously created" {
69 70 71 72
			t.Fatal(err)
		}
	}

=Corey Hulen's avatar
=Corey Hulen committed
73 74 75 76 77 78
	channel = model.Channel{DisplayName: "Channel on Different Team", Name: "aaaa" + model.NewId() + "abbb", Type: model.CHANNEL_OPEN, TeamId: team2.Id}

	if _, err := Client.CreateChannel(&channel); err.StatusCode != http.StatusForbidden {
		t.Fatal(err)
	}

79 80 81 82 83 84
	channel = model.Channel{DisplayName: "Channel With No TeamId", Name: "aaaa" + model.NewId() + "abbb", Type: model.CHANNEL_OPEN, TeamId: ""}

	if _, err := Client.CreateChannel(&channel); err != nil {
		t.Fatal(err)
	}

=Corey Hulen's avatar
=Corey Hulen committed
85 86 87 88 89 90
	channel = model.Channel{DisplayName: "Test API Name", Name: model.NewId() + "__" + model.NewId(), Type: model.CHANNEL_OPEN, TeamId: team.Id}

	if _, err := Client.CreateChannel(&channel); err == nil {
		t.Fatal("Should have errored out on invalid '__' character")
	}

91
	channel = model.Channel{DisplayName: "Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_DIRECT, TeamId: team.Id}
=Corey Hulen's avatar
=Corey Hulen committed
92 93 94 95

	if _, err := Client.CreateChannel(&channel); err == nil {
		t.Fatal("Should have errored out on direct channel type")
	}
96

97 98
	isLicensed := utils.IsLicensed()
	license := utils.License()
99 100 101 102 103
	restrictPublicChannel := *utils.Cfg.TeamSettings.RestrictPublicChannelManagement
	restrictPrivateChannel := *utils.Cfg.TeamSettings.RestrictPrivateChannelManagement
	defer func() {
		*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = restrictPublicChannel
		*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = restrictPrivateChannel
104 105
		utils.SetIsLicensed(isLicensed)
		utils.SetLicense(license)
106
		utils.SetDefaultRolesBasedOnConfig()
107 108 109
	}()
	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_ALL
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_ALL
110
	utils.SetDefaultRolesBasedOnConfig()
111 112 113
	utils.SetIsLicensed(true)
	utils.SetLicense(&model.License{Features: &model.Features{}})
	utils.License().Features.SetDefaults()
114

115 116
	channel2 := &model.Channel{DisplayName: "Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	channel3 := &model.Channel{DisplayName: "Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
117 118 119 120 121 122 123
	if _, err := Client.CreateChannel(channel2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.CreateChannel(channel3); err != nil {
		t.Fatal(err)
	}

124 125
	*utils.Cfg.TeamSettings.RestrictPublicChannelCreation = model.PERMISSIONS_TEAM_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelCreation = model.PERMISSIONS_TEAM_ADMIN
126
	utils.SetDefaultRolesBasedOnConfig()
127

128
	th.LoginBasic2()
129 130
	channel2.Name = "zz" + model.NewId() + "a"
	channel3.Name = "zz" + model.NewId() + "a"
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	if _, err := Client.CreateChannel(channel2); err == nil {
		t.Fatal("should have errored not team admin")
	}
	if _, err := Client.CreateChannel(channel3); err == nil {
		t.Fatal("should have errored not team admin")
	}

	UpdateUserToTeamAdmin(th.BasicUser, team)
	Client.Logout()
	th.LoginBasic()
	Client.SetTeamId(team.Id)

	if _, err := Client.CreateChannel(channel2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.CreateChannel(channel3); err != nil {
		t.Fatal(err)
	}

150 151
	*utils.Cfg.TeamSettings.RestrictPublicChannelCreation = model.PERMISSIONS_SYSTEM_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelCreation = model.PERMISSIONS_SYSTEM_ADMIN
152
	utils.SetDefaultRolesBasedOnConfig()
153

154 155
	channel2.Name = "zz" + model.NewId() + "a"
	channel3.Name = "zz" + model.NewId() + "a"
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	if _, err := Client.CreateChannel(channel2); err == nil {
		t.Fatal("should have errored not system admin")
	}
	if _, err := Client.CreateChannel(channel3); err == nil {
		t.Fatal("should have errored not system admin")
	}

	LinkUserToTeam(th.SystemAdminUser, team)

	if _, err := SystemAdminClient.CreateChannel(channel2); err != nil {
		t.Fatal(err)
	}
	if _, err := SystemAdminClient.CreateChannel(channel3); err != nil {
		t.Fatal(err)
	}
171

172
	// Check that if unlicensed the policy restriction is not enforced.
173 174
	utils.SetIsLicensed(false)
	utils.SetLicense(nil)
175 176
	utils.SetDefaultRolesBasedOnConfig()

177 178
	channel4 := model.Channel{DisplayName: "Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	channel5 := model.Channel{DisplayName: "Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
179 180 181 182 183 184 185
	if _, err := Client.CreateChannel(&channel4); err != nil {
		t.Fatal("should have succeeded")
	}
	if _, err := Client.CreateChannel(&channel5); err != nil {
		t.Fatal("should have succeeded")
	}

186 187 188
	*utils.Cfg.TeamSettings.RestrictPublicChannelCreation = model.PERMISSIONS_ALL
	*utils.Cfg.TeamSettings.RestrictPrivateChannelCreation = model.PERMISSIONS_ALL
	utils.SetDefaultRolesBasedOnConfig()
=Corey Hulen's avatar
=Corey Hulen committed
189 190 191
}

func TestCreateDirectChannel(t *testing.T) {
192 193 194 195
	th := Setup().InitBasic()
	Client := th.BasicClient
	user := th.BasicUser
	user2 := th.BasicUser2
=Corey Hulen's avatar
=Corey Hulen committed
196

197 198
	var channel *model.Channel
	if result, err := Client.CreateDirectChannel(th.BasicUser2.Id); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
199
		t.Fatal(err)
200 201
	} else {
		channel = result.Data.(*model.Channel)
=Corey Hulen's avatar
=Corey Hulen committed
202 203 204 205 206 207 208 209 210
	}

	channelName := ""
	if user2.Id > user.Id {
		channelName = user.Id + "__" + user2.Id
	} else {
		channelName = user2.Id + "__" + user.Id
	}

211
	if channel.Name != channelName {
=Corey Hulen's avatar
=Corey Hulen committed
212 213 214
		t.Fatal("channel name didn't match")
	}

215
	if channel.Type != model.CHANNEL_DIRECT {
=Corey Hulen's avatar
=Corey Hulen committed
216 217 218
		t.Fatal("channel type was not direct")
	}

219 220
	// Don't fail on direct channels already existing and return the original channel again
	if result, err := Client.CreateDirectChannel(th.BasicUser2.Id); err != nil {
221
		t.Fatal(err)
222 223
	} else if result.Data.(*model.Channel).Id != channel.Id {
		t.Fatal("didn't return original direct channel when saving a duplicate")
=Corey Hulen's avatar
=Corey Hulen committed
224 225
	}

226
	if _, err := Client.CreateDirectChannel("junk"); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
227 228 229
		t.Fatal("should have failed with bad user id")
	}

230
	if _, err := Client.CreateDirectChannel("12345678901234567890123456"); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
231 232 233 234
		t.Fatal("should have failed with non-existent user")
	}
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
func TestCreateGroupChannel(t *testing.T) {
	th := Setup().InitBasic()
	Client := th.BasicClient
	user := th.BasicUser
	user2 := th.BasicUser2
	user3 := th.CreateUser(Client)

	userIds := []string{user.Id, user2.Id, user3.Id}

	var channel *model.Channel
	if result, err := Client.CreateGroupChannel(userIds); err != nil {
		t.Fatal(err)
	} else {
		channel = result.Data.(*model.Channel)
	}

	if channel.Type != model.CHANNEL_GROUP {
		t.Fatal("channel type was not group")
	}

	// Don't fail on group channels already existing and return the original channel again
	if result, err := Client.CreateGroupChannel(userIds); err != nil {
		t.Fatal(err)
	} else if result.Data.(*model.Channel).Id != channel.Id {
		t.Fatal("didn't return original group channel when saving a duplicate")
	}

	if _, err := Client.CreateGroupChannel([]string{user.Id}); err == nil {
		t.Fatal("should have failed with not enough users")
	}

	if _, err := Client.CreateGroupChannel([]string{}); err == nil {
		t.Fatal("should have failed with not enough users")
	}

	if _, err := Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, "junk"}); err == nil {
		t.Fatal("should have failed with non-existent user")
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
275
func TestUpdateChannel(t *testing.T) {
276
	th := Setup().InitBasic().InitSystemAdmin()
277 278 279 280 281 282
	Client := th.SystemAdminClient
	team := th.SystemAdminTeam
	sysAdminUser := th.SystemAdminUser
	user := th.CreateUser(Client)
	LinkUserToTeam(user, team)
	user2 := th.CreateUser(Client)
283
	LinkUserToTeam(user2, team)
=Corey Hulen's avatar
=Corey Hulen committed
284

285 286
	Client.Login(user.Email, user.Password)

287
	channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
=Corey Hulen's avatar
=Corey Hulen committed
288 289
	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)

290
	Client.AddChannelMember(channel1.Id, user.Id)
=Corey Hulen's avatar
=Corey Hulen committed
291

292 293
	header := "zz" + model.NewId() + "a"
	purpose := "zz" + model.NewId() + "a"
hmhealey's avatar
hmhealey committed
294
	upChannel1 := &model.Channel{Id: channel1.Id, Header: header, Purpose: purpose}
=Corey Hulen's avatar
=Corey Hulen committed
295 296
	upChannel1 = Client.Must(Client.UpdateChannel(upChannel1)).Data.(*model.Channel)

297 298
	if upChannel1.Header != header {
		t.Fatal("Channel admin failed to update header")
=Corey Hulen's avatar
=Corey Hulen committed
299 300
	}

hmhealey's avatar
hmhealey committed
301 302 303 304
	if upChannel1.Purpose != purpose {
		t.Fatal("Channel admin failed to update purpose")
	}

=Corey Hulen's avatar
=Corey Hulen committed
305 306 307 308 309
	if upChannel1.DisplayName != channel1.DisplayName {
		t.Fatal("Channel admin failed to skip displayName")
	}

	rget := Client.Must(Client.GetChannels(""))
310 311
	channels := rget.Data.(*model.ChannelList)
	for _, c := range *channels {
=Corey Hulen's avatar
=Corey Hulen committed
312
		if c.Name == model.DEFAULT_CHANNEL {
313
			c.Header = "new header"
Reed Garmsen's avatar
Reed Garmsen committed
314
			c.Name = "pseudo-square"
=Corey Hulen's avatar
=Corey Hulen committed
315
			if _, err := Client.UpdateChannel(c); err == nil {
Reed Garmsen's avatar
Reed Garmsen committed
316
				t.Fatal("should have errored on updating default channel name")
=Corey Hulen's avatar
=Corey Hulen committed
317 318 319 320 321
			}
			break
		}
	}

322
	Client.Login(user2.Email, user2.Password)
=Corey Hulen's avatar
=Corey Hulen committed
323 324 325 326

	if _, err := Client.UpdateChannel(upChannel1); err == nil {
		t.Fatal("Standard User should have failed to update")
	}
327 328 329 330

	Client.Must(Client.JoinChannel(channel1.Id))
	UpdateUserToTeamAdmin(user2, team)

331 332 333 334
	Client.Logout()
	Client.Login(user2.Email, user2.Password)
	Client.SetTeamId(team.Id)

335 336 337
	if _, err := Client.UpdateChannel(upChannel1); err != nil {
		t.Fatal(err)
	}
338 339

	Client.Login(sysAdminUser.Email, sysAdminUser.Password)
340
	LinkUserToTeam(sysAdminUser, team)
341 342 343 344 345 346 347 348 349 350 351
	Client.Must(Client.JoinChannel(channel1.Id))

	if _, err := Client.UpdateChannel(upChannel1); err != nil {
		t.Fatal(err)
	}

	Client.Must(Client.DeleteChannel(channel1.Id))

	if _, err := Client.UpdateChannel(upChannel1); err == nil {
		t.Fatal("should have failed - channel deleted")
	}
352

353 354
	isLicensed := utils.IsLicensed()
	license := utils.License()
355 356 357 358 359
	restrictPublicChannel := *utils.Cfg.TeamSettings.RestrictPublicChannelManagement
	restrictPrivateChannel := *utils.Cfg.TeamSettings.RestrictPrivateChannelManagement
	defer func() {
		*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = restrictPublicChannel
		*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = restrictPrivateChannel
360 361
		utils.SetIsLicensed(isLicensed)
		utils.SetLicense(license)
362
		utils.SetDefaultRolesBasedOnConfig()
363 364 365
	}()
	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_ALL
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_ALL
366 367 368
	utils.SetIsLicensed(true)
	utils.SetLicense(&model.License{Features: &model.Features{}})
	utils.License().Features.SetDefaults()
369
	utils.SetDefaultRolesBasedOnConfig()
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

	channel2 := th.CreateChannel(Client, team)
	channel3 := th.CreatePrivateChannel(Client, team)

	LinkUserToTeam(th.BasicUser, team)

	Client.Must(Client.AddChannelMember(channel2.Id, th.BasicUser.Id))
	Client.Must(Client.AddChannelMember(channel3.Id, th.BasicUser.Id))

	Client.Login(th.BasicUser.Email, th.BasicUser.Password)

	if _, err := Client.UpdateChannel(channel2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannel(channel3); err != nil {
		t.Fatal(err)
	}

388 389
	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_CHANNEL_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_CHANNEL_ADMIN
390 391 392
	utils.SetIsLicensed(true)
	utils.SetLicense(&model.License{Features: &model.Features{}})
	utils.License().Features.SetDefaults()
393 394 395 396 397 398
	utils.SetDefaultRolesBasedOnConfig()
	MakeUserChannelUser(th.BasicUser, channel2)
	MakeUserChannelUser(th.BasicUser, channel3)
	store.ClearChannelCaches()

	if _, err := Client.UpdateChannel(channel2); err == nil {
399
		t.Fatal("should have errored not channel admin")
400 401
	}
	if _, err := Client.UpdateChannel(channel3); err == nil {
402
		t.Fatal("should have errored not channel admin")
403 404
	}

405
	UpdateUserToTeamAdmin(th.BasicUser, team)
Chris's avatar
Chris committed
406
	th.App.InvalidateAllCaches()
407 408 409 410 411 412 413
	if _, err := Client.UpdateChannel(channel2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannel(channel3); err != nil {
		t.Fatal(err)
	}
	UpdateUserToNonTeamAdmin(th.BasicUser, team)
Chris's avatar
Chris committed
414
	th.App.InvalidateAllCaches()
415

416 417 418 419 420 421 422 423 424 425 426
	MakeUserChannelAdmin(th.BasicUser, channel2)
	MakeUserChannelAdmin(th.BasicUser, channel3)
	store.ClearChannelCaches()

	if _, err := Client.UpdateChannel(channel2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannel(channel3); err != nil {
		t.Fatal(err)
	}

427 428
	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_TEAM_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_TEAM_ADMIN
429 430 431
	utils.SetIsLicensed(true)
	utils.SetLicense(&model.License{Features: &model.Features{}})
	utils.License().Features.SetDefaults()
432
	utils.SetDefaultRolesBasedOnConfig()
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

	if _, err := Client.UpdateChannel(channel2); err == nil {
		t.Fatal("should have errored not team admin")
	}
	if _, err := Client.UpdateChannel(channel3); err == nil {
		t.Fatal("should have errored not team admin")
	}

	UpdateUserToTeamAdmin(th.BasicUser, team)
	Client.Logout()
	Client.Login(th.BasicUser.Email, th.BasicUser.Password)
	Client.SetTeamId(team.Id)

	if _, err := Client.UpdateChannel(channel2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannel(channel3); err != nil {
		t.Fatal(err)
	}

	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_SYSTEM_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_SYSTEM_ADMIN
455 456 457
	utils.SetIsLicensed(true)
	utils.SetLicense(&model.License{Features: &model.Features{}})
	utils.License().Features.SetDefaults()
458
	utils.SetDefaultRolesBasedOnConfig()
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

	if _, err := Client.UpdateChannel(channel2); err == nil {
		t.Fatal("should have errored not system admin")
	}
	if _, err := Client.UpdateChannel(channel3); err == nil {
		t.Fatal("should have errored not system admin")
	}

	th.LoginSystemAdmin()

	if _, err := Client.UpdateChannel(channel2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannel(channel3); err != nil {
		t.Fatal(err)
	}
475 476

	// Check that if unlicensed the policy restriction is not enforced.
477 478
	utils.SetIsLicensed(false)
	utils.SetLicense(nil)
479 480 481 482 483 484 485 486
	utils.SetDefaultRolesBasedOnConfig()

	if _, err := Client.UpdateChannel(channel2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannel(channel3); err != nil {
		t.Fatal(err)
	}
=Corey Hulen's avatar
=Corey Hulen committed
487 488
}

489 490 491 492 493 494 495 496 497
func TestUpdateChannelDisplayName(t *testing.T) {
	th := Setup().InitBasic().InitSystemAdmin()
	Client := th.SystemAdminClient
	team := th.SystemAdminTeam
	user := th.CreateUser(Client)
	LinkUserToTeam(user, team)

	Client.Login(user.Email, user.Password)

498
	channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)

	Client.AddChannelMember(channel1.Id, user.Id)

	newDisplayName := "a" + channel1.DisplayName + "a"
	channel1.DisplayName = newDisplayName
	channel1 = Client.Must(Client.UpdateChannel(channel1)).Data.(*model.Channel)

	time.Sleep(100 * time.Millisecond)

	r1 := Client.Must(Client.GetPosts(channel1.Id, 0, 1, "")).Data.(*model.PostList)
	if len(r1.Order) != 1 {
		t.Fatal("Displayname update system message was not found")
	}
}

515
func TestUpdateChannelHeader(t *testing.T) {
516
	th := Setup().InitBasic().InitSystemAdmin()
517
	Client := th.BasicClient
518
	SystemAdminClient := th.SystemAdminClient
519
	team := th.BasicTeam
=Corey Hulen's avatar
=Corey Hulen committed
520

521
	channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
=Corey Hulen's avatar
=Corey Hulen committed
522 523 524 525
	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)

	data := make(map[string]string)
	data["channel_id"] = channel1.Id
526
	data["channel_header"] = "new header"
=Corey Hulen's avatar
=Corey Hulen committed
527 528

	var upChannel1 *model.Channel
529
	if result, err := Client.UpdateChannelHeader(data); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
530 531 532 533 534
		t.Fatal(err)
	} else {
		upChannel1 = result.Data.(*model.Channel)
	}

535 536
	time.Sleep(100 * time.Millisecond)

537 538 539 540 541 542 543 544 545
	r1 := Client.Must(Client.GetPosts(channel1.Id, 0, 1, "")).Data.(*model.PostList)
	if len(r1.Order) != 1 {
		t.Fatal("Header update system message was not found")
	} else if val, ok := r1.Posts[r1.Order[0]].Props["old_header"]; !ok || val != "" {
		t.Fatal("Props should contain old_header with old header value")
	} else if val, ok := r1.Posts[r1.Order[0]].Props["new_header"]; !ok || val != "new header" {
		t.Fatal("Props should contain new_header with new header value")
	}

546 547
	if upChannel1.Header != data["channel_header"] {
		t.Fatal("Failed to update header")
=Corey Hulen's avatar
=Corey Hulen committed
548 549 550
	}

	data["channel_id"] = "junk"
551
	if _, err := Client.UpdateChannelHeader(data); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
552 553 554 555
		t.Fatal("should have errored on junk channel id")
	}

	data["channel_id"] = "12345678901234567890123456"
556
	if _, err := Client.UpdateChannelHeader(data); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
557 558 559 560
		t.Fatal("should have errored on non-existent channel id")
	}

	data["channel_id"] = channel1.Id
hmhealey's avatar
hmhealey committed
561
	data["channel_header"] = strings.Repeat("a", 1050)
562 563
	if _, err := Client.UpdateChannelHeader(data); err == nil {
		t.Fatal("should have errored on bad channel header")
=Corey Hulen's avatar
=Corey Hulen committed
564 565
	}

566 567 568 569 570 571 572 573 574 575 576 577 578 579
	rchannel := Client.Must(Client.CreateDirectChannel(th.BasicUser2.Id)).Data.(*model.Channel)
	data["channel_id"] = rchannel.Id
	data["channel_header"] = "new header"
	var upChanneld *model.Channel
	if result, err := Client.UpdateChannelHeader(data); err != nil {
		t.Fatal(err)
	} else {
		upChanneld = result.Data.(*model.Channel)
	}

	if upChanneld.Header != data["channel_header"] {
		t.Fatal("Failed to update header")
	}

580
	th.LoginBasic2()
=Corey Hulen's avatar
=Corey Hulen committed
581 582

	data["channel_id"] = channel1.Id
583 584 585
	data["channel_header"] = "new header"
	if _, err := Client.UpdateChannelHeader(data); err == nil {
		t.Fatal("should have errored non-channel member trying to update header")
=Corey Hulen's avatar
=Corey Hulen committed
586
	}
587

588 589
	isLicensed := utils.IsLicensed()
	license := utils.License()
590 591 592 593 594
	restrictPublicChannel := *utils.Cfg.TeamSettings.RestrictPublicChannelManagement
	restrictPrivateChannel := *utils.Cfg.TeamSettings.RestrictPrivateChannelManagement
	defer func() {
		*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = restrictPublicChannel
		*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = restrictPrivateChannel
595 596
		utils.SetIsLicensed(isLicensed)
		utils.SetLicense(license)
597
		utils.SetDefaultRolesBasedOnConfig()
598 599 600
	}()
	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_ALL
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_ALL
601 602 603
	utils.SetIsLicensed(true)
	utils.SetLicense(&model.License{Features: &model.Features{}})
	utils.License().Features.SetDefaults()
604
	utils.SetDefaultRolesBasedOnConfig()
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

	th.LoginBasic()
	channel2 := th.CreateChannel(Client, team)
	channel3 := th.CreatePrivateChannel(Client, team)

	data2 := make(map[string]string)
	data2["channel_id"] = channel2.Id
	data2["channel_header"] = "new header"

	data3 := make(map[string]string)
	data3["channel_id"] = channel3.Id
	data3["channel_header"] = "new header"

	Client.Login(th.BasicUser.Email, th.BasicUser.Password)

	if _, err := Client.UpdateChannelHeader(data2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannelHeader(data3); err != nil {
		t.Fatal(err)
	}

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_CHANNEL_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_CHANNEL_ADMIN
	utils.SetDefaultRolesBasedOnConfig()
	MakeUserChannelUser(th.BasicUser, channel2)
	MakeUserChannelUser(th.BasicUser, channel3)
	store.ClearChannelCaches()

	if _, err := Client.UpdateChannelHeader(data2); err == nil {
		t.Fatal("should have errored not channel admin")
	}
	if _, err := Client.UpdateChannelHeader(data3); err == nil {
		t.Fatal("should have errored not channel admin")
	}

	MakeUserChannelAdmin(th.BasicUser, channel2)
	MakeUserChannelAdmin(th.BasicUser, channel3)
	store.ClearChannelCaches()

	if _, err := Client.UpdateChannelHeader(data2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannelHeader(data3); err != nil {
		t.Fatal(err)
	}

652 653
	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_TEAM_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_TEAM_ADMIN
654
	utils.SetDefaultRolesBasedOnConfig()
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

	if _, err := Client.UpdateChannelHeader(data2); err == nil {
		t.Fatal("should have errored not team admin")
	}
	if _, err := Client.UpdateChannelHeader(data3); err == nil {
		t.Fatal("should have errored not team admin")
	}

	UpdateUserToTeamAdmin(th.BasicUser, team)
	Client.Logout()
	th.LoginBasic()
	Client.SetTeamId(team.Id)

	if _, err := Client.UpdateChannelHeader(data2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannelHeader(data3); err != nil {
		t.Fatal(err)
	}

	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_SYSTEM_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_SYSTEM_ADMIN
677
	utils.SetDefaultRolesBasedOnConfig()
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

	if _, err := Client.UpdateChannelHeader(data2); err == nil {
		t.Fatal("should have errored not system admin")
	}
	if _, err := Client.UpdateChannelHeader(data3); err == nil {
		t.Fatal("should have errored not system admin")
	}

	LinkUserToTeam(th.SystemAdminUser, team)
	Client.Must(Client.AddChannelMember(channel2.Id, th.SystemAdminUser.Id))
	Client.Must(Client.AddChannelMember(channel3.Id, th.SystemAdminUser.Id))
	th.LoginSystemAdmin()

	if _, err := SystemAdminClient.UpdateChannelHeader(data2); err != nil {
		t.Fatal(err)
	}
	if _, err := SystemAdminClient.UpdateChannelHeader(data3); err != nil {
		t.Fatal(err)
	}
697 698

	// Check that if unlicensed the policy restriction is not enforced.
699 700
	utils.SetIsLicensed(false)
	utils.SetLicense(nil)
701 702 703 704 705 706 707 708
	utils.SetDefaultRolesBasedOnConfig()

	if _, err := SystemAdminClient.UpdateChannelHeader(data2); err != nil {
		t.Fatal(err)
	}
	if _, err := SystemAdminClient.UpdateChannelHeader(data3); err != nil {
		t.Fatal(err)
	}
=Corey Hulen's avatar
=Corey Hulen committed
709 710
}

hmhealey's avatar
hmhealey committed
711
func TestUpdateChannelPurpose(t *testing.T) {
712
	th := Setup().InitBasic().InitSystemAdmin()
713
	Client := th.BasicClient
714
	SystemAdminClient := th.SystemAdminClient
715
	team := th.BasicTeam
hmhealey's avatar
hmhealey committed
716

717
	channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
hmhealey's avatar
hmhealey committed
718 719 720 721 722 723 724 725 726 727 728 729 730
	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)

	data := make(map[string]string)
	data["channel_id"] = channel1.Id
	data["channel_purpose"] = "new purpose"

	var upChannel1 *model.Channel
	if result, err := Client.UpdateChannelPurpose(data); err != nil {
		t.Fatal(err)
	} else {
		upChannel1 = result.Data.(*model.Channel)
	}

731 732 733 734 735 736 737 738 739 740 741
	time.Sleep(100 * time.Millisecond)

	r1 := Client.Must(Client.GetPosts(channel1.Id, 0, 1, "")).Data.(*model.PostList)
	if len(r1.Order) != 1 {
		t.Fatal("Purpose update system message was not found")
	} else if val, ok := r1.Posts[r1.Order[0]].Props["old_purpose"]; !ok || val != "" {
		t.Fatal("Props should contain old_header with old purpose value")
	} else if val, ok := r1.Posts[r1.Order[0]].Props["new_purpose"]; !ok || val != "new purpose" {
		t.Fatal("Props should contain new_header with new purpose value")
	}

hmhealey's avatar
hmhealey committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
	if upChannel1.Purpose != data["channel_purpose"] {
		t.Fatal("Failed to update purpose")
	}

	data["channel_id"] = "junk"
	if _, err := Client.UpdateChannelPurpose(data); err == nil {
		t.Fatal("should have errored on junk channel id")
	}

	data["channel_id"] = "12345678901234567890123456"
	if _, err := Client.UpdateChannelPurpose(data); err == nil {
		t.Fatal("should have errored on non-existent channel id")
	}

	data["channel_id"] = channel1.Id
757
	data["channel_purpose"] = strings.Repeat("a", 350)
hmhealey's avatar
hmhealey committed
758 759 760 761
	if _, err := Client.UpdateChannelPurpose(data); err == nil {
		t.Fatal("should have errored on bad channel purpose")
	}

762
	th.LoginBasic2()
hmhealey's avatar
hmhealey committed
763 764 765 766 767 768

	data["channel_id"] = channel1.Id
	data["channel_purpose"] = "new purpose"
	if _, err := Client.UpdateChannelPurpose(data); err == nil {
		t.Fatal("should have errored non-channel member trying to update purpose")
	}
769

770 771
	isLicensed := utils.IsLicensed()
	license := utils.License()
772 773 774 775 776
	restrictPublicChannel := *utils.Cfg.TeamSettings.RestrictPublicChannelManagement
	restrictPrivateChannel := *utils.Cfg.TeamSettings.RestrictPrivateChannelManagement
	defer func() {
		*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = restrictPublicChannel
		*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = restrictPrivateChannel
777 778
		utils.SetIsLicensed(isLicensed)
		utils.SetLicense(license)
779
		utils.SetDefaultRolesBasedOnConfig()
780 781 782
	}()
	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_ALL
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_ALL
783 784 785
	utils.SetIsLicensed(true)
	utils.SetLicense(&model.License{Features: &model.Features{}})
	utils.License().Features.SetDefaults()
786
	utils.SetDefaultRolesBasedOnConfig()
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

	th.LoginBasic()
	channel2 := th.CreateChannel(Client, team)
	channel3 := th.CreatePrivateChannel(Client, team)

	data2 := make(map[string]string)
	data2["channel_id"] = channel2.Id
	data2["channel_purpose"] = "new purpose"

	data3 := make(map[string]string)
	data3["channel_id"] = channel3.Id
	data3["channel_purpose"] = "new purpose"

	Client.Login(th.BasicUser.Email, th.BasicUser.Password)

	if _, err := Client.UpdateChannelPurpose(data2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannelPurpose(data3); err != nil {
		t.Fatal(err)
	}

809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_CHANNEL_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_CHANNEL_ADMIN
	utils.SetDefaultRolesBasedOnConfig()
	MakeUserChannelUser(th.BasicUser, channel2)
	MakeUserChannelUser(th.BasicUser, channel3)
	store.ClearChannelCaches()

	if _, err := Client.UpdateChannelPurpose(data2); err == nil {
		t.Fatal("should have errored not channel admin")
	}
	if _, err := Client.UpdateChannelPurpose(data3); err == nil {
		t.Fatal("should have errored not channel admin")
	}

	MakeUserChannelAdmin(th.BasicUser, channel2)
	MakeUserChannelAdmin(th.BasicUser, channel3)
	store.ClearChannelCaches()

	if _, err := Client.UpdateChannelPurpose(data2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannelPurpose(data3); err != nil {
		t.Fatal(err)
	}

834 835
	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_TEAM_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_TEAM_ADMIN
836
	utils.SetDefaultRolesBasedOnConfig()
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858

	if _, err := Client.UpdateChannelPurpose(data2); err == nil {
		t.Fatal("should have errored not team admin")
	}
	if _, err := Client.UpdateChannelPurpose(data3); err == nil {
		t.Fatal("should have errored not team admin")
	}

	UpdateUserToTeamAdmin(th.BasicUser, team)
	Client.Logout()
	th.LoginBasic()
	Client.SetTeamId(team.Id)

	if _, err := Client.UpdateChannelPurpose(data2); err != nil {
		t.Fatal(err)
	}
	if _, err := Client.UpdateChannelPurpose(data3); err != nil {
		t.Fatal(err)
	}

	*utils.Cfg.TeamSettings.RestrictPublicChannelManagement = model.PERMISSIONS_SYSTEM_ADMIN
	*utils.Cfg.TeamSettings.RestrictPrivateChannelManagement = model.PERMISSIONS_SYSTEM_ADMIN
859
	utils.SetDefaultRolesBasedOnConfig()
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

	if _, err := Client.UpdateChannelPurpose(data2); err == nil {
		t.Fatal("should have errored not system admin")
	}
	if _, err := Client.UpdateChannelPurpose(data3); err == nil {
		t.Fatal("should have errored not system admin")
	}

	LinkUserToTeam(th.SystemAdminUser, team)
	Client.Must(Client.AddChannelMember(channel2.Id, th.SystemAdminUser.Id))
	Client.Must(Client.AddChannelMember(channel3.Id, th.SystemAdminUser.Id))
	th.LoginSystemAdmin()

	if _, err := SystemAdminClient.UpdateChannelPurpose(data2); err != nil {
		t.Fatal(err)
	}
	if _, err := SystemAdminClient.UpdateChannelPurpose(data3); err != nil {
		t.Fatal(err)
	}
879 880

	// Check that if unlicensed the policy restriction is not enforced.
881 882
	utils.SetIsLicensed(false)
	utils.SetLicense(nil)
883 884 885 886 887 888 889
	utils.SetDefaultRolesBasedOnConfig()
	if _, err := SystemAdminClient.UpdateChannelHeader(data2); err != nil {
		t.Fatal(err)
	}
	if _, err := SystemAdminClient.UpdateChannelHeader(data3); err != nil {
		t.Fatal(err)
	}
hmhealey's avatar
hmhealey committed
890 891
}

=Corey Hulen's avatar
=Corey Hulen committed
892
func TestGetChannel(t *testing.T) {
893 894 895
	th := Setup().InitBasic()
	Client := th.BasicClient
	team := th.BasicTeam
896
	team2 := th.CreateTeam(Client)
=Corey Hulen's avatar
=Corey Hulen committed
897

898
	channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
=Corey Hulen's avatar
=Corey Hulen committed
899 900
	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)

901
	channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
=Corey Hulen's avatar
=Corey Hulen committed
902 903 904
	channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)

	rget := Client.Must(Client.GetChannels(""))
905
	channels := rget.Data.(*model.ChannelList)
=Corey Hulen's avatar
=Corey Hulen committed
906

907
	if (*channels)[0].DisplayName != channel1.DisplayName {
=Corey Hulen's avatar
=Corey Hulen committed
908 909 910
		t.Fatal("full name didn't match")
	}

911
	if (*channels)[1].DisplayName != channel2.DisplayName {
=Corey Hulen's avatar
=Corey Hulen committed
912 913 914 915 916 917 918 919 920 921 922
		t.Fatal("full name didn't match")
	}

	// test etag caching
	if cache_result, err := Client.GetChannels(rget.Etag); err != nil {
		t.Fatal(err)
	} else if cache_result.Data.(*model.ChannelList) != nil {
		t.Log(cache_result.Data)
		t.Fatal("cache should be empty")
	}

923 924 925
	view := model.ChannelView{ChannelId: channel2.Id, PrevChannelId: channel1.Id}
	if _, resp := Client.ViewChannel(view); resp.Error != nil {
		t.Fatal(resp.Error)
=Corey Hulen's avatar
=Corey Hulen committed
926
	}
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

	if resp, err := Client.GetChannel(channel1.Id, ""); err != nil {
		t.Fatal(err)
	} else {
		data := resp.Data.(*model.ChannelData)
		if data.Channel.DisplayName != channel1.DisplayName {
			t.Fatal("name didn't match")
		}

		// test etag caching
		if cache_result, err := Client.GetChannel(channel1.Id, resp.Etag); err != nil {
			t.Fatal(err)
		} else if cache_result.Data.(*model.ChannelData) != nil {
			t.Log(cache_result.Data)
			t.Fatal("cache should be empty")
		}
	}

	if _, err := Client.GetChannel("junk", ""); err == nil {
		t.Fatal("should have failed - bad channel id")
	}
948

949
	th.BasicClient.SetTeamId(team2.Id)
950 951 952
	if _, err := Client.GetChannel(channel2.Id, ""); err == nil {
		t.Fatal("should have failed - wrong team")
	}
953 954 955 956 957 958 959 960 961 962

	//Test if a wrong team id is supplied should return error
	if _, err := Client.CreateDirectChannel(th.BasicUser2.Id); err != nil {
		t.Fatal(err)
	}

	th.BasicClient.SetTeamId("nonexitingteamid")
	if _, err := Client.GetChannels(""); err == nil {
		t.Fatal("should have failed - wrong team id")
	}
=Corey Hulen's avatar
=Corey Hulen committed
963 964
}

965
func TestGetMoreChannelsPage(t *testing.T) {
966 967 968
	th := Setup().InitBasic()
	Client := th.BasicClient
	team := th.BasicTeam
=Corey Hulen's avatar
=Corey Hulen committed
969

970
	channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
=Corey Hulen's avatar
=Corey Hulen committed
971 972
	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)

973
	channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "b" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
=Corey Hulen's avatar
=Corey Hulen committed
974 975
	channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)

976 977 978
	channel3 := &model.Channel{DisplayName: "C Test API Name", Name: "c" + model.NewId() + "a", Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
	channel3 = Client.Must(Client.CreateChannel(channel3)).Data.(*model.Channel)

979
	th.LoginBasic2()
=Corey Hulen's avatar
=Corey Hulen committed
980

981 982 983 984
	if r, err := Client.GetMoreChannelsPage(0, 100); err != nil {
		t.Fatal(err)
	} else {
		channels := r.Data.(*model.ChannelList)
=Corey Hulen's avatar
=Corey Hulen committed
985

Gabin Aureche's avatar
Gabin Aureche committed
986 987
		// 1 for BasicChannel, 1 for PinnedPostChannel, 2 for open channels created above
		if len(*channels) != 4 {
988 989 990 991 992 993 994 995 996 997
			t.Fatal("wrong length")
		}

		if (*channels)[0].DisplayName != channel1.DisplayName {
			t.Fatal("full name didn't match")
		}

		if (*channels)[1].DisplayName != channel2.DisplayName {
			t.Fatal("full name didn't match")
		}
=Corey Hulen's avatar
=Corey Hulen committed
998 999
	}

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	if r, err := Client.GetMoreChannelsPage(0, 1); err != nil {
		t.Fatal(err)
	} else {
		channels := r.Data.(*model.ChannelList)

		if len(*channels) != 1 {
			t.Fatal("wrong length")
		}

		if (*channels)[0].DisplayName != channel1.DisplayName {
			t.Fatal("full name didn't match")
		}
=Corey Hulen's avatar
=Corey Hulen committed
1012 1013
	}

1014
	if r, err := Client.GetMoreChannelsPage(1, 1); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
1015
		t.Fatal(err)
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
	} else {
		channels := r.Data.(*model.ChannelList)

		if len(*channels) != 1 {
			t.Fatal("wrong length")
		}

		if (*channels)[0].DisplayName != channel2.DisplayName {
			t.Fatal("full name didn't match")
		}
	}

	Client.SetTeamId("junk")
	if _, err := Client.GetMoreChannelsPage(0, 1); err == nil {
		t.Fatal("should have failed - bad team id")
=Corey Hulen's avatar
=Corey Hulen committed
1031 1032 1033
	}
}

1034
func TestGetChannelCounts(t *testing.T) {
1035 1036 1037
	th := Setup().InitBasic()
	Client := th.BasicClient
	team := th.BasicTeam
1038

1039
	channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
1040 1041
	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)

1042
	channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
1043 1044 1045 1046 1047 1048 1049
	channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)

	if result, err := Client.GetChannelCounts(""); err != nil {
		t.Fatal(err)
	} else {
		counts := result.Data.(*model.ChannelCounts)

Gabin Aureche's avatar
Gabin Aureche committed
1050
		if len(counts.Counts) != 6 {
1051 1052 1053
			t.Fatal("wrong number of channel counts")
		}

Gabin Aureche's avatar
Gabin Aureche committed
1054
		if len(counts.UpdateTimes) != 6 {
1055 1056
			t.Fatal("wrong number of channel update times")
		}
1057 1058 1059 1060 1061

		if cache_result, err := Client.GetChannelCounts(result.Etag); err != nil {
			t.Fatal(err)
		} else if cache_result.Data.(*model.ChannelCounts) != nil {
			t.Log(cache_result.Data)
1062
			t.Fatal("result data should be empty")
1063
		}
1064 1065 1066 1067
	}

}

1068 1069 1070 1071 1072
func TestGetMyChannelMembers(t *testing.T) {
	th := Setup().InitBasic()
	Client := th.BasicClient
	team := th.BasicTeam

1073
	channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
1074 1075
	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)

1076
	channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
1077 1078 1079 1080 1081 1082 1083
	channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)

	if result, err := Client.GetMyChannelMembers(); err != nil {
		t.Fatal(err)
	} else {
		members := result.Data.(*model.ChannelMembers)

Gabin Aureche's avatar
Gabin Aureche committed
1084 1085
		// town-square, off-topic, basic test channel, pinned post channel, channel1, channel2
		if len(*members) != 6 {
1086 1087 1088 1089 1090 1091
			t.Fatal("wrong number of members", len(*members))
		}
	}

}

1092
func TestJoinChannelById(t *testing.T) {
1093 1094 1095
	th := Setup().InitBasic()
	Client := th.BasicClient
	team := th.BasicTeam
=Corey Hulen's avatar
=Corey Hulen committed
1096

1097
	channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
=Corey Hulen's avatar
=Corey Hulen committed
1098 1099
	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)

1100
	channel3 := &model.Channel{DisplayName: "B Test API Name", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
=Corey Hulen's avatar
=Corey Hulen committed
1101 1102
	channel3 = Client.Must(Client.CreateChannel(channel3)).Data.(*model.Channel)

1103
	th.LoginBasic2()
=Corey Hulen's avatar
=Corey Hulen committed
1104 1105 1106 1107 1108 1109 1110

	Client.Must(Client.JoinChannel(channel1.Id))

	if _, err := Client.JoinChannel(channel3.Id); err == nil {
		t.Fatal("shouldn't be able to join secret group")
	}

1111
	rchannel := Client.Must(Client.CreateDirectChannel(th.BasicUser.Id)).Data.(*model.Channel)
=Corey Hulen's avatar
=Corey Hulen committed
1112

1113 1114
	user3 := th.CreateUser(th.BasicClient)
	LinkUserToTeam(user3, team)