]> git.openstreetmap.org Git - rails.git/commitdiff
Added tests for validators
authorJ Guthrie <jamie.guthrie@gmail.com>
Mon, 5 Nov 2018 15:41:35 +0000 (15:41 +0000)
committerJ Guthrie <jamie.guthrie@gmail.com>
Mon, 5 Nov 2018 16:23:30 +0000 (16:23 +0000)
21 files changed:
app/models/changeset_comment.rb
app/models/changeset_tag.rb
app/models/diary_comment.rb
app/models/diary_entry.rb
app/models/message.rb
app/models/node_tag.rb
app/models/old_node_tag.rb
app/models/old_relation_tag.rb
app/models/old_way_tag.rb
app/models/redaction.rb
app/models/relation_tag.rb
app/models/trace.rb
app/models/tracetag.rb
app/models/user_block.rb
app/models/user_preference.rb
app/models/way_tag.rb
test/models/user_test.rb
test/validators/invalid_chars_validator_test.rb [new file with mode: 0644]
test/validators/invalid_url_chars_validator_test.rb [new file with mode: 0644]
test/validators/leading_whitespace_validator_test.rb [new file with mode: 0644]
test/validators/trailing_whitespace_validator_test.rb [new file with mode: 0644]

index 232785d95de585f6d2d508bf7381f31a0adfbb4f..0b84409dbcd3ffea5126deaad13173565fc5207f 100644 (file)
@@ -3,8 +3,8 @@
 # Table name: changeset_comments
 #
 #  id           :integer          not null, primary key
 # 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
 #  body         :text             not null
 #  created_at   :datetime         not null
 #  visible      :boolean          not null
index 1231fc24f5c2367b9d63a39a84a642b604e79da9..7cfea1e043ba378d86f2f5a2f0d1b6a5d6f1fd47 100644 (file)
@@ -2,7 +2,7 @@
 #
 # Table name: changeset_tags
 #
 #
 # 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
 #
 #  k            :string           default(""), not null, primary key
 #  v            :string           default(""), not null
 #
index 472129b1b6e174c0a525ff70a648b9d78b8904f9..475872b26bbeba365613ba30bd9819831d08c59f 100644 (file)
@@ -2,9 +2,9 @@
 #
 # Table name: diary_comments
 #
 #
 # 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
 #  body           :text             not null
 #  created_at     :datetime         not null
 #  updated_at     :datetime         not null
index d4139f65228f4d1e4a01a4dca73b45808fffe1a4..4990a999ad6de32134951cebb935a2755551e986 100644 (file)
@@ -2,8 +2,8 @@
 #
 # Table name: diary_entries
 #
 #
 # 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
 #  title         :string           not null
 #  body          :text             not null
 #  created_at    :datetime         not null
index 2447c3b7940821da7380764bbdc70801484dd057..889137de0f4b248d23f2e5830913c6a19a38d418 100644 (file)
@@ -2,13 +2,13 @@
 #
 # Table name: messages
 #
 #
 # 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
 #  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
 #  to_user_visible   :boolean          default(TRUE), not null
 #  from_user_visible :boolean          default(TRUE), not null
 #  body_format       :enum             default("markdown"), not null
index d1e80059232980373c6b1c18daeb10db458e7a4d..cdc11300e8e1f16df244ffb6738f6f572ff5b443 100644 (file)
@@ -2,7 +2,7 @@
 #
 # Table name: current_node_tags
 #
 #
 # 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
 #
 #  k       :string           default(""), not null, primary key
 #  v       :string           default(""), not null
 #
index f6fb93e17b8ab77b63e98dabeaeab5123f00e7ab..365841dca3134882039109adbe70b294a4c15680 100644 (file)
@@ -2,8 +2,8 @@
 #
 # Table name: node_tags
 #
 #
 # 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
 #
 #  k       :string           default(""), not null, primary key
 #  v       :string           default(""), not null
 #
index 3b75e1c820cb37a2d0351c840fac70bc9a1bf7bc..801ca7cab48cf48ee6513d5b856c449c82086b65 100644 (file)
@@ -2,10 +2,10 @@
 #
 # Table name: relation_tags
 #
 #
 # 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
 #  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
 #
 #
 # Foreign Keys
 #
index 14043546cb38feff5f0406757ffd39ece9452435..3662b1b7c8376f68a7a8dcc63e5a8a5912636dc0 100644 (file)
@@ -2,10 +2,10 @@
 #
 # Table name: way_tags
 #
 #
 # 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
 #  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
 #
 #
 # Foreign Keys
 #
index d9a5c6cd793091fd893facd7b8130c1af2bc2473..812224d36a657d922ad80c8e86365df322b9a8b4 100644 (file)
@@ -7,7 +7,7 @@
 #  description        :text
 #  created_at         :datetime
 #  updated_at         :datetime
 #  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
 #  description_format :enum             default("markdown"), not null
 #
 # Foreign Keys
index 02d5a216be67f29786a3a371e928a4ae6d11df1c..fc0ecdddf5ec043842924c4b08510c74750dae11 100644 (file)
@@ -2,7 +2,7 @@
 #
 # Table name: current_relation_tags
 #
 #
 # 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
 #
 #  k           :string           default(""), not null, primary key
 #  v           :string           default(""), not null
 #
index 0e81805509aea9ad62a3abe88de54f557bdb0f47..70b2aebbe3dff9e038bb681474a82e79ba9049b3 100644 (file)
@@ -2,18 +2,17 @@
 #
 # Table name: gpx_files
 #
 #
 # 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
 #  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
 #  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
 #
 #
 # Indexes
 #
index cb7cf1145814ef7dd7a9fa400510fa76956cdb5c..0a59ba29abb1dcbe9fbf856437298034a38b5e12 100644 (file)
@@ -2,9 +2,9 @@
 #
 # Table name: gpx_file_tags
 #
 #
 # Table name: gpx_file_tags
 #
-#  gpx_id :bigint(8)        default(0), not null
+#  gpx_id :integer          default(0), not null
 #  tag    :string           not null
 #  tag    :string           not null
-#  id     :bigint(8)        not null, primary key
+#  id     :integer          not null, primary key
 #
 # Indexes
 #
 #
 # Indexes
 #
index f56f87b9de255408ed72fb0d0a7044103c9591d0..5f313c640e993d316e184497b0b967b6e766a43e 100644 (file)
@@ -3,12 +3,12 @@
 # Table name: user_blocks
 #
 #  id            :integer          not null, primary key
 # 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
 #  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
 #  created_at    :datetime
 #  updated_at    :datetime
 #  reason_format :enum             default("markdown"), not null
index 781dd626a7c251cc98dcc56232f9c013bf5500ad..639bbc07af6b7e27bafeb595b8682956cdc021ed 100644 (file)
@@ -2,7 +2,7 @@
 #
 # Table name: user_preferences
 #
 #
 # 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
 #
 #  k       :string           not null, primary key
 #  v       :string           not null
 #
index 4e0f7ee2df933fb714646e01b54de1b0c4f38b50..7a5d9e72cc20b36f3b85620ce66bef1b750881e7 100644 (file)
@@ -2,7 +2,7 @@
 #
 # Table name: current_way_tags
 #
 #
 # 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
 #
 #  k      :string           default(""), not null, primary key
 #  v      :string           default(""), not null
 #
index 584f5df0e97c989a2618f25280c16dc5e99ea8a4..a17c30deb5eded5191d4e99ff872696d3efc97da 100644 (file)
@@ -79,7 +79,7 @@ class UserTest < ActiveSupport::TestCase
     # expact are allowed
     # However, would they affect the xml planet dumps?
     ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
     # expact are allowed
     # However, would they affect the xml planet dumps?
     ok = ["Name", "'me", "he\"", "<hr>", "*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 = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",
     # These need to be 3 chars in length, otherwise the length test above
     # should be used.
     bad = ["<hr/>", "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 (file)
index 0000000..1e46269
--- /dev/null
@@ -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\"", "<hr>", "*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<hr/>", "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 (file)
index 0000000..ac35dcd
--- /dev/null
@@ -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<hr>", "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\"#", "<hr/>", "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 (file)
index 0000000..e5c8374
--- /dev/null
@@ -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 (file)
index 0000000..5aac3be
--- /dev/null
@@ -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