github.com/turgay/mattermost-server@v5.3.2-0.20181002173352-2945e8a2b0ce+incompatible/api4/preference_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  )
    13  
    14  func TestGetPreferences(t *testing.T) {
    15  	th := Setup().InitBasic()
    16  	defer th.TearDown()
    17  	Client := th.Client
    18  
    19  	th.LoginBasic()
    20  	user1 := th.BasicUser
    21  
    22  	category := model.NewId()
    23  	preferences1 := model.Preferences{
    24  		{
    25  			UserId:   user1.Id,
    26  			Category: category,
    27  			Name:     model.NewId(),
    28  		},
    29  		{
    30  			UserId:   user1.Id,
    31  			Category: category,
    32  			Name:     model.NewId(),
    33  		},
    34  		{
    35  			UserId:   user1.Id,
    36  			Category: model.NewId(),
    37  			Name:     model.NewId(),
    38  		},
    39  	}
    40  
    41  	Client.UpdatePreferences(user1.Id, &preferences1)
    42  
    43  	prefs, resp := Client.GetPreferences(user1.Id)
    44  	CheckNoError(t, resp)
    45  	if len(prefs) != 4 {
    46  		t.Fatal("received the wrong number of preferences")
    47  	}
    48  
    49  	for _, preference := range prefs {
    50  		if preference.UserId != th.BasicUser.Id {
    51  			t.Fatal("user id does not match")
    52  		}
    53  	}
    54  
    55  	th.LoginBasic2()
    56  
    57  	prefs, resp = Client.GetPreferences(th.BasicUser2.Id)
    58  	CheckNoError(t, resp)
    59  
    60  	if len(prefs) == 0 {
    61  		t.Fatal("received the wrong number of preferences")
    62  	}
    63  
    64  	_, resp = Client.GetPreferences(th.BasicUser.Id)
    65  	CheckForbiddenStatus(t, resp)
    66  
    67  	Client.Logout()
    68  	_, resp = Client.GetPreferences(th.BasicUser2.Id)
    69  	CheckUnauthorizedStatus(t, resp)
    70  }
    71  
    72  func TestGetPreferencesByCategory(t *testing.T) {
    73  	th := Setup().InitBasic()
    74  	defer th.TearDown()
    75  	Client := th.Client
    76  
    77  	th.LoginBasic()
    78  	user1 := th.BasicUser
    79  
    80  	category := model.NewId()
    81  	preferences1 := model.Preferences{
    82  		{
    83  			UserId:   user1.Id,
    84  			Category: category,
    85  			Name:     model.NewId(),
    86  		},
    87  		{
    88  			UserId:   user1.Id,
    89  			Category: category,
    90  			Name:     model.NewId(),
    91  		},
    92  		{
    93  			UserId:   user1.Id,
    94  			Category: model.NewId(),
    95  			Name:     model.NewId(),
    96  		},
    97  	}
    98  
    99  	Client.UpdatePreferences(user1.Id, &preferences1)
   100  
   101  	prefs, resp := Client.GetPreferencesByCategory(user1.Id, category)
   102  	CheckNoError(t, resp)
   103  
   104  	if len(prefs) != 2 {
   105  		t.Fatalf("received the wrong number of preferences %v:%v", len(prefs), 2)
   106  	}
   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  	if len(prefs) != 0 {
   123  		t.Fatal("received the wrong number of preferences")
   124  	}
   125  
   126  	Client.Logout()
   127  	_, resp = Client.GetPreferencesByCategory(th.BasicUser2.Id, category)
   128  	CheckUnauthorizedStatus(t, resp)
   129  }
   130  
   131  func TestGetPreferenceByCategoryAndName(t *testing.T) {
   132  	th := Setup().InitBasic()
   133  	defer th.TearDown()
   134  	Client := th.Client
   135  
   136  	th.LoginBasic()
   137  	user := th.BasicUser
   138  	name := model.NewId()
   139  	value := model.NewId()
   140  
   141  	preferences := model.Preferences{
   142  		{
   143  			UserId:   user.Id,
   144  			Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
   145  			Name:     name,
   146  			Value:    value,
   147  		},
   148  		{
   149  			UserId:   user.Id,
   150  			Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
   151  			Name:     model.NewId(),
   152  			Value:    model.NewId(),
   153  		},
   154  	}
   155  
   156  	Client.UpdatePreferences(user.Id, &preferences)
   157  
   158  	pref, resp := Client.GetPreferenceByCategoryAndName(user.Id, model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, name)
   159  	CheckNoError(t, resp)
   160  
   161  	if (pref.UserId != preferences[0].UserId) && (pref.Category != preferences[0].Category) && (pref.Name != preferences[0].Name) {
   162  		t.Fatal("preference saved incorrectly")
   163  	}
   164  
   165  	preferences[0].Value = model.NewId()
   166  	Client.UpdatePreferences(user.Id, &preferences)
   167  
   168  	_, resp = Client.GetPreferenceByCategoryAndName(user.Id, "junk", preferences[0].Name)
   169  	CheckBadRequestStatus(t, resp)
   170  
   171  	_, resp = Client.GetPreferenceByCategoryAndName(user.Id, preferences[0].Category, "junk")
   172  	CheckBadRequestStatus(t, resp)
   173  
   174  	_, resp = Client.GetPreferenceByCategoryAndName(th.BasicUser2.Id, preferences[0].Category, "junk")
   175  	CheckForbiddenStatus(t, resp)
   176  
   177  	_, resp = Client.GetPreferenceByCategoryAndName(user.Id, preferences[0].Category, preferences[0].Name)
   178  	CheckNoError(t, resp)
   179  
   180  	Client.Logout()
   181  	_, resp = Client.GetPreferenceByCategoryAndName(user.Id, preferences[0].Category, preferences[0].Name)
   182  	CheckUnauthorizedStatus(t, resp)
   183  
   184  }
   185  
   186  func TestUpdatePreferences(t *testing.T) {
   187  	th := Setup().InitBasic()
   188  	defer th.TearDown()
   189  	Client := th.Client
   190  
   191  	th.LoginBasic()
   192  	user1 := th.BasicUser
   193  
   194  	category := model.NewId()
   195  	preferences1 := model.Preferences{
   196  		{
   197  			UserId:   user1.Id,
   198  			Category: category,
   199  			Name:     model.NewId(),
   200  		},
   201  		{
   202  			UserId:   user1.Id,
   203  			Category: category,
   204  			Name:     model.NewId(),
   205  		},
   206  		{
   207  			UserId:   user1.Id,
   208  			Category: model.NewId(),
   209  			Name:     model.NewId(),
   210  		},
   211  	}
   212  
   213  	_, resp := Client.UpdatePreferences(user1.Id, &preferences1)
   214  	CheckNoError(t, resp)
   215  
   216  	preferences := model.Preferences{
   217  		{
   218  			UserId:   model.NewId(),
   219  			Category: category,
   220  			Name:     model.NewId(),
   221  		},
   222  	}
   223  
   224  	_, resp = Client.UpdatePreferences(user1.Id, &preferences)
   225  	CheckForbiddenStatus(t, resp)
   226  
   227  	preferences = model.Preferences{
   228  		{
   229  			UserId: user1.Id,
   230  			Name:   model.NewId(),
   231  		},
   232  	}
   233  
   234  	_, resp = Client.UpdatePreferences(user1.Id, &preferences)
   235  	CheckBadRequestStatus(t, resp)
   236  
   237  	_, resp = Client.UpdatePreferences(th.BasicUser2.Id, &preferences)
   238  	CheckForbiddenStatus(t, resp)
   239  
   240  	Client.Logout()
   241  	_, resp = Client.UpdatePreferences(user1.Id, &preferences1)
   242  	CheckUnauthorizedStatus(t, resp)
   243  }
   244  
   245  func TestUpdatePreferencesWebsocket(t *testing.T) {
   246  	th := Setup().InitBasic()
   247  	defer th.TearDown()
   248  
   249  	WebSocketClient, err := th.CreateWebSocketClient()
   250  	if err != nil {
   251  		t.Fatal(err)
   252  	}
   253  
   254  	WebSocketClient.Listen()
   255  	time.Sleep(300 * time.Millisecond)
   256  	if resp := <-WebSocketClient.ResponseChannel; resp.Status != model.STATUS_OK {
   257  		t.Fatal("should have responded OK to authentication challenge")
   258  	}
   259  
   260  	userId := th.BasicUser.Id
   261  	preferences := &model.Preferences{
   262  		{
   263  			UserId:   userId,
   264  			Category: model.NewId(),
   265  			Name:     model.NewId(),
   266  		},
   267  		{
   268  			UserId:   userId,
   269  			Category: model.NewId(),
   270  			Name:     model.NewId(),
   271  		},
   272  	}
   273  	_, resp := th.Client.UpdatePreferences(userId, preferences)
   274  	CheckNoError(t, resp)
   275  
   276  	timeout := time.After(300 * time.Millisecond)
   277  
   278  	waiting := true
   279  	for waiting {
   280  		select {
   281  		case event := <-WebSocketClient.EventChannel:
   282  			if event.Event != model.WEBSOCKET_EVENT_PREFERENCES_CHANGED {
   283  				// Ignore any other events
   284  				continue
   285  			}
   286  
   287  			received, err := model.PreferencesFromJson(strings.NewReader(event.Data["preferences"].(string)))
   288  			if err != nil {
   289  				t.Fatal(err)
   290  			}
   291  
   292  			for i, preference := range *preferences {
   293  				if preference.UserId != received[i].UserId || preference.Category != received[i].Category || preference.Name != received[i].Name {
   294  					t.Fatal("received incorrect preference")
   295  				}
   296  			}
   297  
   298  			waiting = false
   299  		case <-timeout:
   300  			t.Fatal("timed out waiting for preference update event")
   301  		}
   302  	}
   303  }
   304  
   305  func TestDeletePreferences(t *testing.T) {
   306  	th := Setup().InitBasic()
   307  	defer th.TearDown()
   308  	Client := th.Client
   309  
   310  	th.LoginBasic()
   311  
   312  	prefs, _ := Client.GetPreferences(th.BasicUser.Id)
   313  	originalCount := len(prefs)
   314  
   315  	// save 10 preferences
   316  	var preferences model.Preferences
   317  	for i := 0; i < 10; i++ {
   318  		preference := model.Preference{
   319  			UserId:   th.BasicUser.Id,
   320  			Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
   321  			Name:     model.NewId(),
   322  		}
   323  		preferences = append(preferences, preference)
   324  	}
   325  
   326  	Client.UpdatePreferences(th.BasicUser.Id, &preferences)
   327  
   328  	// delete 10 preferences
   329  	th.LoginBasic2()
   330  
   331  	_, resp := Client.DeletePreferences(th.BasicUser2.Id, &preferences)
   332  	CheckForbiddenStatus(t, resp)
   333  
   334  	th.LoginBasic()
   335  
   336  	_, resp = Client.DeletePreferences(th.BasicUser.Id, &preferences)
   337  	CheckNoError(t, resp)
   338  
   339  	_, resp = Client.DeletePreferences(th.BasicUser2.Id, &preferences)
   340  	CheckForbiddenStatus(t, resp)
   341  
   342  	prefs, _ = Client.GetPreferences(th.BasicUser.Id)
   343  	if len(prefs) != originalCount {
   344  		t.Fatal("should've deleted preferences")
   345  	}
   346  
   347  	Client.Logout()
   348  	_, resp = Client.DeletePreferences(th.BasicUser.Id, &preferences)
   349  	CheckUnauthorizedStatus(t, resp)
   350  }
   351  
   352  func TestDeletePreferencesWebsocket(t *testing.T) {
   353  	th := Setup().InitBasic()
   354  	defer th.TearDown()
   355  
   356  	userId := th.BasicUser.Id
   357  	preferences := &model.Preferences{
   358  		{
   359  			UserId:   userId,
   360  			Category: model.NewId(),
   361  			Name:     model.NewId(),
   362  		},
   363  		{
   364  			UserId:   userId,
   365  			Category: model.NewId(),
   366  			Name:     model.NewId(),
   367  		},
   368  	}
   369  	_, resp := th.Client.UpdatePreferences(userId, preferences)
   370  	CheckNoError(t, resp)
   371  
   372  	WebSocketClient, err := th.CreateWebSocketClient()
   373  	if err != nil {
   374  		t.Fatal(err)
   375  	}
   376  
   377  	WebSocketClient.Listen()
   378  	time.Sleep(300 * time.Millisecond)
   379  	if resp := <-WebSocketClient.ResponseChannel; resp.Status != model.STATUS_OK {
   380  		t.Fatal("should have responded OK to authentication challenge")
   381  	}
   382  
   383  	_, resp = th.Client.DeletePreferences(userId, preferences)
   384  	CheckNoError(t, resp)
   385  
   386  	timeout := time.After(30000 * time.Millisecond)
   387  
   388  	waiting := true
   389  	for waiting {
   390  		select {
   391  		case event := <-WebSocketClient.EventChannel:
   392  			if event.Event != model.WEBSOCKET_EVENT_PREFERENCES_DELETED {
   393  				// Ignore any other events
   394  				continue
   395  			}
   396  
   397  			received, err := model.PreferencesFromJson(strings.NewReader(event.Data["preferences"].(string)))
   398  			if err != nil {
   399  				t.Fatal(err)
   400  			}
   401  
   402  			for i, preference := range *preferences {
   403  				if preference.UserId != received[i].UserId || preference.Category != received[i].Category || preference.Name != received[i].Name {
   404  					t.Fatal("received incorrect preference")
   405  				}
   406  			}
   407  
   408  			waiting = false
   409  		case <-timeout:
   410  			t.Fatal("timed out waiting for preference delete event")
   411  		}
   412  	}
   413  }