From 3c56c9b6370b68d4483539d4c78dc7eead1faffa Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Mon, 11 Dec 2017 13:45:50 +0530 Subject: [PATCH] FIX: strip webhook payload_url --- app/jobs/regular/emit_web_hook_event.rb | 2 +- app/models/web_hook.rb | 6 ++++++ spec/models/web_hook_spec.rb | 6 +++++- 3 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 305c10d7e0f..f58b558ebb1 100644 --- a/app/jobs/regular/emit_web_hook_event.rb +++ b/app/jobs/regular/emit_web_hook_event.rb @@ -71,7 +71,7 @@ module Jobs end def web_hook_request(args, web_hook) - uri = URI(web_hook.payload_url) + uri = URI(web_hook.payload_url.strip) conn = Excon.new( uri.to_s, diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb index e3335e02043..493bdc22c00 100644 --- a/app/models/web_hook.rb +++ b/app/models/web_hook.rb @@ -13,6 +13,8 @@ class WebHook < ActiveRecord::Base validates_presence_of :last_delivery_status validates_presence_of :web_hook_event_types, unless: :wildcard_web_hook? + before_save :strip_url + def self.content_types @content_types ||= Enum.new('application/json' => 1, 'application/x-www-form-urlencoded' => 2) @@ -47,6 +49,10 @@ class WebHook < ActiveRecord::Base def self.enqueue_post_hooks(event, post, user = nil) WebHook.enqueue_hooks(:post, post_id: post.id, category_id: post&.topic&.category_id, event_name: event.to_s) end + + def strip_url + self.payload_url = (payload_url || "").strip.presence + end end # == Schema Information diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb index 8a8c3092a16..6a2fef7f396 100644 --- a/spec/models/web_hook_spec.rb +++ b/spec/models/web_hook_spec.rb @@ -35,9 +35,13 @@ describe WebHook do end context 'web hooks' do - let!(:post_hook) { Fabricate(:web_hook) } + let!(:post_hook) { Fabricate(:web_hook, payload_url: " https://example.com ") } let!(:topic_hook) { Fabricate(:topic_web_hook) } + it "removes whitspace from payload_url before saving" do + expect(post_hook.payload_url).to eq("https://example.com") + end + describe '#find_by_type' do it 'find relevant hooks' do expect(WebHook.find_by_type(:post)).to eq([post_hook])