github.com/adacta-ru/mattermost-server/v6@v6.0.0/api4/preference_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  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/adacta-ru/mattermost-server/v6/model"
    15  )
    16  
    17  func TestGetPreferences(t *testing.T) {
    18  	th := Setup(t).InitBasic()
    19  	defer th.TearDown()
    20  	Client := th.Client
    21  
    22  	// recreate basic user (cached has no default preferences)
    23  	th.BasicUser = th.CreateUser()
    24  	th.LoginBasic()
    25  
    26  	user1 := th.BasicUser
    27  
    28  	category := model.NewId()
    29  	preferences1 := model.Preferences{
    30  		{
    31  			UserId:   user1.Id,
    32  			Category: category,
    33  			Name:     model.NewId(),
    34  		},
    35  		{
    36  			UserId:   user1.Id,
    37  			Category: category,
    38  			Name:     model.NewId(),
    39  		},
    40  		{
    41  			UserId:   user1.Id,
    42  			Category: model.NewId(),
    43  			Name:     model.NewId(),
    44  		},
    45  	}
    46  
    47  	Client.UpdatePreferences(user1.Id, &preferences1)
    48  
    49  	prefs, resp := Client.GetPreferences(user1.Id)
    50  	CheckNoError(t, resp)
    51  	require.Equal(t, len(prefs), 4, "received the wrong number of preferences")
    52  
    53  	for _, preference := range prefs {
    54  		require.Equal(t, preference.UserId, th.BasicUser.Id, "user id does not match")
    55  	}
    56  
    57  	// recreate basic user2
    58  	th.BasicUser2 = th.CreateUser()
    59  	th.LoginBasic2()
    60  
    61  	prefs, resp = Client.GetPreferences(th.BasicUser2.Id)
    62  	CheckNoError(t, resp)
    63  
    64  	require.Greater(t, len(prefs), 0, "received the wrong number of preferences")
    65  
    66  	_, resp = Client.GetPreferences(th.BasicUser.Id)
    67  	CheckForbiddenStatus(t, resp)
    68  
    69  	Client.Logout()
    70  	_, resp = Client.GetPreferences(th.BasicUser2.Id)
    71  	CheckUnauthorizedStatus(t, resp)
    72  }
    73  
    74  func TestGetPreferencesByCategory(t *testing.T) {
    75  	th := Setup(t).InitBasic()
    76  	defer th.TearDown()
    77  	Client := th.Client
    78  
    79  	th.LoginBasic()
    80  	user1 := th.BasicUser
    81  
    82  	category := model.NewId()
    83  	preferences1 := model.Preferences{
    84  		{
    85  			UserId:   user1.Id,
    86  			Category: category,
    87  			Name:     model.NewId(),
    88  		},
    89  		{
    90  			UserId:   user1.Id,
    91  			Category: category,
    92  			Name:     model.NewId(),
    93  		},
    94  		{
    95  			UserId:   user1.Id,
    96  			Category: model.NewId(),
    97  			Name:     model.NewId(),
    98  		},
    99  	}
   100  
   101  	Client.UpdatePreferences(user1.Id, &preferences1)
   102  
   103  	prefs, resp := Client.GetPreferencesByCategory(user1.Id, category)
   104  	CheckNoError(t, resp)
   105  
   106  	require.Equal(t, len(prefs), 2, "received the wrong number of preferences")
   107  
   108  	_, resp = Client.GetPreferencesByCategory(user1.Id, "junk")
   109  	CheckNotFoundStatus(t, resp)
   110  
   111  	th.LoginBasic2()
   112  
   113  	_, resp = Client.GetPreferencesByCategory(th.BasicUser2.Id, category)
   114  	CheckNotFoundStatus(t, resp)
   115  
   116  	_, resp = Client.GetPreferencesByCategory(user1.Id, category)
   117  	CheckForbiddenStatus(t, resp)
   118  
   119  	prefs, resp = Client.GetPreferencesByCategory(th.BasicUser2.Id, "junk")
   120  	CheckNotFoundStatus(t, resp)
   121  
   122  	require.Equal(t, len(prefs), 0, "received the wrong number of preferences")
   123  
   124  	Client.Logout()
   125  	_, resp = Client.GetPreferencesByCategory(th.BasicUser2.Id, category)
   126  	CheckUnauthorizedStatus(t, resp)
   127  }
   128  
   129  func TestGetPreferenceByCategoryAndName(t *testing.T) {
   130  	th := Setup(t).InitBasic()
   131  	defer th.TearDown()
   132  	Client := th.Client
   133  
   134  	th.LoginBasic()
   135  	user := th.BasicUser
   136  	name := model.NewId()
   137  	value := model.NewId()
   138  
   139  	preferences := model.Preferences{
   140  		{
   141  			UserId:   user.Id,
   142  			Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
   143  			Name:     name,
   144  			Value:    value,
   145  		},
   146  		{
   147  			UserId:   user.Id,
   148  			Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
   149  			Name:     model.NewId(),
   150  			Value:    model.NewId(),
   151  		},
   152  	}
   153  
   154  	Client.UpdatePreferences(user.Id, &preferences)
   155  
   156  	pref, resp := Client.GetPreferenceByCategoryAndName(user.Id, model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, name)
   157  	CheckNoError(t, resp)
   158  
   159  	require.Equal(t, preferences[0].UserId, pref.UserId, "UserId preference not saved")
   160  	require.Equal(t, preferences[0].Category, pref.Category, "Category preference not saved")
   161  	require.Equal(t, preferences[0].Name, pref.Name, "Name preference not saved")
   162  
   163  	preferences[0].Value = model.NewId()
   164  	Client.UpdatePreferences(user.Id, &preferences)
   165  
   166  	_, resp = Client.GetPreferenceByCategoryAndName(user.Id, "junk", preferences[0].Name)
   167  	CheckBadRequestStatus(t, resp)
   168  
   169  	_, resp = Client.GetPreferenceByCategoryAndName(user.Id, preferences[0].Category, "junk")
   170  	CheckBadRequestStatus(t, resp)
   171  
   172  	_, resp = Client.GetPreferenceByCategoryAndName(th.BasicUser2.Id, preferences[0].Category, "junk")
   173  	CheckForbiddenStatus(t, resp)
   174  
   175  	_, resp = Client.GetPreferenceByCategoryAndName(user.Id, preferences[0].Category, preferences[0].Name)
   176  	CheckNoError(t, resp)
   177  
   178  	Client.Logout()
   179  	_, resp = Client.GetPreferenceByCategoryAndName(user.Id, preferences[0].Category, preferences[0].Name)
   180  	CheckUnauthorizedStatus(t, resp)
   181  
   182  }
   183  
   184  func TestUpdatePreferences(t *testing.T) {
   185  	th := Setup(t).InitBasic()
   186  	defer th.TearDown()
   187  	Client := th.Client
   188  
   189  	th.LoginBasic()
   190  	user1 := th.BasicUser
   191  
   192  	category := model.NewId()
   193  	preferences1 := model.Preferences{
   194  		{
   195  			UserId:   user1.Id,
   196  			Category: category,
   197  			Name:     model.NewId(),
   198  		},
   199  		{
   200  			UserId:   user1.Id,
   201  			Category: category,
   202  			Name:     model.NewId(),
   203  		},
   204  		{
   205  			UserId:   user1.Id,
   206  			Category: model.NewId(),
   207  			Name:     model.NewId(),
   208  		},
   209  	}
   210  
   211  	_, resp := Client.UpdatePreferences(user1.Id, &preferences1)
   212  	CheckNoError(t, resp)
   213  
   214  	preferences := model.Preferences{
   215  		{
   216  			UserId:   model.NewId(),
   217  			Category: category,
   218  			Name:     model.NewId(),
   219  		},
   220  	}
   221  
   222  	_, resp = Client.UpdatePreferences(user1.Id, &preferences)
   223  	CheckForbiddenStatus(t, resp)
   224  
   225  	preferences = model.Preferences{
   226  		{
   227  			UserId: user1.Id,
   228  			Name:   model.NewId(),
   229  		},
   230  	}
   231  
   232  	_, resp = Client.UpdatePreferences(user1.Id, &preferences)
   233  	CheckBadRequestStatus(t, resp)
   234  
   235  	_, resp = Client.UpdatePreferences(th.BasicUser2.Id, &preferences)
   236  	CheckForbiddenStatus(t, resp)
   237  
   238  	Client.Logout()
   239  	_, resp = Client.UpdatePreferences(user1.Id, &preferences1)
   240  	CheckUnauthorizedStatus(t, resp)
   241  }
   242  
   243  func TestUpdatePreferencesWebsocket(t *testing.T) {
   244  	th := Setup(t).InitBasic()
   245  	defer th.TearDown()
   246  
   247  	WebSocketClient, err := th.CreateWebSocketClient()
   248  	require.Nil(t, err)
   249  
   250  	WebSocketClient.Listen()
   251  	time.Sleep(300 * time.Millisecond)
   252  	wsResp := <-WebSocketClient.ResponseChannel
   253  	require.Equal(t, wsResp.Status, model.STATUS_OK, "expected OK from auth challenge")
   254  
   255  	userId := th.BasicUser.Id
   256  	preferences := &model.Preferences{
   257  		{
   258  			UserId:   userId,
   259  			Category: model.NewId(),
   260  			Name:     model.NewId(),
   261  		},
   262  		{
   263  			UserId:   userId,
   264  			Category: model.NewId(),
   265  			Name:     model.NewId(),
   266  		},
   267  	}
   268  
   269  	_, resp := th.Client.UpdatePreferences(userId, preferences)
   270  	CheckNoError(t, resp)
   271  
   272  	timeout := time.After(300 * time.Millisecond)
   273  
   274  	waiting := true
   275  	for waiting {
   276  		select {
   277  		case event := <-WebSocketClient.EventChannel:
   278  			if event.EventType() != model.WEBSOCKET_EVENT_PREFERENCES_CHANGED {
   279  				// Ignore any other events
   280  				continue
   281  			}
   282  
   283  			received, err := model.PreferencesFromJson(strings.NewReader(event.GetData()["preferences"].(string)))
   284  			require.NoError(t, err)
   285  
   286  			for i, p := range *preferences {
   287  				require.Equal(t, received[i].UserId, p.UserId, "received incorrect UserId")
   288  				require.Equal(t, received[i].Category, p.Category, "received incorrect Category")
   289  				require.Equal(t, received[i].Name, p.Name, "received incorrect Name")
   290  			}
   291  
   292  			waiting = false
   293  		case <-timeout:
   294  			require.Fail(t, "timed timed out waiting for preference update event")
   295  		}
   296  	}
   297  }
   298  
   299  func TestUpdateSidebarPreferences(t *testing.T) {
   300  	t.Run("when favoriting a channel, should add it to the Favorites sidebar category", func(t *testing.T) {
   301  		th := Setup(t).InitBasic()
   302  		defer th.TearDown()
   303  
   304  		user := th.BasicUser
   305  
   306  		team1 := th.CreateTeam()
   307  		th.LinkUserToTeam(user, team1)
   308  
   309  		_, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   310  		require.Nil(t, resp.Error)
   311  
   312  		channel := th.CreateChannelWithClientAndTeam(th.Client, model.CHANNEL_OPEN, team1.Id)
   313  		th.AddUserToChannel(user, channel)
   314  
   315  		// Confirm that the sidebar is populated correctly to begin with
   316  		categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   317  		require.Nil(t, resp.Error)
   318  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   319  		require.NotContains(t, categories.Categories[0].Channels, channel.Id)
   320  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   321  		require.Contains(t, categories.Categories[1].Channels, channel.Id)
   322  
   323  		// Favorite the channel
   324  		_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
   325  			{
   326  				UserId:   user.Id,
   327  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   328  				Name:     channel.Id,
   329  				Value:    "true",
   330  			},
   331  		})
   332  		require.Nil(t, resp.Error)
   333  
   334  		// Confirm that the channel was added to the Favorites
   335  		categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   336  		require.Nil(t, resp.Error)
   337  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   338  		assert.Contains(t, categories.Categories[0].Channels, channel.Id)
   339  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   340  		assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
   341  
   342  		// And unfavorite the channel
   343  		_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
   344  			{
   345  				UserId:   user.Id,
   346  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   347  				Name:     channel.Id,
   348  				Value:    "false",
   349  			},
   350  		})
   351  		require.Nil(t, resp.Error)
   352  
   353  		// The channel should've been removed from the Favorites
   354  		categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   355  		require.Nil(t, resp.Error)
   356  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   357  		require.NotContains(t, categories.Categories[0].Channels, channel.Id)
   358  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   359  		assert.Contains(t, categories.Categories[1].Channels, channel.Id)
   360  	})
   361  
   362  	t.Run("when favoriting a DM channel, should add it to the Favorites sidebar category for all teams", func(t *testing.T) {
   363  		th := Setup(t).InitBasic()
   364  		defer th.TearDown()
   365  
   366  		user := th.BasicUser
   367  		user2 := th.BasicUser2
   368  
   369  		team1 := th.CreateTeam()
   370  		th.LinkUserToTeam(user, team1)
   371  		team2 := th.CreateTeam()
   372  		th.LinkUserToTeam(user, team2)
   373  
   374  		dmChannel := th.CreateDmChannel(user2)
   375  
   376  		// Favorite the channel
   377  		_, resp := th.Client.UpdatePreferences(user.Id, &model.Preferences{
   378  			{
   379  				UserId:   user.Id,
   380  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   381  				Name:     dmChannel.Id,
   382  				Value:    "true",
   383  			},
   384  		})
   385  		require.Nil(t, resp.Error)
   386  
   387  		// Confirm that the channel was added to the Favorites on all teams
   388  		categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   389  		require.Nil(t, resp.Error)
   390  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   391  		assert.Contains(t, categories.Categories[0].Channels, dmChannel.Id)
   392  		require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
   393  		assert.NotContains(t, categories.Categories[2].Channels, dmChannel.Id)
   394  
   395  		categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team2.Id, "")
   396  		require.Nil(t, resp.Error)
   397  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   398  		assert.Contains(t, categories.Categories[0].Channels, dmChannel.Id)
   399  		require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
   400  		assert.NotContains(t, categories.Categories[2].Channels, dmChannel.Id)
   401  
   402  		// And unfavorite the channel
   403  		_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
   404  			{
   405  				UserId:   user.Id,
   406  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   407  				Name:     dmChannel.Id,
   408  				Value:    "false",
   409  			},
   410  		})
   411  		require.Nil(t, resp.Error)
   412  
   413  		// The channel should've been removed from the Favorites on all teams
   414  		categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   415  		require.Nil(t, resp.Error)
   416  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   417  		require.NotContains(t, categories.Categories[0].Channels, dmChannel.Id)
   418  		require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
   419  		assert.Contains(t, categories.Categories[2].Channels, dmChannel.Id)
   420  
   421  		categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team2.Id, "")
   422  		require.Nil(t, resp.Error)
   423  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   424  		require.NotContains(t, categories.Categories[0].Channels, dmChannel.Id)
   425  		require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
   426  		assert.Contains(t, categories.Categories[2].Channels, dmChannel.Id)
   427  	})
   428  
   429  	t.Run("when favoriting a channel, should not affect other users' favorites categories", func(t *testing.T) {
   430  		th := Setup(t).InitBasic()
   431  		defer th.TearDown()
   432  
   433  		user := th.BasicUser
   434  		user2 := th.BasicUser2
   435  
   436  		client2 := th.CreateClient()
   437  		th.LoginBasic2WithClient(client2)
   438  
   439  		team1 := th.CreateTeam()
   440  		th.LinkUserToTeam(user, team1)
   441  		th.LinkUserToTeam(user2, team1)
   442  
   443  		_, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   444  		require.Nil(t, resp.Error)
   445  		_, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
   446  		require.Nil(t, resp.Error)
   447  
   448  		channel := th.CreateChannelWithClientAndTeam(th.Client, model.CHANNEL_OPEN, team1.Id)
   449  		th.AddUserToChannel(user, channel)
   450  		th.AddUserToChannel(user2, channel)
   451  
   452  		// Confirm that the sidebar is populated correctly to begin with
   453  		categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   454  		require.Nil(t, resp.Error)
   455  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   456  		require.NotContains(t, categories.Categories[0].Channels, channel.Id)
   457  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   458  		require.Contains(t, categories.Categories[1].Channels, channel.Id)
   459  
   460  		categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
   461  		require.Nil(t, resp.Error)
   462  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   463  		require.NotContains(t, categories.Categories[0].Channels, channel.Id)
   464  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   465  		require.Contains(t, categories.Categories[1].Channels, channel.Id)
   466  
   467  		// Favorite the channel
   468  		_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
   469  			{
   470  				UserId:   user.Id,
   471  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   472  				Name:     channel.Id,
   473  				Value:    "true",
   474  			},
   475  		})
   476  		require.Nil(t, resp.Error)
   477  
   478  		// Confirm that the channel was not added to Favorites for the second user
   479  		categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
   480  		require.Nil(t, resp.Error)
   481  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   482  		assert.NotContains(t, categories.Categories[0].Channels, channel.Id)
   483  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   484  		assert.Contains(t, categories.Categories[1].Channels, channel.Id)
   485  
   486  		// Favorite the channel for the second user
   487  		_, resp = client2.UpdatePreferences(user2.Id, &model.Preferences{
   488  			{
   489  				UserId:   user2.Id,
   490  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   491  				Name:     channel.Id,
   492  				Value:    "true",
   493  			},
   494  		})
   495  		require.Nil(t, resp.Error)
   496  
   497  		// Confirm that the channel is now in the Favorites for the second user
   498  		categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
   499  		require.Nil(t, resp.Error)
   500  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   501  		assert.Contains(t, categories.Categories[0].Channels, channel.Id)
   502  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   503  		assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
   504  
   505  		// And unfavorite the channel
   506  		_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
   507  			{
   508  				UserId:   user.Id,
   509  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   510  				Name:     channel.Id,
   511  				Value:    "false",
   512  			},
   513  		})
   514  		require.Nil(t, resp.Error)
   515  
   516  		// The channel should still be in the second user's favorites
   517  		categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
   518  		require.Nil(t, resp.Error)
   519  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   520  		assert.Contains(t, categories.Categories[0].Channels, channel.Id)
   521  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   522  		assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
   523  	})
   524  }
   525  
   526  func TestDeletePreferences(t *testing.T) {
   527  	th := Setup(t).InitBasic()
   528  	defer th.TearDown()
   529  	Client := th.Client
   530  
   531  	th.LoginBasic()
   532  
   533  	prefs, _ := Client.GetPreferences(th.BasicUser.Id)
   534  	originalCount := len(prefs)
   535  
   536  	// save 10 preferences
   537  	var preferences model.Preferences
   538  	for i := 0; i < 10; i++ {
   539  		preference := model.Preference{
   540  			UserId:   th.BasicUser.Id,
   541  			Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
   542  			Name:     model.NewId(),
   543  		}
   544  		preferences = append(preferences, preference)
   545  	}
   546  
   547  	Client.UpdatePreferences(th.BasicUser.Id, &preferences)
   548  
   549  	// delete 10 preferences
   550  	th.LoginBasic2()
   551  
   552  	_, resp := Client.DeletePreferences(th.BasicUser2.Id, &preferences)
   553  	CheckForbiddenStatus(t, resp)
   554  
   555  	th.LoginBasic()
   556  
   557  	_, resp = Client.DeletePreferences(th.BasicUser.Id, &preferences)
   558  	CheckNoError(t, resp)
   559  
   560  	_, resp = Client.DeletePreferences(th.BasicUser2.Id, &preferences)
   561  	CheckForbiddenStatus(t, resp)
   562  
   563  	prefs, _ = Client.GetPreferences(th.BasicUser.Id)
   564  	require.Len(t, prefs, originalCount, "should've deleted preferences")
   565  
   566  	Client.Logout()
   567  	_, resp = Client.DeletePreferences(th.BasicUser.Id, &preferences)
   568  	CheckUnauthorizedStatus(t, resp)
   569  }
   570  
   571  func TestDeletePreferencesWebsocket(t *testing.T) {
   572  	th := Setup(t).InitBasic()
   573  	defer th.TearDown()
   574  
   575  	userId := th.BasicUser.Id
   576  	preferences := &model.Preferences{
   577  		{
   578  			UserId:   userId,
   579  			Category: model.NewId(),
   580  			Name:     model.NewId(),
   581  		},
   582  		{
   583  			UserId:   userId,
   584  			Category: model.NewId(),
   585  			Name:     model.NewId(),
   586  		},
   587  	}
   588  	_, resp := th.Client.UpdatePreferences(userId, preferences)
   589  	CheckNoError(t, resp)
   590  
   591  	WebSocketClient, err := th.CreateWebSocketClient()
   592  	require.Nil(t, err)
   593  
   594  	WebSocketClient.Listen()
   595  	wsResp := <-WebSocketClient.ResponseChannel
   596  	require.Equal(t, model.STATUS_OK, wsResp.Status, "should have responded OK to authentication challenge")
   597  
   598  	_, resp = th.Client.DeletePreferences(userId, preferences)
   599  	CheckNoError(t, resp)
   600  
   601  	timeout := time.After(30000 * time.Millisecond)
   602  
   603  	waiting := true
   604  	for waiting {
   605  		select {
   606  		case event := <-WebSocketClient.EventChannel:
   607  			if event.EventType() != model.WEBSOCKET_EVENT_PREFERENCES_DELETED {
   608  				// Ignore any other events
   609  				continue
   610  			}
   611  
   612  			received, err := model.PreferencesFromJson(strings.NewReader(event.GetData()["preferences"].(string)))
   613  			require.Nil(t, err)
   614  
   615  			for i, preference := range *preferences {
   616  				require.Equal(t, preference.UserId, received[i].UserId)
   617  				require.Equal(t, preference.Category, received[i].Category)
   618  				require.Equal(t, preference.Name, received[i].Name)
   619  			}
   620  
   621  			waiting = false
   622  		case <-timeout:
   623  			require.Fail(t, "timed out waiting for preference delete event")
   624  		}
   625  	}
   626  }
   627  
   628  func TestDeleteSidebarPreferences(t *testing.T) {
   629  	t.Run("when removing a favorited channel preference, should remove it from the Favorites sidebar category", func(t *testing.T) {
   630  		th := Setup(t).InitBasic()
   631  		defer th.TearDown()
   632  
   633  		user := th.BasicUser
   634  
   635  		team1 := th.CreateTeam()
   636  		th.LinkUserToTeam(user, team1)
   637  
   638  		_, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   639  		require.Nil(t, resp.Error)
   640  
   641  		channel := th.CreateChannelWithClientAndTeam(th.Client, model.CHANNEL_OPEN, team1.Id)
   642  		th.AddUserToChannel(user, channel)
   643  
   644  		// Confirm that the sidebar is populated correctly to begin with
   645  		categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   646  		require.Nil(t, resp.Error)
   647  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   648  		require.NotContains(t, categories.Categories[0].Channels, channel.Id)
   649  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   650  		require.Contains(t, categories.Categories[1].Channels, channel.Id)
   651  
   652  		// Favorite the channel
   653  		_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
   654  			{
   655  				UserId:   user.Id,
   656  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   657  				Name:     channel.Id,
   658  				Value:    "true",
   659  			},
   660  		})
   661  		require.Nil(t, resp.Error)
   662  
   663  		// Confirm that the channel was added to the Favorites
   664  		categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   665  		require.Nil(t, resp.Error)
   666  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   667  		assert.Contains(t, categories.Categories[0].Channels, channel.Id)
   668  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   669  		assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
   670  
   671  		// And unfavorite the channel by deleting the preference
   672  		_, resp = th.Client.DeletePreferences(user.Id, &model.Preferences{
   673  			{
   674  				UserId:   user.Id,
   675  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   676  				Name:     channel.Id,
   677  			},
   678  		})
   679  		require.Nil(t, resp.Error)
   680  
   681  		// The channel should've been removed from the Favorites
   682  		categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   683  		require.Nil(t, resp.Error)
   684  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   685  		require.NotContains(t, categories.Categories[0].Channels, channel.Id)
   686  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   687  		assert.Contains(t, categories.Categories[1].Channels, channel.Id)
   688  	})
   689  
   690  	t.Run("when removing a favorited DM preference, should remove it from the Favorites sidebar category", func(t *testing.T) {
   691  		th := Setup(t).InitBasic()
   692  		defer th.TearDown()
   693  
   694  		user := th.BasicUser
   695  		user2 := th.BasicUser2
   696  
   697  		team1 := th.CreateTeam()
   698  		th.LinkUserToTeam(user, team1)
   699  		team2 := th.CreateTeam()
   700  		th.LinkUserToTeam(user, team2)
   701  
   702  		dmChannel := th.CreateDmChannel(user2)
   703  
   704  		// Favorite the channel
   705  		_, resp := th.Client.UpdatePreferences(user.Id, &model.Preferences{
   706  			{
   707  				UserId:   user.Id,
   708  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   709  				Name:     dmChannel.Id,
   710  				Value:    "true",
   711  			},
   712  		})
   713  		require.Nil(t, resp.Error)
   714  
   715  		// Confirm that the channel was added to the Favorites on all teams
   716  		categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   717  		require.Nil(t, resp.Error)
   718  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   719  		assert.Contains(t, categories.Categories[0].Channels, dmChannel.Id)
   720  		require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
   721  		assert.NotContains(t, categories.Categories[2].Channels, dmChannel.Id)
   722  
   723  		categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team2.Id, "")
   724  		require.Nil(t, resp.Error)
   725  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   726  		assert.Contains(t, categories.Categories[0].Channels, dmChannel.Id)
   727  		require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
   728  		assert.NotContains(t, categories.Categories[2].Channels, dmChannel.Id)
   729  
   730  		// And unfavorite the channel by deleting the preference
   731  		_, resp = th.Client.DeletePreferences(user.Id, &model.Preferences{
   732  			{
   733  				UserId:   user.Id,
   734  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   735  				Name:     dmChannel.Id,
   736  			},
   737  		})
   738  		require.Nil(t, resp.Error)
   739  
   740  		// The channel should've been removed from the Favorites on all teams
   741  		categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   742  		require.Nil(t, resp.Error)
   743  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   744  		require.NotContains(t, categories.Categories[0].Channels, dmChannel.Id)
   745  		require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
   746  		assert.Contains(t, categories.Categories[2].Channels, dmChannel.Id)
   747  
   748  		categories, resp = th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team2.Id, "")
   749  		require.Nil(t, resp.Error)
   750  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   751  		require.NotContains(t, categories.Categories[0].Channels, dmChannel.Id)
   752  		require.Equal(t, model.SidebarCategoryDirectMessages, categories.Categories[2].Type)
   753  		assert.Contains(t, categories.Categories[2].Channels, dmChannel.Id)
   754  	})
   755  
   756  	t.Run("when removing a favorited channel preference, should not affect other users' favorites categories", func(t *testing.T) {
   757  		th := Setup(t).InitBasic()
   758  		defer th.TearDown()
   759  
   760  		user := th.BasicUser
   761  		user2 := th.BasicUser2
   762  
   763  		client2 := th.CreateClient()
   764  		th.LoginBasic2WithClient(client2)
   765  
   766  		team1 := th.CreateTeam()
   767  		th.LinkUserToTeam(user, team1)
   768  		th.LinkUserToTeam(user2, team1)
   769  
   770  		_, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   771  		require.Nil(t, resp.Error)
   772  		_, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
   773  		require.Nil(t, resp.Error)
   774  
   775  		channel := th.CreateChannelWithClientAndTeam(th.Client, model.CHANNEL_OPEN, team1.Id)
   776  		th.AddUserToChannel(user, channel)
   777  		th.AddUserToChannel(user2, channel)
   778  
   779  		// Confirm that the sidebar is populated correctly to begin with
   780  		categories, resp := th.Client.GetSidebarCategoriesForTeamForUser(user.Id, team1.Id, "")
   781  		require.Nil(t, resp.Error)
   782  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   783  		require.NotContains(t, categories.Categories[0].Channels, channel.Id)
   784  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   785  		require.Contains(t, categories.Categories[1].Channels, channel.Id)
   786  
   787  		categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
   788  		require.Nil(t, resp.Error)
   789  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   790  		require.NotContains(t, categories.Categories[0].Channels, channel.Id)
   791  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   792  		require.Contains(t, categories.Categories[1].Channels, channel.Id)
   793  
   794  		// Favorite the channel for both users
   795  		_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
   796  			{
   797  				UserId:   user.Id,
   798  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   799  				Name:     channel.Id,
   800  				Value:    "true",
   801  			},
   802  		})
   803  		require.Nil(t, resp.Error)
   804  
   805  		_, resp = client2.UpdatePreferences(user2.Id, &model.Preferences{
   806  			{
   807  				UserId:   user2.Id,
   808  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   809  				Name:     channel.Id,
   810  				Value:    "true",
   811  			},
   812  		})
   813  		require.Nil(t, resp.Error)
   814  
   815  		// Confirm that the channel is in the Favorites for the second user
   816  		categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
   817  		require.Nil(t, resp.Error)
   818  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   819  		assert.Contains(t, categories.Categories[0].Channels, channel.Id)
   820  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   821  		assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
   822  
   823  		// And unfavorite the channel for the first user by deleting the preference
   824  		_, resp = th.Client.UpdatePreferences(user.Id, &model.Preferences{
   825  			{
   826  				UserId:   user.Id,
   827  				Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
   828  				Name:     channel.Id,
   829  				Value:    "false",
   830  			},
   831  		})
   832  		require.Nil(t, resp.Error)
   833  
   834  		// The channel should still be in the second user's favorites
   835  		categories, resp = client2.GetSidebarCategoriesForTeamForUser(user2.Id, team1.Id, "")
   836  		require.Nil(t, resp.Error)
   837  		require.Equal(t, model.SidebarCategoryFavorites, categories.Categories[0].Type)
   838  		assert.Contains(t, categories.Categories[0].Channels, channel.Id)
   839  		require.Equal(t, model.SidebarCategoryChannels, categories.Categories[1].Type)
   840  		assert.NotContains(t, categories.Categories[1].Channels, channel.Id)
   841  	})
   842  }