From 340874d3451dd47e729a80d6b8a5a7d093a283e1 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 4 Sep 2016 15:50:53 +1000 Subject: [PATCH] FIX: post notifications in JSON so we properly support arrays and so on --- app/jobs/regular/push_notification.rb | 3 +- spec/services/post_alerter_spec.rb | 71 +++++++++++++++------------ 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/app/jobs/regular/push_notification.rb b/app/jobs/regular/push_notification.rb index 78c9130ed72..651a7486bbe 100644 --- a/app/jobs/regular/push_notification.rb +++ b/app/jobs/regular/push_notification.rb @@ -20,7 +20,8 @@ module Jobs }) end - RestClient.send :post, push_url, payload.merge({notifications: notifications}) + RestClient.send :post, push_url, payload.merge({notifications: notifications}).to_json, content_type: :json, accept: :json + end end diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb index 184f965b364..93380605430 100644 --- a/spec/services/post_alerter_spec.rb +++ b/spec/services/post_alerter_spec.rb @@ -344,43 +344,50 @@ describe PostAlerter do push_url: "https://site2.com/push") end - # I want to test payload ... but we have chicked egg problem - # if I test it then it makes the req and the the expects is not correct ... - # need to track all reqs in rest client and check after the fact - # payload = { - # secret_key: SiteSetting.push_api_secret_key, - # url: Discourse.base_url, - # title: SiteSetting.title, - # description: SiteSetting.site_description, - # notifications: [ - # { - # 'notification_type' => 1, - # 'post_number' => 1, - # 'topic_title' => topic.title, - # 'topic_id' => topic.id, - # 'excerpt' => 'Hello @eviltrout', - # 'username' => user.username, - # 'url' => UrlHelper.absolute(mention_post.url), - # 'client_id' => 'xxx0' - # }, - # { - # 'notification_type' => 1, - # 'post_number' => 1, - # 'topic_title' => topic.title, - # 'topic_id' => topic.id, - # 'excerpt' => 'Hello @eviltrout', - # 'username' => user.username, - # 'url' => UrlHelper.absolute(mention_post.url), - # 'client_id' => 'xxx1' - # } - # ] - # } + + body = nil + headers = nil # should only happen once even though we are using 2 keys - RestClient.expects(:post).returns("OK") + RestClient.expects(:post).with{|_req,_body,_headers| + headers = _headers + body = _body + }.returns("OK") mention_post + + payload = { + "secret_key" => SiteSetting.push_api_secret_key, + "url" => Discourse.base_url, + "title" => SiteSetting.title, + "description" => SiteSetting.site_description, + "notifications" => [ + { + 'notification_type' => 1, + 'post_number' => 1, + 'topic_title' => topic.title, + 'topic_id' => topic.id, + 'excerpt' => 'Hello @eviltrout', + 'username' => user.username, + 'url' => UrlHelper.absolute(mention_post.url), + 'client_id' => 'xxx0' + }, + { + 'notification_type' => 1, + 'post_number' => 1, + 'topic_title' => topic.title, + 'topic_id' => topic.id, + 'excerpt' => 'Hello @eviltrout', + 'username' => user.username, + 'url' => UrlHelper.absolute(mention_post.url), + 'client_id' => 'xxx1' + } + ] + } + + expect(JSON.parse(body)).to eq(payload) + expect(headers[:content_type]).to eq(:json) end end