MM-14559 Rewrite existing plugin API tests to exclusively test via actual plugin (#10494)
* initial work on rewriting test cases using the raw literal format * split out the embedded tests into separate file and implement the test using a generic tester function * initial work on rewriting test cases using the raw literal format * split out the embedded tests into separate file and implement the test using a generic tester function * moved tests to separate dir to avoid conflicts * removed duplicate files * added license header * fixed plugin paths * moved all trivial tests to separate files created "meta-test" that scans a folder of api tests and runs them * remove rpc tests from make * use fileutil.FindDir instead of pwd clean up the test files
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
d14235b4e0
Коммит
0e534c8a49
@@ -0,0 +1,46 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type configuration struct {
|
||||
MyStringSetting string
|
||||
MyIntSetting int
|
||||
MyBoolSetting bool
|
||||
}
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
|
||||
configuration configuration
|
||||
}
|
||||
|
||||
func (p *MyPlugin) OnConfigurationChange() error {
|
||||
if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
if p.configuration.MyStringSetting != "override" {
|
||||
return nil, "MyStringSetting has invalid value"
|
||||
}
|
||||
if p.configuration.MyIntSetting != 35 {
|
||||
return nil, "MyIntSetting has invalid value"
|
||||
}
|
||||
if p.configuration.MyBoolSetting != true {
|
||||
return nil, "MyBoolSetting has invalid value"
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
48
tests/plugin_tests/manual.test_load_configuration_plugin/main.go
Обычный файл
48
tests/plugin_tests/manual.test_load_configuration_plugin/main.go
Обычный файл
@@ -0,0 +1,48 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type configuration struct {
|
||||
MyStringSetting string
|
||||
MyIntSetting int
|
||||
MyBoolSetting bool
|
||||
}
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
|
||||
configuration configuration
|
||||
}
|
||||
|
||||
func (p *MyPlugin) OnConfigurationChange() error {
|
||||
if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
if p.configuration.MyStringSetting != "str" {
|
||||
return nil, "MyStringSetting has invalid value"
|
||||
}
|
||||
if p.configuration.MyIntSetting != 32 {
|
||||
return nil, fmt.Sprintf("MyIntSetting has invalid value %v != %v", p.configuration.MyIntSetting, 32)
|
||||
}
|
||||
if p.configuration.MyBoolSetting != true {
|
||||
return nil, "MyBoolSetting has invalid value"
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
117
tests/plugin_tests/test_bots_plugin/main.go
Обычный файл
117
tests/plugin_tests/test_bots_plugin/main.go
Обычный файл
@@ -0,0 +1,117 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
createdBot, err := p.API.CreateBot(&model.Bot{
|
||||
Username: "bot",
|
||||
Description: "a plugin bot",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err.Error() + "failed to create bot"
|
||||
}
|
||||
|
||||
fetchedBot, err := p.API.GetBot(createdBot.UserId, false)
|
||||
if err != nil {
|
||||
return nil, err.Error() + "failed to get bot"
|
||||
}
|
||||
if fetchedBot.Description != "a plugin bot" {
|
||||
return nil, "GetBot did not return the expected bot Description"
|
||||
}
|
||||
if fetchedBot.OwnerId != "test_bots_plugin" {
|
||||
return nil, "GetBot did not return the expected bot OwnerId"
|
||||
}
|
||||
|
||||
updatedDescription := createdBot.Description + ", updated"
|
||||
patchedBot, err := p.API.PatchBot(createdBot.UserId, &model.BotPatch{
|
||||
Description: &updatedDescription,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err.Error() + "failed to patch bot"
|
||||
}
|
||||
|
||||
fetchedBot, err = p.API.GetBot(patchedBot.UserId, false)
|
||||
if err != nil {
|
||||
return nil, err.Error() + "failed to get bot"
|
||||
}
|
||||
|
||||
if fetchedBot.UserId != patchedBot.UserId {
|
||||
return nil, "GetBot did not return the expected bot"
|
||||
}
|
||||
if fetchedBot.Description != "a plugin bot, updated" {
|
||||
return nil, "GetBot did not return the updated bot Description"
|
||||
}
|
||||
|
||||
fetchedBots, err := p.API.GetBots(&model.BotGetOptions{
|
||||
Page: 0,
|
||||
PerPage: 1,
|
||||
OwnerId: "",
|
||||
IncludeDeleted: false,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err.Error() + "failed to get bots"
|
||||
}
|
||||
|
||||
if len(fetchedBots) != 1 {
|
||||
return nil, "GetBots did not return a single bot"
|
||||
}
|
||||
|
||||
if fetchedBot.UserId != fetchedBots[0].UserId {
|
||||
return nil, "GetBots did not return the expected bot"
|
||||
}
|
||||
if _, err = p.API.UpdateBotActive(fetchedBot.UserId, false); err != nil {
|
||||
return nil, err.Error() + "failed to disable bot"
|
||||
}
|
||||
if fetchedBot, err = p.API.GetBot(patchedBot.UserId, false); err == nil {
|
||||
return nil, "expected not to find disabled bot"
|
||||
}
|
||||
if _, err = p.API.UpdateBotActive(fetchedBot.UserId, true); err != nil {
|
||||
return nil, err.Error() + "failed to disable bot"
|
||||
}
|
||||
if fetchedBot, err = p.API.GetBot(patchedBot.UserId, false); err != nil {
|
||||
return nil, err.Error() + "failed to get bot after enabling"
|
||||
}
|
||||
if fetchedBot.UserId != patchedBot.UserId {
|
||||
return nil, "GetBot did not return the expected bot after enabling"
|
||||
}
|
||||
if err = p.API.PermanentDeleteBot(patchedBot.UserId); err != nil {
|
||||
return nil, err.Error() + "failed to delete bot"
|
||||
}
|
||||
|
||||
if _, err = p.API.GetBot(patchedBot.UserId, false); err == nil {
|
||||
return nil, err.Error() + "found bot after permanently deleting"
|
||||
}
|
||||
createdBotWithOverriddenCreator, err := p.API.CreateBot(&model.Bot{
|
||||
Username: "bot",
|
||||
Description: "a plugin bot",
|
||||
OwnerId: "abc123",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err.Error() + "failed to create bot with overridden creator"
|
||||
}
|
||||
if fetchedBot, err = p.API.GetBot(createdBotWithOverriddenCreator.UserId, false); err != nil {
|
||||
return nil, err.Error() + "failed to get bot"
|
||||
}
|
||||
if fetchedBot.Description != "a plugin bot" {
|
||||
return nil, "GetBot did not return the expected bot Description"
|
||||
}
|
||||
if fetchedBot.OwnerId != "abc123" {
|
||||
return nil, "GetBot did not return the expected bot OwnerId"
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
31
tests/plugin_tests/test_get_bundle_path_plugin/main.go
Обычный файл
31
tests/plugin_tests/test_get_bundle_path_plugin/main.go
Обычный файл
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
bundlePath, err := p.API.GetBundlePath()
|
||||
if err != nil {
|
||||
return nil, err.Error() + "failed get bundle path"
|
||||
} else if bundlePathFromConfig, _ := filepath.Abs(filepath.Join(*p.API.GetConfig().PluginSettings.Directory, "test_get_bundle_path_plugin")); bundlePathFromConfig != bundlePath {
|
||||
return nil, fmt.Sprintf("Invalid bundle path returned: %v vs %v", bundlePathFromConfig, bundlePath)
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
|
||||
channels, err := p.API.GetChannelsForTeamForUser("{{.BasicTeam.Id}}", "{{.BasicUser.Id}}", false)
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
if len(channels) != 3 {
|
||||
return nil, "Returned invalid number of channels"
|
||||
}
|
||||
channels, err = p.API.GetChannelsForTeamForUser("invalidid", "{{.BasicUser.Id}}", false)
|
||||
if err == nil {
|
||||
return nil, "Expected to get an error while retrieving channels for invalid id"
|
||||
}
|
||||
if len(channels) != 0 {
|
||||
return nil, "Returned invalid number of channels"
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
73
tests/plugin_tests/test_get_direct_channel_plugin/main.go
Обычный файл
73
tests/plugin_tests/test_get_direct_channel_plugin/main.go
Обычный файл
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func isEmpty(object interface{}) bool {
|
||||
|
||||
// get nil case out of the way
|
||||
if object == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
objValue := reflect.ValueOf(object)
|
||||
|
||||
switch objValue.Kind() {
|
||||
// collection types are empty when they have no element
|
||||
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
||||
return objValue.Len() == 0
|
||||
// pointers are empty if nil or if the value they point to is empty
|
||||
case reflect.Ptr:
|
||||
if objValue.IsNil() {
|
||||
return true
|
||||
}
|
||||
deref := objValue.Elem().Interface()
|
||||
return isEmpty(deref)
|
||||
// for all other types, compare against the zero value
|
||||
default:
|
||||
zero := reflect.Zero(objValue.Type())
|
||||
return reflect.DeepEqual(object, zero.Interface())
|
||||
}
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
dm1, err := p.API.GetDirectChannel("{{.BasicUser.Id}}", "{{.BasicUser2.Id}}")
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
if isEmpty(dm1) {
|
||||
return nil, "dm1 is empty"
|
||||
}
|
||||
|
||||
dm2, err := p.API.GetDirectChannel("{{.BasicUser.Id}}", "{{.BasicUser.Id}}")
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
if isEmpty(dm2) {
|
||||
return nil, "dm2 is empty"
|
||||
}
|
||||
|
||||
dm3, err := p.API.GetDirectChannel("{{.BasicUser.Id}}", model.NewId())
|
||||
if err == nil {
|
||||
return nil, "Expected to get error while fetching incorrect channel"
|
||||
}
|
||||
if !isEmpty(dm3) {
|
||||
return nil, "dm3 is NOT empty"
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
65
tests/plugin_tests/test_get_profile_image_plugin/main.go
Обычный файл
65
tests/plugin_tests/test_get_profile_image_plugin/main.go
Обычный файл
@@ -0,0 +1,65 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func isEmpty(object interface{}) bool {
|
||||
|
||||
// get nil case out of the way
|
||||
if object == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
objValue := reflect.ValueOf(object)
|
||||
|
||||
switch objValue.Kind() {
|
||||
// collection types are empty when they have no element
|
||||
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
||||
return objValue.Len() == 0
|
||||
// pointers are empty if nil or if the value they point to is empty
|
||||
case reflect.Ptr:
|
||||
if objValue.IsNil() {
|
||||
return true
|
||||
}
|
||||
deref := objValue.Elem().Interface()
|
||||
return isEmpty(deref)
|
||||
// for all other types, compare against the zero value
|
||||
default:
|
||||
zero := reflect.Zero(objValue.Type())
|
||||
return reflect.DeepEqual(object, zero.Interface())
|
||||
}
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
|
||||
// check existing user first
|
||||
data, err := p.API.GetProfileImage("{{.BasicUser.Id}}")
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
if isEmpty(data) {
|
||||
return nil, "GetProfileImage return empty"
|
||||
}
|
||||
|
||||
// then unknown user
|
||||
data, err = p.API.GetProfileImage(model.NewId())
|
||||
if err == nil || data != nil {
|
||||
return nil, "GetProfileImage should've returned an error"
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
31
tests/plugin_tests/test_member_channels_plugin/main.go
Обычный файл
31
tests/plugin_tests/test_member_channels_plugin/main.go
Обычный файл
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
channelMembers, err := p.API.GetChannelMembersForUser("{{.BasicTeam.Id}}", "{{.BasicUser.Id}}", 0, 10)
|
||||
|
||||
if err != nil {
|
||||
return nil, err.Error() + "failed to get channel members"
|
||||
} else if len(channelMembers) != 3 {
|
||||
return nil, "Invalid number of channel members"
|
||||
} else if channelMembers[0].UserId != "{{.BasicUser.Id}}" {
|
||||
return nil, "Invalid user id returned"
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
29
tests/plugin_tests/test_members_plugin/main.go
Обычный файл
29
tests/plugin_tests/test_members_plugin/main.go
Обычный файл
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
teamMembers, err := p.API.GetTeamMembersForUser("{{.BasicUser.Id}}", 0, 10)
|
||||
if err != nil {
|
||||
return nil, err.Error() + "failed to get team members"
|
||||
} else if len(teamMembers) != 1 {
|
||||
return nil, "Invalid number of team members"
|
||||
} else if teamMembers[0].UserId != "{{.BasicUser.Id}}" || teamMembers[0].TeamId != "{{.BasicTeam.Id}}" {
|
||||
return nil, "Invalid user or team id returned"
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
38
tests/plugin_tests/test_search_channels_plugin/main.go
Обычный файл
38
tests/plugin_tests/test_search_channels_plugin/main.go
Обычный файл
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
|
||||
channels, err := p.API.SearchChannels("{{.BasicTeam.Id}}", "{{.BasicChannel.Name}}")
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
if len(channels) != 1 {
|
||||
return nil, "Returned invalid number of channels"
|
||||
}
|
||||
|
||||
channels, err = p.API.SearchChannels("invalidid", "{{.BasicChannel.Name}}")
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
if len(channels) != 0 {
|
||||
return nil, "Returned invalid number of channels"
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
65
tests/plugin_tests/test_search_posts_in_team_plugin/main.go
Обычный файл
65
tests/plugin_tests/test_search_posts_in_team_plugin/main.go
Обычный файл
@@ -0,0 +1,65 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
|
||||
testCases := []struct {
|
||||
description string
|
||||
teamId string
|
||||
params []*model.SearchParams
|
||||
expectedPostsLen int
|
||||
}{
|
||||
{
|
||||
"nil params",
|
||||
"{{.BasicTeam.Id}}",
|
||||
nil,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"empty params",
|
||||
"{{.BasicTeam.Id}}",
|
||||
[]*model.SearchParams{},
|
||||
0,
|
||||
},
|
||||
{
|
||||
"doesn't match any posts",
|
||||
"{{.BasicTeam.Id}}",
|
||||
model.ParseSearchParams("bad message", 0),
|
||||
0,
|
||||
},
|
||||
{
|
||||
"matched posts",
|
||||
"{{.BasicTeam.Id}}",
|
||||
model.ParseSearchParams("{{.BasicPost.Message}}", 0),
|
||||
1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
posts, err := p.API.SearchPostsInTeam(testCase.teamId, testCase.params)
|
||||
if err != nil {
|
||||
return nil, fmt.Sprintf("%v: %v", testCase.description, err.Error())
|
||||
}
|
||||
if testCase.expectedPostsLen != len(posts) {
|
||||
return nil, fmt.Sprintf("%v: invalid number of posts: %v != %v", testCase.description, testCase.expectedPostsLen, len(posts))
|
||||
}
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
56
tests/plugin_tests/test_search_teams_plugin/main.go
Обычный файл
56
tests/plugin_tests/test_search_teams_plugin/main.go
Обычный файл
@@ -0,0 +1,56 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
|
||||
teams, err := p.API.SearchTeams("{{.BasicTeam.Name}}")
|
||||
if err != nil {
|
||||
return nil, "search failed: " + err.Message
|
||||
}
|
||||
if len(teams) != 1 {
|
||||
return nil, fmt.Sprintf("search failed, wrong number of teams: %v", len(teams))
|
||||
}
|
||||
|
||||
teams, err = p.API.SearchTeams("{{.BasicTeam.DisplayName}}")
|
||||
if err != nil {
|
||||
return nil, "search failed: " + err.Message
|
||||
}
|
||||
if len(teams) != 1 {
|
||||
return nil, fmt.Sprintf("search failed, wrong number of teams: %v", len(teams))
|
||||
}
|
||||
|
||||
teams, err = p.API.SearchTeams("{{.BasicTeam.Name}}"[:3])
|
||||
|
||||
if err != nil {
|
||||
return nil, "search failed: " + err.Message
|
||||
}
|
||||
if len(teams) != 1 {
|
||||
return nil, fmt.Sprintf("search failed, wrong number of teams: %v", len(teams))
|
||||
}
|
||||
|
||||
teams, err = p.API.SearchTeams("not found")
|
||||
if err != nil {
|
||||
return nil, "search failed: " + err.Message
|
||||
}
|
||||
if len(teams) != 0 {
|
||||
return nil, fmt.Sprintf("search failed, wrong number of teams: %v", len(teams))
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
59
tests/plugin_tests/test_send_mail_plugin/main.go
Обычный файл
59
tests/plugin_tests/test_send_mail_plugin/main.go
Обычный файл
@@ -0,0 +1,59 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
"github.com/mattermost/mattermost-server/services/mailservice"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
to := "{{.BasicUser.Email}}"
|
||||
subject := "testing plugin api sending email"
|
||||
body := "this is a test."
|
||||
|
||||
if err := p.API.SendMail(to, subject, body); err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
|
||||
// Check if we received the email
|
||||
var resultsMailbox mailservice.JSONMessageHeaderInbucket
|
||||
if errMail := mailservice.RetryInbucket(5, func() error {
|
||||
var err error
|
||||
resultsMailbox, err = mailservice.GetMailBox(to)
|
||||
return err
|
||||
}); errMail != nil {
|
||||
return nil, errMail.Error()
|
||||
}
|
||||
if len(resultsMailbox) == 0 {
|
||||
return nil, fmt.Sprintf("No mailbox results. Should be %v", len(resultsMailbox))
|
||||
}
|
||||
if !strings.ContainsAny(resultsMailbox[len(resultsMailbox)-1].To[0], to) {
|
||||
return nil, "Result doesn't contain recipient"
|
||||
}
|
||||
|
||||
resultsEmail, err1 := mailservice.GetMessageFromMailbox(to, resultsMailbox[len(resultsMailbox)-1].ID)
|
||||
if err1 != nil {
|
||||
return nil, err1.Error()
|
||||
}
|
||||
if resultsEmail.Subject != subject {
|
||||
return nil, fmt.Sprintf("subject differs: %v vs %s", resultsEmail.Subject, subject)
|
||||
}
|
||||
if resultsEmail.Body.Text != body {
|
||||
return nil, fmt.Sprintf("body differs: %v vs %s", resultsEmail.Body.Text, body)
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
90
tests/plugin_tests/test_set_profile_image_plugin/main.go
Обычный файл
90
tests/plugin_tests/test_set_profile_image_plugin/main.go
Обычный файл
@@ -0,0 +1,90 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"reflect"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func isEmpty(object interface{}) bool {
|
||||
|
||||
// get nil case out of the way
|
||||
if object == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
objValue := reflect.ValueOf(object)
|
||||
|
||||
switch objValue.Kind() {
|
||||
// collection types are empty when they have no element
|
||||
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
||||
return objValue.Len() == 0
|
||||
// pointers are empty if nil or if the value they point to is empty
|
||||
case reflect.Ptr:
|
||||
if objValue.IsNil() {
|
||||
return true
|
||||
}
|
||||
deref := objValue.Elem().Interface()
|
||||
return isEmpty(deref)
|
||||
// for all other types, compare against the zero value
|
||||
default:
|
||||
zero := reflect.Zero(objValue.Type())
|
||||
return reflect.DeepEqual(object, zero.Interface())
|
||||
}
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
|
||||
// Create an 128 x 128 image
|
||||
img := image.NewRGBA(image.Rect(0, 0, 128, 128))
|
||||
// Draw a red dot at (2, 3)
|
||||
img.Set(2, 3, color.RGBA{255, 0, 0, 255})
|
||||
buf := new(bytes.Buffer)
|
||||
if err := png.Encode(buf, img); err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
|
||||
dataBytes := buf.Bytes()
|
||||
|
||||
// Set the user profile image
|
||||
if err := p.API.SetProfileImage("{{.BasicUser.Id}}", dataBytes); err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
|
||||
// Get the user profile image to check
|
||||
imageProfile, err := p.API.GetProfileImage("{{.BasicUser.Id}}")
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
if isEmpty(imageProfile) {
|
||||
return nil, "profile image is empty"
|
||||
}
|
||||
|
||||
colorful := color.NRGBA{255, 0, 0, 255}
|
||||
byteReader := bytes.NewReader(imageProfile)
|
||||
img2, _, err2 := image.Decode(byteReader)
|
||||
if err2 != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
if img2.At(2, 3) != colorful {
|
||||
return nil, fmt.Sprintf("color mismatch %v != %v", img2.At(2, 3), colorful)
|
||||
}
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
68
tests/plugin_tests/test_update_user_active_plugin/main.go
Обычный файл
68
tests/plugin_tests/test_update_user_active_plugin/main.go
Обычный файл
@@ -0,0 +1,68 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
uid := "{{.BasicUser.Id}}"
|
||||
if err := p.API.UpdateUserActive(uid, true); err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
|
||||
user, err := p.API.GetUser(uid)
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
|
||||
if int64(0) != user.DeleteAt {
|
||||
return nil, "DeleteAt value is not 0"
|
||||
}
|
||||
|
||||
if err = p.API.UpdateUserActive(uid, false); err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
|
||||
user, err = p.API.GetUser(uid)
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
if user == nil {
|
||||
return nil, "GetUser returned nil"
|
||||
}
|
||||
|
||||
if int64(0) == user.DeleteAt {
|
||||
return nil, "DeleteAt value is 0"
|
||||
}
|
||||
|
||||
if err = p.API.UpdateUserActive(uid, true); err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
|
||||
if err = p.API.UpdateUserActive(uid, true); err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
|
||||
user, err = p.API.GetUser(uid)
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
|
||||
if int64(0) != user.DeleteAt {
|
||||
return nil, "DeleteAt value is not 0"
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
49
tests/plugin_tests/test_update_user_status_plugin/main.go
Обычный файл
49
tests/plugin_tests/test_update_user_status_plugin/main.go
Обычный файл
@@ -0,0 +1,49 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
|
||||
uid := "{{.BasicUser.Id}}"
|
||||
|
||||
statuses := []string{model.STATUS_ONLINE, model.STATUS_AWAY, model.STATUS_DND, model.STATUS_OFFLINE}
|
||||
|
||||
for _, s := range statuses {
|
||||
status, err := p.API.UpdateUserStatus(uid, s)
|
||||
if err != nil {
|
||||
return nil, err.Error()
|
||||
}
|
||||
if status == nil {
|
||||
return nil, "Status was expected, got nil"
|
||||
}
|
||||
if s != status.Status {
|
||||
return nil, fmt.Sprintf("Invalid status returned: %v != %v", s, status.Status)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
status, err := p.API.UpdateUserStatus(uid, "notrealstatus")
|
||||
if err == nil {
|
||||
return nil, "Expected to get an error while updating invalid user status"
|
||||
}
|
||||
if status != nil {
|
||||
return nil, "Status was expected to be nil, got: " + status.Status
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
Ссылка в новой задаче
Block a user