From 19b753467d37209f2227567637e60138d05dd405 Mon Sep 17 00:00:00 2001 From: Poornima Date: Mon, 27 Feb 2017 00:18:20 +0530 Subject: Adding edit of incoming webhook (#5272) Adding edit of outgoing webhook Fixing spelling of error Fixing style Changing from PUT to POST for updates Fixing test failures due to merge --- api/webhook.go | 282 ++++++++++--- api/webhook_test.go | 395 +++++++++++++++++- app/webhook.go | 2 +- i18n/de.json | 6 +- i18n/en.json | 42 +- i18n/es.json | 6 +- i18n/fr.json | 6 +- i18n/ja.json | 6 +- i18n/ko.json | 6 +- i18n/nl.json | 6 +- i18n/pt-BR.json | 6 +- i18n/ru.json | 6 +- i18n/zh_CN.json | 6 +- i18n/zh_TW.json | 6 +- model/client.go | 20 + store/sql_webhook_store.go | 21 + store/sql_webhook_store_test.go | 67 +-- store/store.go | 3 + webapp/client/client.jsx | 24 ++ .../components/abstract_incoming_webhook.jsx | 242 +++++++++++ .../components/abstract_outgoing_webhook.jsx | 460 +++++++++++++++++++++ .../components/add_incoming_webhook.jsx | 223 +--------- .../components/add_outgoing_webhook.jsx | 432 +------------------ .../components/edit_incoming_webhook.jsx | 78 ++++ .../components/edit_outgoing_webhook.jsx | 190 +++++++++ .../components/installed_incoming_webhook.jsx | 11 +- .../components/installed_incoming_webhooks.jsx | 1 + .../components/installed_outgoing_webhook.jsx | 11 +- .../components/installed_outgoing_webhooks.jsx | 1 + webapp/i18n/en.json | 4 + webapp/routes/route_integrations.jsx | 12 + webapp/stores/integration_store.jsx | 18 + webapp/tests/client_hooks.test.jsx | 48 ++- webapp/utils/async_client.jsx | 46 +++ webapp/utils/constants.jsx | 1 + 35 files changed, 1935 insertions(+), 759 deletions(-) create mode 100644 webapp/components/integrations/components/abstract_incoming_webhook.jsx create mode 100644 webapp/components/integrations/components/abstract_outgoing_webhook.jsx create mode 100644 webapp/components/integrations/components/edit_incoming_webhook.jsx create mode 100644 webapp/components/integrations/components/edit_outgoing_webhook.jsx diff --git a/api/webhook.go b/api/webhook.go index 638607a32..c1e1ce974 100644 --- a/api/webhook.go +++ b/api/webhook.go @@ -21,10 +21,12 @@ func InitWebhook() { l4g.Debug(utils.T("api.webhook.init.debug")) BaseRoutes.Hooks.Handle("/incoming/create", ApiUserRequired(createIncomingHook)).Methods("POST") + BaseRoutes.Hooks.Handle("/incoming/update", ApiUserRequired(updateIncomingHook)).Methods("POST") BaseRoutes.Hooks.Handle("/incoming/delete", ApiUserRequired(deleteIncomingHook)).Methods("POST") BaseRoutes.Hooks.Handle("/incoming/list", ApiUserRequired(getIncomingHooks)).Methods("GET") BaseRoutes.Hooks.Handle("/outgoing/create", ApiUserRequired(createOutgoingHook)).Methods("POST") + BaseRoutes.Hooks.Handle("/outgoing/update", ApiUserRequired(updateOutgoingHook)).Methods("POST") BaseRoutes.Hooks.Handle("/outgoing/regen_token", ApiUserRequired(regenOutgoingHookToken)).Methods("POST") BaseRoutes.Hooks.Handle("/outgoing/delete", ApiUserRequired(deleteOutgoingHook)).Methods("POST") BaseRoutes.Hooks.Handle("/outgoing/list", ApiUserRequired(getOutgoingHooks)).Methods("GET") @@ -71,16 +73,86 @@ func createIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) { } } +func updateIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) { + if err := checkIncomingWebHooks("updateIncomingHook", "api.webhook.update_incoming.disabled.app_error"); err != nil { + c.Err = err + return + } + + if err := checkManageWebhooksPermission(c, "updateIncomingHook", "api.command.admin_only.app_error"); err != nil { + c.Err = err + return + } + + c.LogAudit("attempt") + + hook := model.IncomingWebhookFromJson(r.Body) + + if hook == nil { + c.SetInvalidParam("updateIncomingHook", "webhook") + return + } + + var oldHook *model.IncomingWebhook + var result store.StoreResult + + if result = <-app.Srv.Store.Webhook().GetIncoming(hook.Id, true); result.Err != nil { + c.LogAudit("no existing incoming hook found") + c.Err = result.Err + return + } + + oldHook = result.Data.(*model.IncomingWebhook) + cchan := app.Srv.Store.Channel().Get(hook.ChannelId, true) + + var channel *model.Channel + if result = <-cchan; result.Err != nil { + c.Err = result.Err + return + } + + channel = result.Data.(*model.Channel) + if channel.Type != model.CHANNEL_OPEN && !app.SessionHasPermissionToChannel(c.Session, channel.Id, model.PERMISSION_READ_CHANNEL) { + c.LogAudit("fail - bad channel permissions") + c.SetPermissionError(model.PERMISSION_READ_CHANNEL) + return + } + + if c.Session.UserId != oldHook.UserId && !app.SessionHasPermissionToTeam(c.Session, c.TeamId, model.PERMISSION_MANAGE_OTHERS_WEBHOOKS) { + c.LogAudit("fail - inappropriate permissions") + c.Err = model.NewLocAppError("updateIncomingHook", "api.webhook.update_incoming.permissions.app_error", nil, "user_id="+c.Session.UserId) + return + } + + if c.TeamId != oldHook.TeamId { + c.Err = model.NewLocAppError("UpdateIncomingHook", "api.webhook.team_mismatch.app_error", nil, "user_id="+c.Session.UserId) + return + } + + hook.UserId = oldHook.UserId + hook.CreateAt = oldHook.CreateAt + hook.UpdateAt = model.GetMillis() + hook.TeamId = oldHook.TeamId + hook.DeleteAt = oldHook.DeleteAt + + if result = <-app.Srv.Store.Webhook().UpdateIncoming(hook); result.Err != nil { + c.Err = result.Err + return + } + + c.LogAudit("success") + rhook := result.Data.(*model.IncomingWebhook) + w.Write([]byte(rhook.ToJson())) +} + func deleteIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) { - if !utils.Cfg.ServiceSettings.EnableIncomingWebhooks { - c.Err = model.NewLocAppError("deleteIncomingHook", "api.webhook.delete_incoming.disabled.app_errror", nil, "") - c.Err.StatusCode = http.StatusNotImplemented + if err := checkIncomingWebHooks("deleteIncomingHook", "api.webhook.delete_incoming.disabled.app_error"); err != nil { + c.Err = err return } - if !app.SessionHasPermissionToTeam(c.Session, c.TeamId, model.PERMISSION_MANAGE_WEBHOOKS) { - c.Err = model.NewLocAppError("deleteIncomingHook", "api.command.admin_only.app_error", nil, "") - c.Err.StatusCode = http.StatusForbidden + if err := checkManageWebhooksPermission(c, "deleteIncomingHook", "api.command.admin_only.app_error"); err != nil { + c.Err = err return } @@ -100,7 +172,7 @@ func deleteIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) { } else { if c.Session.UserId != result.Data.(*model.IncomingWebhook).UserId && !app.SessionHasPermissionToTeam(c.Session, c.TeamId, model.PERMISSION_MANAGE_OTHERS_WEBHOOKS) { c.LogAudit("fail - inappropriate permissions") - c.Err = model.NewLocAppError("deleteIncomingHook", "api.webhook.delete_incoming.permissions.app_errror", nil, "user_id="+c.Session.UserId) + c.Err = model.NewLocAppError("deleteIncomingHook", "api.webhook.delete_incoming.permissions.app_error", nil, "user_id="+c.Session.UserId) return } } @@ -130,55 +202,89 @@ func getIncomingHooks(c *Context, w http.ResponseWriter, r *http.Request) { } } -func createOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) { +func checkOutgoingWebHooks(where string, id string) *model.AppError { if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks { - c.Err = model.NewLocAppError("createOutgoingHook", "api.webhook.create_outgoing.disabled.app_error", nil, "") - c.Err.StatusCode = http.StatusNotImplemented - return + err := model.NewLocAppError(where, id, nil, "") + err.StatusCode = http.StatusNotImplemented + return err } - if !app.SessionHasPermissionToTeam(c.Session, c.TeamId, model.PERMISSION_MANAGE_WEBHOOKS) { - c.Err = model.NewLocAppError("createOutgoingHook", "api.command.admin_only.app_error", nil, "") - c.Err.StatusCode = http.StatusForbidden - return - } + return nil +} - c.LogAudit("attempt") +func checkIncomingWebHooks(where string, id string) *model.AppError { + if !utils.Cfg.ServiceSettings.EnableIncomingWebhooks { + err := model.NewLocAppError(where, id, nil, "") + err.StatusCode = http.StatusNotImplemented + return err + } - hook := model.OutgoingWebhookFromJson(r.Body) + return nil +} - if hook == nil { - c.SetInvalidParam("createOutgoingHook", "webhook") - return +func checkManageWebhooksPermission(c *Context, where string, id string) *model.AppError { + if !app.SessionHasPermissionToTeam(c.Session, c.TeamId, model.PERMISSION_MANAGE_WEBHOOKS) { + err := model.NewLocAppError(where, id, nil, "") + err.StatusCode = http.StatusForbidden + return err } - hook.CreatorId = c.Session.UserId - hook.TeamId = c.TeamId + return nil +} +func checkValidOutgoingHook(hook *model.OutgoingWebhook, c *Context, where string, id string) *model.AppError { if len(hook.ChannelId) != 0 { cchan := app.Srv.Store.Channel().Get(hook.ChannelId, true) var channel *model.Channel - if result := <-cchan; result.Err != nil { - c.Err = result.Err - return - } else { - channel = result.Data.(*model.Channel) + var result store.StoreResult + if result = <-cchan; result.Err != nil { + return result.Err } + channel = result.Data.(*model.Channel) + if channel.Type != model.CHANNEL_OPEN { c.LogAudit("fail - not open channel") - c.Err = model.NewLocAppError("createOutgoingHook", "api.webhook.create_outgoing.not_open.app_error", nil, "") - return + return model.NewLocAppError(where, "api.webhook."+id+".not_open.app_error", nil, "") } - if channel.Type != model.CHANNEL_OPEN || channel.TeamId != c.TeamId { - c.LogAudit("fail - bad channel permissions") - c.Err = model.NewLocAppError("createOutgoingHook", "api.webhook.create_outgoing.permissions.app_error", nil, "") - return + if channel.TeamId != c.TeamId { + c.LogAudit("fail - cannot update command to a different team") + return model.NewLocAppError(where, "api.webhook."+id+".permissions.app_error", nil, "") } } else if len(hook.TriggerWords) == 0 { - c.Err = model.NewLocAppError("createOutgoingHook", "api.webhook.create_outgoing.triggers.app_error", nil, "") + return model.NewLocAppError(where, "api.webhook."+id+".triggers.app_error", nil, "") + } + + return nil +} + +func createOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) { + if err := checkOutgoingWebHooks("createOutgoingHook", "api.webhook.create_outgoing.disabled.app_error"); err != nil { + c.Err = err + return + } + + if err := checkManageWebhooksPermission(c, "createOutgoingHook", "api.command.admin_only.app_error"); err != nil { + c.Err = err + return + } + + c.LogAudit("attempt") + + hook := model.OutgoingWebhookFromJson(r.Body) + + if hook == nil { + c.SetInvalidParam("createOutgoingHook", "webhook") + return + } + + hook.CreatorId = c.Session.UserId + hook.TeamId = c.TeamId + + if err := checkValidOutgoingHook(hook, c, "createOutgoingHook", "create_outgoing"); err != nil { + c.Err = err return } @@ -210,15 +316,13 @@ func createOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) { } func getOutgoingHooks(c *Context, w http.ResponseWriter, r *http.Request) { - if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks { - c.Err = model.NewLocAppError("getOutgoingHooks", "api.webhook.get_outgoing.disabled.app_error", nil, "") - c.Err.StatusCode = http.StatusNotImplemented + if err := checkOutgoingWebHooks("getOutgoingHooks", "api.webhook.get_outgoing.disabled.app_error"); err != nil { + c.Err = err return } - if !app.SessionHasPermissionToTeam(c.Session, c.TeamId, model.PERMISSION_MANAGE_WEBHOOKS) { - c.Err = model.NewLocAppError("getOutgoingHooks", "api.command.admin_only.app_error", nil, "") - c.Err.StatusCode = http.StatusForbidden + if err := checkManageWebhooksPermission(c, "getOutgoingHooks", "api.command.admin_only.app_error"); err != nil { + c.Err = err return } @@ -231,16 +335,85 @@ func getOutgoingHooks(c *Context, w http.ResponseWriter, r *http.Request) { } } +func updateOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) { + if err := checkOutgoingWebHooks("updateOutgoingHook", "api.webhook.update_outgoing.disabled.app_error"); err != nil { + c.Err = err + return + } + + if err := checkManageWebhooksPermission(c, "updateOutgoingHook", "api.command.admin_only.app_error"); err != nil { + c.Err = err + return + } + + c.LogAudit("attempt") + + hook := model.OutgoingWebhookFromJson(r.Body) + + if hook == nil { + c.SetInvalidParam("updateOutgoingHook", "webhook") + return + } + + if err := checkValidOutgoingHook(hook, c, "updateOutgoingHook", "update_outgoing"); err != nil { + c.Err = err + return + } + + var result store.StoreResult + if result = <-app.Srv.Store.Webhook().GetOutgoingByTeam(c.TeamId); result.Err != nil { + c.Err = result.Err + return + } + + allHooks := result.Data.([]*model.OutgoingWebhook) + + for _, existingOutHook := range allHooks { + urlIntersect := utils.StringArrayIntersection(existingOutHook.CallbackURLs, hook.CallbackURLs) + triggerIntersect := utils.StringArrayIntersection(existingOutHook.TriggerWords, hook.TriggerWords) + + if existingOutHook.ChannelId == hook.ChannelId && len(urlIntersect) != 0 && len(triggerIntersect) != 0 && existingOutHook.Id != hook.Id { + c.Err = model.NewLocAppError("updateOutgoingHook", "api.webhook.update_outgoing.intersect.app_error", nil, "") + return + } + } + + if result = <-app.Srv.Store.Webhook().GetOutgoing(hook.Id); result.Err != nil { + c.LogAudit("fail - no existing outgoing webhook found") + c.Err = result.Err + return + } + + oldHook := result.Data.(*model.OutgoingWebhook) + if c.TeamId != oldHook.TeamId { + c.Err = model.NewLocAppError("UpdateOutgoingHook", "api.webhook.team_mismatch.app_error", nil, "user_id="+c.Session.UserId) + return + } + + hook.CreatorId = oldHook.CreatorId + hook.CreateAt = oldHook.CreateAt + hook.DeleteAt = oldHook.DeleteAt + hook.TeamId = oldHook.TeamId + hook.UpdateAt = model.GetMillis() + + if result = <-app.Srv.Store.Webhook().UpdateOutgoing(hook); result.Err != nil { + c.Err = result.Err + return + } + + c.LogAudit("success") + rhook := result.Data.(*model.OutgoingWebhook) + w.Write([]byte(rhook.ToJson())) +} + func deleteOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) { - if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks { - c.Err = model.NewLocAppError("deleteOutgoingHook", "api.webhook.delete_outgoing.disabled.app_error", nil, "") - c.Err.StatusCode = http.StatusNotImplemented + if err := checkOutgoingWebHooks("deleteOutgoingHook", "api.webhook.delete_outgoing.disabled.app_error"); err != nil { + c.Err = err return } - if !app.SessionHasPermissionToTeam(c.Session, c.TeamId, model.PERMISSION_MANAGE_WEBHOOKS) { - c.Err = model.NewLocAppError("deleteOutgoingHook", "api.command.admin_only.app_error", nil, "") - c.Err.StatusCode = http.StatusForbidden + if err := checkManageWebhooksPermission(c, "deleteOutgoingHook", "api.command.admin_only.app_error"); err != nil { + c.Err = err return } @@ -275,15 +448,13 @@ func deleteOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) { } func regenOutgoingHookToken(c *Context, w http.ResponseWriter, r *http.Request) { - if !utils.Cfg.ServiceSettings.EnableOutgoingWebhooks { - c.Err = model.NewLocAppError("regenOutgoingHookToken", "api.webhook.regen_outgoing_token.disabled.app_error", nil, "") - c.Err.StatusCode = http.StatusNotImplemented + if err := checkOutgoingWebHooks("regenOutgoingHookToken", "api.webhook.regen_outgoing_token.disabled.app_error"); err != nil { + c.Err = err return } - if !app.SessionHasPermissionToTeam(c.Session, c.TeamId, model.PERMISSION_MANAGE_WEBHOOKS) { - c.Err = model.NewLocAppError("regenOutgoingHookToken", "api.command.admin_only.app_error", nil, "") - c.Err.StatusCode = http.StatusForbidden + if err := checkManageWebhooksPermission(c, "regenOutgoingHookToken", "api.command.admin_only.app_error"); err != nil { + c.Err = err return } @@ -322,9 +493,8 @@ func regenOutgoingHookToken(c *Context, w http.ResponseWriter, r *http.Request) } func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) { - if !utils.Cfg.ServiceSettings.EnableIncomingWebhooks { - c.Err = model.NewLocAppError("incomingWebhook", "web.incoming_webhook.disabled.app_error", nil, "") - c.Err.StatusCode = http.StatusNotImplemented + if err := checkIncomingWebHooks("incomingWebhook", "web.incoming_webhook.disabled.app_error"); err != nil { + c.Err = err return } diff --git a/api/webhook_test.go b/api/webhook_test.go index 6daa0c334..dc708cf76 100644 --- a/api/webhook_test.go +++ b/api/webhook_test.go @@ -5,10 +5,11 @@ package api import ( "fmt" - "github.com/mattermost/platform/model" - "github.com/mattermost/platform/utils" "net/http" "testing" + + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" ) func TestCreateIncomingHook(t *testing.T) { @@ -117,6 +118,212 @@ func TestCreateIncomingHook(t *testing.T) { } } +func TestUpdateIncomingHook(t *testing.T) { + th := Setup().InitSystemAdmin() + Client := th.SystemAdminClient + team := th.SystemAdminTeam + + channel1 := th.CreateChannel(Client, team) + channel2 := th.CreatePrivateChannel(Client, team) + channel3 := th.CreateChannel(Client, team) + + user2 := th.CreateUser(Client) + LinkUserToTeam(user2, team) + + team2 := th.CreateTeam(Client) + user3 := th.CreateUser(Client) + LinkUserToTeam(user3, team2) + UpdateUserToTeamAdmin(user3, team2) + + enableIncomingHooks := utils.Cfg.ServiceSettings.EnableIncomingWebhooks + enableAdminOnlyHooks := utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations + defer func() { + utils.Cfg.ServiceSettings.EnableIncomingWebhooks = enableIncomingHooks + utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = enableAdminOnlyHooks + utils.SetDefaultRolesBasedOnConfig() + }() + + utils.Cfg.ServiceSettings.EnableIncomingWebhooks = true + *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = true + utils.SetDefaultRolesBasedOnConfig() + + hook := createIncomingWebhook(channel1.Id, Client, t) + + t.Run("UpdateIncomingHook", func(t *testing.T) { + hook.DisplayName = "hook2" + hook.Description = "description" + hook.ChannelId = channel3.Id + + if result, err := Client.UpdateIncomingWebhook(hook); err != nil { + t.Fatal("Update hook should not fail") + } else { + updatedHook := result.Data.(*model.IncomingWebhook) + + if updatedHook.DisplayName != "hook2" { + t.Fatal("Hook name is not updated") + } + + if updatedHook.Description != "description" { + t.Fatal("Hook description is not updated") + } + + if updatedHook.ChannelId != channel3.Id { + t.Fatal("Hook channel is not updated") + } + } + }) + + t.Run("RetainCreateAt", func(t *testing.T) { + hook2 := &model.IncomingWebhook{ChannelId: channel1.Id, CreateAt: 100} + + if result, err := Client.CreateIncomingWebhook(hook2); err != nil { + t.Fatal("hook creation failed") + } else { + createdHook := result.Data.(*model.IncomingWebhook) + createdHook.DisplayName = "Name2" + + if result, err := Client.UpdateIncomingWebhook(createdHook); err != nil { + t.Fatal("Update hook should not fail") + } else { + updatedHook := result.Data.(*model.IncomingWebhook) + + if updatedHook.CreateAt != createdHook.CreateAt { + t.Fatal("failed - hook create at should not be changed") + } + } + } + }) + + t.Run("ModifyUpdateAt", func(t *testing.T) { + hook.DisplayName = "Name3" + + if result, err := Client.UpdateIncomingWebhook(hook); err != nil { + t.Fatal("Update hook should not fail") + } else { + updatedHook := result.Data.(*model.IncomingWebhook) + + if updatedHook.UpdateAt == hook.UpdateAt { + t.Fatal("failed - hook updateAt is not updated") + } + } + }) + + t.Run("UpdateNonExistentHook", func(t *testing.T) { + nonExistentHook := &model.IncomingWebhook{ChannelId: channel1.Id} + + if _, err := Client.UpdateIncomingWebhook(nonExistentHook); err == nil { + t.Fatal("should have failed - update a non-existent hook") + } + }) + + Client.Logout() + Client.Must(Client.LoginById(user2.Id, user2.Password)) + Client.SetTeamId(team.Id) + t.Run("UserIsNotAdminOfTeam", func(t *testing.T) { + if _, err := Client.UpdateIncomingWebhook(hook); err == nil { + t.Fatal("should have failed - user is not admin of team") + } + }) + + utils.Cfg.ServiceSettings.EnableIncomingWebhooks = true + + t.Run("OnlyAdminIntegrationsDisabled", func(t *testing.T) { + *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = false + utils.SetDefaultRolesBasedOnConfig() + + t.Run("UpdateHookOfSameUser", func(t *testing.T) { + sameUserHook := &model.IncomingWebhook{ChannelId: channel1.Id, UserId: user2.Id} + if result, err := Client.CreateIncomingWebhook(sameUserHook); err != nil { + t.Fatal("Hook creation failed") + } else { + sameUserHook = result.Data.(*model.IncomingWebhook) + } + + if _, err := Client.UpdateIncomingWebhook(sameUserHook); err != nil { + t.Fatal("should not fail - only admin integrations are disabled & hook of same user") + } + }) + + t.Run("UpdateHookOfDifferentUser", func(t *testing.T) { + if _, err := Client.UpdateIncomingWebhook(hook); err == nil { + t.Fatal("should have failed - user does not have permissions to update other user's hooks") + } + }) + }) + + *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = true + utils.SetDefaultRolesBasedOnConfig() + + Client.Logout() + UpdateUserToTeamAdmin(user2, team) + Client.Must(Client.LoginById(user2.Id, user2.Password)) + Client.SetTeamId(team.Id) + t.Run("UpdateByDifferentUser", func(t *testing.T) { + if result, err := Client.UpdateIncomingWebhook(hook); err != nil { + t.Fatal("Update hook should not fail") + } else { + updatedHook := result.Data.(*model.IncomingWebhook) + + if updatedHook.UserId == user2.Id { + t.Fatal("Hook's creator userId is not retained") + } + } + }) + + t.Run("IncomingHooksDisabled", func(t *testing.T) { + utils.Cfg.ServiceSettings.EnableIncomingWebhooks = false + if _, err := Client.UpdateIncomingWebhook(hook); err == nil { + t.Fatal("should have failed - incoming hooks are disabled") + } + }) + + t.Run("PrivateChannel", func(t *testing.T) { + hook.ChannelId = channel2.Id + + if _, err := Client.UpdateIncomingWebhook(hook); err == nil { + t.Fatal("should have failed - updating to a private channel where the user is not a member") + } + }) + + t.Run("UpdateToNonExistentChannel", func(t *testing.T) { + hook.ChannelId = "junk" + if _, err := Client.UpdateIncomingWebhook(hook); err == nil { + t.Fatal("should have failed - bad channel id") + } + }) + + Client.Logout() + Client.Must(Client.LoginById(user3.Id, user3.Password)) + Client.SetTeamId(team2.Id) + t.Run("UpdateToADifferentTeam", func(t *testing.T) { + if _, err := Client.UpdateIncomingWebhook(hook); err == nil { + t.Fatal("should have failed - update to a different team is not allowed") + } + }) +} + +func createIncomingWebhook(channelID string, Client *model.Client, t *testing.T) *model.IncomingWebhook { + hook := &model.IncomingWebhook{ChannelId: channelID} + if result, err := Client.CreateIncomingWebhook(hook); err != nil { + t.Fatal("Hook creation failed") + } else { + hook = result.Data.(*model.IncomingWebhook) + } + + return hook +} + +func createOutgoingWebhook(channelID string, callbackURLs []string, triggerWords []string, Client *model.Client, t *testing.T) *model.OutgoingWebhook { + hook := &model.OutgoingWebhook{ChannelId: channelID, CallbackURLs: callbackURLs, TriggerWords: triggerWords} + if result, err := Client.CreateOutgoingWebhook(hook); err != nil { + t.Fatal("Hook creation failed") + } else { + hook = result.Data.(*model.OutgoingWebhook) + } + + return hook +} + func TestListIncomingHooks(t *testing.T) { th := Setup().InitSystemAdmin() Client := th.SystemAdminClient @@ -416,6 +623,190 @@ func TestListOutgoingHooks(t *testing.T) { } } +func TestUpdateOutgoingHook(t *testing.T) { + th := Setup().InitSystemAdmin() + Client := th.SystemAdminClient + user := th.SystemAdminUser + team := th.SystemAdminTeam + team2 := th.CreateTeam(Client) + channel1 := th.CreateChannel(Client, team) + channel2 := th.CreatePrivateChannel(Client, team) + channel3 := th.CreateChannel(Client, team) + user2 := th.CreateUser(Client) + LinkUserToTeam(user2, team) + user3 := th.CreateUser(Client) + LinkUserToTeam(user3, team2) + + enableOutgoingHooks := utils.Cfg.ServiceSettings.EnableOutgoingWebhooks + enableAdminOnlyHooks := utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations + defer func() { + utils.Cfg.ServiceSettings.EnableOutgoingWebhooks = enableOutgoingHooks + utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = enableAdminOnlyHooks + utils.SetDefaultRolesBasedOnConfig() + }() + + utils.Cfg.ServiceSettings.EnableOutgoingWebhooks = true + *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = true + utils.SetDefaultRolesBasedOnConfig() + + hook := createOutgoingWebhook(channel1.Id, []string{"http://nowhere.com"}, []string{"cats"}, Client, t) + createOutgoingWebhook(channel1.Id, []string{"http://nowhere.com"}, []string{"dogs"}, Client, t) + + hook.DisplayName = "Cats" + hook.Description = "Get me some cats" + t.Run("OutgoingHooksDisabled", func(t *testing.T) { + utils.Cfg.ServiceSettings.EnableOutgoingWebhooks = false + if _, err := Client.UpdateOutgoingWebhook(hook); err == nil { + t.Fatal("should have failed - outgoing webhooks disabled") + } + }) + + utils.Cfg.ServiceSettings.EnableOutgoingWebhooks = true + t.Run("UpdateOutgoingWebhook", func(t *testing.T) { + if result, err := Client.UpdateOutgoingWebhook(hook); err != nil { + t.Fatal("failed to update outgoing web hook") + } else { + updatedHook := result.Data.(*model.OutgoingWebhook) + + if updatedHook.DisplayName != hook.DisplayName { + t.Fatal("Hook display name did not get updated") + } + + if updatedHook.Description != hook.Description { + t.Fatal("Hook description did not get updated") + } + } + }) + + t.Run("RetainCreateAt", func(t *testing.T) { + hook2 := &model.OutgoingWebhook{ChannelId: channel1.Id, CallbackURLs: []string{"http://nowhere.com"}, TriggerWords: []string{"rats"}} + + if result, err := Client.CreateOutgoingWebhook(hook2); err != nil { + t.Fatal("hook creation failed") + } else { + createdHook := result.Data.(*model.OutgoingWebhook) + createdHook.DisplayName = "Name2" + + if result, err := Client.UpdateOutgoingWebhook(createdHook); err != nil { + t.Fatal("Update hook should not fail") + } else { + updatedHook := result.Data.(*model.OutgoingWebhook) + + if updatedHook.CreateAt != createdHook.CreateAt { + t.Fatal("failed - hook create at should not be changed") + } + } + } + }) + + t.Run("ModifyUpdateAt", func(t *testing.T) { + hook.DisplayName = "Name3" + + if result, err := Client.UpdateOutgoingWebhook(hook); err != nil { + t.Fatal("Update hook should not fail") + } else { + updatedHook := result.Data.(*model.OutgoingWebhook) + + if updatedHook.UpdateAt == hook.UpdateAt { + t.Fatal("failed - hook updateAt is not updated") + } + } + }) + + Client.Logout() + Client.Must(Client.LoginById(user2.Id, user2.Password)) + Client.SetTeamId(team.Id) + t.Run("UpdateByUserWithoutPermissions", func(t *testing.T) { + if _, err := Client.UpdateOutgoingWebhook(hook); err == nil { + t.Fatal("should have failed - user does not have permissions to manage webhooks") + } + + *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = false + utils.SetDefaultRolesBasedOnConfig() + t.Run("WithoutOnlyAdminIntegrations", func(t *testing.T) { + if _, err := Client.UpdateOutgoingWebhook(hook); err != nil { + t.Fatal("update webhook failed when admin only integrations is turned off") + } + }) + }) + + *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = true + utils.SetDefaultRolesBasedOnConfig() + + Client.Logout() + LinkUserToTeam(user3, team) + UpdateUserToTeamAdmin(user3, team) + Client.Must(Client.LoginById(user3.Id, user3.Password)) + Client.SetTeamId(team.Id) + t.Run("RetainHookCreator", func(t *testing.T) { + if result, err := Client.UpdateOutgoingWebhook(hook); err != nil { + t.Fatal("failed to update outgoing web hook") + } else { + updatedHook := result.Data.(*model.OutgoingWebhook) + + if updatedHook.CreatorId != user.Id { + t.Fatal("hook creator should not be changed") + } + } + }) + + Client.Logout() + Client.Must(Client.LoginById(user.Id, user.Password)) + Client.SetTeamId(team.Id) + t.Run("UpdateToExistingTriggerWordAndCallback", func(t *testing.T) { + t.Run("OnSameChannel", func(t *testing.T) { + hook.TriggerWords = []string{"dogs"} + + if _, err := Client.UpdateOutgoingWebhook(hook); err == nil { + t.Fatal("should have failed - duplicate trigger words & channel urls") + } + }) + + t.Run("OnDifferentChannel", func(t *testing.T) { + hook.TriggerWords = []string{"dogs"} + hook.ChannelId = channel3.Id + + if _, err := Client.UpdateOutgoingWebhook(hook); err != nil { + t.Fatal("update of hook failed with duplicate trigger word but different channel") + } + }) + }) + + t.Run("UpdateToNonExistentChannel", func(t *testing.T) { + hook.ChannelId = "junk" + + if _, err := Client.UpdateOutgoingWebhook(hook); err == nil { + t.Fatal("should have failed - non existent channel") + } + }) + + t.Run("UpdateToPrivateChannel", func(t *testing.T) { + hook.ChannelId = channel2.Id + + if _, err := Client.UpdateOutgoingWebhook(hook); err == nil { + t.Fatal("should have failed - update to a private channel") + } + }) + + t.Run("UpdateToBlankTriggerWordAndChannel", func(t *testing.T) { + hook.ChannelId = "" + hook.TriggerWords = nil + + if _, err := Client.UpdateOutgoingWebhook(hook); err == nil { + t.Fatal("should have failed - update to blank trigger words & channel") + } + }) + + Client.Logout() + Client.Must(Client.LoginById(user3.Id, user3.Password)) + Client.SetTeamId(team2.Id) + t.Run("UpdateToADifferentTeam", func(t *testing.T) { + if _, err := Client.UpdateOutgoingWebhook(hook); err == nil { + t.Fatal("should have failed - update to a different team is not allowed") + } + }) +} + func TestDeleteOutgoingHook(t *testing.T) { th := Setup().InitSystemAdmin() Client := th.SystemAdminClient diff --git a/app/webhook.go b/app/webhook.go index ff7f2726e..c9485c807 100644 --- a/app/webhook.go +++ b/app/webhook.go @@ -195,7 +195,7 @@ func CreateWebhookPost(userId, teamId, channelId, text, overrideUsername, overri func CreateIncomingWebhookForChannel(userId string, channel *model.Channel, hook *model.IncomingWebhook) (*model.IncomingWebhook, *model.AppError) { if !utils.Cfg.ServiceSettings.EnableIncomingWebhooks { - return nil, model.NewAppError("CreateIncomingWebhookForChannel", "api.webhook.create_incoming.disabled.app_errror", nil, "", http.StatusNotImplemented) + return nil, model.NewAppError("CreateIncomingWebhookForChannel", "api.webhook.create_incoming.disabled.app_error", nil, "", http.StatusNotImplemented) } hook.UserId = userId diff --git a/i18n/de.json b/i18n/de.json index 60fa73d6e..afe2ac0ef 100644 --- a/i18n/de.json +++ b/i18n/de.json @@ -2684,7 +2684,7 @@ "translation": "Team-Hub wird gestoppt für teamId=%v" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "Eingehende Webhooks wurden vom Systemadministrator deaktiviert." }, { @@ -2708,11 +2708,11 @@ "translation": "Entweder trigger_words oder channel_id müssen gesetzt sein" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "Eingehende Webhooks wurden vom Systemadministrator deaktiviert." }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "Ungültige Berechtigungen um eingehenden Webhook zu löschen" }, { diff --git a/i18n/en.json b/i18n/en.json index 8aebd4adc..9e1452da5 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -2692,9 +2692,21 @@ "translation": "team hub stopping for teamId=%v" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "Incoming webhooks have been disabled by the system admin." }, + { + "id": "api.webhook.update_incoming.disabled.app_error", + "translation": "Incoming webhooks have been disabled by the system admin." + }, + { + "id": "api.webhook.update_incoming.permissions.app_error", + "translation": "Invalid permissions to update incoming webhook" + }, + { + "id": "api.webhook.team_mismatch.app_error", + "translation": "Cannot update webhook across teams" + }, { "id": "api.webhook.create_outgoing.disabled.app_error", "translation": "Outgoing webhooks have been disabled by the system admin." @@ -2716,11 +2728,31 @@ "translation": "Either trigger_words or channel_id must be set" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.update_outgoing.disabled.app_error", + "translation": "Outgoing webhooks have been disabled by the system admin." + }, + { + "id": "api.webhook.update_outgoing.intersect.app_error", + "translation": "Outgoing webhooks from the same channel cannot have the same trigger words/callback URLs." + }, + { + "id": "api.webhook.update_outgoing.not_open.app_error", + "translation": "Outgoing webhooks can only be updated to public channels." + }, + { + "id": "api.webhook.update_outgoing.permissions.app_error", + "translation": "Invalid permissions to update outgoing webhook." + }, + { + "id": "api.webhook.update_outgoing.triggers.app_error", + "translation": "Either trigger_words or channel_id must be set" + }, + { + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "Incoming webhooks have been disabled by the system admin." }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "Invalid permissions to delete incoming webhook" }, { @@ -5547,6 +5579,10 @@ "id": "store.sql_webhooks.save_incoming.existing.app_error", "translation": "You cannot overwrite an existing IncomingWebhook" }, + { + "id": "store.sql_webhooks.update_incoming.app_error", + "translation": "We couldn't update the IncomingWebhook" + }, { "id": "store.sql_webhooks.save_outgoing.app_error", "translation": "We couldn't save the OutgoingWebhook" diff --git a/i18n/es.json b/i18n/es.json index f0fc23454..ade4a8c8b 100644 --- a/i18n/es.json +++ b/i18n/es.json @@ -2684,7 +2684,7 @@ "translation": "deteniendo el hub de equipo para teamId=%v" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "Webhooks entrantes han sido inhabilitados por el administrador del sistema." }, { @@ -2708,11 +2708,11 @@ "translation": "Debe establecerse palabras que desencadenen una acción o un channel_id" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "Webhooks entrantes han sido inhabilitados por el administrador del sistema." }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "Permisos inapropiados para eliminar un webhook de entrada" }, { diff --git a/i18n/fr.json b/i18n/fr.json index 02222e7b2..d39aa34e8 100644 --- a/i18n/fr.json +++ b/i18n/fr.json @@ -2684,7 +2684,7 @@ "translation": "Hub d'équipe arrêté pour teamId=%v" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "Les webhooks entrants ont été désactivés par l'administrateur système." }, { @@ -2708,11 +2708,11 @@ "translation": "Les trigger_words ou channel_id doivent être définis" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "Les webhooks entrants ont été désactivées par l'administrateur système." }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "Droits insuffisants pour supprimer le webhook entrant" }, { diff --git a/i18n/ja.json b/i18n/ja.json index 040b45433..6060cf51c 100644 --- a/i18n/ja.json +++ b/i18n/ja.json @@ -2684,7 +2684,7 @@ "translation": "teamId=%v用のチームハブを停止しています" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "内向きのウェブフックはシステム管理者によって無効にされています。" }, { @@ -2708,11 +2708,11 @@ "translation": "trigger_wordsまたはchannel_idを設定してください" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "内向きのウェブフックはシステム管理者によって無効にされています。" }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "内向きのウェブフックを削除するのに十分な権限が付与されていません" }, { diff --git a/i18n/ko.json b/i18n/ko.json index 73d5a2aa7..8010dc23a 100644 --- a/i18n/ko.json +++ b/i18n/ko.json @@ -2684,7 +2684,7 @@ "translation": "team hub stopping for teamId=%v" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "Incoming webhook은 관리자가 사용할 수 없게 설정했습니다." }, { @@ -2708,11 +2708,11 @@ "translation": "Either trigger_words or channel_id must be set" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "Incoming webhook은 관리자가 사용할 수 없게 설정했습니다." }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "Incoming webhook을 삭제할 권한이 없습니다" }, { diff --git a/i18n/nl.json b/i18n/nl.json index b85ddecfb..14be05658 100644 --- a/i18n/nl.json +++ b/i18n/nl.json @@ -2684,7 +2684,7 @@ "translation": "Team hub word gestopt voor teamId=%v" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "Commando's zijn uitgeschakeld door de beheerder." }, { @@ -2708,11 +2708,11 @@ "translation": "Ofwel trigger_words of channel_id moet worden geconfigueerd" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "Commando's zijn uitgeschakeld door de beheerder." }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "Onjuiste rechten voor het verwijderen van de webhook opdracht" }, { diff --git a/i18n/pt-BR.json b/i18n/pt-BR.json index 5d2c75f68..d47baadde 100644 --- a/i18n/pt-BR.json +++ b/i18n/pt-BR.json @@ -2684,7 +2684,7 @@ "translation": "central de equipes parou para teamId=%v" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "Webhooks de entrada foram desabilitados pelo administrador do sistema." }, { @@ -2708,11 +2708,11 @@ "translation": "Ou trigger_words ou channel_id precisa ser definido" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "Webhooks de entrada foram desabilitados pelo administrador do sistema." }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "Permissões inadequadas para deletar o webhook de entrada" }, { diff --git a/i18n/ru.json b/i18n/ru.json index c487aa704..77eefe91a 100644 --- a/i18n/ru.json +++ b/i18n/ru.json @@ -2684,7 +2684,7 @@ "translation": "остановка командного хаба для teamId=%v" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "Входящие вебхуки были отключены системным администратором." }, { @@ -2708,11 +2708,11 @@ "translation": "Должны быть заданы trigger_words или channel_id" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "Входящие вебхуки отключены системным администратором." }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "Отсутствуют права на удаление входящего вебхука" }, { diff --git a/i18n/zh_CN.json b/i18n/zh_CN.json index 025b9edff..d1ea18c4e 100644 --- a/i18n/zh_CN.json +++ b/i18n/zh_CN.json @@ -2684,7 +2684,7 @@ "translation": "团队枢纽停止 teamId=%v" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "传入的webhooks已被系统管理员禁用。" }, { @@ -2708,11 +2708,11 @@ "translation": "无论是trigger_words或channel_id必须设置" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "传入的webhooks已被系统管理员禁用。" }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "无效的删除传入的 webhook 权限" }, { diff --git a/i18n/zh_TW.json b/i18n/zh_TW.json index 98137e0a5..06f1b8908 100644 --- a/i18n/zh_TW.json +++ b/i18n/zh_TW.json @@ -2684,7 +2684,7 @@ "translation": "正在停止團隊中心 teamId=%v" }, { - "id": "api.webhook.create_incoming.disabled.app_errror", + "id": "api.webhook.create_incoming.disabled.app_error", "translation": "傳入的 Webhook 已被系統管理員停用。" }, { @@ -2708,11 +2708,11 @@ "translation": "必須設定觸發詞或者頻道 ID" }, { - "id": "api.webhook.delete_incoming.disabled.app_errror", + "id": "api.webhook.delete_incoming.disabled.app_error", "translation": "傳入的 Webhook 已被系統管理員停用" }, { - "id": "api.webhook.delete_incoming.permissions.app_errror", + "id": "api.webhook.delete_incoming.permissions.app_error", "translation": "沒有適當的權限刪除傳入的 Webhook" }, { diff --git a/model/client.go b/model/client.go index 26efa62b4..820386aa4 100644 --- a/model/client.go +++ b/model/client.go @@ -2008,6 +2008,16 @@ func (c *Client) CreateIncomingWebhook(hook *IncomingWebhook) (*Result, *AppErro } } +func (c *Client) UpdateIncomingWebhook(hook *IncomingWebhook) (*Result, *AppError) { + if r, err := c.DoApiPost(c.GetTeamRoute()+"/hooks/incoming/update", hook.ToJson()); err != nil { + return nil, err + } else { + defer closeBody(r) + return &Result{r.Header.Get(HEADER_REQUEST_ID), + r.Header.Get(HEADER_ETAG_SERVER), IncomingWebhookFromJson(r.Body)}, nil + } +} + func (c *Client) PostToWebhook(id, payload string) (*Result, *AppError) { if r, err := c.DoPost("/hooks/"+id, payload, "application/x-www-form-urlencoded"); err != nil { return nil, err @@ -2099,6 +2109,16 @@ func (c *Client) CreateOutgoingWebhook(hook *OutgoingWebhook) (*Result, *AppErro } } +func (c *Client) UpdateOutgoingWebhook(hook *OutgoingWebhook) (*Result, *AppError) { + if r, err := c.DoApiPost(c.GetTeamRoute()+"/hooks/outgoing/update", hook.ToJson()); err != nil { + return nil, err + } else { + defer closeBody(r) + return &Result{r.Header.Get(HEADER_REQUEST_ID), + r.Header.Get(HEADER_ETAG_SERVER), OutgoingWebhookFromJson(r.Body)}, nil + } +} + func (c *Client) DeleteOutgoingWebhook(id string) (*Result, *AppError) { data := make(map[string]string) data["id"] = id diff --git a/store/sql_webhook_store.go b/store/sql_webhook_store.go index 0e61130ad..355678064 100644 --- a/store/sql_webhook_store.go +++ b/store/sql_webhook_store.go @@ -107,6 +107,27 @@ func (s SqlWebhookStore) SaveIncoming(webhook *model.IncomingWebhook) StoreChann return storeChannel } +func (s SqlWebhookStore) UpdateIncoming(hook *model.IncomingWebhook) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + hook.UpdateAt = model.GetMillis() + + if _, err := s.GetMaster().Update(hook); err != nil { + result.Err = model.NewLocAppError("SqlWebhookStore.UpdateIncoming", "store.sql_webhooks.update_incoming.app_error", nil, "id="+hook.Id+", "+err.Error()) + } else { + result.Data = hook + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlWebhookStore) GetIncoming(id string, allowFromCache bool) StoreChannel { storeChannel := make(StoreChannel, 1) diff --git a/store/sql_webhook_store_test.go b/store/sql_webhook_store_test.go index 3d79d9ad3..e1aaad1b7 100644 --- a/store/sql_webhook_store_test.go +++ b/store/sql_webhook_store_test.go @@ -4,35 +4,49 @@ package store import ( - "github.com/mattermost/platform/model" "testing" + + "github.com/mattermost/platform/model" ) func TestWebhookStoreSaveIncoming(t *testing.T) { Setup() + o1 := buildIncomingWebhook() - o1 := model.IncomingWebhook{} - o1.ChannelId = model.NewId() - o1.UserId = model.NewId() - o1.TeamId = model.NewId() - - if err := (<-store.Webhook().SaveIncoming(&o1)).Err; err != nil { + if err := (<-store.Webhook().SaveIncoming(o1)).Err; err != nil { t.Fatal("couldn't save item", err) } - if err := (<-store.Webhook().SaveIncoming(&o1)).Err; err == nil { + if err := (<-store.Webhook().SaveIncoming(o1)).Err; err == nil { t.Fatal("shouldn't be able to update from save") } } -func TestWebhookStoreGetIncoming(t *testing.T) { +func TestWebhookStoreUpdateIncoming(t *testing.T) { Setup() + o1 := buildIncomingWebhook() + o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) + previousUpdatedAt := o1.UpdateAt - o1 := &model.IncomingWebhook{} - o1.ChannelId = model.NewId() - o1.UserId = model.NewId() - o1.TeamId = model.NewId() + o1.DisplayName = "TestHook" + + if result := (<-store.Webhook().UpdateIncoming(o1)); result.Err != nil { + t.Fatal("updation of incoming hook failed", result.Err) + } else { + if result.Data.(*model.IncomingWebhook).UpdateAt == previousUpdatedAt { + t.Fatal("should have updated the UpdatedAt of the hook") + } + + if result.Data.(*model.IncomingWebhook).DisplayName != "TestHook" { + t.Fatal("display name is not updated") + } + } +} + +func TestWebhookStoreGetIncoming(t *testing.T) { + Setup() + o1 := buildIncomingWebhook() o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) if r1 := <-store.Webhook().GetIncoming(o1.Id, false); r1.Err != nil { @@ -96,11 +110,7 @@ func TestWebhookStoreGetIncomingList(t *testing.T) { func TestWebhookStoreGetIncomingByTeam(t *testing.T) { Setup() - - o1 := &model.IncomingWebhook{} - o1.ChannelId = model.NewId() - o1.UserId = model.NewId() - o1.TeamId = model.NewId() + o1 := buildIncomingWebhook() o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) @@ -123,11 +133,7 @@ func TestWebhookStoreGetIncomingByTeam(t *testing.T) { func TestWebhookStoreDeleteIncoming(t *testing.T) { Setup() - - o1 := &model.IncomingWebhook{} - o1.ChannelId = model.NewId() - o1.UserId = model.NewId() - o1.TeamId = model.NewId() + o1 := buildIncomingWebhook() o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) @@ -153,11 +159,7 @@ func TestWebhookStoreDeleteIncoming(t *testing.T) { func TestWebhookStoreDeleteIncomingByUser(t *testing.T) { Setup() - - o1 := &model.IncomingWebhook{} - o1.ChannelId = model.NewId() - o1.UserId = model.NewId() - o1.TeamId = model.NewId() + o1 := buildIncomingWebhook() o1 = (<-store.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) @@ -181,6 +183,15 @@ func TestWebhookStoreDeleteIncomingByUser(t *testing.T) { } } +func buildIncomingWebhook() *model.IncomingWebhook { + o1 := &model.IncomingWebhook{} + o1.ChannelId = model.NewId() + o1.UserId = model.NewId() + o1.TeamId = model.NewId() + + return o1 +} + func TestWebhookStoreSaveOutgoing(t *testing.T) { Setup() diff --git a/store/store.go b/store/store.go index 34a709568..06564a8c2 100644 --- a/store/store.go +++ b/store/store.go @@ -260,9 +260,11 @@ type WebhookStore interface { GetIncoming(id string, allowFromCache bool) StoreChannel GetIncomingList(offset, limit int) StoreChannel GetIncomingByTeam(teamId string, offset, limit int) StoreChannel + UpdateIncoming(webhook *model.IncomingWebhook) StoreChannel GetIncomingByChannel(channelId string) StoreChannel DeleteIncoming(webhookId string, time int64) StoreChannel PermanentDeleteIncomingByUser(userId string) StoreChannel + SaveOutgoing(webhook *model.OutgoingWebhook) StoreChannel GetOutgoing(id string) StoreChannel GetOutgoingByChannel(channelId string) StoreChannel @@ -270,6 +272,7 @@ type WebhookStore interface { DeleteOutgoing(webhookId string, time int64) StoreChannel PermanentDeleteOutgoingByUser(userId string) StoreChannel UpdateOutgoing(hook *model.OutgoingWebhook) StoreChannel + AnalyticsIncomingCount(teamId string) StoreChannel AnalyticsOutgoingCount(teamId string) StoreChannel InvalidateWebhookCache(webhook string) diff --git a/webapp/client/client.jsx b/webapp/client/client.jsx index 24eb7eabb..390c07d13 100644 --- a/webapp/client/client.jsx +++ b/webapp/client/client.jsx @@ -1975,6 +1975,18 @@ export default class Client { this.trackEvent('api', 'api_integrations_created', {team_id: this.getTeamId()}); } + updateIncomingHook(hook, success, error) { + request. + post(`${this.getHooksRoute()}/incoming/update`). + set(this.defaultHeaders). + type('application/json'). + accept('application/json'). + send(hook). + end(this.handleResponse.bind(this, 'updateIncomingHook', success, error)); + + this.trackEvent('api', 'api_integrations_updated', {team_id: this.getTeamId()}); + } + deleteIncomingHook(hookId, success, error) { request. post(`${this.getHooksRoute()}/incoming/delete`). @@ -2008,6 +2020,18 @@ export default class Client { this.trackEvent('api', 'api_integrations_created', {team_id: this.getTeamId()}); } + updateOutgoingHook(hook, success, error) { + request. + post(`${this.getHooksRoute()}/outgoing/update`). + set(this.defaultHeaders). + type('application/json'). + accept('application/json'). + send(hook). + end(this.handleResponse.bind(this, 'updateOutgoingHook', success, error)); + + this.trackEvent('api', 'api_integrations_updated', {team_id: this.getTeamId()}); + } + deleteOutgoingHook(hookId, success, error) { request. post(`${this.getHooksRoute()}/outgoing/delete`). diff --git a/webapp/components/integrations/components/abstract_incoming_webhook.jsx b/webapp/components/integrations/components/abstract_incoming_webhook.jsx new file mode 100644 index 000000000..04322d77e --- /dev/null +++ b/webapp/components/integrations/components/abstract_incoming_webhook.jsx @@ -0,0 +1,242 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import React from 'react'; + +import BackstageHeader from 'components/backstage/components/backstage_header.jsx'; +import ChannelSelect from 'components/channel_select.jsx'; +import {FormattedMessage} from 'react-intl'; +import FormError from 'components/form_error.jsx'; +import SpinnerButton from 'components/spinner_button.jsx'; +import {Link} from 'react-router/es6'; + +export default class AbstractIncomingWebhook extends React.Component { + static get propTypes() { + return { + team: React.PropTypes.object + }; + } + + constructor(props) { + super(props); + + this.handleSubmit = this.handleSubmit.bind(this); + + this.updateDisplayName = this.updateDisplayName.bind(this); + this.updateDescription = this.updateDescription.bind(this); + this.updateChannelId = this.updateChannelId.bind(this); + + this.state = { + displayName: '', + description: '', + channelId: '', + saving: false, + serverError: '', + clientError: null + }; + + if (typeof this.performAction === 'undefined') { + throw new TypeError('Subclasses must override performAction'); + } + + if (typeof this.header === 'undefined') { + throw new TypeError('Subclasses must override header'); + } + + if (typeof this.footer === 'undefined') { + throw new TypeError('Subclasses must override footer'); + } + + this.performAction = this.performAction.bind(this); + this.header = this.header.bind(this); + this.footer = this.footer.bind(this); + } + + handleSubmit(e) { + e.preventDefault(); + + if (this.state.saving) { + return; + } + + this.setState({ + saving: true, + serverError: '', + clientError: '' + }); + + if (!this.state.channelId) { + this.setState({ + saving: false, + clientError: ( + + ) + }); + + return; + } + + const hook = { + channel_id: this.state.channelId, + display_name: this.state.displayName, + description: this.state.description + }; + + this.performAction(hook); + } + + updateDisplayName(e) { + this.setState({ + displayName: e.target.value + }); + } + + updateDescription(e) { + this.setState({ + description: e.target.value + }); + } + + updateChannelId(e) { + this.setState({ + channelId: e.target.value + }); + } + + render() { + var headerToRender = this.header(); + var footerToRender = this.footer(); + return ( +
+ + + + + + +
+
+
+ +
+ +
+ +
+
+
+
+ +
+ +
+ +
+
+
+
+ +
+ +
+ +
+
+
+
+ + + + + + + +
+
+
+
+ ); + } +} diff --git a/webapp/components/integrations/components/abstract_outgoing_webhook.jsx b/webapp/components/integrations/components/abstract_outgoing_webhook.jsx new file mode 100644 index 000000000..6033647af --- /dev/null +++ b/webapp/components/integrations/components/abstract_outgoing_webhook.jsx @@ -0,0 +1,460 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import React from 'react'; + +import {localizeMessage} from 'utils/utils.jsx'; + +import BackstageHeader from 'components/backstage/components/backstage_header.jsx'; +import ChannelSelect from 'components/channel_select.jsx'; +import {FormattedMessage} from 'react-intl'; +import FormError from 'components/form_error.jsx'; +import {Link} from 'react-router/es6'; +import SpinnerButton from 'components/spinner_button.jsx'; + +export default class AbstractOutgoingWebhook extends React.Component { + static get propTypes() { + return { + team: React.PropTypes.object + }; + } + + constructor(props) { + super(props); + + this.handleSubmit = this.handleSubmit.bind(this); + + this.updateDisplayName = this.updateDisplayName.bind(this); + this.updateDescription = this.updateDescription.bind(this); + this.updateContentType = this.updateContentType.bind(this); + this.updateChannelId = this.updateChannelId.bind(this); + this.updateTriggerWords = this.updateTriggerWords.bind(this); + this.updateTriggerWhen = this.updateTriggerWhen.bind(this); + this.updateCallbackUrls = this.updateCallbackUrls.bind(this); + + this.state = { + displayName: '', + description: '', + contentType: 'application/x-www-form-urlencoded', + channelId: '', + triggerWords: '', + triggerWhen: 0, + callbackUrls: '', + saving: false, + serverError: '', + clientError: null + }; + + if (typeof this.performAction === 'undefined') { + throw new TypeError('Subclasses must override performAction'); + } + + if (typeof this.header === 'undefined') { + throw new TypeError('Subclasses must override header'); + } + + if (typeof this.footer === 'undefined') { + throw new TypeError('Subclasses must override footer'); + } + + if (typeof this.renderExtra === 'undefined') { + throw new TypeError('Subclasses must override renderExtra'); + } + + this.performAction = this.performAction.bind(this); + this.header = this.header.bind(this); + this.footer = this.footer.bind(this); + this.renderExtra = this.renderExtra.bind(this); + } + + handleSubmit(e) { + e.preventDefault(); + + if (this.state.saving) { + return; + } + + this.setState({ + saving: true, + serverError: '', + clientError: '' + }); + + const triggerWords = []; + if (this.state.triggerWords) { + for (let triggerWord of this.state.triggerWords.split('\n')) { + triggerWord = triggerWord.trim(); + + if (triggerWord.length > 0) { + triggerWords.push(triggerWord); + } + } + } + + if (!this.state.channelId && triggerWords.length === 0) { + this.setState({ + saving: false, + clientError: ( + + ) + }); + + return; + } + + const callbackUrls = []; + for (let callbackUrl of this.state.callbackUrls.split('\n')) { + callbackUrl = callbackUrl.trim(); + + if (callbackUrl.length > 0) { + callbackUrls.push(callbackUrl); + } + } + + if (callbackUrls.length === 0) { + this.setState({ + saving: false, + clientError: ( + + ) + }); + + return; + } + + const hook = { + channel_id: this.state.channelId, + trigger_words: triggerWords, + trigger_when: parseInt(this.state.triggerWhen, 10), + callback_urls: callbackUrls, + display_name: this.state.displayName, + content_type: this.state.contentType, + description: this.state.description + }; + + this.performAction(hook); + } + + updateDisplayName(e) { + this.setState({ + displayName: e.target.value + }); + } + + updateDescription(e) { + this.setState({ + description: e.target.value + }); + } + + updateContentType(e) { + this.setState({ + contentType: e.target.value + }); + } + + updateChannelId(e) { + this.setState({ + channelId: e.target.value + }); + } + + updateTriggerWords(e) { + this.setState({ + triggerWords: e.target.value + }); + } + + updateTriggerWhen(e) { + this.setState({ + triggerWhen: e.target.value + }); + } + + updateCallbackUrls(e) { + this.setState({ + callbackUrls: e.target.value + }); + } + + render() { + const contentTypeOption1 = 'application/x-www-form-urlencoded'; + const contentTypeOption2 = 'application/json'; + + var headerToRender = this.header(); + var footerToRender = this.footer(); + var renderExtra = this.renderExtra(); + + return ( +
+ + + + + + +
+
+
+ +
+ +
+ +
+
+
+
+ +
+ +
+ +
+
+
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+
+
+ +
+ +
+ +
+
+
+
+ +
+