github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/api/channel_test.go (about)

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