4 class OldNodesControllerTest < ActionDispatch::IntegrationTest
10 # test all routes which lead to this controller
13 { :path => "/api/0.6/node/1/history", :method => :get },
14 { :controller => "api/old_nodes", :action => "history", :id => "1" }
17 { :path => "/api/0.6/node/1/2", :method => :get },
18 { :controller => "api/old_nodes", :action => "version", :id => "1", :version => "2" }
21 { :path => "/api/0.6/node/1/history.json", :method => :get },
22 { :controller => "api/old_nodes", :action => "history", :id => "1", :format => "json" }
25 { :path => "/api/0.6/node/1/2.json", :method => :get },
26 { :controller => "api/old_nodes", :action => "version", :id => "1", :version => "2", :format => "json" }
29 { :path => "/api/0.6/node/1/2/redact", :method => :post },
30 { :controller => "api/old_nodes", :action => "redact", :id => "1", :version => "2" }
35 # test the version call by submitting several revisions of a new node
36 # to the API and ensuring that later calls to version return the
37 # matching versions of the object.
40 # FIXME: Move this test to being an integration test since it spans multiple controllers
42 private_user = create(:user, :data_public => false)
43 private_node = create(:node, :with_history, :version => 4, :changeset => create(:changeset, :user => private_user))
45 node = create(:node, :with_history, :version => 4, :changeset => create(:changeset, :user => user))
46 create_list(:node_tag, 2, :node => node)
47 # Ensure that the current tags are propagated to the history too
48 propagate_tags(node, node.old_nodes.last)
50 ## First try this with a non-public user
51 auth_header = basic_authorization_header private_user.email, "test"
53 # setup a simple XML node
54 xml_doc = xml_for_node(private_node)
55 xml_node = xml_doc.find("//osm/node").first
56 nodeid = private_node.id
58 # keep a hash of the versions => string, as we'll need something
59 # to test against later
62 # save a version for later checking
63 versions[xml_node["version"]] = xml_doc.to_s
65 # randomly move the node about
67 # move the node somewhere else
68 xml_node["lat"] = precision(rand * 180 - 90).to_s
69 xml_node["lon"] = precision(rand * 360 - 180).to_s
70 with_controller(NodesController.new) do
71 put api_node_path(:id => nodeid), :params => xml_doc.to_s, :headers => auth_header
72 assert_response :forbidden, "Should have rejected node update"
73 xml_node["version"] = @response.body.to_s
75 # save a version for later checking
76 versions[xml_node["version"]] = xml_doc.to_s
79 # add a bunch of random tags
81 xml_tag = XML::Node.new("tag")
82 xml_tag["k"] = random_string
83 xml_tag["v"] = random_string
85 with_controller(NodesController.new) do
86 put api_node_path(:id => nodeid), :params => xml_doc.to_s, :headers => auth_header
87 assert_response :forbidden,
88 "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
89 xml_node["version"] = @response.body.to_s
91 # save a version for later checking
92 versions[xml_node["version"]] = xml_doc.to_s
95 # probably should check that they didn't get written to the database
97 ## Now do it with the public user
98 auth_header = basic_authorization_header user.email, "test"
100 # setup a simple XML node
102 xml_doc = xml_for_node(node)
103 xml_node = xml_doc.find("//osm/node").first
106 # keep a hash of the versions => string, as we'll need something
107 # to test against later
110 # save a version for later checking
111 versions[xml_node["version"]] = xml_doc.to_s
113 # randomly move the node about
115 # move the node somewhere else
116 xml_node["lat"] = precision(rand * 180 - 90).to_s
117 xml_node["lon"] = precision(rand * 360 - 180).to_s
118 with_controller(NodesController.new) do
119 put api_node_path(:id => nodeid), :params => xml_doc.to_s, :headers => auth_header
120 assert_response :success
121 xml_node["version"] = @response.body.to_s
123 # save a version for later checking
124 versions[xml_node["version"]] = xml_doc.to_s
127 # add a bunch of random tags
129 xml_tag = XML::Node.new("tag")
130 xml_tag["k"] = random_string
131 xml_tag["v"] = random_string
133 with_controller(NodesController.new) do
134 put api_node_path(:id => nodeid), :params => xml_doc.to_s, :headers => auth_header
135 assert_response :success,
136 "couldn't update node #{nodeid} (#{@response.body})"
137 xml_node["version"] = @response.body.to_s
139 # save a version for later checking
140 versions[xml_node["version"]] = xml_doc.to_s
143 # check all the versions
144 versions.each_key do |key|
145 get node_version_path(:id => nodeid, :version => key.to_i)
147 assert_response :success,
148 "couldn't get version #{key.to_i} of node #{nodeid}"
150 check_node = Node.from_xml(versions[key])
151 api_node = Node.from_xml(@response.body.to_s)
153 assert_nodes_are_equal check_node, api_node
157 def test_not_found_version
158 check_not_found_id_version(70000, 312344)
159 check_not_found_id_version(-1, -13)
160 check_not_found_id_version(create(:node).id, 24354)
161 check_not_found_id_version(24356, create(:node).version)
164 def check_not_found_id_version(id, version)
165 get node_version_path(:id => id, :version => version)
166 assert_response :not_found
167 rescue ActionController::UrlGenerationError => e
168 assert_match(/No route matches/, e.to_s)
172 # Test that getting the current version is identical to picking
173 # that version with the version URI call.
174 def test_current_version
175 node = create(:node, :with_history)
176 used_node = create(:node, :with_history)
177 create(:way_node, :node => used_node)
178 node_used_by_relationship = create(:node, :with_history)
179 create(:relation_member, :member => node_used_by_relationship)
180 node_with_versions = create(:node, :with_history, :version => 4)
182 create(:node_tag, :node => node)
183 create(:node_tag, :node => used_node)
184 create(:node_tag, :node => node_used_by_relationship)
185 create(:node_tag, :node => node_with_versions)
186 propagate_tags(node, node.old_nodes.last)
187 propagate_tags(used_node, used_node.old_nodes.last)
188 propagate_tags(node_used_by_relationship, node_used_by_relationship.old_nodes.last)
189 propagate_tags(node_with_versions, node_with_versions.old_nodes.last)
191 check_current_version(node)
192 check_current_version(used_node)
193 check_current_version(node_used_by_relationship)
194 check_current_version(node_with_versions)
198 # test the redaction of an old version of a node, while not being
200 def test_redact_node_unauthorised
201 node = create(:node, :with_history, :version => 4)
202 node_v3 = node.old_nodes.find_by(:version => 3)
204 do_redact_node(node_v3,
206 assert_response :unauthorized, "should need to be authenticated to redact."
210 # test the redaction of an old version of a node, while being
211 # authorised as a normal user.
212 def test_redact_node_normal_user
213 auth_header = basic_authorization_header create(:user).email, "test"
215 node = create(:node, :with_history, :version => 4)
216 node_v3 = node.old_nodes.find_by(:version => 3)
218 do_redact_node(node_v3,
221 assert_response :forbidden, "should need to be moderator to redact."
225 # test that, even as moderator, the current version of a node
227 def test_redact_node_current_version
228 auth_header = basic_authorization_header create(:moderator_user).email, "test"
230 node = create(:node, :with_history, :version => 4)
231 node_v4 = node.old_nodes.find_by(:version => 4)
233 do_redact_node(node_v4,
236 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
240 # test that redacted nodes aren't visible, regardless of
241 # authorisation except as moderator...
242 def test_version_redacted
243 node = create(:node, :with_history, :version => 2)
244 node_v1 = node.old_nodes.find_by(:version => 1)
245 node_v1.redact!(create(:redaction))
247 get node_version_path(:id => node_v1.node_id, :version => node_v1.version)
248 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
250 # not even to a logged-in user
251 auth_header = basic_authorization_header create(:user).email, "test"
252 get node_version_path(:id => node_v1.node_id, :version => node_v1.version), :headers => auth_header
253 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
257 # test that redacted nodes aren't visible in the history
258 def test_history_redacted
259 node = create(:node, :with_history, :version => 2)
260 node_v1 = node.old_nodes.find_by(:version => 1)
261 node_v1.redact!(create(:redaction))
263 get api_node_history_path(:id => node_v1.node_id)
264 assert_response :success, "Redaction shouldn't have stopped history working."
265 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0,
266 "redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history."
268 # not even to a logged-in user
269 auth_header = basic_authorization_header create(:user).email, "test"
270 get api_node_history_path(:id => node_v1.node_id), :headers => auth_header
271 assert_response :success, "Redaction shouldn't have stopped history working."
272 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0,
273 "redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history, even when logged in."
277 # test the redaction of an old version of a node, while being
278 # authorised as a moderator.
279 def test_redact_node_moderator
280 node = create(:node, :with_history, :version => 4)
281 node_v3 = node.old_nodes.find_by(:version => 3)
282 auth_header = basic_authorization_header create(:moderator_user).email, "test"
284 do_redact_node(node_v3, create(:redaction), auth_header)
285 assert_response :success, "should be OK to redact old version as moderator."
287 # check moderator can still see the redacted data, when passing
288 # the appropriate flag
289 get node_version_path(:id => node_v3.node_id, :version => node_v3.version), :headers => auth_header
290 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
291 get node_version_path(:id => node_v3.node_id, :version => node_v3.version), :params => { :show_redactions => "true" }, :headers => auth_header
292 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
294 # and when accessed via history
295 get api_node_history_path(:id => node_v3.node_id)
296 assert_response :success, "Redaction shouldn't have stopped history working."
297 assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
298 "node #{node_v3.node_id} version #{node_v3.version} should not be present in the history for moderators when not passing flag."
299 get api_node_history_path(:id => node_v3.node_id), :params => { :show_redactions => "true" }, :headers => auth_header
300 assert_response :success, "Redaction shouldn't have stopped history working."
301 assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 1,
302 "node #{node_v3.node_id} version #{node_v3.version} should still be present in the history for moderators when passing flag."
305 # testing that if the moderator drops auth, he can't see the
306 # redacted stuff any more.
307 def test_redact_node_is_redacted
308 node = create(:node, :with_history, :version => 4)
309 node_v3 = node.old_nodes.find_by(:version => 3)
310 auth_header = basic_authorization_header create(:moderator_user).email, "test"
312 do_redact_node(node_v3, create(:redaction), auth_header)
313 assert_response :success, "should be OK to redact old version as moderator."
315 # re-auth as non-moderator
316 auth_header = basic_authorization_header create(:user).email, "test"
318 # check can't see the redacted data
319 get node_version_path(:id => node_v3.node_id, :version => node_v3.version), :headers => auth_header
320 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
322 # and when accessed via history
323 get api_node_history_path(:id => node_v3.node_id), :headers => auth_header
324 assert_response :success, "Redaction shouldn't have stopped history working."
325 assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
326 "redacted node #{node_v3.node_id} version #{node_v3.version} shouldn't be present in the history."
330 # test the unredaction of an old version of a node, while not being
332 def test_unredact_node_unauthorised
333 node = create(:node, :with_history, :version => 2)
334 node_v1 = node.old_nodes.find_by(:version => 1)
335 node_v1.redact!(create(:redaction))
337 post node_version_redact_path(:id => node_v1.node_id, :version => node_v1.version)
338 assert_response :unauthorized, "should need to be authenticated to unredact."
342 # test the unredaction of an old version of a node, while being
343 # authorised as a normal user.
344 def test_unredact_node_normal_user
346 node = create(:node, :with_history, :version => 2)
347 node_v1 = node.old_nodes.find_by(:version => 1)
348 node_v1.redact!(create(:redaction))
350 auth_header = basic_authorization_header user.email, "test"
352 post node_version_redact_path(:id => node_v1.node_id, :version => node_v1.version), :headers => auth_header
353 assert_response :forbidden, "should need to be moderator to unredact."
357 # test the unredaction of an old version of a node, while being
358 # authorised as a moderator.
359 def test_unredact_node_moderator
360 moderator_user = create(:moderator_user)
361 node = create(:node, :with_history, :version => 2)
362 node_v1 = node.old_nodes.find_by(:version => 1)
363 node_v1.redact!(create(:redaction))
365 auth_header = basic_authorization_header moderator_user.email, "test"
367 post node_version_redact_path(:id => node_v1.node_id, :version => node_v1.version), :headers => auth_header
368 assert_response :success, "should be OK to unredact old version as moderator."
370 # check moderator can now see the redacted data, when not
371 # passing the aspecial flag
372 get node_version_path(:id => node_v1.node_id, :version => node_v1.version), :headers => auth_header
373 assert_response :success, "After unredaction, node should not be gone for moderator."
375 # and when accessed via history
376 get api_node_history_path(:id => node_v1.node_id)
377 assert_response :success, "Unredaction shouldn't have stopped history working."
378 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
379 "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for moderators without passing flag."
381 auth_header = basic_authorization_header create(:user).email, "test"
383 # check normal user can now see the redacted data
384 get node_version_path(:id => node_v1.node_id, :version => node_v1.version), :headers => auth_header
385 assert_response :success, "After unredaction, node should be visible to normal users."
387 # and when accessed via history
388 get api_node_history_path(:id => node_v1.node_id)
389 assert_response :success, "Unredaction shouldn't have stopped history working."
390 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
391 "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for normal users without passing flag."
396 def do_redact_node(node, redaction, headers = {})
397 get node_version_path(:id => node.node_id, :version => node.version), :headers => headers
398 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
401 post node_version_redact_path(:id => node.node_id, :version => node.version), :params => { :redaction => redaction.id }, :headers => headers
404 def check_current_version(node_id)
405 # get the current version of the node
406 current_node = with_controller(NodesController.new) do
407 get api_node_path(:id => node_id)
408 assert_response :success, "cant get current node #{node_id}"
409 Node.from_xml(@response.body)
411 assert_not_nil current_node, "getting node #{node_id} returned nil"
413 # get the "old" version of the node from the old_node interface
414 get node_version_path(:id => node_id, :version => current_node.version)
415 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
416 old_node = Node.from_xml(@response.body)
418 # check the nodes are the same
419 assert_nodes_are_equal current_node, old_node
423 # returns a 16 character long string with some nasty characters in it.
424 # this ought to stress-test the tag handling as well as the versioning.
426 letters = [["!", '"', "$", "&", ";", "@"],
429 ("0".."9").to_a].flatten
430 (1..16).map { |_i| letters[rand(letters.length)] }.join
434 # truncate a floating point number to the scale that it is stored in
435 # the database. otherwise rounding errors can produce failing unit
436 # tests when they shouldn't.
438 (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
441 def propagate_tags(node, old_node)
442 node.tags.each do |k, v|
443 create(:old_node_tag, :old_node => old_node, :k => k, :v => v)