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