1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_node_controller'
4 class OldNodeControllerTest < ActionController::TestCase
12 # test all routes which lead to this controller
15 { :path => "/api/0.6/node/1/history", :method => :get },
16 { :controller => "old_node", :action => "history", :id => "1" }
19 { :path => "/api/0.6/node/1/2", :method => :get },
20 { :controller => "old_node", :action => "version", :id => "1", :version => "2" }
25 # test the version call by submitting several revisions of a new node
26 # to the API and ensuring that later calls to version return the
27 # matching versions of the object.
30 # FIXME Move this test to being an integration test since it spans multiple controllers
32 ## First try this with a non-public user
33 basic_authorization(users(:normal_user).email, "test")
34 changeset_id = changesets(:normal_user_first_change).id
36 # setup a simple XML node
37 xml_doc = current_nodes(:visible_node).to_xml
38 xml_node = xml_doc.find("//osm/node").first
39 nodeid = current_nodes(:visible_node).id
41 # keep a hash of the versions => string, as we'll need something
42 # to test against later
45 # save a version for later checking
46 versions[xml_node['version']] = xml_doc.to_s
48 # randomly move the node about
50 # move the node somewhere else
51 xml_node['lat'] = precision(rand * 180 - 90).to_s
52 xml_node['lon'] = precision(rand * 360 - 180).to_s
53 with_controller(NodeController.new) do
55 put :update, :id => nodeid
56 assert_response :forbidden, "Should have rejected node update"
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 # add a bunch of random tags
65 xml_tag = XML::Node.new("tag")
66 xml_tag['k'] = random_string
67 xml_tag['v'] = random_string
69 with_controller(NodeController.new) do
71 put :update, :id => nodeid
72 assert_response :forbidden,
73 "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
74 xml_node['version'] = @response.body.to_s
76 # save a version for later checking
77 versions[xml_node['version']] = xml_doc.to_s
80 # probably should check that they didn't get written to the database
83 ## Now do it with the public user
84 basic_authorization(users(:public_user).email, "test")
85 changeset_id = changesets(:public_user_first_change).id
87 # setup a simple XML node
88 xml_doc = current_nodes(:node_with_versions).to_xml
89 xml_node = xml_doc.find("//osm/node").first
90 nodeid = current_nodes(:node_with_versions).id
92 # keep a hash of the versions => string, as we'll need something
93 # to test against later
96 # save a version for later checking
97 versions[xml_node['version']] = xml_doc.to_s
99 # randomly move the node about
101 # move the node somewhere else
102 xml_node['lat'] = precision(rand * 180 - 90).to_s
103 xml_node['lon'] = precision(rand * 360 - 180).to_s
104 with_controller(NodeController.new) do
106 put :update, :id => nodeid
107 assert_response :success
108 xml_node['version'] = @response.body.to_s
110 # save a version for later checking
111 versions[xml_node['version']] = xml_doc.to_s
114 # add a bunch of random tags
116 xml_tag = XML::Node.new("tag")
117 xml_tag['k'] = random_string
118 xml_tag['v'] = random_string
120 with_controller(NodeController.new) do
122 put :update, :id => nodeid
123 assert_response :success,
124 "couldn't update node #{nodeid} (#{@response.body})"
125 xml_node['version'] = @response.body.to_s
127 # save a version for later checking
128 versions[xml_node['version']] = xml_doc.to_s
131 # check all the versions
132 versions.keys.each do |key|
133 get :version, :id => nodeid, :version => key.to_i
135 assert_response :success,
136 "couldn't get version #{key.to_i} of node #{nodeid}"
138 check_node = Node.from_xml(versions[key])
139 api_node = Node.from_xml(@response.body.to_s)
141 assert_nodes_are_equal check_node, api_node
145 def test_not_found_version
146 check_not_found_id_version(70000,312344)
147 check_not_found_id_version(-1, -13)
148 check_not_found_id_version(nodes(:visible_node).id, 24354)
149 check_not_found_id_version(24356, nodes(:visible_node).version)
152 def check_not_found_id_version(id, version)
153 get :version, :id => id, :version => version
154 assert_response :not_found
158 # Test that getting the current version is identical to picking
159 # that version with the version URI call.
160 def test_current_version
161 check_current_version(current_nodes(:visible_node))
162 check_current_version(current_nodes(:used_node_1))
163 check_current_version(current_nodes(:used_node_2))
164 check_current_version(current_nodes(:node_used_by_relationship))
165 check_current_version(current_nodes(:node_with_versions))
168 def check_current_version(node_id)
169 # get the current version of the node
170 current_node = with_controller(NodeController.new) do
171 get :read, :id => node_id
172 assert_response :success, "cant get current node #{node_id}"
173 Node.from_xml(@response.body)
175 assert_not_nil current_node, "getting node #{node_id} returned nil"
177 # get the "old" version of the node from the old_node interface
178 get :version, :id => node_id, :version => current_node.version
179 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
180 old_node = Node.from_xml(@response.body)
182 # check the nodes are the same
183 assert_nodes_are_equal current_node, old_node
187 # returns a 16 character long string with some nasty characters in it.
188 # this ought to stress-test the tag handling as well as the versioning.
190 letters = [['!','"','$','&',';','@'],
193 ('0'..'9').to_a].flatten
194 (1..16).map { |i| letters[ rand(letters.length) ] }.join
198 # truncate a floating point number to the scale that it is stored in
199 # the database. otherwise rounding errors can produce failing unit
200 # tests when they shouldn't.
202 return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE