[MM-20723] Add golint for plugin package to golangci (#13090)

Этот коммит содержится в:
Ben Schumacher
2019-12-05 20:31:53 +01:00
коммит произвёл GitHub
родитель d12bf77f53
Коммит 5122b9e292
12 изменённых файлов: 117 добавлений и 115 удалений

Просмотреть файл

@@ -48,8 +48,8 @@ func TestEnsureBot(t *testing.T) {
p := &plugin.HelpersImpl{}
p.API = api
botId, err := p.EnsureBot(nil)
assert.Equal(t, "", botId)
botID, err := p.EnsureBot(nil)
assert.Equal(t, "", botID)
assert.NotNil(t, err)
})
t.Run("bad username", func(t *testing.T) {
@@ -58,29 +58,29 @@ func TestEnsureBot(t *testing.T) {
p := &plugin.HelpersImpl{}
p.API = api
botId, err := p.EnsureBot(&model.Bot{
botID, err := p.EnsureBot(&model.Bot{
Username: "",
})
assert.Equal(t, "", botId)
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()
expectedBotID := model.NewId()
api := setupAPI()
api.On("GetServerVersion").Return("5.10.0")
api.On("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotId), nil)
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)
botID, err := p.EnsureBot(testbot)
assert.Equal(t, expectedBotId, botId)
assert.Equal(t, expectedBotID, botID)
assert.Nil(t, err)
})
@@ -93,23 +93,23 @@ func TestEnsureBot(t *testing.T) {
p := &plugin.HelpersImpl{}
p.API = api
botId, err := p.EnsureBot(testbot)
botID, err := p.EnsureBot(testbot)
assert.Equal(t, "", botId)
assert.Equal(t, "", botID)
assert.NotNil(t, err)
})
t.Run("should set the bot profile image when specified", func(t *testing.T) {
expectedBotId := model.NewId()
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("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotID), nil)
api.On("GetBundlePath").Return("", nil)
api.On("SetProfileImage", expectedBotId, imageBytes).Return(nil)
api.On("SetProfileImage", expectedBotID, imageBytes).Return(nil)
api.On("GetServerVersion").Return("5.10.0")
defer api.AssertExpectations(t)
@@ -118,13 +118,13 @@ func TestEnsureBot(t *testing.T) {
assert.Nil(t, err)
botId, err := p.EnsureBot(testbot, plugin.ProfileImagePath(testImage))
assert.Equal(t, expectedBotId, botId)
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()
expectedBotID := model.NewId()
api := setupAPI()
testsDir, _ := fileutils.FindDir("tests")
@@ -132,22 +132,22 @@ func TestEnsureBot(t *testing.T) {
imageBytes, err := ioutil.ReadFile(testImage)
assert.Nil(t, err)
api.On("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotId), nil)
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("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)
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()
expectedBotID := model.NewId()
api := setupAPI()
testsDir, _ := fileutils.FindDir("tests")
@@ -155,74 +155,74 @@ func TestEnsureBot(t *testing.T) {
imageBytes, err := ioutil.ReadFile(testImage)
assert.Nil(t, err)
api.On("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotId), nil)
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("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)
botID, err := p.EnsureBot(testbot, plugin.ProfileImagePath(testImage), plugin.IconImagePath(testImage))
assert.Equal(t, expectedBotID, botID)
assert.Nil(t, err)
})
})
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()
expectedBotID := model.NewId()
api := setupAPI()
api.On("GetServerVersion").Return("5.10.0")
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,
UserId: expectedBotID,
}, nil)
api.On("KVSet", plugin.BOT_USER_KEY, []byte(expectedBotId)).Return(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)
botID, err := p.EnsureBot(testbot)
assert.Equal(t, expectedBotId, botId)
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()
expectedBotID := model.NewId()
api := setupAPI()
api.On("GetServerVersion").Return("5.10.0")
api.On("KVGet", plugin.BOT_USER_KEY).Return(nil, nil)
api.On("GetUserByUsername", testbot.Username).Return(&model.User{
Id: expectedBotId,
Id: expectedBotID,
IsBot: true,
}, nil)
api.On("KVSet", plugin.BOT_USER_KEY, []byte(expectedBotId)).Return(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)
botID, err := p.EnsureBot(testbot)
assert.Equal(t, expectedBotId, botId)
assert.Equal(t, expectedBotID, botID)
assert.Nil(t, err)
})
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()
expectedBotID := model.NewId()
api := setupAPI()
api.On("GetServerVersion").Return("5.10.0")
api.On("KVGet", plugin.BOT_USER_KEY).Return(nil, nil)
api.On("GetUserByUsername", testbot.Username).Return(&model.User{
Id: expectedBotId,
Id: expectedBotID,
IsBot: false,
}, nil)
api.On("LogError", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return()
@@ -231,9 +231,9 @@ func TestEnsureBot(t *testing.T) {
p := &plugin.HelpersImpl{}
p.API = api
botId, err := p.EnsureBot(testbot)
botID, err := p.EnsureBot(testbot)
assert.Equal(t, expectedBotId, botId)
assert.Equal(t, expectedBotID, botID)
assert.Nil(t, err)
})
@@ -248,14 +248,14 @@ func TestEnsureBot(t *testing.T) {
p := &plugin.HelpersImpl{}
p.API = api
botId, err := p.EnsureBot(testbot)
botID, err := p.EnsureBot(testbot)
assert.Equal(t, "", botId)
assert.Equal(t, "", botID)
assert.NotNil(t, err)
})
t.Run("should create bot and set the bot profile image when specified", func(t *testing.T) {
expectedBotId := model.NewId()
expectedBotID := model.NewId()
api := setupAPI()
testsDir, _ := fileutils.FindDir("tests")
@@ -266,24 +266,24 @@ func TestEnsureBot(t *testing.T) {
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,
UserId: expectedBotID,
}, nil)
api.On("KVSet", plugin.BOT_USER_KEY, []byte(expectedBotId)).Return(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("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)
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()
expectedBotID := model.NewId()
api := setupAPI()
testsDir, _ := fileutils.FindDir("tests")
@@ -294,24 +294,24 @@ func TestEnsureBot(t *testing.T) {
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,
UserId: expectedBotID,
}, nil)
api.On("KVSet", plugin.BOT_USER_KEY, []byte(expectedBotId)).Return(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("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)
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()
expectedBotID := model.NewId()
api := setupAPI()
testsDir, _ := fileutils.FindDir("tests")
@@ -322,20 +322,20 @@ func TestEnsureBot(t *testing.T) {
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,
UserId: expectedBotID,
}, nil)
api.On("KVSet", plugin.BOT_USER_KEY, []byte(expectedBotId)).Return(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("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)
botID, err := p.EnsureBot(testbot, plugin.ProfileImagePath(testImage), plugin.IconImagePath(testImage))
assert.Equal(t, expectedBotID, botID)
assert.Nil(t, err)
})
})
@@ -343,7 +343,7 @@ func TestEnsureBot(t *testing.T) {
func TestShouldProcessMessage(t *testing.T) {
p := &plugin.HelpersImpl{}
expectedBotId := model.NewId()
expectedBotID := model.NewId()
setupAPI := func() *plugintest.API {
return &plugintest.API{}
@@ -351,9 +351,9 @@ func TestShouldProcessMessage(t *testing.T) {
t.Run("should not respond to itself", func(t *testing.T) {
api := setupAPI()
api.On("KVGet", plugin.BOT_USER_KEY).Return([]byte(expectedBotId), nil)
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())
shouldProcessMessage, _ := p.ShouldProcessMessage(&model.Post{Type: model.POST_HEADER_CHANGE, UserId: expectedBotID}, plugin.AllowSystemMessages(), plugin.AllowBots())
assert.False(t, shouldProcessMessage)
})
@@ -369,7 +369,7 @@ func TestShouldProcessMessage(t *testing.T) {
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)
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"}))
@@ -382,7 +382,7 @@ func TestShouldProcessMessage(t *testing.T) {
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)
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"}))
@@ -394,12 +394,12 @@ func TestShouldProcessMessage(t *testing.T) {
userID := "user-id"
channelID := "1"
channel := model.Channel{
Name: "user1__" + expectedBotId,
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)
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())
@@ -410,7 +410,7 @@ func TestShouldProcessMessage(t *testing.T) {
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)
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},
@@ -435,11 +435,11 @@ func TestShouldProcessMessage(t *testing.T) {
channelID := "1"
api := setupAPI()
channel := model.Channel{
Name: "user1__" + expectedBotId,
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)
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},