/
main.go
73 lines (65 loc) · 1.65 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package main
import (
"fmt"
"github.com/mattermost/mattermost-server/v5/app/plugin_api_tests"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
)
type MyPlugin struct {
plugin.MattermostPlugin
configuration plugin_api_tests.BasicConfig
}
func (p *MyPlugin) OnConfigurationChange() error {
if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil {
return err
}
return nil
}
func (p *MyPlugin) MessageWillBePosted(_ *plugin.Context, _ *model.Post) (*model.Post, string) {
testCases := []struct {
description string
teamID string
params []*model.SearchParams
expectedPostsLen int
}{
{
"nil params",
p.configuration.BasicTeamID,
nil,
0,
},
{
"empty params",
p.configuration.BasicTeamID,
[]*model.SearchParams{},
0,
},
{
"doesn't match any posts",
p.configuration.BasicTeamID,
model.ParseSearchParams("bad message", 0),
0,
},
{
"matched posts",
p.configuration.BasicTeamID,
model.ParseSearchParams(p.configuration.BasicPostMessage, 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, "OK"
}
func main() {
plugin.ClientMain(&MyPlugin{})
}