4 class OldNodesControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/node/1/history", :method => :get },
10 { :controller => "api/old_nodes", :action => "index", :node_id => "1" }
13 { :path => "/api/0.6/node/1/history.json", :method => :get },
14 { :controller => "api/old_nodes", :action => "index", :node_id => "1", :format => "json" }
17 { :path => "/api/0.6/node/1/2", :method => :get },
18 { :controller => "api/old_nodes", :action => "show", :node_id => "1", :version => "2" }
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" }
25 { :path => "/api/0.6/node/1/2/redact", :method => :post },
26 { :controller => "api/old_nodes", :action => "redact", :node_id => "1", :version => "2" }
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)
35 get api_node_versions_path(node)
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"
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"
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))
59 get api_node_versions_path(node)
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."
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))
70 get api_node_versions_path(node), :headers => bearer_authorization_header
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."
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)
82 get api_node_version_path(node, 1)
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"
94 get api_node_version_path(node, 2)
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"
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)
115 # test that redacted nodes aren't visible, regardless of
116 # authorisation except as moderator...
117 def test_show_redacted
118 node = create(:node, :with_history, :version => 2)
119 node_v1 = node.old_nodes.find_by(:version => 1)
120 node_v1.redact!(create(:redaction))
122 get api_node_version_path(node_v1.node_id, node_v1.version)
123 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
125 # not even to a logged-in user
126 auth_header = bearer_authorization_header
127 get api_node_version_path(node_v1.node_id, node_v1.version), :headers => auth_header
128 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
131 # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
132 def test_lat_lon_xml_format
133 old_node = create(:old_node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
135 get api_node_versions_path(old_node.node_id)
136 assert_match(/lat="0.0000400"/, response.body)
137 assert_match(/lon="0.0000800"/, response.body)
141 # test the redaction of an old version of a node, while not being
143 def test_redact_node_unauthorised
144 node = create(:node, :with_history, :version => 4)
145 node_v3 = node.old_nodes.find_by(:version => 3)
147 do_redact_node(node_v3,
149 assert_response :unauthorized, "should need to be authenticated to redact."
153 # test the redaction of an old version of a node, while being
154 # authorised as a normal user.
155 def test_redact_node_normal_user
156 auth_header = bearer_authorization_header
158 node = create(:node, :with_history, :version => 4)
159 node_v3 = node.old_nodes.find_by(:version => 3)
161 do_redact_node(node_v3,
164 assert_response :forbidden, "should need to be moderator to redact."
168 # test that, even as moderator, the current version of a node
170 def test_redact_node_current_version
171 auth_header = bearer_authorization_header create(:moderator_user)
173 node = create(:node, :with_history, :version => 4)
174 node_v4 = node.old_nodes.find_by(:version => 4)
176 do_redact_node(node_v4,
179 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
182 def test_redact_node_by_regular_without_write_redactions_scope
183 auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
184 do_redact_redactable_node(auth_header)
185 assert_response :forbidden, "should need to be moderator to redact."
188 def test_redact_node_by_regular_with_write_redactions_scope
189 auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
190 do_redact_redactable_node(auth_header)
191 assert_response :forbidden, "should need to be moderator to redact."
194 def test_redact_node_by_moderator_without_write_redactions_scope
195 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
196 do_redact_redactable_node(auth_header)
197 assert_response :forbidden, "should need to have write_redactions scope to redact."
200 def test_redact_node_by_moderator_with_write_redactions_scope
201 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
202 do_redact_redactable_node(auth_header)
203 assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
207 # test the redaction of an old version of a node, while being
208 # authorised as a moderator.
209 def test_redact_node_moderator
210 node = create(:node, :with_history, :version => 4)
211 node_v3 = node.old_nodes.find_by(:version => 3)
212 auth_header = bearer_authorization_header create(:moderator_user)
214 do_redact_node(node_v3, create(:redaction), auth_header)
215 assert_response :success, "should be OK to redact old version as moderator."
217 # check moderator can still see the redacted data, when passing
218 # the appropriate flag
219 get api_node_version_path(node_v3.node_id, node_v3.version), :headers => auth_header
220 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
221 get api_node_version_path(node_v3.node_id, node_v3.version, :show_redactions => "true"), :headers => auth_header
222 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
224 # and when accessed via history
225 get api_node_versions_path(node)
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}']", 0,
228 "node #{node_v3.node_id} version #{node_v3.version} should not be present in the history for moderators when not passing flag."
229 get api_node_versions_path(node, :show_redactions => "true"), :headers => auth_header
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}']", 1,
232 "node #{node_v3.node_id} version #{node_v3.version} should still be present in the history for moderators when passing flag."
235 # testing that if the moderator drops auth, he can't see the
236 # redacted stuff any more.
237 def test_redact_node_is_redacted
238 node = create(:node, :with_history, :version => 4)
239 node_v3 = node.old_nodes.find_by(:version => 3)
240 auth_header = bearer_authorization_header create(:moderator_user)
242 do_redact_node(node_v3, create(:redaction), auth_header)
243 assert_response :success, "should be OK to redact old version as moderator."
245 # re-auth as non-moderator
246 auth_header = bearer_authorization_header
248 # check can't see the redacted data
249 get api_node_version_path(node_v3.node_id, node_v3.version), :headers => auth_header
250 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
252 # and when accessed via history
253 get api_node_versions_path(node), :headers => auth_header
254 assert_response :success, "Redaction shouldn't have stopped history working."
255 assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
256 "redacted node #{node_v3.node_id} version #{node_v3.version} shouldn't be present in the history."
260 # test the unredaction of an old version of a node, while not being
262 def test_unredact_node_unauthorised
263 node = create(:node, :with_history, :version => 2)
264 node_v1 = node.old_nodes.find_by(:version => 1)
265 node_v1.redact!(create(:redaction))
267 post node_version_redact_path(node_v1.node_id, node_v1.version)
268 assert_response :unauthorized, "should need to be authenticated to unredact."
272 # test the unredaction of an old version of a node, while being
273 # authorised as a normal user.
274 def test_unredact_node_normal_user
276 node = create(:node, :with_history, :version => 2)
277 node_v1 = node.old_nodes.find_by(:version => 1)
278 node_v1.redact!(create(:redaction))
280 auth_header = bearer_authorization_header user
282 post node_version_redact_path(node_v1.node_id, node_v1.version), :headers => auth_header
283 assert_response :forbidden, "should need to be moderator to unredact."
287 # test the unredaction of an old version of a node, while being
288 # authorised as a moderator.
289 def test_unredact_node_moderator
290 moderator_user = create(:moderator_user)
291 node = create(:node, :with_history, :version => 2)
292 node_v1 = node.old_nodes.find_by(:version => 1)
293 node_v1.redact!(create(:redaction))
295 auth_header = bearer_authorization_header moderator_user
297 post node_version_redact_path(node_v1.node_id, node_v1.version), :headers => auth_header
298 assert_response :success, "should be OK to unredact old version as moderator."
300 # check moderator can now see the redacted data, when not
301 # passing the aspecial flag
302 get api_node_version_path(node_v1.node_id, node_v1.version), :headers => auth_header
303 assert_response :success, "After unredaction, node should not be gone for moderator."
305 # and when accessed via history
306 get api_node_versions_path(node)
307 assert_response :success, "Unredaction shouldn't have stopped history working."
308 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
309 "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for moderators without passing flag."
311 auth_header = bearer_authorization_header
313 # check normal user can now see the redacted data
314 get api_node_version_path(node_v1.node_id, node_v1.version), :headers => auth_header
315 assert_response :success, "After unredaction, node should be visible to normal users."
317 # and when accessed via history
318 get api_node_versions_path(node)
319 assert_response :success, "Unredaction shouldn't have stopped history working."
320 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
321 "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for normal users without passing flag."
326 def do_redact_redactable_node(headers = {})
327 node = create(:node, :with_history, :version => 4)
328 node_v3 = node.old_nodes.find_by(:version => 3)
329 do_redact_node(node_v3, create(:redaction), headers)
332 def do_redact_node(node, redaction, headers = {})
333 get api_node_version_path(node.node_id, node.version), :headers => headers
334 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
337 post node_version_redact_path(node.node_id, node.version), :params => { :redaction => redaction.id }, :headers => headers
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)