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