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 private_user = create(:user, :data_public => false)
37 private_node = create(:node, :with_history, :version => 4, :changeset => create(:changeset, :user => private_user))
39 node = create(:node, :with_history, :version => 4, :changeset => create(:changeset, :user => user))
40 create_list(:node_tag, 2, :node => node)
41 # Ensure that the current tags are propagated to the history too
42 propagate_tags(node, node.old_nodes.last)
44 ## First try this with a non-public user
45 basic_authorization(private_user.email, "test")
47 # setup a simple XML node
48 xml_doc = private_node.to_xml
49 xml_node = xml_doc.find("//osm/node").first
50 nodeid = private_node.id
52 # keep a hash of the versions => string, as we'll need something
53 # to test against later
56 # save a version for later checking
57 versions[xml_node["version"]] = xml_doc.to_s
59 # randomly move the node about
61 # move the node somewhere else
62 xml_node["lat"] = precision(rand * 180 - 90).to_s
63 xml_node["lon"] = precision(rand * 360 - 180).to_s
64 with_controller(NodeController.new) do
66 put :update, :id => nodeid
67 assert_response :forbidden, "Should have rejected node update"
68 xml_node["version"] = @response.body.to_s
70 # save a version for later checking
71 versions[xml_node["version"]] = xml_doc.to_s
74 # add a bunch of random tags
76 xml_tag = XML::Node.new("tag")
77 xml_tag["k"] = random_string
78 xml_tag["v"] = random_string
80 with_controller(NodeController.new) do
82 put :update, :id => nodeid
83 assert_response :forbidden,
84 "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
85 xml_node["version"] = @response.body.to_s
87 # save a version for later checking
88 versions[xml_node["version"]] = xml_doc.to_s
91 # probably should check that they didn't get written to the database
93 ## Now do it with the public user
94 basic_authorization(user.email, "test")
96 # setup a simple XML node
99 xml_node = xml_doc.find("//osm/node").first
102 # keep a hash of the versions => string, as we'll need something
103 # to test against later
106 # save a version for later checking
107 versions[xml_node["version"]] = xml_doc.to_s
109 # randomly move the node about
111 # move the node somewhere else
112 xml_node["lat"] = precision(rand * 180 - 90).to_s
113 xml_node["lon"] = precision(rand * 360 - 180).to_s
114 with_controller(NodeController.new) do
116 put :update, :id => nodeid
117 assert_response :success
118 xml_node["version"] = @response.body.to_s
120 # save a version for later checking
121 versions[xml_node["version"]] = xml_doc.to_s
124 # add a bunch of random tags
126 xml_tag = XML::Node.new("tag")
127 xml_tag["k"] = random_string
128 xml_tag["v"] = random_string
130 with_controller(NodeController.new) do
132 put :update, :id => nodeid
133 assert_response :success,
134 "couldn't update node #{nodeid} (#{@response.body})"
135 xml_node["version"] = @response.body.to_s
137 # save a version for later checking
138 versions[xml_node["version"]] = xml_doc.to_s
141 # check all the versions
142 versions.keys.each do |key|
143 get :version, :id => nodeid, :version => key.to_i
145 assert_response :success,
146 "couldn't get version #{key.to_i} of node #{nodeid}"
148 check_node = Node.from_xml(versions[key])
149 api_node = Node.from_xml(@response.body.to_s)
151 assert_nodes_are_equal check_node, api_node
155 def test_not_found_version
156 check_not_found_id_version(70000, 312344)
157 check_not_found_id_version(-1, -13)
158 check_not_found_id_version(create(:node).id, 24354)
159 check_not_found_id_version(24356, create(:node).version)
162 def check_not_found_id_version(id, version)
163 get :version, :id => id, :version => version
164 assert_response :not_found
165 rescue ActionController::UrlGenerationError => ex
166 assert_match /No route matches/, ex.to_s
170 # Test that getting the current version is identical to picking
171 # that version with the version URI call.
172 def test_current_version
173 node = create(:node, :with_history)
174 used_node = create(:node, :with_history)
175 create(:way_node, :node => used_node)
176 node_used_by_relationship = create(:node, :with_history)
177 create(:relation_member, :member => node_used_by_relationship)
178 node_with_versions = create(:node, :with_history, :version => 4)
180 create(:node_tag, :node => node)
181 create(:node_tag, :node => used_node)
182 create(:node_tag, :node => node_used_by_relationship)
183 create(:node_tag, :node => node_with_versions)
184 propagate_tags(node, node.old_nodes.last)
185 propagate_tags(used_node, used_node.old_nodes.last)
186 propagate_tags(node_used_by_relationship, node_used_by_relationship.old_nodes.last)
187 propagate_tags(node_with_versions, node_with_versions.old_nodes.last)
189 check_current_version(node)
190 check_current_version(used_node)
191 check_current_version(node_used_by_relationship)
192 check_current_version(node_with_versions)
196 # test the redaction of an old version of a node, while not being
198 def test_redact_node_unauthorised
199 node = create(:node, :with_history, :version => 4)
200 node_v3 = node.old_nodes.find_by(:version => 3)
202 do_redact_node(node_v3,
204 assert_response :unauthorized, "should need to be authenticated to redact."
208 # test the redaction of an old version of a node, while being
209 # authorised as a normal user.
210 def test_redact_node_normal_user
211 basic_authorization(create(:user).email, "test")
213 node = create(:node, :with_history, :version => 4)
214 node_v3 = node.old_nodes.find_by(:version => 3)
216 do_redact_node(node_v3,
218 assert_response :forbidden, "should need to be moderator to redact."
222 # test that, even as moderator, the current version of a node
224 def test_redact_node_current_version
225 basic_authorization(create(:moderator_user).email, "test")
227 node = create(:node, :with_history, :version => 4)
228 node_v4 = node.old_nodes.find_by(:version => 4)
230 do_redact_node(node_v4,
232 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
236 # test that redacted nodes aren't visible, regardless of
237 # authorisation except as moderator...
238 def test_version_redacted
239 node = create(:node, :with_history, :version => 2)
240 node_v1 = node.old_nodes.find_by(:version => 1)
241 node_v1.redact!(create(:redaction))
243 get :version, :id => node_v1.node_id, :version => node_v1.version
244 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
246 # not even to a logged-in user
247 basic_authorization(create(:user).email, "test")
248 get :version, :id => node_v1.node_id, :version => node_v1.version
249 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
253 # test that redacted nodes aren't visible in the history
254 def test_history_redacted
255 node = create(:node, :with_history, :version => 2)
256 node_v1 = node.old_nodes.find_by(:version => 1)
257 node_v1.redact!(create(:redaction))
259 get :history, :id => node_v1.node_id
260 assert_response :success, "Redaction shouldn't have stopped history working."
261 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0, "redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history."
263 # not even to a logged-in user
264 basic_authorization(create(:user).email, "test")
265 get :history, :id => node_v1.node_id
266 assert_response :success, "Redaction shouldn't have stopped history working."
267 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0, "redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history, even when logged in."
271 # test the redaction of an old version of a node, while being
272 # authorised as a moderator.
273 def test_redact_node_moderator
274 node = create(:node, :with_history, :version => 4)
275 node_v3 = node.old_nodes.find_by(:version => 3)
276 basic_authorization(create(:moderator_user).email, "test")
278 do_redact_node(node_v3, create(:redaction))
279 assert_response :success, "should be OK to redact old version as moderator."
281 # check moderator can still see the redacted data, when passing
282 # the appropriate flag
283 get :version, :id => node_v3.node_id, :version => node_v3.version
284 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
285 get :version, :id => node_v3.node_id, :version => node_v3.version, :show_redactions => "true"
286 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
288 # and when accessed via history
289 get :history, :id => node_v3.node_id
290 assert_response :success, "Redaction shouldn't have stopped history working."
291 assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0, "node #{node_v3.node_id} version #{node_v3.version} should not be present in the history for moderators when not passing flag."
292 get :history, :id => node_v3.node_id, :show_redactions => "true"
293 assert_response :success, "Redaction shouldn't have stopped history working."
294 assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 1, "node #{node_v3.node_id} version #{node_v3.version} should still be present in the history for moderators when passing flag."
297 # testing that if the moderator drops auth, he can't see the
298 # redacted stuff any more.
299 def test_redact_node_is_redacted
300 node = create(:node, :with_history, :version => 4)
301 node_v3 = node.old_nodes.find_by(:version => 3)
302 basic_authorization(create(:moderator_user).email, "test")
304 do_redact_node(node_v3, create(:redaction))
305 assert_response :success, "should be OK to redact old version as moderator."
307 # re-auth as non-moderator
308 basic_authorization(create(:user).email, "test")
310 # check can't see the redacted data
311 get :version, :id => node_v3.node_id, :version => node_v3.version
312 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
314 # and when accessed via history
315 get :history, :id => node_v3.node_id
316 assert_response :success, "Redaction shouldn't have stopped history working."
317 assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0, "redacted node #{node_v3.node_id} version #{node_v3.version} shouldn't be present in the history."
321 # test the unredaction of an old version of a node, while not being
323 def test_unredact_node_unauthorised
324 node = create(:node, :with_history, :version => 2)
325 node_v1 = node.old_nodes.find_by(:version => 1)
326 node_v1.redact!(create(:redaction))
328 post :redact, :id => node_v1.node_id, :version => node_v1.version
329 assert_response :unauthorized, "should need to be authenticated to unredact."
333 # test the unredaction of an old version of a node, while being
334 # authorised as a normal user.
335 def test_unredact_node_normal_user
337 node = create(:node, :with_history, :version => 2)
338 node_v1 = node.old_nodes.find_by(:version => 1)
339 node_v1.redact!(create(:redaction))
341 basic_authorization(user.email, "test")
343 post :redact, :id => node_v1.node_id, :version => node_v1.version
344 assert_response :forbidden, "should need to be moderator to unredact."
348 # test the unredaction of an old version of a node, while being
349 # authorised as a moderator.
350 def test_unredact_node_moderator
351 moderator_user = create(:moderator_user)
352 node = create(:node, :with_history, :version => 2)
353 node_v1 = node.old_nodes.find_by(:version => 1)
354 node_v1.redact!(create(:redaction))
356 basic_authorization(moderator_user.email, "test")
358 post :redact, :id => node_v1.node_id, :version => node_v1.version
359 assert_response :success, "should be OK to unredact old version as moderator."
361 # check moderator can now see the redacted data, when not
362 # passing the aspecial flag
363 get :version, :id => node_v1.node_id, :version => node_v1.version
364 assert_response :success, "After unredaction, node should not be gone for moderator."
366 # and when accessed via history
367 get :history, :id => node_v1.node_id
368 assert_response :success, "Unredaction shouldn't have stopped history working."
369 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1, "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for moderators without passing flag."
371 basic_authorization(create(:user).email, "test")
373 # check normal user can now see the redacted data
374 get :version, :id => node_v1.node_id, :version => node_v1.version
375 assert_response :success, "After unredaction, node should be visible to normal users."
377 # and when accessed via history
378 get :history, :id => node_v1.node_id
379 assert_response :success, "Unredaction shouldn't have stopped history working."
380 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1, "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for normal users without passing flag."
385 def do_redact_node(node, redaction)
386 get :version, :id => node.node_id, :version => node.version
387 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
390 post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
393 def check_current_version(node_id)
394 # get the current version of the node
395 current_node = with_controller(NodeController.new) do
396 get :read, :id => node_id
397 assert_response :success, "cant get current node #{node_id}"
398 Node.from_xml(@response.body)
400 assert_not_nil current_node, "getting node #{node_id} returned nil"
402 # get the "old" version of the node from the old_node interface
403 get :version, :id => node_id, :version => current_node.version
404 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
405 old_node = Node.from_xml(@response.body)
407 # check the nodes are the same
408 assert_nodes_are_equal current_node, old_node
412 # returns a 16 character long string with some nasty characters in it.
413 # this ought to stress-test the tag handling as well as the versioning.
415 letters = [["!", '"', "$", "&", ";", "@"],
418 ("0".."9").to_a].flatten
419 (1..16).map { |_i| letters[rand(letters.length)] }.join
423 # truncate a floating point number to the scale that it is stored in
424 # the database. otherwise rounding errors can produce failing unit
425 # tests when they shouldn't.
427 (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
430 def propagate_tags(node, old_node)
431 node.tags.each do |k, v|
432 create(:old_node_tag, :old_node => old_node, :k => k, :v => v)