2 require "old_node_controller"
4 class OldNodeControllerTest < ActionController::TestCase
12 # test all routes which lead to this controller
15 { :path => "/api/0.6/node/1/history", :method => :get },
16 { :controller => "old_node", :action => "history", :id => "1" }
19 { :path => "/api/0.6/node/1/2", :method => :get },
20 { :controller => "old_node", :action => "version", :id => "1", :version => "2" }
23 { :path => "/api/0.6/node/1/2/redact", :method => :post },
24 { :controller => "old_node", :action => "redact", :id => "1", :version => "2" }
29 # test the version call by submitting several revisions of a new node
30 # to the API and ensuring that later calls to version return the
31 # matching versions of the object.
34 # FIXME: Move this test to being an integration test since it spans multiple controllers
36 ## First try this with a non-public user
37 basic_authorization(users(:normal_user).email, "test")
39 # setup a simple XML node
40 xml_doc = current_nodes(:visible_node).to_xml
41 xml_node = xml_doc.find("//osm/node").first
42 nodeid = current_nodes(:visible_node).id
44 # keep a hash of the versions => string, as we'll need something
45 # to test against later
48 # save a version for later checking
49 versions[xml_node["version"]] = xml_doc.to_s
51 # randomly move the node about
53 # move the node somewhere else
54 xml_node["lat"] = precision(rand * 180 - 90).to_s
55 xml_node["lon"] = precision(rand * 360 - 180).to_s
56 with_controller(NodeController.new) do
58 put :update, :id => nodeid
59 assert_response :forbidden, "Should have rejected node update"
60 xml_node["version"] = @response.body.to_s
62 # save a version for later checking
63 versions[xml_node["version"]] = xml_doc.to_s
66 # add a bunch of random tags
68 xml_tag = XML::Node.new("tag")
69 xml_tag["k"] = random_string
70 xml_tag["v"] = random_string
72 with_controller(NodeController.new) do
74 put :update, :id => nodeid
75 assert_response :forbidden,
76 "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
77 xml_node["version"] = @response.body.to_s
79 # save a version for later checking
80 versions[xml_node["version"]] = xml_doc.to_s
83 # probably should check that they didn't get written to the database
85 ## Now do it with the public user
86 basic_authorization(users(:public_user).email, "test")
88 # setup a simple XML node
89 create_list(:node_tag, 2, :node => current_nodes(:node_with_versions))
90 xml_doc = current_nodes(:node_with_versions).to_xml
91 xml_node = xml_doc.find("//osm/node").first
92 nodeid = current_nodes(:node_with_versions).id
94 # Ensure that the current tags are propagated to the history too
95 propagate_tags(current_nodes(:node_with_versions), nodes(:node_with_versions_v4))
97 # keep a hash of the versions => string, as we'll need something
98 # to test against later
101 # save a version for later checking
102 versions[xml_node["version"]] = xml_doc.to_s
104 # randomly move the node about
106 # move the node somewhere else
107 xml_node["lat"] = precision(rand * 180 - 90).to_s
108 xml_node["lon"] = precision(rand * 360 - 180).to_s
109 with_controller(NodeController.new) do
111 put :update, :id => nodeid
112 assert_response :success
113 xml_node["version"] = @response.body.to_s
115 # save a version for later checking
116 versions[xml_node["version"]] = xml_doc.to_s
119 # add a bunch of random tags
121 xml_tag = XML::Node.new("tag")
122 xml_tag["k"] = random_string
123 xml_tag["v"] = random_string
125 with_controller(NodeController.new) do
127 put :update, :id => nodeid
128 assert_response :success,
129 "couldn't update node #{nodeid} (#{@response.body})"
130 xml_node["version"] = @response.body.to_s
132 # save a version for later checking
133 versions[xml_node["version"]] = xml_doc.to_s
136 # check all the versions
137 versions.keys.each do |key|
138 get :version, :id => nodeid, :version => key.to_i
140 assert_response :success,
141 "couldn't get version #{key.to_i} of node #{nodeid}"
143 check_node = Node.from_xml(versions[key])
144 api_node = Node.from_xml(@response.body.to_s)
146 assert_nodes_are_equal check_node, api_node
150 def test_not_found_version
151 check_not_found_id_version(70000, 312344)
152 check_not_found_id_version(-1, -13)
153 check_not_found_id_version(nodes(:visible_node).id, 24354)
154 check_not_found_id_version(24356, nodes(:visible_node).version)
157 def check_not_found_id_version(id, version)
158 get :version, :id => id, :version => version
159 assert_response :not_found
160 rescue ActionController::UrlGenerationError => ex
161 assert_match /No route matches/, ex.to_s
165 # Test that getting the current version is identical to picking
166 # that version with the version URI call.
167 def test_current_version
168 create(:node_tag, :node => current_nodes(:visible_node))
169 create(:node_tag, :node => current_nodes(:used_node_1))
170 create(:node_tag, :node => current_nodes(:used_node_2))
171 create(:node_tag, :node => current_nodes(:node_used_by_relationship))
172 create(:node_tag, :node => current_nodes(:node_with_versions))
173 propagate_tags(current_nodes(:visible_node), nodes(:visible_node))
174 propagate_tags(current_nodes(:used_node_1), nodes(:used_node_1))
175 propagate_tags(current_nodes(:used_node_2), nodes(:used_node_2))
176 propagate_tags(current_nodes(:node_used_by_relationship), nodes(:node_used_by_relationship))
177 propagate_tags(current_nodes(:node_with_versions), nodes(:node_with_versions_v4))
179 check_current_version(current_nodes(:visible_node))
180 check_current_version(current_nodes(:used_node_1))
181 check_current_version(current_nodes(:used_node_2))
182 check_current_version(current_nodes(:node_used_by_relationship))
183 check_current_version(current_nodes(:node_with_versions))
187 # test the redaction of an old version of a node, while not being
189 def test_redact_node_unauthorised
190 do_redact_node(nodes(:node_with_versions_v3),
192 assert_response :unauthorized, "should need to be authenticated to redact."
196 # test the redaction of an old version of a node, while being
197 # authorised as a normal user.
198 def test_redact_node_normal_user
200 basic_authorization(user.email, "test")
202 do_redact_node(nodes(:node_with_versions_v3),
203 create(:redaction, :user => user))
204 assert_response :forbidden, "should need to be moderator to redact."
208 # test that, even as moderator, the current version of a node
210 def test_redact_node_current_version
211 moderator_user = create(:moderator_user)
212 basic_authorization(moderator_user.email, "test")
214 do_redact_node(nodes(:node_with_versions_v4),
215 create(:redaction, :user => moderator_user))
216 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
220 # test that redacted nodes aren't visible, regardless of
221 # authorisation except as moderator...
222 def test_version_redacted
223 node = nodes(:redacted_node_redacted_version)
225 get :version, :id => node.node_id, :version => node.version
226 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
228 # not even to a logged-in user
229 basic_authorization(users(:public_user).email, "test")
230 get :version, :id => node.node_id, :version => node.version
231 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
235 # test that redacted nodes aren't visible in the history
236 def test_history_redacted
237 node = nodes(:redacted_node_redacted_version)
239 get :history, :id => node.node_id
240 assert_response :success, "Redaction shouldn't have stopped history working."
241 assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history."
243 # not even to a logged-in user
244 basic_authorization(users(:public_user).email, "test")
245 get :history, :id => node.node_id
246 assert_response :success, "Redaction shouldn't have stopped history working."
247 assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history, even when logged in."
251 # test the redaction of an old version of a node, while being
252 # authorised as a moderator.
253 def test_redact_node_moderator
254 moderator_user = create(:moderator_user)
255 node = nodes(:node_with_versions_v3)
256 basic_authorization(moderator_user.email, "test")
258 do_redact_node(node, create(:redaction, :user => moderator_user))
259 assert_response :success, "should be OK to redact old version as moderator."
261 # check moderator can still see the redacted data, when passing
262 # the appropriate flag
263 get :version, :id => node.node_id, :version => node.version
264 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
265 get :version, :id => node.node_id, :version => node.version, :show_redactions => "true"
266 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
268 # and when accessed via history
269 get :history, :id => node.node_id
270 assert_response :success, "Redaction shouldn't have stopped history working."
271 assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 0, "node #{node.node_id} version #{node.version} should not be present in the history for moderators when not passing flag."
272 get :history, :id => node.node_id, :show_redactions => "true"
273 assert_response :success, "Redaction shouldn't have stopped history working."
274 assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 1, "node #{node.node_id} version #{node.version} should still be present in the history for moderators when passing flag."
277 # testing that if the moderator drops auth, he can't see the
278 # redacted stuff any more.
279 def test_redact_node_is_redacted
280 moderator_user = create(:moderator_user)
281 node = nodes(:node_with_versions_v3)
282 basic_authorization(moderator_user.email, "test")
284 do_redact_node(node, create(:redaction, :user => moderator_user))
285 assert_response :success, "should be OK to redact old version as moderator."
287 # re-auth as non-moderator
288 basic_authorization(create(:user).email, "test")
290 # check can't see the redacted data
291 get :version, :id => node.node_id, :version => node.version
292 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
294 # and when accessed via history
295 get :history, :id => node.node_id
296 assert_response :success, "Redaction shouldn't have stopped history working."
297 assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history."
301 # test the unredaction of an old version of a node, while not being
303 def test_unredact_node_unauthorised
304 node = nodes(:redacted_node_redacted_version)
306 post :redact, :id => node.node_id, :version => node.version
307 assert_response :unauthorized, "should need to be authenticated to unredact."
311 # test the unredaction of an old version of a node, while being
312 # authorised as a normal user.
313 def test_unredact_node_normal_user
315 node = nodes(:redacted_node_redacted_version)
316 basic_authorization(user.email, "test")
318 post :redact, :id => node.node_id, :version => node.version
319 assert_response :forbidden, "should need to be moderator to unredact."
323 # test the unredaction of an old version of a node, while being
324 # authorised as a moderator.
325 def test_unredact_node_moderator
326 moderator_user = create(:moderator_user)
327 node = nodes(:redacted_node_redacted_version)
328 basic_authorization(moderator_user.email, "test")
330 post :redact, :id => node.node_id, :version => node.version
331 assert_response :success, "should be OK to redact old version as moderator."
333 # check moderator can now see the redacted data, when not
334 # passing the aspecial flag
335 get :version, :id => node.node_id, :version => node.version
336 assert_response :success, "After unredaction, node should not be gone for moderator."
338 # and when accessed via history
339 get :history, :id => node.node_id
340 assert_response :success, "Unredaction shouldn't have stopped history working."
341 assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 1, "node #{node.node_id} version #{node.version} should now be present in the history for moderators without passing flag."
343 basic_authorization(create(:user).email, "test")
345 # check normal user can now see the redacted data
346 get :version, :id => node.node_id, :version => node.version
347 assert_response :success, "After unredaction, node should be visible to normal users."
349 # and when accessed via history
350 get :history, :id => node.node_id
351 assert_response :success, "Unredaction shouldn't have stopped history working."
352 assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 1, "node #{node.node_id} version #{node.version} should now be present in the history for normal users without passing flag."
357 def do_redact_node(node, redaction)
358 get :version, :id => node.node_id, :version => node.version
359 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
362 post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
365 def check_current_version(node_id)
366 # get the current version of the node
367 current_node = with_controller(NodeController.new) do
368 get :read, :id => node_id
369 assert_response :success, "cant get current node #{node_id}"
370 Node.from_xml(@response.body)
372 assert_not_nil current_node, "getting node #{node_id} returned nil"
374 # get the "old" version of the node from the old_node interface
375 get :version, :id => node_id, :version => current_node.version
376 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
377 old_node = Node.from_xml(@response.body)
379 # check the nodes are the same
380 assert_nodes_are_equal current_node, old_node
384 # returns a 16 character long string with some nasty characters in it.
385 # this ought to stress-test the tag handling as well as the versioning.
387 letters = [["!", '"', "$", "&", ";", "@"],
390 ("0".."9").to_a].flatten
391 (1..16).map { |_i| letters[rand(letters.length)] }.join
395 # truncate a floating point number to the scale that it is stored in
396 # the database. otherwise rounding errors can produce failing unit
397 # tests when they shouldn't.
399 (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
402 def propagate_tags(node, old_node)
403 node.tags.each do |k, v|
404 create(:old_node_tag, :old_node => old_node, :k => k, :v => v)