discourse/spec/jobs/mass_award_badge_spec.rb
Osama Sayegh 31aa701518
FEATURE: Add option to grant badge multiple times to users using Bulk Award (#13571)
Currently when bulk-awarding a badge that can be granted multiple times, users in the CSV file are granted the badge once no matter how many times they're listed in the file and only if they don't have the badge already.

This PR adds a new option to the Badge Bulk Award feature so that it's possible to grant users a badge even if they already have the badge and as many times as they appear in the CSV file.
2021-07-15 05:53:26 +03:00

53 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe Jobs::MassAwardBadge do
describe '#execute' do
fab!(:badge) { Fabricate(:badge) }
fab!(:user) { Fabricate(:user) }
let(:email_mode) { 'email' }
it 'creates the badge for an existing user' do
execute_job(user)
expect(UserBadge.where(user: user, badge: badge).exists?).to eq(true)
end
it 'also creates a notification for the user' do
execute_job(user)
expect(Notification.exists?(user: user)).to eq(true)
expect(UserBadge.where.not(notification_id: nil).exists?(user: user, badge: badge)).to eq(true)
end
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)
execute_job(user)
execute_job(user_2)
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
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)
end
end
end