1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'way_controller'
4 class WayControllerTest < ActionController::TestCase
7 # -------------------------------------
9 # -------------------------------------
12 # check that a visible way is returned properly
13 get :read, :id => current_ways(:visible_way).id
14 assert_response :success
16 # check that an invisible way is not returned
17 get :read, :id => current_ways(:invisible_way).id
20 # check chat a non-existent way is not returned
22 assert_response :not_found
26 # check the "full" mode
28 Way.find(:all).each do |way|
29 get :full, :id => way.id
31 # full call should say "gone" for non-visible ways...
37 # otherwise it should say success
38 assert_response :success
40 # Check the way is correctly returned
41 assert_select "osm way[id=#{way.id}][version=#{way.version}][visible=#{way.visible}]", 1
43 # check that each node in the way appears once in the output as a
44 # reference and as the node element. note the slightly dodgy assumption
45 # that nodes appear only once. this is currently the case with the
46 # fixtures, but it doesn't have to be.
48 assert_select "osm way nd[ref=#{n.id}]", 1
49 assert_select "osm node[id=#{n.id}][version=#{n.version}][lat=#{n.lat}][lon=#{n.lon}]", 1
54 # -------------------------------------
55 # Test simple way creation.
56 # -------------------------------------
59 ## First check that it fails when creating a way using a non-public user
60 nid1 = current_nodes(:used_node_1).id
61 nid2 = current_nodes(:used_node_2).id
62 basic_authorization users(:normal_user).email, "test"
64 # use the first user's open changeset
65 changeset_id = changesets(:normal_user_first_change).id
67 # create a way with pre-existing nodes
68 content "<osm><way changeset='#{changeset_id}'>" +
69 "<nd ref='#{nid1}'/><nd ref='#{nid2}'/>" +
70 "<tag k='test' v='yes' /></way></osm>"
73 assert_response :forbidden,
74 "way upload did not return success status"
75 # read id of created way and search for it
76 wayid = @response.body
78 ## Now use a public user
79 nid1 = current_nodes(:used_node_1).id
80 nid2 = current_nodes(:used_node_2).id
81 basic_authorization users(:public_user).email, "test"
83 # use the first user's open changeset
84 changeset_id = changesets(:public_user_first_change).id
86 # create a way with pre-existing nodes
87 content "<osm><way changeset='#{changeset_id}'>" +
88 "<nd ref='#{nid1}'/><nd ref='#{nid2}'/>" +
89 "<tag k='test' v='yes' /></way></osm>"
92 assert_response :success,
93 "way upload did not return success status"
94 # read id of created way and search for it
95 wayid = @response.body
96 checkway = Way.find(wayid)
97 assert_not_nil checkway,
98 "uploaded way not found in data base after upload"
100 assert_equal checkway.nds.length, 2,
101 "saved way does not contain exactly one node"
102 assert_equal checkway.nds[0], nid1,
103 "saved way does not contain the right node on pos 0"
104 assert_equal checkway.nds[1], nid2,
105 "saved way does not contain the right node on pos 1"
106 assert_equal checkway.changeset_id, changeset_id,
107 "saved way does not belong to the correct changeset"
108 assert_equal users(:public_user).id, checkway.changeset.user_id,
109 "saved way does not belong to user that created it"
110 assert_equal true, checkway.visible,
111 "saved way is not visible"
114 # -------------------------------------
115 # Test creating some invalid ways.
116 # -------------------------------------
118 def test_create_invalid
119 ## First test with a private user to make sure that they are not authorized
120 basic_authorization users(:normal_user).email, "test"
122 # use the first user's open changeset
123 open_changeset_id = changesets(:normal_user_first_change).id
124 closed_changeset_id = changesets(:normal_user_closed_change).id
125 nid1 = current_nodes(:used_node_1).id
127 # create a way with non-existing node
128 content "<osm><way changeset='#{open_changeset_id}'>" +
129 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
132 assert_response :forbidden,
133 "way upload with invalid node using a private user did not return 'forbidden'"
135 # create a way with no nodes
136 content "<osm><way changeset='#{open_changeset_id}'>" +
137 "<tag k='test' v='yes' /></way></osm>"
140 assert_response :forbidden,
141 "way upload with no node using a private userdid not return 'forbidden'"
143 # create a way inside a closed changeset
144 content "<osm><way changeset='#{closed_changeset_id}'>" +
145 "<nd ref='#{nid1}'/></way></osm>"
148 assert_response :forbidden,
149 "way upload to closed changeset with a private user did not return 'forbidden'"
152 ## Now test with a public user
153 basic_authorization users(:public_user).email, "test"
155 # use the first user's open changeset
156 open_changeset_id = changesets(:public_user_first_change).id
157 closed_changeset_id = changesets(:public_user_closed_change).id
158 nid1 = current_nodes(:used_node_1).id
160 # create a way with non-existing node
161 content "<osm><way changeset='#{open_changeset_id}'>" +
162 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
165 assert_response :precondition_failed,
166 "way upload with invalid node did not return 'precondition failed'"
167 assert_equal "Precondition failed: Way requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
169 # create a way with no nodes
170 content "<osm><way changeset='#{open_changeset_id}'>" +
171 "<tag k='test' v='yes' /></way></osm>"
174 assert_response :precondition_failed,
175 "way upload with no node did not return 'precondition failed'"
176 assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
178 # create a way inside a closed changeset
179 content "<osm><way changeset='#{closed_changeset_id}'>" +
180 "<nd ref='#{nid1}'/></way></osm>"
183 assert_response :conflict,
184 "way upload to closed changeset did not return 'conflict'"
186 # create a way with a tag which is too long
187 content "<osm><way changeset='#{open_changeset_id}'>" +
188 "<nd ref='#{nid1}'/>" +
189 "<tag k='foo' v='#{'x'*256}'/>" +
193 assert_response :bad_request,
194 "way upload to with too long tag did not return 'bad_request'"
197 # -------------------------------------
198 # Test deleting ways.
199 # -------------------------------------
202 # first try to delete way without auth
203 delete :delete, :id => current_ways(:visible_way).id
204 assert_response :unauthorized
206 # now set auth using the private user
207 basic_authorization(users(:normal_user).email, "test");
209 # this shouldn't work as with the 0.6 api we need pay load to delete
210 delete :delete, :id => current_ways(:visible_way).id
211 assert_response :forbidden
213 # Now try without having a changeset
214 content "<osm><way id='#{current_ways(:visible_way).id}'></osm>"
215 delete :delete, :id => current_ways(:visible_way).id
216 assert_response :forbidden
218 # try to delete with an invalid (closed) changeset
219 content update_changeset(current_ways(:visible_way).to_xml,
220 changesets(:normal_user_closed_change).id)
221 delete :delete, :id => current_ways(:visible_way).id
222 assert_response :forbidden
224 # try to delete with an invalid (non-existent) changeset
225 content update_changeset(current_ways(:visible_way).to_xml,0)
226 delete :delete, :id => current_ways(:visible_way).id
227 assert_response :forbidden
229 # Now try with a valid changeset
230 content current_ways(:visible_way).to_xml
231 delete :delete, :id => current_ways(:visible_way).id
232 assert_response :forbidden
234 # check the returned value - should be the new version number
235 # valid delete should return the new version number, which should
236 # be greater than the old version number
237 #assert @response.body.to_i > current_ways(:visible_way).version,
238 # "delete request should return a new version number for way"
240 # this won't work since the way is already deleted
241 content current_ways(:invisible_way).to_xml
242 delete :delete, :id => current_ways(:invisible_way).id
243 assert_response :forbidden
245 # this shouldn't work as the way is used in a relation
246 content current_ways(:used_way).to_xml
247 delete :delete, :id => current_ways(:used_way).id
248 assert_response :forbidden,
249 "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
251 # this won't work since the way never existed
252 delete :delete, :id => 0
253 assert_response :forbidden
256 ### Now check with a public user
258 basic_authorization(users(:public_user).email, "test");
260 # this shouldn't work as with the 0.6 api we need pay load to delete
261 delete :delete, :id => current_ways(:visible_way).id
262 assert_response :bad_request
264 # Now try without having a changeset
265 content "<osm><way id='#{current_ways(:visible_way).id}'></osm>"
266 delete :delete, :id => current_ways(:visible_way).id
267 assert_response :bad_request
269 # try to delete with an invalid (closed) changeset
270 content update_changeset(current_ways(:visible_way).to_xml,
271 changesets(:public_user_closed_change).id)
272 delete :delete, :id => current_ways(:visible_way).id
273 assert_response :conflict
275 # try to delete with an invalid (non-existent) changeset
276 content update_changeset(current_ways(:visible_way).to_xml,0)
277 delete :delete, :id => current_ways(:visible_way).id
278 assert_response :conflict
280 # Now try with a valid changeset
281 content current_ways(:visible_way).to_xml
282 delete :delete, :id => current_ways(:visible_way).id
283 assert_response :success
285 # check the returned value - should be the new version number
286 # valid delete should return the new version number, which should
287 # be greater than the old version number
288 assert @response.body.to_i > current_ways(:visible_way).version,
289 "delete request should return a new version number for way"
291 # this won't work since the way is already deleted
292 content current_ways(:invisible_way).to_xml
293 delete :delete, :id => current_ways(:invisible_way).id
294 assert_response :gone
296 # this shouldn't work as the way is used in a relation
297 content current_ways(:used_way).to_xml
298 delete :delete, :id => current_ways(:used_way).id
299 assert_response :precondition_failed,
300 "shouldn't be able to delete a way used in a relation (#{@response.body})"
301 assert_equal "Precondition failed: Way 3 still used by relation 1.", @response.body
303 # this won't work since the way never existed
304 delete :delete, :id => 0
305 assert_response :not_found
308 # ------------------------------------------------------------
310 # ------------------------------------------------------------
313 # Try adding a duplicate of an existing tag to a way
314 def test_add_duplicate_tags
315 ## Try with the non-public user
317 basic_authorization(users(:normal_user).email, "test")
319 # add an identical tag to the way
320 tag_xml = XML::Node.new("tag")
321 tag_xml['k'] = current_way_tags(:t1).k
322 tag_xml['v'] = current_way_tags(:t1).v
324 # add the tag into the existing xml
325 way_xml = current_ways(:visible_way).to_xml
326 way_xml.find("//osm/way").first << tag_xml
330 put :update, :id => current_ways(:visible_way).id
331 assert_response :forbidden,
332 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
334 ## Now try with the public user
336 basic_authorization(users(:public_user).email, "test")
338 # add an identical tag to the way
339 tag_xml = XML::Node.new("tag")
340 tag_xml['k'] = current_way_tags(:t1).k
341 tag_xml['v'] = current_way_tags(:t1).v
343 # add the tag into the existing xml
344 way_xml = current_ways(:visible_way).to_xml
345 way_xml.find("//osm/way").first << tag_xml
349 put :update, :id => current_ways(:visible_way).id
350 assert_response :bad_request,
351 "adding a duplicate tag to a way should fail with 'bad request'"
352 assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{current_way_tags(:t1).k}.", @response.body
356 # Try adding a new duplicate tags to a way
357 def test_new_duplicate_tags
358 ## First test with the non-public user so should be rejected
360 basic_authorization(users(:normal_user).email, "test")
362 # create duplicate tag
363 tag_xml = XML::Node.new("tag")
364 tag_xml['k'] = "i_am_a_duplicate"
365 tag_xml['v'] = "foobar"
367 # add the tag into the existing xml
368 way_xml = current_ways(:visible_way).to_xml
370 # add two copies of the tag
371 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
375 put :update, :id => current_ways(:visible_way).id
376 assert_response :forbidden,
377 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
379 ## Now test with the public user
381 basic_authorization(users(:public_user).email, "test")
383 # create duplicate tag
384 tag_xml = XML::Node.new("tag")
385 tag_xml['k'] = "i_am_a_duplicate"
386 tag_xml['v'] = "foobar"
388 # add the tag into the existing xml
389 way_xml = current_ways(:visible_way).to_xml
391 # add two copies of the tag
392 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
396 put :update, :id => current_ways(:visible_way).id
397 assert_response :bad_request,
398 "adding new duplicate tags to a way should fail with 'bad request'"
399 assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key i_am_a_duplicate.", @response.body
404 # Try adding a new duplicate tags to a way.
405 # But be a bit subtle - use unicode decoding ambiguities to use different
406 # binary strings which have the same decoding.
407 def test_invalid_duplicate_tags
408 ## First make sure that you can't with a non-public user
410 basic_authorization(users(:normal_user).email, "test")
412 # add the tag into the existing xml
413 way_str = "<osm><way changeset='1'>"
414 way_str << "<tag k='addr:housenumber' v='1'/>"
415 way_str << "<tag k='addr:housenumber' v='2'/>"
416 way_str << "</way></osm>";
421 assert_response :forbidden,
422 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
424 ## Now do it with a public user
426 basic_authorization(users(:public_user).email, "test")
428 # add the tag into the existing xml
429 way_str = "<osm><way changeset='1'>"
430 way_str << "<tag k='addr:housenumber' v='1'/>"
431 way_str << "<tag k='addr:housenumber' v='2'/>"
432 way_str << "</way></osm>";
437 assert_response :bad_request,
438 "adding new duplicate tags to a way should fail with 'bad request'"
439 assert_equal "Element way/ has duplicate tags with key addr:housenumber.", @response.body
443 # test that a call to ways_for_node returns all ways that contain the node
444 # and none that don't.
445 def test_ways_for_node
446 # in current fixtures ways 1 and 3 all use node 3. ways 2 and 4
447 # *used* to use it but doesn't.
448 get :ways_for_node, :id => current_nodes(:used_node_1).id
449 assert_response :success
450 ways_xml = XML::Parser.string(@response.body).parse
451 assert_not_nil ways_xml, "failed to parse ways_for_node response"
453 # check that the set of IDs match expectations
454 expected_way_ids = [ current_ways(:visible_way).id,
455 current_ways(:used_way).id
457 found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
458 assert_equal expected_way_ids, found_way_ids,
459 "expected ways for node #{current_nodes(:used_node_1).id} did not match found"
461 # check the full ways to ensure we're not missing anything
462 expected_way_ids.each do |id|
463 way_xml = ways_xml.find("//osm/way[@id=#{id}]").first
464 assert_ways_are_equal(Way.find(id),
465 Way.from_xml_node(way_xml))
470 # update the changeset_id of a node element
471 def update_changeset(xml, changeset_id)
472 xml_attr_rewrite(xml, 'changeset', changeset_id)
476 # update an attribute in the node element
477 def xml_attr_rewrite(xml, name, value)
478 xml.find("//osm/way").first[name] = value.to_s