1 require File.dirname(__FILE__) + '/../test_helper'
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" }
25 # test the version call by submitting several revisions of a new node
26 # to the API and ensuring that later calls to version return the
27 # matching versions of the object.
30 # FIXME Move this test to being an integration test since it spans multiple controllers
32 ## First try this with a non-public user
33 basic_authorization(users(:normal_user).email, "test")
34 changeset_id = changesets(:normal_user_first_change).id
36 # setup a simple XML node
37 xml_doc = current_nodes(:visible_node).to_xml
38 xml_node = xml_doc.find("//osm/node").first
39 nodeid = current_nodes(:visible_node).id
41 # keep a hash of the versions => string, as we'll need something
42 # to test against later
45 # save a version for later checking
46 versions[xml_node['version']] = xml_doc.to_s
48 # randomly move the node about
50 # move the node somewhere else
51 xml_node['lat'] = precision(rand * 180 - 90).to_s
52 xml_node['lon'] = precision(rand * 360 - 180).to_s
53 with_controller(NodeController.new) do
55 put :update, :id => nodeid
56 assert_response :forbidden, "Should have rejected node update"
57 xml_node['version'] = @response.body.to_s
59 # save a version for later checking
60 versions[xml_node['version']] = xml_doc.to_s
63 # add a bunch of random tags
65 xml_tag = XML::Node.new("tag")
66 xml_tag['k'] = random_string
67 xml_tag['v'] = random_string
69 with_controller(NodeController.new) do
71 put :update, :id => nodeid
72 assert_response :forbidden,
73 "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
74 xml_node['version'] = @response.body.to_s
76 # save a version for later checking
77 versions[xml_node['version']] = xml_doc.to_s
80 # probably should check that they didn't get written to the database
83 ## Now do it with the public user
84 basic_authorization(users(:public_user).email, "test")
85 changeset_id = changesets(:public_user_first_change).id
87 # setup a simple XML node
88 xml_doc = current_nodes(:node_with_versions).to_xml
89 xml_node = xml_doc.find("//osm/node").first
90 nodeid = current_nodes(:node_with_versions).id
92 # keep a hash of the versions => string, as we'll need something
93 # to test against later
96 # save a version for later checking
97 versions[xml_node['version']] = xml_doc.to_s
99 # randomly move the node about
101 # move the node somewhere else
102 xml_node['lat'] = precision(rand * 180 - 90).to_s
103 xml_node['lon'] = precision(rand * 360 - 180).to_s
104 with_controller(NodeController.new) do
106 put :update, :id => nodeid
107 assert_response :success
108 xml_node['version'] = @response.body.to_s
110 # save a version for later checking
111 versions[xml_node['version']] = xml_doc.to_s
114 # add a bunch of random tags
116 xml_tag = XML::Node.new("tag")
117 xml_tag['k'] = random_string
118 xml_tag['v'] = random_string
120 with_controller(NodeController.new) do
122 put :update, :id => nodeid
123 assert_response :success,
124 "couldn't update node #{nodeid} (#{@response.body})"
125 xml_node['version'] = @response.body.to_s
127 # save a version for later checking
128 versions[xml_node['version']] = xml_doc.to_s
131 # check all the versions
132 versions.keys.each do |key|
133 get :version, :id => nodeid, :version => key.to_i
135 assert_response :success,
136 "couldn't get version #{key.to_i} of node #{nodeid}"
138 check_node = Node.from_xml(versions[key])
139 api_node = Node.from_xml(@response.body.to_s)
141 assert_nodes_are_equal check_node, api_node
145 def test_not_found_version
146 check_not_found_id_version(70000,312344)
147 check_not_found_id_version(-1, -13)
148 check_not_found_id_version(nodes(:visible_node).id, 24354)
149 check_not_found_id_version(24356, nodes(:visible_node).version)
152 def check_not_found_id_version(id, version)
153 get :version, :id => id, :version => version
154 assert_response :not_found
158 # Test that getting the current version is identical to picking
159 # that version with the version URI call.
160 def test_current_version
161 check_current_version(current_nodes(:visible_node))
162 check_current_version(current_nodes(:used_node_1))
163 check_current_version(current_nodes(:used_node_2))
164 check_current_version(current_nodes(:node_used_by_relationship))
165 check_current_version(current_nodes(:node_with_versions))
169 # test the redaction of an old version of a node, while not being
171 def test_redact_node_unauthorised
172 do_redact_node(nodes(:node_with_versions_v3),
173 redactions(:example))
174 assert_response :unauthorized, "should need to be authenticated to redact."
178 # test the redaction of an old version of a node, while being
179 # authorised as a normal user.
180 def test_redact_node_normal_user
181 basic_authorization(users(:public_user).email, "test")
183 do_redact_node(nodes(:node_with_versions_v3),
184 redactions(:example))
185 assert_response :forbidden, "should need to be moderator to redact."
189 # test that, even as moderator, the current version of a node
191 def test_redact_node_current_version
192 basic_authorization(users(:moderator_user).email, "test")
194 do_redact_node(nodes(:node_with_versions_v4),
195 redactions(:example))
196 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
200 # test that redacted nodes aren't visible, regardless of
201 # authorisation except as moderator...
202 def test_version_redacted
203 node = nodes(:redacted_node_redacted_version)
205 get :version, :id => node.node_id, :version => node.version
206 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
208 # not even to a logged-in user
209 basic_authorization(users(:public_user).email, "test")
210 get :version, :id => node.node_id, :version => node.version
211 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
215 # test that redacted nodes aren't visible in the history
216 def test_history_redacted
217 node = nodes(:redacted_node_redacted_version)
219 get :history, :id => node.node_id
220 assert_response :success, "Redaction shouldn't have stopped history working."
221 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."
223 # not even to a logged-in user
224 basic_authorization(users(:public_user).email, "test")
225 get :history, :id => node.node_id
226 assert_response :success, "Redaction shouldn't have stopped history working."
227 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."
231 # test the redaction of an old version of a node, while being
232 # authorised as a moderator.
233 def test_redact_node_moderator
234 node = nodes(:node_with_versions_v3)
235 basic_authorization(users(:moderator_user).email, "test")
237 do_redact_node(node, redactions(:example))
238 assert_response :success, "should be OK to redact old version as moderator."
240 # check moderator can still see the redacted data, when passing
241 # the appropriate flag
242 get :version, :id => node.node_id, :version => node.version
243 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
244 get :version, :id => node.node_id, :version => node.version, :show_redactions => 'true'
245 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
247 # and when accessed via history
248 get :history, :id => node.node_id
249 assert_response :success, "Redaction shouldn't have stopped history working."
250 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."
251 get :history, :id => node.node_id, :show_redactions => 'true'
252 assert_response :success, "Redaction shouldn't have stopped history working."
253 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."
256 # testing that if the moderator drops auth, he can't see the
257 # redacted stuff any more.
258 def test_redact_node_is_redacted
259 node = nodes(:node_with_versions_v3)
260 basic_authorization(users(:moderator_user).email, "test")
262 do_redact_node(node, redactions(:example))
263 assert_response :success, "should be OK to redact old version as moderator."
265 # re-auth as non-moderator
266 basic_authorization(users(:public_user).email, "test")
268 # check can't see the redacted data
269 get :version, :id => node.node_id, :version => node.version
270 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
272 # and when accessed via history
273 get :history, :id => node.node_id
274 assert_response :success, "Redaction shouldn't have stopped history working."
275 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."
278 def do_redact_node(node, redaction)
279 get :version, :id => node.node_id, :version => node.version
280 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
283 post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
286 def check_current_version(node_id)
287 # get the current version of the node
288 current_node = with_controller(NodeController.new) do
289 get :read, :id => node_id
290 assert_response :success, "cant get current node #{node_id}"
291 Node.from_xml(@response.body)
293 assert_not_nil current_node, "getting node #{node_id} returned nil"
295 # get the "old" version of the node from the old_node interface
296 get :version, :id => node_id, :version => current_node.version
297 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
298 old_node = Node.from_xml(@response.body)
300 # check the nodes are the same
301 assert_nodes_are_equal current_node, old_node
305 # returns a 16 character long string with some nasty characters in it.
306 # this ought to stress-test the tag handling as well as the versioning.
308 letters = [['!','"','$','&',';','@'],
311 ('0'..'9').to_a].flatten
312 (1..16).map { |i| letters[ rand(letters.length) ] }.join
316 # truncate a floating point number to the scale that it is stored in
317 # the database. otherwise rounding errors can produce failing unit
318 # tests when they shouldn't.
320 return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE