github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/api4/channel_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"reflect"
    10  	"sort"
    11  	"strconv"
    12  	"strings"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/mattermost/mattermost-server/model"
    17  	"github.com/mattermost/mattermost-server/utils"
    18  )
    19  
    20  func TestCreateChannel(t *testing.T) {
    21  	th := Setup().InitBasic().InitSystemAdmin()
    22  	defer th.TearDown()
    23  	Client := th.Client
    24  	team := th.BasicTeam
    25  
    26  	channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id}
    27  	private := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
    28  
    29  	rchannel, resp := Client.CreateChannel(channel)
    30  	CheckNoError(t, resp)
    31  	CheckCreatedStatus(t, resp)
    32  
    33  	if rchannel.Name != channel.Name {
    34  		t.Fatal("names did not match")
    35  	}
    36  
    37  	if rchannel.DisplayName != channel.DisplayName {
    38  		t.Fatal("display names did not match")
    39  	}
    40  
    41  	if rchannel.TeamId != channel.TeamId {
    42  		t.Fatal("team ids did not match")
    43  	}
    44  
    45  	rprivate, resp := Client.CreateChannel(private)
    46  	CheckNoError(t, resp)
    47  
    48  	if rprivate.Name != private.Name {
    49  		t.Fatal("names did not match")
    50  	}
    51  
    52  	if rprivate.Type != model.CHANNEL_PRIVATE {
    53  		t.Fatal("wrong channel type")
    54  	}
    55  
    56  	if rprivate.CreatorId != th.BasicUser.Id {
    57  		t.Fatal("wrong creator id")
    58  	}
    59  
    60  	_, resp = Client.CreateChannel(channel)
    61  	CheckErrorMessage(t, resp, "store.sql_channel.save_channel.exists.app_error")
    62  	CheckBadRequestStatus(t, resp)
    63  
    64  	direct := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_DIRECT, TeamId: team.Id}
    65  	_, resp = Client.CreateChannel(direct)
    66  	CheckErrorMessage(t, resp, "api.channel.create_channel.direct_channel.app_error")
    67  	CheckBadRequestStatus(t, resp)
    68  
    69  	Client.Logout()
    70  	_, resp = Client.CreateChannel(channel)
    71  	CheckUnauthorizedStatus(t, resp)
    72  
    73  	userNotOnTeam := th.CreateUser()
    74  	Client.Login(userNotOnTeam.Email, userNotOnTeam.Password)
    75  
    76  	_, resp = Client.CreateChannel(channel)
    77  	CheckForbiddenStatus(t, resp)
    78  
    79  	_, resp = Client.CreateChannel(private)
    80  	CheckForbiddenStatus(t, resp)
    81  
    82  	// Check the appropriate permissions are enforced.
    83  	defaultRolePermissions := th.SaveDefaultRolePermissions()
    84  	defer func() {
    85  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
    86  	}()
    87  
    88  	th.AddPermissionToRole(model.PERMISSION_CREATE_PUBLIC_CHANNEL.Id, model.TEAM_USER_ROLE_ID)
    89  	th.AddPermissionToRole(model.PERMISSION_CREATE_PRIVATE_CHANNEL.Id, model.TEAM_USER_ROLE_ID)
    90  
    91  	th.LoginBasic()
    92  
    93  	channel.Name = GenerateTestChannelName()
    94  	_, resp = Client.CreateChannel(channel)
    95  	CheckNoError(t, resp)
    96  
    97  	private.Name = GenerateTestChannelName()
    98  	_, resp = Client.CreateChannel(private)
    99  	CheckNoError(t, resp)
   100  
   101  	th.AddPermissionToRole(model.PERMISSION_CREATE_PUBLIC_CHANNEL.Id, model.TEAM_ADMIN_ROLE_ID)
   102  	th.AddPermissionToRole(model.PERMISSION_CREATE_PRIVATE_CHANNEL.Id, model.TEAM_ADMIN_ROLE_ID)
   103  	th.RemovePermissionFromRole(model.PERMISSION_CREATE_PUBLIC_CHANNEL.Id, model.TEAM_USER_ROLE_ID)
   104  	th.RemovePermissionFromRole(model.PERMISSION_CREATE_PRIVATE_CHANNEL.Id, model.TEAM_USER_ROLE_ID)
   105  
   106  	_, resp = Client.CreateChannel(channel)
   107  	CheckForbiddenStatus(t, resp)
   108  
   109  	_, resp = Client.CreateChannel(private)
   110  	CheckForbiddenStatus(t, resp)
   111  
   112  	th.LoginTeamAdmin()
   113  
   114  	channel.Name = GenerateTestChannelName()
   115  	_, resp = Client.CreateChannel(channel)
   116  	CheckNoError(t, resp)
   117  
   118  	private.Name = GenerateTestChannelName()
   119  	_, resp = Client.CreateChannel(private)
   120  	CheckNoError(t, resp)
   121  
   122  	channel.Name = GenerateTestChannelName()
   123  	_, resp = th.SystemAdminClient.CreateChannel(channel)
   124  	CheckNoError(t, resp)
   125  
   126  	private.Name = GenerateTestChannelName()
   127  	_, resp = th.SystemAdminClient.CreateChannel(private)
   128  	CheckNoError(t, resp)
   129  
   130  	// Test posting Garbage
   131  	if r, err := Client.DoApiPost("/channels", "garbage"); err == nil {
   132  		t.Fatal("should have errored")
   133  	} else {
   134  		if r.StatusCode != http.StatusBadRequest {
   135  			t.Log("actual: " + strconv.Itoa(r.StatusCode))
   136  			t.Log("expected: " + strconv.Itoa(http.StatusBadRequest))
   137  			t.Fatal("wrong status code")
   138  		}
   139  	}
   140  }
   141  
   142  func TestUpdateChannel(t *testing.T) {
   143  	th := Setup().InitBasic().InitSystemAdmin()
   144  	defer th.TearDown()
   145  	Client := th.Client
   146  	team := th.BasicTeam
   147  
   148  	channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id}
   149  	private := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.CHANNEL_PRIVATE, TeamId: team.Id}
   150  
   151  	channel, resp := Client.CreateChannel(channel)
   152  	private, resp = Client.CreateChannel(private)
   153  
   154  	//Update a open channel
   155  	channel.DisplayName = "My new display name"
   156  	channel.Header = "My fancy header"
   157  	channel.Purpose = "Mattermost ftw!"
   158  
   159  	newChannel, resp := Client.UpdateChannel(channel)
   160  	CheckNoError(t, resp)
   161  
   162  	if newChannel.DisplayName != channel.DisplayName {
   163  		t.Fatal("Update failed for DisplayName")
   164  	}
   165  
   166  	if newChannel.Header != channel.Header {
   167  		t.Fatal("Update failed for Header")
   168  	}
   169  
   170  	if newChannel.Purpose != channel.Purpose {
   171  		t.Fatal("Update failed for Purpose")
   172  	}
   173  
   174  	//Update a private channel
   175  	private.DisplayName = "My new display name for private channel"
   176  	private.Header = "My fancy private header"
   177  	private.Purpose = "Mattermost ftw! in private mode"
   178  
   179  	newPrivateChannel, resp := Client.UpdateChannel(private)
   180  	CheckNoError(t, resp)
   181  
   182  	if newPrivateChannel.DisplayName != private.DisplayName {
   183  		t.Fatal("Update failed for DisplayName in private channel")
   184  	}
   185  
   186  	if newPrivateChannel.Header != private.Header {
   187  		t.Fatal("Update failed for Header in private channel")
   188  	}
   189  
   190  	if newPrivateChannel.Purpose != private.Purpose {
   191  		t.Fatal("Update failed for Purpose in private channel")
   192  	}
   193  
   194  	//Non existing channel
   195  	channel1 := &model.Channel{DisplayName: "Test API Name for apiv4", Name: GenerateTestChannelName(), Type: model.CHANNEL_OPEN, TeamId: team.Id}
   196  	_, resp = Client.UpdateChannel(channel1)
   197  	CheckNotFoundStatus(t, resp)
   198  
   199  	//Try to update with not logged user
   200  	Client.Logout()
   201  	_, resp = Client.UpdateChannel(channel)
   202  	CheckUnauthorizedStatus(t, resp)
   203  
   204  	//Try to update using another user
   205  	user := th.CreateUser()
   206  	Client.Login(user.Email, user.Password)
   207  
   208  	channel.DisplayName = "Should not update"
   209  	_, resp = Client.UpdateChannel(channel)
   210  	CheckNotFoundStatus(t, resp)
   211  
   212  }
   213  
   214  func TestPatchChannel(t *testing.T) {
   215  	th := Setup().InitBasic().InitSystemAdmin()
   216  	defer th.TearDown()
   217  	Client := th.Client
   218  
   219  	patch := &model.ChannelPatch{
   220  		Name:        new(string),
   221  		DisplayName: new(string),
   222  		Header:      new(string),
   223  		Purpose:     new(string),
   224  	}
   225  	*patch.Name = model.NewId()
   226  	*patch.DisplayName = model.NewId()
   227  	*patch.Header = model.NewId()
   228  	*patch.Purpose = model.NewId()
   229  
   230  	channel, resp := Client.PatchChannel(th.BasicChannel.Id, patch)
   231  	CheckNoError(t, resp)
   232  
   233  	if *patch.Name != channel.Name {
   234  		t.Fatal("do not match")
   235  	} else if *patch.DisplayName != channel.DisplayName {
   236  		t.Fatal("do not match")
   237  	} else if *patch.Header != channel.Header {
   238  		t.Fatal("do not match")
   239  	} else if *patch.Purpose != channel.Purpose {
   240  		t.Fatal("do not match")
   241  	}
   242  
   243  	patch.Name = nil
   244  	oldName := channel.Name
   245  	channel, resp = Client.PatchChannel(th.BasicChannel.Id, patch)
   246  	CheckNoError(t, resp)
   247  
   248  	if channel.Name != oldName {
   249  		t.Fatal("should not have updated")
   250  	}
   251  
   252  	_, resp = Client.PatchChannel("junk", patch)
   253  	CheckBadRequestStatus(t, resp)
   254  
   255  	_, resp = Client.PatchChannel(model.NewId(), patch)
   256  	CheckNotFoundStatus(t, resp)
   257  
   258  	user := th.CreateUser()
   259  	Client.Login(user.Email, user.Password)
   260  	_, resp = Client.PatchChannel(th.BasicChannel.Id, patch)
   261  	CheckForbiddenStatus(t, resp)
   262  
   263  	_, resp = th.SystemAdminClient.PatchChannel(th.BasicChannel.Id, patch)
   264  	CheckNoError(t, resp)
   265  
   266  	_, resp = th.SystemAdminClient.PatchChannel(th.BasicPrivateChannel.Id, patch)
   267  	CheckNoError(t, resp)
   268  }
   269  
   270  func TestCreateDirectChannel(t *testing.T) {
   271  	th := Setup().InitBasic().InitSystemAdmin()
   272  	defer th.TearDown()
   273  	Client := th.Client
   274  	user1 := th.BasicUser
   275  	user2 := th.BasicUser2
   276  	user3 := th.CreateUser()
   277  
   278  	dm, resp := Client.CreateDirectChannel(user1.Id, user2.Id)
   279  	CheckNoError(t, resp)
   280  
   281  	channelName := ""
   282  	if user2.Id > user1.Id {
   283  		channelName = user1.Id + "__" + user2.Id
   284  	} else {
   285  		channelName = user2.Id + "__" + user1.Id
   286  	}
   287  
   288  	if dm.Name != channelName {
   289  		t.Fatal("dm name didn't match")
   290  	}
   291  
   292  	_, resp = Client.CreateDirectChannel("junk", user2.Id)
   293  	CheckBadRequestStatus(t, resp)
   294  
   295  	_, resp = Client.CreateDirectChannel(user1.Id, model.NewId())
   296  	CheckBadRequestStatus(t, resp)
   297  
   298  	_, resp = Client.CreateDirectChannel(model.NewId(), user1.Id)
   299  	CheckBadRequestStatus(t, resp)
   300  
   301  	_, resp = Client.CreateDirectChannel(model.NewId(), user2.Id)
   302  	CheckForbiddenStatus(t, resp)
   303  
   304  	if r, err := Client.DoApiPost("/channels/direct", "garbage"); err == nil {
   305  		t.Fatal("should have errored")
   306  	} else {
   307  		if r.StatusCode != http.StatusBadRequest {
   308  			t.Log("actual: " + strconv.Itoa(r.StatusCode))
   309  			t.Log("expected: " + strconv.Itoa(http.StatusBadRequest))
   310  			t.Fatal("wrong status code")
   311  		}
   312  	}
   313  
   314  	Client.Logout()
   315  	_, resp = Client.CreateDirectChannel(model.NewId(), user2.Id)
   316  	CheckUnauthorizedStatus(t, resp)
   317  
   318  	_, resp = th.SystemAdminClient.CreateDirectChannel(user3.Id, user2.Id)
   319  	CheckNoError(t, resp)
   320  }
   321  
   322  func TestCreateGroupChannel(t *testing.T) {
   323  	th := Setup().InitBasic().InitSystemAdmin()
   324  	defer th.TearDown()
   325  	Client := th.Client
   326  	user := th.BasicUser
   327  	user2 := th.BasicUser2
   328  	user3 := th.CreateUser()
   329  
   330  	userIds := []string{user.Id, user2.Id, user3.Id}
   331  
   332  	rgc, resp := Client.CreateGroupChannel(userIds)
   333  	CheckNoError(t, resp)
   334  	CheckCreatedStatus(t, resp)
   335  
   336  	if rgc == nil {
   337  		t.Fatal("should have created a group channel")
   338  	}
   339  
   340  	if rgc.Type != model.CHANNEL_GROUP {
   341  		t.Fatal("should have created a channel of group type")
   342  	}
   343  
   344  	m, _ := th.App.GetChannelMembersPage(rgc.Id, 0, 10)
   345  	if len(*m) != 3 {
   346  		t.Fatal("should have 3 channel members")
   347  	}
   348  
   349  	// saving duplicate group channel
   350  	rgc2, resp := Client.CreateGroupChannel([]string{user3.Id, user2.Id})
   351  	CheckNoError(t, resp)
   352  
   353  	if rgc.Id != rgc2.Id {
   354  		t.Fatal("should have returned existing channel")
   355  	}
   356  
   357  	m2, _ := th.App.GetChannelMembersPage(rgc2.Id, 0, 10)
   358  	if !reflect.DeepEqual(*m, *m2) {
   359  		t.Fatal("should be equal")
   360  	}
   361  
   362  	rgc, resp = Client.CreateGroupChannel([]string{user2.Id})
   363  	CheckBadRequestStatus(t, resp)
   364  
   365  	user4 := th.CreateUser()
   366  	user5 := th.CreateUser()
   367  	user6 := th.CreateUser()
   368  	user7 := th.CreateUser()
   369  	user8 := th.CreateUser()
   370  	user9 := th.CreateUser()
   371  
   372  	rgc, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, user4.Id, user5.Id, user6.Id, user7.Id, user8.Id, user9.Id})
   373  	CheckBadRequestStatus(t, resp)
   374  
   375  	if rgc != nil {
   376  		t.Fatal("should return nil")
   377  	}
   378  
   379  	_, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, GenerateTestId()})
   380  	CheckBadRequestStatus(t, resp)
   381  
   382  	_, resp = Client.CreateGroupChannel([]string{user.Id, user2.Id, user3.Id, "junk"})
   383  	CheckBadRequestStatus(t, resp)
   384  
   385  	Client.Logout()
   386  
   387  	_, resp = Client.CreateGroupChannel(userIds)
   388  	CheckUnauthorizedStatus(t, resp)
   389  
   390  	_, resp = th.SystemAdminClient.CreateGroupChannel(userIds)
   391  	CheckNoError(t, resp)
   392  }
   393  
   394  func TestGetChannel(t *testing.T) {
   395  	th := Setup().InitBasic().InitSystemAdmin()
   396  	defer th.TearDown()
   397  	Client := th.Client
   398  
   399  	channel, resp := Client.GetChannel(th.BasicChannel.Id, "")
   400  	CheckNoError(t, resp)
   401  
   402  	if channel.Id != th.BasicChannel.Id {
   403  		t.Fatal("ids did not match")
   404  	}
   405  
   406  	Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id)
   407  	_, resp = Client.GetChannel(th.BasicChannel.Id, "")
   408  	CheckNoError(t, resp)
   409  
   410  	channel, resp = Client.GetChannel(th.BasicPrivateChannel.Id, "")
   411  	CheckNoError(t, resp)
   412  
   413  	if channel.Id != th.BasicPrivateChannel.Id {
   414  		t.Fatal("ids did not match")
   415  	}
   416  
   417  	Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id)
   418  	_, resp = Client.GetChannel(th.BasicPrivateChannel.Id, "")
   419  	CheckForbiddenStatus(t, resp)
   420  
   421  	_, resp = Client.GetChannel(model.NewId(), "")
   422  	CheckNotFoundStatus(t, resp)
   423  
   424  	Client.Logout()
   425  	_, resp = Client.GetChannel(th.BasicChannel.Id, "")
   426  	CheckUnauthorizedStatus(t, resp)
   427  
   428  	user := th.CreateUser()
   429  	Client.Login(user.Email, user.Password)
   430  	_, resp = Client.GetChannel(th.BasicChannel.Id, "")
   431  	CheckForbiddenStatus(t, resp)
   432  
   433  	_, resp = th.SystemAdminClient.GetChannel(th.BasicChannel.Id, "")
   434  	CheckNoError(t, resp)
   435  
   436  	_, resp = th.SystemAdminClient.GetChannel(th.BasicPrivateChannel.Id, "")
   437  	CheckNoError(t, resp)
   438  
   439  	_, resp = th.SystemAdminClient.GetChannel(th.BasicUser.Id, "")
   440  	CheckNotFoundStatus(t, resp)
   441  }
   442  
   443  func TestGetDeletedChannelsForTeam(t *testing.T) {
   444  	th := Setup().InitBasic().InitSystemAdmin()
   445  	defer th.TearDown()
   446  	Client := th.Client
   447  	team := th.BasicTeam
   448  
   449  	channels, resp := Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "")
   450  	CheckForbiddenStatus(t, resp)
   451  
   452  	th.LoginTeamAdmin()
   453  
   454  	channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "")
   455  	CheckNoError(t, resp)
   456  	numInitialChannelsForTeam := len(channels)
   457  
   458  	// create and delete public channel
   459  	publicChannel1 := th.CreatePublicChannel()
   460  	Client.DeleteChannel(publicChannel1.Id)
   461  
   462  	channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "")
   463  	CheckNoError(t, resp)
   464  	if len(channels) != numInitialChannelsForTeam+1 {
   465  		t.Fatal("should be 1 deleted channel")
   466  	}
   467  
   468  	publicChannel2 := th.CreatePublicChannel()
   469  	Client.DeleteChannel(publicChannel2.Id)
   470  
   471  	channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "")
   472  	CheckNoError(t, resp)
   473  	if len(channels) != numInitialChannelsForTeam+2 {
   474  		t.Fatal("should be 2 deleted channels")
   475  	}
   476  
   477  	channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 1, "")
   478  	CheckNoError(t, resp)
   479  	if len(channels) != 1 {
   480  		t.Fatal("should be one channel per page")
   481  	}
   482  
   483  	channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 1, 1, "")
   484  	CheckNoError(t, resp)
   485  	if len(channels) != 1 {
   486  		t.Fatal("should be one channel per page")
   487  	}
   488  }
   489  
   490  func TestGetPublicChannelsForTeam(t *testing.T) {
   491  	th := Setup().InitBasic().InitSystemAdmin()
   492  	defer th.TearDown()
   493  	Client := th.Client
   494  	team := th.BasicTeam
   495  	publicChannel1 := th.BasicChannel
   496  	publicChannel2 := th.BasicChannel2
   497  
   498  	channels, resp := Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
   499  	CheckNoError(t, resp)
   500  	if len(channels) != 4 {
   501  		t.Fatal("wrong length")
   502  	}
   503  
   504  	for i, c := range channels {
   505  		if c.Type != model.CHANNEL_OPEN {
   506  			t.Fatal("should include open channel only")
   507  		}
   508  
   509  		// only check the created 2 public channels
   510  		if i < 2 && !(c.DisplayName == publicChannel1.DisplayName || c.DisplayName == publicChannel2.DisplayName) {
   511  			t.Logf("channel %v: %v", i, c.DisplayName)
   512  			t.Fatal("should match public channel display name only")
   513  		}
   514  	}
   515  
   516  	privateChannel := th.CreatePrivateChannel()
   517  	channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
   518  	CheckNoError(t, resp)
   519  	if len(channels) != 4 {
   520  		t.Fatal("wrong length")
   521  	}
   522  
   523  	for _, c := range channels {
   524  		if c.Type != model.CHANNEL_OPEN {
   525  			t.Fatal("should not include private channel")
   526  		}
   527  
   528  		if c.DisplayName == privateChannel.DisplayName {
   529  			t.Fatal("should not match private channel display name")
   530  		}
   531  	}
   532  
   533  	channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 1, "")
   534  	CheckNoError(t, resp)
   535  	if len(channels) != 1 {
   536  		t.Fatal("should be one channel per page")
   537  	}
   538  
   539  	channels, resp = Client.GetPublicChannelsForTeam(team.Id, 1, 1, "")
   540  	CheckNoError(t, resp)
   541  	if len(channels) != 1 {
   542  		t.Fatal("should be one channel per page")
   543  	}
   544  
   545  	channels, resp = Client.GetPublicChannelsForTeam(team.Id, 10000, 100, "")
   546  	CheckNoError(t, resp)
   547  	if len(channels) != 0 {
   548  		t.Fatal("should be no channel")
   549  	}
   550  
   551  	_, resp = Client.GetPublicChannelsForTeam("junk", 0, 100, "")
   552  	CheckBadRequestStatus(t, resp)
   553  
   554  	_, resp = Client.GetPublicChannelsForTeam(model.NewId(), 0, 100, "")
   555  	CheckForbiddenStatus(t, resp)
   556  
   557  	Client.Logout()
   558  	_, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
   559  	CheckUnauthorizedStatus(t, resp)
   560  
   561  	user := th.CreateUser()
   562  	Client.Login(user.Email, user.Password)
   563  	_, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
   564  	CheckForbiddenStatus(t, resp)
   565  
   566  	_, resp = th.SystemAdminClient.GetPublicChannelsForTeam(team.Id, 0, 100, "")
   567  	CheckNoError(t, resp)
   568  }
   569  
   570  func TestGetPublicChannelsByIdsForTeam(t *testing.T) {
   571  	th := Setup().InitBasic().InitSystemAdmin()
   572  	defer th.TearDown()
   573  	Client := th.Client
   574  	teamId := th.BasicTeam.Id
   575  	input := []string{th.BasicChannel.Id}
   576  	output := []string{th.BasicChannel.DisplayName}
   577  
   578  	channels, resp := Client.GetPublicChannelsByIdsForTeam(teamId, input)
   579  	CheckNoError(t, resp)
   580  
   581  	if len(channels) != 1 {
   582  		t.Fatal("should return 1 channel")
   583  	}
   584  
   585  	if (channels)[0].DisplayName != output[0] {
   586  		t.Fatal("missing channel")
   587  	}
   588  
   589  	input = append(input, GenerateTestId())
   590  	input = append(input, th.BasicChannel2.Id)
   591  	input = append(input, th.BasicPrivateChannel.Id)
   592  	output = append(output, th.BasicChannel2.DisplayName)
   593  	sort.Strings(output)
   594  
   595  	channels, resp = Client.GetPublicChannelsByIdsForTeam(teamId, input)
   596  	CheckNoError(t, resp)
   597  
   598  	if len(channels) != 2 {
   599  		t.Fatal("should return 2 channels")
   600  	}
   601  
   602  	for i, c := range channels {
   603  		if c.DisplayName != output[i] {
   604  			t.Fatal("missing channel")
   605  		}
   606  	}
   607  
   608  	_, resp = Client.GetPublicChannelsByIdsForTeam(GenerateTestId(), input)
   609  	CheckForbiddenStatus(t, resp)
   610  
   611  	_, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{})
   612  	CheckBadRequestStatus(t, resp)
   613  
   614  	_, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{"junk"})
   615  	CheckBadRequestStatus(t, resp)
   616  
   617  	_, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{GenerateTestId()})
   618  	CheckNotFoundStatus(t, resp)
   619  
   620  	_, resp = Client.GetPublicChannelsByIdsForTeam(teamId, []string{th.BasicPrivateChannel.Id})
   621  	CheckNotFoundStatus(t, resp)
   622  
   623  	Client.Logout()
   624  
   625  	_, resp = Client.GetPublicChannelsByIdsForTeam(teamId, input)
   626  	CheckUnauthorizedStatus(t, resp)
   627  
   628  	_, resp = th.SystemAdminClient.GetPublicChannelsByIdsForTeam(teamId, input)
   629  	CheckNoError(t, resp)
   630  }
   631  
   632  func TestGetChannelsForTeamForUser(t *testing.T) {
   633  	th := Setup().InitBasic().InitSystemAdmin()
   634  	defer th.TearDown()
   635  	Client := th.Client
   636  
   637  	channels, resp := Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "")
   638  	CheckNoError(t, resp)
   639  
   640  	found := make([]bool, 3)
   641  	for _, c := range channels {
   642  		if c.Id == th.BasicChannel.Id {
   643  			found[0] = true
   644  		} else if c.Id == th.BasicChannel2.Id {
   645  			found[1] = true
   646  		} else if c.Id == th.BasicPrivateChannel.Id {
   647  			found[2] = true
   648  		}
   649  
   650  		if c.TeamId != th.BasicTeam.Id && c.TeamId != "" {
   651  			t.Fatal("wrong team")
   652  		}
   653  	}
   654  
   655  	for _, f := range found {
   656  		if !f {
   657  			t.Fatal("missing a channel")
   658  		}
   659  	}
   660  
   661  	channels, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, resp.Etag)
   662  	CheckEtag(t, channels, resp)
   663  
   664  	_, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, "junk", "")
   665  	CheckBadRequestStatus(t, resp)
   666  
   667  	_, resp = Client.GetChannelsForTeamForUser("junk", th.BasicUser.Id, "")
   668  	CheckBadRequestStatus(t, resp)
   669  
   670  	_, resp = Client.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser2.Id, "")
   671  	CheckForbiddenStatus(t, resp)
   672  
   673  	_, resp = Client.GetChannelsForTeamForUser(model.NewId(), th.BasicUser.Id, "")
   674  	CheckForbiddenStatus(t, resp)
   675  
   676  	_, resp = th.SystemAdminClient.GetChannelsForTeamForUser(th.BasicTeam.Id, th.BasicUser.Id, "")
   677  	CheckNoError(t, resp)
   678  }
   679  
   680  func TestSearchChannels(t *testing.T) {
   681  	th := Setup().InitBasic().InitSystemAdmin()
   682  	defer th.TearDown()
   683  	Client := th.Client
   684  
   685  	search := &model.ChannelSearch{Term: th.BasicChannel.Name}
   686  
   687  	channels, resp := Client.SearchChannels(th.BasicTeam.Id, search)
   688  	CheckNoError(t, resp)
   689  
   690  	found := false
   691  	for _, c := range channels {
   692  		if c.Type != model.CHANNEL_OPEN {
   693  			t.Fatal("should only return public channels")
   694  		}
   695  
   696  		if c.Id == th.BasicChannel.Id {
   697  			found = true
   698  		}
   699  	}
   700  
   701  	if !found {
   702  		t.Fatal("didn't find channel")
   703  	}
   704  
   705  	search.Term = th.BasicPrivateChannel.Name
   706  	channels, resp = Client.SearchChannels(th.BasicTeam.Id, search)
   707  	CheckNoError(t, resp)
   708  
   709  	found = false
   710  	for _, c := range channels {
   711  		if c.Id == th.BasicPrivateChannel.Id {
   712  			found = true
   713  		}
   714  	}
   715  
   716  	if found {
   717  		t.Fatal("shouldn't find private channel")
   718  	}
   719  
   720  	search.Term = ""
   721  	_, resp = Client.SearchChannels(th.BasicTeam.Id, search)
   722  	CheckNoError(t, resp)
   723  
   724  	search.Term = th.BasicChannel.Name
   725  	_, resp = Client.SearchChannels(model.NewId(), search)
   726  	CheckForbiddenStatus(t, resp)
   727  
   728  	_, resp = Client.SearchChannels("junk", search)
   729  	CheckBadRequestStatus(t, resp)
   730  
   731  	_, resp = th.SystemAdminClient.SearchChannels(th.BasicTeam.Id, search)
   732  	CheckNoError(t, resp)
   733  }
   734  
   735  func TestDeleteChannel(t *testing.T) {
   736  	th := Setup().InitBasic().InitSystemAdmin()
   737  	defer th.TearDown()
   738  	Client := th.Client
   739  	team := th.BasicTeam
   740  	user := th.BasicUser
   741  	user2 := th.BasicUser2
   742  
   743  	// successful delete of public channel
   744  	publicChannel1 := th.CreatePublicChannel()
   745  	pass, resp := Client.DeleteChannel(publicChannel1.Id)
   746  	CheckNoError(t, resp)
   747  
   748  	if !pass {
   749  		t.Fatal("should have passed")
   750  	}
   751  
   752  	if ch, err := th.App.GetChannel(publicChannel1.Id); err == nil && ch.DeleteAt == 0 {
   753  		t.Fatal("should have failed to get deleted channel")
   754  	} else if err := th.App.JoinChannel(ch, user2.Id); err == nil {
   755  		t.Fatal("should have failed to join deleted channel")
   756  	}
   757  
   758  	post1 := &model.Post{ChannelId: publicChannel1.Id, Message: "a" + GenerateTestId() + "a"}
   759  	if _, err := Client.CreatePost(post1); err == nil {
   760  		t.Fatal("should have failed to post to deleted channel")
   761  	}
   762  
   763  	// successful delete of private channel
   764  	privateChannel2 := th.CreatePrivateChannel()
   765  	_, resp = Client.DeleteChannel(privateChannel2.Id)
   766  	CheckNoError(t, resp)
   767  
   768  	// successful delete of channel with multiple members
   769  	publicChannel3 := th.CreatePublicChannel()
   770  	th.App.AddUserToChannel(user2, publicChannel3)
   771  	_, resp = Client.DeleteChannel(publicChannel3.Id)
   772  	CheckNoError(t, resp)
   773  
   774  	// default channel cannot be deleted.
   775  	defaultChannel, _ := th.App.GetChannelByName(model.DEFAULT_CHANNEL, team.Id)
   776  	pass, resp = Client.DeleteChannel(defaultChannel.Id)
   777  	CheckBadRequestStatus(t, resp)
   778  
   779  	if pass {
   780  		t.Fatal("should have failed")
   781  	}
   782  
   783  	// check system admin can delete a channel without any appropriate team or channel membership.
   784  	sdTeam := th.CreateTeamWithClient(Client)
   785  	sdPublicChannel := &model.Channel{
   786  		DisplayName: "dn_" + model.NewId(),
   787  		Name:        GenerateTestChannelName(),
   788  		Type:        model.CHANNEL_OPEN,
   789  		TeamId:      sdTeam.Id,
   790  	}
   791  	sdPublicChannel, resp = Client.CreateChannel(sdPublicChannel)
   792  	CheckNoError(t, resp)
   793  	_, resp = th.SystemAdminClient.DeleteChannel(sdPublicChannel.Id)
   794  	CheckNoError(t, resp)
   795  
   796  	sdPrivateChannel := &model.Channel{
   797  		DisplayName: "dn_" + model.NewId(),
   798  		Name:        GenerateTestChannelName(),
   799  		Type:        model.CHANNEL_PRIVATE,
   800  		TeamId:      sdTeam.Id,
   801  	}
   802  	sdPrivateChannel, resp = Client.CreateChannel(sdPrivateChannel)
   803  	CheckNoError(t, resp)
   804  	_, resp = th.SystemAdminClient.DeleteChannel(sdPrivateChannel.Id)
   805  	CheckNoError(t, resp)
   806  
   807  	th.LoginBasic()
   808  	publicChannel5 := th.CreatePublicChannel()
   809  	Client.Logout()
   810  
   811  	Client.Login(user2.Id, user2.Password)
   812  	_, resp = Client.DeleteChannel(publicChannel5.Id)
   813  	CheckUnauthorizedStatus(t, resp)
   814  
   815  	_, resp = Client.DeleteChannel("junk")
   816  	CheckUnauthorizedStatus(t, resp)
   817  
   818  	Client.Logout()
   819  	_, resp = Client.DeleteChannel(GenerateTestId())
   820  	CheckUnauthorizedStatus(t, resp)
   821  
   822  	_, resp = th.SystemAdminClient.DeleteChannel(publicChannel5.Id)
   823  	CheckNoError(t, resp)
   824  
   825  	th.InitBasic().InitSystemAdmin()
   826  
   827  	// Check the appropriate permissions are enforced.
   828  	defaultRolePermissions := th.SaveDefaultRolePermissions()
   829  	defer func() {
   830  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
   831  	}()
   832  
   833  	th.AddPermissionToRole(model.PERMISSION_DELETE_PUBLIC_CHANNEL.Id, model.TEAM_USER_ROLE_ID)
   834  	th.AddPermissionToRole(model.PERMISSION_DELETE_PRIVATE_CHANNEL.Id, model.TEAM_USER_ROLE_ID)
   835  
   836  	Client = th.Client
   837  	team = th.BasicTeam
   838  	user = th.BasicUser
   839  	user2 = th.BasicUser2
   840  
   841  	// channels created by SystemAdmin
   842  	publicChannel6 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN)
   843  	privateChannel7 := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
   844  	th.App.AddUserToChannel(user, publicChannel6)
   845  	th.App.AddUserToChannel(user, privateChannel7)
   846  	th.App.AddUserToChannel(user2, privateChannel7)
   847  
   848  	// successful delete by user
   849  	_, resp = Client.DeleteChannel(publicChannel6.Id)
   850  	CheckNoError(t, resp)
   851  
   852  	_, resp = Client.DeleteChannel(privateChannel7.Id)
   853  	CheckNoError(t, resp)
   854  
   855  	// Restrict permissions to Channel Admins
   856  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_PUBLIC_CHANNEL.Id, model.TEAM_USER_ROLE_ID)
   857  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_PRIVATE_CHANNEL.Id, model.TEAM_USER_ROLE_ID)
   858  	th.AddPermissionToRole(model.PERMISSION_DELETE_PUBLIC_CHANNEL.Id, model.CHANNEL_ADMIN_ROLE_ID)
   859  	th.AddPermissionToRole(model.PERMISSION_DELETE_PRIVATE_CHANNEL.Id, model.CHANNEL_ADMIN_ROLE_ID)
   860  
   861  	// channels created by SystemAdmin
   862  	publicChannel6 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_OPEN)
   863  	privateChannel7 = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
   864  	th.App.AddUserToChannel(user, publicChannel6)
   865  	th.App.AddUserToChannel(user, privateChannel7)
   866  	th.App.AddUserToChannel(user2, privateChannel7)
   867  
   868  	// cannot delete by user
   869  	_, resp = Client.DeleteChannel(publicChannel6.Id)
   870  	CheckForbiddenStatus(t, resp)
   871  
   872  	_, resp = Client.DeleteChannel(privateChannel7.Id)
   873  	CheckForbiddenStatus(t, resp)
   874  
   875  	// successful delete by channel admin
   876  	th.MakeUserChannelAdmin(user, publicChannel6)
   877  	th.MakeUserChannelAdmin(user, privateChannel7)
   878  	th.App.Srv.Store.Channel().ClearCaches()
   879  
   880  	_, resp = Client.DeleteChannel(publicChannel6.Id)
   881  	CheckNoError(t, resp)
   882  
   883  	_, resp = Client.DeleteChannel(privateChannel7.Id)
   884  	CheckNoError(t, resp)
   885  
   886  	// Make sure team admins don't have permission to delete channels.
   887  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_PUBLIC_CHANNEL.Id, model.CHANNEL_ADMIN_ROLE_ID)
   888  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_PRIVATE_CHANNEL.Id, model.CHANNEL_ADMIN_ROLE_ID)
   889  
   890  	// last member of a public channel should have required permission to delete
   891  	publicChannel6 = th.CreateChannelWithClient(th.Client, model.CHANNEL_OPEN)
   892  	_, resp = Client.DeleteChannel(publicChannel6.Id)
   893  	CheckForbiddenStatus(t, resp)
   894  
   895  	// last member of a private channel should not be able to delete it if they don't have required permissions
   896  	privateChannel7 = th.CreateChannelWithClient(th.Client, model.CHANNEL_PRIVATE)
   897  	_, resp = Client.DeleteChannel(privateChannel7.Id)
   898  	CheckForbiddenStatus(t, resp)
   899  }
   900  
   901  func TestConvertChannelToPrivate(t *testing.T) {
   902  	th := Setup().InitBasic().InitSystemAdmin()
   903  	defer th.TearDown()
   904  	Client := th.Client
   905  
   906  	defaultChannel, _ := th.App.GetChannelByName(model.DEFAULT_CHANNEL, th.BasicTeam.Id)
   907  	_, resp := Client.ConvertChannelToPrivate(defaultChannel.Id)
   908  	CheckForbiddenStatus(t, resp)
   909  
   910  	privateChannel := th.CreatePrivateChannel()
   911  	_, resp = Client.ConvertChannelToPrivate(privateChannel.Id)
   912  	CheckForbiddenStatus(t, resp)
   913  
   914  	publicChannel := th.CreatePublicChannel()
   915  	_, resp = Client.ConvertChannelToPrivate(publicChannel.Id)
   916  	CheckForbiddenStatus(t, resp)
   917  
   918  	th.LoginTeamAdmin()
   919  	rchannel, resp := Client.ConvertChannelToPrivate(publicChannel.Id)
   920  	CheckOKStatus(t, resp)
   921  	if rchannel.Type != model.CHANNEL_PRIVATE {
   922  		t.Fatal("channel should be converted from public to private")
   923  	}
   924  
   925  	rchannel, resp = th.SystemAdminClient.ConvertChannelToPrivate(privateChannel.Id)
   926  	CheckBadRequestStatus(t, resp)
   927  	if rchannel != nil {
   928  		t.Fatal("should not return a channel")
   929  	}
   930  
   931  	rchannel, resp = th.SystemAdminClient.ConvertChannelToPrivate(defaultChannel.Id)
   932  	CheckBadRequestStatus(t, resp)
   933  	if rchannel != nil {
   934  		t.Fatal("should not return a channel")
   935  	}
   936  
   937  	WebSocketClient, err := th.CreateWebSocketClient()
   938  	if err != nil {
   939  		t.Fatal(err)
   940  	}
   941  	WebSocketClient.Listen()
   942  
   943  	publicChannel2 := th.CreatePublicChannel()
   944  	rchannel, resp = th.SystemAdminClient.ConvertChannelToPrivate(publicChannel2.Id)
   945  	CheckOKStatus(t, resp)
   946  	if rchannel.Type != model.CHANNEL_PRIVATE {
   947  		t.Fatal("channel should be converted from public to private")
   948  	}
   949  
   950  	stop := make(chan bool)
   951  	eventHit := false
   952  
   953  	go func() {
   954  		for {
   955  			select {
   956  			case resp := <-WebSocketClient.EventChannel:
   957  				if resp.Event == model.WEBSOCKET_EVENT_CHANNEL_CONVERTED && resp.Data["channel_id"].(string) == publicChannel2.Id {
   958  					eventHit = true
   959  				}
   960  			case <-stop:
   961  				return
   962  			}
   963  		}
   964  	}()
   965  
   966  	time.Sleep(400 * time.Millisecond)
   967  
   968  	stop <- true
   969  
   970  	if !eventHit {
   971  		t.Fatal("did not receive channel_converted event")
   972  	}
   973  }
   974  
   975  func TestRestoreChannel(t *testing.T) {
   976  	th := Setup().InitBasic().InitSystemAdmin()
   977  	defer th.TearDown()
   978  	Client := th.Client
   979  
   980  	publicChannel1 := th.CreatePublicChannel()
   981  	Client.DeleteChannel(publicChannel1.Id)
   982  
   983  	privateChannel1 := th.CreatePrivateChannel()
   984  	Client.DeleteChannel(privateChannel1.Id)
   985  
   986  	_, resp := Client.RestoreChannel(publicChannel1.Id)
   987  	CheckForbiddenStatus(t, resp)
   988  
   989  	_, resp = Client.RestoreChannel(privateChannel1.Id)
   990  	CheckForbiddenStatus(t, resp)
   991  
   992  	th.LoginTeamAdmin()
   993  
   994  	_, resp = Client.RestoreChannel(publicChannel1.Id)
   995  	CheckOKStatus(t, resp)
   996  
   997  	_, resp = Client.RestoreChannel(privateChannel1.Id)
   998  	CheckOKStatus(t, resp)
   999  }
  1000  
  1001  func TestGetChannelByName(t *testing.T) {
  1002  	th := Setup().InitBasic().InitSystemAdmin()
  1003  	defer th.TearDown()
  1004  	Client := th.Client
  1005  
  1006  	channel, resp := Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "")
  1007  	CheckNoError(t, resp)
  1008  
  1009  	if channel.Name != th.BasicChannel.Name {
  1010  		t.Fatal("names did not match")
  1011  	}
  1012  
  1013  	channel, resp = Client.GetChannelByName(th.BasicPrivateChannel.Name, th.BasicTeam.Id, "")
  1014  	CheckNoError(t, resp)
  1015  
  1016  	if channel.Name != th.BasicPrivateChannel.Name {
  1017  		t.Fatal("names did not match")
  1018  	}
  1019  
  1020  	_, resp = Client.GetChannelByName(strings.ToUpper(th.BasicPrivateChannel.Name), th.BasicTeam.Id, "")
  1021  	CheckNoError(t, resp)
  1022  
  1023  	Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id)
  1024  	_, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "")
  1025  	CheckNoError(t, resp)
  1026  
  1027  	Client.RemoveUserFromChannel(th.BasicPrivateChannel.Id, th.BasicUser.Id)
  1028  	_, resp = Client.GetChannelByName(th.BasicPrivateChannel.Name, th.BasicTeam.Id, "")
  1029  	CheckForbiddenStatus(t, resp)
  1030  
  1031  	_, resp = Client.GetChannelByName(GenerateTestChannelName(), th.BasicTeam.Id, "")
  1032  	CheckNotFoundStatus(t, resp)
  1033  
  1034  	_, resp = Client.GetChannelByName(GenerateTestChannelName(), "junk", "")
  1035  	CheckBadRequestStatus(t, resp)
  1036  
  1037  	Client.Logout()
  1038  	_, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "")
  1039  	CheckUnauthorizedStatus(t, resp)
  1040  
  1041  	user := th.CreateUser()
  1042  	Client.Login(user.Email, user.Password)
  1043  	_, resp = Client.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "")
  1044  	CheckForbiddenStatus(t, resp)
  1045  
  1046  	_, resp = th.SystemAdminClient.GetChannelByName(th.BasicChannel.Name, th.BasicTeam.Id, "")
  1047  	CheckNoError(t, resp)
  1048  }
  1049  
  1050  func TestGetChannelByNameForTeamName(t *testing.T) {
  1051  	th := Setup().InitBasic().InitSystemAdmin()
  1052  	defer th.TearDown()
  1053  	Client := th.Client
  1054  
  1055  	channel, resp := th.SystemAdminClient.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "")
  1056  	CheckNoError(t, resp)
  1057  
  1058  	if channel.Name != th.BasicChannel.Name {
  1059  		t.Fatal("names did not match")
  1060  	}
  1061  
  1062  	_, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "")
  1063  	CheckNoError(t, resp)
  1064  
  1065  	_, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, model.NewRandomString(15), "")
  1066  	CheckNotFoundStatus(t, resp)
  1067  
  1068  	_, resp = Client.GetChannelByNameForTeamName(GenerateTestChannelName(), th.BasicTeam.Name, "")
  1069  	CheckNotFoundStatus(t, resp)
  1070  
  1071  	Client.Logout()
  1072  	_, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "")
  1073  	CheckUnauthorizedStatus(t, resp)
  1074  
  1075  	user := th.CreateUser()
  1076  	Client.Login(user.Email, user.Password)
  1077  	_, resp = Client.GetChannelByNameForTeamName(th.BasicChannel.Name, th.BasicTeam.Name, "")
  1078  	CheckForbiddenStatus(t, resp)
  1079  }
  1080  
  1081  func TestGetChannelMembers(t *testing.T) {
  1082  	th := Setup().InitBasic().InitSystemAdmin()
  1083  	defer th.TearDown()
  1084  	Client := th.Client
  1085  
  1086  	members, resp := Client.GetChannelMembers(th.BasicChannel.Id, 0, 60, "")
  1087  	CheckNoError(t, resp)
  1088  
  1089  	if len(*members) != 3 {
  1090  		t.Fatal("should only be 3 users in channel")
  1091  	}
  1092  
  1093  	members, resp = Client.GetChannelMembers(th.BasicChannel.Id, 0, 2, "")
  1094  	CheckNoError(t, resp)
  1095  
  1096  	if len(*members) != 2 {
  1097  		t.Fatal("should only be 2 users")
  1098  	}
  1099  
  1100  	members, resp = Client.GetChannelMembers(th.BasicChannel.Id, 1, 1, "")
  1101  	CheckNoError(t, resp)
  1102  
  1103  	if len(*members) != 1 {
  1104  		t.Fatal("should only be 1 user")
  1105  	}
  1106  
  1107  	members, resp = Client.GetChannelMembers(th.BasicChannel.Id, 1000, 100000, "")
  1108  	CheckNoError(t, resp)
  1109  
  1110  	if len(*members) != 0 {
  1111  		t.Fatal("should be 0 users")
  1112  	}
  1113  
  1114  	_, resp = Client.GetChannelMembers("", 0, 60, "")
  1115  	CheckBadRequestStatus(t, resp)
  1116  
  1117  	_, resp = Client.GetChannelMembers("junk", 0, 60, "")
  1118  	CheckBadRequestStatus(t, resp)
  1119  
  1120  	_, resp = Client.GetChannelMembers(model.NewId(), 0, 60, "")
  1121  	CheckForbiddenStatus(t, resp)
  1122  
  1123  	Client.Logout()
  1124  	_, resp = Client.GetChannelMembers(th.BasicChannel.Id, 0, 60, "")
  1125  	CheckUnauthorizedStatus(t, resp)
  1126  
  1127  	user := th.CreateUser()
  1128  	Client.Login(user.Email, user.Password)
  1129  	_, resp = Client.GetChannelMembers(th.BasicChannel.Id, 0, 60, "")
  1130  	CheckForbiddenStatus(t, resp)
  1131  
  1132  	_, resp = th.SystemAdminClient.GetChannelMembers(th.BasicChannel.Id, 0, 60, "")
  1133  	CheckNoError(t, resp)
  1134  }
  1135  
  1136  func TestGetChannelMembersByIds(t *testing.T) {
  1137  	th := Setup().InitBasic().InitSystemAdmin()
  1138  	defer th.TearDown()
  1139  	Client := th.Client
  1140  
  1141  	cm, resp := Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id})
  1142  	CheckNoError(t, resp)
  1143  
  1144  	if (*cm)[0].UserId != th.BasicUser.Id {
  1145  		t.Fatal("returned wrong user")
  1146  	}
  1147  
  1148  	_, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{})
  1149  	CheckBadRequestStatus(t, resp)
  1150  
  1151  	cm1, resp := Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{"junk"})
  1152  	CheckNoError(t, resp)
  1153  	if len(*cm1) > 0 {
  1154  		t.Fatal("no users should be returned")
  1155  	}
  1156  
  1157  	cm1, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{"junk", th.BasicUser.Id})
  1158  	CheckNoError(t, resp)
  1159  	if len(*cm1) != 1 {
  1160  		t.Fatal("1 member should be returned")
  1161  	}
  1162  
  1163  	cm1, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser2.Id, th.BasicUser.Id})
  1164  	CheckNoError(t, resp)
  1165  	if len(*cm1) != 2 {
  1166  		t.Fatal("2 members should be returned")
  1167  	}
  1168  
  1169  	_, resp = Client.GetChannelMembersByIds("junk", []string{th.BasicUser.Id})
  1170  	CheckBadRequestStatus(t, resp)
  1171  
  1172  	_, resp = Client.GetChannelMembersByIds(model.NewId(), []string{th.BasicUser.Id})
  1173  	CheckForbiddenStatus(t, resp)
  1174  
  1175  	Client.Logout()
  1176  	_, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id})
  1177  	CheckUnauthorizedStatus(t, resp)
  1178  
  1179  	_, resp = th.SystemAdminClient.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser2.Id, th.BasicUser.Id})
  1180  	CheckNoError(t, resp)
  1181  }
  1182  
  1183  func TestGetChannelMember(t *testing.T) {
  1184  	th := Setup().InitBasic().InitSystemAdmin()
  1185  	defer th.TearDown()
  1186  	Client := th.Client
  1187  
  1188  	member, resp := Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "")
  1189  	CheckNoError(t, resp)
  1190  
  1191  	if member.ChannelId != th.BasicChannel.Id {
  1192  		t.Fatal("wrong channel id")
  1193  	}
  1194  
  1195  	if member.UserId != th.BasicUser.Id {
  1196  		t.Fatal("wrong user id")
  1197  	}
  1198  
  1199  	_, resp = Client.GetChannelMember("", th.BasicUser.Id, "")
  1200  	CheckNotFoundStatus(t, resp)
  1201  
  1202  	_, resp = Client.GetChannelMember("junk", th.BasicUser.Id, "")
  1203  	CheckBadRequestStatus(t, resp)
  1204  
  1205  	_, resp = Client.GetChannelMember(model.NewId(), th.BasicUser.Id, "")
  1206  	CheckForbiddenStatus(t, resp)
  1207  
  1208  	_, resp = Client.GetChannelMember(th.BasicChannel.Id, "", "")
  1209  	CheckNotFoundStatus(t, resp)
  1210  
  1211  	_, resp = Client.GetChannelMember(th.BasicChannel.Id, "junk", "")
  1212  	CheckBadRequestStatus(t, resp)
  1213  
  1214  	_, resp = Client.GetChannelMember(th.BasicChannel.Id, model.NewId(), "")
  1215  	CheckNotFoundStatus(t, resp)
  1216  
  1217  	Client.Logout()
  1218  	_, resp = Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "")
  1219  	CheckUnauthorizedStatus(t, resp)
  1220  
  1221  	user := th.CreateUser()
  1222  	Client.Login(user.Email, user.Password)
  1223  	_, resp = Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "")
  1224  	CheckForbiddenStatus(t, resp)
  1225  
  1226  	_, resp = th.SystemAdminClient.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "")
  1227  	CheckNoError(t, resp)
  1228  }
  1229  
  1230  func TestGetChannelMembersForUser(t *testing.T) {
  1231  	th := Setup().InitBasic().InitSystemAdmin()
  1232  	defer th.TearDown()
  1233  	Client := th.Client
  1234  
  1235  	members, resp := Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "")
  1236  	CheckNoError(t, resp)
  1237  
  1238  	if len(*members) != 5 {
  1239  		t.Fatal("should have 5 members on team")
  1240  	}
  1241  
  1242  	_, resp = Client.GetChannelMembersForUser("", th.BasicTeam.Id, "")
  1243  	CheckNotFoundStatus(t, resp)
  1244  
  1245  	_, resp = Client.GetChannelMembersForUser("junk", th.BasicTeam.Id, "")
  1246  	CheckBadRequestStatus(t, resp)
  1247  
  1248  	_, resp = Client.GetChannelMembersForUser(model.NewId(), th.BasicTeam.Id, "")
  1249  	CheckForbiddenStatus(t, resp)
  1250  
  1251  	_, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, "", "")
  1252  	CheckNotFoundStatus(t, resp)
  1253  
  1254  	_, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, "junk", "")
  1255  	CheckBadRequestStatus(t, resp)
  1256  
  1257  	_, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, model.NewId(), "")
  1258  	CheckForbiddenStatus(t, resp)
  1259  
  1260  	Client.Logout()
  1261  	_, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "")
  1262  	CheckUnauthorizedStatus(t, resp)
  1263  
  1264  	user := th.CreateUser()
  1265  	Client.Login(user.Email, user.Password)
  1266  	_, resp = Client.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "")
  1267  	CheckForbiddenStatus(t, resp)
  1268  
  1269  	_, resp = th.SystemAdminClient.GetChannelMembersForUser(th.BasicUser.Id, th.BasicTeam.Id, "")
  1270  	CheckNoError(t, resp)
  1271  }
  1272  
  1273  func TestViewChannel(t *testing.T) {
  1274  	th := Setup().InitBasic().InitSystemAdmin()
  1275  	defer th.TearDown()
  1276  	Client := th.Client
  1277  
  1278  	view := &model.ChannelView{
  1279  		ChannelId: th.BasicChannel.Id,
  1280  	}
  1281  
  1282  	viewResp, resp := Client.ViewChannel(th.BasicUser.Id, view)
  1283  	CheckNoError(t, resp)
  1284  
  1285  	if viewResp.Status != "OK" {
  1286  		t.Fatal("should have passed")
  1287  	}
  1288  
  1289  	channel, _ := th.App.GetChannel(th.BasicChannel.Id)
  1290  
  1291  	if lastViewedAt := viewResp.LastViewedAtTimes[channel.Id]; lastViewedAt != channel.LastPostAt {
  1292  		t.Fatal("LastPostAt does not match returned LastViewedAt time")
  1293  	}
  1294  
  1295  	view.PrevChannelId = th.BasicChannel.Id
  1296  	_, resp = Client.ViewChannel(th.BasicUser.Id, view)
  1297  	CheckNoError(t, resp)
  1298  
  1299  	view.PrevChannelId = ""
  1300  	_, resp = Client.ViewChannel(th.BasicUser.Id, view)
  1301  	CheckNoError(t, resp)
  1302  
  1303  	view.PrevChannelId = "junk"
  1304  	_, resp = Client.ViewChannel(th.BasicUser.Id, view)
  1305  	CheckNoError(t, resp)
  1306  
  1307  	member, resp := Client.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id, "")
  1308  	CheckNoError(t, resp)
  1309  	channel, resp = Client.GetChannel(th.BasicChannel.Id, "")
  1310  	CheckNoError(t, resp)
  1311  
  1312  	if member.MsgCount != channel.TotalMsgCount {
  1313  		t.Fatal("should match message counts")
  1314  	}
  1315  
  1316  	if member.MentionCount != 0 {
  1317  		t.Fatal("should have no mentions")
  1318  	}
  1319  
  1320  	_, resp = Client.ViewChannel("junk", view)
  1321  	CheckBadRequestStatus(t, resp)
  1322  
  1323  	_, resp = Client.ViewChannel(th.BasicUser2.Id, view)
  1324  	CheckForbiddenStatus(t, resp)
  1325  
  1326  	if r, err := Client.DoApiPost(fmt.Sprintf("/channels/members/%v/view", th.BasicUser.Id), "garbage"); err == nil {
  1327  		t.Fatal("should have errored")
  1328  	} else {
  1329  		if r.StatusCode != http.StatusBadRequest {
  1330  			t.Log("actual: " + strconv.Itoa(r.StatusCode))
  1331  			t.Log("expected: " + strconv.Itoa(http.StatusBadRequest))
  1332  			t.Fatal("wrong status code")
  1333  		}
  1334  	}
  1335  
  1336  	Client.Logout()
  1337  	_, resp = Client.ViewChannel(th.BasicUser.Id, view)
  1338  	CheckUnauthorizedStatus(t, resp)
  1339  
  1340  	_, resp = th.SystemAdminClient.ViewChannel(th.BasicUser.Id, view)
  1341  	CheckNoError(t, resp)
  1342  }
  1343  
  1344  func TestGetChannelUnread(t *testing.T) {
  1345  	th := Setup().InitBasic().InitSystemAdmin()
  1346  	defer th.TearDown()
  1347  	Client := th.Client
  1348  	user := th.BasicUser
  1349  	channel := th.BasicChannel
  1350  
  1351  	channelUnread, resp := Client.GetChannelUnread(channel.Id, user.Id)
  1352  	CheckNoError(t, resp)
  1353  	if channelUnread.TeamId != th.BasicTeam.Id {
  1354  		t.Fatal("wrong team id returned for a regular user call")
  1355  	} else if channelUnread.ChannelId != channel.Id {
  1356  		t.Fatal("wrong team id returned for a regular user call")
  1357  	}
  1358  
  1359  	_, resp = Client.GetChannelUnread("junk", user.Id)
  1360  	CheckBadRequestStatus(t, resp)
  1361  
  1362  	_, resp = Client.GetChannelUnread(channel.Id, "junk")
  1363  	CheckBadRequestStatus(t, resp)
  1364  
  1365  	_, resp = Client.GetChannelUnread(channel.Id, model.NewId())
  1366  	CheckForbiddenStatus(t, resp)
  1367  
  1368  	_, resp = Client.GetChannelUnread(model.NewId(), user.Id)
  1369  	CheckForbiddenStatus(t, resp)
  1370  
  1371  	newUser := th.CreateUser()
  1372  	Client.Login(newUser.Email, newUser.Password)
  1373  	_, resp = Client.GetChannelUnread(th.BasicChannel.Id, user.Id)
  1374  	CheckForbiddenStatus(t, resp)
  1375  
  1376  	Client.Logout()
  1377  
  1378  	_, resp = th.SystemAdminClient.GetChannelUnread(channel.Id, user.Id)
  1379  	CheckNoError(t, resp)
  1380  
  1381  	_, resp = th.SystemAdminClient.GetChannelUnread(model.NewId(), user.Id)
  1382  	CheckForbiddenStatus(t, resp)
  1383  
  1384  	_, resp = th.SystemAdminClient.GetChannelUnread(channel.Id, model.NewId())
  1385  	CheckNotFoundStatus(t, resp)
  1386  }
  1387  
  1388  func TestGetChannelStats(t *testing.T) {
  1389  	th := Setup().InitBasic().InitSystemAdmin()
  1390  	defer th.TearDown()
  1391  	Client := th.Client
  1392  	channel := th.CreatePrivateChannel()
  1393  
  1394  	stats, resp := Client.GetChannelStats(channel.Id, "")
  1395  	CheckNoError(t, resp)
  1396  
  1397  	if stats.ChannelId != channel.Id {
  1398  		t.Fatal("couldnt't get extra info")
  1399  	} else if stats.MemberCount != 1 {
  1400  		t.Fatal("got incorrect member count")
  1401  	}
  1402  
  1403  	_, resp = Client.GetChannelStats("junk", "")
  1404  	CheckBadRequestStatus(t, resp)
  1405  
  1406  	_, resp = Client.GetChannelStats(model.NewId(), "")
  1407  	CheckForbiddenStatus(t, resp)
  1408  
  1409  	Client.Logout()
  1410  	_, resp = Client.GetChannelStats(channel.Id, "")
  1411  	CheckUnauthorizedStatus(t, resp)
  1412  
  1413  	th.LoginBasic2()
  1414  
  1415  	_, resp = Client.GetChannelStats(channel.Id, "")
  1416  	CheckForbiddenStatus(t, resp)
  1417  
  1418  	_, resp = th.SystemAdminClient.GetChannelStats(channel.Id, "")
  1419  	CheckNoError(t, resp)
  1420  }
  1421  
  1422  func TestGetPinnedPosts(t *testing.T) {
  1423  	th := Setup().InitBasic().InitSystemAdmin()
  1424  	defer th.TearDown()
  1425  	Client := th.Client
  1426  	channel := th.BasicChannel
  1427  
  1428  	posts, resp := Client.GetPinnedPosts(channel.Id, "")
  1429  	CheckNoError(t, resp)
  1430  	if len(posts.Posts) != 0 {
  1431  		t.Fatal("should not have gotten a pinned post")
  1432  	}
  1433  
  1434  	pinnedPost := th.CreatePinnedPost()
  1435  	posts, resp = Client.GetPinnedPosts(channel.Id, "")
  1436  	CheckNoError(t, resp)
  1437  	if len(posts.Posts) != 1 {
  1438  		t.Fatal("should have returned 1 pinned post")
  1439  	}
  1440  	if _, ok := posts.Posts[pinnedPost.Id]; !ok {
  1441  		t.Fatal("missing pinned post")
  1442  	}
  1443  
  1444  	posts, resp = Client.GetPinnedPosts(channel.Id, resp.Etag)
  1445  	CheckEtag(t, posts, resp)
  1446  
  1447  	_, resp = Client.GetPinnedPosts(GenerateTestId(), "")
  1448  	CheckForbiddenStatus(t, resp)
  1449  
  1450  	_, resp = Client.GetPinnedPosts("junk", "")
  1451  	CheckBadRequestStatus(t, resp)
  1452  
  1453  	Client.Logout()
  1454  	_, resp = Client.GetPinnedPosts(channel.Id, "")
  1455  	CheckUnauthorizedStatus(t, resp)
  1456  
  1457  	_, resp = th.SystemAdminClient.GetPinnedPosts(channel.Id, "")
  1458  	CheckNoError(t, resp)
  1459  }
  1460  
  1461  func TestUpdateChannelRoles(t *testing.T) {
  1462  	th := Setup().InitBasic().InitSystemAdmin()
  1463  	defer th.TearDown()
  1464  	Client := th.Client
  1465  
  1466  	const CHANNEL_ADMIN = "channel_user channel_admin"
  1467  	const CHANNEL_MEMBER = "channel_user"
  1468  
  1469  	// User 1 creates a channel, making them channel admin by default.
  1470  	channel := th.CreatePublicChannel()
  1471  
  1472  	// Adds User 2 to the channel, making them a channel member by default.
  1473  	th.App.AddUserToChannel(th.BasicUser2, channel)
  1474  
  1475  	// User 1 promotes User 2
  1476  	pass, resp := Client.UpdateChannelRoles(channel.Id, th.BasicUser2.Id, CHANNEL_ADMIN)
  1477  	CheckNoError(t, resp)
  1478  
  1479  	if !pass {
  1480  		t.Fatal("should have passed")
  1481  	}
  1482  
  1483  	member, resp := Client.GetChannelMember(channel.Id, th.BasicUser2.Id, "")
  1484  	CheckNoError(t, resp)
  1485  
  1486  	if member.Roles != CHANNEL_ADMIN {
  1487  		t.Fatal("roles don't match")
  1488  	}
  1489  
  1490  	// User 1 demotes User 2
  1491  	_, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser2.Id, CHANNEL_MEMBER)
  1492  	CheckNoError(t, resp)
  1493  
  1494  	th.LoginBasic2()
  1495  
  1496  	// User 2 cannot demote User 1
  1497  	_, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_MEMBER)
  1498  	CheckForbiddenStatus(t, resp)
  1499  
  1500  	// User 2 cannot promote self
  1501  	_, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser2.Id, CHANNEL_ADMIN)
  1502  	CheckForbiddenStatus(t, resp)
  1503  
  1504  	th.LoginBasic()
  1505  
  1506  	// User 1 demotes self
  1507  	_, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_MEMBER)
  1508  	CheckNoError(t, resp)
  1509  
  1510  	// System Admin promotes User 1
  1511  	_, resp = th.SystemAdminClient.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_ADMIN)
  1512  	CheckNoError(t, resp)
  1513  
  1514  	// System Admin demotes User 1
  1515  	_, resp = th.SystemAdminClient.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_MEMBER)
  1516  	CheckNoError(t, resp)
  1517  
  1518  	// System Admin promotes User 1
  1519  	pass, resp = th.SystemAdminClient.UpdateChannelRoles(channel.Id, th.BasicUser.Id, CHANNEL_ADMIN)
  1520  	CheckNoError(t, resp)
  1521  
  1522  	th.LoginBasic()
  1523  
  1524  	_, resp = Client.UpdateChannelRoles(channel.Id, th.BasicUser.Id, "junk")
  1525  	CheckBadRequestStatus(t, resp)
  1526  
  1527  	_, resp = Client.UpdateChannelRoles(channel.Id, "junk", CHANNEL_MEMBER)
  1528  	CheckBadRequestStatus(t, resp)
  1529  
  1530  	_, resp = Client.UpdateChannelRoles("junk", th.BasicUser.Id, CHANNEL_MEMBER)
  1531  	CheckBadRequestStatus(t, resp)
  1532  
  1533  	_, resp = Client.UpdateChannelRoles(channel.Id, model.NewId(), CHANNEL_MEMBER)
  1534  	CheckNotFoundStatus(t, resp)
  1535  
  1536  	_, resp = Client.UpdateChannelRoles(model.NewId(), th.BasicUser.Id, CHANNEL_MEMBER)
  1537  	CheckForbiddenStatus(t, resp)
  1538  }
  1539  
  1540  func TestUpdateChannelNotifyProps(t *testing.T) {
  1541  	th := Setup().InitBasic().InitSystemAdmin()
  1542  	defer th.TearDown()
  1543  	Client := th.Client
  1544  
  1545  	props := map[string]string{}
  1546  	props[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_MENTION
  1547  	props[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_MENTION
  1548  
  1549  	pass, resp := Client.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, props)
  1550  	CheckNoError(t, resp)
  1551  
  1552  	if !pass {
  1553  		t.Fatal("should have passed")
  1554  	}
  1555  
  1556  	member, err := th.App.GetChannelMember(th.BasicChannel.Id, th.BasicUser.Id)
  1557  	if err != nil {
  1558  		t.Fatal(err)
  1559  	}
  1560  
  1561  	if member.NotifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION {
  1562  		t.Fatal("bad update")
  1563  	} else if member.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_MENTION {
  1564  		t.Fatal("bad update")
  1565  	}
  1566  
  1567  	_, resp = Client.UpdateChannelNotifyProps("junk", th.BasicUser.Id, props)
  1568  	CheckBadRequestStatus(t, resp)
  1569  
  1570  	_, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, "junk", props)
  1571  	CheckBadRequestStatus(t, resp)
  1572  
  1573  	_, resp = Client.UpdateChannelNotifyProps(model.NewId(), th.BasicUser.Id, props)
  1574  	CheckNotFoundStatus(t, resp)
  1575  
  1576  	_, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, model.NewId(), props)
  1577  	CheckForbiddenStatus(t, resp)
  1578  
  1579  	_, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, map[string]string{})
  1580  	CheckNoError(t, resp)
  1581  
  1582  	Client.Logout()
  1583  	_, resp = Client.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, props)
  1584  	CheckUnauthorizedStatus(t, resp)
  1585  
  1586  	_, resp = th.SystemAdminClient.UpdateChannelNotifyProps(th.BasicChannel.Id, th.BasicUser.Id, props)
  1587  	CheckNoError(t, resp)
  1588  }
  1589  
  1590  func TestAddChannelMember(t *testing.T) {
  1591  	th := Setup().InitBasic().InitSystemAdmin()
  1592  	defer th.TearDown()
  1593  	Client := th.Client
  1594  	user := th.BasicUser
  1595  	user2 := th.BasicUser2
  1596  	team := th.BasicTeam
  1597  	publicChannel := th.CreatePublicChannel()
  1598  	privateChannel := th.CreatePrivateChannel()
  1599  
  1600  	user3 := th.CreateUserWithClient(th.SystemAdminClient)
  1601  	_, resp := th.SystemAdminClient.AddTeamMember(team.Id, user3.Id)
  1602  	CheckNoError(t, resp)
  1603  
  1604  	cm, resp := Client.AddChannelMember(publicChannel.Id, user2.Id)
  1605  	CheckNoError(t, resp)
  1606  	CheckCreatedStatus(t, resp)
  1607  
  1608  	if cm.ChannelId != publicChannel.Id {
  1609  		t.Fatal("should have returned exact channel")
  1610  	}
  1611  
  1612  	if cm.UserId != user2.Id {
  1613  		t.Fatal("should have returned exact user added to public channel")
  1614  	}
  1615  
  1616  	cm, resp = Client.AddChannelMember(privateChannel.Id, user2.Id)
  1617  	CheckNoError(t, resp)
  1618  
  1619  	if cm.ChannelId != privateChannel.Id {
  1620  		t.Fatal("should have returned exact channel")
  1621  	}
  1622  
  1623  	if cm.UserId != user2.Id {
  1624  		t.Fatal("should have returned exact user added to private channel")
  1625  	}
  1626  
  1627  	post := &model.Post{ChannelId: publicChannel.Id, Message: "a" + GenerateTestId() + "a"}
  1628  	rpost, err := Client.CreatePost(post)
  1629  	if err == nil {
  1630  		t.Fatal("should have created a post")
  1631  	}
  1632  
  1633  	Client.RemoveUserFromChannel(publicChannel.Id, user.Id)
  1634  	_, resp = Client.AddChannelMemberWithRootId(publicChannel.Id, user.Id, rpost.Id)
  1635  	CheckNoError(t, resp)
  1636  	CheckCreatedStatus(t, resp)
  1637  
  1638  	Client.RemoveUserFromChannel(publicChannel.Id, user.Id)
  1639  	_, resp = Client.AddChannelMemberWithRootId(publicChannel.Id, user.Id, "junk")
  1640  	CheckBadRequestStatus(t, resp)
  1641  
  1642  	_, resp = Client.AddChannelMemberWithRootId(publicChannel.Id, user.Id, GenerateTestId())
  1643  	CheckNotFoundStatus(t, resp)
  1644  
  1645  	Client.RemoveUserFromChannel(publicChannel.Id, user.Id)
  1646  	_, resp = Client.AddChannelMember(publicChannel.Id, user.Id)
  1647  	CheckNoError(t, resp)
  1648  
  1649  	cm, resp = Client.AddChannelMember(publicChannel.Id, "junk")
  1650  	CheckBadRequestStatus(t, resp)
  1651  
  1652  	if cm != nil {
  1653  		t.Fatal("should return nothing")
  1654  	}
  1655  
  1656  	_, resp = Client.AddChannelMember(publicChannel.Id, GenerateTestId())
  1657  	CheckNotFoundStatus(t, resp)
  1658  
  1659  	_, resp = Client.AddChannelMember("junk", user2.Id)
  1660  	CheckBadRequestStatus(t, resp)
  1661  
  1662  	_, resp = Client.AddChannelMember(GenerateTestId(), user2.Id)
  1663  	CheckNotFoundStatus(t, resp)
  1664  
  1665  	otherUser := th.CreateUser()
  1666  	otherChannel := th.CreatePublicChannel()
  1667  	Client.Logout()
  1668  	Client.Login(user2.Id, user2.Password)
  1669  
  1670  	_, resp = Client.AddChannelMember(publicChannel.Id, otherUser.Id)
  1671  	CheckUnauthorizedStatus(t, resp)
  1672  
  1673  	_, resp = Client.AddChannelMember(privateChannel.Id, otherUser.Id)
  1674  	CheckUnauthorizedStatus(t, resp)
  1675  
  1676  	_, resp = Client.AddChannelMember(otherChannel.Id, otherUser.Id)
  1677  	CheckUnauthorizedStatus(t, resp)
  1678  
  1679  	Client.Logout()
  1680  	Client.Login(user.Id, user.Password)
  1681  
  1682  	// should fail adding user who is not a member of the team
  1683  	_, resp = Client.AddChannelMember(otherChannel.Id, otherUser.Id)
  1684  	CheckUnauthorizedStatus(t, resp)
  1685  
  1686  	Client.DeleteChannel(otherChannel.Id)
  1687  
  1688  	// should fail adding user to a deleted channel
  1689  	_, resp = Client.AddChannelMember(otherChannel.Id, user2.Id)
  1690  	CheckUnauthorizedStatus(t, resp)
  1691  
  1692  	Client.Logout()
  1693  	_, resp = Client.AddChannelMember(publicChannel.Id, user2.Id)
  1694  	CheckUnauthorizedStatus(t, resp)
  1695  
  1696  	_, resp = Client.AddChannelMember(privateChannel.Id, user2.Id)
  1697  	CheckUnauthorizedStatus(t, resp)
  1698  
  1699  	_, resp = th.SystemAdminClient.AddChannelMember(publicChannel.Id, user2.Id)
  1700  	CheckNoError(t, resp)
  1701  
  1702  	_, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id)
  1703  	CheckNoError(t, resp)
  1704  
  1705  	// Check the appropriate permissions are enforced.
  1706  	defaultRolePermissions := th.SaveDefaultRolePermissions()
  1707  	defer func() {
  1708  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
  1709  	}()
  1710  
  1711  	th.AddPermissionToRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_USER_ROLE_ID)
  1712  
  1713  	// Check that a regular channel user can add other users.
  1714  	Client.Login(user2.Username, user2.Password)
  1715  	privateChannel = th.CreatePrivateChannel()
  1716  	_, resp = Client.AddChannelMember(privateChannel.Id, user.Id)
  1717  	CheckNoError(t, resp)
  1718  	Client.Logout()
  1719  
  1720  	Client.Login(user.Username, user.Password)
  1721  	_, resp = Client.AddChannelMember(privateChannel.Id, user3.Id)
  1722  	CheckNoError(t, resp)
  1723  	Client.Logout()
  1724  
  1725  	// Restrict the permission for adding users to Channel Admins
  1726  	th.AddPermissionToRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_ADMIN_ROLE_ID)
  1727  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_USER_ROLE_ID)
  1728  
  1729  	Client.Login(user2.Username, user2.Password)
  1730  	privateChannel = th.CreatePrivateChannel()
  1731  	_, resp = Client.AddChannelMember(privateChannel.Id, user.Id)
  1732  	CheckNoError(t, resp)
  1733  	Client.Logout()
  1734  
  1735  	Client.Login(user.Username, user.Password)
  1736  	_, resp = Client.AddChannelMember(privateChannel.Id, user3.Id)
  1737  	CheckForbiddenStatus(t, resp)
  1738  	Client.Logout()
  1739  
  1740  	th.MakeUserChannelAdmin(user, privateChannel)
  1741  	th.App.InvalidateAllCaches()
  1742  
  1743  	Client.Login(user.Username, user.Password)
  1744  	_, resp = Client.AddChannelMember(privateChannel.Id, user3.Id)
  1745  	CheckNoError(t, resp)
  1746  	Client.Logout()
  1747  }
  1748  
  1749  func TestRemoveChannelMember(t *testing.T) {
  1750  	th := Setup().InitBasic().InitSystemAdmin()
  1751  	user1 := th.BasicUser
  1752  	user2 := th.BasicUser2
  1753  	team := th.BasicTeam
  1754  	defer th.TearDown()
  1755  	Client := th.Client
  1756  
  1757  	pass, resp := Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser2.Id)
  1758  	CheckNoError(t, resp)
  1759  
  1760  	if !pass {
  1761  		t.Fatal("should have passed")
  1762  	}
  1763  
  1764  	_, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser2.Id)
  1765  	CheckNoError(t, resp)
  1766  
  1767  	_, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, "junk")
  1768  	CheckBadRequestStatus(t, resp)
  1769  
  1770  	_, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, model.NewId())
  1771  	CheckNotFoundStatus(t, resp)
  1772  
  1773  	_, resp = Client.RemoveUserFromChannel(model.NewId(), th.BasicUser2.Id)
  1774  	CheckNotFoundStatus(t, resp)
  1775  
  1776  	th.LoginBasic2()
  1777  	_, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id)
  1778  	CheckForbiddenStatus(t, resp)
  1779  
  1780  	th.App.AddUserToChannel(th.BasicUser2, th.BasicChannel)
  1781  	_, resp = Client.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser2.Id)
  1782  	CheckNoError(t, resp)
  1783  
  1784  	_, resp = Client.RemoveUserFromChannel(th.BasicChannel2.Id, th.BasicUser.Id)
  1785  	CheckNoError(t, resp)
  1786  
  1787  	_, resp = th.SystemAdminClient.RemoveUserFromChannel(th.BasicChannel.Id, th.BasicUser.Id)
  1788  	CheckNoError(t, resp)
  1789  
  1790  	th.LoginBasic()
  1791  	private := th.CreatePrivateChannel()
  1792  	th.App.AddUserToChannel(th.BasicUser2, private)
  1793  
  1794  	_, resp = Client.RemoveUserFromChannel(private.Id, th.BasicUser2.Id)
  1795  	CheckNoError(t, resp)
  1796  
  1797  	th.LoginBasic2()
  1798  	_, resp = Client.RemoveUserFromChannel(private.Id, th.BasicUser.Id)
  1799  	CheckForbiddenStatus(t, resp)
  1800  
  1801  	_, resp = th.SystemAdminClient.RemoveUserFromChannel(private.Id, th.BasicUser.Id)
  1802  	CheckNoError(t, resp)
  1803  
  1804  	th.LoginBasic()
  1805  	th.UpdateUserToNonTeamAdmin(user1, team)
  1806  	th.App.InvalidateAllCaches()
  1807  
  1808  	// Check the appropriate permissions are enforced.
  1809  	defaultRolePermissions := th.SaveDefaultRolePermissions()
  1810  	defer func() {
  1811  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
  1812  	}()
  1813  
  1814  	th.AddPermissionToRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_USER_ROLE_ID)
  1815  
  1816  	// Check that a regular channel user can remove other users.
  1817  	privateChannel := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
  1818  	_, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user1.Id)
  1819  	CheckNoError(t, resp)
  1820  	_, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id)
  1821  	CheckNoError(t, resp)
  1822  
  1823  	_, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id)
  1824  	CheckNoError(t, resp)
  1825  
  1826  	// Restrict the permission for adding users to Channel Admins
  1827  	th.AddPermissionToRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_ADMIN_ROLE_ID)
  1828  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS.Id, model.CHANNEL_USER_ROLE_ID)
  1829  
  1830  	privateChannel = th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
  1831  	_, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user1.Id)
  1832  	CheckNoError(t, resp)
  1833  	_, resp = th.SystemAdminClient.AddChannelMember(privateChannel.Id, user2.Id)
  1834  	CheckNoError(t, resp)
  1835  
  1836  	_, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id)
  1837  	CheckForbiddenStatus(t, resp)
  1838  
  1839  	th.MakeUserChannelAdmin(user1, privateChannel)
  1840  	th.App.InvalidateAllCaches()
  1841  
  1842  	_, resp = Client.RemoveUserFromChannel(privateChannel.Id, user2.Id)
  1843  	CheckNoError(t, resp)
  1844  }
  1845  
  1846  func TestAutocompleteChannels(t *testing.T) {
  1847  	th := Setup().InitBasic()
  1848  	defer th.TearDown()
  1849  
  1850  	// A private channel to make sure private channels are not used
  1851  	utils.DisableDebugLogForTest()
  1852  	ptown, _ := th.Client.CreateChannel(&model.Channel{
  1853  		DisplayName: "Town",
  1854  		Name:        "town",
  1855  		Type:        model.CHANNEL_PRIVATE,
  1856  		TeamId:      th.BasicTeam.Id,
  1857  	})
  1858  	utils.EnableDebugLogForTest()
  1859  	defer func() {
  1860  		th.Client.DeleteChannel(ptown.Id)
  1861  	}()
  1862  
  1863  	for _, tc := range []struct {
  1864  		description      string
  1865  		teamId           string
  1866  		fragment         string
  1867  		expectedIncludes []string
  1868  		expectedExcludes []string
  1869  	}{
  1870  		{
  1871  			"Basic town-square",
  1872  			th.BasicTeam.Id,
  1873  			"town",
  1874  			[]string{"town-square"},
  1875  			[]string{"off-topic", "town"},
  1876  		},
  1877  		{
  1878  			"Basic off-topic",
  1879  			th.BasicTeam.Id,
  1880  			"off-to",
  1881  			[]string{"off-topic"},
  1882  			[]string{"town-square", "town"},
  1883  		},
  1884  		{
  1885  			"Basic town square and off topic",
  1886  			th.BasicTeam.Id,
  1887  			"to",
  1888  			[]string{"off-topic", "town-square"},
  1889  			[]string{"town"},
  1890  		},
  1891  	} {
  1892  		if channels, resp := th.Client.AutocompleteChannelsForTeam(tc.teamId, tc.fragment); resp.Error != nil {
  1893  			t.Fatal("Test case " + tc.description + " failed. Err: " + resp.Error.Error())
  1894  		} else {
  1895  			for _, expectedInclude := range tc.expectedIncludes {
  1896  				found := false
  1897  				for _, channel := range *channels {
  1898  					if channel.Name == expectedInclude {
  1899  						found = true
  1900  						break
  1901  					}
  1902  				}
  1903  				if !found {
  1904  					t.Fatal("Test case " + tc.description + " failed. Expected but didn't find channel: " + expectedInclude)
  1905  				}
  1906  			}
  1907  			for _, expectedExclude := range tc.expectedExcludes {
  1908  				for _, channel := range *channels {
  1909  					if channel.Name == expectedExclude {
  1910  						t.Fatal("Test case " + tc.description + " failed. Found channel we didn't want: " + expectedExclude)
  1911  					}
  1912  				}
  1913  			}
  1914  		}
  1915  	}
  1916  }
  1917  
  1918  func TestUpdateChannelScheme(t *testing.T) {
  1919  	th := Setup().InitBasic().InitSystemAdmin()
  1920  	defer th.TearDown()
  1921  
  1922  	th.App.SetLicense(model.NewTestLicense(""))
  1923  
  1924  	th.App.SetPhase2PermissionsMigrationStatus(true)
  1925  
  1926  	team := &model.Team{
  1927  		DisplayName:     "Name",
  1928  		Description:     "Some description",
  1929  		CompanyName:     "Some company name",
  1930  		AllowOpenInvite: false,
  1931  		InviteId:        "inviteid0",
  1932  		Name:            "z-z-" + model.NewId() + "a",
  1933  		Email:           "success+" + model.NewId() + "@simulator.amazonses.com",
  1934  		Type:            model.TEAM_OPEN,
  1935  	}
  1936  	team, _ = th.SystemAdminClient.CreateTeam(team)
  1937  
  1938  	channel := &model.Channel{
  1939  		DisplayName: "Name",
  1940  		Name:        "z-z-" + model.NewId() + "a",
  1941  		Type:        model.CHANNEL_OPEN,
  1942  		TeamId:      team.Id,
  1943  	}
  1944  	channel, _ = th.SystemAdminClient.CreateChannel(channel)
  1945  
  1946  	channelScheme := &model.Scheme{
  1947  		DisplayName: "DisplayName",
  1948  		Name:        model.NewId(),
  1949  		Description: "Some description",
  1950  		Scope:       model.SCHEME_SCOPE_CHANNEL,
  1951  	}
  1952  	channelScheme, _ = th.SystemAdminClient.CreateScheme(channelScheme)
  1953  	teamScheme := &model.Scheme{
  1954  		DisplayName: "DisplayName",
  1955  		Name:        model.NewId(),
  1956  		Description: "Some description",
  1957  		Scope:       model.SCHEME_SCOPE_TEAM,
  1958  	}
  1959  	teamScheme, _ = th.SystemAdminClient.CreateScheme(teamScheme)
  1960  
  1961  	// Test the setup/base case.
  1962  	_, resp := th.SystemAdminClient.UpdateChannelScheme(channel.Id, channelScheme.Id)
  1963  	CheckNoError(t, resp)
  1964  
  1965  	// Test various invalid channel and scheme id combinations.
  1966  	_, resp = th.SystemAdminClient.UpdateChannelScheme(channel.Id, "x")
  1967  	CheckBadRequestStatus(t, resp)
  1968  	_, resp = th.SystemAdminClient.UpdateChannelScheme("x", channelScheme.Id)
  1969  	CheckBadRequestStatus(t, resp)
  1970  	_, resp = th.SystemAdminClient.UpdateChannelScheme("x", "x")
  1971  	CheckBadRequestStatus(t, resp)
  1972  
  1973  	// Test that permissions are required.
  1974  	_, resp = th.Client.UpdateChannelScheme(channel.Id, channelScheme.Id)
  1975  	CheckForbiddenStatus(t, resp)
  1976  
  1977  	// Test that a license is requried.
  1978  	th.App.SetLicense(nil)
  1979  	_, resp = th.SystemAdminClient.UpdateChannelScheme(channel.Id, channelScheme.Id)
  1980  	CheckNotImplementedStatus(t, resp)
  1981  	th.App.SetLicense(model.NewTestLicense(""))
  1982  
  1983  	// Test an invalid scheme scope.
  1984  	_, resp = th.SystemAdminClient.UpdateChannelScheme(channel.Id, teamScheme.Id)
  1985  	fmt.Printf("resp: %+v\n", resp)
  1986  	CheckBadRequestStatus(t, resp)
  1987  
  1988  	// Test that an unauthenticated user gets rejected.
  1989  	th.SystemAdminClient.Logout()
  1990  	_, resp = th.SystemAdminClient.UpdateChannelScheme(channel.Id, channelScheme.Id)
  1991  	CheckUnauthorizedStatus(t, resp)
  1992  }