From 2d9e85e82af2143143aa275629829adaf8f341e7 Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Tue, 14 Jan 2020 07:18:14 +0100 Subject: [PATCH] [MM-20706] Drop 26 character requirement from post action IDs (#13225) --- api4/integration_action.go | 2 +- api4/integration_action_test.go | 123 ++++++++++++++++++++++---------- web/context.go | 11 --- 3 files changed, 86 insertions(+), 50 deletions(-) diff --git a/api4/integration_action.go b/api4/integration_action.go index 0991aeafbe..24304ec8d2 100644 --- a/api4/integration_action.go +++ b/api4/integration_action.go @@ -18,7 +18,7 @@ func (api *API) InitAction() { } func doPostAction(c *Context, w http.ResponseWriter, r *http.Request) { - c.RequirePostId().RequireActionId() + c.RequirePostId() if c.Err != nil { return } diff --git a/api4/integration_action_test.go b/api4/integration_action_test.go index fe858e948a..17dd457830 100644 --- a/api4/integration_action_test.go +++ b/api4/integration_action_test.go @@ -51,49 +51,96 @@ func TestPostActionCookies(t *testing.T) { handler := &testHandler{t} server := httptest.NewServer(handler) - action := model.PostAction{ - Id: model.NewId(), - Name: "Test-action", - Type: model.POST_ACTION_TYPE_BUTTON, - Integration: &model.PostActionIntegration{ - URL: server.URL, - Context: map[string]interface{}{ - "test-key": "test-value", - }, - }, - } - post := &model.Post{ - Id: model.NewId(), - Type: model.POST_EPHEMERAL, - UserId: th.BasicUser.Id, - ChannelId: th.BasicChannel.Id, - CreateAt: model.GetMillis(), - UpdateAt: model.GetMillis(), - Props: map[string]interface{}{ - "attachments": []*model.SlackAttachment{ - { - Title: "some-title", - TitleLink: "https://some-url.com", - Text: "some-text", - ImageURL: "https://some-other-url.com", - Actions: []*model.PostAction{&action}, + for name, test := range map[string]struct { + Action model.PostAction + ExpectedSucess bool + ExpectedStatusCode int + }{ + "32 character ID": { + Action: model.PostAction{ + Id: model.NewId(), + Name: "Test-action", + Type: model.POST_ACTION_TYPE_BUTTON, + Integration: &model.PostActionIntegration{ + URL: server.URL, + Context: map[string]interface{}{ + "test-key": "test-value", + }, }, }, + ExpectedSucess: true, + ExpectedStatusCode: http.StatusOK, }, + "6 character ID": { + Action: model.PostAction{ + Id: "someID", + Name: "Test-action", + Type: model.POST_ACTION_TYPE_BUTTON, + Integration: &model.PostActionIntegration{ + URL: server.URL, + Context: map[string]interface{}{ + "test-key": "test-value", + }, + }, + }, + ExpectedSucess: true, + ExpectedStatusCode: http.StatusOK, + }, + "Empty ID": { + Action: model.PostAction{ + Id: "", + Name: "Test-action", + Type: model.POST_ACTION_TYPE_BUTTON, + Integration: &model.PostActionIntegration{ + URL: server.URL, + Context: map[string]interface{}{ + "test-key": "test-value", + }, + }, + }, + ExpectedSucess: false, + ExpectedStatusCode: http.StatusNotFound, + }, + } { + t.Run(name, func(t *testing.T) { + post := &model.Post{ + Id: model.NewId(), + Type: model.POST_EPHEMERAL, + UserId: th.BasicUser.Id, + ChannelId: th.BasicChannel.Id, + CreateAt: model.GetMillis(), + UpdateAt: model.GetMillis(), + Props: map[string]interface{}{ + "attachments": []*model.SlackAttachment{ + { + Title: "some-title", + TitleLink: "https://some-url.com", + Text: "some-text", + ImageURL: "https://some-other-url.com", + Actions: []*model.PostAction{&test.Action}, + }, + }, + }, + } + + assert.Equal(t, 32, len(th.App.PostActionCookieSecret())) + post = model.AddPostActionCookies(post, th.App.PostActionCookieSecret()) + + ok, resp := Client.DoPostActionWithCookie(post.Id, test.Action.Id, "", test.Action.Cookie) + require.NotNil(t, resp) + if test.ExpectedSucess { + assert.True(t, ok) + assert.Nil(t, resp.Error) + } else { + assert.False(t, ok) + assert.NotNil(t, resp.Error) + } + assert.Equal(t, test.ExpectedStatusCode, resp.StatusCode) + assert.NotNil(t, resp.RequestId) + assert.NotNil(t, resp.ServerVersion) + }) } - - post.GenerateActionIds() - assert.Equal(t, 32, len(th.App.PostActionCookieSecret())) - post = model.AddPostActionCookies(post, th.App.PostActionCookieSecret()) - - ok, resp := Client.DoPostActionWithCookie(post.Id, action.Id, "", action.Cookie) - assert.True(t, ok) - assert.NotNil(t, resp) - assert.Equal(t, 200, resp.StatusCode) - assert.Nil(t, resp.Error) - assert.NotNil(t, resp.RequestId) - assert.NotNil(t, resp.ServerVersion) } func TestOpenDialog(t *testing.T) { diff --git a/web/context.go b/web/context.go index 8bc7f17992..33bda4d08a 100644 --- a/web/context.go +++ b/web/context.go @@ -496,17 +496,6 @@ func (c *Context) RequireJobType() *Context { return c } -func (c *Context) RequireActionId() *Context { - if c.Err != nil { - return c - } - - if len(c.Params.ActionId) != 26 { - c.SetInvalidUrlParam("action_id") - } - return c -} - func (c *Context) RequireRoleId() *Context { if c.Err != nil { return c