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" }
23 { :path => "/api/0.6/node/1/2/redact", :method => :post },
24 { :controller => "old_node", :action => "redact", :id => "1", :version => "2" }
29 # test the version call by submitting several revisions of a new node
30 # to the API and ensuring that later calls to version return the
31 # matching versions of the object.
34 # FIXME Move this test to being an integration test since it spans multiple controllers
36 ## First try this with a non-public user
37 basic_authorization(users(:normal_user).email, "test")
38 changeset_id = changesets(:normal_user_first_change).id
40 # setup a simple XML node
41 xml_doc = current_nodes(:visible_node).to_xml
42 xml_node = xml_doc.find("//osm/node").first
43 nodeid = current_nodes(:visible_node).id
45 # keep a hash of the versions => string, as we'll need something
46 # to test against later
49 # save a version for later checking
50 versions[xml_node['version']] = xml_doc.to_s
52 # randomly move the node about
54 # move the node somewhere else
55 xml_node['lat'] = precision(rand * 180 - 90).to_s
56 xml_node['lon'] = precision(rand * 360 - 180).to_s
57 with_controller(NodeController.new) do
59 put :update, :id => nodeid
60 assert_response :forbidden, "Should have rejected node update"
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 # add a bunch of random tags
69 xml_tag = XML::Node.new("tag")
70 xml_tag['k'] = random_string
71 xml_tag['v'] = random_string
73 with_controller(NodeController.new) do
75 put :update, :id => nodeid
76 assert_response :forbidden,
77 "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
78 xml_node['version'] = @response.body.to_s
80 # save a version for later checking
81 versions[xml_node['version']] = xml_doc.to_s
84 # probably should check that they didn't get written to the database
87 ## Now do it with the public user
88 basic_authorization(users(:public_user).email, "test")
89 changeset_id = changesets(:public_user_first_change).id
91 # setup a simple XML node
92 xml_doc = current_nodes(:node_with_versions).to_xml
93 xml_node = xml_doc.find("//osm/node").first
94 nodeid = current_nodes(:node_with_versions).id
96 # keep a hash of the versions => string, as we'll need something
97 # to test against later
100 # save a version for later checking
101 versions[xml_node['version']] = xml_doc.to_s
103 # randomly move the node about
105 # move the node somewhere else
106 xml_node['lat'] = precision(rand * 180 - 90).to_s
107 xml_node['lon'] = precision(rand * 360 - 180).to_s
108 with_controller(NodeController.new) do
110 put :update, :id => nodeid
111 assert_response :success
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 # add a bunch of random tags
120 xml_tag = XML::Node.new("tag")
121 xml_tag['k'] = random_string
122 xml_tag['v'] = random_string
124 with_controller(NodeController.new) do
126 put :update, :id => nodeid
127 assert_response :success,
128 "couldn't update node #{nodeid} (#{@response.body})"
129 xml_node['version'] = @response.body.to_s
131 # save a version for later checking
132 versions[xml_node['version']] = xml_doc.to_s
135 # check all the versions
136 versions.keys.each do |key|
137 get :version, :id => nodeid, :version => key.to_i
139 assert_response :success,
140 "couldn't get version #{key.to_i} of node #{nodeid}"
142 check_node = Node.from_xml(versions[key])
143 api_node = Node.from_xml(@response.body.to_s)
145 assert_nodes_are_equal check_node, api_node
149 def test_not_found_version
150 check_not_found_id_version(70000,312344)
151 check_not_found_id_version(-1, -13)
152 check_not_found_id_version(nodes(:visible_node).id, 24354)
153 check_not_found_id_version(24356, nodes(:visible_node).version)
156 def check_not_found_id_version(id, version)
157 get :version, :id => id, :version => version
158 assert_response :not_found
159 rescue ActionController::RoutingError => ex
160 assert_match /No route matches/, ex.to_s
164 # Test that getting the current version is identical to picking
165 # that version with the version URI call.
166 def test_current_version
167 check_current_version(current_nodes(:visible_node))
168 check_current_version(current_nodes(:used_node_1))
169 check_current_version(current_nodes(:used_node_2))
170 check_current_version(current_nodes(:node_used_by_relationship))
171 check_current_version(current_nodes(:node_with_versions))
175 # test the redaction of an old version of a node, while not being
177 def test_redact_node_unauthorised
178 do_redact_node(nodes(:node_with_versions_v3),
179 redactions(:example))
180 assert_response :unauthorized, "should need to be authenticated to redact."
184 # test the redaction of an old version of a node, while being
185 # authorised as a normal user.
186 def test_redact_node_normal_user
187 basic_authorization(users(:public_user).email, "test")
189 do_redact_node(nodes(:node_with_versions_v3),
190 redactions(:example))
191 assert_response :forbidden, "should need to be moderator to redact."
195 # test that, even as moderator, the current version of a node
197 def test_redact_node_current_version
198 basic_authorization(users(:moderator_user).email, "test")
200 do_redact_node(nodes(:node_with_versions_v4),
201 redactions(:example))
202 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
206 # test that redacted nodes aren't visible, regardless of
207 # authorisation except as moderator...
208 def test_version_redacted
209 node = nodes(:redacted_node_redacted_version)
211 get :version, :id => node.node_id, :version => node.version
212 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
214 # not even to a logged-in user
215 basic_authorization(users(:public_user).email, "test")
216 get :version, :id => node.node_id, :version => node.version
217 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
221 # test that redacted nodes aren't visible in the history
222 def test_history_redacted
223 node = nodes(:redacted_node_redacted_version)
225 get :history, :id => node.node_id
226 assert_response :success, "Redaction shouldn't have stopped history working."
227 assert_select "osm node[id=#{node.node_id}][version=#{node.version}]", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history."
229 # not even to a logged-in user
230 basic_authorization(users(:public_user).email, "test")
231 get :history, :id => node.node_id
232 assert_response :success, "Redaction shouldn't have stopped history working."
233 assert_select "osm node[id=#{node.node_id}][version=#{node.version}]", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history, even when logged in."
237 # test the redaction of an old version of a node, while being
238 # authorised as a moderator.
239 def test_redact_node_moderator
240 node = nodes(:node_with_versions_v3)
241 basic_authorization(users(:moderator_user).email, "test")
243 do_redact_node(node, redactions(:example))
244 assert_response :success, "should be OK to redact old version as moderator."
246 # check moderator can still see the redacted data, when passing
247 # the appropriate flag
248 get :version, :id => node.node_id, :version => node.version
249 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
250 get :version, :id => node.node_id, :version => node.version, :show_redactions => 'true'
251 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
253 # and when accessed via history
254 get :history, :id => node.node_id
255 assert_response :success, "Redaction shouldn't have stopped history working."
256 assert_select "osm node[id=#{node.node_id}][version=#{node.version}]", 0, "node #{node.node_id} version #{node.version} should not be present in the history for moderators when not passing flag."
257 get :history, :id => node.node_id, :show_redactions => 'true'
258 assert_response :success, "Redaction shouldn't have stopped history working."
259 assert_select "osm node[id=#{node.node_id}][version=#{node.version}]", 1, "node #{node.node_id} version #{node.version} should still be present in the history for moderators when passing flag."
262 # testing that if the moderator drops auth, he can't see the
263 # redacted stuff any more.
264 def test_redact_node_is_redacted
265 node = nodes(:node_with_versions_v3)
266 basic_authorization(users(:moderator_user).email, "test")
268 do_redact_node(node, redactions(:example))
269 assert_response :success, "should be OK to redact old version as moderator."
271 # re-auth as non-moderator
272 basic_authorization(users(:public_user).email, "test")
274 # check can't see the redacted data
275 get :version, :id => node.node_id, :version => node.version
276 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
278 # and when accessed via history
279 get :history, :id => node.node_id
280 assert_response :success, "Redaction shouldn't have stopped history working."
281 assert_select "osm node[id=#{node.node_id}][version=#{node.version}]", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history."
284 def do_redact_node(node, redaction)
285 get :version, :id => node.node_id, :version => node.version
286 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
289 post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
292 def check_current_version(node_id)
293 # get the current version of the node
294 current_node = with_controller(NodeController.new) do
295 get :read, :id => node_id
296 assert_response :success, "cant get current node #{node_id}"
297 Node.from_xml(@response.body)
299 assert_not_nil current_node, "getting node #{node_id} returned nil"
301 # get the "old" version of the node from the old_node interface
302 get :version, :id => node_id, :version => current_node.version
303 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
304 old_node = Node.from_xml(@response.body)
306 # check the nodes are the same
307 assert_nodes_are_equal current_node, old_node
311 # returns a 16 character long string with some nasty characters in it.
312 # this ought to stress-test the tag handling as well as the versioning.
314 letters = [['!','"','$','&',';','@'],
317 ('0'..'9').to_a].flatten
318 (1..16).map { |i| letters[ rand(letters.length) ] }.join
322 # truncate a floating point number to the scale that it is stored in
323 # the database. otherwise rounding errors can produce failing unit
324 # tests when they shouldn't.
326 return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE