+ ##
+ # tests whether the API works and prevents incorrect use while trying
+ # to update ways.
+ def test_update
+ private_user = create(:user, :data_public => false)
+ private_way = create(:way, :changeset => create(:changeset, :user => private_user))
+ user = create(:user)
+ way = create(:way, :changeset => create(:changeset, :user => user))
+ node = create(:node)
+ create(:way_node, :way => private_way, :node => node)
+ create(:way_node, :way => way, :node => node)
+
+ ## First test with no user credentials
+ # try and update a way without authorisation
+ content way.to_xml
+ put :update, :params => { :id => way.id }
+ assert_response :unauthorized
+
+ ## Second test with the private user
+
+ # setup auth
+ basic_authorization private_user.email, "test"
+
+ ## trying to break changesets
+
+ # try and update in someone else's changeset
+ content update_changeset(private_way.to_xml,
+ create(:changeset).id)
+ put :update, :params => { :id => private_way.id }
+ 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
+ content update_changeset(private_way.to_xml,
+ create(:changeset, :closed, :user => private_user).id)
+ put :update, :params => { :id => private_way.id }
+ assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
+
+ # try and update in a non-existant changeset
+ content update_changeset(private_way.to_xml, 0)
+ put :update, :params => { :id => private_way.id }
+ assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
+
+ ## try and submit invalid updates
+ content xml_replace_node(private_way.to_xml, node.id, 9999)
+ put :update, :params => { :id => private_way.id }
+ assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"
+
+ content xml_replace_node(private_way.to_xml, node.id, create(:node, :deleted).id)
+ put :update, :params => { :id => private_way.id }
+ 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
+ content private_way.to_xml
+ put :update, :params => { :id => private_way.id }
+ assert_require_public_data "should have failed with a forbidden when data isn't public"
+
+ ## Finally test with the public user
+
+ # setup auth
+ basic_authorization user.email, "test"
+
+ ## trying to break changesets
+
+ # try and update in someone else's changeset
+ content update_changeset(way.to_xml,
+ create(:changeset).id)
+ put :update, :params => { :id => way.id }
+ assert_response :conflict, "update with other user's changeset should be rejected"
+
+ # try and update in a closed changeset
+ content update_changeset(way.to_xml,
+ create(:changeset, :closed, :user => user).id)
+ put :update, :params => { :id => way.id }
+ assert_response :conflict, "update with closed changeset should be rejected"
+
+ # try and update in a non-existant changeset
+ content update_changeset(way.to_xml, 0)
+ put :update, :params => { :id => way.id }
+ assert_response :conflict, "update with changeset=0 should be rejected"
+
+ ## try and submit invalid updates
+ content xml_replace_node(way.to_xml, node.id, 9999)
+ put :update, :params => { :id => way.id }
+ assert_response :precondition_failed, "way with non-existent node should be rejected"
+
+ content xml_replace_node(way.to_xml, node.id, create(:node, :deleted).id)
+ put :update, :params => { :id => way.id }
+ 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
+ content xml_attr_rewrite(way.to_xml,
+ "version", current_way_version - 1)
+ put :update, :params => { :id => way.id }
+ assert_response :conflict, "should have failed on old version number"
+
+ # try and submit a version ahead
+ content xml_attr_rewrite(way.to_xml,
+ "version", current_way_version + 1)
+ put :update, :params => { :id => way.id }
+ assert_response :conflict, "should have failed on skipped version number"
+
+ # try and submit total crap in the version field
+ content xml_attr_rewrite(way.to_xml,
+ "version", "p1r4t3s!")
+ put :update, :params => { :id => way.id }
+ assert_response :conflict,
+ "should not be able to put 'p1r4at3s!' in the version field"
+
+ ## try an update with the wrong ID
+ content create(:way).to_xml
+ put :update, :params => { :id => way.id }
+ assert_response :bad_request,
+ "should not be able to update a way with a different ID from the XML"
+
+ ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
+ content "<update/>"
+ put :update, :params => { :id => way.id }
+ assert_response :bad_request,
+ "should not be able to update a way with non-OSM XML doc."
+
+ ## finally, produce a good request which should work
+ content way.to_xml
+ put :update, :params => { :id => way.id }
+ assert_response :success, "a valid update request failed"
+ end
+