]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_nodes_controller_test.rb
Add oauth scope for redactions
[rails.git] / test / controllers / api / old_nodes_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class OldNodesControllerTest < ActionDispatch::IntegrationTest
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       auth_header = basic_authorization_header 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 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
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 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
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       auth_header = basic_authorization_header 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 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
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 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
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 node_version_path(: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     ##
165     # Test that getting the current version is identical to picking
166     # that version with the version URI call.
167     def test_current_version
168       node = create(:node, :with_history)
169       used_node = create(:node, :with_history)
170       create(:way_node, :node => used_node)
171       node_used_by_relationship = create(:node, :with_history)
172       create(:relation_member, :member => node_used_by_relationship)
173       node_with_versions = create(:node, :with_history, :version => 4)
174
175       create(:node_tag, :node => node)
176       create(:node_tag, :node => used_node)
177       create(:node_tag, :node => node_used_by_relationship)
178       create(:node_tag, :node => node_with_versions)
179       propagate_tags(node, node.old_nodes.last)
180       propagate_tags(used_node, used_node.old_nodes.last)
181       propagate_tags(node_used_by_relationship, node_used_by_relationship.old_nodes.last)
182       propagate_tags(node_with_versions, node_with_versions.old_nodes.last)
183
184       check_current_version(node)
185       check_current_version(used_node)
186       check_current_version(node_used_by_relationship)
187       check_current_version(node_with_versions)
188     end
189
190     # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
191     def test_lat_lon_xml_format
192       old_node = create(:old_node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
193
194       get api_node_history_path(:id => old_node.node_id, :version => old_node.version)
195       assert_match(/lat="0.0000400"/, response.body)
196       assert_match(/lon="0.0000800"/, response.body)
197     end
198
199     ##
200     # test the redaction of an old version of a node, while not being
201     # authorised.
202     def test_redact_node_unauthorised
203       node = create(:node, :with_history, :version => 4)
204       node_v3 = node.old_nodes.find_by(:version => 3)
205
206       do_redact_node(node_v3,
207                      create(:redaction))
208       assert_response :unauthorized, "should need to be authenticated to redact."
209     end
210
211     ##
212     # test the redaction of an old version of a node, while being
213     # authorised as a normal user.
214     def test_redact_node_normal_user
215       auth_header = basic_authorization_header create(:user).email, "test"
216
217       node = create(:node, :with_history, :version => 4)
218       node_v3 = node.old_nodes.find_by(:version => 3)
219
220       do_redact_node(node_v3,
221                      create(:redaction),
222                      auth_header)
223       assert_response :forbidden, "should need to be moderator to redact."
224     end
225
226     ##
227     # test that, even as moderator, the current version of a node
228     # can't be redacted.
229     def test_redact_node_current_version
230       auth_header = basic_authorization_header create(:moderator_user).email, "test"
231
232       node = create(:node, :with_history, :version => 4)
233       node_v4 = node.old_nodes.find_by(:version => 4)
234
235       do_redact_node(node_v4,
236                      create(:redaction),
237                      auth_header)
238       assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
239     end
240
241     def test_redact_node_by_regular_with_read_prefs_scope
242       auth_header = create_bearer_auth_header(create(:user), %w[read_prefs])
243       do_redact_redactable_node(auth_header)
244       assert_response :forbidden, "should need to be moderator to redact."
245     end
246
247     def test_redact_node_by_regular_with_write_api_scope
248       auth_header = create_bearer_auth_header(create(:user), %w[write_api])
249       do_redact_redactable_node(auth_header)
250       assert_response :forbidden, "should need to be moderator to redact."
251     end
252
253     def test_redact_node_by_regular_with_write_redactions_scope
254       auth_header = create_bearer_auth_header(create(:user), %w[write_redactions])
255       do_redact_redactable_node(auth_header)
256       assert_response :forbidden, "should need to be moderator to redact."
257     end
258
259     def test_redact_node_by_moderator_with_read_prefs_scope
260       auth_header = create_bearer_auth_header(create(:moderator_user), %w[read_prefs])
261       do_redact_redactable_node(auth_header)
262       assert_response :forbidden, "should need to have write_redactions scope to redact."
263     end
264
265     def test_redact_node_by_moderator_with_write_api_scope
266       auth_header = create_bearer_auth_header(create(:moderator_user), %w[write_api])
267       do_redact_redactable_node(auth_header)
268       assert_response :success, "should be OK to redact old version as moderator with write_api scope."
269       # assert_response :forbidden, "should need to have write_redactions scope to redact."
270     end
271
272     def test_redact_node_by_moderator_with_write_redactions_scope
273       auth_header = create_bearer_auth_header(create(:moderator_user), %w[write_redactions])
274       do_redact_redactable_node(auth_header)
275       assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
276     end
277
278     ##
279     # test that redacted nodes aren't visible, regardless of
280     # authorisation except as moderator...
281     def test_version_redacted
282       node = create(:node, :with_history, :version => 2)
283       node_v1 = node.old_nodes.find_by(:version => 1)
284       node_v1.redact!(create(:redaction))
285
286       get node_version_path(:id => node_v1.node_id, :version => node_v1.version)
287       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
288
289       # not even to a logged-in user
290       auth_header = basic_authorization_header create(:user).email, "test"
291       get node_version_path(:id => node_v1.node_id, :version => node_v1.version), :headers => auth_header
292       assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
293     end
294
295     ##
296     # test that redacted nodes aren't visible in the history
297     def test_history_redacted
298       node = create(:node, :with_history, :version => 2)
299       node_v1 = node.old_nodes.find_by(:version => 1)
300       node_v1.redact!(create(:redaction))
301
302       get api_node_history_path(:id => node_v1.node_id)
303       assert_response :success, "Redaction shouldn't have stopped history working."
304       assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0,
305                     "redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history."
306
307       # not even to a logged-in user
308       auth_header = basic_authorization_header create(:user).email, "test"
309       get api_node_history_path(:id => node_v1.node_id), :headers => auth_header
310       assert_response :success, "Redaction shouldn't have stopped history working."
311       assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0,
312                     "redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history, even when logged in."
313     end
314
315     ##
316     # test the redaction of an old version of a node, while being
317     # authorised as a moderator.
318     def test_redact_node_moderator
319       node = create(:node, :with_history, :version => 4)
320       node_v3 = node.old_nodes.find_by(:version => 3)
321       auth_header = basic_authorization_header create(:moderator_user).email, "test"
322
323       do_redact_node(node_v3, create(:redaction), auth_header)
324       assert_response :success, "should be OK to redact old version as moderator."
325
326       # check moderator can still see the redacted data, when passing
327       # the appropriate flag
328       get node_version_path(:id => node_v3.node_id, :version => node_v3.version), :headers => auth_header
329       assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
330       get node_version_path(:id => node_v3.node_id, :version => node_v3.version), :params => { :show_redactions => "true" }, :headers => auth_header
331       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
332
333       # and when accessed via history
334       get api_node_history_path(:id => node_v3.node_id)
335       assert_response :success, "Redaction shouldn't have stopped history working."
336       assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
337                     "node #{node_v3.node_id} version #{node_v3.version} should not be present in the history for moderators when not passing flag."
338       get api_node_history_path(:id => node_v3.node_id), :params => { :show_redactions => "true" }, :headers => auth_header
339       assert_response :success, "Redaction shouldn't have stopped history working."
340       assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 1,
341                     "node #{node_v3.node_id} version #{node_v3.version} should still be present in the history for moderators when passing flag."
342     end
343
344     # testing that if the moderator drops auth, he can't see the
345     # redacted stuff any more.
346     def test_redact_node_is_redacted
347       node = create(:node, :with_history, :version => 4)
348       node_v3 = node.old_nodes.find_by(:version => 3)
349       auth_header = basic_authorization_header create(:moderator_user).email, "test"
350
351       do_redact_node(node_v3, create(:redaction), auth_header)
352       assert_response :success, "should be OK to redact old version as moderator."
353
354       # re-auth as non-moderator
355       auth_header = basic_authorization_header create(:user).email, "test"
356
357       # check can't see the redacted data
358       get node_version_path(:id => node_v3.node_id, :version => node_v3.version), :headers => auth_header
359       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
360
361       # and when accessed via history
362       get api_node_history_path(:id => node_v3.node_id), :headers => auth_header
363       assert_response :success, "Redaction shouldn't have stopped history working."
364       assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
365                     "redacted node #{node_v3.node_id} version #{node_v3.version} shouldn't be present in the history."
366     end
367
368     ##
369     # test the unredaction of an old version of a node, while not being
370     # authorised.
371     def test_unredact_node_unauthorised
372       node = create(:node, :with_history, :version => 2)
373       node_v1 = node.old_nodes.find_by(:version => 1)
374       node_v1.redact!(create(:redaction))
375
376       post node_version_redact_path(:id => node_v1.node_id, :version => node_v1.version)
377       assert_response :unauthorized, "should need to be authenticated to unredact."
378     end
379
380     ##
381     # test the unredaction of an old version of a node, while being
382     # authorised as a normal user.
383     def test_unredact_node_normal_user
384       user = create(:user)
385       node = create(:node, :with_history, :version => 2)
386       node_v1 = node.old_nodes.find_by(:version => 1)
387       node_v1.redact!(create(:redaction))
388
389       auth_header = basic_authorization_header user.email, "test"
390
391       post node_version_redact_path(:id => node_v1.node_id, :version => node_v1.version), :headers => auth_header
392       assert_response :forbidden, "should need to be moderator to unredact."
393     end
394
395     ##
396     # test the unredaction of an old version of a node, while being
397     # authorised as a moderator.
398     def test_unredact_node_moderator
399       moderator_user = create(:moderator_user)
400       node = create(:node, :with_history, :version => 2)
401       node_v1 = node.old_nodes.find_by(:version => 1)
402       node_v1.redact!(create(:redaction))
403
404       auth_header = basic_authorization_header moderator_user.email, "test"
405
406       post node_version_redact_path(:id => node_v1.node_id, :version => node_v1.version), :headers => auth_header
407       assert_response :success, "should be OK to unredact old version as moderator."
408
409       # check moderator can now see the redacted data, when not
410       # passing the aspecial flag
411       get node_version_path(:id => node_v1.node_id, :version => node_v1.version), :headers => auth_header
412       assert_response :success, "After unredaction, node should not be gone for moderator."
413
414       # and when accessed via history
415       get api_node_history_path(:id => node_v1.node_id)
416       assert_response :success, "Unredaction shouldn't have stopped history working."
417       assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
418                     "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for moderators without passing flag."
419
420       auth_header = basic_authorization_header create(:user).email, "test"
421
422       # check normal user can now see the redacted data
423       get node_version_path(:id => node_v1.node_id, :version => node_v1.version), :headers => auth_header
424       assert_response :success, "After unredaction, node should be visible to normal users."
425
426       # and when accessed via history
427       get api_node_history_path(:id => node_v1.node_id)
428       assert_response :success, "Unredaction shouldn't have stopped history working."
429       assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
430                     "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for normal users without passing flag."
431     end
432
433     private
434
435     def create_bearer_auth_header(user, scopes)
436       token = create(:oauth_access_token,
437                      :resource_owner_id => user.id,
438                      :scopes => scopes)
439       bearer_authorization_header(token.token)
440     end
441
442     def do_redact_redactable_node(headers = {})
443       node = create(:node, :with_history, :version => 4)
444       node_v3 = node.old_nodes.find_by(:version => 3)
445       do_redact_node(node_v3, create(:redaction), headers)
446     end
447
448     def do_redact_node(node, redaction, headers = {})
449       get node_version_path(:id => node.node_id, :version => node.version), :headers => headers
450       assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
451
452       # now redact it
453       post node_version_redact_path(:id => node.node_id, :version => node.version), :params => { :redaction => redaction.id }, :headers => headers
454     end
455
456     def check_current_version(node_id)
457       # get the current version of the node
458       current_node = with_controller(NodesController.new) do
459         get api_node_path(:id => node_id)
460         assert_response :success, "cant get current node #{node_id}"
461         Node.from_xml(@response.body)
462       end
463       assert_not_nil current_node, "getting node #{node_id} returned nil"
464
465       # get the "old" version of the node from the old_node interface
466       get node_version_path(:id => node_id, :version => current_node.version)
467       assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
468       old_node = Node.from_xml(@response.body)
469
470       # check the nodes are the same
471       assert_nodes_are_equal current_node, old_node
472     end
473
474     def check_not_found_id_version(id, version)
475       get node_version_path(:id => id, :version => version)
476       assert_response :not_found
477     rescue ActionController::UrlGenerationError => e
478       assert_match(/No route matches/, e.to_s)
479     end
480
481     ##
482     # returns a 16 character long string with some nasty characters in it.
483     # this ought to stress-test the tag handling as well as the versioning.
484     def random_string
485       letters = [["!", '"', "$", "&", ";", "@"],
486                  ("a".."z").to_a,
487                  ("A".."Z").to_a,
488                  ("0".."9").to_a].flatten
489       (1..16).map { letters[rand(letters.length)] }.join
490     end
491
492     ##
493     # truncate a floating point number to the scale that it is stored in
494     # the database. otherwise rounding errors can produce failing unit
495     # tests when they shouldn't.
496     def precision(f)
497       (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
498     end
499
500     def propagate_tags(node, old_node)
501       node.tags.each do |k, v|
502         create(:old_node_tag, :old_node => old_node, :k => k, :v => v)
503       end
504     end
505   end
506 end