Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
415 lines
8.4 KiB
Go
415 lines
8.4 KiB
Go
package poster
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
|
|
"github.com/mattermost/mattermost/server/public/pluginapi"
|
|
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/poster/mock_import"
|
|
)
|
|
|
|
const (
|
|
botID = "test-bot-user"
|
|
userID = "test-user-1"
|
|
dmChannelID = "dm-channel-id"
|
|
)
|
|
|
|
func TestInterface(t *testing.T) {
|
|
t.Run("Plugin API satisfy the interface", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
driver := &plugintest.Driver{}
|
|
client := pluginapi.NewClient(api, driver)
|
|
_ = NewPoster(&client.Post, botID)
|
|
})
|
|
}
|
|
|
|
func TestDM(t *testing.T) {
|
|
format := "test format, string: %s int: %d value: %v"
|
|
args := []any{"some string", 5, 8.423}
|
|
expectedMessage := "test format, string: some string int: 5 value: 8.423"
|
|
|
|
expectedPostID := "expected-post-id"
|
|
|
|
post := &model.Post{
|
|
Message: expectedMessage,
|
|
}
|
|
|
|
postWithID := model.Post{
|
|
Id: expectedPostID,
|
|
UserId: botID,
|
|
ChannelId: dmChannelID,
|
|
Message: expectedMessage,
|
|
}
|
|
|
|
mockError := errors.New("mock error")
|
|
|
|
t.Run("DM Success", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
//nolint:govet //copy lock, but only used in tests
|
|
postAPI.
|
|
EXPECT().
|
|
DM(botID, userID, post).
|
|
SetArg(2, postWithID).
|
|
Return(nil).
|
|
Times(1)
|
|
|
|
postID, err := poster.DM(userID, format, args...)
|
|
assert.Equal(t, expectedPostID, postID)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("DM error", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
DM(botID, userID, post).
|
|
Return(mockError).
|
|
Times(1)
|
|
|
|
_, err := poster.DM(userID, format, args...)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestDMWithAttachments(t *testing.T) {
|
|
expectedPostID := "expected-post-id"
|
|
|
|
attachments := []*model.MessageAttachment{
|
|
{},
|
|
{},
|
|
}
|
|
|
|
post := &model.Post{}
|
|
|
|
model.ParseMessageAttachment(post, attachments)
|
|
|
|
postWithID := model.Post{
|
|
Id: expectedPostID,
|
|
UserId: botID,
|
|
ChannelId: dmChannelID,
|
|
Type: model.PostTypeMessageAttachment,
|
|
Props: model.StringInterface{
|
|
model.PostPropsAttachments: attachments,
|
|
},
|
|
}
|
|
|
|
mockError := errors.New("mock error")
|
|
t.Run("DM Success", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
//nolint:govet //copy lock, but only used in tests
|
|
postAPI.
|
|
EXPECT().
|
|
DM(botID, userID, post).
|
|
SetArg(2, postWithID).
|
|
Return(nil).
|
|
Times(1)
|
|
|
|
postID, err := poster.DMWithAttachments(userID, attachments...)
|
|
assert.Equal(t, expectedPostID, postID)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("DM error", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
DM(botID, userID, post).
|
|
Return(mockError).
|
|
Times(1)
|
|
|
|
_, err := poster.DMWithAttachments(userID, attachments...)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestEphemeral(t *testing.T) {
|
|
format := "test format, string: %s int: %d value: %v"
|
|
args := []any{"some string", 5, 8.423}
|
|
expectedMessage := "test format, string: some string int: 5 value: 8.423"
|
|
|
|
channelID := "some-channel"
|
|
|
|
post := &model.Post{
|
|
UserId: botID,
|
|
ChannelId: channelID,
|
|
Message: expectedMessage,
|
|
}
|
|
|
|
expectedPostID := "some-post-ID"
|
|
|
|
postWithID := model.Post{
|
|
Id: expectedPostID,
|
|
UserId: botID,
|
|
ChannelId: channelID,
|
|
Message: expectedMessage,
|
|
}
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
//nolint:govet //copy lock, but only used in tests
|
|
postAPI.
|
|
EXPECT().
|
|
SendEphemeralPost(userID, post).
|
|
SetArg(1, postWithID).
|
|
Times(1)
|
|
|
|
poster.Ephemeral(userID, channelID, format, args...)
|
|
})
|
|
}
|
|
|
|
func TestUpdatePostByID(t *testing.T) {
|
|
format := "test format, string: %s int: %d value: %v"
|
|
args := []any{"some string", 5, 8.423}
|
|
expectedMessage := "test format, string: some string int: 5 value: 8.423"
|
|
|
|
postID := "some-post-id"
|
|
originalPost := &model.Post{
|
|
Id: postID,
|
|
Message: "some message",
|
|
}
|
|
|
|
updatedPost := &model.Post{
|
|
Id: postID,
|
|
Message: expectedMessage,
|
|
}
|
|
|
|
mockError := errors.New("mock error")
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
GetPost(postID).
|
|
Return(originalPost, nil).
|
|
Times(1)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
UpdatePost(updatedPost).
|
|
Return(nil).
|
|
Times(1)
|
|
|
|
err := poster.UpdatePostByID(postID, format, args...)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("Error fetching", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
GetPost(postID).
|
|
Return(nil, mockError).
|
|
Times(1)
|
|
|
|
err := poster.UpdatePostByID(postID, format, args...)
|
|
assert.Error(t, err)
|
|
})
|
|
|
|
t.Run("Error updating", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
GetPost(postID).
|
|
Return(originalPost, nil).
|
|
Times(1)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
UpdatePost(updatedPost).
|
|
Return(mockError).
|
|
Times(1)
|
|
|
|
err := poster.UpdatePostByID(postID, format, args...)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestDeletePost(t *testing.T) {
|
|
postID := "some-post-id"
|
|
|
|
mockError := errors.New("mock channel error")
|
|
t.Run("Success", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
DeletePost(postID).
|
|
Return(nil).
|
|
Times(1)
|
|
|
|
err := poster.DeletePost(postID)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("Error", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
DeletePost(postID).
|
|
Return(mockError).
|
|
Times(1)
|
|
|
|
err := poster.DeletePost(postID)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestUpdatePost(t *testing.T) {
|
|
post := &model.Post{
|
|
Id: "some-post-id",
|
|
Message: "some message",
|
|
}
|
|
|
|
mockError := errors.New("mock channel error")
|
|
t.Run("Success", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
UpdatePost(post).
|
|
Return(nil).
|
|
Times(1)
|
|
|
|
err := poster.UpdatePost(post)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("Error", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
postAPI.
|
|
EXPECT().
|
|
UpdatePost(post).
|
|
Return(mockError).
|
|
Times(1)
|
|
|
|
err := poster.UpdatePost(post)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestUpdatePosterID(t *testing.T) {
|
|
format := "test format, string: %s int: %d value: %v"
|
|
args := []any{"some string", 5, 8.423}
|
|
expectedMessage := "test format, string: some string int: 5 value: 8.423"
|
|
|
|
expectedPostID := "expected-post-id"
|
|
|
|
post := &model.Post{
|
|
Message: expectedMessage,
|
|
}
|
|
|
|
postWithID := model.Post{
|
|
Id: expectedPostID,
|
|
UserId: botID,
|
|
ChannelId: dmChannelID,
|
|
Message: expectedMessage,
|
|
}
|
|
|
|
newBotID := "new-bot-id"
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
postAPI := mock_import.NewMockPostAPI(ctrl)
|
|
|
|
poster := NewPoster(postAPI, botID)
|
|
|
|
//nolint:govet //copy lock, but only used in tests
|
|
postAPI.
|
|
EXPECT().
|
|
DM(botID, userID, post).
|
|
SetArg(2, postWithID).
|
|
Return(nil).
|
|
Times(1)
|
|
|
|
_, _ = poster.DM(userID, format, args...)
|
|
poster.UpdatePosterID(newBotID)
|
|
|
|
//nolint:govet //copy lock, but only used in tests
|
|
postAPI.
|
|
EXPECT().
|
|
DM(newBotID, userID, post).
|
|
SetArg(2, postWithID).
|
|
Return(nil).
|
|
Times(1)
|
|
|
|
_, _ = poster.DM(userID, format, args...)
|
|
})
|
|
}
|