]> git.openstreetmap.org Git - rails.git/commitdiff
Add validation for relation member roles
authorTom Hughes <tom@compton.nu>
Thu, 9 Jun 2022 18:11:59 +0000 (19:11 +0100)
committerTom Hughes <tom@compton.nu>
Thu, 9 Jun 2022 18:21:08 +0000 (19:21 +0100)
Fixes #3563

app/models/old_relation_member.rb
app/models/relation_member.rb
test/models/relation_member_test.rb

index 5bc2e22e00c70bc989acb92c365691aeafcb7f14..c99f60e7c95e97d943f42326bf46c40065d45558 100644 (file)
@@ -25,4 +25,6 @@ class OldRelationMember < ApplicationRecord
   belongs_to :old_relation, :foreign_key => [:relation_id, :version], :inverse_of => :old_members
   # A bit messy, referring to the current tables, should do for the data browser for now
   belongs_to :member, :polymorphic => true
+
+  validates :member_role, :allow_blank => true, :length => { :maximum => 255 }, :characters => true
 end
index 2de551c3f437c2b420574ca84aa58e17f9421bf3..dd47bdcf6d4cbd31a8affa796fc1eabac9166fcc 100644 (file)
@@ -23,4 +23,6 @@ class RelationMember < ApplicationRecord
 
   belongs_to :relation
   belongs_to :member, :polymorphic => true
+
+  validates :member_role, :allow_blank => true, :length => { :maximum => 255 }, :characters => true
 end
index 170b1977c423445aadc8eba97644848b30b9059d..a82cea457f104c211cb974bc0f83bec3ae3afdf4 100644 (file)
@@ -1,4 +1,24 @@
 require "test_helper"
 
 class RelationMemberTest < ActiveSupport::TestCase
+  def test_role_with_invalid_characters
+    invalid = ["\x7f<hr/>", "test@example.com\x0e-", "s/\x1ff", "aa/\ufffe",
+               "aa\x0b-,", "aa?\x08", "/;\uffff.,?", "\x0c#ping",
+               "foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"]
+    relation = create(:relation)
+    node = create(:node)
+    invalid.each do |r|
+      member = build(:relation_member, :relation => relation, :member => node, :member_role => r)
+      assert_not member.valid?, "'#{r}' should not be valid"
+      assert_predicate member.errors[:member_role], :any?
+    end
+  end
+
+  def test_role_too_long
+    relation = create(:relation)
+    node = create(:node)
+    member = build(:relation_member, :relation => relation, :member => node, :member_role => "r" * 256)
+    assert_not member.valid?, "Role should be too long"
+    assert_predicate member.errors[:member_role], :any?
+  end
 end