github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/channel_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  	"github.com/mattermost/mattermost-server/store"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestPermanentDeleteChannel(t *testing.T) {
    16  	th := Setup().InitBasic()
    17  	defer th.TearDown()
    18  
    19  	th.App.UpdateConfig(func(cfg *model.Config) {
    20  		cfg.ServiceSettings.EnableIncomingWebhooks = true
    21  		cfg.ServiceSettings.EnableOutgoingWebhooks = true
    22  	})
    23  
    24  	channel, err := th.App.CreateChannel(&model.Channel{DisplayName: "deletion-test", Name: "deletion-test", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id}, false)
    25  	if err != nil {
    26  		t.Fatal(err.Error())
    27  	}
    28  	defer func() {
    29  		th.App.PermanentDeleteChannel(channel)
    30  	}()
    31  
    32  	incoming, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, channel, &model.IncomingWebhook{ChannelId: channel.Id})
    33  	if err != nil {
    34  		t.Fatal(err.Error())
    35  	}
    36  	defer th.App.DeleteIncomingWebhook(incoming.Id)
    37  
    38  	if incoming, err = th.App.GetIncomingWebhook(incoming.Id); incoming == nil || err != nil {
    39  		t.Fatal("unable to get new incoming webhook")
    40  	}
    41  
    42  	outgoing, err := th.App.CreateOutgoingWebhook(&model.OutgoingWebhook{
    43  		ChannelId:    channel.Id,
    44  		TeamId:       channel.TeamId,
    45  		CreatorId:    th.BasicUser.Id,
    46  		CallbackURLs: []string{"http://foo"},
    47  	})
    48  	if err != nil {
    49  		t.Fatal(err.Error())
    50  	}
    51  	defer th.App.DeleteOutgoingWebhook(outgoing.Id)
    52  
    53  	if outgoing, err = th.App.GetOutgoingWebhook(outgoing.Id); outgoing == nil || err != nil {
    54  		t.Fatal("unable to get new outgoing webhook")
    55  	}
    56  
    57  	if err := th.App.PermanentDeleteChannel(channel); err != nil {
    58  		t.Fatal(err.Error())
    59  	}
    60  
    61  	if incoming, err = th.App.GetIncomingWebhook(incoming.Id); incoming != nil || err == nil {
    62  		t.Error("incoming webhook wasn't deleted")
    63  	}
    64  
    65  	if outgoing, err = th.App.GetOutgoingWebhook(outgoing.Id); outgoing != nil || err == nil {
    66  		t.Error("outgoing webhook wasn't deleted")
    67  	}
    68  }
    69  
    70  func TestMoveChannel(t *testing.T) {
    71  	th := Setup().InitBasic()
    72  	defer th.TearDown()
    73  
    74  	sourceTeam := th.CreateTeam()
    75  	targetTeam := th.CreateTeam()
    76  	channel1 := th.CreateChannel(sourceTeam)
    77  	defer func() {
    78  		th.App.PermanentDeleteChannel(channel1)
    79  		th.App.PermanentDeleteTeam(sourceTeam)
    80  		th.App.PermanentDeleteTeam(targetTeam)
    81  	}()
    82  
    83  	if _, err := th.App.AddUserToTeam(sourceTeam.Id, th.BasicUser.Id, ""); err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	if _, err := th.App.AddUserToTeam(sourceTeam.Id, th.BasicUser2.Id, ""); err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	if _, err := th.App.AddUserToTeam(targetTeam.Id, th.BasicUser.Id, ""); err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	if _, err := th.App.AddUserToChannel(th.BasicUser, channel1); err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	if _, err := th.App.AddUserToChannel(th.BasicUser2, channel1); err != nil {
    98  		t.Fatal(err)
    99  	}
   100  
   101  	if err := th.App.MoveChannel(targetTeam, channel1, th.BasicUser); err == nil {
   102  		t.Fatal("Should have failed due to mismatched members.")
   103  	}
   104  
   105  	if _, err := th.App.AddUserToTeam(targetTeam.Id, th.BasicUser2.Id, ""); err != nil {
   106  		t.Fatal(err)
   107  	}
   108  
   109  	if err := th.App.MoveChannel(targetTeam, channel1, th.BasicUser); err != nil {
   110  		t.Fatal(err)
   111  	}
   112  }
   113  
   114  func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordTownSquare(t *testing.T) {
   115  	th := Setup().InitBasic()
   116  	defer th.TearDown()
   117  
   118  	// figure out the initial number of users in town square
   119  	townSquareChannelId := store.Must(th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "town-square", true)).(*model.Channel).Id
   120  	initialNumTownSquareUsers := len(store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, townSquareChannelId)).([]*model.ChannelMemberHistoryResult))
   121  
   122  	// create a new user that joins the default channels
   123  	user := th.CreateUser()
   124  	th.App.JoinDefaultChannels(th.BasicTeam.Id, user, false, "")
   125  
   126  	// there should be a ChannelMemberHistory record for the user
   127  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, townSquareChannelId)).([]*model.ChannelMemberHistoryResult)
   128  	assert.Len(t, histories, initialNumTownSquareUsers+1)
   129  
   130  	found := false
   131  	for _, history := range histories {
   132  		if user.Id == history.UserId && townSquareChannelId == history.ChannelId {
   133  			found = true
   134  			break
   135  		}
   136  	}
   137  	assert.True(t, found)
   138  }
   139  
   140  func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordOffTopic(t *testing.T) {
   141  	th := Setup().InitBasic()
   142  	defer th.TearDown()
   143  
   144  	// figure out the initial number of users in off-topic
   145  	offTopicChannelId := store.Must(th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "off-topic", true)).(*model.Channel).Id
   146  	initialNumTownSquareUsers := len(store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, offTopicChannelId)).([]*model.ChannelMemberHistoryResult))
   147  
   148  	// create a new user that joins the default channels
   149  	user := th.CreateUser()
   150  	th.App.JoinDefaultChannels(th.BasicTeam.Id, user, false, "")
   151  
   152  	// there should be a ChannelMemberHistory record for the user
   153  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, offTopicChannelId)).([]*model.ChannelMemberHistoryResult)
   154  	assert.Len(t, histories, initialNumTownSquareUsers+1)
   155  
   156  	found := false
   157  	for _, history := range histories {
   158  		if user.Id == history.UserId && offTopicChannelId == history.ChannelId {
   159  			found = true
   160  			break
   161  		}
   162  	}
   163  	assert.True(t, found)
   164  }
   165  
   166  func TestJoinDefaultChannelsExperimentalDefaultChannels(t *testing.T) {
   167  	th := Setup().InitBasic()
   168  	defer th.TearDown()
   169  
   170  	basicChannel2 := th.CreateChannel(th.BasicTeam)
   171  	defaultChannelList := []string{th.BasicChannel.Name, basicChannel2.Name, basicChannel2.Name}
   172  	th.App.Config().TeamSettings.ExperimentalDefaultChannels = defaultChannelList
   173  
   174  	user := th.CreateUser()
   175  	th.App.JoinDefaultChannels(th.BasicTeam.Id, user, false, "")
   176  
   177  	for _, channelName := range defaultChannelList {
   178  		channel, err := th.App.GetChannelByName(channelName, th.BasicTeam.Id, false)
   179  
   180  		if err != nil {
   181  			t.Errorf("Expected nil, got %s", err)
   182  		}
   183  
   184  		member, err := th.App.GetChannelMember(channel.Id, user.Id)
   185  
   186  		if member == nil {
   187  			t.Errorf("Expected member object, got nil")
   188  		}
   189  
   190  		if err != nil {
   191  			t.Errorf("Expected nil object, got %s", err)
   192  		}
   193  	}
   194  }
   195  
   196  func TestCreateChannelPublicCreatesChannelMemberHistoryRecord(t *testing.T) {
   197  	th := Setup().InitBasic()
   198  	defer th.TearDown()
   199  
   200  	// creates a public channel and adds basic user to it
   201  	publicChannel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN)
   202  
   203  	// there should be a ChannelMemberHistory record for the user
   204  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistoryResult)
   205  	assert.Len(t, histories, 1)
   206  	assert.Equal(t, th.BasicUser.Id, histories[0].UserId)
   207  	assert.Equal(t, publicChannel.Id, histories[0].ChannelId)
   208  }
   209  
   210  func TestCreateChannelPrivateCreatesChannelMemberHistoryRecord(t *testing.T) {
   211  	th := Setup().InitBasic()
   212  	defer th.TearDown()
   213  
   214  	// creates a private channel and adds basic user to it
   215  	privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE)
   216  
   217  	// there should be a ChannelMemberHistory record for the user
   218  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, privateChannel.Id)).([]*model.ChannelMemberHistoryResult)
   219  	assert.Len(t, histories, 1)
   220  	assert.Equal(t, th.BasicUser.Id, histories[0].UserId)
   221  	assert.Equal(t, privateChannel.Id, histories[0].ChannelId)
   222  }
   223  
   224  func TestUpdateChannelPrivacy(t *testing.T) {
   225  	th := Setup().InitBasic()
   226  	defer th.TearDown()
   227  
   228  	privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE)
   229  	privateChannel.Type = model.CHANNEL_OPEN
   230  
   231  	if publicChannel, err := th.App.UpdateChannelPrivacy(privateChannel, th.BasicUser); err != nil {
   232  		t.Fatal("Failed to update channel privacy. Error: " + err.Error())
   233  	} else {
   234  		assert.Equal(t, publicChannel.Id, privateChannel.Id)
   235  		assert.Equal(t, publicChannel.Type, model.CHANNEL_OPEN)
   236  	}
   237  }
   238  
   239  func TestCreateGroupChannelCreatesChannelMemberHistoryRecord(t *testing.T) {
   240  	th := Setup().InitBasic()
   241  	defer th.TearDown()
   242  
   243  	user1 := th.CreateUser()
   244  	user2 := th.CreateUser()
   245  
   246  	groupUserIds := make([]string, 0)
   247  	groupUserIds = append(groupUserIds, user1.Id)
   248  	groupUserIds = append(groupUserIds, user2.Id)
   249  	groupUserIds = append(groupUserIds, th.BasicUser.Id)
   250  
   251  	if channel, err := th.App.CreateGroupChannel(groupUserIds, th.BasicUser.Id); err != nil {
   252  		t.Fatal("Failed to create group channel. Error: " + err.Message)
   253  	} else {
   254  		// there should be a ChannelMemberHistory record for each user
   255  		histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult)
   256  		assert.Len(t, histories, 3)
   257  
   258  		channelMemberHistoryUserIds := make([]string, 0)
   259  		for _, history := range histories {
   260  			assert.Equal(t, channel.Id, history.ChannelId)
   261  			channelMemberHistoryUserIds = append(channelMemberHistoryUserIds, history.UserId)
   262  		}
   263  		assert.Equal(t, groupUserIds, channelMemberHistoryUserIds)
   264  	}
   265  }
   266  
   267  func TestCreateDirectChannelCreatesChannelMemberHistoryRecord(t *testing.T) {
   268  	th := Setup().InitBasic()
   269  	defer th.TearDown()
   270  
   271  	user1 := th.CreateUser()
   272  	user2 := th.CreateUser()
   273  
   274  	if channel, err := th.App.CreateDirectChannel(user1.Id, user2.Id); err != nil {
   275  		t.Fatal("Failed to create direct channel. Error: " + err.Message)
   276  	} else {
   277  		// there should be a ChannelMemberHistory record for both users
   278  		histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult)
   279  		assert.Len(t, histories, 2)
   280  
   281  		historyId0 := histories[0].UserId
   282  		historyId1 := histories[1].UserId
   283  		switch historyId0 {
   284  		case user1.Id:
   285  			assert.Equal(t, user2.Id, historyId1)
   286  		case user2.Id:
   287  			assert.Equal(t, user1.Id, historyId1)
   288  		default:
   289  			t.Fatal("Unexpected user id " + historyId0 + " in ChannelMemberHistory table")
   290  		}
   291  	}
   292  }
   293  
   294  func TestGetDirectChannelCreatesChannelMemberHistoryRecord(t *testing.T) {
   295  	th := Setup().InitBasic()
   296  	defer th.TearDown()
   297  
   298  	user1 := th.CreateUser()
   299  	user2 := th.CreateUser()
   300  
   301  	// this function call implicitly creates a direct channel between the two users if one doesn't already exist
   302  	if channel, err := th.App.GetDirectChannel(user1.Id, user2.Id); err != nil {
   303  		t.Fatal("Failed to create direct channel. Error: " + err.Message)
   304  	} else {
   305  		// there should be a ChannelMemberHistory record for both users
   306  		histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult)
   307  		assert.Len(t, histories, 2)
   308  
   309  		historyId0 := histories[0].UserId
   310  		historyId1 := histories[1].UserId
   311  		switch historyId0 {
   312  		case user1.Id:
   313  			assert.Equal(t, user2.Id, historyId1)
   314  		case user2.Id:
   315  			assert.Equal(t, user1.Id, historyId1)
   316  		default:
   317  			t.Fatal("Unexpected user id " + historyId0 + " in ChannelMemberHistory table")
   318  		}
   319  	}
   320  }
   321  
   322  func TestAddUserToChannelCreatesChannelMemberHistoryRecord(t *testing.T) {
   323  	th := Setup().InitBasic()
   324  	defer th.TearDown()
   325  
   326  	// create a user and add it to a channel
   327  	user := th.CreateUser()
   328  	if _, err := th.App.AddTeamMember(th.BasicTeam.Id, user.Id); err != nil {
   329  		t.Fatal("Failed to add user to team. Error: " + err.Message)
   330  	}
   331  
   332  	groupUserIds := make([]string, 0)
   333  	groupUserIds = append(groupUserIds, th.BasicUser.Id)
   334  	groupUserIds = append(groupUserIds, user.Id)
   335  
   336  	channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN)
   337  	if _, err := th.App.AddUserToChannel(user, channel); err != nil {
   338  		t.Fatal("Failed to add user to channel. Error: " + err.Message)
   339  	}
   340  
   341  	// there should be a ChannelMemberHistory record for the user
   342  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult)
   343  	assert.Len(t, histories, 2)
   344  	channelMemberHistoryUserIds := make([]string, 0)
   345  	for _, history := range histories {
   346  		assert.Equal(t, channel.Id, history.ChannelId)
   347  		channelMemberHistoryUserIds = append(channelMemberHistoryUserIds, history.UserId)
   348  	}
   349  	assert.Equal(t, groupUserIds, channelMemberHistoryUserIds)
   350  }
   351  
   352  func TestRemoveUserFromChannelUpdatesChannelMemberHistoryRecord(t *testing.T) {
   353  	th := Setup().InitBasic()
   354  	defer th.TearDown()
   355  
   356  	// a user creates a channel
   357  	publicChannel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN)
   358  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistoryResult)
   359  	assert.Len(t, histories, 1)
   360  	assert.Equal(t, th.BasicUser.Id, histories[0].UserId)
   361  	assert.Equal(t, publicChannel.Id, histories[0].ChannelId)
   362  	assert.Nil(t, histories[0].LeaveTime)
   363  
   364  	// the user leaves that channel
   365  	if err := th.App.LeaveChannel(publicChannel.Id, th.BasicUser.Id); err != nil {
   366  		t.Fatal("Failed to remove user from channel. Error: " + err.Message)
   367  	}
   368  	histories = store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, publicChannel.Id)).([]*model.ChannelMemberHistoryResult)
   369  	assert.Len(t, histories, 1)
   370  	assert.Equal(t, th.BasicUser.Id, histories[0].UserId)
   371  	assert.Equal(t, publicChannel.Id, histories[0].ChannelId)
   372  	assert.NotNil(t, histories[0].LeaveTime)
   373  }
   374  
   375  func TestAddChannelMemberNoUserRequestor(t *testing.T) {
   376  	th := Setup().InitBasic()
   377  	defer th.TearDown()
   378  
   379  	// create a user and add it to a channel
   380  	user := th.CreateUser()
   381  	if _, err := th.App.AddTeamMember(th.BasicTeam.Id, user.Id); err != nil {
   382  		t.Fatal("Failed to add user to team. Error: " + err.Message)
   383  	}
   384  
   385  	groupUserIds := make([]string, 0)
   386  	groupUserIds = append(groupUserIds, th.BasicUser.Id)
   387  	groupUserIds = append(groupUserIds, user.Id)
   388  
   389  	channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN)
   390  	userRequestorId := ""
   391  	postRootId := ""
   392  	if _, err := th.App.AddChannelMember(user.Id, channel, userRequestorId, postRootId, false); err != nil {
   393  		t.Fatal("Failed to add user to channel. Error: " + err.Message)
   394  	}
   395  
   396  	// there should be a ChannelMemberHistory record for the user
   397  	histories := store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, channel.Id)).([]*model.ChannelMemberHistoryResult)
   398  	assert.Len(t, histories, 2)
   399  	channelMemberHistoryUserIds := make([]string, 0)
   400  	for _, history := range histories {
   401  		assert.Equal(t, channel.Id, history.ChannelId)
   402  		channelMemberHistoryUserIds = append(channelMemberHistoryUserIds, history.UserId)
   403  	}
   404  	assert.Equal(t, groupUserIds, channelMemberHistoryUserIds)
   405  
   406  	postList := store.Must(th.App.Srv.Store.Post().GetPosts(channel.Id, 0, 1, false)).(*model.PostList)
   407  	if assert.Len(t, postList.Order, 1) {
   408  		post := postList.Posts[postList.Order[0]]
   409  
   410  		assert.Equal(t, model.POST_JOIN_CHANNEL, post.Type)
   411  		assert.Equal(t, user.Id, post.UserId)
   412  		assert.Equal(t, user.Username, post.Props["username"])
   413  	}
   414  }
   415  
   416  func TestAppUpdateChannelScheme(t *testing.T) {
   417  	th := Setup().InitBasic()
   418  	defer th.TearDown()
   419  
   420  	channel := th.BasicChannel
   421  	mockID := model.NewString("x")
   422  	channel.SchemeId = mockID
   423  
   424  	updatedChannel, err := th.App.UpdateChannelScheme(channel)
   425  	if err != nil {
   426  		t.Fatal(err)
   427  	}
   428  
   429  	if updatedChannel.SchemeId != mockID {
   430  		t.Fatal("Wrong Channel SchemeId")
   431  	}
   432  }
   433  
   434  func TestFillInChannelProps(t *testing.T) {
   435  	th := Setup().InitBasic()
   436  	defer th.TearDown()
   437  
   438  	channelPublic1, err := th.App.CreateChannel(&model.Channel{DisplayName: "Public 1", Name: "public1", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id}, false)
   439  	require.Nil(t, err)
   440  	defer th.App.PermanentDeleteChannel(channelPublic1)
   441  
   442  	channelPublic2, err := th.App.CreateChannel(&model.Channel{DisplayName: "Public 2", Name: "public2", Type: model.CHANNEL_OPEN, TeamId: th.BasicTeam.Id}, false)
   443  	require.Nil(t, err)
   444  	defer th.App.PermanentDeleteChannel(channelPublic2)
   445  
   446  	channelPrivate, err := th.App.CreateChannel(&model.Channel{DisplayName: "Private", Name: "private", Type: model.CHANNEL_PRIVATE, TeamId: th.BasicTeam.Id}, false)
   447  	require.Nil(t, err)
   448  	defer th.App.PermanentDeleteChannel(channelPrivate)
   449  
   450  	otherTeamId := model.NewId()
   451  	otherTeam := &model.Team{
   452  		DisplayName: "dn_" + otherTeamId,
   453  		Name:        "name" + otherTeamId,
   454  		Email:       "success+" + otherTeamId + "@simulator.amazonses.com",
   455  		Type:        model.TEAM_OPEN,
   456  	}
   457  	otherTeam, err = th.App.CreateTeam(otherTeam)
   458  	require.Nil(t, err)
   459  	defer th.App.PermanentDeleteTeam(otherTeam)
   460  
   461  	channelOtherTeam, err := th.App.CreateChannel(&model.Channel{DisplayName: "Other Team Channel", Name: "other-team", Type: model.CHANNEL_OPEN, TeamId: otherTeam.Id}, false)
   462  	require.Nil(t, err)
   463  	defer th.App.PermanentDeleteChannel(channelOtherTeam)
   464  
   465  	// Note that purpose is intentionally plaintext below.
   466  
   467  	t.Run("single channels", func(t *testing.T) {
   468  		testCases := []struct {
   469  			Description          string
   470  			Channel              *model.Channel
   471  			ExpectedChannelProps map[string]interface{}
   472  		}{
   473  			{
   474  				"channel on basic team without references",
   475  				&model.Channel{
   476  					TeamId:  th.BasicTeam.Id,
   477  					Header:  "No references",
   478  					Purpose: "No references",
   479  				},
   480  				nil,
   481  			},
   482  			{
   483  				"channel on basic team",
   484  				&model.Channel{
   485  					TeamId:  th.BasicTeam.Id,
   486  					Header:  "~public1, ~private, ~other-team",
   487  					Purpose: "~public2, ~private, ~other-team",
   488  				},
   489  				map[string]interface{}{
   490  					"channel_mentions": map[string]interface{}{
   491  						"public1": map[string]interface{}{
   492  							"display_name": "Public 1",
   493  						},
   494  					},
   495  				},
   496  			},
   497  			{
   498  				"channel on other team",
   499  				&model.Channel{
   500  					TeamId:  otherTeam.Id,
   501  					Header:  "~public1, ~private, ~other-team",
   502  					Purpose: "~public2, ~private, ~other-team",
   503  				},
   504  				map[string]interface{}{
   505  					"channel_mentions": map[string]interface{}{
   506  						"other-team": map[string]interface{}{
   507  							"display_name": "Other Team Channel",
   508  						},
   509  					},
   510  				},
   511  			},
   512  		}
   513  
   514  		for _, testCase := range testCases {
   515  			t.Run(testCase.Description, func(t *testing.T) {
   516  				err = th.App.FillInChannelProps(testCase.Channel)
   517  				require.Nil(t, err)
   518  
   519  				assert.Equal(t, testCase.ExpectedChannelProps, testCase.Channel.Props)
   520  			})
   521  		}
   522  	})
   523  
   524  	t.Run("multiple channels", func(t *testing.T) {
   525  		testCases := []struct {
   526  			Description          string
   527  			Channels             *model.ChannelList
   528  			ExpectedChannelProps map[string]interface{}
   529  		}{
   530  			{
   531  				"single channel on basic team",
   532  				&model.ChannelList{
   533  					{
   534  						Name:    "test",
   535  						TeamId:  th.BasicTeam.Id,
   536  						Header:  "~public1, ~private, ~other-team",
   537  						Purpose: "~public2, ~private, ~other-team",
   538  					},
   539  				},
   540  				map[string]interface{}{
   541  					"test": map[string]interface{}{
   542  						"channel_mentions": map[string]interface{}{
   543  							"public1": map[string]interface{}{
   544  								"display_name": "Public 1",
   545  							},
   546  						},
   547  					},
   548  				},
   549  			},
   550  			{
   551  				"multiple channels on basic team",
   552  				&model.ChannelList{
   553  					{
   554  						Name:    "test",
   555  						TeamId:  th.BasicTeam.Id,
   556  						Header:  "~public1, ~private, ~other-team",
   557  						Purpose: "~public2, ~private, ~other-team",
   558  					},
   559  					{
   560  						Name:    "test2",
   561  						TeamId:  th.BasicTeam.Id,
   562  						Header:  "~private, ~other-team",
   563  						Purpose: "~public2, ~private, ~other-team",
   564  					},
   565  					{
   566  						Name:    "test3",
   567  						TeamId:  th.BasicTeam.Id,
   568  						Header:  "No references",
   569  						Purpose: "No references",
   570  					},
   571  				},
   572  				map[string]interface{}{
   573  					"test": map[string]interface{}{
   574  						"channel_mentions": map[string]interface{}{
   575  							"public1": map[string]interface{}{
   576  								"display_name": "Public 1",
   577  							},
   578  						},
   579  					},
   580  					"test2": map[string]interface{}(nil),
   581  					"test3": map[string]interface{}(nil),
   582  				},
   583  			},
   584  			{
   585  				"multiple channels across teams",
   586  				&model.ChannelList{
   587  					{
   588  						Name:    "test",
   589  						TeamId:  th.BasicTeam.Id,
   590  						Header:  "~public1, ~private, ~other-team",
   591  						Purpose: "~public2, ~private, ~other-team",
   592  					},
   593  					{
   594  						Name:    "test2",
   595  						TeamId:  otherTeam.Id,
   596  						Header:  "~private, ~other-team",
   597  						Purpose: "~public2, ~private, ~other-team",
   598  					},
   599  					{
   600  						Name:    "test3",
   601  						TeamId:  th.BasicTeam.Id,
   602  						Header:  "No references",
   603  						Purpose: "No references",
   604  					},
   605  				},
   606  				map[string]interface{}{
   607  					"test": map[string]interface{}{
   608  						"channel_mentions": map[string]interface{}{
   609  							"public1": map[string]interface{}{
   610  								"display_name": "Public 1",
   611  							},
   612  						},
   613  					},
   614  					"test2": map[string]interface{}{
   615  						"channel_mentions": map[string]interface{}{
   616  							"other-team": map[string]interface{}{
   617  								"display_name": "Other Team Channel",
   618  							},
   619  						},
   620  					},
   621  					"test3": map[string]interface{}(nil),
   622  				},
   623  			},
   624  		}
   625  
   626  		for _, testCase := range testCases {
   627  			t.Run(testCase.Description, func(t *testing.T) {
   628  				err = th.App.FillInChannelsProps(testCase.Channels)
   629  				require.Nil(t, err)
   630  
   631  				for _, channel := range *testCase.Channels {
   632  					assert.Equal(t, testCase.ExpectedChannelProps[channel.Name], channel.Props)
   633  				}
   634  			})
   635  		}
   636  	})
   637  }
   638  
   639  func TestRenameChannel(t *testing.T) {
   640  	th := Setup().InitBasic()
   641  	defer th.TearDown()
   642  
   643  	testCases := []struct {
   644  		Name                string
   645  		Channel             *model.Channel
   646  		ExpectError         bool
   647  		ExpectedName        string
   648  		ExpectedDisplayName string
   649  	}{
   650  		{
   651  			"Rename open channel",
   652  			th.createChannel(th.BasicTeam, model.CHANNEL_OPEN),
   653  			false,
   654  			"newchannelname",
   655  			"New Display Name",
   656  		},
   657  		{
   658  			"Fail on rename direct message channel",
   659  			th.CreateDmChannel(th.BasicUser2),
   660  			true,
   661  			"",
   662  			"",
   663  		},
   664  		{
   665  			"Fail on rename direct message channel",
   666  			th.CreateGroupChannel(th.BasicUser2, th.CreateUser()),
   667  			true,
   668  			"",
   669  			"",
   670  		},
   671  	}
   672  
   673  	for _, tc := range testCases {
   674  		t.Run(tc.Name, func(t *testing.T) {
   675  			channel, err := th.App.RenameChannel(tc.Channel, "newchannelname", "New Display Name")
   676  			if tc.ExpectError {
   677  				assert.NotNil(t, err)
   678  			} else {
   679  				assert.Equal(t, tc.ExpectedName, channel.Name)
   680  				assert.Equal(t, tc.ExpectedDisplayName, channel.DisplayName)
   681  			}
   682  		})
   683  	}
   684  }