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.
17 # FIXME Move this test to being an integration test since it spans multiple controllers
19 ## First try this with a non-public user
20 basic_authorization(users(:normal_user).email, "test")
21 changeset_id = changesets(:normal_user_first_change).id
23 # setup a simple XML node
24 xml_doc = current_nodes(:visible_node).to_xml
25 xml_node = xml_doc.find("//osm/node").first
26 nodeid = current_nodes(:visible_node).id
28 # keep a hash of the versions => string, as we'll need something
29 # to test against later
32 # save a version for later checking
33 versions[xml_node['version']] = xml_doc.to_s
35 # randomly move the node about
37 # move the node somewhere else
38 xml_node['lat'] = precision(rand * 180 - 90).to_s
39 xml_node['lon'] = precision(rand * 360 - 180).to_s
40 with_controller(NodeController.new) do
42 put :update, :id => nodeid
43 assert_response :forbidden, "Should have rejected node update"
44 xml_node['version'] = @response.body.to_s
46 # save a version for later checking
47 versions[xml_node['version']] = xml_doc.to_s
50 # add a bunch of random tags
52 xml_tag = XML::Node.new("tag")
53 xml_tag['k'] = random_string
54 xml_tag['v'] = random_string
56 with_controller(NodeController.new) do
58 put :update, :id => nodeid
59 assert_response :forbidden,
60 "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
61 xml_node['version'] = @response.body.to_s
63 # save a version for later checking
64 versions[xml_node['version']] = xml_doc.to_s
67 # probably should check that they didn't get written to the database
70 ## Now do it with the public user
71 basic_authorization(users(:public_user).email, "test")
72 changeset_id = changesets(:public_user_first_change).id
74 # setup a simple XML node
75 xml_doc = current_nodes(:node_with_versions).to_xml
76 xml_node = xml_doc.find("//osm/node").first
77 nodeid = current_nodes(:node_with_versions).id
79 # keep a hash of the versions => string, as we'll need something
80 # to test against later
83 # save a version for later checking
84 versions[xml_node['version']] = xml_doc.to_s
86 # randomly move the node about
88 # move the node somewhere else
89 xml_node['lat'] = precision(rand * 180 - 90).to_s
90 xml_node['lon'] = precision(rand * 360 - 180).to_s
91 with_controller(NodeController.new) do
93 put :update, :id => nodeid
94 assert_response :success
95 xml_node['version'] = @response.body.to_s
97 # save a version for later checking
98 versions[xml_node['version']] = xml_doc.to_s
101 # add a bunch of random tags
103 xml_tag = XML::Node.new("tag")
104 xml_tag['k'] = random_string
105 xml_tag['v'] = random_string
107 with_controller(NodeController.new) do
109 put :update, :id => nodeid
110 assert_response :success,
111 "couldn't update node #{nodeid} (#{@response.body})"
112 xml_node['version'] = @response.body.to_s
114 # save a version for later checking
115 versions[xml_node['version']] = xml_doc.to_s
118 # check all the versions
119 versions.keys.each do |key|
120 get :version, :id => nodeid, :version => key.to_i
122 assert_response :success,
123 "couldn't get version #{key.to_i} of node #{nodeid}"
125 check_node = Node.from_xml(versions[key])
126 api_node = Node.from_xml(@response.body.to_s)
128 assert_nodes_are_equal check_node, api_node
132 def test_not_found_version
133 check_not_found_id_version(70000,312344)
134 check_not_found_id_version(-1, -13)
135 check_not_found_id_version(nodes(:visible_node).id, 24354)
136 check_not_found_id_version(24356, nodes(:visible_node).version)
139 def check_not_found_id_version(id, version)
140 get :version, :id => id, :version => version
141 assert_response :not_found
145 # Test that getting the current version is identical to picking
146 # that version with the version URI call.
147 def test_current_version
148 check_current_version(current_nodes(:visible_node))
149 check_current_version(current_nodes(:used_node_1))
150 check_current_version(current_nodes(:used_node_2))
151 check_current_version(current_nodes(:node_used_by_relationship))
152 check_current_version(current_nodes(:node_with_versions))
155 def check_current_version(node_id)
156 # get the current version of the node
157 current_node = with_controller(NodeController.new) do
158 get :read, :id => node_id
159 assert_response :success, "cant get current node #{node_id}"
160 Node.from_xml(@response.body)
162 assert_not_nil current_node, "getting node #{node_id} returned nil"
164 # get the "old" version of the node from the old_node interface
165 get :version, :id => node_id, :version => current_node.version
166 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
167 old_node = Node.from_xml(@response.body)
169 # check the nodes are the same
170 assert_nodes_are_equal current_node, old_node
174 # returns a 16 character long string with some nasty characters in it.
175 # this ought to stress-test the tag handling as well as the versioning.
177 letters = [['!','"','$','&',';','@'],
180 ('0'..'9').to_a].flatten
181 (1..16).map { |i| letters[ rand(letters.length) ] }.join
185 # truncate a floating point number to the scale that it is stored in
186 # the database. otherwise rounding errors can produce failing unit
187 # tests when they shouldn't.
189 return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE