Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MM-819]: Added testcases for multiple functions in command_hooks.go file #3

Closed
wants to merge 6 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions server/command_hooks_test.go
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,42 @@ func TestExecuteCommand(t *testing.T) {
time.Sleep(1 * time.Second)
})

t.Run("not enough permmission to export channel", func(t *testing.T) {
mockCtrl := gomock.NewController(t)

mockChannel := mock_pluginapi.NewMockChannel(mockCtrl)
mockFile := mock_pluginapi.NewMockFile(mockCtrl)
mockLog := mock_pluginapi.NewMockLog(mockCtrl)
mockPost := mock_pluginapi.NewMockPost(mockCtrl)
mockSlashCommand := mock_pluginapi.NewMockSlashCommand(mockCtrl)
mockUser := mock_pluginapi.NewMockUser(mockCtrl)
mockSystem := mock_pluginapi.NewMockSystem(mockCtrl)
mockConfiguration := mock_pluginapi.NewMockConfiguration(mockCtrl)
mockCluster := mock_pluginapi.NewMockCluster(mockCtrl)
mockCluster.EXPECT().NewMutex(gomock.Eq(KeyClusterMutex)).Return(pluginapi.NewClusterMutexMock(), nil)
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved

mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved

plugin, pluginContext := setupPlugin(t, mockAPI, time.Now())
plugin.setConfiguration(&configuration{EnableAdminRestrictions: true})

mockSystem.EXPECT().GetLicense().Return(&model.License{Features: &model.Features{
FutureFeatures: &trueValue,
}}).Times(2)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)
mockUser.EXPECT().HasPermissionTo("", model.PermissionManageSystem).Return(false)
mockUser.EXPECT().HasPermissionToChannel("", "channel_id", model.PermissionManageChannelRoles).Return(false)

commandResponse, appError := plugin.ExecuteCommand(pluginContext, &model.CommandArgs{
Command: "/export",
ChannelId: "channel_id",
})

require.Nil(t, appError)
assert.Equal(t, model.CommandResponseTypeEphemeral, commandResponse.ResponseType)
assert.Equal(t, "You do not have enough permissions to export this channel", commandResponse.Text)
})

t.Run("failed channel fetch", func(t *testing.T) {
mockCtrl := gomock.NewController(t)

Expand Down Expand Up @@ -424,3 +460,145 @@ func TestExecuteCommand(t *testing.T) {
wg.Wait()
})
}

func TestHasPermissionToExportChannel(t *testing.T) {
testCases := []struct {
name string
setupMocks func(mockUser *mock_pluginapi.MockUser, mockSystem *mock_pluginapi.MockSystem, mockConfiguration *mock_pluginapi.MockConfiguration)
expectedExportPermission bool
}{
{
name: "user does not have permission to manage channel and is not a system admin",
setupMocks: func(mockUser *mock_pluginapi.MockUser, mockSystem *mock_pluginapi.MockSystem, mockConfiguration *mock_pluginapi.MockConfiguration) {
mockUser.EXPECT().HasPermissionTo("userID", model.PermissionManageSystem).Return(false)
mockUser.EXPECT().HasPermissionToChannel("userID", "chanelID", model.PermissionManageChannelRoles).Return(false)
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
},
expectedExportPermission: false,
raghavaggarwal2308 marked this conversation as resolved.
Show resolved Hide resolved
},
{
name: "user has permission to manage channel but is not a system admin",
setupMocks: func(mockUser *mock_pluginapi.MockUser, mockSystem *mock_pluginapi.MockSystem, mockConfiguration *mock_pluginapi.MockConfiguration) {
mockUser.EXPECT().HasPermissionToChannel("userID", "chanelID", model.PermissionManageChannelRoles).Return(true)
},
expectedExportPermission: true,
},
{
name: "user is not having permission to manage the channel but is a system admin",
setupMocks: func(mockUser *mock_pluginapi.MockUser, mockSystem *mock_pluginapi.MockSystem, mockConfiguration *mock_pluginapi.MockConfiguration) {
mockUser.EXPECT().HasPermissionTo("userID", model.PermissionManageSystem).Return(true)
mockUser.EXPECT().HasPermissionToChannel("userID", "chanelID", model.PermissionManageChannelRoles).Return(false)
},
expectedExportPermission: true,
},
{
name: "user has permission to manage the channel and is a system admin",
setupMocks: func(mockUser *mock_pluginapi.MockUser, mockSystem *mock_pluginapi.MockSystem, mockConfiguration *mock_pluginapi.MockConfiguration) {
mockUser.EXPECT().HasPermissionToChannel("userID", "chanelID", model.PermissionManageChannelRoles).Return(true)
},
expectedExportPermission: true,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockChannel := mock_pluginapi.NewMockChannel(mockCtrl)
mockFile := mock_pluginapi.NewMockFile(mockCtrl)
mockLog := mock_pluginapi.NewMockLog(mockCtrl)
mockPost := mock_pluginapi.NewMockPost(mockCtrl)
mockSlashCommand := mock_pluginapi.NewMockSlashCommand(mockCtrl)
mockUser := mock_pluginapi.NewMockUser(mockCtrl)
mockSystem := mock_pluginapi.NewMockSystem(mockCtrl)
mockConfiguration := mock_pluginapi.NewMockConfiguration(mockCtrl)
mockCluster := mock_pluginapi.NewMockCluster(mockCtrl)

mockCluster.EXPECT().NewMutex(gomock.Eq(KeyClusterMutex)).Return(pluginapi.NewClusterMutexMock(), nil)

testCase.setupMocks(mockUser, mockSystem, mockConfiguration)

mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)

plugin, _ := setupPlugin(t, mockAPI, time.Now())
raghavaggarwal2308 marked this conversation as resolved.
Show resolved Hide resolved
plugin.setConfiguration(&configuration{EnableAdminRestrictions: true})

exportPermission := plugin.hasPermissionToExportChannel("userID", "chanelID")

assert.Equal(t, testCase.expectedExportPermission, exportPermission)
})
}
}

func TestUploadFileTo(t *testing.T) {
trueValue := true

testCases := []struct {
name string
setupMocks func(mockFile *mock_pluginapi.MockFile, mockLog *mock_pluginapi.MockLog, mockSystem *mock_pluginapi.MockSystem, mockConfiguration *mock_pluginapi.MockConfiguration)
expectedError string
expectedFile *model.FileInfo
}{
{
name: "file upload error",
setupMocks: func(mockFile *mock_pluginapi.MockFile, mockLog *mock_pluginapi.MockLog, mockSystem *mock_pluginapi.MockSystem, mockConfiguration *mock_pluginapi.MockConfiguration) {
mockSystem.EXPECT().GetLicense().Return(&model.License{Features: &model.Features{
FutureFeatures: &trueValue,
}}).AnyTimes()
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).AnyTimes()
mockLog.EXPECT().Error("unable to upload the exported file to the channel", "Channel ID", "channelID", "Error", gomock.Any())
mockFile.EXPECT().Upload(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("file upload error"))
},
expectedError: "unable to upload the exported file: file upload error",
},
{
name: "file upload successful",
setupMocks: func(mockFile *mock_pluginapi.MockFile, mockLog *mock_pluginapi.MockLog, mockSystem *mock_pluginapi.MockSystem, mockConfiguration *mock_pluginapi.MockConfiguration) {
mockSystem.EXPECT().GetLicense().Return(&model.License{Features: &model.Features{
FutureFeatures: &trueValue,
}}).AnyTimes()
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).AnyTimes()
var file *model.FileInfo
mockFile.EXPECT().Upload(gomock.Any(), gomock.Any(), gomock.Any()).Return(file, nil)
},
expectedFile: nil,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockChannel := mock_pluginapi.NewMockChannel(mockCtrl)
mockFile := mock_pluginapi.NewMockFile(mockCtrl)
mockLog := mock_pluginapi.NewMockLog(mockCtrl)
mockPost := mock_pluginapi.NewMockPost(mockCtrl)
mockSlashCommand := mock_pluginapi.NewMockSlashCommand(mockCtrl)
mockUser := mock_pluginapi.NewMockUser(mockCtrl)
mockSystem := mock_pluginapi.NewMockSystem(mockCtrl)
mockConfiguration := mock_pluginapi.NewMockConfiguration(mockCtrl)
mockCluster := mock_pluginapi.NewMockCluster(mockCtrl)

mockCluster.EXPECT().NewMutex(gomock.Eq(KeyClusterMutex)).Return(pluginapi.NewClusterMutexMock(), nil)

testCase.setupMocks(mockFile, mockLog, mockSystem, mockConfiguration)

mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)

plugin, _ := setupPlugin(t, mockAPI, time.Now())
plugin.setConfiguration(&configuration{EnableAdminRestrictions: true})

var content io.Reader
uploadedFile, err := plugin.uploadFileTo("fileName", content, "channelID")

if testCase.expectedError != "" {
assert.Error(t, err)
assert.Equal(t, testCase.expectedError, err.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, testCase.expectedFile, uploadedFile)
}
})
}
}