From a129eb4251f5a49e123c632467e081b81980c5ff Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 19 Dec 2020 17:51:11 -0600 Subject: [PATCH] wikis: force wiki names to follow same rules as tag names. Don't allow wiki pages to have invalid names. This incidentally means that you can't create wiki pages for pools. For example, you can't create a wiki titled "pool:almost_heart-warming". This is not a valid tag name, so it's not a valid wiki name either. This was done in a handful of cases to translate Pixiv tags to Danbooru pools (see: ) Also fix it so that titles are normalized before validation, not before save. --- app/models/wiki_page.rb | 12 ++++------ test/functional/wiki_pages_controller_test.rb | 7 ------ test/unit/wiki_page_test.rb | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index a60e10863..79922b98c 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -3,14 +3,13 @@ class WikiPage < ApplicationRecord META_WIKIS = ["list_of_", "tag_group:", "pool_group:", "howto:", "about:", "help:", "template:"] - before_save :normalize_title - before_save :normalize_other_names + before_validation :normalize_title + before_validation :normalize_other_names before_save :update_dtext_links, if: :dtext_links_changed? after_save :create_version - validates_uniqueness_of :title, :case_sensitive => false - validates_presence_of :title - validates_presence_of :body, :unless => -> { is_deleted? || other_names.present? } + validates :title, tag_name: true, presence: true, uniqueness: true, if: :title_changed? + validates :body, presence: true, unless: -> { is_deleted? || other_names.present? } validate :validate_rename validate :validate_other_names @@ -149,8 +148,7 @@ class WikiPage < ApplicationRecord end def self.normalize_title(title) - return if title.blank? - title.downcase.delete_prefix("~").gsub(/[[:space:]]+/, "_").gsub(/__/, "_").gsub(/\A_|_\z/, "") + title.to_s.downcase.delete_prefix("~").gsub(/[[:space:]]+/, "_").gsub(/__/, "_").gsub(/\A_|_\z/, "") end def normalize_title diff --git a/test/functional/wiki_pages_controller_test.rb b/test/functional/wiki_pages_controller_test.rb index 5a4ad9c2b..0c8ae20b3 100644 --- a/test/functional/wiki_pages_controller_test.rb +++ b/test/functional/wiki_pages_controller_test.rb @@ -109,13 +109,6 @@ class WikiPagesControllerTest < ActionDispatch::IntegrationTest assert_response 404 end - should "render for a negated tag" do - as(@user) { @wiki_page.update(title: "-aaa") } - - get wiki_page_path(@wiki_page.id) - assert_redirected_to wiki_page_path(@wiki_page.title) - end - should "work for a title containing dots" do as(@user) { create(:wiki_page, title: "...") } diff --git a/test/unit/wiki_page_test.rb b/test/unit/wiki_page_test.rb index 41b814a8a..32391cfb8 100644 --- a/test/unit/wiki_page_test.rb +++ b/test/unit/wiki_page_test.rb @@ -78,6 +78,29 @@ class WikiPageTest < ActiveSupport::TestCase end end + context "during title validation" do + # these values are allowed because they're normalized first + should allow_value(" foo ").for(:title).on(:create) + should allow_value("~foo").for(:title).on(:create) + should allow_value("_foo").for(:title).on(:create) + should allow_value("foo_").for(:title).on(:create) + should allow_value("foo__bar").for(:title).on(:create) + should allow_value("FOO").for(:title).on(:create) + should allow_value("foo bar").for(:title).on(:create) + + should_not allow_value("").for(:title).on(:create) + should_not allow_value("___").for(:title).on(:create) + should_not allow_value("-foo").for(:title).on(:create) + should_not allow_value("/foo").for(:title).on(:create) + should_not allow_value("foo*bar").for(:title).on(:create) + should_not allow_value("foo,bar").for(:title).on(:create) + should_not allow_value("foo\abar").for(:title).on(:create) + should_not allow_value("café").for(:title).on(:create) + should_not allow_value("東方").for(:title).on(:create) + should_not allow_value("FAV:blah").for(:title).on(:create) + should_not allow_value("X"*171).for(:title).on(:create) + end + context "with other names" do should "not allow artist wikis to have other names" do tag = create(:artist_tag)