From da313ba49b718452c67f1084747480d64b87a533 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Thu, 28 Sep 2023 10:51:05 +0200 Subject: [PATCH] FEATURE: Add webhooks for user suspend and unsuspend (#23684) --- config/initializers/012-web_hook_events.rb | 4 ++ config/locales/client.en.yml | 2 +- spec/requests/admin/users_controller_spec.rb | 46 ++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/config/initializers/012-web_hook_events.rb b/config/initializers/012-web_hook_events.rb index d7a7e2979b8..9db88e1e1c7 100644 --- a/config/initializers/012-web_hook_events.rb +++ b/config/initializers/012-web_hook_events.rb @@ -43,6 +43,10 @@ end DiscourseEvent.on(event) { |user| WebHook.enqueue_object_hooks(:user, user, event) } end +%i[user_suspended user_unsuspended].each do |event| + DiscourseEvent.on(event) { |args| WebHook.enqueue_object_hooks(:user, args[:user], event) } +end + %i[group_created group_updated].each do |event| DiscourseEvent.on(event) { |group| WebHook.enqueue_object_hooks(:group, group, event) } end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 81151a77352..694276e9caa 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -4939,7 +4939,7 @@ en: details: "When there is a new reply, edit, deleted or recovered." user_event: name: "User Event" - details: "When a user logs in, logs out, confirms their email, is created, approved or updated." + details: "When a user logs in, logs out, confirms their email, is created, approved, updated, suspended or unsuspended." group_event: name: "Group Event" details: "When a group is created, updated or destroyed." diff --git a/spec/requests/admin/users_controller_spec.rb b/spec/requests/admin/users_controller_spec.rb index 6ccabc56a18..105ee759fd0 100644 --- a/spec/requests/admin/users_controller_spec.rb +++ b/spec/requests/admin/users_controller_spec.rb @@ -311,6 +311,29 @@ RSpec.describe Admin::UsersController do ) end + context "with webhook" do + fab!(:user_web_hook) { Fabricate(:user_web_hook) } + + it "enqueues a user_suspended webhook event" do + expect do + put "/admin/users/#{user.id}/suspend.json", + params: { + suspend_until: 5.hours.from_now, + reason: "because I said so", + } + end.to change { Jobs::EmitWebHookEvent.jobs.size }.by(2) + + user.reload + job_args = + Jobs::EmitWebHookEvent.jobs.last["args"].find do |args| + args["event_name"] == "user_suspended" + end + expect(job_args).to be_present + expect(job_args["id"]).to eq(user.id) + expect(job_args["payload"]).to eq(WebHook.generate_payload(:user, user)) + end + end + it "requires suspend_until and reason" do expect(user).not_to be_suspended put "/admin/users/#{user.id}/suspend.json", params: {} @@ -496,6 +519,29 @@ RSpec.describe Admin::UsersController do end end + describe "#unsuspend" do + context "when logged in as an admin" do + before { sign_in(admin) } + + context "with webhook" do + fab!(:user_web_hook) { Fabricate(:user_web_hook) } + + it "enqueues a user_unsuspended webhook event" do + user.update!(suspended_at: DateTime.now, suspended_till: 2.years.from_now) + + expect do put "/admin/users/#{user.id}/unsuspend.json" end.to change { + Jobs::EmitWebHookEvent.jobs.size + }.by(1) + + user.reload + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + expect(job_args["id"]).to eq(user.id) + expect(job_args["payload"]).to eq(WebHook.generate_payload(:user, user)) + end + end + end + end + describe "#revoke_admin" do fab!(:another_admin) { Fabricate(:admin) }