From: J Guthrie Date: Mon, 5 Nov 2018 15:41:35 +0000 (+0000) Subject: Added tests for validators X-Git-Tag: live~2826^2~5 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/1e57189366c0e4a49cb29658e9bf5985996709a2?ds=sidebyside Added tests for validators --- diff --git a/app/models/changeset_comment.rb b/app/models/changeset_comment.rb index 232785d95..0b84409db 100644 --- a/app/models/changeset_comment.rb +++ b/app/models/changeset_comment.rb @@ -3,8 +3,8 @@ # Table name: changeset_comments # # id :integer not null, primary key -# changeset_id :bigint(8) not null -# author_id :bigint(8) not null +# changeset_id :integer not null +# author_id :integer not null # body :text not null # created_at :datetime not null # visible :boolean not null diff --git a/app/models/changeset_tag.rb b/app/models/changeset_tag.rb index 1231fc24f..7cfea1e04 100644 --- a/app/models/changeset_tag.rb +++ b/app/models/changeset_tag.rb @@ -2,7 +2,7 @@ # # Table name: changeset_tags # -# changeset_id :bigint(8) not null, primary key +# changeset_id :integer not null, primary key # k :string default(""), not null, primary key # v :string default(""), not null # diff --git a/app/models/diary_comment.rb b/app/models/diary_comment.rb index 472129b1b..475872b26 100644 --- a/app/models/diary_comment.rb +++ b/app/models/diary_comment.rb @@ -2,9 +2,9 @@ # # Table name: diary_comments # -# id :bigint(8) not null, primary key -# diary_entry_id :bigint(8) not null -# user_id :bigint(8) not null +# id :integer not null, primary key +# diary_entry_id :integer not null +# user_id :integer not null # body :text not null # created_at :datetime not null # updated_at :datetime not null diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index d4139f652..4990a999a 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -2,8 +2,8 @@ # # Table name: diary_entries # -# id :bigint(8) not null, primary key -# user_id :bigint(8) not null +# id :integer not null, primary key +# user_id :integer not null # title :string not null # body :text not null # created_at :datetime not null diff --git a/app/models/message.rb b/app/models/message.rb index 2447c3b79..889137de0 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -2,13 +2,13 @@ # # Table name: messages # -# id :bigint(8) not null, primary key -# from_user_id :bigint(8) not null +# id :integer not null, primary key +# from_user_id :integer not null # title :string not null # body :text not null # sent_on :datetime not null # message_read :boolean default(FALSE), not null -# to_user_id :bigint(8) not null +# to_user_id :integer not null # to_user_visible :boolean default(TRUE), not null # from_user_visible :boolean default(TRUE), not null # body_format :enum default("markdown"), not null diff --git a/app/models/node_tag.rb b/app/models/node_tag.rb index d1e800592..cdc11300e 100644 --- a/app/models/node_tag.rb +++ b/app/models/node_tag.rb @@ -2,7 +2,7 @@ # # Table name: current_node_tags # -# node_id :bigint(8) not null, primary key +# node_id :integer not null, primary key # k :string default(""), not null, primary key # v :string default(""), not null # diff --git a/app/models/old_node_tag.rb b/app/models/old_node_tag.rb index f6fb93e17..365841dca 100644 --- a/app/models/old_node_tag.rb +++ b/app/models/old_node_tag.rb @@ -2,8 +2,8 @@ # # Table name: node_tags # -# node_id :bigint(8) not null, primary key -# version :bigint(8) not null, primary key +# node_id :integer not null, primary key +# version :integer not null, primary key # k :string default(""), not null, primary key # v :string default(""), not null # diff --git a/app/models/old_relation_tag.rb b/app/models/old_relation_tag.rb index 3b75e1c82..801ca7cab 100644 --- a/app/models/old_relation_tag.rb +++ b/app/models/old_relation_tag.rb @@ -2,10 +2,10 @@ # # Table name: relation_tags # -# relation_id :bigint(8) default(0), not null, primary key +# relation_id :integer default(0), not null, primary key # k :string default(""), not null, primary key # v :string default(""), not null -# version :bigint(8) not null, primary key +# version :integer not null, primary key # # Foreign Keys # diff --git a/app/models/old_way_tag.rb b/app/models/old_way_tag.rb index 14043546c..3662b1b7c 100644 --- a/app/models/old_way_tag.rb +++ b/app/models/old_way_tag.rb @@ -2,10 +2,10 @@ # # Table name: way_tags # -# way_id :bigint(8) default(0), not null, primary key +# way_id :integer default(0), not null, primary key # k :string not null, primary key # v :string not null -# version :bigint(8) not null, primary key +# version :integer not null, primary key # # Foreign Keys # diff --git a/app/models/redaction.rb b/app/models/redaction.rb index d9a5c6cd7..812224d36 100644 --- a/app/models/redaction.rb +++ b/app/models/redaction.rb @@ -7,7 +7,7 @@ # description :text # created_at :datetime # updated_at :datetime -# user_id :bigint(8) not null +# user_id :integer not null # description_format :enum default("markdown"), not null # # Foreign Keys diff --git a/app/models/relation_tag.rb b/app/models/relation_tag.rb index 02d5a216b..fc0ecdddf 100644 --- a/app/models/relation_tag.rb +++ b/app/models/relation_tag.rb @@ -2,7 +2,7 @@ # # Table name: current_relation_tags # -# relation_id :bigint(8) not null, primary key +# relation_id :integer not null, primary key # k :string default(""), not null, primary key # v :string default(""), not null # diff --git a/app/models/trace.rb b/app/models/trace.rb index 0e8180550..70b2aebbe 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -2,18 +2,17 @@ # # Table name: gpx_files # -# id :bigint(8) not null, primary key -# user_id :bigint(8) not null +# id :integer not null, primary key +# user_id :integer not null # visible :boolean default(TRUE), not null # name :string default(""), not null -# size :bigint(8) +# size :integer # latitude :float # longitude :float # timestamp :datetime not null # description :string default(""), not null # inserted :boolean not null # visibility :enum default("public"), not null -# length :bigint(8) # # Indexes # diff --git a/app/models/tracetag.rb b/app/models/tracetag.rb index cb7cf1145..0a59ba29a 100644 --- a/app/models/tracetag.rb +++ b/app/models/tracetag.rb @@ -2,9 +2,9 @@ # # Table name: gpx_file_tags # -# gpx_id :bigint(8) default(0), not null +# gpx_id :integer default(0), not null # tag :string not null -# id :bigint(8) not null, primary key +# id :integer not null, primary key # # Indexes # diff --git a/app/models/user_block.rb b/app/models/user_block.rb index f56f87b9d..5f313c640 100644 --- a/app/models/user_block.rb +++ b/app/models/user_block.rb @@ -3,12 +3,12 @@ # Table name: user_blocks # # id :integer not null, primary key -# user_id :bigint(8) not null -# creator_id :bigint(8) not null +# user_id :integer not null +# creator_id :integer not null # reason :text not null # ends_at :datetime not null # needs_view :boolean default(FALSE), not null -# revoker_id :bigint(8) +# revoker_id :integer # created_at :datetime # updated_at :datetime # reason_format :enum default("markdown"), not null diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 781dd626a..639bbc07a 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -2,7 +2,7 @@ # # Table name: user_preferences # -# user_id :bigint(8) not null, primary key +# user_id :integer not null, primary key # k :string not null, primary key # v :string not null # diff --git a/app/models/way_tag.rb b/app/models/way_tag.rb index 4e0f7ee2d..7a5d9e72c 100644 --- a/app/models/way_tag.rb +++ b/app/models/way_tag.rb @@ -2,7 +2,7 @@ # # Table name: current_way_tags # -# way_id :bigint(8) not null, primary key +# way_id :integer not null, primary key # k :string default(""), not null, primary key # v :string default(""), not null # diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 584f5df0e..a17c30deb 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -79,7 +79,7 @@ class UserTest < ActiveSupport::TestCase # expact are allowed # However, would they affect the xml planet dumps? ok = ["Name", "'me", "he\"", "
", "*ho", "\"help\"@", - "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲"] + "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "space space"] # These need to be 3 chars in length, otherwise the length test above # should be used. bad = ["
", "test@example.com", "s/f", "aa/", "aa;", "aa.", diff --git a/test/validators/invalid_chars_validator_test.rb b/test/validators/invalid_chars_validator_test.rb new file mode 100644 index 000000000..1e46269a0 --- /dev/null +++ b/test/validators/invalid_chars_validator_test.rb @@ -0,0 +1,36 @@ +require "test_helper" + +class InvalidCharsValidatable + include ActiveModel::Validations + validates :chars, :invalid_chars => true + attr_accessor :chars +end + +class InvalidCharsValidatorTest < ActiveSupport::TestCase + include Rails::Dom::Testing::Assertions::SelectorAssertions + + def test_with_valid_chars + c = InvalidCharsValidatable.new + + valid = ["Name.", "'me", "he\"", "
", "*ho", "\"help\"@", + "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "/;.,?%#"] + + valid.each do |v| + c.chars = v + assert c.valid?, "'#{v}' should be valid" + end + end + + def test_with_invalid_chars + c = InvalidCharsValidatable.new + + invalid = ["\x7f
", "test@example.com\x0e-", "s/\x1ff", "aa/\ufffe", + "aa\x0b-,", "aa?\x08", "/;\uffff.,?", "\x00-も対応します/", "\x0c#ping", + "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"] + + invalid.each do |v| + c.chars = v + assert_not c.valid?, "'#{v}' should not be valid" + end + end +end diff --git a/test/validators/invalid_url_chars_validator_test.rb b/test/validators/invalid_url_chars_validator_test.rb new file mode 100644 index 000000000..ac35dcdee --- /dev/null +++ b/test/validators/invalid_url_chars_validator_test.rb @@ -0,0 +1,36 @@ +require "test_helper" + +class InvalidUrlCharsValidatable + include ActiveModel::Validations + validates :chars, :invalid_url_chars => true + attr_accessor :chars +end + +class InvalidUrlCharsValidatorTest < ActiveSupport::TestCase + include Rails::Dom::Testing::Assertions::SelectorAssertions + + def test_with_valid_url_chars + c = InvalidUrlCharsValidatable.new + + valid = ["\x7f
", "test@examplecom\x0e-", "s\x1ff", "aa\ufffe", + "aa\x0b-", "aa\x08", "\uffff::", "\x00-も対応します", "\x0c*ping", + "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"] + + valid.each do |v| + c.chars = v + assert c.valid?, "'#{v}' should be valid" + end + end + + def test_with_invalid_url_chars + c = InvalidUrlCharsValidatable.new + + invalid = ["Name.", "you;me", "he\"#", "
", "50%", "good?", + "vergrößern,deutsche", "ルシステムに;.も対応します", "輕觸搖/晃的遊戲", "/;.,?%#"] + + invalid.each do |v| + c.chars = v + assert_not c.valid?, "'#{v}' should not be valid" + end + end +end diff --git a/test/validators/leading_whitespace_validator_test.rb b/test/validators/leading_whitespace_validator_test.rb new file mode 100644 index 000000000..e5c8374df --- /dev/null +++ b/test/validators/leading_whitespace_validator_test.rb @@ -0,0 +1,33 @@ +require "test_helper" + +class LeadingWhitespaceValidatable + include ActiveModel::Validations + validates :string, :leading_whitespace => true + attr_accessor :string +end + +class LeadingWhitespaceValidatorTest < ActiveSupport::TestCase + include Rails::Dom::Testing::Assertions::SelectorAssertions + + def test_with_leading_whitespace + validator = LeadingWhitespaceValidatable.new + + strings = [" ", " test", " ", "\ttest"] + + strings.each do |v| + validator.string = v + assert_not validator.valid?, "'#{v}' should not be valid" + end + end + + def test_without_leading_whitespace + validator = LeadingWhitespaceValidatable.new + + strings = ["test", "test ", "t est", "test\t", ".test", "_test"] + + strings.each do |v| + validator.string = v + assert validator.valid?, "'#{v}' should be valid" + end + end +end diff --git a/test/validators/trailing_whitespace_validator_test.rb b/test/validators/trailing_whitespace_validator_test.rb new file mode 100644 index 000000000..5aac3bef2 --- /dev/null +++ b/test/validators/trailing_whitespace_validator_test.rb @@ -0,0 +1,33 @@ +require "test_helper" + +class TrailingWhitespaceValidatable + include ActiveModel::Validations + validates :string, :trailing_whitespace => true + attr_accessor :string +end + +class TrailingWhitespaceValidatorTest < ActiveSupport::TestCase + include Rails::Dom::Testing::Assertions::SelectorAssertions + + def test_with_trailing_whitespace + validator = TrailingWhitespaceValidatable.new + + strings = [" ", "test ", " ", "test\t", "_test_ "] + + strings.each do |v| + validator.string = v + assert_not validator.valid?, "'#{v}' should not be valid" + end + end + + def test_without_trailing_whitespace + validator = TrailingWhitespaceValidatable.new + + strings = ["test", " test", "tes t", "\ttest", "test.", "test_"] + + strings.each do |v| + validator.string = v + assert validator.valid?, "'#{v}' should be valid" + end + end +end