diff --git a/cmd/mattermost/commands/webhook.go b/cmd/mattermost/commands/webhook.go index 3869cf8637..1372c6d99f 100644 --- a/cmd/mattermost/commands/webhook.go +++ b/cmd/mattermost/commands/webhook.go @@ -50,6 +50,14 @@ var WebhookCreateOutgoingCmd = &cobra.Command{ RunE: createOutgoingWebhookCmdF, } +var WebhookDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete webhooks", + Long: "Delete webhook with given id", + Example: " webhook delete [webhookID]", + RunE: deleteWebhookCmdF, +} + func listWebhookCmdF(command *cobra.Command, args []string) error { app, err := InitDBCommandContextCobra(command) if err != nil { @@ -262,6 +270,28 @@ func createOutgoingWebhookCmdF(command *cobra.Command, args []string) error { return nil } +func deleteWebhookCmdF(command *cobra.Command, args []string) error { + app, err := InitDBCommandContextCobra(command) + if err != nil { + return err + } + defer app.Shutdown() + + if len(args) < 1 { + return errors.New("WebhookID is not specified") + } + + webhookId := args[0] + errIncomingWebhook := app.DeleteIncomingWebhook(webhookId) + errOutgoingWebhook := app.DeleteOutgoingWebhook(webhookId) + + if errIncomingWebhook != nil && errOutgoingWebhook != nil { + return errors.New("Unable to delete webhook '" + webhookId + "'") + } + + return nil +} + func init() { WebhookCreateIncomingCmd.Flags().String("channel", "", "Channel ID") WebhookCreateIncomingCmd.Flags().String("user", "", "User ID") @@ -292,6 +322,7 @@ func init() { WebhookCreateIncomingCmd, WebhookModifyIncomingCmd, WebhookCreateOutgoingCmd, + WebhookDeleteCmd, ) RootCmd.AddCommand(WebhookCmd) diff --git a/cmd/mattermost/commands/webhook_test.go b/cmd/mattermost/commands/webhook_test.go index d849a187b0..51d632e073 100644 --- a/cmd/mattermost/commands/webhook_test.go +++ b/cmd/mattermost/commands/webhook_test.go @@ -210,3 +210,54 @@ func TestCreateOutgoingWebhook(t *testing.T) { t.Fatal("Failed to create incoming webhook") } } + +func TestDeleteWebhooks(t *testing.T) { + th := api4.Setup().InitBasic() + defer th.TearDown() + adminClient := th.SystemAdminClient + + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableIncomingWebhooks = true }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOutgoingWebhooks = true }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnablePostUsernameOverride = true }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnablePostIconOverride = true }) + + defaultRolePermissions := th.SaveDefaultRolePermissions() + defer func() { + th.RestoreDefaultRolePermissions(defaultRolePermissions) + }() + th.AddPermissionToRole(model.PERMISSION_MANAGE_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID) + th.RemovePermissionFromRole(model.PERMISSION_MANAGE_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID) + + dispName := "myhookinc" + inHookStruct := &model.IncomingWebhook{DisplayName: dispName, ChannelId: th.BasicChannel.Id, TeamId: th.BasicChannel.TeamId} + incomingHook, resp := adminClient.CreateIncomingWebhook(inHookStruct) + api4.CheckNoError(t, resp) + + dispName2 := "myhookout" + outHookStruct := &model.OutgoingWebhook{DisplayName: dispName2, ChannelId: th.BasicChannel.Id, TeamId: th.BasicChannel.TeamId, CallbackURLs: []string{"http://nowhere.com"}, Username: "some-user-name", IconURL: "http://some-icon-url/"} + outgoingHook, resp := adminClient.CreateOutgoingWebhook(outHookStruct) + api4.CheckNoError(t, resp) + + hooksBeforeDeletion := CheckCommand(t, "webhook", "list", th.BasicTeam.Name) + + if !strings.Contains(string(hooksBeforeDeletion), dispName) { + t.Fatal("Should have incoming webhooks") + } + + if !strings.Contains(string(hooksBeforeDeletion), dispName2) { + t.Fatal("Should have outgoing webhooks") + } + + CheckCommand(t, "webhook", "delete", incomingHook.Id) + CheckCommand(t, "webhook", "delete", outgoingHook.Id) + + hooksAfterDeletion := CheckCommand(t, "webhook", "list", th.BasicTeam.Name) + + if strings.Contains(string(hooksAfterDeletion), dispName) { + t.Fatal("Should not have incoming webhooks") + } + + if strings.Contains(string(hooksAfterDeletion), dispName2) { + t.Fatal("Should not have outgoing webhooks") + } +}