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 Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
444 lines
13 KiB
Go
444 lines
13 KiB
Go
package pluginapi_test
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
|
|
"github.com/mattermost/mattermost/server/public/pluginapi"
|
|
)
|
|
|
|
func TestGetMembers(t *testing.T) {
|
|
t.Run("empty list", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
api.On("GetChannelMembers", "channelID", 1, 10).Return(nil, nil)
|
|
|
|
cm, err := client.Channel.ListMembers("channelID", 1, 10)
|
|
require.NoError(t, err)
|
|
require.Empty(t, cm)
|
|
})
|
|
}
|
|
|
|
func TestRestoreChannel(t *testing.T) {
|
|
t.Run("success", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
api.On("RestoreChannel", "channelID").Return(nil)
|
|
|
|
err := client.Channel.Restore("channelID")
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("failure", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
api.On("RestoreChannel", "channelID").Return(newAppError())
|
|
|
|
err := client.Channel.Restore("channelID")
|
|
require.EqualError(t, err, "here: id, an error occurred")
|
|
})
|
|
}
|
|
|
|
func TestGetChannelOfType(t *testing.T) {
|
|
t.Run("success", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
expected := &model.Channel{Id: "channelID", Type: model.ChannelTypeSpace}
|
|
api.On("GetChannelOfType", "channelID", model.ChannelTypeSpace).Return(expected, nil)
|
|
|
|
channel, err := client.Channel.GetChannelOfType("channelID", model.ChannelTypeSpace)
|
|
require.NoError(t, err)
|
|
require.Equal(t, expected, channel)
|
|
})
|
|
|
|
t.Run("failure", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
api.On("GetChannelOfType", "channelID", model.ChannelTypeSpace).Return(nil, newAppError())
|
|
|
|
_, err := client.Channel.GetChannelOfType("channelID", model.ChannelTypeSpace)
|
|
require.EqualError(t, err, "here: id, an error occurred")
|
|
})
|
|
}
|
|
|
|
func TestGetTeamChannelByName(t *testing.T) {
|
|
t.Run("success", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
api.On("GetChannelByNameForTeamName", "1", "2", true).Return(&model.Channel{TeamId: "3"}, nil)
|
|
|
|
channel, err := client.Channel.GetByNameForTeamName("1", "2", true)
|
|
require.NoError(t, err)
|
|
require.Equal(t, &model.Channel{TeamId: "3"}, channel)
|
|
})
|
|
|
|
t.Run("failure", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
api.On("GetChannelByNameForTeamName", "1", "2", true).Return(nil, newAppError())
|
|
|
|
channel, err := client.Channel.GetByNameForTeamName("1", "2", true)
|
|
require.EqualError(t, err, "here: id, an error occurred")
|
|
require.Zero(t, channel)
|
|
})
|
|
}
|
|
|
|
func TestGetTeamUserChannels(t *testing.T) {
|
|
t.Run("success", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
api.On("GetChannelsForTeamForUser", "1", "2", true).Return([]*model.Channel{{TeamId: "3"}, {TeamId: "4"}}, nil)
|
|
|
|
channels, err := client.Channel.ListForTeamForUser("1", "2", true)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []*model.Channel{{TeamId: "3"}, {TeamId: "4"}}, channels)
|
|
})
|
|
|
|
t.Run("failure", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
appErr := model.NewAppError("here", "id", nil, "an error occurred", http.StatusInternalServerError)
|
|
|
|
api.On("GetChannelsForTeamForUser", "1", "2", true).Return(nil, appErr)
|
|
|
|
channels, err := client.Channel.ListForTeamForUser("1", "2", true)
|
|
require.Equal(t, appErr, err)
|
|
require.Len(t, channels, 0)
|
|
})
|
|
}
|
|
|
|
func TestGetPublicTeamChannels(t *testing.T) {
|
|
t.Run("success", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
api.On("GetPublicChannelsForTeam", "1", 2, 3).Return([]*model.Channel{{TeamId: "3"}, {TeamId: "4"}}, nil)
|
|
|
|
channels, err := client.Channel.ListPublicChannelsForTeam("1", 2, 3)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []*model.Channel{{TeamId: "3"}, {TeamId: "4"}}, channels)
|
|
})
|
|
|
|
t.Run("failure", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
appErr := model.NewAppError("here", "id", nil, "an error occurred", http.StatusInternalServerError)
|
|
|
|
api.On("GetPublicChannelsForTeam", "1", 2, 3).Return(nil, appErr)
|
|
|
|
channels, err := client.Channel.ListPublicChannelsForTeam("1", 2, 3)
|
|
require.Equal(t, appErr, err)
|
|
require.Len(t, channels, 0)
|
|
})
|
|
}
|
|
|
|
func TestCreateChannel(t *testing.T) {
|
|
t.Run("create channel with no replicas", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
config := &model.Config{
|
|
SqlSettings: model.SqlSettings{
|
|
DataSourceReplicas: []string{},
|
|
},
|
|
}
|
|
api.On("GetConfig").Return(config).Once()
|
|
|
|
c := &model.Channel{
|
|
Id: model.NewId(),
|
|
Name: "name",
|
|
DisplayName: "displayname",
|
|
}
|
|
api.On("CreateChannel", c).Return(c, nil).Once()
|
|
|
|
err := client.Channel.Create(c)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("create space channel with replicas skips the wait", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
c := &model.Channel{
|
|
Id: model.NewId(),
|
|
Name: "name",
|
|
DisplayName: "displayname",
|
|
Type: model.ChannelTypeSpace,
|
|
}
|
|
// The generic GetChannel the replica wait polls excludes space channels, so waiting
|
|
// would always time out; Create must return without consulting the config or polling.
|
|
api.On("CreateChannel", c).Return(c, nil).Once()
|
|
|
|
err := client.Channel.Create(c)
|
|
require.NoError(t, err)
|
|
api.AssertNotCalled(t, "GetChannel", c.Id)
|
|
})
|
|
|
|
t.Run("create channel and wait once", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
config := &model.Config{
|
|
SqlSettings: model.SqlSettings{
|
|
DataSourceReplicas: []string{"replica1"},
|
|
},
|
|
}
|
|
api.On("GetConfig").Return(config).Once()
|
|
|
|
c := &model.Channel{
|
|
Id: model.NewId(),
|
|
Name: "name",
|
|
DisplayName: "displayname",
|
|
}
|
|
api.On("CreateChannel", c).Return(c, nil).Once()
|
|
api.On("GetChannel", c.Id).Return(c, nil).Once()
|
|
|
|
err := client.Channel.Create(c)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("create channel and wait multiple times", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
config := &model.Config{
|
|
SqlSettings: model.SqlSettings{
|
|
DataSourceReplicas: []string{"replica1"},
|
|
},
|
|
}
|
|
api.On("GetConfig").Return(config).Once()
|
|
|
|
c := &model.Channel{
|
|
Id: model.NewId(),
|
|
Name: "name",
|
|
DisplayName: "displayname",
|
|
}
|
|
api.On("CreateChannel", c).Return(c, nil).Once()
|
|
|
|
notFoundErr := model.NewAppError("", "", nil, "", http.StatusNotFound)
|
|
api.On("GetChannel", c.Id).Return(c, notFoundErr).Times(3)
|
|
api.On("GetChannel", c.Id).Return(c, nil).Times(1)
|
|
|
|
err := client.Channel.Create(c)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("create channel, wait multiple times and return error", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
config := &model.Config{
|
|
SqlSettings: model.SqlSettings{
|
|
DataSourceReplicas: []string{"replica1"},
|
|
},
|
|
}
|
|
api.On("GetConfig").Return(config).Once()
|
|
|
|
c := &model.Channel{
|
|
Id: model.NewId(),
|
|
Name: "name",
|
|
DisplayName: "displayname",
|
|
}
|
|
api.On("CreateChannel", c).Return(c, nil).Once()
|
|
|
|
notFoundErr := model.NewAppError("", "", nil, "", http.StatusNotFound)
|
|
api.On("GetChannel", c.Id).Return(c, notFoundErr).Times(3)
|
|
|
|
otherErr := model.NewAppError("", "", nil, "", http.StatusInternalServerError)
|
|
api.On("GetChannel", c.Id).Return(c, otherErr).Times(1)
|
|
|
|
err := client.Channel.Create(c)
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("create channel, give up waiting", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
config := &model.Config{
|
|
SqlSettings: model.SqlSettings{
|
|
DataSourceReplicas: []string{"replica1"},
|
|
},
|
|
}
|
|
api.On("GetConfig").Return(config).Once()
|
|
|
|
c := &model.Channel{
|
|
Id: model.NewId(),
|
|
Name: "name",
|
|
DisplayName: "displayname",
|
|
}
|
|
api.On("CreateChannel", c).Return(c, nil).Once()
|
|
|
|
notFoundErr := model.NewAppError("", "", nil, "", http.StatusNotFound)
|
|
api.On("GetChannel", c.Id).Return(c, notFoundErr)
|
|
|
|
err := client.Channel.Create(c)
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "giving up waiting")
|
|
})
|
|
}
|
|
|
|
func TestCreateSidebarCategory(t *testing.T) {
|
|
t.Run("success", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
category := model.SidebarCategoryWithChannels{}
|
|
|
|
api.On("CreateChannelSidebarCategory", "user_id", "team_id", &category).
|
|
Return(&model.SidebarCategoryWithChannels{
|
|
SidebarCategory: model.SidebarCategory{
|
|
Id: "id",
|
|
UserId: "user_id",
|
|
TeamId: "team_id",
|
|
},
|
|
Channels: []string{"channelA", "channelB"}},
|
|
nil)
|
|
|
|
err := client.Channel.CreateSidebarCategory("user_id", "team_id", &category)
|
|
|
|
require.NoError(t, err)
|
|
require.Equal(t,
|
|
model.SidebarCategoryWithChannels{
|
|
SidebarCategory: model.SidebarCategory{Id: "id", UserId: "user_id", TeamId: "team_id"},
|
|
Channels: []string{"channelA", "channelB"}},
|
|
category)
|
|
})
|
|
|
|
t.Run("failure", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
inputCategory := model.SidebarCategoryWithChannels{}
|
|
appErr := model.NewAppError("here", "id", nil, "an error occurred", http.StatusInternalServerError)
|
|
|
|
api.On("CreateChannelSidebarCategory", "user_id", "team_id", &inputCategory).
|
|
Return(&model.SidebarCategoryWithChannels{}, appErr)
|
|
|
|
err := client.Channel.CreateSidebarCategory("user_id", "team_id", &inputCategory)
|
|
|
|
require.Equal(t, appErr, err)
|
|
})
|
|
}
|
|
|
|
func TestGetSidebarCategories(t *testing.T) {
|
|
t.Run("success", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
api.On("GetChannelSidebarCategories", "user_id", "team_id").
|
|
Return(&model.OrderedSidebarCategories{
|
|
Categories: nil,
|
|
Order: []string{"channelA", "channelB"},
|
|
},
|
|
nil)
|
|
|
|
categories, err := client.Channel.GetSidebarCategories("user_id", "team_id")
|
|
|
|
require.NoError(t, err)
|
|
require.Equal(t,
|
|
model.OrderedSidebarCategories{
|
|
Categories: nil,
|
|
Order: []string{"channelA", "channelB"},
|
|
},
|
|
*categories)
|
|
})
|
|
|
|
t.Run("failure", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
defer api.AssertExpectations(t)
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
appErr := model.NewAppError("here", "id", nil, "an error occurred", http.StatusInternalServerError)
|
|
|
|
api.On("GetChannelSidebarCategories", "user_id", "team_id").Return(nil, appErr)
|
|
|
|
_, err := client.Channel.GetSidebarCategories("user_id", "team_id")
|
|
|
|
require.Equal(t, appErr, err)
|
|
})
|
|
}
|
|
|
|
func TestUpdateSidebarCategories(t *testing.T) {
|
|
t.Run("success", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
categories := []*model.SidebarCategoryWithChannels{
|
|
{
|
|
SidebarCategory: model.SidebarCategory{},
|
|
Channels: nil,
|
|
},
|
|
}
|
|
updatedCategories := []*model.SidebarCategoryWithChannels{
|
|
{
|
|
SidebarCategory: model.SidebarCategory{
|
|
Id: "id",
|
|
UserId: "user_id",
|
|
TeamId: "team_id",
|
|
},
|
|
Channels: []string{"channelA", "channelB"},
|
|
}}
|
|
|
|
api.On("UpdateChannelSidebarCategories", "user_id", "team_id", categories).Return(updatedCategories, nil)
|
|
|
|
err := client.Channel.UpdateSidebarCategories("user_id", "team_id", categories)
|
|
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, updatedCategories, categories)
|
|
})
|
|
|
|
t.Run("failure", func(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
client := pluginapi.NewClient(api, &plugintest.Driver{})
|
|
|
|
inputCategories := []*model.SidebarCategoryWithChannels{
|
|
{
|
|
SidebarCategory: model.SidebarCategory{},
|
|
Channels: nil,
|
|
},
|
|
}
|
|
appErr := model.NewAppError("here", "id", nil, "an error occurred", http.StatusInternalServerError)
|
|
|
|
api.On("UpdateChannelSidebarCategories", "user_id", "team_id", inputCategories).Return(nil, appErr)
|
|
|
|
err := client.Channel.UpdateSidebarCategories("user_id", "team_id", inputCategories)
|
|
|
|
require.Equal(t, appErr, err)
|
|
})
|
|
}
|