From 1d1998c6686e969a6d3fdfcdfa0592ea5945bb9c Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 7 Nov 2017 13:12:38 -0600 Subject: add a few docs for plugin testing (#7798) * add a few docs for plugin testing * fix typo --- plugin/example_test.go | 35 ++++++++++++++++++++++++++++++++ plugin/plugin.go | 6 ++++++ plugin/plugintest/plugintest.go | 45 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 plugin/example_test.go create mode 100644 plugin/plugintest/plugintest.go (limited to 'plugin') diff --git a/plugin/example_test.go b/plugin/example_test.go new file mode 100644 index 000000000..e6ae3c2ea --- /dev/null +++ b/plugin/example_test.go @@ -0,0 +1,35 @@ +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)) +} diff --git a/plugin/plugin.go b/plugin/plugin.go index b14ab7ced..3150bf56a 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -9,4 +9,10 @@ // Plugins should define a type that implements some of the methods from the Hook interface, then // pass an instance of that object into the rpcplugin package's Main function (See the HelloWorld // example.). +// +// Testing +// +// To make testing plugins easier, you can use the plugintest package to create a mock API for your +// plugin to interact with. See +// https://godoc.org/github.com/mattermost/mattermost-server/plugin/plugintest package plugin diff --git a/plugin/plugintest/plugintest.go b/plugin/plugintest/plugintest.go new file mode 100644 index 000000000..5cc8ab7e5 --- /dev/null +++ b/plugin/plugintest/plugintest.go @@ -0,0 +1,45 @@ +// 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 +package plugintest -- cgit v1.2.3-1-g7c22