Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validations of reactions limit #12955

Merged
merged 1 commit into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/validators/reaction_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class ReactionValidator < ActiveModel::Validator
LIMIT = 8

def validate(reaction)
return if reaction.name.blank? || reaction.custom_emoji_id.present?
return if reaction.name.blank?

reaction.errors.add(:name, I18n.t('reactions.errors.unrecognized_emoji')) unless unicode_emoji?(reaction.name)
reaction.errors.add(:base, I18n.t('reactions.errors.limit_reached')) if limit_reached?(reaction)
reaction.errors.add(:name, I18n.t('reactions.errors.unrecognized_emoji')) if reaction.custom_emoji_id.blank? && !unicode_emoji?(reaction.name)
reaction.errors.add(:base, I18n.t('reactions.errors.limit_reached')) if new_reaction?(reaction) && limit_reached?(reaction)
end

private
Expand All @@ -18,6 +18,10 @@ def unicode_emoji?(name)
SUPPORTED_EMOJIS.include?(name)
end

def new_reaction?(reaction)
!reaction.announcement.announcement_reactions.where(name: reaction.name).exists?
end

def limit_reached?(reaction)
reaction.announcement.announcement_reactions.where.not(name: reaction.name).count('distinct name') >= LIMIT
end
Expand Down
42 changes: 42 additions & 0 deletions spec/validators/reaction_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require 'rails_helper'

describe ReactionValidator do
let(:announcement) { Fabricate(:announcement) }

describe '#validate' do
it 'adds error when not a valid unicode emoji' do
reaction = announcement.announcement_reactions.build(name: 'F')
subject.validate(reaction)
expect(reaction.errors).to_not be_empty
end

it 'does not add error when non-unicode emoji is a custom emoji' do
custom_emoji = Fabricate(:custom_emoji)
reaction = announcement.announcement_reactions.build(name: custom_emoji.shortcode, custom_emoji_id: custom_emoji.id)
subject.validate(reaction)
expect(reaction.errors).to be_empty
end

it 'adds error when 8 reactions already exist' do
%w(🐘 ❤️ 🙉 😍 😋 😂 😞 👍).each do |name|
announcement.announcement_reactions.create!(name: name, account: Fabricate(:account))
end

reaction = announcement.announcement_reactions.build(name: '😘')
subject.validate(reaction)
expect(reaction.errors).to_not be_empty
end

it 'does not add error when new reaction is part of the existing ones' do
%w(🐘 ❤️ 🙉 😍 😋 😂 😞 👍).each do |name|
announcement.announcement_reactions.create!(name: name, account: Fabricate(:account))
end

reaction = announcement.announcement_reactions.build(name: '😋')
subject.validate(reaction)
expect(reaction.errors).to be_empty
end
end
end