diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-03-13 10:40:43 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-13 10:40:43 -0400 |
commit | 19c67d7fe35f92ae8a288dcdb9877d3bede41a61 (patch) | |
tree | 1119f74c30564ce53d5e7dc566009594e348f494 /api4 | |
parent | 3ebfb369530e28ca3246c5cd2833e666edce9c90 (diff) | |
download | chat-19c67d7fe35f92ae8a288dcdb9877d3bede41a61.tar.gz chat-19c67d7fe35f92ae8a288dcdb9877d3bede41a61.tar.bz2 chat-19c67d7fe35f92ae8a288dcdb9877d3bede41a61.zip |
Implement GET and POST /hooks/outgoing endpoints for APIv4 (#5645)
Diffstat (limited to 'api4')
-rw-r--r-- | api4/webhook.go | 72 | ||||
-rw-r--r-- | api4/webhook_test.go | 162 |
2 files changed, 232 insertions, 2 deletions
diff --git a/api4/webhook.go b/api4/webhook.go index 19a851390..923f66ad3 100644 --- a/api4/webhook.go +++ b/api4/webhook.go @@ -17,15 +17,17 @@ func InitWebhook() { BaseRoutes.IncomingHooks.Handle("", ApiSessionRequired(createIncomingHook)).Methods("POST") BaseRoutes.IncomingHooks.Handle("", ApiSessionRequired(getIncomingHooks)).Methods("GET") - BaseRoutes.IncomingHook.Handle("", ApiSessionRequired(getIncomingHook)).Methods("GET") BaseRoutes.IncomingHook.Handle("", ApiSessionRequired(deleteIncomingHook)).Methods("DELETE") + + BaseRoutes.OutgoingHooks.Handle("", ApiSessionRequired(createOutgoingHook)).Methods("POST") + BaseRoutes.OutgoingHooks.Handle("", ApiSessionRequired(getOutgoingHooks)).Methods("GET") } func createIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) { hook := model.IncomingWebhookFromJson(r.Body) if hook == nil { - c.SetInvalidParam("webhook") + c.SetInvalidParam("incoming_webhook") return } @@ -53,6 +55,7 @@ func createIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) { return } else { c.LogAudit("success") + w.WriteHeader(http.StatusCreated) w.Write([]byte(incomingHook.ToJson())) } } @@ -158,3 +161,68 @@ func deleteIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) { } } } + +func createOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) { + hook := model.OutgoingWebhookFromJson(r.Body) + if hook == nil { + c.SetInvalidParam("outgoing_webhook") + return + } + + c.LogAudit("attempt") + + hook.CreatorId = c.Session.UserId + + if !app.SessionHasPermissionToTeam(c.Session, hook.TeamId, model.PERMISSION_MANAGE_WEBHOOKS) { + c.SetPermissionError(model.PERMISSION_MANAGE_WEBHOOKS) + return + } + + if rhook, err := app.CreateOutgoingWebhook(hook); err != nil { + c.LogAudit("fail") + c.Err = err + return + } else { + c.LogAudit("success") + w.WriteHeader(http.StatusCreated) + w.Write([]byte(rhook.ToJson())) + } +} + +func getOutgoingHooks(c *Context, w http.ResponseWriter, r *http.Request) { + channelId := r.URL.Query().Get("channel_id") + teamId := r.URL.Query().Get("team_id") + + var hooks []*model.OutgoingWebhook + var err *model.AppError + + if len(channelId) > 0 { + if !app.SessionHasPermissionToChannel(c.Session, channelId, model.PERMISSION_MANAGE_WEBHOOKS) { + c.SetPermissionError(model.PERMISSION_MANAGE_WEBHOOKS) + return + } + + hooks, err = app.GetOutgoingWebhooksForChannelPage(channelId, c.Params.Page, c.Params.PerPage) + } else if len(teamId) > 0 { + if !app.SessionHasPermissionToTeam(c.Session, teamId, model.PERMISSION_MANAGE_WEBHOOKS) { + c.SetPermissionError(model.PERMISSION_MANAGE_WEBHOOKS) + return + } + + hooks, err = app.GetOutgoingWebhooksForTeamPage(teamId, c.Params.Page, c.Params.PerPage) + } else { + if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_WEBHOOKS) { + c.SetPermissionError(model.PERMISSION_MANAGE_WEBHOOKS) + return + } + + hooks, err = app.GetOutgoingWebhooksPage(c.Params.Page, c.Params.PerPage) + } + + if err != nil { + c.Err = err + return + } + + w.Write([]byte(model.OutgoingWebhookListToJson(hooks))) +} diff --git a/api4/webhook_test.go b/api4/webhook_test.go index bfd75c7ec..2d8a1e8f9 100644 --- a/api4/webhook_test.go +++ b/api4/webhook_test.go @@ -256,3 +256,165 @@ func TestDeleteIncomingWebhook(t *testing.T) { CheckForbiddenStatus(t, resp) }) } + +func TestCreateOutgoingWebhook(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + 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 := &model.OutgoingWebhook{ChannelId: th.BasicChannel.Id, TeamId: th.BasicChannel.TeamId, CallbackURLs: []string{"http://nowhere.com"}} + + rhook, resp := th.SystemAdminClient.CreateOutgoingWebhook(hook) + CheckNoError(t, resp) + + if rhook.ChannelId != hook.ChannelId { + t.Fatal("channel ids didn't match") + } else if rhook.CreatorId != th.SystemAdminUser.Id { + t.Fatal("user ids didn't match") + } else if rhook.TeamId != th.BasicChannel.TeamId { + t.Fatal("team ids didn't match") + } + + hook.ChannelId = "junk" + _, resp = th.SystemAdminClient.CreateOutgoingWebhook(hook) + CheckNotFoundStatus(t, resp) + + hook.ChannelId = th.BasicChannel.Id + th.LoginTeamAdmin() + _, resp = Client.CreateOutgoingWebhook(hook) + CheckNoError(t, resp) + + th.LoginBasic() + _, resp = Client.CreateOutgoingWebhook(hook) + CheckForbiddenStatus(t, resp) + + *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = false + utils.SetDefaultRolesBasedOnConfig() + + _, resp = Client.CreateOutgoingWebhook(hook) + CheckNoError(t, resp) + + utils.Cfg.ServiceSettings.EnableOutgoingWebhooks = false + _, resp = Client.CreateOutgoingWebhook(hook) + CheckNotImplementedStatus(t, resp) +} + +func TestGetOutgoingWebhooks(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + + 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 := &model.OutgoingWebhook{ChannelId: th.BasicChannel.Id, TeamId: th.BasicChannel.TeamId, CallbackURLs: []string{"http://nowhere.com"}} + rhook, resp := th.SystemAdminClient.CreateOutgoingWebhook(hook) + CheckNoError(t, resp) + + hooks, resp := th.SystemAdminClient.GetOutgoingWebhooks(0, 1000, "") + CheckNoError(t, resp) + + found := false + for _, h := range hooks { + if rhook.Id == h.Id { + found = true + } + } + + if !found { + t.Fatal("missing hook") + } + + hooks, resp = th.SystemAdminClient.GetOutgoingWebhooks(0, 1, "") + CheckNoError(t, resp) + + if len(hooks) != 1 { + t.Fatal("should only be 1") + } + + hooks, resp = th.SystemAdminClient.GetOutgoingWebhooksForTeam(th.BasicTeam.Id, 0, 1000, "") + CheckNoError(t, resp) + + found = false + for _, h := range hooks { + if rhook.Id == h.Id { + found = true + } + } + + if !found { + t.Fatal("missing hook") + } + + hooks, resp = th.SystemAdminClient.GetOutgoingWebhooksForTeam(model.NewId(), 0, 1000, "") + CheckNoError(t, resp) + + if len(hooks) != 0 { + t.Fatal("no hooks should be returned") + } + + hooks, resp = th.SystemAdminClient.GetOutgoingWebhooksForChannel(th.BasicChannel.Id, 0, 1000, "") + CheckNoError(t, resp) + + found = false + for _, h := range hooks { + if rhook.Id == h.Id { + found = true + } + } + + if !found { + t.Fatal("missing hook") + } + + hooks, resp = th.SystemAdminClient.GetOutgoingWebhooksForChannel(model.NewId(), 0, 1000, "") + CheckNoError(t, resp) + + if len(hooks) != 0 { + t.Fatal("no hooks should be returned") + } + + _, resp = Client.GetOutgoingWebhooks(0, 1000, "") + CheckForbiddenStatus(t, resp) + + *utils.Cfg.ServiceSettings.EnableOnlyAdminIntegrations = false + utils.SetDefaultRolesBasedOnConfig() + + _, resp = Client.GetOutgoingWebhooksForTeam(th.BasicTeam.Id, 0, 1000, "") + CheckNoError(t, resp) + + _, resp = Client.GetOutgoingWebhooksForTeam(model.NewId(), 0, 1000, "") + CheckForbiddenStatus(t, resp) + + _, resp = Client.GetOutgoingWebhooksForChannel(th.BasicChannel.Id, 0, 1000, "") + CheckNoError(t, resp) + + _, resp = Client.GetOutgoingWebhooksForChannel(model.NewId(), 0, 1000, "") + CheckForbiddenStatus(t, resp) + + _, resp = Client.GetOutgoingWebhooks(0, 1000, "") + CheckForbiddenStatus(t, resp) + + Client.Logout() + _, resp = Client.GetOutgoingWebhooks(0, 1000, "") + CheckUnauthorizedStatus(t, resp) +} |