1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_node_controller'
4 class OldNodeControllerTest < ActionController::TestCase
12 # test the version call by submitting several revisions of a new node
13 # to the API and ensuring that later calls to version return the
14 # matching versions of the object.
16 basic_authorization(users(:normal_user).email, "test")
17 changeset_id = changesets(:normal_user_first_change).id
19 # setup a simple XML node
20 xml_doc = current_nodes(:visible_node).to_xml
21 xml_node = xml_doc.find("//osm/node").first
22 nodeid = current_nodes(:visible_node).id
24 # keep a hash of the versions => string, as we'll need something
25 # to test against later
28 # save a version for later checking
29 versions[xml_node['version']] = xml_doc.to_s
31 # randomly move the node about
33 # move the node somewhere else
34 xml_node['lat'] = precision(rand * 180 - 90).to_s
35 xml_node['lon'] = precision(rand * 360 - 180).to_s
36 with_controller(NodeController.new) do
38 put :update, :id => nodeid
39 assert_response :success
40 xml_node['version'] = @response.body.to_s
42 # save a version for later checking
43 versions[xml_node['version']] = xml_doc.to_s
46 # add a bunch of random tags
48 xml_tag = XML::Node.new("tag")
49 xml_tag['k'] = random_string
50 xml_tag['v'] = random_string
52 with_controller(NodeController.new) do
54 put :update, :id => nodeid
55 assert_response :success,
56 "couldn't update node #{nodeid} (#{@response.body})"
57 xml_node['version'] = @response.body.to_s
59 # save a version for later checking
60 versions[xml_node['version']] = xml_doc.to_s
63 # check all the versions
64 versions.keys.each do |key|
65 get :version, :id => nodeid, :version => key.to_i
67 assert_response :success,
68 "couldn't get version #{key.to_i} of node #{nodeid}"
70 check_node = Node.from_xml(versions[key])
71 api_node = Node.from_xml(@response.body.to_s)
73 assert_nodes_are_equal check_node, api_node
78 # Test that getting the current version is identical to picking
79 # that version with the version URI call.
80 def test_current_version
81 check_current_version(current_nodes(:visible_node))
82 check_current_version(current_nodes(:used_node_1))
83 check_current_version(current_nodes(:used_node_2))
84 check_current_version(current_nodes(:node_used_by_relationship))
85 check_current_version(current_nodes(:node_with_versions))
88 def check_current_version(node_id)
89 # get the current version of the node
90 current_node = with_controller(NodeController.new) do
91 get :read, :id => node_id
92 assert_response :success, "cant get current node #{node_id}"
93 Node.from_xml(@response.body)
95 assert_not_nil current_node, "getting node #{node_id} returned nil"
97 # get the "old" version of the node from the old_node interface
98 get :version, :id => node_id, :version => current_node.version
99 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
100 old_node = Node.from_xml(@response.body)
102 # check the nodes are the same
103 assert_nodes_are_equal current_node, old_node
107 # returns a 16 character long string with some nasty characters in it.
108 # this ought to stress-test the tag handling as well as the versioning.
110 letters = [['!','"','$','&',';','@'],
113 ('0'..'9').to_a].flatten
114 (1..16).map { |i| letters[ rand(letters.length) ] }.join
118 # truncate a floating point number to the scale that it is stored in
119 # the database. otherwise rounding errors can produce failing unit
120 # tests when they shouldn't.
122 return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
125 def basic_authorization(user, pass)
126 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
130 @request.env["RAW_POST_DATA"] = c.to_s