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."
281 def do_redact_node(node, redaction)
282 get :version, :id => node.node_id, :version => node.version
283 assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
286 post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
289 def check_current_version(node_id)
290 # get the current version of the node
291 current_node = with_controller(NodeController.new) do
292 get :read, :id => node_id
293 assert_response :success, "cant get current node #{node_id}"
294 Node.from_xml(@response.body)
296 assert_not_nil current_node, "getting node #{node_id} returned nil"
298 # get the "old" version of the node from the old_node interface
299 get :version, :id => node_id, :version => current_node.version
300 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
301 old_node = Node.from_xml(@response.body)
303 # check the nodes are the same
304 assert_nodes_are_equal current_node, old_node
308 # returns a 16 character long string with some nasty characters in it.
309 # this ought to stress-test the tag handling as well as the versioning.
311 letters = [["!", '"', "$", "&", ";", "@"],
314 ("0".."9").to_a].flatten
315 (1..16).map { |_i| letters[rand(letters.length)] }.join
319 # truncate a floating point number to the scale that it is stored in
320 # the database. otherwise rounding errors can produce failing unit
321 # tests when they shouldn't.
323 (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE