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