Files
danbooru/test/unit/favorite_group_test.rb
evazion d9dc84325f Fix #5365: Don't allow whitespace-only text submission.
Fix bug where it was possible to submit blank text in various text fields.

Caused by `String#blank?` not considering certain Unicode characters as blank. `blank?` is defined
as `match?(/\A[[:space:]]*\z/)`, where `[[:space:]]` matches ASCII spaces (space, tab, newline, etc)
and Unicode characters in the Space category ([1]). However, there are other space-like characters
not in the Space category. This includes U+200B (Zero-Width Space), and many more.

It turns out the "Default ignorable code points" [2][3] are what we're after. These are the set of 400
or so formatting and control characters that are invisible when displayed.

Note that there are other control characters that aren't invisible when rendered, instead they're
shown with a placeholder glyph. These include the ASCII C0 and C1 control codes [4], certain Unicode
control characters [5], and unassigned, reserved, and private use codepoints.

There is one outlier: the Braille pattern blank (U+2800) [6]. This character is visually blank, but is
not considered to be a space or an ignorable code point.

[1]: https://codepoints.net/search?gc[]=Z
[2]: https://codepoints.net/search?DI=1
[3]: https://www.unicode.org/review/pr-5.html
[4]: https://codepoints.net/search?gc[]=Cc
[5]: https://codepoints.net/search?gc[]=Cf
[6]: https://codepoints.net/U+2800
[7]: https://en.wikipedia.org/wiki/Whitespace_character
[8]: https://character.construction/blanks
[9]: https://invisible-characters.com
2022-12-05 01:58:34 -06:00

76 lines
2.2 KiB
Ruby

require 'test_helper'
class FavoriteGroupTest < ActiveSupport::TestCase
def setup
@fav_group = create(:favorite_group)
end
context "searching by post id" do
should "return the fav group" do
posts = create_list(:post, 3)
@fav_group.add(posts[0])
assert_equal(@fav_group.id, FavoriteGroup.for_post(posts[0].id).first.id)
@fav_group.add(posts[1])
assert_equal(@fav_group.id, FavoriteGroup.for_post(posts[1].id).first.id)
@fav_group.add(posts[2])
assert_equal(@fav_group.id, FavoriteGroup.for_post(posts[2].id).first.id)
end
end
context "expunging a post" do
should "remove it from all favorite groups" do
@post = create(:post_with_file, filename: "test.jpg")
@fav_group.add(@post)
assert_equal([@post.id], @fav_group.post_ids)
@post.expunge!(create(:admin_user))
assert_equal([], @fav_group.reload.post_ids)
end
end
context "adding a post to a favgroup" do
should "not allow adding duplicate posts" do
post = create(:post)
@fav_group.add(post)
assert(@fav_group.valid?)
assert_equal([post.id], @fav_group.reload.post_ids)
@fav_group.add(post)
assert_equal(false, @fav_group.valid?)
assert_match(/Favgroup already contains post #{post.id}/, @fav_group.errors.full_messages.join)
assert_equal([post.id], @fav_group.reload.post_ids)
@fav_group.reload.update(post_ids: [post.id, post.id])
refute(@fav_group.valid?)
assert_equal([post.id], @fav_group.reload.post_ids)
end
should "not allow adding nonexistent posts" do
@fav_group.update(post_ids: [0])
refute(@fav_group.valid?)
assert_equal([], @fav_group.reload.post_ids)
end
end
context "when validating names" do
subject { build(:favorite_group) }
should_not allow_value("foo,bar").for(:name)
should_not allow_value("foo*bar").for(:name)
should_not allow_value("123").for(:name)
should_not allow_value("_").for(:name)
should_not allow_value("any").for(:name)
should_not allow_value("none").for(:name)
should_not allow_value("").for(:name)
should_not allow_value(" ").for(:name)
should_not allow_value("\u200B").for(:name)
end
end