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

AO3-6626: stop tags from being added with Chinese or Japanese commas #4663

Merged
merged 6 commits into from
Dec 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading