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

package api

import (
	"fmt"
	"github.com/mattermost/platform/model"
=Corey Hulen's avatar
=Corey Hulen committed
9
	"github.com/mattermost/platform/store"
=Corey Hulen's avatar
=Corey Hulen committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	"github.com/mattermost/platform/utils"
	"strings"
	"testing"
)

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

	_, err := Client.SignupTeam("test@nowhere.com", "name")
	if err != nil {
		t.Fatal(err)
	}
}

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

	props := make(map[string]string)
	props["email"] = strings.ToLower(model.NewId()) + "corey@test.com"
	props["name"] = "Test Company name"
	props["time"] = fmt.Sprintf("%v", model.GetMillis())

	data := model.MapToJson(props)
	hash := model.HashPassword(fmt.Sprintf("%v:%v", data, utils.Cfg.ServiceSettings.InviteSalt))

35
	team := model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
36
	user := model.User{Email: props["email"], Nickname: "Corey Hulen", Password: "hello"}
=Corey Hulen's avatar
=Corey Hulen committed
37 38 39 40 41 42 43 44

	ts := model.TeamSignup{Team: team, User: user, Invites: []string{"corey@test.com"}, Data: data, Hash: hash}

	rts, err := Client.CreateTeamFromSignup(&ts)
	if err != nil {
		t.Fatal(err)
	}

45
	if rts.Data.(*model.TeamSignup).Team.DisplayName != team.DisplayName {
=Corey Hulen's avatar
=Corey Hulen committed
46 47 48 49 50 51 52 53 54 55 56 57 58
		t.Fatal("full name didn't match")
	}

	ruser := rts.Data.(*model.TeamSignup).User

	if result, err := Client.LoginById(ruser.Id, user.Password); err != nil {
		t.Fatal(err)
	} else {
		if result.Data.(*model.User).Email != user.Email {
			t.Fatal("email's didn't match")
		}
	}

59 60 61 62 63
	c1 := Client.Must(Client.GetChannels("")).Data.(*model.ChannelList)
	if len(c1.Channels) != 2 {
		t.Fatal("default channels not created")
	}

=Corey Hulen's avatar
=Corey Hulen committed
64 65 66 67 68 69 70 71 72 73
	ts.Data = "garbage"
	_, err = Client.CreateTeamFromSignup(&ts)
	if err == nil {
		t.Fatal(err)
	}
}

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

74
	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
75 76 77 78 79
	rteam, err := Client.CreateTeam(&team)
	if err != nil {
		t.Fatal(err)
	}

80
	user := &model.User{TeamId: rteam.Data.(*model.Team).Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
81
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
=Corey Hulen's avatar
=Corey Hulen committed
82
	store.Must(Srv.Store.User().VerifyEmail(user.Id))
83

84
	Client.LoginByEmail(team.Name, user.Email, "pwd")
85 86 87 88 89 90

	c1 := Client.Must(Client.GetChannels("")).Data.(*model.ChannelList)
	if len(c1.Channels) != 2 {
		t.Fatal("default channels not created")
	}

91
	if rteam.Data.(*model.Team).DisplayName != team.DisplayName {
=Corey Hulen's avatar
=Corey Hulen committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105
		t.Fatal("full name didn't match")
	}

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

	rteam.Data.(*model.Team).Id = ""
	if _, err := Client.CreateTeam(rteam.Data.(*model.Team)); err != nil {
		if err.Message != "A team with that domain already exists" {
			t.Fatal(err)
		}
	}

106
	if _, err := Client.DoApiPost("/teams/create", "garbage"); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
107 108 109 110 111 112 113
		t.Fatal("should have been an error")
	}
}

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

114
	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
115 116
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

117
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
118
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
=Corey Hulen's avatar
=Corey Hulen committed
119
	store.Must(Srv.Store.User().VerifyEmail(user.Id))
=Corey Hulen's avatar
=Corey Hulen committed
120 121 122 123 124

	if r1, err := Client.FindTeams(user.Email); err != nil {
		t.Fatal(err)
	} else {
		domains := r1.Data.([]string)
125
		if domains[0] != team.Name {
=Corey Hulen's avatar
=Corey Hulen committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
			t.Fatal(domains)
		}
	}

	if _, err := Client.FindTeams("missing"); err != nil {
		t.Fatal(err)
	}
}

/*

XXXXXX investigate and fix failing test

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

142
	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
143 144
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

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

149
	if r1, err := Client.FindTeamByDomain(team.Name, false); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
150 151 152 153 154 155 156 157
		t.Fatal(err)
	} else {
		val := r1.Data.(bool)
		if !val {
			t.Fatal("should be a valid domain")
		}
	}

158
	if r1, err := Client.FindTeamByDomain(team.Name, true); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
		t.Fatal(err)
	} else {
		val := r1.Data.(bool)
		if !val {
			t.Fatal("should be a valid domain")
		}
	}

	if r1, err := Client.FindTeamByDomain("a"+model.NewId()+"a", false); err != nil {
		t.Fatal(err)
	} else {
		val := r1.Data.(bool)
		if val {
			t.Fatal("shouldn't be a valid domain")
		}
	}
}

*/

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

182
	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
183 184
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

185
	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
=Corey Hulen's avatar
=Corey Hulen committed
186
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
=Corey Hulen's avatar
=Corey Hulen committed
187
	store.Must(Srv.Store.User().VerifyEmail(user.Id))
=Corey Hulen's avatar
=Corey Hulen committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

	if _, err := Client.FindTeamsSendEmail(user.Email); err != nil {
		t.Fatal(err)
	} else {
	}

	if _, err := Client.FindTeamsSendEmail("missing"); err != nil {

		// It should actually succeed at sending the email since it doesn't exist
		if !strings.Contains(err.DetailedError, "Failed to add to email address") {
			t.Fatal(err)
		}
	}
}

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

206
	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
207 208
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

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

213
	Client.LoginByEmail(team.Name, user.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

	invite := make(map[string]string)
	invite["email"] = model.NewId() + "corey@test.com"
	invite["first_name"] = "Test"
	invite["last_name"] = "Guy"
	invites := &model.Invites{Invites: []map[string]string{invite}}
	invites.Invites = append(invites.Invites, invite)

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

	invites = &model.Invites{Invites: []map[string]string{}}
	if _, err := Client.InviteMembers(invites); err == nil {
		t.Fatal("Should have errored out on no invites to send")
	}
}

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

235
	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
236 237
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

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

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

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

	data := make(map[string]string)
	data["new_name"] = "NewName"
250
	if _, err := Client.UpdateTeamDisplayName(data); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
251 252 253
		t.Fatal("Should have errored, not admin")
	}

254
	Client.LoginByEmail(team.Name, user.Email, "pwd")
=Corey Hulen's avatar
=Corey Hulen committed
255 256

	data["new_name"] = ""
257
	if _, err := Client.UpdateTeamDisplayName(data); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
258 259 260 261
		t.Fatal("Should have errored, empty name")
	}

	data["new_name"] = "NewName"
262
	if _, err := Client.UpdateTeamDisplayName(data); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
263 264 265 266 267
		t.Fatal(err)
	}
	// No GET team web service, so hard to confirm here that team name updated

	data["team_id"] = "junk"
268
	if _, err := Client.UpdateTeamDisplayName(data); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
269 270 271 272
		t.Fatal("Should have errored, junk team id")
	}

	data["team_id"] = "12345678901234567890123456"
273
	if _, err := Client.UpdateTeamDisplayName(data); err == nil {
=Corey Hulen's avatar
=Corey Hulen committed
274 275 276 277 278
		t.Fatal("Should have errored, bad team id")
	}

	data["team_id"] = team.Id
	data["new_name"] = "NewNameAgain"
279
	if _, err := Client.UpdateTeamDisplayName(data); err != nil {
=Corey Hulen's avatar
=Corey Hulen committed
280 281 282 283 284 285 286 287
		t.Fatal(err)
	}
	// No GET team web service, so hard to confirm here that team name updated
}

func TestFuzzyTeamCreate(t *testing.T) {

	for i := 0; i < len(utils.FUZZY_STRINGS_NAMES) || i < len(utils.FUZZY_STRINGS_EMAILS); i++ {
288
		testDisplayName := "Name"
=Corey Hulen's avatar
=Corey Hulen committed
289 290 291
		testEmail := "test@nowhere.com"

		if i < len(utils.FUZZY_STRINGS_NAMES) {
292
			testDisplayName = utils.FUZZY_STRINGS_NAMES[i]
=Corey Hulen's avatar
=Corey Hulen committed
293 294 295 296 297
		}
		if i < len(utils.FUZZY_STRINGS_EMAILS) {
			testEmail = utils.FUZZY_STRINGS_EMAILS[i]
		}

298
		team := model.Team{DisplayName: testDisplayName, Name: "z-z-" + model.NewId() + "a", Email: testEmail, Type: model.TEAM_OPEN}
=Corey Hulen's avatar
=Corey Hulen committed
299 300 301 302 303 304 305

		_, err := Client.CreateTeam(&team)
		if err != nil {
			t.Fatal(err)
		}
	}
}
306 307 308 309

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

310
	team := model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
311 312
	rteam, _ := Client.CreateTeam(&team)

313
	user := model.User{TeamId: rteam.Data.(*model.Team).Id, Email: strings.ToLower(model.NewId()) + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
314
	ruser, _ := Client.CreateUser(&user, "")
=Corey Hulen's avatar
=Corey Hulen committed
315
	store.Must(Srv.Store.User().VerifyEmail(ruser.Data.(*model.User).Id))
316

317
	Client.LoginByEmail(team.Name, user.Email, user.Password)
318 319 320 321

	if result, err := Client.GetMyTeam(""); err != nil {
		t.Fatal("Failed to get user")
	} else {
322
		if result.Data.(*model.Team).DisplayName != team.DisplayName {
323 324
			t.Fatal("team names did not match")
		}
325
		if result.Data.(*model.Team).Name != team.Name {
326 327 328 329 330 331 332 333 334 335 336
			t.Fatal("team domains did not match")
		}
		if result.Data.(*model.Team).Type != team.Type {
			t.Fatal("team types did not match")
		}
	}
}

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

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

340
	user := &model.User{TeamId: team.Id, Email: "test@nowhere.com", Nickname: "Corey Hulen", Password: "pwd"}
341
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
=Corey Hulen's avatar
=Corey Hulen committed
342
	store.Must(Srv.Store.User().VerifyEmail(user.Id))
343

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

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

351
	user3 := &model.User{TeamId: team2.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
352
	user3 = Client.Must(Client.CreateUser(user3, "")).Data.(*model.User)
=Corey Hulen's avatar
=Corey Hulen committed
353
	store.Must(Srv.Store.User().VerifyEmail(user3.Id))
354

355
	Client.LoginByEmail(team.Name, user2.Email, "pwd")
356 357 358 359 360 361 362

	data := make(map[string]string)
	data["allow_valet"] = "true"
	if _, err := Client.UpdateValetFeature(data); err == nil {
		t.Fatal("Should have errored, not admin")
	}

363
	Client.LoginByEmail(team.Name, user.Email, "pwd")
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 391 392 393 394 395 396 397 398 399 400

	data["allow_valet"] = ""
	if _, err := Client.UpdateValetFeature(data); err == nil {
		t.Fatal("Should have errored, empty allow_valet field")
	}

	data["allow_valet"] = "true"
	if _, err := Client.UpdateValetFeature(data); err != nil {
		t.Fatal(err)
	}

	rteam := Client.Must(Client.GetMyTeam("")).Data.(*model.Team)
	if rteam.AllowValet != true {
		t.Fatal("Should have errored - allow valet property not updated")
	}

	data["team_id"] = "junk"
	if _, err := Client.UpdateValetFeature(data); err == nil {
		t.Fatal("Should have errored, junk team id")
	}

	data["team_id"] = "12345678901234567890123456"
	if _, err := Client.UpdateValetFeature(data); err == nil {
		t.Fatal("Should have errored, bad team id")
	}

	data["team_id"] = team.Id
	data["allow_valet"] = "false"
	if _, err := Client.UpdateValetFeature(data); err != nil {
		t.Fatal(err)
	}

	rteam = Client.Must(Client.GetMyTeam("")).Data.(*model.Team)
	if rteam.AllowValet != false {
		t.Fatal("Should have errored - allow valet property not updated")
	}

401
	Client.LoginByEmail(team2.Name, user3.Email, "pwd")
402 403 404 405 406 407 408

	data["team_id"] = team.Id
	data["allow_valet"] = "true"
	if _, err := Client.UpdateValetFeature(data); err == nil {
		t.Fatal("Should have errored, not part of team")
	}
}