Skip to content

Commit

Permalink
Move Way.to_xml and to_xml_node out of the model and into tests
Browse files Browse the repository at this point in the history
This code is only used in the tests. Refs openstreetmap#2433
  • Loading branch information
gravitystorm committed Nov 20, 2019
1 parent 6852bc9 commit 2b1bac1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 76 deletions.
38 changes: 0 additions & 38 deletions app/models/way.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,44 +106,6 @@ def self.from_xml_node(pt, create = false)
way
end

# Find a way given it's ID, and in a single SQL call also grab its nodes and tags
def to_xml
doc = OSM::API.new.get_xml_doc
doc.root << to_xml_node
doc
end

def to_xml_node(visible_nodes = nil, changeset_cache = {}, user_display_name_cache = {})
el = XML::Node.new "way"
el["id"] = id.to_s

add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)

# make sure nodes are output in sequence_id order
ordered_nodes = []
way_nodes.each do |nd|
if visible_nodes
# if there is a list of visible nodes then use that to weed out deleted nodes
ordered_nodes[nd.sequence_id] = nd.node_id.to_s if visible_nodes[nd.node_id]
else
# otherwise, manually go to the db to check things
ordered_nodes[nd.sequence_id] = nd.node_id.to_s if nd.node&.visible?
end
end

ordered_nodes.each do |nd_id|
next unless nd_id && nd_id != "0"

node_el = XML::Node.new "nd"
node_el["ref"] = nd_id
el << node_el
end

add_tags_to_xml_node(el, way_tags)

el
end

def nds
@nds ||= way_nodes.collect(&:node_id)
end
Expand Down
10 changes: 5 additions & 5 deletions test/controllers/api/changesets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def test_upload_delete
diff.root << delete
delete << super_relation.to_xml_node
delete << used_relation.to_xml_node
delete << used_way.to_xml_node
delete << xml_node_for_way(used_way)
delete << xml_node_for_node(used_node)

# update the changeset to one that this user owns
Expand Down Expand Up @@ -591,7 +591,7 @@ def test_upload_delete_invalid
delete = XML::Node.new "delete"
diff.root << delete
delete << other_relation.to_xml_node
delete << used_way.to_xml_node
delete << xml_node_for_way(used_way)
delete << xml_node_for_node(used_node)

# update the changeset to one that this user owns
Expand Down Expand Up @@ -634,7 +634,7 @@ def test_upload_delete_if_unused
diff.root << delete
delete["if-unused"] = ""
delete << used_relation.to_xml_node
delete << used_way.to_xml_node
delete << xml_node_for_way(used_way)
delete << xml_node_for_node(used_node)

# update the changeset to one that this user owns
Expand Down Expand Up @@ -1175,7 +1175,7 @@ def test_upload_way_extend
diff = XML::Document.new
diff.root = XML::Node.new "osmChange"
modify = XML::Node.new "modify"
xml_old_way = old_way.to_xml_node
xml_old_way = xml_node_for_way(old_way)
nd_ref = XML::Node.new "nd"
nd_ref["ref"] = create(:node, :lat => 3, :lon => 3).id.to_s
xml_old_way << nd_ref
Expand Down Expand Up @@ -1487,7 +1487,7 @@ def test_changeset_bbox

# add (delete) a way to it, which contains a point at (3,3)
with_controller(WaysController.new) do
xml = update_changeset(way.to_xml, changeset_id)
xml = update_changeset(xml_for_way(way), changeset_id)
put :delete, :params => { :id => way.id }, :body => xml.to_s
assert_response :success, "Couldn't delete a way."
end
Expand Down
66 changes: 33 additions & 33 deletions test/controllers/api/ways_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,17 @@ def test_delete
assert_response :forbidden

# try to delete with an invalid (closed) changeset
xml = update_changeset(private_way.to_xml, private_closed_changeset.id)
xml = update_changeset(xml_for_way(private_way), private_closed_changeset.id)
delete :delete, :params => { :id => private_way.id }, :body => xml.to_s
assert_response :forbidden

# try to delete with an invalid (non-existent) changeset
xml = update_changeset(private_way.to_xml, 0)
xml = update_changeset(xml_for_way(private_way), 0)
delete :delete, :params => { :id => private_way.id }, :body => xml.to_s
assert_response :forbidden

# Now try with a valid changeset
xml = private_way.to_xml
xml = xml_for_way(private_way)
delete :delete, :params => { :id => private_way.id }, :body => xml.to_s
assert_response :forbidden

Expand All @@ -311,12 +311,12 @@ def test_delete
# "delete request should return a new version number for way"

# this won't work since the way is already deleted
xml = private_deleted_way.to_xml
xml = xml_for_way(private_deleted_way)
delete :delete, :params => { :id => private_deleted_way.id }, :body => xml.to_s
assert_response :forbidden

# this shouldn't work as the way is used in a relation
xml = private_used_way.to_xml
xml = xml_for_way(private_used_way)
delete :delete, :params => { :id => private_used_way.id }, :body => xml.to_s
assert_response :forbidden,
"shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
Expand All @@ -339,17 +339,17 @@ def test_delete
assert_response :bad_request

# try to delete with an invalid (closed) changeset
xml = update_changeset(way.to_xml, closed_changeset.id)
xml = update_changeset(xml_for_way(way), closed_changeset.id)
delete :delete, :params => { :id => way.id }, :body => xml.to_s
assert_response :conflict

# try to delete with an invalid (non-existent) changeset
xml = update_changeset(way.to_xml, 0)
xml = update_changeset(xml_for_way(way), 0)
delete :delete, :params => { :id => way.id }, :body => xml.to_s
assert_response :conflict

# Now try with a valid changeset
xml = way.to_xml
xml = xml_for_way(way)
delete :delete, :params => { :id => way.id }, :body => xml.to_s
assert_response :success

Expand All @@ -360,12 +360,12 @@ def test_delete
"delete request should return a new version number for way"

# this won't work since the way is already deleted
xml = deleted_way.to_xml
xml = xml_for_way(deleted_way)
delete :delete, :params => { :id => deleted_way.id }, :body => xml.to_s
assert_response :gone

# this shouldn't work as the way is used in a relation
xml = used_way.to_xml
xml = xml_for_way(used_way)
delete :delete, :params => { :id => used_way.id }, :body => xml.to_s
assert_response :precondition_failed,
"shouldn't be able to delete a way used in a relation (#{@response.body})"
Expand All @@ -390,7 +390,7 @@ def test_update

## First test with no user credentials
# try and update a way without authorisation
xml = way.to_xml
xml = xml_for_way(way)
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :unauthorized

Expand All @@ -402,33 +402,33 @@ def test_update
## trying to break changesets

# try and update in someone else's changeset
xml = update_changeset(private_way.to_xml,
xml = update_changeset(xml_for_way(private_way),
create(:changeset).id)
put :update, :params => { :id => private_way.id }, :body => xml.to_s
assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"

# try and update in a closed changeset
xml = update_changeset(private_way.to_xml,
xml = update_changeset(xml_for_way(private_way),
create(:changeset, :closed, :user => private_user).id)
put :update, :params => { :id => private_way.id }, :body => xml.to_s
assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"

# try and update in a non-existant changeset
xml = update_changeset(private_way.to_xml, 0)
xml = update_changeset(xml_for_way(private_way), 0)
put :update, :params => { :id => private_way.id }, :body => xml.to_s
assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")

## try and submit invalid updates
xml = xml_replace_node(private_way.to_xml, node.id, 9999)
xml = xml_replace_node(xml_for_way(private_way), node.id, 9999)
put :update, :params => { :id => private_way.id }, :body => xml.to_s
assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"

xml = xml_replace_node(private_way.to_xml, node.id, create(:node, :deleted).id)
xml = xml_replace_node(xml_for_way(private_way), node.id, create(:node, :deleted).id)
put :update, :params => { :id => private_way.id }, :body => xml.to_s
assert_require_public_data "way with deleted node should be forbidden, when data isn't public"

## finally, produce a good request which will still not work
xml = private_way.to_xml
xml = xml_for_way(private_way)
put :update, :params => { :id => private_way.id }, :body => xml.to_s
assert_require_public_data "should have failed with a forbidden when data isn't public"

Expand All @@ -440,55 +440,55 @@ def test_update
## trying to break changesets

# try and update in someone else's changeset
xml = update_changeset(way.to_xml,
xml = update_changeset(xml_for_way(way),
create(:changeset).id)
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :conflict, "update with other user's changeset should be rejected"

# try and update in a closed changeset
xml = update_changeset(way.to_xml,
xml = update_changeset(xml_for_way(way),
create(:changeset, :closed, :user => user).id)
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :conflict, "update with closed changeset should be rejected"

# try and update in a non-existant changeset
xml = update_changeset(way.to_xml, 0)
xml = update_changeset(xml_for_way(way), 0)
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :conflict, "update with changeset=0 should be rejected"

## try and submit invalid updates
xml = xml_replace_node(way.to_xml, node.id, 9999)
xml = xml_replace_node(xml_for_way(way), node.id, 9999)
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :precondition_failed, "way with non-existent node should be rejected"

xml = xml_replace_node(way.to_xml, node.id, create(:node, :deleted).id)
xml = xml_replace_node(xml_for_way(way), node.id, create(:node, :deleted).id)
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :precondition_failed, "way with deleted node should be rejected"

## next, attack the versioning
current_way_version = way.version

# try and submit a version behind
xml = xml_attr_rewrite(way.to_xml,
xml = xml_attr_rewrite(xml_for_way(way),
"version", current_way_version - 1)
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :conflict, "should have failed on old version number"

# try and submit a version ahead
xml = xml_attr_rewrite(way.to_xml,
xml = xml_attr_rewrite(xml_for_way(way),
"version", current_way_version + 1)
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :conflict, "should have failed on skipped version number"

# try and submit total crap in the version field
xml = xml_attr_rewrite(way.to_xml,
xml = xml_attr_rewrite(xml_for_way(way),
"version", "p1r4t3s!")
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :conflict,
"should not be able to put 'p1r4at3s!' in the version field"

## try an update with the wrong ID
xml = create(:way).to_xml
xml = xml_for_way(create(:way))
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :bad_request,
"should not be able to update a way with a different ID from the XML"
Expand All @@ -500,7 +500,7 @@ def test_update
"should not be able to update a way with non-OSM XML doc."

## finally, produce a good request which should work
xml = way.to_xml
xml = xml_for_way(way)
put :update, :params => { :id => way.id }, :body => xml.to_s
assert_response :success, "a valid update request failed"
end
Expand All @@ -527,7 +527,7 @@ def test_add_tags
tag_xml["v"] = "yes"

# add the tag into the existing xml
way_xml = private_way.to_xml
way_xml = xml_for_way(private_way)
way_xml.find("//osm/way").first << tag_xml

# try and upload it
Expand All @@ -545,7 +545,7 @@ def test_add_tags
tag_xml["v"] = "yes"

# add the tag into the existing xml
way_xml = way.to_xml
way_xml = xml_for_way(way)
way_xml.find("//osm/way").first << tag_xml

# try and upload it
Expand Down Expand Up @@ -575,7 +575,7 @@ def test_add_duplicate_tags
tag_xml["v"] = private_existing_tag.v

# add the tag into the existing xml
way_xml = private_way.to_xml
way_xml = xml_for_way(private_way)
way_xml.find("//osm/way").first << tag_xml

# try and upload it
Expand All @@ -593,7 +593,7 @@ def test_add_duplicate_tags
tag_xml["v"] = existing_tag.v

# add the tag into the existing xml
way_xml = way.to_xml
way_xml = xml_for_way(way)
way_xml.find("//osm/way").first << tag_xml

# try and upload it
Expand Down Expand Up @@ -621,7 +621,7 @@ def test_new_duplicate_tags
tag_xml["v"] = "foobar"

# add the tag into the existing xml
way_xml = private_way.to_xml
way_xml = xml_for_way(private_way)

# add two copies of the tag
way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
Expand All @@ -641,7 +641,7 @@ def test_new_duplicate_tags
tag_xml["v"] = "foobar"

# add the tag into the existing xml
way_xml = way.to_xml
way_xml = xml_for_way(way)

# add two copies of the tag
way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
Expand Down
31 changes: 31 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,37 @@ def xml_node_for_node(node)
el
end

def xml_for_way(way)
doc = OSM::API.new.get_xml_doc
doc.root << xml_node_for_way(way)
doc
end

def xml_node_for_way(way)
el = XML::Node.new "way"
el["id"] = way.id.to_s

OMHelper.add_metadata_to_xml_node(el, way, {}, {})

# make sure nodes are output in sequence_id order
ordered_nodes = []
way.way_nodes.each do |nd|
ordered_nodes[nd.sequence_id] = nd.node_id.to_s if nd.node&.visible?
end

ordered_nodes.each do |nd_id|
next unless nd_id && nd_id != "0"

node_el = XML::Node.new "nd"
node_el["ref"] = nd_id
el << node_el
end

OMHelper.add_tags_to_xml_node(el, way.way_tags)

el
end

class OMHelper
extend ObjectMetadata
end
Expand Down

0 comments on commit 2b1bac1

Please sign in to comment.