Skip to content

Commit

Permalink
Merge pull request #13 from drose-shopify/fix_interactive_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jusleg authored Aug 13, 2020
2 parents 900d3a9 + f7c2dfa commit b6cf1db
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
11 changes: 9 additions & 2 deletions app/controllers/slackify/slack_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ def event_callback

def interactive_callback
parsed_payload = JSON.parse(params[:payload])
response = handler_from_callback_id(parsed_payload["callback_id"]).call(parsed_payload)

callback_id = if parsed_payload.key?('view')
parsed_payload.dig('view', 'callback_id')
else
parsed_payload['callback_id']
end

response = handler_from_callback_id(callback_id).call(parsed_payload)
if !response.nil?
Timeout.timeout(SLACK_TIMEOUT_SECONDS) do
render json: response
Expand All @@ -30,7 +37,7 @@ def interactive_callback
head :ok
end
rescue Timeout::Error
raise Timeout::Error, "Slack interactive callback timed out for #{parsed_payload['callback_id']}"
raise Timeout::Error, "Slack interactive callback timed out for #{callback_id}"
end

def slash_command_callback
Expand Down
23 changes: 20 additions & 3 deletions test/integration/slack_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,42 @@ class SlackControllerTest < ActionDispatch::IntegrationTest
post @interactive_callback_url, as: :json, headers: build_webhook_headers(params), params: params
end

test "#interactive_callback returns the proper following blocks" do
# Setting up the Slack Ruby Client Mock
options = { view: { callback_id: "dummy_handler#button_clicked" }, actions: [{ "name" => "btn1", "value" => "btn1", "type" => "button" }] }
params = build_slack_interactive_callback(options)

post @interactive_callback_url, as: :json, headers: build_webhook_headers(params), params: params
assert_equal "{\"attachments\":[{\"text\":\"Test\"}]}", response.body
assert_response :ok

options = { view: { callback_id: "dummy_handler#button_clicked" }, actions: [{ "name" => "btn2", "value" => "btn2", "type" => "button" }] }
params = build_slack_interactive_callback(options)

post @interactive_callback_url, as: :json, headers: build_webhook_headers(params), params: params
assert_equal "{\"attachments\":[{\"text\":\" Button two has been clicked\"}]}", response.body
assert_response :ok
end

test "#interactive_callback returns the proper following attachement" do
# Setting up the Slack Ruby Client Mock
options = { actions: [{ "name" => "btn1", "value" => "btn1", "type" => "button" }], callback_id: "dummy_handler#button_clicked" }
params = build_slack_interactive_callback(options)
params = build_legacy_slack_interactive_callback(options)

post @interactive_callback_url, as: :json, headers: build_webhook_headers(params), params: params
assert_equal "{\"attachments\":[{\"text\":\"Test\"}]}", response.body
assert_response :ok

options = { actions: [{ "name" => "btn2", "value" => "btn2", "type" => "button" }], callback_id: "dummy_handler#button_clicked" }
params = build_slack_interactive_callback(options)
params = build_legacy_slack_interactive_callback(options)

post @interactive_callback_url, as: :json, headers: build_webhook_headers(params), params: params
assert_equal "{\"attachments\":[{\"text\":\" Button two has been clicked\"}]}", response.body
assert_response :ok
end

test "#interactive_callback raises error if the handler is not supported" do
options = { actions: [{ "name" => "btn2", "value" => "btn2", "type" => "button" }], callback_id: "random_handler#button_clicked" }
options = { view: { actions: [{ "name" => "btn2", "value" => "btn2", "type" => "button" }], callback_id: "random_handler#button_clicked" } }
params = build_slack_interactive_callback(options)

assert_raise Slackify::Exceptions::HandlerNotSupported do
Expand Down
44 changes: 43 additions & 1 deletion test/slack_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,21 @@ def build_slack_interactive_callback(**options)
{
payload: {
type: "interactive_message",
view: {
callback_id: "dummy_handler#cool_command",
type: 'modal',
title: {
type: 'plain_text',
text: 'Modal with inputs'
},
},
actions: [
{
name: "btn1",
value: "btn1",
type: "button",
}
],
callback_id: "dummy_handler#cool_command",
team: {
id: "TEAM1234",
domain: "famingo",
Expand All @@ -69,6 +76,41 @@ def build_slack_interactive_callback(**options)
}
end

def build_legacy_slack_interactive_callback(**options)
{
payload: {
type: "interactive_message",
actions: [
{
name: "btn1",
value: "btn1",
type: "button",
}
],
callback_id: "dummy_handler#cool_command",
team: {
id: "TEAM1234",
domain: "famingo",
},
channel: {
id: "CHANNEL1234",
name: "famingo-labs",
},
user: {
id: "USER1234",
name: "jusleg",
},
action_ts: "1458170917.164398",
message_ts: "1458170866.000004",
attachment_id: "1",
token: "testslacktoken123",
original_message: {
},
trigger_id: "13345224609.738474920.8088930838d88f008e0"
}.deep_merge(options).to_json
}
end

def build_webhook_headers(params, timestamp: Time.now, token: "123abcslacksecrettokenabc123")
{
"X-Slack-Signature": "v0=" +
Expand Down

0 comments on commit b6cf1db

Please sign in to comment.