4 class WaysControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/way/create", :method => :put },
10 { :controller => "api/ways", :action => "create" }
13 { :path => "/api/0.6/way/1/full", :method => :get },
14 { :controller => "api/ways", :action => "full", :id => "1" }
17 { :path => "/api/0.6/way/1/full.json", :method => :get },
18 { :controller => "api/ways", :action => "full", :id => "1", :format => "json" }
21 { :path => "/api/0.6/way/1", :method => :get },
22 { :controller => "api/ways", :action => "show", :id => "1" }
25 { :path => "/api/0.6/way/1.json", :method => :get },
26 { :controller => "api/ways", :action => "show", :id => "1", :format => "json" }
29 { :path => "/api/0.6/way/1", :method => :put },
30 { :controller => "api/ways", :action => "update", :id => "1" }
33 { :path => "/api/0.6/way/1", :method => :delete },
34 { :controller => "api/ways", :action => "delete", :id => "1" }
37 { :path => "/api/0.6/ways", :method => :get },
38 { :controller => "api/ways", :action => "index" }
41 { :path => "/api/0.6/ways.json", :method => :get },
42 { :controller => "api/ways", :action => "index", :format => "json" }
46 # -------------------------------------
48 # -------------------------------------
51 # check that a visible way is returned properly
52 get api_way_path(create(:way))
53 assert_response :success
55 # check that an invisible way is not returned
56 get api_way_path(create(:way, :deleted))
59 # check chat a non-existent way is not returned
60 get api_way_path(:id => 0)
61 assert_response :not_found
65 # check the "full" mode
67 way = create(:way_with_nodes, :nodes_count => 3)
69 get way_full_path(way)
71 assert_response :success
73 # Check the way is correctly returned
74 assert_select "osm way[id='#{way.id}'][version='1'][visible='true']", 1
76 # check that each node in the way appears once in the output as a
77 # reference and as the node element.
79 assert_select "osm way nd[ref='#{n.id}']", 1
80 assert_select "osm node[id='#{n.id}'][version='1'][lat='#{format('%.7f', n.lat)}'][lon='#{format('%.7f', n.lon)}']", 1
85 way = create(:way, :deleted)
87 get way_full_path(way)
93 # test fetching multiple ways
96 way2 = create(:way, :deleted)
100 # check error when no parameter provided
102 assert_response :bad_request
104 # check error when no parameter value provided
105 get ways_path, :params => { :ways => "" }
106 assert_response :bad_request
108 # test a working call
109 get ways_path, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}" }
110 assert_response :success
111 assert_select "osm" do
112 assert_select "way", :count => 4
113 assert_select "way[id='#{way1.id}'][visible='true']", :count => 1
114 assert_select "way[id='#{way2.id}'][visible='false']", :count => 1
115 assert_select "way[id='#{way3.id}'][visible='true']", :count => 1
116 assert_select "way[id='#{way4.id}'][visible='true']", :count => 1
119 # test a working call with json format
120 get ways_path, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}", :format => "json" }
122 js = ActiveSupport::JSON.decode(@response.body)
124 assert_equal 4, js["elements"].count
125 assert_equal 4, (js["elements"].count { |a| a["type"] == "way" })
126 assert_equal 1, (js["elements"].count { |a| a["id"] == way1.id && a["visible"].nil? })
127 assert_equal 1, (js["elements"].count { |a| a["id"] == way2.id && a["visible"] == false })
128 assert_equal 1, (js["elements"].count { |a| a["id"] == way3.id && a["visible"].nil? })
129 assert_equal 1, (js["elements"].count { |a| a["id"] == way4.id && a["visible"].nil? })
131 # check error when a non-existent way is included
132 get ways_path, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0" }
133 assert_response :not_found
136 # -------------------------------------
137 # Test simple way creation.
138 # -------------------------------------
141 node1 = create(:node)
142 node2 = create(:node)
143 private_user = create(:user, :data_public => false)
144 private_changeset = create(:changeset, :user => private_user)
146 changeset = create(:changeset, :user => user)
148 ## First check that it fails when creating a way using a non-public user
149 auth_header = basic_authorization_header private_user.email, "test"
151 # use the first user's open changeset
152 changeset_id = private_changeset.id
154 # create a way with pre-existing nodes
155 xml = "<osm><way changeset='#{changeset_id}'>" \
156 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
157 "<tag k='test' v='yes' /></way></osm>"
158 put way_create_path, :params => xml, :headers => auth_header
160 assert_response :forbidden,
161 "way upload did not return forbidden status"
163 ## Now use a public user
164 auth_header = basic_authorization_header user.email, "test"
166 # use the first user's open changeset
167 changeset_id = changeset.id
169 # create a way with pre-existing nodes
170 xml = "<osm><way changeset='#{changeset_id}'>" \
171 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
172 "<tag k='test' v='yes' /></way></osm>"
173 put way_create_path, :params => xml, :headers => auth_header
175 assert_response :success,
176 "way upload did not return success status"
177 # read id of created way and search for it
178 wayid = @response.body
179 checkway = Way.find(wayid)
180 assert_not_nil checkway,
181 "uploaded way not found in data base after upload"
183 assert_equal checkway.nds.length, 2,
184 "saved way does not contain exactly one node"
185 assert_equal checkway.nds[0], node1.id,
186 "saved way does not contain the right node on pos 0"
187 assert_equal checkway.nds[1], node2.id,
188 "saved way does not contain the right node on pos 1"
189 assert_equal checkway.changeset_id, changeset_id,
190 "saved way does not belong to the correct changeset"
191 assert_equal user.id, checkway.changeset.user_id,
192 "saved way does not belong to user that created it"
193 assert checkway.visible,
194 "saved way is not visible"
197 # -------------------------------------
198 # Test creating some invalid ways.
199 # -------------------------------------
201 def test_create_invalid
203 private_user = create(:user, :data_public => false)
204 private_open_changeset = create(:changeset, :user => private_user)
205 private_closed_changeset = create(:changeset, :closed, :user => private_user)
207 open_changeset = create(:changeset, :user => user)
208 closed_changeset = create(:changeset, :closed, :user => user)
210 ## First test with a private user to make sure that they are not authorized
211 auth_header = basic_authorization_header private_user.email, "test"
213 # use the first user's open changeset
214 # create a way with non-existing node
215 xml = "<osm><way changeset='#{private_open_changeset.id}'>" \
216 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
217 put way_create_path, :params => xml, :headers => auth_header
219 assert_response :forbidden,
220 "way upload with invalid node using a private user did not return 'forbidden'"
222 # create a way with no nodes
223 xml = "<osm><way changeset='#{private_open_changeset.id}'>" \
224 "<tag k='test' v='yes' /></way></osm>"
225 put way_create_path, :params => xml, :headers => auth_header
227 assert_response :forbidden,
228 "way upload with no node using a private userdid not return 'forbidden'"
230 # create a way inside a closed changeset
231 xml = "<osm><way changeset='#{private_closed_changeset.id}'>" \
232 "<nd ref='#{node.id}'/></way></osm>"
233 put way_create_path, :params => xml, :headers => auth_header
235 assert_response :forbidden,
236 "way upload to closed changeset with a private user did not return 'forbidden'"
238 ## Now test with a public user
239 auth_header = basic_authorization_header user.email, "test"
241 # use the first user's open changeset
242 # create a way with non-existing node
243 xml = "<osm><way changeset='#{open_changeset.id}'>" \
244 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
245 put way_create_path, :params => xml, :headers => auth_header
247 assert_response :precondition_failed,
248 "way upload with invalid node did not return 'precondition failed'"
249 assert_equal "Precondition failed: Way requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
251 # create a way with no nodes
252 xml = "<osm><way changeset='#{open_changeset.id}'>" \
253 "<tag k='test' v='yes' /></way></osm>"
254 put way_create_path, :params => xml, :headers => auth_header
256 assert_response :precondition_failed,
257 "way upload with no node did not return 'precondition failed'"
258 assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
260 # create a way inside a closed changeset
261 xml = "<osm><way changeset='#{closed_changeset.id}'>" \
262 "<nd ref='#{node.id}'/></way></osm>"
263 put way_create_path, :params => xml, :headers => auth_header
265 assert_response :conflict,
266 "way upload to closed changeset did not return 'conflict'"
268 # create a way with a tag which is too long
269 xml = "<osm><way changeset='#{open_changeset.id}'>" \
270 "<nd ref='#{node.id}'/>" \
271 "<tag k='foo' v='#{'x' * 256}'/>" \
273 put way_create_path, :params => xml, :headers => auth_header
275 assert_response :bad_request,
276 "way upload to with too long tag did not return 'bad_request'"
279 # -------------------------------------
280 # Test deleting ways.
281 # -------------------------------------
284 private_user = create(:user, :data_public => false)
285 private_open_changeset = create(:changeset, :user => private_user)
286 private_closed_changeset = create(:changeset, :closed, :user => private_user)
287 private_way = create(:way, :changeset => private_open_changeset)
288 private_deleted_way = create(:way, :deleted, :changeset => private_open_changeset)
289 private_used_way = create(:way, :changeset => private_open_changeset)
290 create(:relation_member, :member => private_used_way)
292 open_changeset = create(:changeset, :user => user)
293 closed_changeset = create(:changeset, :closed, :user => user)
294 way = create(:way, :changeset => open_changeset)
295 deleted_way = create(:way, :deleted, :changeset => open_changeset)
296 used_way = create(:way, :changeset => open_changeset)
297 relation_member = create(:relation_member, :member => used_way)
298 relation = relation_member.relation
300 # first try to delete way without auth
301 delete api_way_path(way)
302 assert_response :unauthorized
304 # now set auth using the private user
305 auth_header = basic_authorization_header private_user.email, "test"
307 # this shouldn't work as with the 0.6 api we need pay load to delete
308 delete api_way_path(private_way), :headers => auth_header
309 assert_response :forbidden
311 # Now try without having a changeset
312 xml = "<osm><way id='#{private_way.id}'/></osm>"
313 delete api_way_path(private_way), :params => xml.to_s, :headers => auth_header
314 assert_response :forbidden
316 # try to delete with an invalid (closed) changeset
317 xml = update_changeset(xml_for_way(private_way), private_closed_changeset.id)
318 delete api_way_path(private_way), :params => xml.to_s, :headers => auth_header
319 assert_response :forbidden
321 # try to delete with an invalid (non-existent) changeset
322 xml = update_changeset(xml_for_way(private_way), 0)
323 delete api_way_path(private_way), :params => xml.to_s, :headers => auth_header
324 assert_response :forbidden
326 # Now try with a valid changeset
327 xml = xml_for_way(private_way)
328 delete api_way_path(private_way), :params => xml.to_s, :headers => auth_header
329 assert_response :forbidden
331 # check the returned value - should be the new version number
332 # valid delete should return the new version number, which should
333 # be greater than the old version number
334 # assert @response.body.to_i > current_ways(:visible_way).version,
335 # "delete request should return a new version number for way"
337 # this won't work since the way is already deleted
338 xml = xml_for_way(private_deleted_way)
339 delete api_way_path(private_deleted_way), :params => xml.to_s, :headers => auth_header
340 assert_response :forbidden
342 # this shouldn't work as the way is used in a relation
343 xml = xml_for_way(private_used_way)
344 delete api_way_path(private_used_way), :params => xml.to_s, :headers => auth_header
345 assert_response :forbidden,
346 "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
348 # this won't work since the way never existed
349 delete api_way_path(:id => 0), :headers => auth_header
350 assert_response :forbidden
352 ### Now check with a public user
354 auth_header = basic_authorization_header user.email, "test"
356 # this shouldn't work as with the 0.6 api we need pay load to delete
357 delete api_way_path(way), :headers => auth_header
358 assert_response :bad_request
360 # Now try without having a changeset
361 xml = "<osm><way id='#{way.id}'/></osm>"
362 delete api_way_path(way), :params => xml.to_s, :headers => auth_header
363 assert_response :bad_request
365 # try to delete with an invalid (closed) changeset
366 xml = update_changeset(xml_for_way(way), closed_changeset.id)
367 delete api_way_path(way), :params => xml.to_s, :headers => auth_header
368 assert_response :conflict
370 # try to delete with an invalid (non-existent) changeset
371 xml = update_changeset(xml_for_way(way), 0)
372 delete api_way_path(way), :params => xml.to_s, :headers => auth_header
373 assert_response :conflict
375 # Now try with a valid changeset
376 xml = xml_for_way(way)
377 delete api_way_path(way), :params => xml.to_s, :headers => auth_header
378 assert_response :success
380 # check the returned value - should be the new version number
381 # valid delete should return the new version number, which should
382 # be greater than the old version number
383 assert @response.body.to_i > way.version,
384 "delete request should return a new version number for way"
386 # this won't work since the way is already deleted
387 xml = xml_for_way(deleted_way)
388 delete api_way_path(deleted_way), :params => xml.to_s, :headers => auth_header
389 assert_response :gone
391 # this shouldn't work as the way is used in a relation
392 xml = xml_for_way(used_way)
393 delete api_way_path(used_way), :params => xml.to_s, :headers => auth_header
394 assert_response :precondition_failed,
395 "shouldn't be able to delete a way used in a relation (#{@response.body})"
396 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
398 # this won't work since the way never existed
399 delete api_way_path(:id => 0), :params => xml.to_s, :headers => auth_header
400 assert_response :not_found
404 # tests whether the API works and prevents incorrect use while trying
407 private_user = create(:user, :data_public => false)
408 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
410 way = create(:way, :changeset => create(:changeset, :user => user))
412 create(:way_node, :way => private_way, :node => node)
413 create(:way_node, :way => way, :node => node)
415 ## First test with no user credentials
416 # try and update a way without authorisation
417 xml = xml_for_way(way)
418 put api_way_path(way), :params => xml.to_s
419 assert_response :unauthorized
421 ## Second test with the private user
424 auth_header = basic_authorization_header private_user.email, "test"
426 ## trying to break changesets
428 # try and update in someone else's changeset
429 xml = update_changeset(xml_for_way(private_way),
430 create(:changeset).id)
431 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
432 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
434 # try and update in a closed changeset
435 xml = update_changeset(xml_for_way(private_way),
436 create(:changeset, :closed, :user => private_user).id)
437 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
438 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
440 # try and update in a non-existant changeset
441 xml = update_changeset(xml_for_way(private_way), 0)
442 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
443 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
445 ## try and submit invalid updates
446 xml = xml_replace_node(xml_for_way(private_way), node.id, 9999)
447 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
448 assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"
450 xml = xml_replace_node(xml_for_way(private_way), node.id, create(:node, :deleted).id)
451 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
452 assert_require_public_data "way with deleted node should be forbidden, when data isn't public"
454 ## finally, produce a good request which will still not work
455 xml = xml_for_way(private_way)
456 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
457 assert_require_public_data "should have failed with a forbidden when data isn't public"
459 ## Finally test with the public user
462 auth_header = basic_authorization_header user.email, "test"
464 ## trying to break changesets
466 # try and update in someone else's changeset
467 xml = update_changeset(xml_for_way(way),
468 create(:changeset).id)
469 put api_way_path(way), :params => xml.to_s, :headers => auth_header
470 assert_response :conflict, "update with other user's changeset should be rejected"
472 # try and update in a closed changeset
473 xml = update_changeset(xml_for_way(way),
474 create(:changeset, :closed, :user => user).id)
475 put api_way_path(way), :params => xml.to_s, :headers => auth_header
476 assert_response :conflict, "update with closed changeset should be rejected"
478 # try and update in a non-existant changeset
479 xml = update_changeset(xml_for_way(way), 0)
480 put api_way_path(way), :params => xml.to_s, :headers => auth_header
481 assert_response :conflict, "update with changeset=0 should be rejected"
483 ## try and submit invalid updates
484 xml = xml_replace_node(xml_for_way(way), node.id, 9999)
485 put api_way_path(way), :params => xml.to_s, :headers => auth_header
486 assert_response :precondition_failed, "way with non-existent node should be rejected"
488 xml = xml_replace_node(xml_for_way(way), node.id, create(:node, :deleted).id)
489 put api_way_path(way), :params => xml.to_s, :headers => auth_header
490 assert_response :precondition_failed, "way with deleted node should be rejected"
492 ## next, attack the versioning
493 current_way_version = way.version
495 # try and submit a version behind
496 xml = xml_attr_rewrite(xml_for_way(way),
497 "version", current_way_version - 1)
498 put api_way_path(way), :params => xml.to_s, :headers => auth_header
499 assert_response :conflict, "should have failed on old version number"
501 # try and submit a version ahead
502 xml = xml_attr_rewrite(xml_for_way(way),
503 "version", current_way_version + 1)
504 put api_way_path(way), :params => xml.to_s, :headers => auth_header
505 assert_response :conflict, "should have failed on skipped version number"
507 # try and submit total crap in the version field
508 xml = xml_attr_rewrite(xml_for_way(way),
509 "version", "p1r4t3s!")
510 put api_way_path(way), :params => xml.to_s, :headers => auth_header
511 assert_response :conflict,
512 "should not be able to put 'p1r4at3s!' in the version field"
514 ## try an update with the wrong ID
515 xml = xml_for_way(create(:way))
516 put api_way_path(way), :params => xml.to_s, :headers => auth_header
517 assert_response :bad_request,
518 "should not be able to update a way with a different ID from the XML"
520 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
522 put api_way_path(way), :params => xml.to_s, :headers => auth_header
523 assert_response :bad_request,
524 "should not be able to update a way with non-OSM XML doc."
526 ## finally, produce a good request which should work
527 xml = xml_for_way(way)
528 put api_way_path(way), :params => xml.to_s, :headers => auth_header
529 assert_response :success, "a valid update request failed"
532 # ------------------------------------------------------------
534 # ------------------------------------------------------------
537 # Try adding a new tag to a way
539 private_user = create(:user, :data_public => false)
540 private_way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => private_user))
542 way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => user))
544 ## Try with the non-public user
546 auth_header = basic_authorization_header private_user.email, "test"
548 # add an identical tag to the way
549 tag_xml = XML::Node.new("tag")
553 # add the tag into the existing xml
554 way_xml = xml_for_way(private_way)
555 way_xml.find("//osm/way").first << tag_xml
558 put api_way_path(private_way), :params => way_xml.to_s, :headers => auth_header
559 assert_response :forbidden,
560 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
562 ## Now try with the public user
564 auth_header = basic_authorization_header user.email, "test"
566 # add an identical tag to the way
567 tag_xml = XML::Node.new("tag")
571 # add the tag into the existing xml
572 way_xml = xml_for_way(way)
573 way_xml.find("//osm/way").first << tag_xml
576 put api_way_path(way), :params => way_xml.to_s, :headers => auth_header
577 assert_response :success,
578 "adding a new tag to a way should succeed"
579 assert_equal way.version + 1, @response.body.to_i
583 # Try adding a duplicate of an existing tag to a way
584 def test_add_duplicate_tags
585 private_user = create(:user, :data_public => false)
586 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
587 private_existing_tag = create(:way_tag, :way => private_way)
589 way = create(:way, :changeset => create(:changeset, :user => user))
590 existing_tag = create(:way_tag, :way => way)
592 ## Try with the non-public user
594 auth_header = basic_authorization_header private_user.email, "test"
596 # add an identical tag to the way
597 tag_xml = XML::Node.new("tag")
598 tag_xml["k"] = private_existing_tag.k
599 tag_xml["v"] = private_existing_tag.v
601 # add the tag into the existing xml
602 way_xml = xml_for_way(private_way)
603 way_xml.find("//osm/way").first << tag_xml
606 put api_way_path(private_way), :params => way_xml.to_s, :headers => auth_header
607 assert_response :forbidden,
608 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
610 ## Now try with the public user
612 auth_header = basic_authorization_header user.email, "test"
614 # add an identical tag to the way
615 tag_xml = XML::Node.new("tag")
616 tag_xml["k"] = existing_tag.k
617 tag_xml["v"] = existing_tag.v
619 # add the tag into the existing xml
620 way_xml = xml_for_way(way)
621 way_xml.find("//osm/way").first << tag_xml
624 put api_way_path(way), :params => way_xml.to_s, :headers => auth_header
625 assert_response :bad_request,
626 "adding a duplicate tag to a way should fail with 'bad request'"
627 assert_equal "Element way/#{way.id} has duplicate tags with key #{existing_tag.k}", @response.body
631 # Try adding a new duplicate tags to a way
632 def test_new_duplicate_tags
633 private_user = create(:user, :data_public => false)
634 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
636 way = create(:way, :changeset => create(:changeset, :user => user))
638 ## First test with the non-public user so should be rejected
640 auth_header = basic_authorization_header private_user.email, "test"
642 # create duplicate tag
643 tag_xml = XML::Node.new("tag")
644 tag_xml["k"] = "i_am_a_duplicate"
645 tag_xml["v"] = "foobar"
647 # add the tag into the existing xml
648 way_xml = xml_for_way(private_way)
650 # add two copies of the tag
651 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
654 put api_way_path(private_way), :params => way_xml.to_s, :headers => auth_header
655 assert_response :forbidden,
656 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
658 ## Now test with the public user
660 auth_header = basic_authorization_header user.email, "test"
662 # create duplicate tag
663 tag_xml = XML::Node.new("tag")
664 tag_xml["k"] = "i_am_a_duplicate"
665 tag_xml["v"] = "foobar"
667 # add the tag into the existing xml
668 way_xml = xml_for_way(way)
670 # add two copies of the tag
671 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
674 put api_way_path(way), :params => way_xml.to_s, :headers => auth_header
675 assert_response :bad_request,
676 "adding new duplicate tags to a way should fail with 'bad request'"
677 assert_equal "Element way/#{way.id} has duplicate tags with key i_am_a_duplicate", @response.body
681 # Try adding a new duplicate tags to a way.
682 # But be a bit subtle - use unicode decoding ambiguities to use different
683 # binary strings which have the same decoding.
684 def test_invalid_duplicate_tags
685 private_user = create(:user, :data_public => false)
686 private_changeset = create(:changeset, :user => private_user)
688 changeset = create(:changeset, :user => user)
690 ## First make sure that you can't with a non-public user
692 auth_header = basic_authorization_header private_user.email, "test"
694 # add the tag into the existing xml
695 way_str = "<osm><way changeset='#{private_changeset.id}'>"
696 way_str << "<tag k='addr:housenumber' v='1'/>"
697 way_str << "<tag k='addr:housenumber' v='2'/>"
698 way_str << "</way></osm>"
701 put way_create_path, :params => way_str, :headers => auth_header
702 assert_response :forbidden,
703 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
705 ## Now do it with a public user
707 auth_header = basic_authorization_header user.email, "test"
709 # add the tag into the existing xml
710 way_str = "<osm><way changeset='#{changeset.id}'>"
711 way_str << "<tag k='addr:housenumber' v='1'/>"
712 way_str << "<tag k='addr:housenumber' v='2'/>"
713 way_str << "</way></osm>"
716 put way_create_path, :params => way_str, :headers => auth_header
717 assert_response :bad_request,
718 "adding new duplicate tags to a way should fail with 'bad request'"
719 assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
723 # test that a call to ways_for_node returns all ways that contain the node
724 # and none that don't.
725 def test_ways_for_node
729 create(:way_node, :way => way1, :node => node)
730 create(:way_node, :way => way2, :node => node)
731 # create an unrelated way
732 create(:way_with_nodes, :nodes_count => 2)
733 # create a way which used to use the node
734 way3_v1 = create(:old_way, :version => 1)
735 _way3_v2 = create(:old_way, :current_way => way3_v1.current_way, :version => 2)
736 create(:old_way_node, :old_way => way3_v1, :node => node)
738 get node_ways_path(node)
739 assert_response :success
740 ways_xml = XML::Parser.string(@response.body).parse
741 assert_not_nil ways_xml, "failed to parse ways_for_node response"
743 # check that the set of IDs match expectations
744 expected_way_ids = [way1.id,
746 found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
747 assert_equal expected_way_ids.sort, found_way_ids.sort,
748 "expected ways for node #{node.id} did not match found"
750 # check the full ways to ensure we're not missing anything
751 expected_way_ids.each do |id|
752 way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
753 assert_ways_are_equal(Way.find(id),
754 Way.from_xml_node(way_xml))
759 # update the changeset_id of a way element
760 def update_changeset(xml, changeset_id)
761 xml_attr_rewrite(xml, "changeset", changeset_id)
765 # update an attribute in the way element
766 def xml_attr_rewrite(xml, name, value)
767 xml.find("//osm/way").first[name] = value.to_s
772 # replace a node in a way element
773 def xml_replace_node(xml, old_node, new_node)
774 xml.find("//osm/way/nd[@ref='#{old_node}']").first["ref"] = new_node.to_s