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."
65 get api_node_versions_path(node, :show_redactions => "true")
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."
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))
76 get api_node_versions_path(node), :headers => bearer_authorization_header
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."
82 get api_node_versions_path(node, :show_redactions => "true"), :headers => bearer_authorization_header
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."
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)
94 get api_node_versions_path(node), :headers => auth_header
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."
100 get api_node_versions_path(node, :show_redactions => "true"), :headers => auth_header
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."
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)
112 get api_node_version_path(node, 1)
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"
124 get api_node_version_path(node, 2)
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"
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)
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))
151 get api_node_version_path(node, 1)
153 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
155 get api_node_version_path(node, 1, :show_redactions => "true")
157 assert_response :forbidden, "Redacted node shouldn't be visible via the version API when passing flag."
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))
164 get api_node_version_path(node, 1), :headers => bearer_authorization_header
166 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
168 get api_node_version_path(node, 1, :show_redactions => "true"), :headers => bearer_authorization_header
170 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in and passing flag."
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)
178 get api_node_version_path(node, 1), :headers => auth_header
180 assert_response :forbidden, "Redacted node should be gone for moderator, when flag not passed."
182 get api_node_version_path(node, 1, :show_redactions => "true"), :headers => auth_header
184 assert_response :success, "Redacted node should not be gone for moderator, when flag passed."
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)
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)
197 # test the redaction of an old version of a node, while not being
199 def test_redact_node_unauthorised
200 node = create(:node, :with_history, :version => 4)
201 node_v3 = node.old_nodes.find_by(:version => 3)
203 do_redact_node(node_v3,
205 assert_response :unauthorized, "should need to be authenticated to redact."
209 # test the redaction of an old version of a node, while being
210 # authorised as a normal user.
211 def test_redact_node_normal_user
212 auth_header = bearer_authorization_header
214 node = create(:node, :with_history, :version => 4)
215 node_v3 = node.old_nodes.find_by(:version => 3)
217 do_redact_node(node_v3,
220 assert_response :forbidden, "should need to be moderator to redact."
224 # test that, even as moderator, the current version of a node
226 def test_redact_node_current_version
227 auth_header = bearer_authorization_header create(:moderator_user)
229 node = create(:node, :with_history, :version => 4)
230 node_v4 = node.old_nodes.find_by(:version => 4)
232 do_redact_node(node_v4,
235 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
238 def test_redact_node_by_regular_without_write_redactions_scope
239 auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
240 do_redact_redactable_node(auth_header)
241 assert_response :forbidden, "should need to be moderator to redact."
244 def test_redact_node_by_regular_with_write_redactions_scope
245 auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
246 do_redact_redactable_node(auth_header)
247 assert_response :forbidden, "should need to be moderator to redact."
250 def test_redact_node_by_moderator_without_write_redactions_scope
251 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
252 do_redact_redactable_node(auth_header)
253 assert_response :forbidden, "should need to have write_redactions scope to redact."
256 def test_redact_node_by_moderator_with_write_redactions_scope
257 auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
258 do_redact_redactable_node(auth_header)
259 assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
263 # test the redaction of an old version of a node, while being
264 # authorised as a moderator.
265 def test_redact_node_moderator
266 node = create(:node, :with_history, :version => 4)
267 node_v3 = node.old_nodes.find_by(:version => 3)
268 auth_header = bearer_authorization_header create(:moderator_user)
270 do_redact_node(node_v3, create(:redaction), auth_header)
272 assert_response :success, "should be OK to redact old version as moderator."
273 assert_predicate node_v3.reload, :redacted?
277 # test the unredaction of an old version of a node, while not being
279 def test_unredact_node_unauthorised
280 node = create(:node, :with_history, :version => 2)
281 node_v1 = node.old_nodes.find_by(:version => 1)
282 node_v1.redact!(create(:redaction))
284 post node_version_redact_path(node_v1.node_id, node_v1.version)
285 assert_response :unauthorized, "should need to be authenticated to unredact."
289 # test the unredaction of an old version of a node, while being
290 # authorised as a normal user.
291 def test_unredact_node_normal_user
293 node = create(:node, :with_history, :version => 2)
294 node_v1 = node.old_nodes.find_by(:version => 1)
295 node_v1.redact!(create(:redaction))
297 auth_header = bearer_authorization_header user
299 post node_version_redact_path(node_v1.node_id, node_v1.version), :headers => auth_header
300 assert_response :forbidden, "should need to be moderator to unredact."
304 # test the unredaction of an old version of a node, while being
305 # authorised as a moderator.
306 def test_unredact_node_moderator
307 moderator_user = create(:moderator_user)
308 node = create(:node, :with_history, :version => 2)
309 node_v1 = node.old_nodes.find_by(:version => 1)
310 node_v1.redact!(create(:redaction))
312 auth_header = bearer_authorization_header moderator_user
314 post node_version_redact_path(node_v1.node_id, node_v1.version), :headers => auth_header
315 assert_response :success, "should be OK to unredact old version as moderator."
317 # check moderator can now see the redacted data, when not
318 # passing the aspecial flag
319 get api_node_version_path(node_v1.node_id, node_v1.version), :headers => auth_header
320 assert_response :success, "After unredaction, node should not be gone for moderator."
322 # and when accessed via history
323 get api_node_versions_path(node)
324 assert_response :success, "Unredaction shouldn't have stopped history working."
325 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
326 "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for moderators without passing flag."
328 auth_header = bearer_authorization_header
330 # check normal user can now see the redacted data
331 get api_node_version_path(node_v1.node_id, node_v1.version), :headers => auth_header
332 assert_response :success, "After unredaction, node should be visible to normal users."
334 # and when accessed via history
335 get api_node_versions_path(node)
336 assert_response :success, "Unredaction shouldn't have stopped history working."
337 assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
338 "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for normal users without passing flag."
343 def do_redact_redactable_node(headers = {})
344 node = create(:node, :with_history, :version => 4)
345 node_v3 = node.old_nodes.find_by(:version => 3)
346 do_redact_node(node_v3, create(:redaction), headers)
349 def do_redact_node(node, redaction, headers = {})
350 get api_node_version_path(node.node_id, node.version), :headers => headers
351 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
354 post node_version_redact_path(node.node_id, node.version), :params => { :redaction => redaction.id }, :headers => headers
357 def check_not_found_id_version(id, version)
358 get api_node_version_path(id, version)
359 assert_response :not_found
360 rescue ActionController::UrlGenerationError => e
361 assert_match(/No route matches/, e.to_s)