Commit f31ba9e7 authored by Toon Claes's avatar Toon Claes

AwardEmoji: Validate custom emoji as valid name

When validating award emoji, a custom emoji name is also valid.
parent 7a71c0f1
......@@ -11,9 +11,24 @@
module Gitlab
class EmojiNameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless TanukiEmoji.find_by_alpha_code(value.to_s)
record.errors.add(attribute, (options[:message] || 'is not a valid emoji name'))
end
return if valid_tanuki_emoji?(value)
return if valid_custom_emoji?(record, value)
record.errors.add(attribute, (options[:message] || 'is not a valid emoji name'))
end
private
def valid_tanuki_emoji?(value)
TanukiEmoji.find_by_alpha_code(value.to_s)
end
def valid_custom_emoji?(record, value)
namespace = record.try(:awardable).try(:namespace)
return unless namespace
namespace.custom_emoji&.by_name(value.to_s)&.any?
end
end
end
......@@ -58,6 +58,19 @@ RSpec.describe AwardEmoji do
end
end
end
it 'accepts custom emoji' do
user = create(:user)
group = create(:group)
group.add_maintainer(user)
project = create(:project, namespace: group)
issue = create(:issue, project: project)
emoji = create(:custom_emoji, name: 'partyparrot', namespace: group)
new_award = build(:award_emoji, user: user, awardable: issue, name: emoji.name)
expect(new_award).to be_valid
end
end
describe 'scopes' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment