github.com/adacta-ru/mattermost-server/v6@v6.0.0/app/plugin_api_tests/test_search_posts_in_team_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  	"fmt"
     8  
     9  	"github.com/adacta-ru/mattermost-server/v6/app/plugin_api_tests"
    10  	"github.com/adacta-ru/mattermost-server/v6/model"
    11  	"github.com/adacta-ru/mattermost-server/v6/plugin"
    12  )
    13  
    14  type MyPlugin struct {
    15  	plugin.MattermostPlugin
    16  	configuration plugin_api_tests.BasicConfig
    17  }
    18  
    19  func (p *MyPlugin) OnConfigurationChange() error {
    20  	if err := p.API.LoadPluginConfiguration(&p.configuration); err != nil {
    21  		return err
    22  	}
    23  	return nil
    24  }
    25  
    26  func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) {
    27  	testCases := []struct {
    28  		description      string
    29  		teamID           string
    30  		params           []*model.SearchParams
    31  		expectedPostsLen int
    32  	}{
    33  		{
    34  			"nil params",
    35  			p.configuration.BasicTeamId,
    36  			nil,
    37  			0,
    38  		},
    39  		{
    40  			"empty params",
    41  			p.configuration.BasicTeamId,
    42  			[]*model.SearchParams{},
    43  			0,
    44  		},
    45  		{
    46  			"doesn't match any posts",
    47  			p.configuration.BasicTeamId,
    48  			model.ParseSearchParams("bad message", 0),
    49  			0,
    50  		},
    51  		{
    52  			"matched posts",
    53  			p.configuration.BasicTeamId,
    54  			model.ParseSearchParams(p.configuration.BasicPostMessage, 0),
    55  			1,
    56  		},
    57  	}
    58  
    59  	for _, testCase := range testCases {
    60  		posts, err := p.API.SearchPostsInTeam(testCase.teamID, testCase.params)
    61  		if err != nil {
    62  			return nil, fmt.Sprintf("%v: %v", testCase.description, err.Error())
    63  		}
    64  		if testCase.expectedPostsLen != len(posts) {
    65  			return nil, fmt.Sprintf("%v: invalid number of posts: %v != %v", testCase.description, testCase.expectedPostsLen, len(posts))
    66  		}
    67  	}
    68  	return nil, "OK"
    69  }
    70  
    71  func main() {
    72  	plugin.ClientMain(&MyPlugin{})
    73  }