1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_node_controller'
4 # Re-raise errors caught by the controller.
5 class OldNodeController; def rescue_action(e) raise e end; end
7 class OldNodeControllerTest < Test::Unit::TestCase
11 @controller = OldNodeController.new
12 @request = ActionController::TestRequest.new
13 @response = ActionController::TestResponse.new
21 # test the version call by submitting several revisions of a new node
22 # to the API and ensuring that later calls to version return the
23 # matching versions of the object.
25 basic_authorization(users(:normal_user).email, "test")
26 changeset_id = changesets(:normal_user_first_change).id
28 # setup a simple XML node
29 xml_doc = current_nodes(:visible_node).to_xml
30 xml_node = xml_doc.find("//osm/node").first
31 nodeid = current_nodes(:visible_node).id
33 # keep a hash of the versions => string, as we'll need something
34 # to test against later
37 # save a version for later checking
38 versions[xml_node['version']] = xml_doc.to_s
40 # randomly move the node about
42 # move the node somewhere else
43 xml_node['lat'] = precision(rand * 180 - 90).to_s
44 xml_node['lon'] = precision(rand * 360 - 180).to_s
45 with_controller(NodeController.new) do
47 put :update, :id => nodeid
48 assert_response :success
49 xml_node['version'] = @response.body.to_s
51 # save a version for later checking
52 versions[xml_node['version']] = xml_doc.to_s
55 # add a bunch of random tags
57 xml_tag = XML::Node.new("tag")
58 xml_tag['k'] = random_string
59 xml_tag['v'] = random_string
61 with_controller(NodeController.new) do
63 put :update, :id => nodeid
64 assert_response :success,
65 "couldn't update node #{nodeid} (#{@response.body})"
66 xml_node['version'] = @response.body.to_s
68 # save a version for later checking
69 versions[xml_node['version']] = xml_doc.to_s
72 # check all the versions
73 versions.keys.each do |key|
74 get :version, :id => nodeid, :version => key.to_i
76 assert_response :success,
77 "couldn't get version #{key.to_i} of node #{nodeid}"
79 check_node = Node.from_xml(versions[key])
80 api_node = Node.from_xml(@response.body.to_s)
82 assert_nodes_are_equal check_node, api_node
87 # Test that getting the current version is identical to picking
88 # that version with the version URI call.
89 def test_current_version
90 check_current_version(current_nodes(:visible_node))
91 check_current_version(current_nodes(:used_node_1))
92 check_current_version(current_nodes(:used_node_2))
93 check_current_version(current_nodes(:node_used_by_relationship))
94 check_current_version(current_nodes(:node_with_versions))
97 def check_current_version(node_id)
98 # get the current version of the node
99 current_node = with_controller(NodeController.new) do
100 get :read, :id => node_id
101 assert_response :success, "cant get current node #{node_id}"
102 Node.from_xml(@response.body)
104 assert_not_nil current_node, "getting node #{node_id} returned nil"
106 # get the "old" version of the node from the old_node interface
107 get :version, :id => node_id, :version => current_node.version
108 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
109 old_node = Node.from_xml(@response.body)
111 # check the nodes are the same
112 assert_nodes_are_equal current_node, old_node
116 # for some reason a==b is false, but there doesn't seem to be any
117 # difference between the nodes, so i'm checking all the attributes
118 # manually and blaming it on ActiveRecord
119 def assert_nodes_are_equal(a, b)
120 assert_equal a.id, b.id, "node IDs"
121 assert_equal a.latitude, b.latitude, "latitude on node #{a.id}"
122 assert_equal a.longitude, b.longitude, "longitude on node #{a.id}"
123 assert_equal a.changeset_id, b.changeset_id, "changeset ID on node #{a.id}"
124 assert_equal a.visible, b.visible, "visible on node #{a.id}"
125 assert_equal a.version, b.version, "version on node #{a.id}"
126 assert_equal a.tags, b.tags, "tags on node #{a.id}"
130 # returns a 16 character long string with some nasty characters in it.
131 # this ought to stress-test the tag handling as well as the versioning.
133 letters = [['!','"','$','&',';','@'],
136 ('0'..'9').to_a].flatten
137 (1..16).map { |i| letters[ rand(letters.length) ] }.join
141 # truncate a floating point number to the scale that it is stored in
142 # the database. otherwise rounding errors can produce failing unit
143 # tests when they shouldn't.
145 return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
148 def basic_authorization(user, pass)
149 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
153 @request.env["RAW_POST_DATA"] = c.to_s
157 # takes a block which is executed in the context of a different
158 # ActionController instance. this is used so that code can call methods
159 # on the node controller whilst testing the old_node controller.
160 def with_controller(new_controller)
161 controller_save = @controller
163 @controller = new_controller
166 @controller = controller_save