-
Notifications
You must be signed in to change notification settings - Fork 90
MM-67398 Added checks for instance and webhooks commands #635
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -550,6 +550,192 @@ func setupInstanceCommandTest(t *testing.T, instanceList []string, instanceConfi | |
| return p, &capturedMessage, api | ||
| } | ||
|
|
||
| func TestAdminProtectedCommands(t *testing.T) { | ||
| t.Run("webhook command requires admin", func(t *testing.T) { | ||
| p := new(Plugin) | ||
| p.configuration = &configuration{EncryptionKey: testEncryptionKey} | ||
|
|
||
| // Mock a non-admin user | ||
| nonAdminUser := &model.User{ | ||
| Id: "user_id", | ||
| Roles: "system_user", | ||
| } | ||
|
|
||
| api := &plugintest.API{} | ||
| api.On("GetUser", "user_id").Return(nonAdminUser, nil) | ||
|
|
||
| var capturedMessage string | ||
| api.On("SendEphemeralPost", mock.Anything, mock.MatchedBy(func(post *model.Post) bool { | ||
| capturedMessage = post.Message | ||
| return true | ||
| })).Return(&model.Post{}) | ||
|
|
||
| p.SetAPI(api) | ||
| p.client = pluginapi.NewClient(api, p.Driver) | ||
|
|
||
| args := &model.CommandArgs{UserId: "user_id", ChannelId: "channel_id"} | ||
| userInfo := &gitlab.UserInfo{UserID: "user_id"} | ||
|
|
||
| _, _ = p.handleWebhookHandler(context.Background(), args, []string{"list", "group/project"}, userInfo) | ||
|
|
||
| assert.Contains(t, capturedMessage, "Only System Admins are allowed to manage webhooks.") | ||
| }) | ||
|
|
||
| t.Run("webhook command allowed for admin", func(t *testing.T) { | ||
| p := new(Plugin) | ||
|
|
||
| mockCtrl := gomock.NewController(t) | ||
| mockedClient := mocks.NewMockGitlab(mockCtrl) | ||
|
|
||
| // Mock admin user | ||
| adminUser := &model.User{ | ||
| Id: "admin_id", | ||
| Roles: "system_admin system_user", | ||
| } | ||
|
|
||
| encryptedToken, _ := encrypt([]byte(testEncryptionKey), testGitlabToken) | ||
|
|
||
| p.configuration = &configuration{ | ||
| EncryptionKey: testEncryptionKey, | ||
| EnablePrivateRepo: true, | ||
| } | ||
|
|
||
| mockedClient.EXPECT().ResolveNamespaceAndProject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), true).Return("group", "project", nil) | ||
| mockedClient.EXPECT().GetProjectHooks(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]*gitlab.WebhookInfo{}, nil) | ||
| p.GitlabClient = mockedClient | ||
|
|
||
| api := &plugintest.API{} | ||
| api.On("GetUser", "admin_id").Return(adminUser, nil) | ||
| api.On("KVGet", "admin_id_usertoken").Return([]byte(encryptedToken), nil) | ||
|
|
||
| var capturedMessage string | ||
| api.On("SendEphemeralPost", mock.Anything, mock.MatchedBy(func(post *model.Post) bool { | ||
| capturedMessage = post.Message | ||
| return true | ||
| })).Return(&model.Post{}) | ||
|
|
||
| p.SetAPI(api) | ||
| p.client = pluginapi.NewClient(api, p.Driver) | ||
|
|
||
| args := &model.CommandArgs{UserId: "admin_id", ChannelId: "channel_id"} | ||
| userInfo := &gitlab.UserInfo{UserID: "admin_id"} | ||
|
|
||
| _, _ = p.handleWebhookHandler(context.Background(), args, []string{"list", "group/project"}, userInfo) | ||
|
|
||
| assert.Contains(t, capturedMessage, "No webhooks found") | ||
| }) | ||
|
|
||
| t.Run("install instance requires admin", func(t *testing.T) { | ||
| p := new(Plugin) | ||
| p.configuration = &configuration{EncryptionKey: testEncryptionKey} | ||
|
|
||
| // Mock a non-admin user | ||
| nonAdminUser := &model.User{ | ||
| Id: "user_id", | ||
| Roles: "system_user", | ||
| } | ||
|
|
||
| api := &plugintest.API{} | ||
| api.On("GetUser", "user_id").Return(nonAdminUser, nil) | ||
|
|
||
| var capturedMessage string | ||
| api.On("SendEphemeralPost", mock.Anything, mock.MatchedBy(func(post *model.Post) bool { | ||
| capturedMessage = post.Message | ||
| return true | ||
| })).Return(&model.Post{}) | ||
|
|
||
| p.SetAPI(api) | ||
| p.client = pluginapi.NewClient(api, p.Driver) | ||
|
|
||
| args := &model.CommandArgs{UserId: "user_id", ChannelId: "channel_id"} | ||
|
|
||
| _, _ = p.handleInstallInstance(args, []string{}) | ||
|
|
||
| assert.Contains(t, capturedMessage, "Only System Admins are allowed to set up the plugin.") | ||
|
||
| }) | ||
|
|
||
| t.Run("instance commands require admin", func(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| parameters []string | ||
| }{ | ||
| {"list", []string{"list"}}, | ||
| {"install", []string{"install"}}, | ||
| {"uninstall", []string{"uninstall", "test-instance"}}, | ||
| {"set-default", []string{"set-default", "test-instance"}}, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| p := new(Plugin) | ||
| p.configuration = &configuration{EncryptionKey: testEncryptionKey} | ||
|
|
||
| // Mock a non-admin user | ||
| nonAdminUser := &model.User{ | ||
| Id: "user_id", | ||
| Roles: "system_user", | ||
| } | ||
|
|
||
| api := &plugintest.API{} | ||
| api.On("GetUser", "user_id").Return(nonAdminUser, nil) | ||
|
|
||
| var capturedMessage string | ||
| api.On("SendEphemeralPost", mock.Anything, mock.MatchedBy(func(post *model.Post) bool { | ||
| capturedMessage = post.Message | ||
| return true | ||
| })).Return(&model.Post{}) | ||
|
|
||
| p.SetAPI(api) | ||
| p.client = pluginapi.NewClient(api, p.Driver) | ||
|
|
||
| args := &model.CommandArgs{UserId: "user_id", ChannelId: "channel_id"} | ||
|
|
||
| _, _ = p.handleInstance(args, tc.parameters) | ||
|
|
||
| assert.Contains(t, capturedMessage, "Only System Admins are allowed to manage instances.") | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("instance commands allowed for admin", func(t *testing.T) { | ||
| p := new(Plugin) | ||
| p.configuration = &configuration{EncryptionKey: testEncryptionKey} | ||
|
|
||
| // Mock admin user | ||
| adminUser := &model.User{ | ||
| Id: "admin_id", | ||
| Roles: "system_admin system_user", | ||
| } | ||
|
|
||
| instanceList := []string{"test-instance"} | ||
| instanceConfig := map[string]InstanceConfiguration{"test-instance": {GitlabURL: "https://gitlab.example.com"}} | ||
| instanceListJSON, _ := json.Marshal(instanceList) | ||
| instanceConfigJSON, _ := json.Marshal(instanceConfig) | ||
|
|
||
| api := &plugintest.API{} | ||
| api.On("GetUser", "admin_id").Return(adminUser, nil) | ||
| api.On("KVGet", instanceConfigNameListKey).Return(instanceListJSON, nil) | ||
| api.On("KVGet", instanceConfigMapKey).Return(instanceConfigJSON, nil) | ||
|
|
||
| var capturedMessage string | ||
| api.On("SendEphemeralPost", mock.Anything, mock.MatchedBy(func(post *model.Post) bool { | ||
| capturedMessage = post.Message | ||
| return true | ||
| })).Return(&model.Post{}) | ||
|
|
||
| p.SetAPI(api) | ||
| p.client = pluginapi.NewClient(api, p.Driver) | ||
|
|
||
| args := &model.CommandArgs{UserId: "admin_id", ChannelId: "channel_id"} | ||
|
|
||
| _, _ = p.handleInstance(args, []string{"list"}) | ||
|
|
||
| // Should show instance list, not an error | ||
| assert.Contains(t, capturedMessage, "test-instance") | ||
| assert.NotContains(t, capturedMessage, "Only System Admins") | ||
| }) | ||
| } | ||
|
|
||
| func TestInstanceCommands(t *testing.T) { | ||
| args := &model.CommandArgs{UserId: "user_id", ChannelId: "channel_id"} | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Use return p.getCommandResponse(args, ...) here to match the pattern in handleWebhookHandler