Convert the node_controller update tests to use factories.

This commit is contained in:
Andy Allan 2017-04-24 20:42:08 +02:00
parent d595577a49
commit e0f7bb1b07

View file

@ -292,140 +292,145 @@ class NodeControllerTest < ActionController::TestCase
## First test with no user credentials ## First test with no user credentials
# try and update a node without authorisation # try and update a node without authorisation
# first try to delete node without auth # first try to delete node without auth
content current_nodes(:visible_node).to_xml private_user = create(:user, :data_public => false)
put :update, :id => current_nodes(:visible_node).id private_node = create(:node, :changeset => create(:changeset, :user => private_user))
user = create(:user)
node = create(:node, :changeset => create(:changeset, :user => user))
content node.to_xml
put :update, :id => node.id
assert_response :unauthorized assert_response :unauthorized
## Second test with the private user ## Second test with the private user
# setup auth # setup auth
basic_authorization(users(:normal_user).email, "test") basic_authorization(private_user.email, "test")
## trying to break changesets ## trying to break changesets
# try and update in someone else's changeset # try and update in someone else's changeset
content update_changeset(current_nodes(:visible_node).to_xml, content update_changeset(private_node.to_xml,
changesets(:public_user_first_change).id) create(:changeset).id)
put :update, :id => current_nodes(:visible_node).id put :update, :id => private_node.id
assert_require_public_data "update with other user's changeset should be forbidden when date isn't public" assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
# try and update in a closed changeset # try and update in a closed changeset
content update_changeset(current_nodes(:visible_node).to_xml, content update_changeset(private_node.to_xml,
changesets(:normal_user_closed_change).id) create(:changeset, :closed, :user => private_user).id)
put :update, :id => current_nodes(:visible_node).id put :update, :id => private_node.id
assert_require_public_data "update with closed changeset should be forbidden, when data isn't public" assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
# try and update in a non-existant changeset # try and update in a non-existant changeset
content update_changeset(current_nodes(:visible_node).to_xml, 0) content update_changeset(private_node.to_xml, 0)
put :update, :id => current_nodes(:visible_node).id put :update, :id => private_node.id
assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public") assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
## try and submit invalid updates ## try and submit invalid updates
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", 91.0) content xml_attr_rewrite(private_node.to_xml, "lat", 91.0)
put :update, :id => current_nodes(:visible_node).id put :update, :id => private_node.id
assert_require_public_data "node at lat=91 should be forbidden, when data isn't public" assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", -91.0) content xml_attr_rewrite(private_node.to_xml, "lat", -91.0)
put :update, :id => current_nodes(:visible_node).id put :update, :id => private_node.id
assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public" assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", 181.0) content xml_attr_rewrite(private_node.to_xml, "lon", 181.0)
put :update, :id => current_nodes(:visible_node).id put :update, :id => private_node.id
assert_require_public_data "node at lon=181 should be forbidden, when data isn't public" assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", -181.0) content xml_attr_rewrite(private_node.to_xml, "lon", -181.0)
put :update, :id => current_nodes(:visible_node).id put :update, :id => private_node.id
assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public" assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
## finally, produce a good request which should work ## finally, produce a good request which still won't work
content current_nodes(:visible_node).to_xml content private_node.to_xml
put :update, :id => current_nodes(:visible_node).id put :update, :id => private_node.id
assert_require_public_data "should have failed with a forbidden when data isn't public" assert_require_public_data "should have failed with a forbidden when data isn't public"
## Finally test with the public user ## Finally test with the public user
# try and update a node without authorisation # try and update a node without authorisation
# first try to delete node without auth # first try to update node without auth
content current_nodes(:visible_node).to_xml content node.to_xml
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :forbidden assert_response :forbidden
# setup auth # setup auth
basic_authorization(users(:public_user).email, "test") basic_authorization(user.email, "test")
## trying to break changesets ## trying to break changesets
# try and update in someone else's changeset # try and update in someone else's changeset
content update_changeset(current_nodes(:visible_node).to_xml, content update_changeset(node.to_xml,
changesets(:normal_user_first_change).id) create(:changeset).id)
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :conflict, "update with other user's changeset should be rejected" assert_response :conflict, "update with other user's changeset should be rejected"
# try and update in a closed changeset # try and update in a closed changeset
content update_changeset(current_nodes(:visible_node).to_xml, content update_changeset(node.to_xml,
changesets(:normal_user_closed_change).id) create(:changeset, :closed, :user => user).id)
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :conflict, "update with closed changeset should be rejected" assert_response :conflict, "update with closed changeset should be rejected"
# try and update in a non-existant changeset # try and update in a non-existant changeset
content update_changeset(current_nodes(:visible_node).to_xml, 0) content update_changeset(node.to_xml, 0)
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :conflict, "update with changeset=0 should be rejected" assert_response :conflict, "update with changeset=0 should be rejected"
## try and submit invalid updates ## try and submit invalid updates
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", 91.0) content xml_attr_rewrite(node.to_xml, "lat", 91.0)
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :bad_request, "node at lat=91 should be rejected" assert_response :bad_request, "node at lat=91 should be rejected"
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", -91.0) content xml_attr_rewrite(node.to_xml, "lat", -91.0)
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :bad_request, "node at lat=-91 should be rejected" assert_response :bad_request, "node at lat=-91 should be rejected"
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", 181.0) content xml_attr_rewrite(node.to_xml, "lon", 181.0)
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :bad_request, "node at lon=181 should be rejected" assert_response :bad_request, "node at lon=181 should be rejected"
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", -181.0) content xml_attr_rewrite(node.to_xml, "lon", -181.0)
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :bad_request, "node at lon=-181 should be rejected" assert_response :bad_request, "node at lon=-181 should be rejected"
## next, attack the versioning ## next, attack the versioning
current_node_version = current_nodes(:visible_node).version current_node_version = node.version
# try and submit a version behind # try and submit a version behind
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, content xml_attr_rewrite(node.to_xml,
"version", current_node_version - 1) "version", current_node_version - 1)
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :conflict, "should have failed on old version number" assert_response :conflict, "should have failed on old version number"
# try and submit a version ahead # try and submit a version ahead
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, content xml_attr_rewrite(node.to_xml,
"version", current_node_version + 1) "version", current_node_version + 1)
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :conflict, "should have failed on skipped version number" assert_response :conflict, "should have failed on skipped version number"
# try and submit total crap in the version field # try and submit total crap in the version field
content xml_attr_rewrite(current_nodes(:visible_node).to_xml, content xml_attr_rewrite(node.to_xml,
"version", "p1r4t3s!") "version", "p1r4t3s!")
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :conflict, assert_response :conflict,
"should not be able to put 'p1r4at3s!' in the version field" "should not be able to put 'p1r4at3s!' in the version field"
## try an update with the wrong ID ## try an update with the wrong ID
content current_nodes(:public_visible_node).to_xml content create(:node).to_xml
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :bad_request, assert_response :bad_request,
"should not be able to update a node with a different ID from the XML" "should not be able to update a node with a different ID from the XML"
## try an update with a minimal valid XML doc which isn't a well-formed OSM doc. ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
content "<update/>" content "<update/>"
put :update, :id => current_nodes(:visible_node).id put :update, :id => node.id
assert_response :bad_request, assert_response :bad_request,
"should not be able to update a node with non-OSM XML doc." "should not be able to update a node with non-OSM XML doc."
## finally, produce a good request which should work ## finally, produce a good request which should work
content current_nodes(:public_visible_node).to_xml content node.to_xml
put :update, :id => current_nodes(:public_visible_node).id put :update, :id => node.id
assert_response :success, "a valid update request failed" assert_response :success, "a valid update request failed"
end end