2019-05-06 12:44:38 -07:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin_test
import (
2019-11-15 04:19:00 +00:00
"io/ioutil"
"path/filepath"
2019-05-06 12:44:38 -07:00
"testing"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
"github.com/mattermost/mattermost-server/plugin/plugintest"
2019-06-10 07:39:42 -07:00
"github.com/mattermost/mattermost-server/plugin/plugintest/mock"
2019-11-15 04:19:00 +00:00
"github.com/mattermost/mattermost-server/utils/fileutils"
2019-05-06 12:44:38 -07:00
"github.com/stretchr/testify/assert"
)
func TestEnsureBot ( t * testing . T ) {
setupAPI := func ( ) * plugintest . API {
return & plugintest . API { }
}
testbot := & model . Bot {
Username : "testbot" ,
DisplayName : "Test Bot" ,
Description : "testbotdescription" ,
}
2019-11-06 22:12:46 +05:30
t . Run ( "server version incompatible" , func ( t * testing . T ) {
api := setupAPI ( )
api . On ( "GetServerVersion" ) . Return ( "5.9.0" )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
_ , retErr := p . EnsureBot ( nil )
assert . NotNil ( t , retErr )
assert . Equal ( t , "failed to ensure bot: incompatible server version for plugin, minimum required version: 5.10.0, current version: 5.9.0" , retErr . Error ( ) )
} )
2019-05-06 12:44:38 -07:00
t . Run ( "bad parameters" , func ( t * testing . T ) {
t . Run ( "no bot" , func ( t * testing . T ) {
2019-11-06 22:12:46 +05:30
api := setupAPI ( )
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
2019-05-06 12:44:38 -07:00
p := & plugin . HelpersImpl { }
2019-11-06 22:12:46 +05:30
p . API = api
2019-05-06 12:44:38 -07:00
botId , err := p . EnsureBot ( nil )
assert . Equal ( t , "" , botId )
assert . NotNil ( t , err )
} )
t . Run ( "bad username" , func ( t * testing . T ) {
2019-11-06 22:12:46 +05:30
api := setupAPI ( )
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
2019-05-06 12:44:38 -07:00
p := & plugin . HelpersImpl { }
2019-11-06 22:12:46 +05:30
p . API = api
2019-05-06 12:44:38 -07:00
botId , err := p . EnsureBot ( & model . Bot {
Username : "" ,
} )
assert . Equal ( t , "" , botId )
assert . NotNil ( t , err )
} )
} )
t . Run ( "if bot already exists" , func ( t * testing . T ) {
t . Run ( "should find and return the existing bot ID" , func ( t * testing . T ) {
expectedBotId := model . NewId ( )
api := setupAPI ( )
2019-11-06 22:12:46 +05:30
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
2019-05-06 12:44:38 -07:00
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( [ ] byte ( expectedBotId ) , nil )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot )
assert . Equal ( t , expectedBotId , botId )
assert . Nil ( t , err )
} )
t . Run ( "should return an error if unable to get bot" , func ( t * testing . T ) {
api := setupAPI ( )
2019-11-06 22:12:46 +05:30
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
2019-05-06 12:44:38 -07:00
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( nil , & model . AppError { } )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot )
assert . Equal ( t , "" , botId )
assert . NotNil ( t , err )
} )
2019-11-15 04:19:00 +00:00
t . Run ( "should set the bot profile image when specified" , func ( t * testing . T ) {
expectedBotId := model . NewId ( )
api := setupAPI ( )
testsDir , _ := fileutils . FindDir ( "tests" )
testImage := filepath . Join ( testsDir , "test.png" )
imageBytes , err := ioutil . ReadFile ( testImage )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( [ ] byte ( expectedBotId ) , nil )
api . On ( "GetBundlePath" ) . Return ( "" , nil )
api . On ( "SetProfileImage" , expectedBotId , imageBytes ) . Return ( nil )
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
assert . Nil ( t , err )
botId , err := p . EnsureBot ( testbot , plugin . ProfileImagePath ( testImage ) )
assert . Equal ( t , expectedBotId , botId )
assert . Nil ( t , err )
} )
t . Run ( "should set the bot icon image when specified" , func ( t * testing . T ) {
expectedBotId := model . NewId ( )
api := setupAPI ( )
testsDir , _ := fileutils . FindDir ( "tests" )
testImage := filepath . Join ( testsDir , "test.png" )
imageBytes , err := ioutil . ReadFile ( testImage )
assert . Nil ( t , err )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( [ ] byte ( expectedBotId ) , nil )
api . On ( "GetBundlePath" ) . Return ( "" , nil )
api . On ( "SetBotIconImage" , expectedBotId , imageBytes ) . Return ( nil )
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot , plugin . IconImagePath ( testImage ) )
assert . Equal ( t , expectedBotId , botId )
assert . Nil ( t , err )
} )
t . Run ( "should set both the profile image and bot icon image when specified" , func ( t * testing . T ) {
expectedBotId := model . NewId ( )
api := setupAPI ( )
testsDir , _ := fileutils . FindDir ( "tests" )
testImage := filepath . Join ( testsDir , "test.png" )
imageBytes , err := ioutil . ReadFile ( testImage )
assert . Nil ( t , err )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( [ ] byte ( expectedBotId ) , nil )
api . On ( "GetBundlePath" ) . Return ( "" , nil )
api . On ( "SetProfileImage" , expectedBotId , imageBytes ) . Return ( nil )
api . On ( "SetBotIconImage" , expectedBotId , imageBytes ) . Return ( nil )
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot , plugin . ProfileImagePath ( testImage ) , plugin . IconImagePath ( testImage ) )
assert . Equal ( t , expectedBotId , botId )
assert . Nil ( t , err )
} )
2019-05-06 12:44:38 -07:00
} )
t . Run ( "if bot doesn't exist" , func ( t * testing . T ) {
t . Run ( "should create the bot and return the ID" , func ( t * testing . T ) {
expectedBotId := model . NewId ( )
api := setupAPI ( )
2019-11-06 22:12:46 +05:30
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
2019-05-06 12:44:38 -07:00
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( nil , nil )
api . On ( "GetUserByUsername" , testbot . Username ) . Return ( nil , nil )
api . On ( "CreateBot" , testbot ) . Return ( & model . Bot {
UserId : expectedBotId ,
} , nil )
api . On ( "KVSet" , plugin . BOT_USER_KEY , [ ] byte ( expectedBotId ) ) . Return ( nil )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot )
assert . Equal ( t , expectedBotId , botId )
assert . Nil ( t , err )
} )
t . Run ( "should claim existing bot and return the ID" , func ( t * testing . T ) {
expectedBotId := model . NewId ( )
api := setupAPI ( )
2019-11-06 22:12:46 +05:30
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
2019-05-06 12:44:38 -07:00
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( nil , nil )
api . On ( "GetUserByUsername" , testbot . Username ) . Return ( & model . User {
Id : expectedBotId ,
IsBot : true ,
} , nil )
api . On ( "KVSet" , plugin . BOT_USER_KEY , [ ] byte ( expectedBotId ) ) . Return ( nil )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot )
assert . Equal ( t , expectedBotId , botId )
assert . Nil ( t , err )
} )
2019-06-10 07:39:42 -07:00
t . Run ( "should return the non-bot account but log a message if user exists with the same name and is not a bot" , func ( t * testing . T ) {
expectedBotId := model . NewId ( )
2019-05-06 12:44:38 -07:00
api := setupAPI ( )
2019-11-06 22:12:46 +05:30
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
2019-05-06 12:44:38 -07:00
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( nil , nil )
api . On ( "GetUserByUsername" , testbot . Username ) . Return ( & model . User {
2019-06-10 07:39:42 -07:00
Id : expectedBotId ,
2019-05-06 12:44:38 -07:00
IsBot : false ,
} , nil )
2019-06-10 07:39:42 -07:00
api . On ( "LogError" , mock . Anything , mock . Anything , mock . Anything , mock . Anything , mock . Anything ) . Return ( )
2019-05-06 12:44:38 -07:00
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot )
2019-06-10 07:39:42 -07:00
assert . Equal ( t , expectedBotId , botId )
assert . Nil ( t , err )
2019-05-06 12:44:38 -07:00
} )
2019-10-15 19:15:46 +05:30
t . Run ( "should fail if create bot fails" , func ( t * testing . T ) {
2019-05-06 12:44:38 -07:00
api := setupAPI ( )
2019-11-06 22:12:46 +05:30
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
2019-05-06 12:44:38 -07:00
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( nil , nil )
api . On ( "GetUserByUsername" , testbot . Username ) . Return ( nil , nil )
api . On ( "CreateBot" , testbot ) . Return ( nil , & model . AppError { } )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot )
assert . Equal ( t , "" , botId )
assert . NotNil ( t , err )
} )
2019-11-15 04:19:00 +00:00
t . Run ( "should create bot and set the bot profile image when specified" , func ( t * testing . T ) {
expectedBotId := model . NewId ( )
api := setupAPI ( )
testsDir , _ := fileutils . FindDir ( "tests" )
testImage := filepath . Join ( testsDir , "test.png" )
imageBytes , err := ioutil . ReadFile ( testImage )
assert . Nil ( t , err )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( nil , nil )
api . On ( "GetUserByUsername" , testbot . Username ) . Return ( nil , nil )
api . On ( "CreateBot" , testbot ) . Return ( & model . Bot {
UserId : expectedBotId ,
} , nil )
api . On ( "KVSet" , plugin . BOT_USER_KEY , [ ] byte ( expectedBotId ) ) . Return ( nil )
api . On ( "GetBundlePath" ) . Return ( "" , nil )
api . On ( "SetProfileImage" , expectedBotId , imageBytes ) . Return ( nil )
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot , plugin . ProfileImagePath ( testImage ) )
assert . Equal ( t , expectedBotId , botId )
assert . Nil ( t , err )
} )
t . Run ( "should create bot and set the bot icon image when specified" , func ( t * testing . T ) {
expectedBotId := model . NewId ( )
api := setupAPI ( )
testsDir , _ := fileutils . FindDir ( "tests" )
testImage := filepath . Join ( testsDir , "test.png" )
imageBytes , err := ioutil . ReadFile ( testImage )
assert . Nil ( t , err )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( nil , nil )
api . On ( "GetUserByUsername" , testbot . Username ) . Return ( nil , nil )
api . On ( "CreateBot" , testbot ) . Return ( & model . Bot {
UserId : expectedBotId ,
} , nil )
api . On ( "KVSet" , plugin . BOT_USER_KEY , [ ] byte ( expectedBotId ) ) . Return ( nil )
api . On ( "GetBundlePath" ) . Return ( "" , nil )
api . On ( "SetBotIconImage" , expectedBotId , imageBytes ) . Return ( nil )
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot , plugin . IconImagePath ( testImage ) )
assert . Equal ( t , expectedBotId , botId )
assert . Nil ( t , err )
} )
t . Run ( "should create bot and set both the profile image and bot icon image when specified" , func ( t * testing . T ) {
expectedBotId := model . NewId ( )
api := setupAPI ( )
testsDir , _ := fileutils . FindDir ( "tests" )
testImage := filepath . Join ( testsDir , "test.png" )
imageBytes , err := ioutil . ReadFile ( testImage )
assert . Nil ( t , err )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( nil , nil )
api . On ( "GetUserByUsername" , testbot . Username ) . Return ( nil , nil )
api . On ( "CreateBot" , testbot ) . Return ( & model . Bot {
UserId : expectedBotId ,
} , nil )
api . On ( "KVSet" , plugin . BOT_USER_KEY , [ ] byte ( expectedBotId ) ) . Return ( nil )
api . On ( "GetBundlePath" ) . Return ( "" , nil )
api . On ( "SetProfileImage" , expectedBotId , imageBytes ) . Return ( nil )
api . On ( "SetBotIconImage" , expectedBotId , imageBytes ) . Return ( nil )
api . On ( "GetServerVersion" ) . Return ( "5.10.0" )
defer api . AssertExpectations ( t )
p := & plugin . HelpersImpl { }
p . API = api
botId , err := p . EnsureBot ( testbot , plugin . ProfileImagePath ( testImage ) , plugin . IconImagePath ( testImage ) )
assert . Equal ( t , expectedBotId , botId )
assert . Nil ( t , err )
} )
2019-05-06 12:44:38 -07:00
} )
}
2019-11-18 15:17:32 +05:30
func TestShouldProcessMessage ( t * testing . T ) {
p := & plugin . HelpersImpl { }
expectedBotId := model . NewId ( )
setupAPI := func ( ) * plugintest . API {
return & plugintest . API { }
}
t . Run ( "should not respond to itself" , func ( t * testing . T ) {
api := setupAPI ( )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( [ ] byte ( expectedBotId ) , nil )
p . API = api
shouldProcessMessage , _ := p . ShouldProcessMessage ( & model . Post { Type : model . POST_HEADER_CHANGE , UserId : expectedBotId } , plugin . AllowSystemMessages ( ) , plugin . AllowBots ( ) )
assert . False ( t , shouldProcessMessage )
} )
t . Run ( "should not process as the post is generated by system" , func ( t * testing . T ) {
shouldProcessMessage , _ := p . ShouldProcessMessage ( & model . Post { Type : model . POST_HEADER_CHANGE } )
assert . False ( t , shouldProcessMessage )
} )
t . Run ( "should not process as the post is sent to another channel" , func ( t * testing . T ) {
channelID := "channel-id"
api := setupAPI ( )
api . On ( "GetChannel" , channelID ) . Return ( & model . Channel { Id : channelID , Type : model . CHANNEL_GROUP } , nil )
p . API = api
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( [ ] byte ( expectedBotId ) , nil )
shouldProcessMessage , _ := p . ShouldProcessMessage ( & model . Post { ChannelId : channelID } , plugin . AllowSystemMessages ( ) , plugin . AllowBots ( ) , plugin . FilterChannelIDs ( [ ] string { "another-channel-id" } ) )
assert . False ( t , shouldProcessMessage )
} )
t . Run ( "should not process as the post is created by bot" , func ( t * testing . T ) {
userID := "user-id"
channelID := "1"
api := setupAPI ( )
p . API = api
api . On ( "GetUser" , userID ) . Return ( & model . User { IsBot : true } , nil )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( [ ] byte ( expectedBotId ) , nil )
shouldProcessMessage , _ := p . ShouldProcessMessage ( & model . Post { UserId : userID , ChannelId : channelID } ,
plugin . AllowSystemMessages ( ) , plugin . FilterUserIDs ( [ ] string { "another-user-id" } ) )
assert . False ( t , shouldProcessMessage )
} )
t . Run ( "should not process the message as the post is not in bot dm channel" , func ( t * testing . T ) {
userID := "user-id"
channelID := "1"
channel := model . Channel {
Name : "user1__" + expectedBotId ,
Type : model . CHANNEL_OPEN ,
}
api := setupAPI ( )
api . On ( "GetChannel" , channelID ) . Return ( & channel , nil )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( [ ] byte ( expectedBotId ) , nil )
p . API = api
shouldProcessMessage , _ := p . ShouldProcessMessage ( & model . Post { UserId : userID , ChannelId : channelID } , plugin . AllowSystemMessages ( ) , plugin . AllowBots ( ) , plugin . OnlyBotDMs ( ) )
assert . False ( t , shouldProcessMessage )
} )
t . Run ( "should process the message" , func ( t * testing . T ) {
channelID := "1"
api := setupAPI ( )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( [ ] byte ( expectedBotId ) , nil )
p . API = api
shouldProcessMessage , _ := p . ShouldProcessMessage ( & model . Post { UserId : "1" , Type : model . POST_HEADER_CHANGE , ChannelId : channelID } ,
plugin . AllowSystemMessages ( ) , plugin . FilterChannelIDs ( [ ] string { channelID } ) , plugin . AllowBots ( ) , plugin . FilterUserIDs ( [ ] string { "1" } ) )
assert . True ( t , shouldProcessMessage )
} )
t . Run ( "should process the message for plugin without a bot" , func ( t * testing . T ) {
channelID := "1"
api := setupAPI ( )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( nil , nil )
p . API = api
shouldProcessMessage , _ := p . ShouldProcessMessage ( & model . Post { UserId : "1" , Type : model . POST_HEADER_CHANGE , ChannelId : channelID } ,
plugin . AllowSystemMessages ( ) , plugin . FilterChannelIDs ( [ ] string { channelID } ) , plugin . AllowBots ( ) , plugin . FilterUserIDs ( [ ] string { "1" } ) )
assert . True ( t , shouldProcessMessage )
} )
t . Run ( "should process the message when filter channel and filter users list is empty" , func ( t * testing . T ) {
channelID := "1"
api := setupAPI ( )
channel := model . Channel {
Name : "user1__" + expectedBotId ,
Type : model . CHANNEL_DIRECT ,
}
api . On ( "GetChannel" , channelID ) . Return ( & channel , nil )
api . On ( "KVGet" , plugin . BOT_USER_KEY ) . Return ( [ ] byte ( expectedBotId ) , nil )
p . API = api
shouldProcessMessage , _ := p . ShouldProcessMessage ( & model . Post { UserId : "1" , Type : model . POST_HEADER_CHANGE , ChannelId : channelID } ,
plugin . AllowSystemMessages ( ) , plugin . AllowBots ( ) )
assert . True ( t , shouldProcessMessage )
} )
}