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" }
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")
38 changeset_id = changesets(:normal_user_first_change).id
40 # setup a simple XML node
41 xml_doc = current_nodes(:visible_node).to_xml
42 xml_node = xml_doc.find("//osm/node").first
43 nodeid = current_nodes(:visible_node).id
45 # keep a hash of the versions => string, as we'll need something
46 # to test against later
49 # save a version for later checking
50 versions[xml_node['version']] = xml_doc.to_s
52 # randomly move the node about
54 # move the node somewhere else
55 xml_node['lat'] = precision(rand * 180 - 90).to_s
56 xml_node['lon'] = precision(rand * 360 - 180).to_s
57 with_controller(NodeController.new) do
59 put :update, :id => nodeid
60 assert_response :forbidden, "Should have rejected node update"
61 xml_node['version'] = @response.body.to_s
63 # save a version for later checking
64 versions[xml_node['version']] = xml_doc.to_s
67 # add a bunch of random tags
69 xml_tag = XML::Node.new("tag")
70 xml_tag['k'] = random_string
71 xml_tag['v'] = random_string
73 with_controller(NodeController.new) do
75 put :update, :id => nodeid
76 assert_response :forbidden,
77 "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
78 xml_node['version'] = @response.body.to_s
80 # save a version for later checking
81 versions[xml_node['version']] = xml_doc.to_s
84 # probably should check that they didn't get written to the database
87 ## Now do it with the public user
88 basic_authorization(users(:public_user).email, "test")
89 changeset_id = changesets(:public_user_first_change).id
91 # setup a simple XML node
92 xml_doc = current_nodes(:node_with_versions).to_xml
93 xml_node = xml_doc.find("//osm/node").first
94 nodeid = current_nodes(:node_with_versions).id
96 # keep a hash of the versions => string, as we'll need something
97 # to test against later
100 # save a version for later checking
101 versions[xml_node['version']] = xml_doc.to_s
103 # randomly move the node about
105 # move the node somewhere else
106 xml_node['lat'] = precision(rand * 180 - 90).to_s
107 xml_node['lon'] = precision(rand * 360 - 180).to_s
108 with_controller(NodeController.new) do
110 put :update, :id => nodeid
111 assert_response :success
112 xml_node['version'] = @response.body.to_s
114 # save a version for later checking
115 versions[xml_node['version']] = xml_doc.to_s
118 # add a bunch of random tags
120 xml_tag = XML::Node.new("tag")
121 xml_tag['k'] = random_string
122 xml_tag['v'] = random_string
124 with_controller(NodeController.new) do
126 put :update, :id => nodeid
127 assert_response :success,
128 "couldn't update node #{nodeid} (#{@response.body})"
129 xml_node['version'] = @response.body.to_s
131 # save a version for later checking
132 versions[xml_node['version']] = xml_doc.to_s
135 # check all the versions
136 versions.keys.each do |key|
137 get :version, :id => nodeid, :version => key.to_i
139 assert_response :success,
140 "couldn't get version #{key.to_i} of node #{nodeid}"
142 check_node = Node.from_xml(versions[key])
143 api_node = Node.from_xml(@response.body.to_s)
145 assert_nodes_are_equal check_node, api_node
149 def test_not_found_version
150 check_not_found_id_version(70000,312344)
151 check_not_found_id_version(-1, -13)
152 check_not_found_id_version(nodes(:visible_node).id, 24354)
153 check_not_found_id_version(24356, nodes(:visible_node).version)
156 def check_not_found_id_version(id, version)
157 get :version, :id => id, :version => version
158 assert_response :not_found
162 # Test that getting the current version is identical to picking
163 # that version with the version URI call.
164 def test_current_version
165 check_current_version(current_nodes(:visible_node))
166 check_current_version(current_nodes(:used_node_1))
167 check_current_version(current_nodes(:used_node_2))
168 check_current_version(current_nodes(:node_used_by_relationship))
169 check_current_version(current_nodes(:node_with_versions))
173 # test the redaction of an old version of a node, while not being
175 def test_redact_node_unauthorised
176 do_redact_node(nodes(:node_with_versions_v3),
177 redactions(:example))
178 assert_response :unauthorized, "should need to be authenticated to redact."
182 # test the redaction of an old version of a node, while being
183 # authorised as a normal user.
184 def test_redact_node_normal_user
185 basic_authorization(users(:public_user).email, "test")
187 do_redact_node(nodes(:node_with_versions_v3),
188 redactions(:example))
189 assert_response :forbidden, "should need to be moderator to redact."
193 # test that, even as moderator, the current version of a node
195 def test_redact_node_current_version
196 basic_authorization(users(:moderator_user).email, "test")
198 do_redact_node(nodes(:node_with_versions_v4),
199 redactions(:example))
200 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
204 # test that redacted nodes aren't visible, regardless of
205 # authorisation except as moderator...
206 def test_version_redacted
207 node = nodes(:redacted_node_redacted_version)
209 get :version, :id => node.node_id, :version => node.version
210 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
212 # not even to a logged-in user
213 basic_authorization(users(:public_user).email, "test")
214 get :version, :id => node.node_id, :version => node.version
215 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
219 # test that redacted nodes aren't visible in the history
220 def test_history_redacted
221 node = nodes(:redacted_node_redacted_version)
223 get :history, :id => node.node_id
224 assert_response :success, "Redaction shouldn't have stopped history working."
225 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."
227 # not even to a logged-in user
228 basic_authorization(users(:public_user).email, "test")
229 get :history, :id => node.node_id
230 assert_response :success, "Redaction shouldn't have stopped history working."
231 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."
235 # test the redaction of an old version of a node, while being
236 # authorised as a moderator.
237 def test_redact_node_moderator
238 node = nodes(:node_with_versions_v3)
239 basic_authorization(users(:moderator_user).email, "test")
241 do_redact_node(node, redactions(:example))
242 assert_response :success, "should be OK to redact old version as moderator."
244 # check moderator can still see the redacted data, when passing
245 # the appropriate flag
246 get :version, :id => node.node_id, :version => node.version
247 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
248 get :version, :id => node.node_id, :version => node.version, :show_redactions => 'true'
249 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
251 # and when accessed via history
252 get :history, :id => node.node_id
253 assert_response :success, "Redaction shouldn't have stopped history working."
254 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."
255 get :history, :id => node.node_id, :show_redactions => 'true'
256 assert_response :success, "Redaction shouldn't have stopped history working."
257 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."
260 # testing that if the moderator drops auth, he can't see the
261 # redacted stuff any more.
262 def test_redact_node_is_redacted
263 node = nodes(:node_with_versions_v3)
264 basic_authorization(users(:moderator_user).email, "test")
266 do_redact_node(node, redactions(:example))
267 assert_response :success, "should be OK to redact old version as moderator."
269 # re-auth as non-moderator
270 basic_authorization(users(:public_user).email, "test")
272 # check can't see the redacted data
273 get :version, :id => node.node_id, :version => node.version
274 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
276 # and when accessed via history
277 get :history, :id => node.node_id
278 assert_response :success, "Redaction shouldn't have stopped history working."
279 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."
282 def do_redact_node(node, redaction)
283 get :version, :id => node.node_id, :version => node.version
284 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
287 post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
290 def check_current_version(node_id)
291 # get the current version of the node
292 current_node = with_controller(NodeController.new) do
293 get :read, :id => node_id
294 assert_response :success, "cant get current node #{node_id}"
295 Node.from_xml(@response.body)
297 assert_not_nil current_node, "getting node #{node_id} returned nil"
299 # get the "old" version of the node from the old_node interface
300 get :version, :id => node_id, :version => current_node.version
301 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
302 old_node = Node.from_xml(@response.body)
304 # check the nodes are the same
305 assert_nodes_are_equal current_node, old_node
309 # returns a 16 character long string with some nasty characters in it.
310 # this ought to stress-test the tag handling as well as the versioning.
312 letters = [['!','"','$','&',';','@'],
315 ('0'..'9').to_a].flatten
316 (1..16).map { |i| letters[ rand(letters.length) ] }.join
320 # truncate a floating point number to the scale that it is stored in
321 # the database. otherwise rounding errors can produce failing unit
322 # tests when they shouldn't.
324 return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE