]> git.openstreetmap.org Git - rails.git/blob - test/validators/characters_validator_test.rb
Convert `UserMailer#email_confirm` to new style
[rails.git] / test / validators / characters_validator_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class CharactersValidatorTest < ActiveSupport::TestCase
6   include Rails::Dom::Testing::Assertions::SelectorAssertions
7
8   class InvalidCharsValidatable
9     include ActiveModel::Validations
10
11     validates :chars, :characters => true
12     attr_accessor :chars
13   end
14
15   class InvalidUrlCharsValidatable
16     include ActiveModel::Validations
17
18     validates :chars, :characters => { :url_safe => true }
19     attr_accessor :chars
20   end
21
22   def test_with_valid_chars
23     c = InvalidCharsValidatable.new
24
25     valid = ["Name.", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
26              "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "/;.,?%#"]
27
28     valid.each do |v|
29       c.chars = v
30       assert_predicate c, :valid?, "'#{v}' should be valid"
31     end
32   end
33
34   def test_with_invalid_chars
35     c = InvalidCharsValidatable.new
36
37     invalid = ["\x7f<hr/>", "test@example.com\x0e-", "s/\x1ff", "aa/\ufffe",
38                "aa\x0b-,", "aa?\x08", "/;\uffff.,?", "\x00-も対応します/", "\x0c#ping",
39                "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"]
40
41     invalid.each do |v|
42       c.chars = v
43       assert_not_predicate c, :valid?, "'#{v}' should not be valid"
44     end
45   end
46
47   def test_with_valid_url_chars
48     c = InvalidUrlCharsValidatable.new
49
50     valid = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
51              "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲"]
52
53     valid.each do |v|
54       c.chars = v
55       assert_predicate c, :valid?, "'#{v}' should be valid"
56     end
57   end
58
59   def test_with_invalid_url_chars
60     c = InvalidUrlCharsValidatable.new
61
62     invalid = ["Name.", "you;me", "he\"#", "<hr/>", "50%", "good?",
63                "vergrößern,deutsche", "ルシステムに;.も対応します", "輕觸搖/晃的遊戲", "/;.,?%#"]
64
65     invalid.each do |v|
66       c.chars = v
67       assert_not_predicate c, :valid?, "'#{v}' should not be valid"
68     end
69   end
70 end