[MM-16709] Add helper method for plugins using MessageHasBeenPosted (#12539)
The PR introduces a method in helper interface. The method has common code for filtering to be used across plugins which use MessageHasBeenPosted.
Этот коммит содержится в:
коммит произвёл
Ben Schumacher
родитель
9b6a0674f7
Коммит
f2fcfa8f9d
@@ -340,3 +340,111 @@ func TestEnsureBot(t *testing.T) {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user