2 require 'way_controller'
4 class WayControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/way/create", :method => :put },
12 { :controller => "way", :action => "create" }
15 { :path => "/api/0.6/way/1/full", :method => :get },
16 { :controller => "way", :action => "full", :id => "1" }
19 { :path => "/api/0.6/way/1", :method => :get },
20 { :controller => "way", :action => "read", :id => "1" }
23 { :path => "/api/0.6/way/1", :method => :put },
24 { :controller => "way", :action => "update", :id => "1" }
27 { :path => "/api/0.6/way/1", :method => :delete },
28 { :controller => "way", :action => "delete", :id => "1" }
31 { :path => "/api/0.6/ways", :method => :get },
32 { :controller => "way", :action => "ways" }
36 # -------------------------------------
38 # -------------------------------------
41 # check that a visible way is returned properly
42 get :read, :id => current_ways(:visible_way).id
43 assert_response :success
45 # check that an invisible way is not returned
46 get :read, :id => current_ways(:invisible_way).id
49 # check chat a non-existent way is not returned
51 assert_response :not_found
55 # check the "full" mode
58 get :full, :id => way.id
60 # full call should say "gone" for non-visible ways...
66 # otherwise it should say success
67 assert_response :success
69 # Check the way is correctly returned
70 assert_select "osm way[id='#{way.id}'][version='#{way.version}'][visible='#{way.visible}']", 1
72 # check that each node in the way appears once in the output as a
73 # reference and as the node element.
75 count = (way.nodes - (way.nodes - [n])).length
76 assert_select "osm way nd[ref='#{n.id}']", count
77 assert_select "osm node[id='#{n.id}'][version='#{n.version}'][lat='#{n.lat}'][lon='#{n.lon}']", 1
83 # test fetching multiple ways
85 # check error when no parameter provided
87 assert_response :bad_request
89 # check error when no parameter value provided
90 get :ways, :ways => ""
91 assert_response :bad_request
94 get :ways, :ways => "1,2,4,6"
95 assert_response :success
96 assert_select "osm" do
97 assert_select "way", :count => 4
98 assert_select "way[id='1'][visible='true']", :count => 1
99 assert_select "way[id='2'][visible='false']", :count => 1
100 assert_select "way[id='4'][visible='true']", :count => 1
101 assert_select "way[id='6'][visible='true']", :count => 1
104 # check error when a non-existent way is included
105 get :ways, :ways => "1,2,4,6,400"
106 assert_response :not_found
109 # -------------------------------------
110 # Test simple way creation.
111 # -------------------------------------
114 ## First check that it fails when creating a way using a non-public user
115 nid1 = current_nodes(:used_node_1).id
116 nid2 = current_nodes(:used_node_2).id
117 basic_authorization users(:normal_user).email, "test"
119 # use the first user's open changeset
120 changeset_id = changesets(:normal_user_first_change).id
122 # create a way with pre-existing nodes
123 content "<osm><way changeset='#{changeset_id}'>" +
124 "<nd ref='#{nid1}'/><nd ref='#{nid2}'/>" +
125 "<tag k='test' v='yes' /></way></osm>"
128 assert_response :forbidden,
129 "way upload did not return success status"
130 # read id of created way and search for it
131 wayid = @response.body
133 ## Now use a public user
134 nid1 = current_nodes(:used_node_1).id
135 nid2 = current_nodes(:used_node_2).id
136 basic_authorization users(:public_user).email, "test"
138 # use the first user's open changeset
139 changeset_id = changesets(:public_user_first_change).id
141 # create a way with pre-existing nodes
142 content "<osm><way changeset='#{changeset_id}'>" +
143 "<nd ref='#{nid1}'/><nd ref='#{nid2}'/>" +
144 "<tag k='test' v='yes' /></way></osm>"
147 assert_response :success,
148 "way upload did not return success status"
149 # read id of created way and search for it
150 wayid = @response.body
151 checkway = Way.find(wayid)
152 assert_not_nil checkway,
153 "uploaded way not found in data base after upload"
155 assert_equal checkway.nds.length, 2,
156 "saved way does not contain exactly one node"
157 assert_equal checkway.nds[0], nid1,
158 "saved way does not contain the right node on pos 0"
159 assert_equal checkway.nds[1], nid2,
160 "saved way does not contain the right node on pos 1"
161 assert_equal checkway.changeset_id, changeset_id,
162 "saved way does not belong to the correct changeset"
163 assert_equal users(:public_user).id, checkway.changeset.user_id,
164 "saved way does not belong to user that created it"
165 assert_equal true, checkway.visible,
166 "saved way is not visible"
169 # -------------------------------------
170 # Test creating some invalid ways.
171 # -------------------------------------
173 def test_create_invalid
174 ## First test with a private user to make sure that they are not authorized
175 basic_authorization users(:normal_user).email, "test"
177 # use the first user's open changeset
178 open_changeset_id = changesets(:normal_user_first_change).id
179 closed_changeset_id = changesets(:normal_user_closed_change).id
180 nid1 = current_nodes(:used_node_1).id
182 # create a way with non-existing node
183 content "<osm><way changeset='#{open_changeset_id}'>" +
184 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
187 assert_response :forbidden,
188 "way upload with invalid node using a private user did not return 'forbidden'"
190 # create a way with no nodes
191 content "<osm><way changeset='#{open_changeset_id}'>" +
192 "<tag k='test' v='yes' /></way></osm>"
195 assert_response :forbidden,
196 "way upload with no node using a private userdid not return 'forbidden'"
198 # create a way inside a closed changeset
199 content "<osm><way changeset='#{closed_changeset_id}'>" +
200 "<nd ref='#{nid1}'/></way></osm>"
203 assert_response :forbidden,
204 "way upload to closed changeset with a private user did not return 'forbidden'"
206 ## Now test with a public user
207 basic_authorization users(:public_user).email, "test"
209 # use the first user's open changeset
210 open_changeset_id = changesets(:public_user_first_change).id
211 closed_changeset_id = changesets(:public_user_closed_change).id
212 nid1 = current_nodes(:used_node_1).id
214 # create a way with non-existing node
215 content "<osm><way changeset='#{open_changeset_id}'>" +
216 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
219 assert_response :precondition_failed,
220 "way upload with invalid node did not return 'precondition failed'"
221 assert_equal "Precondition failed: Way requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
223 # create a way with no nodes
224 content "<osm><way changeset='#{open_changeset_id}'>" +
225 "<tag k='test' v='yes' /></way></osm>"
228 assert_response :precondition_failed,
229 "way upload with no node did not return 'precondition failed'"
230 assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
232 # create a way inside a closed changeset
233 content "<osm><way changeset='#{closed_changeset_id}'>" +
234 "<nd ref='#{nid1}'/></way></osm>"
237 assert_response :conflict,
238 "way upload to closed changeset did not return 'conflict'"
240 # create a way with a tag which is too long
241 content "<osm><way changeset='#{open_changeset_id}'>" +
242 "<nd ref='#{nid1}'/>" +
243 "<tag k='foo' v='#{'x' * 256}'/>" +
247 assert_response :bad_request,
248 "way upload to with too long tag did not return 'bad_request'"
251 # -------------------------------------
252 # Test deleting ways.
253 # -------------------------------------
256 # first try to delete way without auth
257 delete :delete, :id => current_ways(:visible_way).id
258 assert_response :unauthorized
260 # now set auth using the private user
261 basic_authorization(users(:normal_user).email, "test")
263 # this shouldn't work as with the 0.6 api we need pay load to delete
264 delete :delete, :id => current_ways(:visible_way).id
265 assert_response :forbidden
267 # Now try without having a changeset
268 content "<osm><way id='#{current_ways(:visible_way).id}'/></osm>"
269 delete :delete, :id => current_ways(:visible_way).id
270 assert_response :forbidden
272 # try to delete with an invalid (closed) changeset
273 content update_changeset(current_ways(:visible_way).to_xml,
274 changesets(:normal_user_closed_change).id)
275 delete :delete, :id => current_ways(:visible_way).id
276 assert_response :forbidden
278 # try to delete with an invalid (non-existent) changeset
279 content update_changeset(current_ways(:visible_way).to_xml, 0)
280 delete :delete, :id => current_ways(:visible_way).id
281 assert_response :forbidden
283 # Now try with a valid changeset
284 content current_ways(:visible_way).to_xml
285 delete :delete, :id => current_ways(:visible_way).id
286 assert_response :forbidden
288 # check the returned value - should be the new version number
289 # valid delete should return the new version number, which should
290 # be greater than the old version number
291 # assert @response.body.to_i > current_ways(:visible_way).version,
292 # "delete request should return a new version number for way"
294 # this won't work since the way is already deleted
295 content current_ways(:invisible_way).to_xml
296 delete :delete, :id => current_ways(:invisible_way).id
297 assert_response :forbidden
299 # this shouldn't work as the way is used in a relation
300 content current_ways(:used_way).to_xml
301 delete :delete, :id => current_ways(:used_way).id
302 assert_response :forbidden,
303 "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
305 # this won't work since the way never existed
306 delete :delete, :id => 0
307 assert_response :forbidden
309 ### Now check with a public user
311 basic_authorization(users(:public_user).email, "test")
313 # this shouldn't work as with the 0.6 api we need pay load to delete
314 delete :delete, :id => current_ways(:visible_way).id
315 assert_response :bad_request
317 # Now try without having a changeset
318 content "<osm><way id='#{current_ways(:visible_way).id}'/></osm>"
319 delete :delete, :id => current_ways(:visible_way).id
320 assert_response :bad_request
322 # try to delete with an invalid (closed) changeset
323 content update_changeset(current_ways(:visible_way).to_xml,
324 changesets(:public_user_closed_change).id)
325 delete :delete, :id => current_ways(:visible_way).id
326 assert_response :conflict
328 # try to delete with an invalid (non-existent) changeset
329 content update_changeset(current_ways(:visible_way).to_xml, 0)
330 delete :delete, :id => current_ways(:visible_way).id
331 assert_response :conflict
333 # Now try with a valid changeset
334 content current_ways(:visible_way).to_xml
335 delete :delete, :id => current_ways(:visible_way).id
336 assert_response :success
338 # check the returned value - should be the new version number
339 # valid delete should return the new version number, which should
340 # be greater than the old version number
341 assert @response.body.to_i > current_ways(:visible_way).version,
342 "delete request should return a new version number for way"
344 # this won't work since the way is already deleted
345 content current_ways(:invisible_way).to_xml
346 delete :delete, :id => current_ways(:invisible_way).id
347 assert_response :gone
349 # this shouldn't work as the way is used in a relation
350 content current_ways(:used_way).to_xml
351 delete :delete, :id => current_ways(:used_way).id
352 assert_response :precondition_failed,
353 "shouldn't be able to delete a way used in a relation (#{@response.body})"
354 assert_equal "Precondition failed: Way 3 is still used by relations 1.", @response.body
356 # this won't work since the way never existed
357 delete :delete, :id => 0
358 assert_response :not_found
361 # ------------------------------------------------------------
363 # ------------------------------------------------------------
366 # Try adding a duplicate of an existing tag to a way
367 def test_add_duplicate_tags
368 ## Try with the non-public user
370 basic_authorization(users(:normal_user).email, "test")
372 # add an identical tag to the way
373 tag_xml = XML::Node.new("tag")
374 tag_xml['k'] = current_way_tags(:t1).k
375 tag_xml['v'] = current_way_tags(:t1).v
377 # add the tag into the existing xml
378 way_xml = current_ways(:visible_way).to_xml
379 way_xml.find("//osm/way").first << tag_xml
383 put :update, :id => current_ways(:visible_way).id
384 assert_response :forbidden,
385 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
387 ## Now try with the public user
389 basic_authorization(users(:public_user).email, "test")
391 # add an identical tag to the way
392 tag_xml = XML::Node.new("tag")
393 tag_xml['k'] = current_way_tags(:t1).k
394 tag_xml['v'] = current_way_tags(:t1).v
396 # add the tag into the existing xml
397 way_xml = current_ways(:visible_way).to_xml
398 way_xml.find("//osm/way").first << tag_xml
402 put :update, :id => current_ways(:visible_way).id
403 assert_response :bad_request,
404 "adding a duplicate tag to a way should fail with 'bad request'"
405 assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{current_way_tags(:t1).k}", @response.body
409 # Try adding a new duplicate tags to a way
410 def test_new_duplicate_tags
411 ## First test with the non-public user so should be rejected
413 basic_authorization(users(:normal_user).email, "test")
415 # create duplicate tag
416 tag_xml = XML::Node.new("tag")
417 tag_xml['k'] = "i_am_a_duplicate"
418 tag_xml['v'] = "foobar"
420 # add the tag into the existing xml
421 way_xml = current_ways(:visible_way).to_xml
423 # add two copies of the tag
424 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
428 put :update, :id => current_ways(:visible_way).id
429 assert_response :forbidden,
430 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
432 ## Now test with the public user
434 basic_authorization(users(:public_user).email, "test")
436 # create duplicate tag
437 tag_xml = XML::Node.new("tag")
438 tag_xml['k'] = "i_am_a_duplicate"
439 tag_xml['v'] = "foobar"
441 # add the tag into the existing xml
442 way_xml = current_ways(:visible_way).to_xml
444 # add two copies of the tag
445 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
449 put :update, :id => current_ways(:visible_way).id
450 assert_response :bad_request,
451 "adding new duplicate tags to a way should fail with 'bad request'"
452 assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key i_am_a_duplicate", @response.body
456 # Try adding a new duplicate tags to a way.
457 # But be a bit subtle - use unicode decoding ambiguities to use different
458 # binary strings which have the same decoding.
459 def test_invalid_duplicate_tags
460 ## First make sure that you can't with a non-public user
462 basic_authorization(users(:normal_user).email, "test")
464 # add the tag into the existing xml
465 way_str = "<osm><way changeset='1'>"
466 way_str << "<tag k='addr:housenumber' v='1'/>"
467 way_str << "<tag k='addr:housenumber' v='2'/>"
468 way_str << "</way></osm>"
473 assert_response :forbidden,
474 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
476 ## Now do it with a public user
478 basic_authorization(users(:public_user).email, "test")
480 # add the tag into the existing xml
481 way_str = "<osm><way changeset='1'>"
482 way_str << "<tag k='addr:housenumber' v='1'/>"
483 way_str << "<tag k='addr:housenumber' v='2'/>"
484 way_str << "</way></osm>"
489 assert_response :bad_request,
490 "adding new duplicate tags to a way should fail with 'bad request'"
491 assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
495 # test that a call to ways_for_node returns all ways that contain the node
496 # and none that don't.
497 def test_ways_for_node
498 # in current fixtures ways 1 and 3 all use node 3. ways 2 and 4
499 # *used* to use it but doesn't.
500 get :ways_for_node, :id => current_nodes(:used_node_1).id
501 assert_response :success
502 ways_xml = XML::Parser.string(@response.body).parse
503 assert_not_nil ways_xml, "failed to parse ways_for_node response"
505 # check that the set of IDs match expectations
506 expected_way_ids = [current_ways(:visible_way).id,
507 current_ways(:used_way).id
509 found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
510 assert_equal expected_way_ids.sort, found_way_ids.sort,
511 "expected ways for node #{current_nodes(:used_node_1).id} did not match found"
513 # check the full ways to ensure we're not missing anything
514 expected_way_ids.each do |id|
515 way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
516 assert_ways_are_equal(Way.find(id),
517 Way.from_xml_node(way_xml))
522 # update the changeset_id of a node element
523 def update_changeset(xml, changeset_id)
524 xml_attr_rewrite(xml, 'changeset', changeset_id)
528 # update an attribute in the node element
529 def xml_attr_rewrite(xml, name, value)
530 xml.find("//osm/way").first[name] = value.to_s