summaryrefslogtreecommitdiffstats
path: root/plugin
diff options
context:
space:
mode:
authorJesse Hallam <jesse.hallam@gmail.com>2018-08-08 06:07:38 -0400
committerCarlos Tadeu Panato Junior <ctadeu@gmail.com>2018-08-08 12:07:38 +0200
commit7475cd260a3aba46e0b7524656b42209c1779c22 (patch)
tree376cf571b6453800465ffa0ab03e5e44da5de02b /plugin
parent6bf09e2c34c568e3cb0d296142d5abed77332635 (diff)
downloadchat-7475cd260a3aba46e0b7524656b42209c1779c22.tar.gz
chat-7475cd260a3aba46e0b7524656b42209c1779c22.tar.bz2
chat-7475cd260a3aba46e0b7524656b42209c1779c22.zip
fix plugintest example and docs (#9213)
Diffstat (limited to 'plugin')
-rw-r--r--plugin/plugintest/doc.go11
-rw-r--r--plugin/plugintest/example_hello_user_test.go55
-rw-r--r--plugin/plugintest/plugintest.go48
3 files changed, 66 insertions, 48 deletions
diff --git a/plugin/plugintest/doc.go b/plugin/plugintest/doc.go
new file mode 100644
index 000000000..69a51bd39
--- /dev/null
+++ b/plugin/plugintest/doc.go
@@ -0,0 +1,11 @@
+// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+// The plugintest package provides mocks that can be used to test plugins.
+//
+// The mocks are created using testify's mock package:
+// https://godoc.org/github.com/stretchr/testify/mock
+//
+// If you need to import the mock package, you can import it with
+// "github.com/mattermost/mattermost-server/plugin/plugintest/mock".
+package plugintest
diff --git a/plugin/plugintest/example_hello_user_test.go b/plugin/plugintest/example_hello_user_test.go
new file mode 100644
index 000000000..3a12f292c
--- /dev/null
+++ b/plugin/plugintest/example_hello_user_test.go
@@ -0,0 +1,55 @@
+package plugintest_test
+
+import (
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "github.com/mattermost/mattermost-server/model"
+ "github.com/mattermost/mattermost-server/plugin"
+ "github.com/mattermost/mattermost-server/plugin/plugintest"
+)
+
+type HelloUserPlugin struct {
+ plugin.MattermostPlugin
+}
+
+func (p *HelloUserPlugin) ServeHTTP(context *plugin.Context, w http.ResponseWriter, r *http.Request) {
+ userId := r.Header.Get("Mattermost-User-Id")
+ user, err := p.API.GetUser(userId)
+ if err != nil {
+ w.WriteHeader(http.StatusBadRequest)
+ p.API.LogError(err.Error())
+ return
+ }
+
+ fmt.Fprintf(w, "Welcome back, %s!", user.Username)
+}
+
+func Example() {
+ t := &testing.T{}
+ user := &model.User{
+ Id: model.NewId(),
+ Username: "billybob",
+ }
+
+ api := &plugintest.API{}
+ api.On("GetUser", user.Id).Return(user, nil)
+ defer api.AssertExpectations(t)
+
+ p := &HelloUserPlugin{}
+ p.SetAPI(api)
+
+ w := httptest.NewRecorder()
+ r := httptest.NewRequest("GET", "/", nil)
+ r.Header.Add("Mattermost-User-Id", user.Id)
+ p.ServeHTTP(&plugin.Context{}, w, r)
+ body, err := ioutil.ReadAll(w.Result().Body)
+ require.NoError(t, err)
+ assert.Equal(t, "Welcome back, billybob!", string(body))
+}
diff --git a/plugin/plugintest/plugintest.go b/plugin/plugintest/plugintest.go
deleted file mode 100644
index 0275edd84..000000000
--- a/plugin/plugintest/plugintest.go
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
-// See License.txt for license information.
-
-// The plugintest package provides mocks that can be used to test plugins. For example, to test the
-// ServeHTTP method of the plugin package's HelloUser example:
-//
-// package plugin_test
-//
-// import (
-// "io/ioutil"
-// "net/http/httptest"
-// "testing"
-//
-// "github.com/stretchr/testify/assert"
-// "github.com/stretchr/testify/require"
-//
-// "github.com/mattermost/mattermost-server/model"
-// "github.com/mattermost/mattermost-server/plugin/plugintest"
-// )
-//
-// func TestHelloUserPlugin(t *testing.T) {
-// user := &model.User{
-// Id: model.NewId(),
-// Username: "billybob",
-// }
-//
-// api := &plugintest.API{}
-// api.On("GetUser", user.Id).Return(user, nil)
-// defer api.AssertExpectations(t)
-//
-// p := &HelloUserPlugin{}
-// p.OnActivate(api)
-//
-// w := httptest.NewRecorder()
-// r := httptest.NewRequest("GET", "/", nil)
-// r.Header.Add("Mattermost-User-Id", user.Id)
-// p.ServeHTTP(w, r)
-// body, err := ioutil.ReadAll(w.Result().Body)
-// require.NoError(t, err)
-// assert.Equal(t, "Welcome back, billybob!", string(body))
-// }
-//
-// The mocks are created using testify's mock package:
-// https://godoc.org/github.com/stretchr/testify/mock
-//
-// If you need to import the mock package, you can import it with
-// "github.com/mattermost/mattermost-server/plugin/plugintest/mock".
-package plugintest