FIX: Bulk badge awards should work even if the CSV has nil values

This commit is contained in:
romanrizzi 2020-02-27 11:07:46 -03:00
parent d461772661
commit 4673f31c75
3 changed files with 24 additions and 6 deletions

View File

@ -55,8 +55,12 @@ class Admin::BadgesController < Admin::AdminController
csv.rewind
csv.each_line do |email_line|
batch.concat CSV.parse_line(email_line)
line_number += 1
line = CSV.parse_line(email_line).first
if line.present?
batch << line
line_number += 1
end
# Split the emails in batches of 200 elements.
full_batch = csv.lineno % (BadgeGranter::MAX_ITEMS_FOR_DELTA * batch_number) == 0

View File

@ -0,0 +1,4 @@
username1,
username2,
username3,
,,
1 username1,
2 username2,
3 username3,
4 ,,

View File

@ -179,6 +179,8 @@ describe Admin::BadgesController do
end
describe '#mass_award' do
before { @user = Fabricate(:user, email: 'user1@test.com', username: 'username1') }
it 'does nothing when there is no file' do
post "/admin/badges/award/#{badge.id}.json", params: { file: '' }
@ -202,23 +204,31 @@ describe Admin::BadgesController do
it 'awards the badge using a list of user emails' do
Jobs.run_immediately!
user = Fabricate(:user, email: 'user1@test.com')
file = file_from_fixtures('user_emails.csv', 'csv')
post "/admin/badges/award/#{badge.id}.json", params: { file: fixture_file_upload(file) }
expect(UserBadge.exists?(user: user, badge: badge)).to eq(true)
expect(UserBadge.exists?(user: @user, badge: badge)).to eq(true)
end
it 'awards the badge using a list of usernames' do
Jobs.run_immediately!
user = Fabricate(:user, username: 'username1')
file = file_from_fixtures('usernames.csv', 'csv')
post "/admin/badges/award/#{badge.id}.json", params: { file: fixture_file_upload(file) }
expect(UserBadge.exists?(user: user, badge: badge)).to eq(true)
expect(UserBadge.exists?(user: @user, badge: badge)).to eq(true)
end
it 'works with a CSV containing nil values' do
Jobs.run_immediately!
file = file_from_fixtures('usernames_with_nil_values.csv', 'csv')
post "/admin/badges/award/#{badge.id}.json", params: { file: fixture_file_upload(file) }
expect(UserBadge.exists?(user: @user, badge: badge)).to eq(true)
end
end
end