Skip to content

Commit

Permalink
Merge 709560b into fd6e2f3
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanato committed Mar 21, 2017
2 parents fd6e2f3 + 709560b commit 328bb03
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api4/webhook.go
Expand Up @@ -24,6 +24,7 @@ func InitWebhook() {
BaseRoutes.OutgoingHooks.Handle("", ApiSessionRequired(createOutgoingHook)).Methods("POST")
BaseRoutes.OutgoingHooks.Handle("", ApiSessionRequired(getOutgoingHooks)).Methods("GET")
BaseRoutes.OutgoingHook.Handle("", ApiSessionRequired(updateOutcomingHook)).Methods("PUT")
BaseRoutes.OutgoingHook.Handle("", ApiSessionRequired(getOutgoingHook)).Methods("GET")
BaseRoutes.OutgoingHook.Handle("/regen_token", ApiSessionRequired(regenOutgoingHookToken)).Methods("POST")
}

Expand Down Expand Up @@ -333,6 +334,35 @@ func getOutgoingHooks(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.OutgoingWebhookListToJson(hooks)))
}

func getOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireHookId()
if c.Err != nil {
return
}

hook, err := app.GetOutgoingWebhook(c.Params.HookId)
if err != nil {
c.Err = err
return
}

c.LogAudit("attempt")

if !app.SessionHasPermissionToTeam(c.Session, hook.TeamId, model.PERMISSION_MANAGE_WEBHOOKS) {
c.SetPermissionError(model.PERMISSION_MANAGE_WEBHOOKS)
return
}

if c.Session.UserId != hook.CreatorId && !app.SessionHasPermissionToTeam(c.Session, hook.TeamId, model.PERMISSION_MANAGE_OTHERS_WEBHOOKS) {
c.LogAudit("fail - inappropriate permissions")
c.SetPermissionError(model.PERMISSION_MANAGE_OTHERS_WEBHOOKS)
return
}

c.LogAudit("success")
w.Write([]byte(hook.ToJson()))
}

func regenOutgoingHookToken(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireHookId()
if c.Err != nil {
Expand Down
39 changes: 39 additions & 0 deletions api4/webhook_test.go
Expand Up @@ -419,6 +419,45 @@ func TestGetOutgoingWebhooks(t *testing.T) {
CheckUnauthorizedStatus(t, resp)
}

func TestGetOutgoingWebhook(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)

getHook, resp := th.SystemAdminClient.GetOutgoingWebhook(rhook.Id)
CheckNoError(t, resp)
if getHook.Id != rhook.Id {
t.Fatal("failed to retrieve the correct outgoing hook")
}

_, resp = Client.GetOutgoingWebhook(rhook.Id)
CheckForbiddenStatus(t, resp)

nonExistentHook := &model.OutgoingWebhook{ChannelId: th.BasicChannel.Id}
_, resp = th.SystemAdminClient.GetOutgoingWebhook(nonExistentHook.Id)
CheckNotFoundStatus(t, resp)

nonExistentHook.Id = model.NewId()
_, resp = th.SystemAdminClient.GetOutgoingWebhook(nonExistentHook.Id)
CheckInternalErrorStatus(t, resp)
}

func TestUpdateIncomingHook(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Expand Down
10 changes: 10 additions & 0 deletions model/client4.go
Expand Up @@ -1321,6 +1321,16 @@ func (c *Client4) GetOutgoingWebhooks(page int, perPage int, etag string) ([]*Ou
}
}

// GetOutgoingWebhook outgoing webhooks on the system requested by Hook Id.
func (c *Client4) GetOutgoingWebhook(hookId string) (*OutgoingWebhook, *Response) {
if r, err := c.DoApiGet(c.GetOutgoingWebhookRoute(hookId), ""); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return OutgoingWebhookFromJson(r.Body), BuildResponse(r)
}
}

// GetOutgoingWebhooksForChannel returns a page of outgoing webhooks for a channel. Page counting starts at 0.
func (c *Client4) GetOutgoingWebhooksForChannel(channelId string, page int, perPage int, etag string) ([]*OutgoingWebhook, *Response) {
query := fmt.Sprintf("?page=%v&per_page=%v&channel_id=%v", page, perPage, channelId)
Expand Down

0 comments on commit 328bb03

Please sign in to comment.