]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_nodes_controller_test.rb
7afa8c99701e2488ac7607aee5997430fc1a9ed3
[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     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/node/1/history", :method => :get },
10         { :controller => "api/old_nodes", :action => "index", :node_id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/node/1/history.json", :method => :get },
14         { :controller => "api/old_nodes", :action => "index", :node_id => "1", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/node/1/2", :method => :get },
18         { :controller => "api/old_nodes", :action => "show", :node_id => "1", :version => "2" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/node/1/2.json", :method => :get },
22         { :controller => "api/old_nodes", :action => "show", :node_id => "1", :version => "2", :format => "json" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/node/1/2/redact", :method => :post },
26         { :controller => "api/old_nodes", :action => "redact", :node_id => "1", :version => "2" }
27       )
28     end
29
30     def test_index
31       node = create(:node, :version => 2)
32       create(:old_node, :node_id => node.id, :version => 1, :latitude => 60 * OldNode::SCALE, :longitude => 30 * OldNode::SCALE)
33       create(:old_node, :node_id => node.id, :version => 2, :latitude => 61 * OldNode::SCALE, :longitude => 31 * OldNode::SCALE)
34
35       get api_node_versions_path(node)
36
37       assert_response :success
38       assert_dom "osm:root", 1 do
39         assert_dom "> node", 2 do |dom_nodes|
40           assert_dom dom_nodes[0], "> @id", node.id.to_s
41           assert_dom dom_nodes[0], "> @version", "1"
42           assert_dom dom_nodes[0], "> @lat", "60.0000000"
43           assert_dom dom_nodes[0], "> @lon", "30.0000000"
44
45           assert_dom dom_nodes[1], "> @id", node.id.to_s
46           assert_dom dom_nodes[1], "> @version", "2"
47           assert_dom dom_nodes[1], "> @lat", "61.0000000"
48           assert_dom dom_nodes[1], "> @lon", "31.0000000"
49         end
50       end
51     end
52
53     ##
54     # test that redacted nodes aren't visible in the history
55     def test_index_redacted_unauthorised
56       node = create(:node, :with_history, :version => 2)
57       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
58
59       get api_node_versions_path(node)
60
61       assert_response :success, "Redaction shouldn't have stopped history working."
62       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
63                  "redacted node #{node.id} version 1 shouldn't be present in the history."
64     end
65
66     def test_index_redacted_normal_user
67       node = create(:node, :with_history, :version => 2)
68       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
69
70       get api_node_versions_path(node), :headers => bearer_authorization_header
71
72       assert_response :success, "Redaction shouldn't have stopped history working."
73       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
74                  "redacted node #{node.id} version 1 shouldn't be present in the history, even when logged in."
75     end
76
77     def test_show
78       node = create(:node, :version => 2)
79       create(:old_node, :node_id => node.id, :version => 1, :latitude => 60 * OldNode::SCALE, :longitude => 30 * OldNode::SCALE)
80       create(:old_node, :node_id => node.id, :version => 2, :latitude => 61 * OldNode::SCALE, :longitude => 31 * OldNode::SCALE)
81
82       get api_node_version_path(node, 1)
83
84       assert_response :success
85       assert_dom "osm:root", 1 do
86         assert_dom "> node", 1 do
87           assert_dom "> @id", node.id.to_s
88           assert_dom "> @version", "1"
89           assert_dom "> @lat", "60.0000000"
90           assert_dom "> @lon", "30.0000000"
91         end
92       end
93
94       get api_node_version_path(node, 2)
95
96       assert_response :success
97       assert_dom "osm:root", 1 do
98         assert_dom "> node", 1 do
99           assert_dom "> @id", node.id.to_s
100           assert_dom "> @version", "2"
101           assert_dom "> @lat", "61.0000000"
102           assert_dom "> @lon", "31.0000000"
103         end
104       end
105     end
106
107     def test_show_not_found
108       check_not_found_id_version(70000, 312344)
109       check_not_found_id_version(-1, -13)
110       check_not_found_id_version(create(:node).id, 24354)
111       check_not_found_id_version(24356, create(:node).version)
112     end
113
114     ##
115     # test that redacted nodes aren't visible, regardless of
116     # authorisation except as moderator...
117     def test_show_redacted_unauthorised
118       node = create(:node, :with_history, :version => 2)
119       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
120
121       get api_node_version_path(node, 1)
122
123       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
124     end
125
126     def test_show_redacted_normal_user
127       node = create(:node, :with_history, :version => 2)
128       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
129
130       get api_node_version_path(node, 1), :headers => bearer_authorization_header
131
132       assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
133     end
134
135     # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
136     def test_lat_lon_xml_format
137       old_node = create(:old_node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
138
139       get api_node_versions_path(old_node.node_id)
140       assert_match(/lat="0.0000400"/, response.body)
141       assert_match(/lon="0.0000800"/, response.body)
142     end
143
144     ##
145     # test the redaction of an old version of a node, while not being
146     # authorised.
147     def test_redact_node_unauthorised
148       node = create(:node, :with_history, :version => 4)
149       node_v3 = node.old_nodes.find_by(:version => 3)
150
151       do_redact_node(node_v3,
152                      create(:redaction))
153       assert_response :unauthorized, "should need to be authenticated to redact."
154     end
155
156     ##
157     # test the redaction of an old version of a node, while being
158     # authorised as a normal user.
159     def test_redact_node_normal_user
160       auth_header = bearer_authorization_header
161
162       node = create(:node, :with_history, :version => 4)
163       node_v3 = node.old_nodes.find_by(:version => 3)
164
165       do_redact_node(node_v3,
166                      create(:redaction),
167                      auth_header)
168       assert_response :forbidden, "should need to be moderator to redact."
169     end
170
171     ##
172     # test that, even as moderator, the current version of a node
173     # can't be redacted.
174     def test_redact_node_current_version
175       auth_header = bearer_authorization_header create(:moderator_user)
176
177       node = create(:node, :with_history, :version => 4)
178       node_v4 = node.old_nodes.find_by(:version => 4)
179
180       do_redact_node(node_v4,
181                      create(:redaction),
182                      auth_header)
183       assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
184     end
185
186     def test_redact_node_by_regular_without_write_redactions_scope
187       auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
188       do_redact_redactable_node(auth_header)
189       assert_response :forbidden, "should need to be moderator to redact."
190     end
191
192     def test_redact_node_by_regular_with_write_redactions_scope
193       auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
194       do_redact_redactable_node(auth_header)
195       assert_response :forbidden, "should need to be moderator to redact."
196     end
197
198     def test_redact_node_by_moderator_without_write_redactions_scope
199       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
200       do_redact_redactable_node(auth_header)
201       assert_response :forbidden, "should need to have write_redactions scope to redact."
202     end
203
204     def test_redact_node_by_moderator_with_write_redactions_scope
205       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
206       do_redact_redactable_node(auth_header)
207       assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
208     end
209
210     ##
211     # test the redaction of an old version of a node, while being
212     # authorised as a moderator.
213     def test_redact_node_moderator
214       node = create(:node, :with_history, :version => 4)
215       node_v3 = node.old_nodes.find_by(:version => 3)
216       auth_header = bearer_authorization_header create(:moderator_user)
217
218       do_redact_node(node_v3, create(:redaction), auth_header)
219       assert_response :success, "should be OK to redact old version as moderator."
220
221       # check moderator can still see the redacted data, when passing
222       # the appropriate flag
223       get api_node_version_path(node_v3.node_id, node_v3.version), :headers => auth_header
224       assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
225       get api_node_version_path(node_v3.node_id, node_v3.version, :show_redactions => "true"), :headers => auth_header
226       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
227
228       # and when accessed via history
229       get api_node_versions_path(node)
230       assert_response :success, "Redaction shouldn't have stopped history working."
231       assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
232                     "node #{node_v3.node_id} version #{node_v3.version} should not be present in the history for moderators when not passing flag."
233       get api_node_versions_path(node, :show_redactions => "true"), :headers => auth_header
234       assert_response :success, "Redaction shouldn't have stopped history working."
235       assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 1,
236                     "node #{node_v3.node_id} version #{node_v3.version} should still be present in the history for moderators when passing flag."
237     end
238
239     # testing that if the moderator drops auth, he can't see the
240     # redacted stuff any more.
241     def test_redact_node_is_redacted
242       node = create(:node, :with_history, :version => 4)
243       node_v3 = node.old_nodes.find_by(:version => 3)
244       auth_header = bearer_authorization_header create(:moderator_user)
245
246       do_redact_node(node_v3, create(:redaction), auth_header)
247       assert_response :success, "should be OK to redact old version as moderator."
248
249       # re-auth as non-moderator
250       auth_header = bearer_authorization_header
251
252       # check can't see the redacted data
253       get api_node_version_path(node_v3.node_id, node_v3.version), :headers => auth_header
254       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
255
256       # and when accessed via history
257       get api_node_versions_path(node), :headers => auth_header
258       assert_response :success, "Redaction shouldn't have stopped history working."
259       assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
260                     "redacted node #{node_v3.node_id} version #{node_v3.version} shouldn't be present in the history."
261     end
262
263     ##
264     # test the unredaction of an old version of a node, while not being
265     # authorised.
266     def test_unredact_node_unauthorised
267       node = create(:node, :with_history, :version => 2)
268       node_v1 = node.old_nodes.find_by(:version => 1)
269       node_v1.redact!(create(:redaction))
270
271       post node_version_redact_path(node_v1.node_id, node_v1.version)
272       assert_response :unauthorized, "should need to be authenticated to unredact."
273     end
274
275     ##
276     # test the unredaction of an old version of a node, while being
277     # authorised as a normal user.
278     def test_unredact_node_normal_user
279       user = create(:user)
280       node = create(:node, :with_history, :version => 2)
281       node_v1 = node.old_nodes.find_by(:version => 1)
282       node_v1.redact!(create(:redaction))
283
284       auth_header = bearer_authorization_header user
285
286       post node_version_redact_path(node_v1.node_id, node_v1.version), :headers => auth_header
287       assert_response :forbidden, "should need to be moderator to unredact."
288     end
289
290     ##
291     # test the unredaction of an old version of a node, while being
292     # authorised as a moderator.
293     def test_unredact_node_moderator
294       moderator_user = create(:moderator_user)
295       node = create(:node, :with_history, :version => 2)
296       node_v1 = node.old_nodes.find_by(:version => 1)
297       node_v1.redact!(create(:redaction))
298
299       auth_header = bearer_authorization_header moderator_user
300
301       post node_version_redact_path(node_v1.node_id, node_v1.version), :headers => auth_header
302       assert_response :success, "should be OK to unredact old version as moderator."
303
304       # check moderator can now see the redacted data, when not
305       # passing the aspecial flag
306       get api_node_version_path(node_v1.node_id, node_v1.version), :headers => auth_header
307       assert_response :success, "After unredaction, node should not be gone for moderator."
308
309       # and when accessed via history
310       get api_node_versions_path(node)
311       assert_response :success, "Unredaction shouldn't have stopped history working."
312       assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
313                     "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for moderators without passing flag."
314
315       auth_header = bearer_authorization_header
316
317       # check normal user can now see the redacted data
318       get api_node_version_path(node_v1.node_id, node_v1.version), :headers => auth_header
319       assert_response :success, "After unredaction, node should be visible to normal users."
320
321       # and when accessed via history
322       get api_node_versions_path(node)
323       assert_response :success, "Unredaction shouldn't have stopped history working."
324       assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
325                     "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for normal users without passing flag."
326     end
327
328     private
329
330     def do_redact_redactable_node(headers = {})
331       node = create(:node, :with_history, :version => 4)
332       node_v3 = node.old_nodes.find_by(:version => 3)
333       do_redact_node(node_v3, create(:redaction), headers)
334     end
335
336     def do_redact_node(node, redaction, headers = {})
337       get api_node_version_path(node.node_id, node.version), :headers => headers
338       assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
339
340       # now redact it
341       post node_version_redact_path(node.node_id, node.version), :params => { :redaction => redaction.id }, :headers => headers
342     end
343
344     def check_not_found_id_version(id, version)
345       get api_node_version_path(id, version)
346       assert_response :not_found
347     rescue ActionController::UrlGenerationError => e
348       assert_match(/No route matches/, e.to_s)
349     end
350   end
351 end