FIX: Show a proper error message when trying to bulk award a disabled badge. (#12384)

This commit is contained in:
Roman Rizzi 2021-03-12 14:28:27 -03:00 committed by GitHub
parent e7ac906f21
commit 9a779ca99a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View File

@ -43,6 +43,14 @@ class Admin::BadgesController < Admin::AdminController
badge = Badge.find_by(id: params[:badge_id])
raise Discourse::InvalidParameters if csv_file.try(:tempfile).nil? || badge.nil?
if !badge.enabled?
render_json_error(
I18n.t('badges.mass_award.errors.badge_disabled', badge_name: badge.display_name),
status: 422
)
return
end
replace_badge_owners = params[:replace_badge_owners] == 'true'
BadgeGranter.revoke_all(badge) if replace_badge_owners

View File

@ -4391,6 +4391,7 @@ en:
mass_award:
errors:
invalid_csv: We encountered an error on line %{line_number}. Please confirm the CSV has one email per line.
badge_disabled: Please enable the %{badge_name} badge first.
editor:
name: Editor
description: First post edit

View File

@ -229,6 +229,16 @@ describe Admin::BadgesController do
expect(UserBadge.exists?(user: @user, badge: badge)).to eq(true)
end
it 'fails when the badge is disabled' do
badge.update!(enabled: false)
file = file_from_fixtures('usernames_with_nil_values.csv', 'csv')
post "/admin/badges/award/#{badge.id}.json", params: { file: fixture_file_upload(file) }
expect(response.status).to eq(422)
end
end
end
end