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