discourse/spec/initializers/web_hook_events_spec.rb
Selase Krakani cdf1589a85
FEATURE: Add support for user badge revocation webhook events (#21204)
Currently, only user badge grants emit webhook events. This change
extends the `user_badge` webhook to emit user badge revocation events.

A new `user_badge_revoked` event has been introduced instead of relying
on the existing `user_badge_removed` event. `user_badge_removed` emitted
just the `badge_id` and `user_id` which aren't helpful for generating a
meaningful webhook payload for revoked(deleted) user badges.

The new event emits  the user badge object.
2023-04-24 20:36:40 +00:00

32 lines
1.1 KiB
Ruby

# frozen_string_literal: true
RSpec.describe "Webhook event handlers" do
fab!(:user_badge) { Fabricate(:user_badge) }
fab!(:web_hook) { Fabricate(:user_badge_web_hook) }
fab!(:user) { Fabricate(:user) }
fab!(:badge) { Fabricate(:badge) }
fab!(:post) { Fabricate(:post) }
describe "user_badge events" do
it "enqueues user_badge_granted webhook event" do
expect do
BadgeGranter.grant(badge, user, granted_by: Discourse.system_user, post_id: post.id)
end.to change { Jobs::EmitWebHookEvent.jobs.size }.by(1)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["id"]).to eq(user.user_badges.last.id)
expect(job_args["event_name"]).to eq("user_badge_granted")
end
it "enqueues user_badge_revoked webhook event" do
expect { BadgeGranter.revoke(user_badge) }.to change { Jobs::EmitWebHookEvent.jobs.size }.by(
1,
)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["id"]).to eq(user_badge.id)
expect(job_args["event_name"]).to eq("user_badge_revoked")
end
end
end