github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/tests/plugin_tests/test_bots_plugin/main.go (about)

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