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