2020-01-13 09:20:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Jobs::MassAwardBadge do
|
2020-01-13 09:20:26 -05:00
|
|
|
describe '#execute' do
|
|
|
|
fab!(:badge) { Fabricate(:badge) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2020-02-18 08:53:12 -05:00
|
|
|
let(:email_mode) { 'email' }
|
2020-01-13 09:20:26 -05:00
|
|
|
|
|
|
|
it 'creates the badge for an existing user' do
|
2021-07-14 22:53:26 -04:00
|
|
|
execute_job(user)
|
2020-01-13 09:20:26 -05:00
|
|
|
|
|
|
|
expect(UserBadge.where(user: user, badge: badge).exists?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'also creates a notification for the user' do
|
2021-07-14 22:53:26 -04:00
|
|
|
execute_job(user)
|
2020-01-13 09:20:26 -05:00
|
|
|
|
|
|
|
expect(Notification.exists?(user: user)).to eq(true)
|
|
|
|
expect(UserBadge.where.not(notification_id: nil).exists?(user: user, badge: badge)).to eq(true)
|
|
|
|
end
|
2020-01-14 09:39:20 -05:00
|
|
|
|
|
|
|
it 'updates badge ranks correctly' do
|
|
|
|
user_2 = Fabricate(:user)
|
|
|
|
|
|
|
|
UserBadge.create!(badge_id: Badge::Member, user: user, granted_by: Discourse.system_user, granted_at: Time.now)
|
|
|
|
|
2021-07-14 22:53:26 -04:00
|
|
|
execute_job(user)
|
|
|
|
execute_job(user_2)
|
2020-01-14 09:39:20 -05: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 08:53:12 -05:00
|
|
|
|
2021-07-14 22:53:26 -04:00
|
|
|
it 'grants a badge multiple times to a user' do
|
|
|
|
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)
|
|
|
|
subject.execute(user: user.id, badge: badge.id, count: count, grant_existing_holders: grant_existing_holders)
|
2020-02-18 08:53:12 -05:00
|
|
|
end
|
2020-01-13 09:20:26 -05:00
|
|
|
end
|
|
|
|
end
|