channel_test.go 40 KB
Newer Older
1
// Copyright (c) 2015 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 (
	"github.com/mattermost/platform/model"
=Corey Hulen's avatar
=Corey Hulen committed
8
	"github.com/mattermost/platform/store"
9
	"github.com/mattermost/platform/utils"
=Corey Hulen's avatar
=Corey Hulen committed
10
	"net/http"
hmhealey's avatar
hmhealey committed
11
	"strings"
=Corey Hulen's avatar
=Corey Hulen committed
12
	"testing"
=Corey Hulen's avatar
=Corey Hulen committed
13
	"time"
=Corey Hulen's avatar
=Corey Hulen committed
14 15 16 17 18
)

func TestCreateChannel(t *testing.T) {
	Setup()

19
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
20 21
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

22
	team2 := &model.Team{DisplayName: "Name Team 2", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
23 24
	team2 = Client.Must(Client.CreateTeam(team2)).Data.(*model.Team)

25
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
26
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
27
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user.Id))
=Corey Hulen's avatar
=Corey Hulen committed
28

29
	Client.LoginByEmail(team.Name, user.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
30 31 32 33 34 35 36 37 38 39 40 41

	channel := model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	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)
42 43 44 45 46 47 48 49 50
	nameMatch := false
	for _, c := range rget.Channels {
		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
51 52 53 54 55 56
	}

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

57 58
	savedId := rchannel.Data.(*model.Channel).Id

=Corey Hulen's avatar
=Corey Hulen committed
59 60
	rchannel.Data.(*model.Channel).Id = ""
	if _, err := Client.CreateChannel(rchannel.Data.(*model.Channel)); err != nil {
61
		if err.Message != "A channel with that URL already exists" {
=Corey Hulen's avatar
=Corey Hulen committed
62 63 64 65
			t.Fatal(err)
		}
	}

66
	if _, err := Client.DoApiPost("/channels/create", "garbage"); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
67 68 69
		t.Fatal("should have been an error")
	}

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

=Corey Hulen's avatar
=Corey Hulen committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	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)
	}

	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")
	}

	channel = model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_DIRECT, TeamId: team.Id}

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

func TestCreateDirectChannel(t *testing.T) {
	Setup()

99
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
100 101
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

102
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
103
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
104
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user.Id))
=Corey Hulen's avatar
=Corey Hulen committed
105

106
	user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
107
	user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
108
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user2.Id))
=Corey Hulen's avatar
=Corey Hulen committed
109

110
	Client.LoginByEmail(team.Name, user.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

	data := make(map[string]string)
	data["user_id"] = user2.Id

	rchannel, err := Client.CreateDirectChannel(data)
	if err != nil {
		t.Fatal(err)
	}

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

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

	if rchannel.Data.(*model.Channel).Type != model.CHANNEL_DIRECT {
		t.Fatal("channel type was not direct")
	}

	if _, err := Client.CreateDirectChannel(data); err == nil {
		t.Fatal("channel already exists and should have failed")
	}

	data["user_id"] = "junk"
	if _, err := Client.CreateDirectChannel(data); err == nil {
		t.Fatal("should have failed with bad user id")
	}

	data["user_id"] = "12345678901234567890123456"
	if _, err := Client.CreateDirectChannel(data); err == nil {
		t.Fatal("should have failed with non-existent user")
	}

}

func TestUpdateChannel(t *testing.T) {
	Setup()

154
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
155 156
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

157
	userTeamAdmin := &model.User{TeamId: team.Id, Email: team.Email, Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
158
	userTeamAdmin = Client.Must(Client.CreateUser(userTeamAdmin, "")).Data.(*model.User)
159
	store.Must(Srv.Store.User().VerifyEmail(utils.T, userTeamAdmin.Id))
=Corey Hulen's avatar
=Corey Hulen committed
160

161
	userChannelAdmin := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
162
	userChannelAdmin = Client.Must(Client.CreateUser(userChannelAdmin, "")).Data.(*model.User)
163
	store.Must(Srv.Store.User().VerifyEmail(utils.T, userChannelAdmin.Id))
=Corey Hulen's avatar
=Corey Hulen committed
164

165
	userStd := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
166
	userStd = Client.Must(Client.CreateUser(userStd, "")).Data.(*model.User)
167
	store.Must(Srv.Store.User().VerifyEmail(utils.T, userStd.Id))
=Corey Hulen's avatar
=Corey Hulen committed
168 169
	userStd.Roles = ""

170
	Client.LoginByEmail(team.Name, userChannelAdmin.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
171 172 173 174 175 176

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

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

177
	header := "a" + model.NewId() + "a"
hmhealey's avatar
hmhealey committed
178 179
	purpose := "a" + model.NewId() + "a"
	upChannel1 := &model.Channel{Id: channel1.Id, Header: header, Purpose: purpose}
=Corey Hulen's avatar
=Corey Hulen committed
180 181
	upChannel1 = Client.Must(Client.UpdateChannel(upChannel1)).Data.(*model.Channel)

182 183
	if upChannel1.Header != header {
		t.Fatal("Channel admin failed to update header")
=Corey Hulen's avatar
=Corey Hulen committed
184 185
	}

hmhealey's avatar
hmhealey committed
186 187 188 189
	if upChannel1.Purpose != purpose {
		t.Fatal("Channel admin failed to update purpose")
	}

=Corey Hulen's avatar
=Corey Hulen committed
190 191 192 193
	if upChannel1.DisplayName != channel1.DisplayName {
		t.Fatal("Channel admin failed to skip displayName")
	}

194
	Client.LoginByEmail(team.Name, userTeamAdmin.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
195

196
	header = "b" + model.NewId() + "b"
hmhealey's avatar
hmhealey committed
197 198
	purpose = "b" + model.NewId() + "b"
	upChannel1 = &model.Channel{Id: channel1.Id, Header: header, Purpose: purpose}
=Corey Hulen's avatar
=Corey Hulen committed
199 200
	upChannel1 = Client.Must(Client.UpdateChannel(upChannel1)).Data.(*model.Channel)

201 202
	if upChannel1.Header != header {
		t.Fatal("Team admin failed to update header")
=Corey Hulen's avatar
=Corey Hulen committed
203 204
	}

hmhealey's avatar
hmhealey committed
205 206 207 208
	if upChannel1.Purpose != purpose {
		t.Fatal("Team admin failed to update purpose")
	}

=Corey Hulen's avatar
=Corey Hulen committed
209 210 211 212 213 214 215 216
	if upChannel1.DisplayName != channel1.DisplayName {
		t.Fatal("Team admin failed to skip displayName")
	}

	rget := Client.Must(Client.GetChannels(""))
	data := rget.Data.(*model.ChannelList)
	for _, c := range data.Channels {
		if c.Name == model.DEFAULT_CHANNEL {
217
			c.Header = "new header"
Reed Garmsen's avatar
Reed Garmsen committed
218
			c.Name = "pseudo-square"
=Corey Hulen's avatar
=Corey Hulen committed
219
			if _, err := Client.UpdateChannel(c); err == nil {
Reed Garmsen's avatar
Reed Garmsen committed
220
				t.Fatal("should have errored on updating default channel name")
=Corey Hulen's avatar
=Corey Hulen committed
221 222 223 224 225
			}
			break
		}
	}

226
	Client.LoginByEmail(team.Name, userStd.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
227 228 229 230 231 232

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

233
func TestUpdateChannelHeader(t *testing.T) {
=Corey Hulen's avatar
=Corey Hulen committed
234 235
	Setup()

236
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
237 238
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

239
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
240
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
241
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user.Id))
=Corey Hulen's avatar
=Corey Hulen committed
242

243
	Client.LoginByEmail(team.Name, user.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
244 245 246 247 248 249

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

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

	var upChannel1 *model.Channel
253
	if result, err := Client.UpdateChannelHeader(data); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
254 255 256 257 258
		t.Fatal(err)
	} else {
		upChannel1 = result.Data.(*model.Channel)
	}

259 260
	if upChannel1.Header != data["channel_header"] {
		t.Fatal("Failed to update header")
=Corey Hulen's avatar
=Corey Hulen committed
261 262 263
	}

	data["channel_id"] = "junk"
264
	if _, err := Client.UpdateChannelHeader(data); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
265 266 267 268
		t.Fatal("should have errored on junk channel id")
	}

	data["channel_id"] = "12345678901234567890123456"
269
	if _, err := Client.UpdateChannelHeader(data); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
270 271 272 273
		t.Fatal("should have errored on non-existent channel id")
	}

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

279
	user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
280
	user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
281
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user2.Id))
=Corey Hulen's avatar
=Corey Hulen committed
282

283
	Client.LoginByEmail(team.Name, user2.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
284 285

	data["channel_id"] = channel1.Id
286 287 288
	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
289 290 291
	}
}

hmhealey's avatar
hmhealey committed
292 293 294 295 296 297
func TestUpdateChannelPurpose(t *testing.T) {
	Setup()

	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

298
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
hmhealey's avatar
hmhealey committed
299
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
300
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user.Id))
hmhealey's avatar
hmhealey committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

	Client.LoginByEmail(team.Name, user.Email, "pwd")

	channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	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)
	}

	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
	data["channel_purpose"] = strings.Repeat("a", 150)
	if _, err := Client.UpdateChannelPurpose(data); err == nil {
		t.Fatal("should have errored on bad channel purpose")
	}

338
	user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
hmhealey's avatar
hmhealey committed
339
	user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
340
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user2.Id))
hmhealey's avatar
hmhealey committed
341 342 343 344 345 346 347 348 349 350

	Client.LoginByEmail(team.Name, user2.Email, "pwd")

	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")
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
351 352 353
func TestGetChannel(t *testing.T) {
	Setup()

354
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
355 356
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

357
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
358
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
359
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user.Id))
=Corey Hulen's avatar
=Corey Hulen committed
360

361
	Client.LoginByEmail(team.Name, user.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

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

	channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)

	rget := Client.Must(Client.GetChannels(""))
	data := rget.Data.(*model.ChannelList)

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

	if data.Channels[1].DisplayName != channel2.DisplayName {
		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")
	}

	if _, err := Client.UpdateLastViewedAt(channel2.Id); err != nil {
		t.Fatal(err)
	}
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

	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")
	}
=Corey Hulen's avatar
=Corey Hulen committed
412 413 414 415 416
}

func TestGetMoreChannel(t *testing.T) {
	Setup()

417
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
418 419
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

420
	user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
421
	user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
422
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user1.Id))
=Corey Hulen's avatar
=Corey Hulen committed
423

424
	Client.LoginByEmail(team.Name, user1.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
425 426 427 428 429 430 431

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

	channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)

432
	user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
433
	user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
434
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user2.Id))
=Corey Hulen's avatar
=Corey Hulen committed
435

436
	Client.LoginByEmail(team.Name, user2.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

	rget := Client.Must(Client.GetMoreChannels(""))
	data := rget.Data.(*model.ChannelList)

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

	if data.Channels[1].DisplayName != channel2.DisplayName {
		t.Fatal("full name didn't match")
	}

	// test etag caching
	if cache_result, err := Client.GetMoreChannels(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")
	}
}

458 459 460 461 462 463
func TestGetChannelCounts(t *testing.T) {
	Setup()

	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

464
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
465
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
466
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user.Id))
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

	Client.LoginByEmail(team.Name, user.Email, "pwd")

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

	channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	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)

		if len(counts.Counts) != 4 {
			t.Fatal("wrong number of channel counts")
		}

		if len(counts.UpdateTimes) != 4 {
			t.Fatal("wrong number of channel update times")
		}
488 489 490 491 492

		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)
493
			t.Fatal("result data should be empty")
494
		}
495 496 497 498
	}

}

=Corey Hulen's avatar
=Corey Hulen committed
499 500 501
func TestJoinChannel(t *testing.T) {
	Setup()

502
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
503 504
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

505
	user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
506
	user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
507
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user1.Id))
=Corey Hulen's avatar
=Corey Hulen committed
508

509
	Client.LoginByEmail(team.Name, user1.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
510 511 512 513 514 515 516

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

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

517
	user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
518
	user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
519
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user2.Id))
=Corey Hulen's avatar
=Corey Hulen committed
520

521
	Client.LoginByEmail(team.Name, user2.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
522 523 524 525 526 527 528 529 530 531 532

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

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

	data := make(map[string]string)
	data["user_id"] = user1.Id
	rchannel := Client.Must(Client.CreateDirectChannel(data)).Data.(*model.Channel)

533
	user3 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
534 535
	user3 = Client.Must(Client.CreateUser(user3, "")).Data.(*model.User)

536
	Client.LoginByEmail(team.Name, user3.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
537 538 539 540 541 542 543 544 545

	if _, err := Client.JoinChannel(rchannel.Id); err == nil {
		t.Fatal("shoudn't be able to join direct channel")
	}
}

func TestLeaveChannel(t *testing.T) {
	Setup()

546
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
547 548
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

549
	user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
550
	user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
551
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user1.Id))
=Corey Hulen's avatar
=Corey Hulen committed
552

553
	Client.LoginByEmail(team.Name, user1.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
554 555 556 557 558 559 560

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

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

561
	user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
562
	user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
563
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user2.Id))
=Corey Hulen's avatar
=Corey Hulen committed
564

565
	Client.LoginByEmail(team.Name, user2.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

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

	// No error if you leave a channel you cannot see
	Client.Must(Client.LeaveChannel(channel3.Id))

	data := make(map[string]string)
	data["user_id"] = user1.Id
	rchannel := Client.Must(Client.CreateDirectChannel(data)).Data.(*model.Channel)

	if _, err := Client.LeaveChannel(rchannel.Id); err == nil {
		t.Fatal("should have errored, cannot leave direct channel")
	}

	rget := Client.Must(Client.GetChannels(""))
	cdata := rget.Data.(*model.ChannelList)
	for _, c := range cdata.Channels {
		if c.Name == model.DEFAULT_CHANNEL {
			if _, err := Client.LeaveChannel(c.Id); err == nil {
				t.Fatal("should have errored on leaving default channel")
			}
			break
		}
	}
}

func TestDeleteChannel(t *testing.T) {
	Setup()

595
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
596 597
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

598
	userTeamAdmin := &model.User{TeamId: team.Id, Email: team.Email, Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
599
	userTeamAdmin = Client.Must(Client.CreateUser(userTeamAdmin, "")).Data.(*model.User)
600
	store.Must(Srv.Store.User().VerifyEmail(utils.T, userTeamAdmin.Id))
=Corey Hulen's avatar
=Corey Hulen committed
601

602
	userChannelAdmin := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
603
	userChannelAdmin = Client.Must(Client.CreateUser(userChannelAdmin, "")).Data.(*model.User)
604
	store.Must(Srv.Store.User().VerifyEmail(utils.T, userChannelAdmin.Id))
=Corey Hulen's avatar
=Corey Hulen committed
605

606
	Client.LoginByEmail(team.Name, userChannelAdmin.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
607 608 609 610 611 612

	channelMadeByCA := &model.Channel{DisplayName: "C Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	channelMadeByCA = Client.Must(Client.CreateChannel(channelMadeByCA)).Data.(*model.Channel)

	Client.AddChannelMember(channelMadeByCA.Id, userTeamAdmin.Id)

613
	Client.LoginByEmail(team.Name, userTeamAdmin.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

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

	channel2 := &model.Channel{DisplayName: "B Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
	channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)

	if _, err := Client.DeleteChannel(channel1.Id); err != nil {
		t.Fatal(err)
	}

	if _, err := Client.DeleteChannel(channelMadeByCA.Id); err != nil {
		t.Fatal("Team admin failed to delete Channel Admin's channel")
	}

	post1 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a"}
	if _, err := Client.CreatePost(post1); err == nil {
		t.Fatal("should have failed to post to deleted channel")
	}

634
	userStd := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
635
	userStd = Client.Must(Client.CreateUser(userStd, "")).Data.(*model.User)
636
	store.Must(Srv.Store.User().VerifyEmail(utils.T, userStd.Id))
=Corey Hulen's avatar
=Corey Hulen committed
637

638
	Client.LoginByEmail(team.Name, userStd.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

	if _, err := Client.JoinChannel(channel1.Id); err == nil {
		t.Fatal("should have failed to join deleted channel")
	}

	Client.Must(Client.JoinChannel(channel2.Id))

	if _, err := Client.DeleteChannel(channel2.Id); err == nil {
		t.Fatal("should have failed to delete channel you're not an admin of")
	}

	rget := Client.Must(Client.GetChannels(""))
	cdata := rget.Data.(*model.ChannelList)
	for _, c := range cdata.Channels {
		if c.Name == model.DEFAULT_CHANNEL {
			if _, err := Client.DeleteChannel(c.Id); err == nil {
				t.Fatal("should have errored on deleting default channel")
			}
			break
		}
	}
}

func TestGetChannelExtraInfo(t *testing.T) {
	Setup()

665
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
666 667
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

668
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
669
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
670
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user.Id))
=Corey Hulen's avatar
=Corey Hulen committed
671

672
	Client.LoginByEmail(team.Name, user.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
673 674 675 676

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

677
	rget := Client.Must(Client.GetChannelExtraInfo(channel1.Id, -1, ""))
678 679
	data := rget.Data.(*model.ChannelExtra)
	if data.Id != channel1.Id {
=Corey Hulen's avatar
=Corey Hulen committed
680
		t.Fatal("couldnt't get extra info")
681 682 683 684
	} else if len(data.Members) != 1 {
		t.Fatal("got incorrect members")
	} else if data.MemberCount != 1 {
		t.Fatal("got incorrect member count")
=Corey Hulen's avatar
=Corey Hulen committed
685
	}
686 687 688 689 690 691 692

	//
	// Testing etag caching
	//

	currentEtag := rget.Etag

693
	if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, -1, currentEtag); err != nil {
694 695 696 697 698 699 700 701
		t.Fatal(err)
	} else if cache_result.Data.(*model.ChannelExtra) != nil {
		t.Log(cache_result.Data)
		t.Fatal("response should be empty")
	} else {
		currentEtag = cache_result.Etag
	}

702
	Client2 := model.NewClient("http://localhost" + utils.Cfg.ServiceSettings.ListenAddress)
703 704 705

	user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "tester2@test.com", Nickname: "Tester 2", Password: "pwd"}
	user2 = Client2.Must(Client2.CreateUser(user2, "")).Data.(*model.User)
706
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user2.Id))
707 708 709 710

	Client2.LoginByEmail(team.Name, user2.Email, "pwd")
	Client2.Must(Client2.JoinChannel(channel1.Id))

711
	if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, -1, currentEtag); err != nil {
712 713 714 715 716 717 718 719
		t.Fatal(err)
	} else if cache_result.Data.(*model.ChannelExtra) == nil {
		t.Log(cache_result.Data)
		t.Fatal("response should not be empty")
	} else {
		currentEtag = cache_result.Etag
	}

720
	if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, -1, currentEtag); err != nil {
721 722 723 724 725 726 727 728 729 730
		t.Fatal(err)
	} else if cache_result.Data.(*model.ChannelExtra) != nil {
		t.Log(cache_result.Data)
		t.Fatal("response should be empty")
	} else {
		currentEtag = cache_result.Etag
	}

	Client2.Must(Client2.LeaveChannel(channel1.Id))

731
	if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, -1, currentEtag); err != nil {
732 733 734 735 736 737 738 739
		t.Fatal(err)
	} else if cache_result.Data.(*model.ChannelExtra) == nil {
		t.Log(cache_result.Data)
		t.Fatal("response should not be empty")
	} else {
		currentEtag = cache_result.Etag
	}

740
	if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, -1, currentEtag); err != nil {
741 742 743 744 745 746 747
		t.Fatal(err)
	} else if cache_result.Data.(*model.ChannelExtra) != nil {
		t.Log(cache_result.Data)
		t.Fatal("response should be empty")
	} else {
		currentEtag = cache_result.Etag
	}
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783

	Client2.Must(Client2.JoinChannel(channel1.Id))

	if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, 2, currentEtag); err != nil {
		t.Fatal(err)
	} else if extra := cache_result.Data.(*model.ChannelExtra); extra == nil {
		t.Fatal("response should not be empty")
	} else if len(extra.Members) != 2 {
		t.Fatal("should've returned 2 members")
	} else if extra.MemberCount != 2 {
		t.Fatal("should've returned member count of 2")
	} else {
		currentEtag = cache_result.Etag
	}

	if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, 1, currentEtag); err != nil {
		t.Fatal(err)
	} else if extra := cache_result.Data.(*model.ChannelExtra); extra == nil {
		t.Fatal("response should not be empty")
	} else if len(extra.Members) != 1 {
		t.Fatal("should've returned only 1 member")
	} else if extra.MemberCount != 2 {
		t.Fatal("should've returned member count of 2")
	} else {
		currentEtag = cache_result.Etag
	}

	if cache_result, err := Client.GetChannelExtraInfo(channel1.Id, 1, currentEtag); err != nil {
		t.Fatal(err)
	} else if cache_result.Data.(*model.ChannelExtra) != nil {
		t.Log(cache_result.Data)
		t.Fatal("response should be empty")
	} else {
		currentEtag = cache_result.Etag
	}

=Corey Hulen's avatar
=Corey Hulen committed
784 785 786 787 788
}

func TestAddChannelMember(t *testing.T) {
	Setup()

789
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
790 791
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

792
	user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
793
	user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
794
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user1.Id))
=Corey Hulen's avatar
=Corey Hulen committed
795

796
	Client.LoginByEmail(team.Name, user1.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
797 798 799 800

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

801
	user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
802
	user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
803
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user2.Id))
=Corey Hulen's avatar
=Corey Hulen committed
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827

	if _, err := Client.AddChannelMember(channel1.Id, user2.Id); err != nil {
		t.Fatal(err)
	}

	if _, err := Client.AddChannelMember(channel1.Id, "dsgsdg"); err == nil {
		t.Fatal("Should have errored, bad user id")
	}

	if _, err := Client.AddChannelMember(channel1.Id, "12345678901234567890123456"); err == nil {
		t.Fatal("Should have errored, bad user id")
	}

	if _, err := Client.AddChannelMember(channel1.Id, user2.Id); err == nil {
		t.Fatal("Should have errored, user already a member")
	}

	if _, err := Client.AddChannelMember("sgdsgsdg", user2.Id); err == nil {
		t.Fatal("Should have errored, bad channel id")
	}

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

828
	Client.LoginByEmail(team.Name, user2.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
829 830 831 832 833

	if _, err := Client.AddChannelMember(channel2.Id, user2.Id); err == nil {
		t.Fatal("Should have errored, user not in channel")
	}

834
	Client.LoginByEmail(team.Name, user1.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
835 836 837 838 839 840 841 842 843 844 845 846

	Client.Must(Client.DeleteChannel(channel2.Id))

	if _, err := Client.AddChannelMember(channel2.Id, user2.Id); err == nil {
		t.Fatal("Should have errored, channel deleted")
	}

}

func TestRemoveChannelMember(t *testing.T) {
	Setup()

847
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
848 849
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

850
	userTeamAdmin := &model.User{TeamId: team.Id, Email: team.Email, Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
851
	userTeamAdmin = Client.Must(Client.CreateUser(userTeamAdmin, "")).Data.(*model.User)
852
	store.Must(Srv.Store.User().VerifyEmail(utils.T, userTeamAdmin.Id))
=Corey Hulen's avatar
=Corey Hulen committed
853

854
	userChannelAdmin := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
855
	userChannelAdmin = Client.Must(Client.CreateUser(userChannelAdmin, "")).Data.(*model.User)
856
	store.Must(Srv.Store.User().VerifyEmail(utils.T, userChannelAdmin.Id))
=Corey Hulen's avatar
=Corey Hulen committed
857

858
	Client.LoginByEmail(team.Name, userChannelAdmin.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
859 860 861 862 863 864

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

	Client.Must(Client.AddChannelMember(channelMadeByCA.Id, userTeamAdmin.Id))

865
	Client.LoginByEmail(team.Name, userTeamAdmin.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
866 867 868 869

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

870
	userStd := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
871
	userStd = Client.Must(Client.CreateUser(userStd, "")).Data.(*model.User)
872
	store.Must(Srv.Store.User().VerifyEmail(utils.T, userStd.Id))
=Corey Hulen's avatar
=Corey Hulen committed
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896

	Client.Must(Client.AddChannelMember(channel1.Id, userStd.Id))

	Client.Must(Client.AddChannelMember(channelMadeByCA.Id, userStd.Id))

	if _, err := Client.RemoveChannelMember(channel1.Id, "dsgsdg"); err == nil {
		t.Fatal("Should have errored, bad user id")
	}

	if _, err := Client.RemoveChannelMember("sgdsgsdg", userStd.Id); err == nil {
		t.Fatal("Should have errored, bad channel id")
	}

	if _, err := Client.RemoveChannelMember(channel1.Id, userStd.Id); err != nil {
		t.Fatal(err)
	}

	if _, err := Client.RemoveChannelMember(channelMadeByCA.Id, userStd.Id); err != nil {
		t.Fatal("Team Admin failed to remove member from Channel Admin's channel")
	}

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

897
	Client.LoginByEmail(team.Name, userStd.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
898 899 900 901 902

	if _, err := Client.RemoveChannelMember(channel2.Id, userStd.Id); err == nil {
		t.Fatal("Should have errored, user not channel admin")
	}

903
	Client.LoginByEmail(team.Name, userTeamAdmin.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
904 905 906 907 908 909 910 911 912 913
	Client.Must(Client.AddChannelMember(channel2.Id, userStd.Id))

	Client.Must(Client.DeleteChannel(channel2.Id))

	if _, err := Client.RemoveChannelMember(channel2.Id, userStd.Id); err == nil {
		t.Fatal("Should have errored, channel deleted")
	}

}

914
func TestUpdateNotifyProps(t *testing.T) {
915 916 917 918 919
	Setup()

	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

920
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
921
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
922
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user.Id))
923 924 925 926 927 928 929 930 931

	Client.LoginByEmail(team.Name, user.Email, "pwd")

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

	data := make(map[string]string)
	data["channel_id"] = channel1.Id
	data["user_id"] = user.Id
932
	data["desktop"] = model.CHANNEL_NOTIFY_MENTION
933 934 935 936

	timeBeforeUpdate := model.GetMillis()
	time.Sleep(100 * time.Millisecond)

937 938
	// test updating desktop
	if result, err := Client.UpdateNotifyProps(data); err != nil {
939
		t.Fatal(err)
940 941 942 943
	} else if notifyProps := result.Data.(map[string]string); notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION {
		t.Fatal("NotifyProps[\"desktop\"] did not update properly")
	} else if notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_ALL {
		t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps["mark_unread"])
944 945 946 947
	}

	rget := Client.Must(Client.GetChannels(""))
	rdata := rget.Data.(*model.ChannelList)
948 949 950 951
	if len(rdata.Members) == 0 || rdata.Members[channel1.Id].NotifyProps["desktop"] != data["desktop"] {
		t.Fatal("NotifyProps[\"desktop\"] did not update properly")
	} else if rdata.Members[channel1.Id].LastUpdateAt <= timeBeforeUpdate {
		t.Fatal("LastUpdateAt did not update")
952 953
	}

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
	// test an empty update
	delete(data, "desktop")

	if result, err := Client.UpdateNotifyProps(data); err != nil {
		t.Fatal(err)
	} else if notifyProps := result.Data.(map[string]string); notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_ALL {
		t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps["mark_unread"])
	} else if notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION {
		t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps["desktop"])
	}

	// test updating mark unread
	data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION

	if result, err := Client.UpdateNotifyProps(data); err != nil {
		t.Fatal(err)
	} else if notifyProps := result.Data.(map[string]string); notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_MENTION {
		t.Fatal("NotifyProps[\"mark_unread\"] did not update properly")
	} else if notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION {
		t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps["desktop"])
974 975
	}

976 977 978 979 980 981 982 983 984 985 986 987 988
	// test updating both
	data["desktop"] = model.CHANNEL_NOTIFY_NONE
	data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION

	if result, err := Client.UpdateNotifyProps(data); err != nil {
		t.Fatal(err)
	} else if notifyProps := result.Data.(map[string]string); notifyProps["desktop"] != model.CHANNEL_NOTIFY_NONE {
		t.Fatal("NotifyProps[\"desktop\"] did not update properly")
	} else if notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_MENTION {
		t.Fatal("NotifyProps[\"mark_unread\"] did not update properly")
	}

	// test error cases
989
	data["user_id"] = "junk"
990
	if _, err := Client.UpdateNotifyProps(data); err == nil {
991 992 993 994
		t.Fatal("Should have errored - bad user id")
	}

	data["user_id"] = "12345678901234567890123456"
995
	if _, err := Client.UpdateNotifyProps(data); err == nil {
996 997 998 999 1000
		t.Fatal("Should have errored - bad user id")
	}

	data["user_id"] = user.Id
	data["channel_id"] = "junk"
1001
	if _, err := Client.UpdateNotifyProps(data); err == nil {
1002 1003 1004 1005
		t.Fatal("Should have errored - bad channel id")
	}

	data["channel_id"] = "12345678901234567890123456"
1006
	if _, err := Client.UpdateNotifyProps(data); err == nil {
1007 1008 1009
		t.Fatal("Should have errored - bad channel id")
	}

1010 1011 1012 1013
	data["desktop"] = "junk"
	data["mark_unread"] = model.CHANNEL_MARK_UNREAD_ALL
	if _, err := Client.UpdateNotifyProps(data); err == nil {
		t.Fatal("Should have errored - bad desktop notify level")
1014 1015
	}

1016 1017 1018 1019
	data["desktop"] = model.CHANNEL_NOTIFY_ALL
	data["mark_unread"] = "junk"
	if _, err := Client.UpdateNotifyProps(data); err == nil {
		t.Fatal("Should have errored - bad mark unread level")
1020 1021
	}

1022
	user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
1023 1024 1025 1026 1027 1028
	user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)

	Client.LoginByEmail(team.Name, user2.Email, "pwd")

	data["channel_id"] = channel1.Id
	data["user_id"] = user2.Id
1029 1030
	data["desktop"] = model.CHANNEL_NOTIFY_MENTION
	data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION
1031
	if _, err := Client.UpdateNotifyProps(data); err == nil {
1032 1033 1034 1035
		t.Fatal("Should have errored - user not in channel")
	}
}

=Corey Hulen's avatar
=Corey Hulen committed
1036
func TestFuzzyChannel(t *testing.T) {
JoramWilander's avatar
JoramWilander committed
1037
	Setup()
=Corey Hulen's avatar
=Corey Hulen committed
1038

1039
	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
1040 1041
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

1042
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
1043
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
1044
	store.Must(Srv.Store.User().VerifyEmail(utils.T, user.Id))
=Corey Hulen's avatar
=Corey Hulen committed
1045

1046
	Client.LoginByEmail(team.Name, user.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
1047 1048

	// Strings that should pass as acceptable channel names
JoramWilander's avatar
JoramWilander committed
1049
	var fuzzyStringsPass = []string{
=Corey Hulen's avatar
=Corey Hulen committed
1050
		"*", "?", ".", "}{][)(><", "{}[]()<>",
JoramWilander's avatar
JoramWilander committed
1051

=Corey Hulen's avatar
=Corey Hulen committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
		"qahwah ( قهوة)",
		"שָׁלוֹם עֲלֵיכֶם",
		"Ramen チャーシュー chāshū",
		"言而无信",
		"Ṫ͌ó̍ ̍͂̓̍̍̀i̊ͯ͒",
		"&amp; &lt; &qu",

		"' or '1'='1' -- ",
		"' or '1'='1' ({ ",
		"' or '1'='1' /* ",
		"1;DROP TABLE users",

		"<b><i><u><strong><em>",

		"sue@thatmightbe",
		"sue@thatmightbe.",
		"sue@thatmightbe.c",
		"sue@thatmightbe.co",
		"su+san@thatmightbe.com",
		"a@b.中国",
		"1@2.am",
		"a@b.co.uk",
		"a@b.cancerresearch",
		"local@[127.0.0.1]",
	}

	for i := 0; i < len(fuzzyStringsPass); i++ {
		channel := model.Channel{DisplayName: fuzzyStringsPass[i], Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}

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