From 43f7cb05c97ba249fdd5aa01e6ac7c6238816f61 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 24 May 2018 15:16:52 +0800 Subject: [PATCH] FIX: Broken ping event for web hooks due to missing payload. --- app/jobs/regular/emit_web_hook_event.rb | 2 +- spec/jobs/emit_web_hook_event_spec.rb | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/jobs/regular/emit_web_hook_event.rb b/app/jobs/regular/emit_web_hook_event.rb index 4a968513670..db90dd04019 100644 --- a/app/jobs/regular/emit_web_hook_event.rb +++ b/app/jobs/regular/emit_web_hook_event.rb @@ -8,7 +8,6 @@ module Jobs %i{ web_hook_id event_type - payload }.each do |key| raise Discourse::InvalidParameters.new(key) unless args[key].present? end @@ -25,6 +24,7 @@ module Jobs return if web_hook.category_ids.present? && (!args[:category_id].present? || !web_hook.category_ids.include?(args[:category_id])) + raise Discourse::InvalidParameters.new(:payload) unless args[:payload].present? args[:payload] = JSON.parse(args[:payload]) end diff --git a/spec/jobs/emit_web_hook_event_spec.rb b/spec/jobs/emit_web_hook_event_spec.rb index 7354d78b94b..97a86f66838 100644 --- a/spec/jobs/emit_web_hook_event_spec.rb +++ b/spec/jobs/emit_web_hook_event_spec.rb @@ -24,6 +24,16 @@ describe Jobs::EmitWebHookEvent do end.to raise_error(Discourse::InvalidParameters) end + it 'does not raise an error for a ping event without payload' do + stub_request(:post, "https://meta.discourse.org/webhook_listener") + .to_return(body: 'OK', status: 200) + + subject.execute( + web_hook_id: post_hook.id, + event_type: described_class::PING_EVENT + ) + end + it "doesn't emit when the hook is inactive" do subject.execute( web_hook_id: inactive_hook.id, @@ -99,7 +109,7 @@ describe Jobs::EmitWebHookEvent do web_hook_id: post_hook.id, event_type: described_class::PING_EVENT, event_name: described_class::PING_EVENT, - payload: { test: "some payload" }.to_json + payload: { test: "this payload shouldn't appear" }.to_json ) event = WebHookEvent.last