0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 04:48:33 +08:00
discourse/spec/jobs/bulk_grant_trust_level_spec.rb
Bannon Tanner 595c5b3be3
PERF: Add Users to Group Pt. 1 (#38737)
#38125 attempted to do the entire performance update to group add users
in one go, this is Pt 1 for splitting it into multiple, easier to digest
PRs. The main goal for this PR is to change any additions to group
members through one place.

GroupUser callbacks intentionally left in place for this PR to not break
the paths that create GroupUser directly.

### Details
- Add new `GroupManager` class that will be the single source of truth
for adding members to a group.
- Modify the `bulk_add` and `bulk_remove` methods to handle all
side-effects from the original callbacks.
- Add other bulk methods in needed places (group_user, category_user,
tag_user, group_action_logger)
- Add tests for all the new paths
- Fix tests that relied on group being saved before a member could be
added (were using `Fabricate.build` and manually saving later)

---------

Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
2026-03-26 09:11:22 -05:00

38 lines
1.2 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe Jobs::BulkGrantTrustLevel do
it "raises an error when trust_level is missing" do
expect { Jobs::BulkGrantTrustLevel.new.execute(user_ids: [1, 2]) }.to raise_error(
Discourse::InvalidParameters,
)
end
it "raises an error when user_ids are missing" do
expect { Jobs::BulkGrantTrustLevel.new.execute(trust_level: 0) }.to raise_error(
Discourse::InvalidParameters,
)
end
it "updates the trust_level" do
user1 = Fabricate(:user, email: "foo@wat.com", trust_level: 0)
user2 = Fabricate(:user, email: "foo@bar.com", trust_level: 2)
Jobs::BulkGrantTrustLevel.new.execute(trust_level: 3, user_ids: [user1.id, user2.id])
user1.reload
user2.reload
expect(user1.trust_level).to eq(3)
expect(user2.trust_level).to eq(3)
end
it "recalculates trust level when recalculate is true" do
group = Fabricate(:group, grant_trust_level: 3)
user = Fabricate(:user, trust_level: 3)
group.bulk_add([user.id])
group.bulk_remove([user.id])
Jobs::BulkGrantTrustLevel.new.execute(user_ids: [user.id], recalculate: true)
expect(user.reload.trust_level).to eq(0)
end
end