]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_nodes_controller_test.rb
76e78ffadaa47b8df30cfe495d40f34d2429e57c
[rails.git] / test / controllers / api / old_nodes_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class OldNodesControllerTest < ActionController::TestCase
5     #
6     # TODO: test history
7     #
8
9     ##
10     # test all routes which lead to this controller
11     def test_routes
12       assert_routing(
13         { :path => "/api/0.6/node/1/history", :method => :get },
14         { :controller => "api/old_nodes", :action => "history", :id => "1" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/node/1/2", :method => :get },
18         { :controller => "api/old_nodes", :action => "version", :id => "1", :version => "2" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/node/1/history.json", :method => :get },
22         { :controller => "api/old_nodes", :action => "history", :id => "1", :format => "json" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/node/1/2.json", :method => :get },
26         { :controller => "api/old_nodes", :action => "version", :id => "1", :version => "2", :format => "json" }
27       )
28       assert_routing(
29         { :path => "/api/0.6/node/1/2/redact", :method => :post },
30         { :controller => "api/old_nodes", :action => "redact", :id => "1", :version => "2" }
31       )
32     end
33
34     ##
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.
38     #
39     ##
40     # FIXME: Move this test to being an integration test since it spans multiple controllers
41     def test_version
42       private_user = create(:user, :data_public => false)
43       private_node = create(:node, :with_history, :version => 4, :changeset => create(:changeset, :user => private_user))
44       user = create(: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)
49
50       ## First try this with a non-public user
51       basic_authorization private_user.email, "test"
52
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
57
58       # keep a hash of the versions => string, as we'll need something
59       # to test against later
60       versions = {}
61
62       # save a version for later checking
63       versions[xml_node["version"]] = xml_doc.to_s
64
65       # randomly move the node about
66       3.times do
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 :update, :params => { :id => nodeid }, :body => xml_doc.to_s
72           assert_response :forbidden, "Should have rejected node update"
73           xml_node["version"] = @response.body.to_s
74         end
75         # save a version for later checking
76         versions[xml_node["version"]] = xml_doc.to_s
77       end
78
79       # add a bunch of random tags
80       3.times do
81         xml_tag = XML::Node.new("tag")
82         xml_tag["k"] = random_string
83         xml_tag["v"] = random_string
84         xml_node << xml_tag
85         with_controller(NodesController.new) do
86           put :update, :params => { :id => nodeid }, :body => xml_doc.to_s
87           assert_response :forbidden,
88                           "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
89           xml_node["version"] = @response.body.to_s
90         end
91         # save a version for later checking
92         versions[xml_node["version"]] = xml_doc.to_s
93       end
94
95       # probably should check that they didn't get written to the database
96
97       ## Now do it with the public user
98       basic_authorization user.email, "test"
99
100       # setup a simple XML node
101
102       xml_doc = xml_for_node(node)
103       xml_node = xml_doc.find("//osm/node").first
104       nodeid = node.id
105
106       # keep a hash of the versions => string, as we'll need something
107       # to test against later
108       versions = {}
109
110       # save a version for later checking
111       versions[xml_node["version"]] = xml_doc.to_s
112
113       # randomly move the node about
114       3.times do
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 :update, :params => { :id => nodeid }, :body => xml_doc.to_s
120           assert_response :success
121           xml_node["version"] = @response.body.to_s
122         end
123         # save a version for later checking
124         versions[xml_node["version"]] = xml_doc.to_s
125       end
126
127       # add a bunch of random tags
128       3.times do
129         xml_tag = XML::Node.new("tag")
130         xml_tag["k"] = random_string
131         xml_tag["v"] = random_string
132         xml_node << xml_tag
133         with_controller(NodesController.new) do
134           put :update, :params => { :id => nodeid }, :body => xml_doc.to_s
135           assert_response :success,
136                           "couldn't update node #{nodeid} (#{@response.body})"
137           xml_node["version"] = @response.body.to_s
138         end
139         # save a version for later checking
140         versions[xml_node["version"]] = xml_doc.to_s
141       end
142
143       # check all the versions
144       versions.each_key do |key|
145         get :version, :params => { :id => nodeid, :version => key.to_i }
146
147         assert_response :success,
148                         "couldn't get version #{key.to_i} of node #{nodeid}"
149
150         check_node = Node.from_xml(versions[key])
151         api_node = Node.from_xml(@response.body.to_s)
152
153         assert_nodes_are_equal check_node, api_node
154       end
155     end
156
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)
162     end
163
164     def check_not_found_id_version(id, version)
165       get :version, :params => { :id => id, :version => version }
166       assert_response :not_found
167     rescue ActionController::UrlGenerationError => e
168       assert_match(/No route matches/, e.to_s)
169     end
170
171     ##
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)
181
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)
190
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)
195     end
196
197     ##
198     # test the redaction of an old version of a node, while not being
199     # authorised.
200     def test_redact_node_unauthorised
201       node = create(:node, :with_history, :version => 4)
202       node_v3 = node.old_nodes.find_by(:version => 3)
203
204       do_redact_node(node_v3,
205                      create(:redaction))
206       assert_response :unauthorized, "should need to be authenticated to redact."
207     end
208
209     ##
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       basic_authorization create(:user).email, "test"
214
215       node = create(:node, :with_history, :version => 4)
216       node_v3 = node.old_nodes.find_by(:version => 3)
217
218       do_redact_node(node_v3,
219                      create(:redaction))
220       assert_response :forbidden, "should need to be moderator to redact."
221     end
222
223     ##
224     # test that, even as moderator, the current version of a node
225     # can't be redacted.
226     def test_redact_node_current_version
227       basic_authorization create(:moderator_user).email, "test"
228
229       node = create(:node, :with_history, :version => 4)
230       node_v4 = node.old_nodes.find_by(:version => 4)
231
232       do_redact_node(node_v4,
233                      create(:redaction))
234       assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
235     end
236
237     ##
238     # test that redacted nodes aren't visible, regardless of
239     # authorisation except as moderator...
240     def test_version_redacted
241       node = create(:node, :with_history, :version => 2)
242       node_v1 = node.old_nodes.find_by(:version => 1)
243       node_v1.redact!(create(:redaction))
244
245       get :version, :params => { :id => node_v1.node_id, :version => node_v1.version }
246       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
247
248       # not even to a logged-in user
249       basic_authorization create(:user).email, "test"
250       get :version, :params => { :id => node_v1.node_id, :version => node_v1.version }
251       assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
252     end
253
254     ##
255     # test that redacted nodes aren't visible in the history
256     def test_history_redacted
257       node = create(:node, :with_history, :version => 2)
258       node_v1 = node.old_nodes.find_by(:version => 1)
259       node_v1.redact!(create(:redaction))
260
261       get :history, :params => { :id => node_v1.node_id }
262       assert_response :success, "Redaction shouldn't have stopped history working."
263       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."
264
265       # not even to a logged-in user
266       basic_authorization create(:user).email, "test"
267       get :history, :params => { :id => node_v1.node_id }
268       assert_response :success, "Redaction shouldn't have stopped history working."
269       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."
270     end
271
272     ##
273     # test the redaction of an old version of a node, while being
274     # authorised as a moderator.
275     def test_redact_node_moderator
276       node = create(:node, :with_history, :version => 4)
277       node_v3 = node.old_nodes.find_by(:version => 3)
278       basic_authorization create(:moderator_user).email, "test"
279
280       do_redact_node(node_v3, create(:redaction))
281       assert_response :success, "should be OK to redact old version as moderator."
282
283       # check moderator can still see the redacted data, when passing
284       # the appropriate flag
285       get :version, :params => { :id => node_v3.node_id, :version => node_v3.version }
286       assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
287       get :version, :params => { :id => node_v3.node_id, :version => node_v3.version, :show_redactions => "true" }
288       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
289
290       # and when accessed via history
291       get :history, :params => { :id => node_v3.node_id }
292       assert_response :success, "Redaction shouldn't have stopped history working."
293       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."
294       get :history, :params => { :id => node_v3.node_id, :show_redactions => "true" }
295       assert_response :success, "Redaction shouldn't have stopped history working."
296       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     end
298
299     # testing that if the moderator drops auth, he can't see the
300     # redacted stuff any more.
301     def test_redact_node_is_redacted
302       node = create(:node, :with_history, :version => 4)
303       node_v3 = node.old_nodes.find_by(:version => 3)
304       basic_authorization create(:moderator_user).email, "test"
305
306       do_redact_node(node_v3, create(:redaction))
307       assert_response :success, "should be OK to redact old version as moderator."
308
309       # re-auth as non-moderator
310       basic_authorization create(:user).email, "test"
311
312       # check can't see the redacted data
313       get :version, :params => { :id => node_v3.node_id, :version => node_v3.version }
314       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
315
316       # and when accessed via history
317       get :history, :params => { :id => node_v3.node_id }
318       assert_response :success, "Redaction shouldn't have stopped history working."
319       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."
320     end
321
322     ##
323     # test the unredaction of an old version of a node, while not being
324     # authorised.
325     def test_unredact_node_unauthorised
326       node = create(:node, :with_history, :version => 2)
327       node_v1 = node.old_nodes.find_by(:version => 1)
328       node_v1.redact!(create(:redaction))
329
330       post :redact, :params => { :id => node_v1.node_id, :version => node_v1.version }
331       assert_response :unauthorized, "should need to be authenticated to unredact."
332     end
333
334     ##
335     # test the unredaction of an old version of a node, while being
336     # authorised as a normal user.
337     def test_unredact_node_normal_user
338       user = create(:user)
339       node = create(:node, :with_history, :version => 2)
340       node_v1 = node.old_nodes.find_by(:version => 1)
341       node_v1.redact!(create(:redaction))
342
343       basic_authorization user.email, "test"
344
345       post :redact, :params => { :id => node_v1.node_id, :version => node_v1.version }
346       assert_response :forbidden, "should need to be moderator to unredact."
347     end
348
349     ##
350     # test the unredaction of an old version of a node, while being
351     # authorised as a moderator.
352     def test_unredact_node_moderator
353       moderator_user = create(:moderator_user)
354       node = create(:node, :with_history, :version => 2)
355       node_v1 = node.old_nodes.find_by(:version => 1)
356       node_v1.redact!(create(:redaction))
357
358       basic_authorization moderator_user.email, "test"
359
360       post :redact, :params => { :id => node_v1.node_id, :version => node_v1.version }
361       assert_response :success, "should be OK to unredact old version as moderator."
362
363       # check moderator can now see the redacted data, when not
364       # passing the aspecial flag
365       get :version, :params => { :id => node_v1.node_id, :version => node_v1.version }
366       assert_response :success, "After unredaction, node should not be gone for moderator."
367
368       # and when accessed via history
369       get :history, :params => { :id => node_v1.node_id }
370       assert_response :success, "Unredaction shouldn't have stopped history working."
371       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."
372
373       basic_authorization create(:user).email, "test"
374
375       # check normal user can now see the redacted data
376       get :version, :params => { :id => node_v1.node_id, :version => node_v1.version }
377       assert_response :success, "After unredaction, node should be visible to normal users."
378
379       # and when accessed via history
380       get :history, :params => { :id => node_v1.node_id }
381       assert_response :success, "Unredaction shouldn't have stopped history working."
382       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."
383     end
384
385     private
386
387     def do_redact_node(node, redaction)
388       get :version, :params => { :id => node.node_id, :version => node.version }
389       assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
390
391       # now redact it
392       post :redact, :params => { :id => node.node_id, :version => node.version, :redaction => redaction.id }
393     end
394
395     def check_current_version(node_id)
396       # get the current version of the node
397       current_node = with_controller(NodesController.new) do
398         get :show, :params => { :id => node_id }
399         assert_response :success, "cant get current node #{node_id}"
400         Node.from_xml(@response.body)
401       end
402       assert_not_nil current_node, "getting node #{node_id} returned nil"
403
404       # get the "old" version of the node from the old_node interface
405       get :version, :params => { :id => node_id, :version => current_node.version }
406       assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
407       old_node = Node.from_xml(@response.body)
408
409       # check the nodes are the same
410       assert_nodes_are_equal current_node, old_node
411     end
412
413     ##
414     # returns a 16 character long string with some nasty characters in it.
415     # this ought to stress-test the tag handling as well as the versioning.
416     def random_string
417       letters = [["!", '"', "$", "&", ";", "@"],
418                  ("a".."z").to_a,
419                  ("A".."Z").to_a,
420                  ("0".."9").to_a].flatten
421       (1..16).map { |_i| letters[rand(letters.length)] }.join
422     end
423
424     ##
425     # truncate a floating point number to the scale that it is stored in
426     # the database. otherwise rounding errors can produce failing unit
427     # tests when they shouldn't.
428     def precision(f)
429       (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
430     end
431
432     def propagate_tags(node, old_node)
433       node.tags.each do |k, v|
434         create(:old_node_tag, :old_node => old_node, :k => k, :v => v)
435       end
436     end
437   end
438 end