- ##
- # test the version call by submitting several revisions of a new node
- # to the API and ensuring that later calls to version return the
- # matching versions of the object.
- #
- ##
- # FIXME: Move this test to being an integration test since it spans multiple controllers
- def test_version
- private_user = create(:user, :data_public => false)
- private_node = create(:node, :with_history, :version => 4, :changeset => create(:changeset, :user => private_user))
- user = create(:user)
- node = create(:node, :with_history, :version => 4, :changeset => create(:changeset, :user => user))
- create_list(:node_tag, 2, :node => node)
- # Ensure that the current tags are propagated to the history too
- propagate_tags(node, node.old_nodes.last)
-
- ## First try this with a non-public user
- basic_authorization private_user.email, "test"
-
- # setup a simple XML node
- xml_doc = private_node.to_xml
- xml_node = xml_doc.find("//osm/node").first
- nodeid = private_node.id
-
- # keep a hash of the versions => string, as we'll need something
- # to test against later
- versions = {}
-
- # save a version for later checking
- versions[xml_node["version"]] = xml_doc.to_s
-
- # randomly move the node about
- 3.times do
- # move the node somewhere else
- xml_node["lat"] = precision(rand * 180 - 90).to_s
- xml_node["lon"] = precision(rand * 360 - 180).to_s
- with_controller(NodesController.new) do
- put :update, :params => { :id => nodeid }, :body => xml_doc.to_s
- assert_response :forbidden, "Should have rejected node update"
- xml_node["version"] = @response.body.to_s
- end
- # save a version for later checking
- versions[xml_node["version"]] = xml_doc.to_s
- end
-
- # add a bunch of random tags
- 3.times do
- xml_tag = XML::Node.new("tag")
- xml_tag["k"] = random_string
- xml_tag["v"] = random_string
- xml_node << xml_tag
- with_controller(NodesController.new) do
- put :update, :params => { :id => nodeid }, :body => xml_doc.to_s
- assert_response :forbidden,
- "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
- xml_node["version"] = @response.body.to_s
- end
- # save a version for later checking
- versions[xml_node["version"]] = xml_doc.to_s
- end
-
- # probably should check that they didn't get written to the database
-
- ## Now do it with the public user
- basic_authorization user.email, "test"
-
- # setup a simple XML node
-
- xml_doc = node.to_xml
- xml_node = xml_doc.find("//osm/node").first
- nodeid = node.id
-
- # keep a hash of the versions => string, as we'll need something
- # to test against later
- versions = {}
-
- # save a version for later checking
- versions[xml_node["version"]] = xml_doc.to_s
-
- # randomly move the node about
- 3.times do
- # move the node somewhere else
- xml_node["lat"] = precision(rand * 180 - 90).to_s
- xml_node["lon"] = precision(rand * 360 - 180).to_s
- with_controller(NodesController.new) do
- put :update, :params => { :id => nodeid }, :body => xml_doc.to_s
- assert_response :success
- xml_node["version"] = @response.body.to_s
- end
- # save a version for later checking
- versions[xml_node["version"]] = xml_doc.to_s
- end
-
- # add a bunch of random tags
- 3.times do
- xml_tag = XML::Node.new("tag")
- xml_tag["k"] = random_string
- xml_tag["v"] = random_string
- xml_node << xml_tag
- with_controller(NodesController.new) do
- put :update, :params => { :id => nodeid }, :body => xml_doc.to_s
- assert_response :success,
- "couldn't update node #{nodeid} (#{@response.body})"
- xml_node["version"] = @response.body.to_s
- end
- # save a version for later checking
- versions[xml_node["version"]] = xml_doc.to_s
- end
-
- # check all the versions
- versions.each_key do |key|
- get :version, :params => { :id => nodeid, :version => key.to_i }
-
- assert_response :success,
- "couldn't get version #{key.to_i} of node #{nodeid}"
-
- check_node = Node.from_xml(versions[key])
- api_node = Node.from_xml(@response.body.to_s)
-
- assert_nodes_are_equal check_node, api_node
- end
- end
-
- def test_not_found_version
- check_not_found_id_version(70000, 312344)
- check_not_found_id_version(-1, -13)
- check_not_found_id_version(create(:node).id, 24354)
- check_not_found_id_version(24356, create(:node).version)
- end
-
- def check_not_found_id_version(id, version)
- get :version, :params => { :id => id, :version => version }
- assert_response :not_found
- rescue ActionController::UrlGenerationError => ex
- assert_match(/No route matches/, ex.to_s)
- end
-
- ##
- # Test that getting the current version is identical to picking
- # that version with the version URI call.
- def test_current_version