summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorShobhit Gupta <smartyshobhit@gmail.com>2018-10-03 13:04:37 -0700
committerJoram Wilander <jwawilander@gmail.com>2018-10-03 16:04:37 -0400
commit8c03e584c182218c84bebc8af23c70fb0cd203d4 (patch)
tree90cad75d922dc3d9fb25c4456b342b2b4e642d3b /app
parent7468f35cb0c86b60f66e1c9228a60da244d3fcc4 (diff)
downloadchat-8c03e584c182218c84bebc8af23c70fb0cd203d4.tar.gz
chat-8c03e584c182218c84bebc8af23c70fb0cd203d4.tar.bz2
chat-8c03e584c182218c84bebc8af23c70fb0cd203d4.zip
MM-11863 Add KVList method (#9467)
* Add KVList method * Add KVList method Add KVList method * Add pagination support * Change offset, limit to page, perPage * Rename constant
Diffstat (limited to 'app')
-rw-r--r--app/plugin_api.go4
-rw-r--r--app/plugin_key_value_store.go11
-rw-r--r--app/plugin_test.go42
3 files changed, 57 insertions, 0 deletions
diff --git a/app/plugin_api.go b/app/plugin_api.go
index 20946cc52..fed4ad027 100644
--- a/app/plugin_api.go
+++ b/app/plugin_api.go
@@ -331,6 +331,10 @@ func (api *PluginAPI) KVDelete(key string) *model.AppError {
return api.app.DeletePluginKey(api.id, key)
}
+func (api *PluginAPI) KVList(page, perPage int) ([]string, *model.AppError) {
+ return api.app.ListPluginKeys(api.id, page, perPage)
+}
+
func (api *PluginAPI) PublishWebSocketEvent(event string, payload map[string]interface{}, broadcast *model.WebsocketBroadcast) {
api.app.Publish(&model.WebSocketEvent{
Event: fmt.Sprintf("custom_%v_%v", api.id, event),
diff --git a/app/plugin_key_value_store.go b/app/plugin_key_value_store.go
index bf2a46004..8c3e1f18b 100644
--- a/app/plugin_key_value_store.go
+++ b/app/plugin_key_value_store.go
@@ -59,3 +59,14 @@ func (a *App) DeletePluginKey(pluginId string, key string) *model.AppError {
return result.Err
}
+
+func (a *App) ListPluginKeys(pluginId string, page, perPage int) ([]string, *model.AppError) {
+ result := <-a.Srv.Store.Plugin().List(pluginId, page, perPage)
+
+ if result.Err != nil {
+ mlog.Error(result.Err.Error())
+ return nil, result.Err
+ }
+
+ return result.Data.([]string), nil
+}
diff --git a/app/plugin_test.go b/app/plugin_test.go
index c051324a7..8dcae9b16 100644
--- a/app/plugin_test.go
+++ b/app/plugin_test.go
@@ -5,6 +5,8 @@ package app
import (
"bytes"
+ "crypto/sha256"
+ "encoding/base64"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -17,6 +19,11 @@ import (
"github.com/stretchr/testify/require"
)
+func getHashedKey(key string) string {
+ hash := sha256.New()
+ hash.Write([]byte(key))
+ return base64.StdEncoding.EncodeToString(hash.Sum(nil))
+}
func TestPluginKeyValueStore(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
@@ -40,6 +47,41 @@ func TestPluginKeyValueStore(t *testing.T) {
assert.Nil(t, th.App.DeletePluginKey(pluginId, "intkey"))
assert.Nil(t, th.App.DeletePluginKey(pluginId, "postkey"))
assert.Nil(t, th.App.DeletePluginKey(pluginId, "notrealkey"))
+
+ // Test ListKeys
+ assert.Nil(t, th.App.SetPluginKey(pluginId, "key2", []byte("test")))
+ hashedKey := getHashedKey("key")
+ hashedKey2 := getHashedKey("key2")
+ list, err := th.App.ListPluginKeys(pluginId, 0, 1)
+ assert.Nil(t, err)
+ assert.Equal(t, 1, len(list))
+ assert.Equal(t, hashedKey, list[0])
+
+ list, err = th.App.ListPluginKeys(pluginId, 1, 1)
+ assert.Nil(t, err)
+ assert.Equal(t, 1, len(list))
+ assert.Equal(t, hashedKey2, list[0])
+
+ //List Keys bad input
+ list, err = th.App.ListPluginKeys(pluginId, 0, 0)
+ assert.Nil(t, err)
+ assert.Equal(t, 2, len(list))
+
+ list, err = th.App.ListPluginKeys(pluginId, 0, -1)
+ assert.Nil(t, err)
+ assert.Equal(t, 2, len(list))
+
+ list, err = th.App.ListPluginKeys(pluginId, -1, 1)
+ assert.Nil(t, err)
+ assert.Equal(t, 1, len(list))
+
+ list, err = th.App.ListPluginKeys(pluginId, -1, 0)
+ assert.Nil(t, err)
+ assert.Equal(t, 2, len(list))
+
+ list, err = th.App.ListPluginKeys(pluginId, 2, 2)
+ assert.Nil(t, err)
+ assert.Equal(t, 0, len(list))
}
func TestServePluginRequest(t *testing.T) {