summaryrefslogtreecommitdiffstats
path: root/plugin/plugintest/example_hello_user_test.go
diff options
context:
space:
mode:
authorcpanato <ctadeu@gmail.com>2018-08-08 13:53:10 +0200
committercpanato <ctadeu@gmail.com>2018-08-08 13:53:10 +0200
commitae891321ad2e4b07e4f77b252379cb998607c687 (patch)
treede7cfa9b9fb1c0d7da2164e42e8516f62c7aaff1 /plugin/plugintest/example_hello_user_test.go
parent99cf15b56eb561dc30def86ad7e3cd97af4c338c (diff)
parent5fbec91c35d7ea5d9b920b26a01fc21da55bb08e (diff)
downloadchat-ae891321ad2e4b07e4f77b252379cb998607c687.tar.gz
chat-ae891321ad2e4b07e4f77b252379cb998607c687.tar.bz2
chat-ae891321ad2e4b07e4f77b252379cb998607c687.zip
Merge remote-tracking branch 'upstream/release-5.2' into release-5.2-daily-merge-20180808
Diffstat (limited to 'plugin/plugintest/example_hello_user_test.go')
-rw-r--r--plugin/plugintest/example_hello_user_test.go55
1 files changed, 55 insertions, 0 deletions
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))
+}