From 6fbd7c23e188b96c8d98e4212c68aa61deea94f9 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 13 Nov 2017 16:54:18 -0600 Subject: [PATCH] Fix #3363: Prevent setting tags to invalid categories. --- app/logical/tag_category.rb | 6 +++++- app/models/tag.rb | 1 + test/unit/tag_test.rb | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/logical/tag_category.rb b/app/logical/tag_category.rb index 6495a05b4..c36d15c62 100644 --- a/app/logical/tag_category.rb +++ b/app/logical/tag_category.rb @@ -45,6 +45,10 @@ class TagCategory @@categories ||= Danbooru.config.full_tag_config_info.keys end + def category_ids + @@categories ||= canonical_mapping.values + end + def short_name_list @@short_name_list ||= short_name_mapping.keys end @@ -75,4 +79,4 @@ class TagCategory extend Mappings extend Lists extend Regexes -end \ No newline at end of file +end diff --git a/app/models/tag.rb b/app/models/tag.rb index 9045295cb..6980dafdb 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -12,6 +12,7 @@ class Tag < ApplicationRecord has_many :consequent_implications, lambda {active}, :class_name => "TagImplication", :foreign_key => "consequent_name", :primary_key => "name" validates :name, uniqueness: true, tag_name: true, on: :create + validates_inclusion_of :category, in: TagCategory.category_ids module ApiMethods def to_legacy_json diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index 6e4a1f32b..bcc47ff3f 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -119,6 +119,10 @@ class TagTest < ActiveSupport::TestCase tag.update_category_cache_for_all assert_equal(Tag.categories.copyright, Cache.get("tc:#{Cache.hash(tag.name)}")) end + + context "not be settable to an invalid category" do + should validate_inclusion_of(:category).in_array(TagCategory.category_ids) + end end context "A tag parser" do