From 519026a31f4dadaf2bbc645d54a77efdf697b96e Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 27 Feb 2018 11:28:31 +0800 Subject: [PATCH] Trim attributes that are not useful from `WebHookTopicViewSerializer`. --- .../web_hook_topic_view_serializer.rb | 17 +++++++---- .../web_hook_topic_view_serializer_spec.rb | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 spec/serializers/web_hook_topic_view_serializer_spec.rb diff --git a/app/serializers/web_hook_topic_view_serializer.rb b/app/serializers/web_hook_topic_view_serializer.rb index 75c99aeb14a..2beca4f3ee6 100644 --- a/app/serializers/web_hook_topic_view_serializer.rb +++ b/app/serializers/web_hook_topic_view_serializer.rb @@ -1,11 +1,18 @@ require_dependency 'pinned_check' class WebHookTopicViewSerializer < TopicViewSerializer - def include_post_stream? - false - end - def include_timeline_lookup? - false + %i{ + post_stream + timeline_lookup + pm_with_non_human_user + draft + draft_key + draft_sequence + message_bus_last_id + }.each do |attr| + define_method("include_#{attr}?") do + false + end end end diff --git a/spec/serializers/web_hook_topic_view_serializer_spec.rb b/spec/serializers/web_hook_topic_view_serializer_spec.rb new file mode 100644 index 00000000000..2f120b26566 --- /dev/null +++ b/spec/serializers/web_hook_topic_view_serializer_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +RSpec.describe WebHookTopicViewSerializer do + let(:admin) { Fabricate(:admin) } + let(:topic) { Fabricate(:topic) } + + let(:serializer) do + WebHookTopicViewSerializer.new(TopicView.new(topic), + scope: Guardian.new(admin), + root: false + ) + end + + it 'should only include the required keys' do + count = serializer.as_json.keys.count + difference = count - 35 + + expect(difference).to eq(0), lambda { + message = "" + + if difference < 0 + message << "#{difference * -1} key(s) have been removed from this serializer." + else + message << "#{difference} key(s) have been added to this serializer." + end + + message << "\nPlease verify if those key(s) are required as part of the web hook's payload." + } + end +end