2022-07-27 10:34:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
describe Jobs::PublishGroupMembershipUpdates do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:job) { described_class.new }
|
|
|
|
|
2022-07-27 10:34:08 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
it "publishes events for added users" do
|
|
|
|
events =
|
|
|
|
DiscourseEvent.track_events do
|
2023-06-21 10:00:19 -04:00
|
|
|
job.execute(user_ids: [user.id], group_id: group.id, type: "add")
|
2022-07-27 10:34:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(events).to include(
|
|
|
|
event_name: :user_added_to_group,
|
|
|
|
params: [user, group, { automatic: group.automatic }],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "publishes events for removed users" do
|
|
|
|
events =
|
|
|
|
DiscourseEvent.track_events do
|
2023-06-21 10:00:19 -04:00
|
|
|
job.execute(user_ids: [user.id], group_id: group.id, type: "remove")
|
2022-07-27 10:34:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(events).to include(event_name: :user_removed_from_group, params: [user, group])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does nothing if the group doesn't exist" do
|
|
|
|
events =
|
2023-06-21 10:00:19 -04:00
|
|
|
DiscourseEvent.track_events { job.execute(user_ids: [user.id], group_id: nil, type: "add") }
|
2022-07-27 10:34:08 -04:00
|
|
|
|
|
|
|
expect(events).not_to include(
|
|
|
|
event_name: :user_added_to_group,
|
|
|
|
params: [user, group, { automatic: group.automatic }],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "fails when the update type is invalid" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect { job.execute(user_ids: [user.id], group_id: nil, type: nil) }.to raise_error(
|
2022-07-27 10:34:08 -04:00
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does nothing when the user is not human" do
|
|
|
|
events =
|
|
|
|
DiscourseEvent.track_events do
|
2023-06-21 10:00:19 -04:00
|
|
|
job.execute(user_ids: [Discourse.system_user.id], group_id: nil, type: "add")
|
2022-07-27 10:34:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(events).not_to include(
|
|
|
|
event_name: :user_added_to_group,
|
|
|
|
params: [user, group, { automatic: group.automatic }],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|