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 xml_doc = current_nodes(:node_with_versions).to_xml
90 xml_node = xml_doc.find("//osm/node").first
91 nodeid = current_nodes(:node_with_versions).id
93 # keep a hash of the versions => string, as we'll need something
94 # to test against later
97 # save a version for later checking
98 versions[xml_node["version"]] = xml_doc.to_s
100 # randomly move the node about
102 # move the node somewhere else
103 xml_node["lat"] = precision(rand * 180 - 90).to_s
104 xml_node["lon"] = precision(rand * 360 - 180).to_s
105 with_controller(NodeController.new) do
107 put :update, :id => nodeid
108 assert_response :success
109 xml_node["version"] = @response.body.to_s
111 # save a version for later checking
112 versions[xml_node["version"]] = xml_doc.to_s
115 # add a bunch of random tags
117 xml_tag = XML::Node.new("tag")
118 xml_tag["k"] = random_string
119 xml_tag["v"] = random_string
121 with_controller(NodeController.new) do
123 put :update, :id => nodeid
124 assert_response :success,
125 "couldn't update node #{nodeid} (#{@response.body})"
126 xml_node["version"] = @response.body.to_s
128 # save a version for later checking
129 versions[xml_node["version"]] = xml_doc.to_s
132 # check all the versions
133 versions.keys.each do |key|
134 get :version, :id => nodeid, :version => key.to_i
136 assert_response :success,
137 "couldn't get version #{key.to_i} of node #{nodeid}"
139 check_node = Node.from_xml(versions[key])
140 api_node = Node.from_xml(@response.body.to_s)
142 assert_nodes_are_equal check_node, api_node
146 def test_not_found_version
147 check_not_found_id_version(70000, 312344)
148 check_not_found_id_version(-1, -13)
149 check_not_found_id_version(nodes(:visible_node).id, 24354)
150 check_not_found_id_version(24356, nodes(:visible_node).version)
153 def check_not_found_id_version(id, version)
154 get :version, :id => id, :version => version
155 assert_response :not_found
156 rescue ActionController::UrlGenerationError => ex
157 assert_match /No route matches/, ex.to_s
161 # Test that getting the current version is identical to picking
162 # that version with the version URI call.
163 def test_current_version
164 check_current_version(current_nodes(:visible_node))
165 check_current_version(current_nodes(:used_node_1))
166 check_current_version(current_nodes(:used_node_2))
167 check_current_version(current_nodes(:node_used_by_relationship))
168 check_current_version(current_nodes(:node_with_versions))
172 # test the redaction of an old version of a node, while not being
174 def test_redact_node_unauthorised
175 do_redact_node(nodes(:node_with_versions_v3),
176 redactions(:example))
177 assert_response :unauthorized, "should need to be authenticated to redact."
181 # test the redaction of an old version of a node, while being
182 # authorised as a normal user.
183 def test_redact_node_normal_user
184 basic_authorization(users(:public_user).email, "test")
186 do_redact_node(nodes(:node_with_versions_v3),
187 redactions(:example))
188 assert_response :forbidden, "should need to be moderator to redact."
192 # test that, even as moderator, the current version of a node
194 def test_redact_node_current_version
195 basic_authorization(users(:moderator_user).email, "test")
197 do_redact_node(nodes(:node_with_versions_v4),
198 redactions(:example))
199 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
203 # test that redacted nodes aren't visible, regardless of
204 # authorisation except as moderator...
205 def test_version_redacted
206 node = nodes(:redacted_node_redacted_version)
208 get :version, :id => node.node_id, :version => node.version
209 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
211 # not even to a logged-in user
212 basic_authorization(users(:public_user).email, "test")
213 get :version, :id => node.node_id, :version => node.version
214 assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
218 # test that redacted nodes aren't visible in the history
219 def test_history_redacted
220 node = nodes(:redacted_node_redacted_version)
222 get :history, :id => node.node_id
223 assert_response :success, "Redaction shouldn't have stopped history working."
224 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."
226 # not even to a logged-in user
227 basic_authorization(users(:public_user).email, "test")
228 get :history, :id => node.node_id
229 assert_response :success, "Redaction shouldn't have stopped history working."
230 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."
234 # test the redaction of an old version of a node, while being
235 # authorised as a moderator.
236 def test_redact_node_moderator
237 node = nodes(:node_with_versions_v3)
238 basic_authorization(users(:moderator_user).email, "test")
240 do_redact_node(node, redactions(:example))
241 assert_response :success, "should be OK to redact old version as moderator."
243 # check moderator can still see the redacted data, when passing
244 # the appropriate flag
245 get :version, :id => node.node_id, :version => node.version
246 assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
247 get :version, :id => node.node_id, :version => node.version, :show_redactions => "true"
248 assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
250 # and when accessed via history
251 get :history, :id => node.node_id
252 assert_response :success, "Redaction shouldn't have stopped history working."
253 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."
254 get :history, :id => node.node_id, :show_redactions => "true"
255 assert_response :success, "Redaction shouldn't have stopped history working."
256 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."
259 # testing that if the moderator drops auth, he can't see the
260 # redacted stuff any more.
261 def test_redact_node_is_redacted
262 node = nodes(:node_with_versions_v3)
263 basic_authorization(users(:moderator_user).email, "test")
265 do_redact_node(node, redactions(:example))
266 assert_response :success, "should be OK to redact old version as moderator."
268 # re-auth as non-moderator
269 basic_authorization(users(:public_user).email, "test")
271 # check can't see the redacted data
272 get :version, :id => node.node_id, :version => node.version
273 assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
275 # and when accessed via history
276 get :history, :id => node.node_id
277 assert_response :success, "Redaction shouldn't have stopped history working."
278 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 # test the unredaction of an old version of a node, while not being
284 def test_unredact_node_unauthorised
285 node = nodes(:redacted_node_redacted_version)
287 post :redact, :id => node.node_id, :version => node.version
288 assert_response :unauthorized, "should need to be authenticated to unredact."
292 # test the unredaction of an old version of a node, while being
293 # authorised as a normal user.
294 def test_unredact_node_normal_user
295 node = nodes(:redacted_node_redacted_version)
296 basic_authorization(users(:public_user).email, "test")
298 post :redact, :id => node.node_id, :version => node.version
299 assert_response :forbidden, "should need to be moderator to unredact."
303 # test the unredaction of an old version of a node, while being
304 # authorised as a moderator.
305 def test_unredact_node_moderator
306 node = nodes(:redacted_node_redacted_version)
307 basic_authorization(users(:moderator_user).email, "test")
309 post :redact, :id => node.node_id, :version => node.version
310 assert_response :success, "should be OK to redact old version as moderator."
312 # check moderator can now see the redacted data, when not
313 # passing the aspecial flag
314 get :version, :id => node.node_id, :version => node.version
315 assert_response :success, "After unredaction, node should not be gone for moderator."
317 # and when accessed via history
318 get :history, :id => node.node_id
319 assert_response :success, "Unredaction shouldn't have stopped history working."
320 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."
322 basic_authorization(users(:normal_user).email, "test")
324 # check normal user can now see the redacted data
325 get :version, :id => node.node_id, :version => node.version
326 assert_response :success, "After unredaction, node should not be gone for moderator."
328 # and when accessed via history
329 get :history, :id => node.node_id
330 assert_response :success, "Unredaction shouldn't have stopped history working."
331 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."
336 def do_redact_node(node, redaction)
337 get :version, :id => node.node_id, :version => node.version
338 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
341 post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
344 def check_current_version(node_id)
345 # get the current version of the node
346 current_node = with_controller(NodeController.new) do
347 get :read, :id => node_id
348 assert_response :success, "cant get current node #{node_id}"
349 Node.from_xml(@response.body)
351 assert_not_nil current_node, "getting node #{node_id} returned nil"
353 # get the "old" version of the node from the old_node interface
354 get :version, :id => node_id, :version => current_node.version
355 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
356 old_node = Node.from_xml(@response.body)
358 # check the nodes are the same
359 assert_nodes_are_equal current_node, old_node
363 # returns a 16 character long string with some nasty characters in it.
364 # this ought to stress-test the tag handling as well as the versioning.
366 letters = [["!", '"', "$", "&", ";", "@"],
369 ("0".."9").to_a].flatten
370 (1..16).map { |_i| letters[rand(letters.length)] }.join
374 # truncate a floating point number to the scale that it is stored in
375 # the database. otherwise rounding errors can produce failing unit
376 # tests when they shouldn't.
378 (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE