github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/api4/channel_category_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/masterhung0112/hk_server/v5/model"
    13  )
    14  
    15  func TestCreateCategoryForTeamForUser(t *testing.T) {
    16  	th := Setup(t).InitBasic()
    17  	defer th.TearDown()
    18  
    19  	t.Run("should silently prevent the user from creating a category with an invalid channel ID", func(t *testing.T) {
    20  		user, client := setupUserForSubtest(t, th)
    21  
    22  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
    23  		require.Nil(t, resp.Error)
    24  		require.Len(t, categories.Categories, 3)
    25  		require.Len(t, categories.Order, 3)
    26  
    27  		// Attempt to create the category
    28  		category := &model.SidebarCategoryWithChannels{
    29  			SidebarCategory: model.SidebarCategory{
    30  				UserId:      user.Id,
    31  				TeamId:      th.BasicTeam.Id,
    32  				DisplayName: "test",
    33  			},
    34  			Channels: []string{th.BasicChannel.Id, "notachannel", th.BasicChannel2.Id},
    35  		}
    36  
    37  		received, resp := client.CreateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, category)
    38  		require.Nil(t, resp.Error)
    39  		assert.NotContains(t, received.Channels, "notachannel")
    40  		assert.Equal(t, []string{th.BasicChannel.Id, th.BasicChannel2.Id}, received.Channels)
    41  	})
    42  
    43  	t.Run("should silently prevent the user from creating a category with a channel that they're not a member of", func(t *testing.T) {
    44  		user, client := setupUserForSubtest(t, th)
    45  
    46  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
    47  		require.Nil(t, resp.Error)
    48  		require.Len(t, categories.Categories, 3)
    49  		require.Len(t, categories.Order, 3)
    50  
    51  		// Have another user create a channel that user isn't a part of
    52  		channel, resp := th.SystemAdminClient.CreateChannel(&model.Channel{
    53  			TeamId: th.BasicTeam.Id,
    54  			Type:   model.CHANNEL_OPEN,
    55  			Name:   "testchannel",
    56  		})
    57  		require.Nil(t, resp.Error)
    58  
    59  		// Attempt to create the category
    60  		category := &model.SidebarCategoryWithChannels{
    61  			SidebarCategory: model.SidebarCategory{
    62  				UserId:      user.Id,
    63  				TeamId:      th.BasicTeam.Id,
    64  				DisplayName: "test",
    65  			},
    66  			Channels: []string{th.BasicChannel.Id, channel.Id},
    67  		}
    68  
    69  		received, resp := client.CreateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, category)
    70  		require.Nil(t, resp.Error)
    71  		assert.NotContains(t, received.Channels, channel.Id)
    72  		assert.Equal(t, []string{th.BasicChannel.Id}, received.Channels)
    73  	})
    74  }
    75  
    76  func TestUpdateCategoryForTeamForUser(t *testing.T) {
    77  	th := Setup(t).InitBasic()
    78  	defer th.TearDown()
    79  
    80  	t.Run("should update the channel order of the Channels category", func(t *testing.T) {
    81  		user, client := setupUserForSubtest(t, th)
    82  
    83  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
    84  		require.Nil(t, resp.Error)
    85  		require.Len(t, categories.Categories, 3)
    86  		require.Len(t, categories.Order, 3)
    87  
    88  		channelsCategory := categories.Categories[1]
    89  		require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type)
    90  		require.Len(t, channelsCategory.Channels, 5) // Town Square, Off Topic, and the 3 channels created by InitBasic
    91  
    92  		// Should return the correct values from the API
    93  		updatedCategory := &model.SidebarCategoryWithChannels{
    94  			SidebarCategory: channelsCategory.SidebarCategory,
    95  			Channels:        []string{channelsCategory.Channels[1], channelsCategory.Channels[0], channelsCategory.Channels[4], channelsCategory.Channels[3], channelsCategory.Channels[2]},
    96  		}
    97  
    98  		received, resp := client.UpdateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, channelsCategory.Id, updatedCategory)
    99  		assert.Nil(t, resp.Error)
   100  		assert.Equal(t, channelsCategory.Id, received.Id)
   101  		assert.Equal(t, updatedCategory.Channels, received.Channels)
   102  
   103  		// And when requesting the category later
   104  		received, resp = client.GetSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, channelsCategory.Id, "")
   105  		assert.Nil(t, resp.Error)
   106  		assert.Equal(t, channelsCategory.Id, received.Id)
   107  		assert.Equal(t, updatedCategory.Channels, received.Channels)
   108  	})
   109  
   110  	t.Run("should update the sort order of the DM category", func(t *testing.T) {
   111  		user, client := setupUserForSubtest(t, th)
   112  
   113  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
   114  		require.Nil(t, resp.Error)
   115  		require.Len(t, categories.Categories, 3)
   116  		require.Len(t, categories.Order, 3)
   117  
   118  		dmsCategory := categories.Categories[2]
   119  		require.Equal(t, model.SidebarCategoryDirectMessages, dmsCategory.Type)
   120  		require.Equal(t, model.SidebarCategorySortRecent, dmsCategory.Sorting)
   121  
   122  		// Should return the correct values from the API
   123  		updatedCategory := &model.SidebarCategoryWithChannels{
   124  			SidebarCategory: dmsCategory.SidebarCategory,
   125  			Channels:        dmsCategory.Channels,
   126  		}
   127  		updatedCategory.Sorting = model.SidebarCategorySortAlphabetical
   128  
   129  		received, resp := client.UpdateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, dmsCategory.Id, updatedCategory)
   130  		assert.Nil(t, resp.Error)
   131  		assert.Equal(t, dmsCategory.Id, received.Id)
   132  		assert.Equal(t, model.SidebarCategorySortAlphabetical, received.Sorting)
   133  
   134  		// And when requesting the category later
   135  		received, resp = client.GetSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, dmsCategory.Id, "")
   136  		assert.Nil(t, resp.Error)
   137  		assert.Equal(t, dmsCategory.Id, received.Id)
   138  		assert.Equal(t, model.SidebarCategorySortAlphabetical, received.Sorting)
   139  	})
   140  
   141  	t.Run("should update the display name of a custom category", func(t *testing.T) {
   142  		user, client := setupUserForSubtest(t, th)
   143  
   144  		customCategory, resp := client.CreateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, &model.SidebarCategoryWithChannels{
   145  			SidebarCategory: model.SidebarCategory{
   146  				UserId:      user.Id,
   147  				TeamId:      th.BasicTeam.Id,
   148  				DisplayName: "custom123",
   149  			},
   150  		})
   151  		require.Nil(t, resp.Error)
   152  		require.Equal(t, "custom123", customCategory.DisplayName)
   153  
   154  		// Should return the correct values from the API
   155  		updatedCategory := &model.SidebarCategoryWithChannels{
   156  			SidebarCategory: customCategory.SidebarCategory,
   157  			Channels:        customCategory.Channels,
   158  		}
   159  		updatedCategory.DisplayName = "abcCustom"
   160  
   161  		received, resp := client.UpdateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, customCategory.Id, updatedCategory)
   162  		assert.Nil(t, resp.Error)
   163  		assert.Equal(t, customCategory.Id, received.Id)
   164  		assert.Equal(t, updatedCategory.DisplayName, received.DisplayName)
   165  
   166  		// And when requesting the category later
   167  		received, resp = client.GetSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, customCategory.Id, "")
   168  		assert.Nil(t, resp.Error)
   169  		assert.Equal(t, customCategory.Id, received.Id)
   170  		assert.Equal(t, updatedCategory.DisplayName, received.DisplayName)
   171  	})
   172  
   173  	t.Run("should update the channel order of the category even if it contains archived channels", func(t *testing.T) {
   174  		user, client := setupUserForSubtest(t, th)
   175  
   176  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
   177  		require.Nil(t, resp.Error)
   178  		require.Len(t, categories.Categories, 3)
   179  		require.Len(t, categories.Order, 3)
   180  
   181  		channelsCategory := categories.Categories[1]
   182  		require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type)
   183  		require.Len(t, channelsCategory.Channels, 5) // Town Square, Off Topic, and the 3 channels created by InitBasic
   184  
   185  		// Delete one of the channels
   186  		_, resp = client.DeleteChannel(th.BasicChannel.Id)
   187  		require.Nil(t, resp.Error)
   188  
   189  		// Should still be able to reorder the channels
   190  		updatedCategory := &model.SidebarCategoryWithChannels{
   191  			SidebarCategory: channelsCategory.SidebarCategory,
   192  			Channels:        []string{channelsCategory.Channels[1], channelsCategory.Channels[0], channelsCategory.Channels[4], channelsCategory.Channels[3], channelsCategory.Channels[2]},
   193  		}
   194  
   195  		received, resp := client.UpdateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, channelsCategory.Id, updatedCategory)
   196  		require.Nil(t, resp.Error)
   197  		assert.Equal(t, channelsCategory.Id, received.Id)
   198  		assert.Equal(t, updatedCategory.Channels, received.Channels)
   199  	})
   200  
   201  	t.Run("should silently prevent the user from adding an invalid channel ID", func(t *testing.T) {
   202  		user, client := setupUserForSubtest(t, th)
   203  
   204  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
   205  		require.Nil(t, resp.Error)
   206  		require.Len(t, categories.Categories, 3)
   207  		require.Len(t, categories.Order, 3)
   208  
   209  		channelsCategory := categories.Categories[1]
   210  		require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type)
   211  
   212  		updatedCategory := &model.SidebarCategoryWithChannels{
   213  			SidebarCategory: channelsCategory.SidebarCategory,
   214  			Channels:        append(channelsCategory.Channels, "notachannel"),
   215  		}
   216  
   217  		received, resp := client.UpdateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, channelsCategory.Id, updatedCategory)
   218  		require.Nil(t, resp.Error)
   219  		assert.Equal(t, channelsCategory.Id, received.Id)
   220  		assert.NotContains(t, received.Channels, "notachannel")
   221  		assert.Equal(t, channelsCategory.Channels, received.Channels)
   222  	})
   223  
   224  	t.Run("should silently prevent the user from adding a channel that they're not a member of", func(t *testing.T) {
   225  		user, client := setupUserForSubtest(t, th)
   226  
   227  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
   228  		require.Nil(t, resp.Error)
   229  		require.Len(t, categories.Categories, 3)
   230  		require.Len(t, categories.Order, 3)
   231  
   232  		channelsCategory := categories.Categories[1]
   233  		require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type)
   234  
   235  		// Have another user create a channel that user isn't a part of
   236  		channel, resp := th.SystemAdminClient.CreateChannel(&model.Channel{
   237  			TeamId: th.BasicTeam.Id,
   238  			Type:   model.CHANNEL_OPEN,
   239  			Name:   "testchannel",
   240  		})
   241  		require.Nil(t, resp.Error)
   242  
   243  		// Attempt to update the category
   244  		updatedCategory := &model.SidebarCategoryWithChannels{
   245  			SidebarCategory: channelsCategory.SidebarCategory,
   246  			Channels:        append(channelsCategory.Channels, channel.Id),
   247  		}
   248  
   249  		received, resp := client.UpdateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, channelsCategory.Id, updatedCategory)
   250  		require.Nil(t, resp.Error)
   251  		assert.Equal(t, channelsCategory.Id, received.Id)
   252  		assert.NotContains(t, received.Channels, channel.Id)
   253  		assert.Equal(t, channelsCategory.Channels, received.Channels)
   254  	})
   255  
   256  	t.Run("muting a category should mute all of its channels", func(t *testing.T) {
   257  		user, client := setupUserForSubtest(t, th)
   258  
   259  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
   260  		require.Nil(t, resp.Error)
   261  		require.Len(t, categories.Categories, 3)
   262  		require.Len(t, categories.Order, 3)
   263  
   264  		channelsCategory := categories.Categories[1]
   265  		require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type)
   266  		require.True(t, len(channelsCategory.Channels) > 0)
   267  
   268  		// Mute the category
   269  		updatedCategory := &model.SidebarCategoryWithChannels{
   270  			SidebarCategory: model.SidebarCategory{
   271  				Id:      channelsCategory.Id,
   272  				UserId:  user.Id,
   273  				TeamId:  th.BasicTeam.Id,
   274  				Sorting: channelsCategory.Sorting,
   275  				Muted:   true,
   276  			},
   277  			Channels: channelsCategory.Channels,
   278  		}
   279  
   280  		received, resp := client.UpdateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, channelsCategory.Id, updatedCategory)
   281  		require.Nil(t, resp.Error)
   282  		assert.Equal(t, channelsCategory.Id, received.Id)
   283  		assert.True(t, received.Muted)
   284  
   285  		// Check that the muted category was saved in the database
   286  		received, resp = client.GetSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, channelsCategory.Id, "")
   287  		require.Nil(t, resp.Error)
   288  		assert.Equal(t, channelsCategory.Id, received.Id)
   289  		assert.True(t, received.Muted)
   290  
   291  		// Confirm that the channels in the category were muted
   292  		member, resp := client.GetChannelMember(channelsCategory.Channels[0], user.Id, "")
   293  		require.Nil(t, resp.Error)
   294  		assert.True(t, member.IsChannelMuted())
   295  	})
   296  
   297  	t.Run("should not be able to mute DM category", func(t *testing.T) {
   298  		user, client := setupUserForSubtest(t, th)
   299  
   300  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
   301  		require.Nil(t, resp.Error)
   302  		require.Len(t, categories.Categories, 3)
   303  		require.Len(t, categories.Order, 3)
   304  
   305  		dmsCategory := categories.Categories[2]
   306  		require.Equal(t, model.SidebarCategoryDirectMessages, dmsCategory.Type)
   307  		require.Len(t, dmsCategory.Channels, 0)
   308  
   309  		// Ensure a DM channel exists
   310  		dmChannel, resp := client.CreateDirectChannel(user.Id, th.BasicUser.Id)
   311  		require.Nil(t, resp.Error)
   312  
   313  		// Attempt to mute the category
   314  		updatedCategory := &model.SidebarCategoryWithChannels{
   315  			SidebarCategory: model.SidebarCategory{
   316  				Id:      dmsCategory.Id,
   317  				UserId:  user.Id,
   318  				TeamId:  th.BasicTeam.Id,
   319  				Sorting: dmsCategory.Sorting,
   320  				Muted:   true,
   321  			},
   322  			Channels: []string{dmChannel.Id},
   323  		}
   324  
   325  		received, resp := client.UpdateSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, dmsCategory.Id, updatedCategory)
   326  		require.Nil(t, resp.Error)
   327  		assert.Equal(t, dmsCategory.Id, received.Id)
   328  		assert.False(t, received.Muted)
   329  
   330  		// Check that the muted category was not saved in the database
   331  		received, resp = client.GetSidebarCategoryForTeamForUser(user.Id, th.BasicTeam.Id, dmsCategory.Id, "")
   332  		require.Nil(t, resp.Error)
   333  		assert.Equal(t, dmsCategory.Id, received.Id)
   334  		assert.False(t, received.Muted)
   335  
   336  		// Confirm that the channels in the category were not muted
   337  		member, resp := client.GetChannelMember(dmChannel.Id, user.Id, "")
   338  		require.Nil(t, resp.Error)
   339  		assert.False(t, member.IsChannelMuted())
   340  	})
   341  }
   342  
   343  func TestUpdateCategoriesForTeamForUser(t *testing.T) {
   344  	th := Setup(t).InitBasic()
   345  	defer th.TearDown()
   346  
   347  	t.Run("should silently prevent the user from adding an invalid channel ID", func(t *testing.T) {
   348  		user, client := setupUserForSubtest(t, th)
   349  
   350  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
   351  		require.Nil(t, resp.Error)
   352  		require.Len(t, categories.Categories, 3)
   353  		require.Len(t, categories.Order, 3)
   354  
   355  		channelsCategory := categories.Categories[1]
   356  		require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type)
   357  
   358  		updatedCategory := &model.SidebarCategoryWithChannels{
   359  			SidebarCategory: channelsCategory.SidebarCategory,
   360  			Channels:        append(channelsCategory.Channels, "notachannel"),
   361  		}
   362  
   363  		received, resp := client.UpdateSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{updatedCategory})
   364  		require.Nil(t, resp.Error)
   365  		assert.Equal(t, channelsCategory.Id, received[0].Id)
   366  		assert.NotContains(t, received[0].Channels, "notachannel")
   367  		assert.Equal(t, channelsCategory.Channels, received[0].Channels)
   368  	})
   369  
   370  	t.Run("should silently prevent the user from adding a channel that they're not a member of", func(t *testing.T) {
   371  		user, client := setupUserForSubtest(t, th)
   372  
   373  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
   374  		require.Nil(t, resp.Error)
   375  		require.Len(t, categories.Categories, 3)
   376  		require.Len(t, categories.Order, 3)
   377  
   378  		channelsCategory := categories.Categories[1]
   379  		require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type)
   380  
   381  		// Have another user create a channel that user isn't a part of
   382  		channel, resp := th.SystemAdminClient.CreateChannel(&model.Channel{
   383  			TeamId: th.BasicTeam.Id,
   384  			Type:   model.CHANNEL_OPEN,
   385  			Name:   "testchannel",
   386  		})
   387  		require.Nil(t, resp.Error)
   388  
   389  		// Attempt to update the category
   390  		updatedCategory := &model.SidebarCategoryWithChannels{
   391  			SidebarCategory: channelsCategory.SidebarCategory,
   392  			Channels:        append(channelsCategory.Channels, channel.Id),
   393  		}
   394  
   395  		received, resp := client.UpdateSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{updatedCategory})
   396  		require.Nil(t, resp.Error)
   397  		assert.Equal(t, channelsCategory.Id, received[0].Id)
   398  		assert.NotContains(t, received[0].Channels, channel.Id)
   399  		assert.Equal(t, channelsCategory.Channels, received[0].Channels)
   400  	})
   401  
   402  	t.Run("should update order", func(t *testing.T) {
   403  		user, client := setupUserForSubtest(t, th)
   404  
   405  		categories, resp := client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
   406  		require.Nil(t, resp.Error)
   407  		require.Len(t, categories.Categories, 3)
   408  		require.Len(t, categories.Order, 3)
   409  
   410  		channelsCategory := categories.Categories[1]
   411  		require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type)
   412  
   413  		_, resp = client.UpdateSidebarCategoryOrderForTeamForUser(user.Id, th.BasicTeam.Id, []string{categories.Order[1], categories.Order[0], categories.Order[2]})
   414  		require.Nil(t, resp.Error)
   415  
   416  		categories, resp = client.GetSidebarCategoriesForTeamForUser(user.Id, th.BasicTeam.Id, "")
   417  		require.Nil(t, resp.Error)
   418  		require.Len(t, categories.Categories, 3)
   419  		require.Len(t, categories.Order, 3)
   420  
   421  		channelsCategory = categories.Categories[0]
   422  		require.Equal(t, model.SidebarCategoryChannels, channelsCategory.Type)
   423  
   424  		// validate order
   425  		newOrder, resp := client.GetSidebarCategoryOrderForTeamForUser(user.Id, th.BasicTeam.Id, "")
   426  		require.Nil(t, resp.Error)
   427  		require.EqualValues(t, newOrder, categories.Order)
   428  
   429  		// try to update with missing category
   430  		_, resp = client.UpdateSidebarCategoryOrderForTeamForUser(user.Id, th.BasicTeam.Id, []string{categories.Order[1], categories.Order[0]})
   431  		require.NotNil(t, resp.Error)
   432  
   433  		// try to update with invalid category
   434  		_, resp = client.UpdateSidebarCategoryOrderForTeamForUser(user.Id, th.BasicTeam.Id, []string{categories.Order[1], categories.Order[0], "asd"})
   435  		require.NotNil(t, resp.Error)
   436  	})
   437  }
   438  
   439  func setupUserForSubtest(t *testing.T, th *TestHelper) (*model.User, *model.Client4) {
   440  	password := "password"
   441  	user, err := th.App.CreateUser(th.Context, &model.User{
   442  		Email:    th.GenerateTestEmail(),
   443  		Username: "user_" + model.NewId(),
   444  		Password: password,
   445  	})
   446  	require.Nil(t, err)
   447  
   448  	th.LinkUserToTeam(user, th.BasicTeam)
   449  	th.AddUserToChannel(user, th.BasicChannel)
   450  	th.AddUserToChannel(user, th.BasicChannel2)
   451  	th.AddUserToChannel(user, th.BasicPrivateChannel)
   452  
   453  	client := th.CreateClient()
   454  	user, resp := client.Login(user.Email, password)
   455  	require.Nil(t, resp.Error)
   456  
   457  	return user, client
   458  }