1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
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
834
835
836
837
838
839
840
841
842
843
844
845
846
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api
import (
"testing"
"github.com/mattermost/platform/app"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
)
func TestCreateTeam(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
rteam, err := Client.CreateTeam(&team)
if err != nil {
t.Fatal(err)
}
user := &model.User{Email: model.NewId() + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
LinkUserToTeam(user, rteam.Data.(*model.Team))
store.Must(app.Srv.Store.User().VerifyEmail(user.Id))
Client.Login(user.Email, "passwd1")
Client.SetTeamId(rteam.Data.(*model.Team).Id)
c1 := Client.Must(Client.GetChannels("")).Data.(*model.ChannelList)
if len(*c1) != 2 {
t.Fatal("default channels not created")
}
if rteam.Data.(*model.Team).DisplayName != team.DisplayName {
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 name already exists" {
t.Fatal(err)
}
}
if _, err := Client.DoApiPost("/teams/create", "garbage"); err == nil {
t.Fatal("should have been an error")
}
}
func TestAddUserToTeam(t *testing.T) {
th := Setup().InitSystemAdmin().InitBasic()
th.BasicClient.Logout()
// Test adding a user to a team you are not a member of.
th.SystemAdminClient.SetTeamId(th.BasicTeam.Id)
th.SystemAdminClient.Must(th.SystemAdminClient.RemoveUserFromTeam(th.BasicTeam.Id, th.BasicUser2.Id))
th.LoginBasic2()
user2 := th.CreateUser(th.BasicClient)
if _, err := th.BasicClient.AddUserToTeam(th.BasicTeam.Id, user2.Id); err == nil {
t.Fatal("Should have failed because of not being a team member")
}
// Test adding a user to a team you are a member of.
th.BasicClient.Logout()
th.LoginBasic()
if _, err := th.BasicClient.AddUserToTeam(th.BasicTeam.Id, user2.Id); err != nil {
t.Fatal(err)
}
// Check it worked properly.
if result, err := th.BasicClient.AddUserToTeam(th.BasicTeam.Id, user2.Id); err != nil {
t.Fatal(err)
} else {
rm := result.Data.(map[string]string)
if rm["user_id"] != user2.Id {
t.Fatal("ids didn't match")
}
}
if _, err := th.BasicClient.GetTeamMember(th.BasicTeam.Id, user2.Id); err != nil {
t.Fatal(err)
}
// Restore config/license at end of test case.
restrictTeamInvite := *utils.Cfg.TeamSettings.RestrictTeamInvite
isLicensed := utils.IsLicensed
license := utils.License
defer func() {
*utils.Cfg.TeamSettings.RestrictTeamInvite = restrictTeamInvite
utils.IsLicensed = isLicensed
utils.License = license
utils.SetDefaultRolesBasedOnConfig()
}()
// Set the config so that only team admins can add a user to a team.
*utils.Cfg.TeamSettings.RestrictTeamInvite = model.PERMISSIONS_TEAM_ADMIN
utils.SetDefaultRolesBasedOnConfig()
// Test without the EE license to see that the permission restriction is ignored.
user3 := th.CreateUser(th.BasicClient)
if _, err := th.BasicClient.AddUserToTeam(th.BasicTeam.Id, user3.Id); err != nil {
t.Fatal(err)
}
// Add an EE license.
utils.IsLicensed = true
utils.License = &model.License{Features: &model.Features{}}
utils.License.Features.SetDefaults()
utils.SetDefaultRolesBasedOnConfig()
// Check that a regular user can't add someone to the team.
user4 := th.CreateUser(th.BasicClient)
if _, err := th.BasicClient.AddUserToTeam(th.BasicTeam.Id, user4.Id); err == nil {
t.Fatal("should have failed due to permissions error")
}
// Should work as team admin.
UpdateUserToTeamAdmin(th.BasicUser, th.BasicTeam)
app.InvalidateAllCaches()
*utils.Cfg.TeamSettings.RestrictTeamInvite = model.PERMISSIONS_TEAM_ADMIN
utils.IsLicensed = true
utils.License = &model.License{Features: &model.Features{}}
utils.License.Features.SetDefaults()
utils.SetDefaultRolesBasedOnConfig()
user5 := th.CreateUser(th.BasicClient)
if _, err := th.BasicClient.AddUserToTeam(th.BasicTeam.Id, user5.Id); err != nil {
t.Fatal(err)
}
// Change permission level to System Admin
*utils.Cfg.TeamSettings.RestrictTeamInvite = model.PERMISSIONS_SYSTEM_ADMIN
utils.SetDefaultRolesBasedOnConfig()
// Should not work as team admin.
user6 := th.CreateUser(th.BasicClient)
if _, err := th.BasicClient.AddUserToTeam(th.BasicTeam.Id, user6.Id); err == nil {
t.Fatal("should have failed due to permissions error")
}
// Should work as system admin.
user7 := th.CreateUser(th.BasicClient)
if _, err := th.SystemAdminClient.AddUserToTeam(th.BasicTeam.Id, user7.Id); err != nil {
t.Fatal(err)
}
}
func TestRemoveUserFromTeam(t *testing.T) {
th := Setup().InitSystemAdmin().InitBasic()
if _, err := th.BasicClient.RemoveUserFromTeam(th.SystemAdminTeam.Id, th.SystemAdminUser.Id); err == nil {
t.Fatal("should fail not enough permissions")
} else {
if err.Id != "api.context.permissions.app_error" {
t.Fatal("wrong error. Got: " + err.Id)
}
}
if _, err := th.BasicClient.RemoveUserFromTeam("", th.SystemAdminUser.Id); err == nil {
t.Fatal("should fail not enough permissions")
} else {
if err.Id != "api.context.permissions.app_error" {
t.Fatal("wrong error")
}
}
if _, err := th.BasicClient.RemoveUserFromTeam("", th.BasicUser.Id); err != nil {
t.Fatal("should have removed the user from the team")
}
th.BasicClient.Logout()
th.LoginSystemAdmin()
th.SystemAdminClient.Must(th.SystemAdminClient.AddUserToTeam(th.BasicTeam.Id, th.BasicUser.Id))
if _, err := th.SystemAdminClient.RemoveUserFromTeam(th.BasicTeam.Id, th.BasicUser.Id); err != nil {
t.Fatal("should have removed the user from the team")
}
}
func TestAddUserToTeamFromInvite(t *testing.T) {
th := Setup().InitBasic()
user2 := th.CreateUser(th.BasicClient)
th.BasicClient.Must(th.BasicClient.Logout())
th.BasicClient.Must(th.BasicClient.Login(user2.Email, user2.Password))
if result, err := th.BasicClient.AddUserToTeamFromInvite("", "", th.BasicTeam.InviteId); err != nil {
t.Fatal(err)
} else {
rtm := result.Data.(*model.Team)
if rtm.Id != th.BasicTeam.Id {
t.Fatal()
}
}
}
func TestGetAllTeams(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
Client := th.BasicClient
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)
Client.Logout()
user := &model.User{Email: model.NewId() + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
LinkUserToTeam(user, team)
store.Must(app.Srv.Store.User().VerifyEmail(user.Id))
Client.Login(user.Email, "passwd1")
Client.SetTeamId(team.Id)
if r1, err := Client.GetAllTeams(); err != nil {
t.Fatal(err)
} else if teams := r1.Data.(map[string]*model.Team); len(teams) != 1 {
t.Fatal("non admin users only get the teams that they're a member of")
} else if receivedTeam, ok := teams[team.Id]; !ok || receivedTeam.Id != team.Id {
t.Fatal("should've received team that the user is a member of")
}
if r1, err := th.SystemAdminClient.GetAllTeams(); err != nil {
t.Fatal(err)
} else if teams := r1.Data.(map[string]*model.Team); len(teams) == 1 {
t.Fatal("admin users should receive all teams")
} else if receivedTeam, ok := teams[team.Id]; !ok || receivedTeam.Id != team.Id {
t.Fatal("admin should've received team that they aren't a member of")
}
}
func TestGetAllTeamListings(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN, AllowOpenInvite: true}
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
Client.Logout()
user := &model.User{Email: model.NewId() + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
LinkUserToTeam(user, team)
store.Must(app.Srv.Store.User().VerifyEmail(user.Id))
Client.Login(user.Email, "passwd1")
Client.SetTeamId(team.Id)
if r1, err := Client.GetAllTeamListings(); err != nil {
t.Fatal(err)
} else {
teams := r1.Data.(map[string]*model.Team)
if teams[team.Id].Name != team.Name {
t.Fatal()
}
if teams[team.Id].Email != "" {
t.Fatal("Non admin users shoudn't get full listings")
}
}
app.UpdateUserRoles(user.Id, model.ROLE_SYSTEM_ADMIN.Id)
Client.Login(user.Email, "passwd1")
Client.SetTeamId(team.Id)
if r1, err := Client.GetAllTeams(); err != nil {
t.Fatal(err)
} else {
teams := r1.Data.(map[string]*model.Team)
if teams[team.Id].Name != team.Name {
t.Fatal()
}
if teams[team.Id].Email != team.Email {
t.Fatal()
}
}
}
func TestTeamPermDelete(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
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)
Client.Logout()
user1 := &model.User{Email: model.NewId() + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
LinkUserToTeam(user1, team)
store.Must(app.Srv.Store.User().VerifyEmail(user1.Id))
Client.Login(user1.Email, "passwd1")
Client.SetTeamId(team.Id)
channel1 := &model.Channel{DisplayName: "TestGetPosts", Name: "zz" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
post1 := &model.Post{ChannelId: channel1.Id, Message: "search for post1"}
post1 = Client.Must(Client.CreatePost(post1)).Data.(*model.Post)
post2 := &model.Post{ChannelId: channel1.Id, Message: "search for post2"}
post2 = Client.Must(Client.CreatePost(post2)).Data.(*model.Post)
post3 := &model.Post{ChannelId: channel1.Id, Message: "#hashtag search for post3"}
post3 = Client.Must(Client.CreatePost(post3)).Data.(*model.Post)
post4 := &model.Post{ChannelId: channel1.Id, Message: "hashtag for post4"}
post4 = Client.Must(Client.CreatePost(post4)).Data.(*model.Post)
c := &Context{}
c.RequestId = model.NewId()
c.IpAddress = "test"
err := app.PermanentDeleteTeam(team)
if err != nil {
t.Fatal(err)
}
Client.ClearOAuthToken()
}
func TestInviteMembers(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
Client := th.BasicClient
SystemAdminClient := th.SystemAdminClient
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)
Client.Logout()
user := &model.User{Email: model.NewId() + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
LinkUserToTeam(user, team)
store.Must(app.Srv.Store.User().VerifyEmail(user.Id))
Client.Login(user.Email, "passwd1")
Client.SetTeamId(team.Id)
invite := make(map[string]string)
invite["email"] = "success+" + model.NewId() + "@simulator.amazonses.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)
}
invites2 := &model.Invites{Invites: []map[string]string{}}
if _, err := Client.InviteMembers(invites2); err == nil {
t.Fatal("Should have errored out on no invites to send")
}
restrictTeamInvite := *utils.Cfg.TeamSettings.RestrictTeamInvite
defer func() {
*utils.Cfg.TeamSettings.RestrictTeamInvite = restrictTeamInvite
utils.SetDefaultRolesBasedOnConfig()
}()
*utils.Cfg.TeamSettings.RestrictTeamInvite = model.PERMISSIONS_TEAM_ADMIN
utils.SetDefaultRolesBasedOnConfig()
th.LoginBasic2()
LinkUserToTeam(th.BasicUser2, team)
if _, err := Client.InviteMembers(invites); err != nil {
t.Fatal(err)
}
isLicensed := utils.IsLicensed
license := utils.License
defer func() {
utils.IsLicensed = isLicensed
utils.License = license
utils.SetDefaultRolesBasedOnConfig()
}()
utils.IsLicensed = true
utils.License = &model.License{Features: &model.Features{}}
utils.License.Features.SetDefaults()
utils.SetDefaultRolesBasedOnConfig()
if _, err := Client.InviteMembers(invites); err == nil {
t.Fatal("should have errored not team admin and licensed")
}
UpdateUserToTeamAdmin(th.BasicUser2, team)
Client.Logout()
th.LoginBasic2()
Client.SetTeamId(team.Id)
if _, err := Client.InviteMembers(invites); err != nil {
t.Fatal(err)
}
*utils.Cfg.TeamSettings.RestrictTeamInvite = model.PERMISSIONS_SYSTEM_ADMIN
utils.SetDefaultRolesBasedOnConfig()
if _, err := Client.InviteMembers(invites); err == nil {
t.Fatal("should have errored not system admin and licensed")
}
LinkUserToTeam(th.SystemAdminUser, team)
if _, err := SystemAdminClient.InviteMembers(invites); err != nil {
t.Fatal(err)
}
}
func TestUpdateTeamDisplayName(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "success+" + model.NewId() + "@simulator.amazonses.com", Type: model.TEAM_OPEN}
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
Client.Logout()
user2 := &model.User{Email: "success+" + model.NewId() + "@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
LinkUserToTeam(user2, team)
store.Must(app.Srv.Store.User().VerifyEmail(user2.Id))
Client.Login(user2.Email, "passwd1")
Client.SetTeamId(team.Id)
vteam := &model.Team{DisplayName: team.DisplayName, Name: team.Name, Email: team.Email, Type: team.Type}
vteam.DisplayName = "NewName"
if _, err := Client.UpdateTeam(vteam); err == nil {
t.Fatal("Should have errored, not admin")
}
th.LoginBasic()
vteam.DisplayName = ""
if _, err := Client.UpdateTeam(vteam); err == nil {
t.Fatal("Should have errored, empty name")
}
vteam.DisplayName = "NewName"
if _, err := Client.UpdateTeam(vteam); err != nil {
t.Fatal(err)
}
}
func TestFuzzyTeamCreate(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
for i := 0; i < len(utils.FUZZY_STRINGS_NAMES) || i < len(utils.FUZZY_STRINGS_EMAILS); i++ {
testDisplayName := "Name"
testEmail := "test@nowhere.com"
if i < len(utils.FUZZY_STRINGS_NAMES) {
testDisplayName = utils.FUZZY_STRINGS_NAMES[i]
}
if i < len(utils.FUZZY_STRINGS_EMAILS) {
testEmail = utils.FUZZY_STRINGS_EMAILS[i]
}
team := model.Team{DisplayName: testDisplayName, Name: "z-z-" + model.NewId() + "a", Email: testEmail, Type: model.TEAM_OPEN}
_, err := Client.CreateTeam(&team)
if err != nil {
t.Fatal(err)
}
}
}
func TestGetMyTeam(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
rteam, _ := Client.CreateTeam(team)
team = rteam.Data.(*model.Team)
Client.Logout()
user := model.User{Email: "success+" + model.NewId() + "@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
ruser, _ := Client.CreateUser(&user, "")
LinkUserToTeam(ruser.Data.(*model.User), rteam.Data.(*model.Team))
store.Must(app.Srv.Store.User().VerifyEmail(ruser.Data.(*model.User).Id))
Client.Login(user.Email, user.Password)
Client.SetTeamId(team.Id)
if result, err := Client.GetMyTeam(""); err != nil {
t.Fatal(err)
} else {
if result.Data.(*model.Team).DisplayName != team.DisplayName {
t.Fatal("team names did not match")
}
if result.Data.(*model.Team).Name != team.Name {
t.Fatal("team domains did not match")
}
if result.Data.(*model.Team).Type != team.Type {
t.Fatal("team types did not match")
}
}
}
func TestGetTeamMembers(t *testing.T) {
th := Setup().InitBasic()
if result, err := th.BasicClient.GetTeamMembers(th.BasicTeam.Id, 0, 100); err != nil {
t.Fatal(err)
} else {
members := result.Data.([]*model.TeamMember)
if len(members) == 0 {
t.Fatal("should have results")
}
}
if _, err := th.BasicClient.GetTeamMembers("junk", 0, 100); err == nil {
t.Fatal("should have errored - bad team id")
}
}
func TestGetMyTeamMembers(t *testing.T) {
th := Setup().InitBasic()
if result, err := th.BasicClient.GetMyTeamMembers(); err != nil {
t.Fatal(err)
} else {
members := result.Data.([]*model.TeamMember)
if len(members) == 0 {
t.Fatal("should have results")
}
}
}
func TestGetMyTeamsUnread(t *testing.T) {
th := Setup().InitBasic()
if result, err := th.BasicClient.GetMyTeamsUnread(""); err != nil {
t.Fatal(err)
} else {
members := result.Data.([]*model.TeamUnread)
if len(members) == 0 {
t.Fatal("should have results")
}
}
if result, err := th.BasicClient.GetMyTeamsUnread(th.BasicTeam.Id); err != nil {
t.Fatal(err)
} else {
members := result.Data.([]*model.TeamUnread)
if len(members) != 0 {
t.Fatal("should not have results")
}
}
}
func TestGetTeamMember(t *testing.T) {
th := Setup().InitBasic()
if result, err := th.BasicClient.GetTeamMember(th.BasicTeam.Id, th.BasicUser.Id); err != nil {
t.Fatal(err)
} else {
member := result.Data.(*model.TeamMember)
if member == nil {
t.Fatal("should be valid")
}
}
if _, err := th.BasicClient.GetTeamMember("junk", th.BasicUser.Id); err == nil {
t.Fatal("should have errored - bad team id")
}
if _, err := th.BasicClient.GetTeamMember(th.BasicTeam.Id, ""); err == nil {
t.Fatal("should have errored - blank user id")
}
if _, err := th.BasicClient.GetTeamMember(th.BasicTeam.Id, "junk"); err == nil {
t.Fatal("should have errored - bad user id")
}
if _, err := th.BasicClient.GetTeamMember(th.BasicTeam.Id, "12345678901234567890123456"); err == nil {
t.Fatal("should have errored - bad user id")
}
}
func TestGetTeamMembersByIds(t *testing.T) {
th := Setup().InitBasic()
if result, err := th.BasicClient.GetTeamMembersByIds(th.BasicTeam.Id, []string{th.BasicUser.Id}); err != nil {
t.Fatal(err)
} else {
member := result.Data.([]*model.TeamMember)[0]
if member.UserId != th.BasicUser.Id {
t.Fatal("user id did not match")
}
if member.TeamId != th.BasicTeam.Id {
t.Fatal("team id did not match")
}
}
if result, err := th.BasicClient.GetTeamMembersByIds(th.BasicTeam.Id, []string{th.BasicUser.Id, th.BasicUser2.Id, model.NewId()}); err != nil {
t.Fatal(err)
} else {
members := result.Data.([]*model.TeamMember)
if len(members) != 2 {
t.Fatal("length should have been 2")
}
}
if _, err := th.BasicClient.GetTeamMembersByIds("junk", []string{th.BasicUser.Id}); err == nil {
t.Fatal("should have errored - bad team id")
}
if _, err := th.BasicClient.GetTeamMembersByIds(th.BasicTeam.Id, []string{}); err == nil {
t.Fatal("should have errored - empty user ids")
}
}
func TestUpdateTeamMemberRoles(t *testing.T) {
th := Setup().InitSystemAdmin().InitBasic()
th.SystemAdminClient.SetTeamId(th.BasicTeam.Id)
LinkUserToTeam(th.SystemAdminUser, th.BasicTeam)
const BASIC_MEMBER = "team_user"
const TEAM_ADMIN = "team_user team_admin"
// user 1 trying to promote user 2
if _, err := th.BasicClient.UpdateTeamRoles(th.BasicUser2.Id, TEAM_ADMIN); err == nil {
t.Fatal("Should have errored, not team admin")
}
// user 1 trying to promote themselves
if _, err := th.BasicClient.UpdateTeamRoles(th.BasicUser.Id, TEAM_ADMIN); err == nil {
t.Fatal("Should have errored, not team admin")
}
// user 1 trying to demote someone
if _, err := th.BasicClient.UpdateTeamRoles(th.SystemAdminUser.Id, BASIC_MEMBER); err == nil {
t.Fatal("Should have errored, not team admin")
}
// system admin promoting user1
if _, err := th.SystemAdminClient.UpdateTeamRoles(th.BasicUser.Id, TEAM_ADMIN); err != nil {
t.Fatal("Should have worked: " + err.Error())
}
// user 1 trying to promote user 2
if _, err := th.BasicClient.UpdateTeamRoles(th.BasicUser2.Id, TEAM_ADMIN); err != nil {
t.Fatal("Should have worked, user is team admin: " + th.BasicUser.Id)
}
// user 1 trying to demote user 2
if _, err := th.BasicClient.UpdateTeamRoles(th.BasicUser2.Id, BASIC_MEMBER); err != nil {
t.Fatal("Should have worked, user is team admin")
}
// user 1 trying to demote a system admin
if _, err := th.BasicClient.UpdateTeamRoles(th.SystemAdminUser.Id, BASIC_MEMBER); err != nil {
t.Fatal("Should have worked, user is team admin and has the ability to manage permissions on this team.")
// Note to anyone who thinks this test is wrong:
// This operation will not effect the system admin's permissions because they have global access to all teams.
// Their team level permissions are irrelavent. A team admin should be able to manage team level permissions.
}
// System admins should be able to manipulate permission no matter what their team level permissions are.
// systemAdmin trying to promote user 2
if _, err := th.SystemAdminClient.UpdateTeamRoles(th.BasicUser2.Id, TEAM_ADMIN); err != nil {
t.Fatal("Should have worked, user is system admin")
}
// system admin trying to demote user 2
if _, err := th.SystemAdminClient.UpdateTeamRoles(th.BasicUser2.Id, BASIC_MEMBER); err != nil {
t.Fatal("Should have worked, user is system admin")
}
// user 1 trying to demote himself
if _, err := th.BasicClient.UpdateTeamRoles(th.BasicUser.Id, BASIC_MEMBER); err != nil {
t.Fatal("Should have worked, user is team admin")
}
}
func TestGetTeamStats(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
Client := th.BasicClient
if result, err := th.SystemAdminClient.GetTeamStats(th.BasicTeam.Id); err != nil {
t.Fatal(err)
} else {
if result.Data.(*model.TeamStats).TotalMemberCount != 2 {
t.Fatal("wrong count")
}
if result.Data.(*model.TeamStats).ActiveMemberCount != 2 {
t.Fatal("wrong count")
}
}
th.SystemAdminClient.Must(th.SystemAdminClient.UpdateActive(th.BasicUser2.Id, false))
if result, err := th.SystemAdminClient.GetTeamStats(th.BasicTeam.Id); err != nil {
t.Fatal(err)
} else {
if result.Data.(*model.TeamStats).TotalMemberCount != 2 {
t.Fatal("wrong count")
}
if result.Data.(*model.TeamStats).ActiveMemberCount != 1 {
t.Fatal("wrong count")
}
}
if _, err := th.SystemAdminClient.GetTeamStats("junk"); err == nil {
t.Fatal("should fail invalid teamid")
} else {
if err.Id != "store.sql_team.get.find.app_error" {
t.Fatal("wrong error. Got: " + err.Id)
}
}
if result, err := th.SystemAdminClient.GetTeamStats(th.BasicTeam.Id); err != nil {
t.Fatal(err)
} else {
if result.Data.(*model.TeamStats).TotalMemberCount != 2 {
t.Fatal("wrong count")
}
}
user := model.User{Email: "success+" + model.NewId() + "@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
ruser, _ := Client.CreateUser(&user, "")
store.Must(app.Srv.Store.User().VerifyEmail(ruser.Data.(*model.User).Id))
Client.Login(user.Email, user.Password)
if _, err := Client.GetTeamStats(th.BasicTeam.Id); err == nil {
t.Fatal("should have errored - not on team")
}
}
func TestUpdateTeamDescription(t *testing.T) {
th := Setup().InitBasic()
Client := th.BasicClient
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "success+" + model.NewId() + "@simulator.amazonses.com", Type: model.TEAM_OPEN}
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
Client.Logout()
user2 := &model.User{Email: "success+" + model.NewId() + "@simulator.amazonses.com", Nickname: "Jabba the Hutt", Password: "passwd1"}
user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
LinkUserToTeam(user2, team)
store.Must(app.Srv.Store.User().VerifyEmail(user2.Id))
Client.Login(user2.Email, "passwd1")
Client.SetTeamId(team.Id)
vteam := &model.Team{DisplayName: team.DisplayName, Name: team.Name, Description: team.Description, Email: team.Email, Type: team.Type}
vteam.Description = "yommamma"
if _, err := Client.UpdateTeam(vteam); err == nil {
t.Fatal("Should have errored, not admin")
}
th.LoginBasic()
vteam.Description = ""
if _, err := Client.UpdateTeam(vteam); err != nil {
t.Fatal("Should have errored, should save blank Description")
}
vteam.Description = "yommamma"
if _, err := Client.UpdateTeam(vteam); err != nil {
t.Fatal(err)
}
}
func TestGetTeamByName(t *testing.T) {
th := Setup().InitSystemAdmin().InitBasic()
Client := th.BasicClient
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "success+" + model.NewId() + "@simulator.amazonses.com", Type: model.TEAM_INVITE}
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
team2 := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "success+" + model.NewId() + "@simulator.amazonses.com", Type: model.TEAM_OPEN}
team2 = Client.Must(Client.CreateTeam(team2)).Data.(*model.Team)
if _, err := Client.GetTeamByName(team.Name); err != nil {
t.Fatal("Failed to get team")
}
if _, err := Client.GetTeamByName("InvalidTeamName"); err == nil {
t.Fatal("Should not exist this team")
}
if _, err := Client.GetTeamByName(team2.Name); err != nil {
t.Fatal("Failed to get team")
}
Client.Must(Client.Logout())
user2 := &model.User{Email: "success+" + model.NewId() + "@simulator.amazonses.com", Nickname: "Jabba the Hutt", Password: "passwd1"}
user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
store.Must(app.Srv.Store.User().VerifyEmail(user2.Id))
Client.Login(user2.Email, "passwd1")
// TEAM_INVITE and user is not part of the team
if _, err := Client.GetTeamByName(team.Name); err == nil {
t.Fatal("Should fail dont have permissions to get the team")
}
if _, err := Client.GetTeamByName("InvalidTeamName"); err == nil {
t.Fatal("Should not exist this team")
}
// TEAM_OPEN and user is not part of the team
if _, err := Client.GetTeamByName(team2.Name); err != nil {
t.Fatal("Should not fail team is open")
}
Client.Must(Client.Logout())
th.BasicClient.Logout()
th.LoginSystemAdmin()
if _, err := th.SystemAdminClient.GetTeamByName(team.Name); err != nil {
t.Fatal("Should not failed to get team the user is admin")
}
if _, err := th.SystemAdminClient.GetTeamByName(team2.Name); err != nil {
t.Fatal("Should not failed to get team the user is admin and team is open")
}
if _, err := Client.GetTeamByName("InvalidTeamName"); err == nil {
t.Fatal("Should not exist this team")
}
}
|