2020-01-13 08:20:26 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe Jobs::MassAwardBadge do
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#execute" do
|
2020-01-13 08:20:26 -06:00
|
|
|
fab!(:badge) { Fabricate(:badge) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2023-01-09 05:18:21 -06:00
|
|
|
let(:email_mode) { "email" }
|
2020-01-13 08:20:26 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "creates the badge for an existing user" do
|
2021-07-14 21:53:26 -05:00
|
|
|
execute_job(user)
|
2020-01-13 08:20:26 -06:00
|
|
|
|
|
|
|
expect(UserBadge.where(user: user, badge: badge).exists?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "also creates a notification for the user" do
|
2021-07-14 21:53:26 -05:00
|
|
|
execute_job(user)
|
2020-01-13 08:20:26 -06:00
|
|
|
|
|
|
|
expect(Notification.exists?(user: user)).to eq(true)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(UserBadge.where.not(notification_id: nil).exists?(user: user, badge: badge)).to eq(
|
|
|
|
true,
|
|
|
|
)
|
2020-01-13 08:20:26 -06:00
|
|
|
end
|
2020-01-14 08:39:20 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "updates badge ranks correctly" do
|
2020-01-14 08:39:20 -06:00
|
|
|
user_2 = Fabricate(:user)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
UserBadge.create!(
|
|
|
|
badge_id: Badge::Member,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
)
|
2020-01-14 08:39:20 -06:00
|
|
|
|
2021-07-14 21:53:26 -05:00
|
|
|
execute_job(user)
|
|
|
|
execute_job(user_2)
|
2020-01-14 08:39:20 -06:00
|
|
|
|
|
|
|
expect(UserBadge.find_by(user: user, badge: badge).featured_rank).to eq(2)
|
|
|
|
expect(UserBadge.find_by(user: user_2, badge: badge).featured_rank).to eq(1)
|
|
|
|
end
|
2020-02-18 07:53:12 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "grants a badge multiple times to a user" do
|
2021-07-14 21:53:26 -05:00
|
|
|
badge.update!(multiple_grant: true)
|
|
|
|
Notification.destroy_all
|
|
|
|
execute_job(user, count: 4, grant_existing_holders: true)
|
|
|
|
instances = UserBadge.where(user: user, badge: badge)
|
|
|
|
expect(instances.count).to eq(4)
|
|
|
|
expect(instances.pluck(:seq).sort).to eq((0...4).to_a)
|
|
|
|
notifications = Notification.where(user: user)
|
|
|
|
expect(notifications.count).to eq(1)
|
|
|
|
expect(instances.map(&:notification_id).uniq).to contain_exactly(notifications.first.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_job(user, count: 1, grant_existing_holders: false)
|
2023-01-09 05:18:21 -06:00
|
|
|
subject.execute(
|
|
|
|
user: user.id,
|
|
|
|
badge: badge.id,
|
|
|
|
count: count,
|
|
|
|
grant_existing_holders: grant_existing_holders,
|
|
|
|
)
|
2020-02-18 07:53:12 -06:00
|
|
|
end
|
2020-01-13 08:20:26 -06:00
|
|
|
end
|
|
|
|
end
|