FEATURE: Add webhooks for user suspend and unsuspend (#23684)

This commit is contained in:
Gerhard Schlager 2023-09-28 10:51:05 +02:00 committed by GitHub
parent a2da2e02e7
commit da313ba49b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 1 deletions

View File

@ -43,6 +43,10 @@ end
DiscourseEvent.on(event) { |user| WebHook.enqueue_object_hooks(:user, user, event) } DiscourseEvent.on(event) { |user| WebHook.enqueue_object_hooks(:user, user, event) }
end 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| %i[group_created group_updated].each do |event|
DiscourseEvent.on(event) { |group| WebHook.enqueue_object_hooks(:group, group, event) } DiscourseEvent.on(event) { |group| WebHook.enqueue_object_hooks(:group, group, event) }
end end

View File

@ -4939,7 +4939,7 @@ en:
details: "When there is a new reply, edit, deleted or recovered." details: "When there is a new reply, edit, deleted or recovered."
user_event: user_event:
name: "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: group_event:
name: "Group Event" name: "Group Event"
details: "When a group is created, updated or destroyed." details: "When a group is created, updated or destroyed."

View File

@ -311,6 +311,29 @@ RSpec.describe Admin::UsersController do
) )
end 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 it "requires suspend_until and reason" do
expect(user).not_to be_suspended expect(user).not_to be_suspended
put "/admin/users/#{user.id}/suspend.json", params: {} put "/admin/users/#{user.id}/suspend.json", params: {}
@ -496,6 +519,29 @@ RSpec.describe Admin::UsersController do
end end
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 describe "#revoke_admin" do
fab!(:another_admin) { Fabricate(:admin) } fab!(:another_admin) { Fabricate(:admin) }