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