From dc33f2d071b39ecacd7323f28aa42c48f2b0bd5c Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Wed, 28 Mar 2018 17:40:29 +0530 Subject: [PATCH] Add new web hook serializers --- app/jobs/regular/emit_web_hook_event.rb | 12 ++++++++++++ .../web_hook_category_serializer.rb | 12 ++++++++++++ app/serializers/web_hook_group_serializer.rb | 12 ++++++++++++ spec/fabricators/web_hook_fabricator.rb | 19 +++++++++++++++++++ spec/jobs/emit_web_hook_event_spec.rb | 11 ++++++++--- 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 app/serializers/web_hook_category_serializer.rb create mode 100644 app/serializers/web_hook_group_serializer.rb diff --git a/app/jobs/regular/emit_web_hook_event.rb b/app/jobs/regular/emit_web_hook_event.rb index cc80fcebb2f..a893b49689a 100644 --- a/app/jobs/regular/emit_web_hook_event.rb +++ b/app/jobs/regular/emit_web_hook_event.rb @@ -50,6 +50,18 @@ module Jobs args[:payload] = WebHookUserSerializer.new(user, scope: guardian, root: false).as_json end + def setup_group(args) + group = Group.find(args[:group_id]) + return if group.blank? + args[:payload] = WebHookGroupSerializer.new(group, scope: guardian, root: false).as_json + end + + def setup_category(args) + category = Category.find(args[:category_id]) + return if category.blank? + args[:payload] = WebHookCategorySerializer.new(category, scope: guardian, root: false).as_json + end + def ping_event?(event_type) event_type.to_s == 'ping'.freeze end diff --git a/app/serializers/web_hook_category_serializer.rb b/app/serializers/web_hook_category_serializer.rb new file mode 100644 index 00000000000..02708ad1546 --- /dev/null +++ b/app/serializers/web_hook_category_serializer.rb @@ -0,0 +1,12 @@ +class WebHookCategorySerializer < CategorySerializer + + %i{ + can_edit + notification_level + }.each do |attr| + define_method("include_#{attr}?") do + false + end + end + +end diff --git a/app/serializers/web_hook_group_serializer.rb b/app/serializers/web_hook_group_serializer.rb new file mode 100644 index 00000000000..c580451db88 --- /dev/null +++ b/app/serializers/web_hook_group_serializer.rb @@ -0,0 +1,12 @@ +class WebHookGroupSerializer < GroupShowSerializer + + %i{ + is_group_user + is_group_owner + }.each do |attr| + define_method("include_#{attr}?") do + false + end + end + +end diff --git a/spec/fabricators/web_hook_fabricator.rb b/spec/fabricators/web_hook_fabricator.rb index d9354fb59e6..937fc0fee67 100644 --- a/spec/fabricators/web_hook_fabricator.rb +++ b/spec/fabricators/web_hook_fabricator.rb @@ -21,6 +21,9 @@ Fabricator(:wildcard_web_hook, from: :web_hook) do wildcard_web_hook true end +Fabricator(:post_web_hook, from: :web_hook) do +end + Fabricator(:topic_web_hook, from: :web_hook) do transient topic_hook: WebHookEventType.find_by(name: 'topic') @@ -36,3 +39,19 @@ Fabricator(:user_web_hook, from: :web_hook) do web_hook.web_hook_event_types = [transients[:user_hook]] end end + +Fabricator(:group_web_hook, from: :web_hook) do + transient group_hook: WebHookEventType.find_by(name: 'group') + + after_build do |web_hook, transients| + web_hook.web_hook_event_types = [transients[:group_hook]] + end +end + +Fabricator(:category_web_hook, from: :web_hook) do + transient category_hook: WebHookEventType.find_by(name: 'category') + + after_build do |web_hook, transients| + web_hook.web_hook_event_types = [transients[:category_hook]] + end +end diff --git a/spec/jobs/emit_web_hook_event_spec.rb b/spec/jobs/emit_web_hook_event_spec.rb index b7e1cca39d5..29be1e69250 100644 --- a/spec/jobs/emit_web_hook_event_spec.rb +++ b/spec/jobs/emit_web_hook_event_spec.rb @@ -56,9 +56,14 @@ describe Jobs::EmitWebHookEvent do stub_request(:post, "https://meta.discourse.org/webhook_listener") .to_return(body: 'OK', status: 200) - expect do - subject.execute(web_hook_id: post_hook.id, event_type: 'post', post_id: post.id) - end.to change(WebHookEvent, :count).by(1) + WebHookEventType.all.pluck(:name).each do |name| + web_hook_id = Fabricate("#{name}_web_hook").id + object_id = Fabricate(name).id + + expect do + subject.execute(web_hook_id: web_hook_id, event_type: name, "#{name}_id": object_id) + end.to change(WebHookEvent, :count).by(1) + end end it 'skips silently on missing post' do