-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
main.go
130 lines (114 loc) · 3.8 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package main
import (
"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) {
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"
}
// TODO: investigate why the following code panics
/*
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, "OK"
}
func main() {
plugin.ClientMain(&MyPlugin{})
}