github.com/nhannv/mattermost-server@v5.11.1+incompatible/store/storetest/plugin_store.go (about)

     1  // Copyright (c) 2017-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/mattermost/mattermost-server/model"
    10  	"github.com/mattermost/mattermost-server/store"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestPluginStore(t *testing.T, ss store.Store) {
    15  	t.Run("PluginSaveGet", func(t *testing.T) { testPluginSaveGet(t, ss) })
    16  	t.Run("PluginSaveGetExpiry", func(t *testing.T) { testPluginSaveGetExpiry(t, ss) })
    17  	t.Run("PluginDelete", func(t *testing.T) { testPluginDelete(t, ss) })
    18  	t.Run("PluginDeleteAll", func(t *testing.T) { testPluginDeleteAll(t, ss) })
    19  	t.Run("PluginDeleteExpired", func(t *testing.T) { testPluginDeleteExpired(t, ss) })
    20  }
    21  
    22  func testPluginSaveGet(t *testing.T, ss store.Store) {
    23  	kv := &model.PluginKeyValue{
    24  		PluginId: model.NewId(),
    25  		Key:      model.NewId(),
    26  		Value:    []byte(model.NewId()),
    27  		ExpireAt: 0,
    28  	}
    29  
    30  	if result := <-ss.Plugin().SaveOrUpdate(kv); result.Err != nil {
    31  		t.Fatal(result.Err)
    32  	}
    33  
    34  	defer func() {
    35  		<-ss.Plugin().Delete(kv.PluginId, kv.Key)
    36  	}()
    37  
    38  	if result := <-ss.Plugin().Get(kv.PluginId, kv.Key); result.Err != nil {
    39  		t.Fatal(result.Err)
    40  	} else {
    41  		received := result.Data.(*model.PluginKeyValue)
    42  		assert.Equal(t, kv.PluginId, received.PluginId)
    43  		assert.Equal(t, kv.Key, received.Key)
    44  		assert.Equal(t, kv.Value, received.Value)
    45  		assert.Equal(t, kv.ExpireAt, received.ExpireAt)
    46  	}
    47  
    48  	// Try inserting when already exists
    49  	kv.Value = []byte(model.NewId())
    50  	if result := <-ss.Plugin().SaveOrUpdate(kv); result.Err != nil {
    51  		t.Fatal(result.Err)
    52  	}
    53  
    54  	if result := <-ss.Plugin().Get(kv.PluginId, kv.Key); result.Err != nil {
    55  		t.Fatal(result.Err)
    56  	} else {
    57  		received := result.Data.(*model.PluginKeyValue)
    58  		assert.Equal(t, kv.PluginId, received.PluginId)
    59  		assert.Equal(t, kv.Key, received.Key)
    60  		assert.Equal(t, kv.Value, received.Value)
    61  	}
    62  }
    63  
    64  func testPluginSaveGetExpiry(t *testing.T, ss store.Store) {
    65  	kv := &model.PluginKeyValue{
    66  		PluginId: model.NewId(),
    67  		Key:      model.NewId(),
    68  		Value:    []byte(model.NewId()),
    69  		ExpireAt: model.GetMillis() + 30000,
    70  	}
    71  
    72  	if result := <-ss.Plugin().SaveOrUpdate(kv); result.Err != nil {
    73  		t.Fatal(result.Err)
    74  	}
    75  
    76  	defer func() {
    77  		<-ss.Plugin().Delete(kv.PluginId, kv.Key)
    78  	}()
    79  
    80  	if result := <-ss.Plugin().Get(kv.PluginId, kv.Key); result.Err != nil {
    81  		t.Fatal(result.Err)
    82  	} else {
    83  		received := result.Data.(*model.PluginKeyValue)
    84  		assert.Equal(t, kv.PluginId, received.PluginId)
    85  		assert.Equal(t, kv.Key, received.Key)
    86  		assert.Equal(t, kv.Value, received.Value)
    87  		assert.Equal(t, kv.ExpireAt, received.ExpireAt)
    88  	}
    89  
    90  	kv = &model.PluginKeyValue{
    91  		PluginId: model.NewId(),
    92  		Key:      model.NewId(),
    93  		Value:    []byte(model.NewId()),
    94  		ExpireAt: model.GetMillis() - 5000,
    95  	}
    96  
    97  	if result := <-ss.Plugin().SaveOrUpdate(kv); result.Err != nil {
    98  		t.Fatal(result.Err)
    99  	}
   100  
   101  	defer func() {
   102  		<-ss.Plugin().Delete(kv.PluginId, kv.Key)
   103  	}()
   104  
   105  	if result := <-ss.Plugin().Get(kv.PluginId, kv.Key); result.Err == nil {
   106  		t.Fatal("result.Err should not be nil")
   107  	}
   108  }
   109  
   110  func testPluginDelete(t *testing.T, ss store.Store) {
   111  	kv := store.Must(ss.Plugin().SaveOrUpdate(&model.PluginKeyValue{
   112  		PluginId: model.NewId(),
   113  		Key:      model.NewId(),
   114  		Value:    []byte(model.NewId()),
   115  	})).(*model.PluginKeyValue)
   116  
   117  	if result := <-ss.Plugin().Delete(kv.PluginId, kv.Key); result.Err != nil {
   118  		t.Fatal(result.Err)
   119  	}
   120  }
   121  
   122  func testPluginDeleteAll(t *testing.T, ss store.Store) {
   123  	pluginId := model.NewId()
   124  
   125  	kv := store.Must(ss.Plugin().SaveOrUpdate(&model.PluginKeyValue{
   126  		PluginId: pluginId,
   127  		Key:      model.NewId(),
   128  		Value:    []byte(model.NewId()),
   129  	})).(*model.PluginKeyValue)
   130  
   131  	kv2 := store.Must(ss.Plugin().SaveOrUpdate(&model.PluginKeyValue{
   132  		PluginId: pluginId,
   133  		Key:      model.NewId(),
   134  		Value:    []byte(model.NewId()),
   135  	})).(*model.PluginKeyValue)
   136  
   137  	if result := <-ss.Plugin().DeleteAllForPlugin(pluginId); result.Err != nil {
   138  		t.Fatal(result.Err)
   139  	}
   140  
   141  	if result := <-ss.Plugin().Get(pluginId, kv.Key); result.Err == nil {
   142  		t.Fatal("result.Err should not be nil")
   143  	}
   144  
   145  	if result := <-ss.Plugin().Get(pluginId, kv2.Key); result.Err == nil {
   146  		t.Fatal("result.Err should not be nil")
   147  	}
   148  }
   149  
   150  func testPluginDeleteExpired(t *testing.T, ss store.Store) {
   151  	pluginId := model.NewId()
   152  
   153  	kv := store.Must(ss.Plugin().SaveOrUpdate(&model.PluginKeyValue{
   154  		PluginId: pluginId,
   155  		Key:      model.NewId(),
   156  		Value:    []byte(model.NewId()),
   157  		ExpireAt: model.GetMillis() - 6000,
   158  	})).(*model.PluginKeyValue)
   159  
   160  	kv2 := store.Must(ss.Plugin().SaveOrUpdate(&model.PluginKeyValue{
   161  		PluginId: pluginId,
   162  		Key:      model.NewId(),
   163  		Value:    []byte(model.NewId()),
   164  		ExpireAt: 0,
   165  	})).(*model.PluginKeyValue)
   166  
   167  	if result := <-ss.Plugin().DeleteAllExpired(); result.Err != nil {
   168  		t.Fatal(result.Err)
   169  	}
   170  
   171  	if result := <-ss.Plugin().Get(pluginId, kv.Key); result.Err == nil {
   172  		t.Fatal("result.Err should not be nil")
   173  	}
   174  
   175  	if result := <-ss.Plugin().Get(kv2.PluginId, kv2.Key); result.Err != nil {
   176  		t.Fatal(result.Err)
   177  	} else {
   178  		received := result.Data.(*model.PluginKeyValue)
   179  		assert.Equal(t, kv2.PluginId, received.PluginId)
   180  		assert.Equal(t, kv2.Key, received.Key)
   181  		assert.Equal(t, kv2.Value, received.Value)
   182  		assert.Equal(t, kv2.ExpireAt, received.ExpireAt)
   183  	}
   184  }