Skip to content

Commit 8574bed

Browse files
authored
Provide default value when actions are missing in payload (#9)
1 parent 1d460a1 commit 8574bed

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### 0.3.1 (Next)
44

5+
* [#9](https://github.com/slack-ruby/slack-ruby-bot-server-events/pull/9): Provide default value when actions are missing in payload - [@nijave](https://github.com/nijave).
56
* Your contribution here.
67

78
#### 0.3.0 (12/30/2020)

lib/slack-ruby-bot-server-events/api/endpoints/slack/actions_endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ActionsEndpoint < Grape::API
7373
action = SlackRubyBotServer::Events::Requests::Action.new(params, request)
7474
payload_type = params[:payload][:type]
7575
callback_id = params[:payload][:callback_id]
76-
action_ids = params[:payload][:actions]&.map { |entity| entity[:action_id] }
76+
action_ids = params[:payload].fetch(:actions, []).map { |entity| entity[:action_id] }
7777
SlackRubyBotServer::Events.config.run_callbacks(
7878
:action,
7979
([payload_type, callback_id] + action_ids).compact,

spec/slack-ruby-bot-server-events/api/endpoints/slack/actions_endpoint_spec.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
let(:payload) do
2323
{
2424
type: 'message_action',
25-
actions: [{ name: 'id', value: '43749' }],
2625
channel: { id: 'C12345', name: 'channel' },
2726
user: { id: 'user_id' },
2827
team: { id: 'team_id' },
@@ -31,6 +30,15 @@
3130
}
3231
end
3332

33+
shared_examples 'message_actions handler' do
34+
it 'performs action' do
35+
post '/api/slack/action', payload: payload.to_json
36+
expect(last_response.status).to eq 201
37+
response = JSON.parse(last_response.body)
38+
expect(response).to eq('text' => 'message_action/action_id')
39+
end
40+
end
41+
3442
context 'with an action handler' do
3543
before do
3644
SlackRubyBotServer::Events.configure do |config|
@@ -40,11 +48,14 @@
4048
end
4149
end
4250

43-
it 'performs action' do
44-
post '/api/slack/action', payload: payload.to_json
45-
expect(last_response.status).to eq 201
46-
response = JSON.parse(last_response.body)
47-
expect(response).to eq('text' => 'message_action/action_id')
51+
it_behaves_like 'message_actions handler'
52+
53+
context 'with actions in the payload' do
54+
before do
55+
payload.merge!(actions: [{ name: 'id', value: '43749' }])
56+
end
57+
58+
it_behaves_like 'message_actions handler'
4859
end
4960
end
5061

0 commit comments

Comments
 (0)