Skip to content

Commit

Permalink
AO3-6626: stop tags from being added with Chinese or Japanese commas (#…
Browse files Browse the repository at this point in the history
…4663)

* AO3-6626: prevent tags from being created with Chinese or Japanese commas

* AO3-6626: fix indentation per styleguide

* AO3-6626 correct rubocop violations

* AO3-6626 test all forbidden characters

* AO3-6626 update to meet rubocop standards

* AO3-6626 define forbidden tags inline

---------

Co-authored-by: nikobee <nikki@baileycharm.net>
  • Loading branch information
kwerey and nikobee authored Dec 4, 2023
1 parent e7979ef commit a0b7645
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
23 changes: 12 additions & 11 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def commentable_owners
end

has_many :mergers, foreign_key: 'merger_id', class_name: 'Tag'
belongs_to :merger, class_name: 'Tag'
belongs_to :merger, class_name: "Tag"
belongs_to :fandom
belongs_to :media
belongs_to :last_wrangler, polymorphic: true
Expand Down Expand Up @@ -161,17 +161,18 @@ def commentable_owners
has_many :tag_set_associations, dependent: :destroy
has_many :parent_tag_set_associations, class_name: 'TagSetAssociation', foreign_key: 'parent_tag_id', dependent: :destroy

validates_presence_of :name
validates :name, presence: true
validates :name, uniqueness: true
validates_length_of :name, minimum: 1, message: "cannot be blank."
validates_length_of :name,
maximum: ArchiveConfig.TAG_MAX,
message: "^Tag name '%{value}' is too long -- try using less than %{count} characters or using commas to separate your tags."
validates_format_of :name,
with: /\A[^,*<>^{}=`\\%]+\z/,
message: "^Tag name '%{value}' cannot include the following restricted characters: , &#94; * < > { } = ` \\ %"

validates_presence_of :sortable_name
validates :name,
length: { minimum: 1,
message: "cannot be blank." }
validates :name,
length: { maximum: ArchiveConfig.TAG_MAX,
message: "^Tag name '%{value}' is too long -- try using less than %{count} characters or using commas to separate your tags." }
validates :name,
format: { with: /\A[^,,、*<>^{}=`\\%]+\z/,
message: "^Tag name '%{value}' cannot include the following restricted characters: , &#94; * < > { } = ` , 、 \\ %" }
validates :sortable_name, presence: true

validate :unwrangleable_status
def unwrangleable_status
Expand Down
62 changes: 40 additions & 22 deletions spec/models/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
User.current_user = nil
end

context 'checking count caching' do
context "checking count caching" do
before(:each) do
# Set the minimal amount of time a tag can be cached for.
ArchiveConfig.TAGGINGS_COUNT_MIN_TIME = 1
Expand All @@ -17,15 +17,15 @@
@fandom_tag = FactoryBot.create(:fandom)
end

context 'without updating taggings_count_cache' do
it 'should not cache tags which are not used much' do
context "without updating taggings_count_cache" do
it "should not cache tags which are not used much" do
FactoryBot.create(:work, fandom_string: @fandom_tag.name)
@fandom_tag.reload
expect(@fandom_tag.taggings_count_cache).to eq 0
expect(@fandom_tag.taggings_count).to eq 1
end

it 'will start caching a when tag when that tag is used significantly' do
it "will start caching a when tag when that tag is used significantly" do
(1..ArchiveConfig.TAGGINGS_COUNT_MIN_CACHE_COUNT).each do |try|
FactoryBot.create(:work, fandom_string: @fandom_tag.name)
@fandom_tag.reload
Expand All @@ -40,16 +40,16 @@
end
end

context 'updating taggings_count_cache' do
it 'should not cache tags which are not used much' do
context "updating taggings_count_cache" do
it "should not cache tags which are not used much" do
FactoryBot.create(:work, fandom_string: @fandom_tag.name)
RedisJobSpawner.perform_now("TagCountUpdateJob")
@fandom_tag.reload
expect(@fandom_tag.taggings_count_cache).to eq 1
expect(@fandom_tag.taggings_count).to eq 1
end

it 'will start caching a when tag when that tag is used significantly' do
it "will start caching a tag when that tag is used significantly" do
(1..ArchiveConfig.TAGGINGS_COUNT_MIN_CACHE_COUNT).each do |try|
FactoryBot.create(:work, fandom_string: @fandom_tag.name)
RedisJobSpawner.perform_now("TagCountUpdateJob")
Expand All @@ -65,7 +65,7 @@
expect(@fandom_tag.taggings_count).to eq ArchiveConfig.TAGGINGS_COUNT_MIN_CACHE_COUNT
end

it "Writes to the database do not happen immeadiately" do
it "Writes to the database do not happen immediately" do
(1..40 * ArchiveConfig.TAGGINGS_COUNT_CACHE_DIVISOR - 1).each do |try|
@fandom_tag.taggings_count = try
@fandom_tag.reload
Expand Down Expand Up @@ -168,11 +168,29 @@ def expect_tag_update_flag_in_redis_to_be(flag)
expect(tag.errors[:name].join).to match(/too long/)
end

it "should not be valid with disallowed characters" do
tag = Tag.new
tag.name = "bad<tag"
expect(tag.save).to be_falsey
expect(tag.errors[:name].join).to match(/restricted characters/)
context "tags using restricted characters should not be saved" do
[
"bad, tag",
"also, bad",
"no、good",
"wild*card",
"back\\slash",
"lesser<tag",
"greater>tag",
"^tag",
"{open",
"close}",
"not=allowed",
"suspicious`character",
"no%maths"
].each do |tag|
forbidden_tag = Tag.new
forbidden_tag.name = tag
it "is not saved and receives an error message about restricted characters" do
expect(forbidden_tag.save).to be_falsey
expect(forbidden_tag.errors[:name].join).to match(/restricted characters/)
end
end
end

context "unwrangleable" do
Expand Down Expand Up @@ -250,20 +268,20 @@ def expect_tag_update_flag_in_redis_to_be(flag)
expect(tag.save).to be_falsey
end

it 'autocomplete should work' do
tag_character = FactoryBot.create(:character, canonical: true, name: 'kirk')
tag_fandom = FactoryBot.create(:fandom, name: 'Star Trek', canonical: true)
it "autocomplete should work" do
tag_character = FactoryBot.create(:character, canonical: true, name: "kirk")
tag_fandom = FactoryBot.create(:fandom, name: "Star Trek", canonical: true)
tag_fandom.add_to_autocomplete
results = Tag.autocomplete_fandom_lookup(term: 'ki', fandom: 'Star Trek')
results = Tag.autocomplete_fandom_lookup(term: "ki", fandom: "Star Trek")
expect(results.include?("#{tag_character.id}: #{tag_character.name}")).to be_truthy
expect(results.include?("brave_sire_robin")).to be_falsey
end

it 'old tag maker still works' do
tag_adult = Rating.create_canonical('adult', true)
tag_normal = ArchiveWarning.create_canonical('other')
expect(tag_adult.name).to eq('adult')
expect(tag_normal.name).to eq('other')
it "old tag maker still works" do
tag_adult = Rating.create_canonical("adult", true)
tag_normal = ArchiveWarning.create_canonical("other")
expect(tag_adult.name).to eq("adult")
expect(tag_normal.name).to eq("other")
expect(tag_adult.adult).to be_truthy
expect(tag_normal.adult).to be_falsey
end
Expand Down

0 comments on commit a0b7645

Please sign in to comment.