github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/preference_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/store"
    13  )
    14  
    15  func TestPreferenceStore(t *testing.T, ss store.Store) {
    16  	t.Run("PreferenceSave", func(t *testing.T) { testPreferenceSave(t, ss) })
    17  	t.Run("PreferenceGet", func(t *testing.T) { testPreferenceGet(t, ss) })
    18  	t.Run("PreferenceGetCategory", func(t *testing.T) { testPreferenceGetCategory(t, ss) })
    19  	t.Run("PreferenceGetAll", func(t *testing.T) { testPreferenceGetAll(t, ss) })
    20  	t.Run("PreferenceDeleteByUser", func(t *testing.T) { testPreferenceDeleteByUser(t, ss) })
    21  	t.Run("IsFeatureEnabled", func(t *testing.T) { testIsFeatureEnabled(t, ss) })
    22  	t.Run("PreferenceDelete", func(t *testing.T) { testPreferenceDelete(t, ss) })
    23  	t.Run("PreferenceDeleteCategory", func(t *testing.T) { testPreferenceDeleteCategory(t, ss) })
    24  	t.Run("PreferenceDeleteCategoryAndName", func(t *testing.T) { testPreferenceDeleteCategoryAndName(t, ss) })
    25  	t.Run("PreferenceCleanupFlagsBatch", func(t *testing.T) { testPreferenceCleanupFlagsBatch(t, ss) })
    26  }
    27  
    28  func testPreferenceSave(t *testing.T, ss store.Store) {
    29  	id := model.NewId()
    30  
    31  	preferences := model.Preferences{
    32  		{
    33  			UserId:   id,
    34  			Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
    35  			Name:     model.NewId(),
    36  			Value:    "value1a",
    37  		},
    38  		{
    39  			UserId:   id,
    40  			Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
    41  			Name:     model.NewId(),
    42  			Value:    "value1b",
    43  		},
    44  	}
    45  	if count := store.Must(ss.Preference().Save(&preferences)); count != 2 {
    46  		t.Fatal("got incorrect number of rows saved")
    47  	}
    48  
    49  	for _, preference := range preferences {
    50  		if data := store.Must(ss.Preference().Get(preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data {
    51  			t.Fatal("got incorrect preference after first Save")
    52  		}
    53  	}
    54  
    55  	preferences[0].Value = "value2a"
    56  	preferences[1].Value = "value2b"
    57  	if count := store.Must(ss.Preference().Save(&preferences)); count != 2 {
    58  		t.Fatal("got incorrect number of rows saved")
    59  	}
    60  
    61  	for _, preference := range preferences {
    62  		if data := store.Must(ss.Preference().Get(preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data {
    63  			t.Fatal("got incorrect preference after second Save")
    64  		}
    65  	}
    66  }
    67  
    68  func testPreferenceGet(t *testing.T, ss store.Store) {
    69  	userId := model.NewId()
    70  	category := model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
    71  	name := model.NewId()
    72  
    73  	preferences := model.Preferences{
    74  		{
    75  			UserId:   userId,
    76  			Category: category,
    77  			Name:     name,
    78  		},
    79  		{
    80  			UserId:   userId,
    81  			Category: category,
    82  			Name:     model.NewId(),
    83  		},
    84  		{
    85  			UserId:   userId,
    86  			Category: model.NewId(),
    87  			Name:     name,
    88  		},
    89  		{
    90  			UserId:   model.NewId(),
    91  			Category: category,
    92  			Name:     name,
    93  		},
    94  	}
    95  
    96  	store.Must(ss.Preference().Save(&preferences))
    97  
    98  	if result := <-ss.Preference().Get(userId, category, name); result.Err != nil {
    99  		t.Fatal(result.Err)
   100  	} else if data := result.Data.(model.Preference); data != preferences[0] {
   101  		t.Fatal("got incorrect preference")
   102  	}
   103  
   104  	// make sure getting a missing preference fails
   105  	if result := <-ss.Preference().Get(model.NewId(), model.NewId(), model.NewId()); result.Err == nil {
   106  		t.Fatal("no error on getting a missing preference")
   107  	}
   108  }
   109  
   110  func testPreferenceGetCategory(t *testing.T, ss store.Store) {
   111  	userId := model.NewId()
   112  	category := model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
   113  	name := model.NewId()
   114  
   115  	preferences := model.Preferences{
   116  		{
   117  			UserId:   userId,
   118  			Category: category,
   119  			Name:     name,
   120  		},
   121  		// same user/category, different name
   122  		{
   123  			UserId:   userId,
   124  			Category: category,
   125  			Name:     model.NewId(),
   126  		},
   127  		// same user/name, different category
   128  		{
   129  			UserId:   userId,
   130  			Category: model.NewId(),
   131  			Name:     name,
   132  		},
   133  		// same name/category, different user
   134  		{
   135  			UserId:   model.NewId(),
   136  			Category: category,
   137  			Name:     name,
   138  		},
   139  	}
   140  
   141  	store.Must(ss.Preference().Save(&preferences))
   142  
   143  	if result := <-ss.Preference().GetCategory(userId, category); result.Err != nil {
   144  		t.Fatal(result.Err)
   145  	} else if data := result.Data.(model.Preferences); len(data) != 2 {
   146  		t.Fatal("got the wrong number of preferences")
   147  	} else if !((data[0] == preferences[0] && data[1] == preferences[1]) || (data[0] == preferences[1] && data[1] == preferences[0])) {
   148  		t.Fatal("got incorrect preferences")
   149  	}
   150  
   151  	// make sure getting a missing preference category doesn't fail
   152  	if result := <-ss.Preference().GetCategory(model.NewId(), model.NewId()); result.Err != nil {
   153  		t.Fatal(result.Err)
   154  	} else if data := result.Data.(model.Preferences); len(data) != 0 {
   155  		t.Fatal("shouldn't have got any preferences")
   156  	}
   157  }
   158  
   159  func testPreferenceGetAll(t *testing.T, ss store.Store) {
   160  	userId := model.NewId()
   161  	category := model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
   162  	name := model.NewId()
   163  
   164  	preferences := model.Preferences{
   165  		{
   166  			UserId:   userId,
   167  			Category: category,
   168  			Name:     name,
   169  		},
   170  		// same user/category, different name
   171  		{
   172  			UserId:   userId,
   173  			Category: category,
   174  			Name:     model.NewId(),
   175  		},
   176  		// same user/name, different category
   177  		{
   178  			UserId:   userId,
   179  			Category: model.NewId(),
   180  			Name:     name,
   181  		},
   182  		// same name/category, different user
   183  		{
   184  			UserId:   model.NewId(),
   185  			Category: category,
   186  			Name:     name,
   187  		},
   188  	}
   189  
   190  	store.Must(ss.Preference().Save(&preferences))
   191  
   192  	if result := <-ss.Preference().GetAll(userId); result.Err != nil {
   193  		t.Fatal(result.Err)
   194  	} else if data := result.Data.(model.Preferences); len(data) != 3 {
   195  		t.Fatal("got the wrong number of preferences")
   196  	} else {
   197  		for i := 0; i < 3; i++ {
   198  			if data[0] != preferences[i] && data[1] != preferences[i] && data[2] != preferences[i] {
   199  				t.Fatal("got incorrect preferences")
   200  			}
   201  		}
   202  	}
   203  }
   204  
   205  func testPreferenceDeleteByUser(t *testing.T, ss store.Store) {
   206  	userId := model.NewId()
   207  	category := model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
   208  	name := model.NewId()
   209  
   210  	preferences := model.Preferences{
   211  		{
   212  			UserId:   userId,
   213  			Category: category,
   214  			Name:     name,
   215  		},
   216  		// same user/category, different name
   217  		{
   218  			UserId:   userId,
   219  			Category: category,
   220  			Name:     model.NewId(),
   221  		},
   222  		// same user/name, different category
   223  		{
   224  			UserId:   userId,
   225  			Category: model.NewId(),
   226  			Name:     name,
   227  		},
   228  		// same name/category, different user
   229  		{
   230  			UserId:   model.NewId(),
   231  			Category: category,
   232  			Name:     name,
   233  		},
   234  	}
   235  
   236  	store.Must(ss.Preference().Save(&preferences))
   237  
   238  	if result := <-ss.Preference().PermanentDeleteByUser(userId); result.Err != nil {
   239  		t.Fatal(result.Err)
   240  	}
   241  }
   242  
   243  func testIsFeatureEnabled(t *testing.T, ss store.Store) {
   244  	feature1 := "testFeat1"
   245  	feature2 := "testFeat2"
   246  	feature3 := "testFeat3"
   247  
   248  	userId := model.NewId()
   249  	category := model.PREFERENCE_CATEGORY_ADVANCED_SETTINGS
   250  
   251  	features := model.Preferences{
   252  		{
   253  			UserId:   userId,
   254  			Category: category,
   255  			Name:     store.FEATURE_TOGGLE_PREFIX + feature1,
   256  			Value:    "true",
   257  		},
   258  		{
   259  			UserId:   userId,
   260  			Category: category,
   261  			Name:     model.NewId(),
   262  			Value:    "false",
   263  		},
   264  		{
   265  			UserId:   userId,
   266  			Category: model.NewId(),
   267  			Name:     store.FEATURE_TOGGLE_PREFIX + feature1,
   268  			Value:    "false",
   269  		},
   270  		{
   271  			UserId:   model.NewId(),
   272  			Category: category,
   273  			Name:     store.FEATURE_TOGGLE_PREFIX + feature2,
   274  			Value:    "false",
   275  		},
   276  		{
   277  			UserId:   model.NewId(),
   278  			Category: category,
   279  			Name:     store.FEATURE_TOGGLE_PREFIX + feature3,
   280  			Value:    "foobar",
   281  		},
   282  	}
   283  
   284  	store.Must(ss.Preference().Save(&features))
   285  
   286  	if result := <-ss.Preference().IsFeatureEnabled(feature1, userId); result.Err != nil {
   287  		t.Fatal(result.Err)
   288  	} else if data := result.Data.(bool); !data {
   289  		t.Fatalf("got incorrect setting for feature1, %v=%v", true, data)
   290  	}
   291  
   292  	if result := <-ss.Preference().IsFeatureEnabled(feature2, userId); result.Err != nil {
   293  		t.Fatal(result.Err)
   294  	} else if data := result.Data.(bool); data {
   295  		t.Fatalf("got incorrect setting for feature2, %v=%v", false, data)
   296  	}
   297  
   298  	// make sure we get false if something different than "true" or "false" has been saved to database
   299  	if result := <-ss.Preference().IsFeatureEnabled(feature3, userId); result.Err != nil {
   300  		t.Fatal(result.Err)
   301  	} else if data := result.Data.(bool); data {
   302  		t.Fatalf("got incorrect setting for feature3, %v=%v", false, data)
   303  	}
   304  
   305  	// make sure false is returned if a non-existent feature is queried
   306  	if result := <-ss.Preference().IsFeatureEnabled("someOtherFeature", userId); result.Err != nil {
   307  		t.Fatal(result.Err)
   308  	} else if data := result.Data.(bool); data {
   309  		t.Fatalf("got incorrect setting for non-existent feature 'someOtherFeature', %v=%v", false, data)
   310  	}
   311  }
   312  
   313  func testPreferenceDelete(t *testing.T, ss store.Store) {
   314  	preference := model.Preference{
   315  		UserId:   model.NewId(),
   316  		Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
   317  		Name:     model.NewId(),
   318  		Value:    "value1a",
   319  	}
   320  
   321  	store.Must(ss.Preference().Save(&model.Preferences{preference}))
   322  
   323  	if prefs := store.Must(ss.Preference().GetAll(preference.UserId)).(model.Preferences); len([]model.Preference(prefs)) != 1 {
   324  		t.Fatal("should've returned 1 preference")
   325  	}
   326  
   327  	if result := <-ss.Preference().Delete(preference.UserId, preference.Category, preference.Name); result.Err != nil {
   328  		t.Fatal(result.Err)
   329  	}
   330  
   331  	if prefs := store.Must(ss.Preference().GetAll(preference.UserId)).(model.Preferences); len([]model.Preference(prefs)) != 0 {
   332  		t.Fatal("should've returned no preferences")
   333  	}
   334  }
   335  
   336  func testPreferenceDeleteCategory(t *testing.T, ss store.Store) {
   337  	category := model.NewId()
   338  	userId := model.NewId()
   339  
   340  	preference1 := model.Preference{
   341  		UserId:   userId,
   342  		Category: category,
   343  		Name:     model.NewId(),
   344  		Value:    "value1a",
   345  	}
   346  
   347  	preference2 := model.Preference{
   348  		UserId:   userId,
   349  		Category: category,
   350  		Name:     model.NewId(),
   351  		Value:    "value1a",
   352  	}
   353  
   354  	store.Must(ss.Preference().Save(&model.Preferences{preference1, preference2}))
   355  
   356  	if prefs := store.Must(ss.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 2 {
   357  		t.Fatal("should've returned 2 preferences")
   358  	}
   359  
   360  	if result := <-ss.Preference().DeleteCategory(userId, category); result.Err != nil {
   361  		t.Fatal(result.Err)
   362  	}
   363  
   364  	if prefs := store.Must(ss.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 0 {
   365  		t.Fatal("should've returned no preferences")
   366  	}
   367  }
   368  
   369  func testPreferenceDeleteCategoryAndName(t *testing.T, ss store.Store) {
   370  	category := model.NewId()
   371  	name := model.NewId()
   372  	userId := model.NewId()
   373  	userId2 := model.NewId()
   374  
   375  	preference1 := model.Preference{
   376  		UserId:   userId,
   377  		Category: category,
   378  		Name:     name,
   379  		Value:    "value1a",
   380  	}
   381  
   382  	preference2 := model.Preference{
   383  		UserId:   userId2,
   384  		Category: category,
   385  		Name:     name,
   386  		Value:    "value1a",
   387  	}
   388  
   389  	store.Must(ss.Preference().Save(&model.Preferences{preference1, preference2}))
   390  
   391  	if prefs := store.Must(ss.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 1 {
   392  		t.Fatal("should've returned 1 preference")
   393  	}
   394  
   395  	if prefs := store.Must(ss.Preference().GetAll(userId2)).(model.Preferences); len([]model.Preference(prefs)) != 1 {
   396  		t.Fatal("should've returned 1 preference")
   397  	}
   398  
   399  	if result := <-ss.Preference().DeleteCategoryAndName(category, name); result.Err != nil {
   400  		t.Fatal(result.Err)
   401  	}
   402  
   403  	if prefs := store.Must(ss.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 0 {
   404  		t.Fatal("should've returned no preferences")
   405  	}
   406  
   407  	if prefs := store.Must(ss.Preference().GetAll(userId2)).(model.Preferences); len([]model.Preference(prefs)) != 0 {
   408  		t.Fatal("should've returned no preferences")
   409  	}
   410  }
   411  
   412  func testPreferenceCleanupFlagsBatch(t *testing.T, ss store.Store) {
   413  	category := model.PREFERENCE_CATEGORY_FLAGGED_POST
   414  	userId := model.NewId()
   415  
   416  	o1 := &model.Post{}
   417  	o1.ChannelId = model.NewId()
   418  	o1.UserId = userId
   419  	o1.Message = "zz" + model.NewId() + "AAAAAAAAAAA"
   420  	o1.CreateAt = 1000
   421  	o1 = (<-ss.Post().Save(o1)).Data.(*model.Post)
   422  
   423  	preference1 := model.Preference{
   424  		UserId:   userId,
   425  		Category: category,
   426  		Name:     o1.Id,
   427  		Value:    "true",
   428  	}
   429  
   430  	preference2 := model.Preference{
   431  		UserId:   userId,
   432  		Category: category,
   433  		Name:     model.NewId(),
   434  		Value:    "true",
   435  	}
   436  
   437  	store.Must(ss.Preference().Save(&model.Preferences{preference1, preference2}))
   438  
   439  	result := <-ss.Preference().CleanupFlagsBatch(10000)
   440  	assert.Nil(t, result.Err)
   441  
   442  	result = <-ss.Preference().Get(userId, category, preference1.Name)
   443  	assert.Nil(t, result.Err)
   444  
   445  	result = <-ss.Preference().Get(userId, category, preference2.Name)
   446  	assert.NotNil(t, result.Err)
   447  }