[MM - 12370] Add cli command "webhook delete" (#9804)

* [MM - 12370] Add cli command "webhook delete"

* Replace spaces with tabs

* Call cli command to delete hooks in tests

* Capture webhook object from create webhook to get webhook id

* Print error message when webhook not deleted

* Remove redundant error message and if condition in webhook delete test

* Fix build
This commit is contained in:
Harshit Patni
2018-11-26 17:02:11 +05:30
committed by Carlos Tadeu Panato Junior
parent ba627c0f92
commit 7663de06bb
2 changed files with 82 additions and 0 deletions

View File

@@ -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)

View File

@@ -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")
}
}