From 467d91347a7e5c42b68ef2d963863ddaddbb12b1 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 21 May 2018 17:29:19 +0800 Subject: [PATCH] Missing specs for `Group`, `Tag`, `Category` and `Flag` web hooks. --- app/controllers/categories_controller.rb | 2 - app/controllers/groups_controller.rb | 2 - app/controllers/tags_controller.rb | 1 - app/models/category.rb | 18 ++-- app/models/group.rb | 18 ++-- app/models/tag.rb | 18 ++-- config/initializers/012-web_hook_events.rb | 4 +- spec/models/post_action_spec.rb | 2 +- spec/models/web_hook_spec.rb | 118 +++++++++++++++++++++ 9 files changed, 151 insertions(+), 32 deletions(-) diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 027a5ddf629..f9216fc56be 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -150,7 +150,6 @@ class CategoriesController < ApplicationController old_permissions = cat.permissions_params if result = cat.update(category_params) - DiscourseEvent.trigger(:category_updated, cat) Scheduler::Defer.later "Log staff action change category settings" do @staff_action_logger.log_category_settings_change(@category, category_params, old_permissions) end @@ -167,7 +166,6 @@ class CategoriesController < ApplicationController custom_slug = params[:slug].to_s if custom_slug.present? && @category.update_attributes(slug: custom_slug) - DiscourseEvent.trigger(:category_updated, @category) render json: success_json else render_json_error(@category) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 54906c2d611..40dab0ff3ee 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -136,8 +136,6 @@ class GroupsController < ApplicationController if group.update(group_params(automatic: group.automatic)) GroupActionLogger.new(current_user, group).log_change_group_settings - DiscourseEvent.trigger(:group_updated, group) - render json: success_json else render_json_error(group) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 09ad79c44fc..a673bb06d08 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -111,7 +111,6 @@ class TagsController < ::ApplicationController tag.name = new_tag_name if tag.save StaffActionLogger.new(current_user).log_custom('renamed_tag', previous_value: params[:tag_id], new_value: new_tag_name) - DiscourseEvent.trigger(:tag_updated, tag) render json: { tag: { id: new_tag_name } } else render_json_error tag.errors.full_messages diff --git a/app/models/category.rb b/app/models/category.rb index 432509ded68..4d042500afd 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -62,6 +62,7 @@ class Category < ActiveRecord::Base after_update :create_category_permalink, if: :saved_change_to_slug? after_commit :trigger_category_created_event, on: :create + after_commit :trigger_category_updated_event, on: :update after_commit :trigger_category_destroyed_event, on: :destroy belongs_to :parent_category, class_name: 'Category' @@ -512,14 +513,15 @@ SQL subcategory_list_style.end_with?("with_featured_topics") end - def trigger_category_created_event - DiscourseEvent.trigger(:category_created, self) - true - end - - def trigger_category_destroyed_event - DiscourseEvent.trigger(:category_destroyed, self) - true + %i{ + category_created + category_updated + category_destroyed + }.each do |event| + define_method("trigger_#{event}_event") do + DiscourseEvent.trigger(event, self) + true + end end end diff --git a/app/models/group.rb b/app/models/group.rb index 53f9d6a3cae..1a895775ded 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -36,6 +36,7 @@ class Group < ActiveRecord::Base after_destroy :expire_cache after_commit :trigger_group_created_event, on: :create + after_commit :trigger_group_updated_event, on: :update after_commit :trigger_group_destroyed_event, on: :destroy def expire_cache @@ -584,14 +585,15 @@ class Group < ActiveRecord::Base self.member_of(groups, user).where("gu.owner") end - def trigger_group_created_event - DiscourseEvent.trigger(:group_created, self) - true - end - - def trigger_group_destroyed_event - DiscourseEvent.trigger(:group_destroyed, self) - true + %i{ + group_created + group_updated + group_destroyed + }.each do |event| + define_method("trigger_#{event}_event") do + DiscourseEvent.trigger(event, self) + true + end end protected diff --git a/app/models/tag.rb b/app/models/tag.rb index 62aee017d37..46ef77bde7b 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -17,6 +17,7 @@ class Tag < ActiveRecord::Base after_save :index_search after_commit :trigger_tag_created_event, on: :create + after_commit :trigger_tag_updated_event, on: :update after_commit :trigger_tag_destroyed_event, on: :destroy def self.ensure_consistency! @@ -124,14 +125,15 @@ class Tag < ActiveRecord::Base SearchIndexer.index(self) end - def trigger_tag_created_event - DiscourseEvent.trigger(:tag_created, self) - true - end - - def trigger_tag_destroyed_event - DiscourseEvent.trigger(:tag_destroyed, self) - true + %i{ + tag_created + tag_updated + tag_destroyed + }.each do |event| + define_method("trigger_#{event}_event") do + DiscourseEvent.trigger(event, self) + true + end end end diff --git a/config/initializers/012-web_hook_events.rb b/config/initializers/012-web_hook_events.rb index 731792717f7..671fbd56b7a 100644 --- a/config/initializers/012-web_hook_events.rb +++ b/config/initializers/012-web_hook_events.rb @@ -73,7 +73,7 @@ end tag_destroyed ).each do |event| DiscourseEvent.on(event) do |tag| - WebHook.enqueue_object_hooks(:tag, tag, event) + WebHook.enqueue_object_hooks(:tag, tag, event, TagSerializer) end end @@ -84,6 +84,6 @@ end flag_deferred ).each do |event| DiscourseEvent.on(event) do |flag| - WebHook.enqueue_object_hooks(:flag, flag, event, WebHookFlagSerializer) + WebHook.enqueue_object_hooks(:flag, flag, event) end end diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 7a10f346485..a8be0634a5a 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -721,7 +721,7 @@ describe PostAction do end end - describe "triggers webhook events" do + describe "triggers Discourse events" do let(:post) { Fabricate(:post) } it 'flag created' do diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb index e18f31f2706..7057d26852f 100644 --- a/spec/models/web_hook_spec.rb +++ b/spec/models/web_hook_spec.rb @@ -297,5 +297,123 @@ describe WebHook do payload = JSON.parse(job_args["payload"]) expect(payload["id"]).to eq(user.id) end + + it 'should enqueue the right hooks for category events' do + Fabricate(:category_web_hook) + category = Fabricate(:category) + + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("category_created") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(category.id) + + category.update!(slug: 'testing') + + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("category_updated") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(category.id) + expect(payload["slug"]).to eq('testing') + + category.destroy! + + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("category_destroyed") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(category.id) + end + + it 'should enqueue the right hooks for group events' do + Fabricate(:group_web_hook) + group = Fabricate(:group) + + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("group_created") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(group.id) + + group.update!(full_name: 'testing') + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("group_updated") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(group.id) + expect(payload["full_name"]).to eq('testing') + + group.destroy! + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("group_destroyed") + payload = JSON.parse(job_args["payload"]) + expect(payload["full_name"]).to eq('testing') + end + + it 'should enqueue the right hooks for tag events' do + Fabricate(:tag_web_hook) + tag = Fabricate(:tag) + + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("tag_created") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(tag.id) + + tag.update!(name: 'testing') + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("tag_updated") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(tag.id) + expect(payload["name"]).to eq('testing') + + tag.destroy! + + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("tag_destroyed") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(tag.id) + end + + it 'should enqueue the right hooks for flag events' do + post = Fabricate(:post) + admin = Fabricate(:admin) + moderator = Fabricate(:moderator) + Fabricate(:flag_web_hook) + + post_action = PostAction.act(admin, post, PostActionType.types[:spam]) + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("flag_created") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(post_action.id) + + PostAction.agree_flags!(post, moderator) + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("flag_agreed") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(post_action.id) + + PostAction.clear_flags!(post, moderator) + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("flag_disagreed") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(post_action.id) + + post = Fabricate(:post) + post_action = PostAction.act(admin, post, PostActionType.types[:spam]) + PostAction.defer_flags!(post, moderator) + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("flag_deferred") + payload = JSON.parse(job_args["payload"]) + expect(payload["id"]).to eq(post_action.id) + end end end