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
199 basic_authorization(create(:user).email, "test")
201 do_redact_node(nodes(:node_with_versions_v3),
203 assert_response :forbidden, "should need to be moderator to redact."
207 # test that, even as moderator, the current version of a node
209 def test_redact_node_current_version
210 basic_authorization(create(:moderator_user).email, "test")
212 do_redact_node(nodes(:node_with_versions_v4),
214 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
218 # test that redacted nodes aren't visible, regardless of
219 # authorisation except as moderator...
220 def test_version_redacted
221 node = nodes(:redacted_node_redacted_version)
223 get :version, :id => node.node_id, :version => node.version
224 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
226 # not even to a logged-in user
227 basic_authorization(users(:public_user).email, "test")
228 get :version, :id => node.node_id, :version => node.version
229 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
233 # test that redacted nodes aren't visible in the history
234 def test_history_redacted
235 node = nodes(:redacted_node_redacted_version)
237 get :history, :id => node.node_id
238 assert_response :success, "Redaction shouldn't have stopped history working."
239 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."
241 # not even to a logged-in user
242 basic_authorization(users(:public_user).email, "test")
243 get :history, :id => node.node_id
244 assert_response :success, "Redaction shouldn't have stopped history working."
245 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."
249 # test the redaction of an old version of a node, while being
250 # authorised as a moderator.
251 def test_redact_node_moderator
252 node = nodes(:node_with_versions_v3)
253 basic_authorization(create(:moderator_user).email, "test")
255 do_redact_node(node, create(:redaction))
256 assert_response :success, "should be OK to redact old version as moderator."
258 # check moderator can still see the redacted data, when passing
259 # the appropriate flag
260 get :version, :id => node.node_id, :version => node.version
261 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
262 get :version, :id => node.node_id, :version => node.version, :show_redactions => "true"
263 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
265 # and when accessed via history
266 get :history, :id => node.node_id
267 assert_response :success, "Redaction shouldn't have stopped history working."
268 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."
269 get :history, :id => node.node_id, :show_redactions => "true"
270 assert_response :success, "Redaction shouldn't have stopped history working."
271 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."
274 # testing that if the moderator drops auth, he can't see the
275 # redacted stuff any more.
276 def test_redact_node_is_redacted
277 node = nodes(:node_with_versions_v3)
278 basic_authorization(create(:moderator_user).email, "test")
280 do_redact_node(node, create(:redaction))
281 assert_response :success, "should be OK to redact old version as moderator."
283 # re-auth as non-moderator
284 basic_authorization(create(:user).email, "test")
286 # check can't see the redacted data
287 get :version, :id => node.node_id, :version => node.version
288 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
290 # and when accessed via history
291 get :history, :id => node.node_id
292 assert_response :success, "Redaction shouldn't have stopped history working."
293 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."
297 # test the unredaction of an old version of a node, while not being
299 def test_unredact_node_unauthorised
300 node = nodes(:redacted_node_redacted_version)
302 post :redact, :id => node.node_id, :version => node.version
303 assert_response :unauthorized, "should need to be authenticated to unredact."
307 # test the unredaction of an old version of a node, while being
308 # authorised as a normal user.
309 def test_unredact_node_normal_user
311 node = nodes(:redacted_node_redacted_version)
312 basic_authorization(user.email, "test")
314 post :redact, :id => node.node_id, :version => node.version
315 assert_response :forbidden, "should need to be moderator to unredact."
319 # test the unredaction of an old version of a node, while being
320 # authorised as a moderator.
321 def test_unredact_node_moderator
322 moderator_user = create(:moderator_user)
323 node = nodes(:redacted_node_redacted_version)
324 basic_authorization(moderator_user.email, "test")
326 post :redact, :id => node.node_id, :version => node.version
327 assert_response :success, "should be OK to redact old version as moderator."
329 # check moderator can now see the redacted data, when not
330 # passing the aspecial flag
331 get :version, :id => node.node_id, :version => node.version
332 assert_response :success, "After unredaction, node should not be gone for moderator."
334 # and when accessed via history
335 get :history, :id => node.node_id
336 assert_response :success, "Unredaction shouldn't have stopped history working."
337 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."
339 basic_authorization(create(:user).email, "test")
341 # check normal user can now see the redacted data
342 get :version, :id => node.node_id, :version => node.version
343 assert_response :success, "After unredaction, node should be visible to normal users."
345 # and when accessed via history
346 get :history, :id => node.node_id
347 assert_response :success, "Unredaction shouldn't have stopped history working."
348 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."
353 def do_redact_node(node, redaction)
354 get :version, :id => node.node_id, :version => node.version
355 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
358 post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
361 def check_current_version(node_id)
362 # get the current version of the node
363 current_node = with_controller(NodeController.new) do
364 get :read, :id => node_id
365 assert_response :success, "cant get current node #{node_id}"
366 Node.from_xml(@response.body)
368 assert_not_nil current_node, "getting node #{node_id} returned nil"
370 # get the "old" version of the node from the old_node interface
371 get :version, :id => node_id, :version => current_node.version
372 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
373 old_node = Node.from_xml(@response.body)
375 # check the nodes are the same
376 assert_nodes_are_equal current_node, old_node
380 # returns a 16 character long string with some nasty characters in it.
381 # this ought to stress-test the tag handling as well as the versioning.
383 letters = [["!", '"', "$", "&", ";", "@"],
386 ("0".."9").to_a].flatten
387 (1..16).map { |_i| letters[rand(letters.length)] }.join
391 # truncate a floating point number to the scale that it is stored in
392 # the database. otherwise rounding errors can produce failing unit
393 # tests when they shouldn't.
395 (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
398 def propagate_tags(node, old_node)
399 node.tags.each do |k, v|
400 create(:old_node_tag, :old_node => old_node, :k => k, :v => v)