github.com/adacta-ru/mattermost-server/v6@v6.0.0/app/plugin_api_tests/test_bots_plugin/main.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package main 5 6 import ( 7 "github.com/adacta-ru/mattermost-server/v6/app/plugin_api_tests" 8 "github.com/adacta-ru/mattermost-server/v6/model" 9 "github.com/adacta-ru/mattermost-server/v6/plugin" 10 ) 11 12 type MyPlugin struct { 13 plugin.MattermostPlugin 14 configuration plugin_api_tests.BasicConfig 15 } 16 17 func (p *MyPlugin) OnConfigurationChange() error { 18 if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil { 19 return err 20 } 21 return nil 22 } 23 24 func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) { 25 createdBot, err := p.API.CreateBot(&model.Bot{ 26 Username: "bot", 27 Description: "a plugin bot", 28 }) 29 30 if err != nil { 31 return nil, err.Error() + "failed to create bot" 32 } 33 34 fetchedBot, err := p.API.GetBot(createdBot.UserId, false) 35 if err != nil { 36 return nil, err.Error() + "failed to get bot" 37 } 38 if fetchedBot.Description != "a plugin bot" { 39 return nil, "GetBot did not return the expected bot Description" 40 } 41 if fetchedBot.OwnerId != "test_bots_plugin" { 42 return nil, "GetBot did not return the expected bot OwnerId" 43 } 44 45 updatedDescription := createdBot.Description + ", updated" 46 patchedBot, err := p.API.PatchBot(createdBot.UserId, &model.BotPatch{ 47 Description: &updatedDescription, 48 }) 49 if err != nil { 50 return nil, err.Error() + "failed to patch bot" 51 } 52 53 fetchedBot, err = p.API.GetBot(patchedBot.UserId, false) 54 if err != nil { 55 return nil, err.Error() + "failed to get bot" 56 } 57 58 if fetchedBot.UserId != patchedBot.UserId { 59 return nil, "GetBot did not return the expected bot" 60 } 61 if fetchedBot.Description != "a plugin bot, updated" { 62 return nil, "GetBot did not return the updated bot Description" 63 } 64 65 fetchedBots, err := p.API.GetBots(&model.BotGetOptions{ 66 Page: 0, 67 PerPage: 1, 68 OwnerId: "", 69 IncludeDeleted: false, 70 }) 71 if err != nil { 72 return nil, err.Error() + "failed to get bots" 73 } 74 75 if len(fetchedBots) != 1 { 76 return nil, "GetBots did not return a single bot" 77 } 78 79 if fetchedBot.UserId != fetchedBots[0].UserId { 80 return nil, "GetBots did not return the expected bot" 81 } 82 if _, err = p.API.UpdateBotActive(fetchedBot.UserId, false); err != nil { 83 return nil, err.Error() + "failed to disable bot" 84 } 85 86 // TODO: investigate why the following code panics 87 /* 88 if fetchedBot, err = p.API.GetBot(patchedBot.UserId, false); err == nil { 89 return nil, "expected not to find disabled bot" 90 } 91 if _, err = p.API.UpdateBotActive(fetchedBot.UserId, true); err != nil { 92 return nil, err.Error() + "failed to disable bot" 93 } 94 if fetchedBot, err = p.API.GetBot(patchedBot.UserId, false); err != nil { 95 return nil, err.Error() + "failed to get bot after enabling" 96 } 97 if fetchedBot.UserId != patchedBot.UserId { 98 return nil, "GetBot did not return the expected bot after enabling" 99 } 100 if err = p.API.PermanentDeleteBot(patchedBot.UserId); err != nil { 101 return nil, err.Error() + "failed to delete bot" 102 } 103 104 if _, err = p.API.GetBot(patchedBot.UserId, false); err == nil { 105 return nil, err.Error() + "found bot after permanently deleting" 106 } 107 createdBotWithOverriddenCreator, err := p.API.CreateBot(&model.Bot{ 108 Username: "bot", 109 Description: "a plugin bot", 110 OwnerId: "abc123", 111 }) 112 if err != nil { 113 return nil, err.Error() + "failed to create bot with overridden creator" 114 } 115 if fetchedBot, err = p.API.GetBot(createdBotWithOverriddenCreator.UserId, false); err != nil { 116 return nil, err.Error() + "failed to get bot" 117 } 118 if fetchedBot.Description != "a plugin bot" { 119 return nil, "GetBot did not return the expected bot Description" 120 } 121 if fetchedBot.OwnerId != "abc123" { 122 return nil, "GetBot did not return the expected bot OwnerId" 123 } 124 */ 125 return nil, "OK" 126 } 127 128 func main() { 129 plugin.ClientMain(&MyPlugin{}) 130 }