]> git.openstreetmap.org Git - rails.git/blob - test/controllers/way_controller_test.rb
Remove all use of the :text option to render
[rails.git] / test / controllers / way_controller_test.rb
1 require "test_helper"
2 require "way_controller"
3
4 class WayControllerTest < ActionController::TestCase
5   ##
6   # test all routes which lead to this controller
7   def test_routes
8     assert_routing(
9       { :path => "/api/0.6/way/create", :method => :put },
10       { :controller => "way", :action => "create" }
11     )
12     assert_routing(
13       { :path => "/api/0.6/way/1/full", :method => :get },
14       { :controller => "way", :action => "full", :id => "1" }
15     )
16     assert_routing(
17       { :path => "/api/0.6/way/1", :method => :get },
18       { :controller => "way", :action => "read", :id => "1" }
19     )
20     assert_routing(
21       { :path => "/api/0.6/way/1", :method => :put },
22       { :controller => "way", :action => "update", :id => "1" }
23     )
24     assert_routing(
25       { :path => "/api/0.6/way/1", :method => :delete },
26       { :controller => "way", :action => "delete", :id => "1" }
27     )
28     assert_routing(
29       { :path => "/api/0.6/ways", :method => :get },
30       { :controller => "way", :action => "ways" }
31     )
32   end
33
34   # -------------------------------------
35   # Test reading ways.
36   # -------------------------------------
37
38   def test_read
39     # check that a visible way is returned properly
40     get :read, :id => create(:way).id
41     assert_response :success
42
43     # check that an invisible way is not returned
44     get :read, :id => create(:way, :deleted).id
45     assert_response :gone
46
47     # check chat a non-existent way is not returned
48     get :read, :id => 0
49     assert_response :not_found
50   end
51
52   ##
53   # check the "full" mode
54   def test_full
55     Way.all.each do |way|
56       get :full, :id => way.id
57
58       # full call should say "gone" for non-visible ways...
59       unless way.visible
60         assert_response :gone
61         next
62       end
63
64       # otherwise it should say success
65       assert_response :success
66
67       # Check the way is correctly returned
68       assert_select "osm way[id='#{way.id}'][version='#{way.version}'][visible='#{way.visible}']", 1
69
70       # check that each node in the way appears once in the output as a
71       # reference and as the node element.
72       way.nodes.each do |n|
73         count = (way.nodes - (way.nodes - [n])).length
74         assert_select "osm way nd[ref='#{n.id}']", count
75         assert_select "osm node[id='#{n.id}'][version='#{n.version}'][lat='#{format('%.7f', n.lat)}'][lon='#{format('%.7f', n.lon)}']", 1
76       end
77     end
78   end
79
80   ##
81   # test fetching multiple ways
82   def test_ways
83     way1 = create(:way)
84     way2 = create(:way, :deleted)
85     way3 = create(:way)
86     way4 = create(:way)
87
88     # check error when no parameter provided
89     get :ways
90     assert_response :bad_request
91
92     # check error when no parameter value provided
93     get :ways, :ways => ""
94     assert_response :bad_request
95
96     # test a working call
97     get :ways, :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}"
98     assert_response :success
99     assert_select "osm" do
100       assert_select "way", :count => 4
101       assert_select "way[id='#{way1.id}'][visible='true']", :count => 1
102       assert_select "way[id='#{way2.id}'][visible='false']", :count => 1
103       assert_select "way[id='#{way3.id}'][visible='true']", :count => 1
104       assert_select "way[id='#{way4.id}'][visible='true']", :count => 1
105     end
106
107     # check error when a non-existent way is included
108     get :ways, :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},400"
109     assert_response :not_found
110   end
111
112   # -------------------------------------
113   # Test simple way creation.
114   # -------------------------------------
115
116   def test_create
117     node1 = create(:node)
118     node2 = create(:node)
119     private_user = create(:user, :data_public => false)
120     private_changeset = create(:changeset, :user => private_user)
121     user = create(:user)
122     changeset = create(:changeset, :user => user)
123
124     ## First check that it fails when creating a way using a non-public user
125     basic_authorization private_user.email, "test"
126
127     # use the first user's open changeset
128     changeset_id = private_changeset.id
129
130     # create a way with pre-existing nodes
131     content "<osm><way changeset='#{changeset_id}'>" +
132             "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" +
133             "<tag k='test' v='yes' /></way></osm>"
134     put :create
135     # hope for failure
136     assert_response :forbidden,
137                     "way upload did not return forbidden status"
138
139     ## Now use a public user
140     basic_authorization user.email, "test"
141
142     # use the first user's open changeset
143     changeset_id = changeset.id
144
145     # create a way with pre-existing nodes
146     content "<osm><way changeset='#{changeset_id}'>" +
147             "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" +
148             "<tag k='test' v='yes' /></way></osm>"
149     put :create
150     # hope for success
151     assert_response :success,
152                     "way upload did not return success status"
153     # read id of created way and search for it
154     wayid = @response.body
155     checkway = Way.find(wayid)
156     assert_not_nil checkway,
157                    "uploaded way not found in data base after upload"
158     # compare values
159     assert_equal checkway.nds.length, 2,
160                  "saved way does not contain exactly one node"
161     assert_equal checkway.nds[0], node1.id,
162                  "saved way does not contain the right node on pos 0"
163     assert_equal checkway.nds[1], node2.id,
164                  "saved way does not contain the right node on pos 1"
165     assert_equal checkway.changeset_id, changeset_id,
166                  "saved way does not belong to the correct changeset"
167     assert_equal user.id, checkway.changeset.user_id,
168                  "saved way does not belong to user that created it"
169     assert_equal true, checkway.visible,
170                  "saved way is not visible"
171   end
172
173   # -------------------------------------
174   # Test creating some invalid ways.
175   # -------------------------------------
176
177   def test_create_invalid
178     node = create(:node)
179     private_user = create(:user, :data_public => false)
180     private_open_changeset = create(:changeset, :user => private_user)
181     private_closed_changeset = create(:changeset, :closed, :user => private_user)
182     user = create(:user)
183     open_changeset = create(:changeset, :user => user)
184     closed_changeset = create(:changeset, :closed, :user => user)
185
186     ## First test with a private user to make sure that they are not authorized
187     basic_authorization private_user.email, "test"
188
189     # use the first user's open changeset
190     # create a way with non-existing node
191     content "<osm><way changeset='#{private_open_changeset.id}'>" +
192             "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
193     put :create
194     # expect failure
195     assert_response :forbidden,
196                     "way upload with invalid node using a private user did not return 'forbidden'"
197
198     # create a way with no nodes
199     content "<osm><way changeset='#{private_open_changeset.id}'>" +
200             "<tag k='test' v='yes' /></way></osm>"
201     put :create
202     # expect failure
203     assert_response :forbidden,
204                     "way upload with no node using a private userdid not return 'forbidden'"
205
206     # create a way inside a closed changeset
207     content "<osm><way changeset='#{private_closed_changeset.id}'>" +
208             "<nd ref='#{node.id}'/></way></osm>"
209     put :create
210     # expect failure
211     assert_response :forbidden,
212                     "way upload to closed changeset with a private user did not return 'forbidden'"
213
214     ## Now test with a public user
215     basic_authorization user.email, "test"
216
217     # use the first user's open changeset
218     # create a way with non-existing node
219     content "<osm><way changeset='#{open_changeset.id}'>" +
220             "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
221     put :create
222     # expect failure
223     assert_response :precondition_failed,
224                     "way upload with invalid node did not return 'precondition failed'"
225     assert_equal "Precondition failed: Way  requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
226
227     # create a way with no nodes
228     content "<osm><way changeset='#{open_changeset.id}'>" +
229             "<tag k='test' v='yes' /></way></osm>"
230     put :create
231     # expect failure
232     assert_response :precondition_failed,
233                     "way upload with no node did not return 'precondition failed'"
234     assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
235
236     # create a way inside a closed changeset
237     content "<osm><way changeset='#{closed_changeset.id}'>" +
238             "<nd ref='#{node.id}'/></way></osm>"
239     put :create
240     # expect failure
241     assert_response :conflict,
242                     "way upload to closed changeset did not return 'conflict'"
243
244     # create a way with a tag which is too long
245     content "<osm><way changeset='#{open_changeset.id}'>" +
246             "<nd ref='#{node.id}'/>" +
247             "<tag k='foo' v='#{'x' * 256}'/>" +
248             "</way></osm>"
249     put :create
250     # expect failure
251     assert_response :bad_request,
252                     "way upload to with too long tag did not return 'bad_request'"
253   end
254
255   # -------------------------------------
256   # Test deleting ways.
257   # -------------------------------------
258
259   def test_delete
260     private_user = create(:user, :data_public => false)
261     private_open_changeset = create(:changeset, :user => private_user)
262     private_closed_changeset = create(:changeset, :closed, :user => private_user)
263     private_way = create(:way, :changeset => private_open_changeset)
264     private_deleted_way = create(:way, :deleted, :changeset => private_open_changeset)
265     private_used_way = create(:way, :changeset => private_open_changeset)
266     create(:relation_member, :member => private_used_way)
267     user = create(:user)
268     open_changeset = create(:changeset, :user => user)
269     closed_changeset = create(:changeset, :closed, :user => user)
270     way = create(:way, :changeset => open_changeset)
271     deleted_way = create(:way, :deleted, :changeset => open_changeset)
272     used_way = create(:way, :changeset => open_changeset)
273     relation_member = create(:relation_member, :member => used_way)
274     relation = relation_member.relation
275
276     # first try to delete way without auth
277     delete :delete, :id => way.id
278     assert_response :unauthorized
279
280     # now set auth using the private user
281     basic_authorization(private_user.email, "test")
282
283     # this shouldn't work as with the 0.6 api we need pay load to delete
284     delete :delete, :id => private_way.id
285     assert_response :forbidden
286
287     # Now try without having a changeset
288     content "<osm><way id='#{private_way.id}'/></osm>"
289     delete :delete, :id => private_way.id
290     assert_response :forbidden
291
292     # try to delete with an invalid (closed) changeset
293     content update_changeset(private_way.to_xml, private_closed_changeset.id)
294     delete :delete, :id => private_way.id
295     assert_response :forbidden
296
297     # try to delete with an invalid (non-existent) changeset
298     content update_changeset(private_way.to_xml, 0)
299     delete :delete, :id => private_way.id
300     assert_response :forbidden
301
302     # Now try with a valid changeset
303     content private_way.to_xml
304     delete :delete, :id => private_way.id
305     assert_response :forbidden
306
307     # check the returned value - should be the new version number
308     # valid delete should return the new version number, which should
309     # be greater than the old version number
310     # assert @response.body.to_i > current_ways(:visible_way).version,
311     #   "delete request should return a new version number for way"
312
313     # this won't work since the way is already deleted
314     content private_deleted_way.to_xml
315     delete :delete, :id => private_deleted_way.id
316     assert_response :forbidden
317
318     # this shouldn't work as the way is used in a relation
319     content private_used_way.to_xml
320     delete :delete, :id => private_used_way.id
321     assert_response :forbidden,
322                     "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
323
324     # this won't work since the way never existed
325     delete :delete, :id => 0
326     assert_response :forbidden
327
328     ### Now check with a public user
329     # now set auth
330     basic_authorization(user.email, "test")
331
332     # this shouldn't work as with the 0.6 api we need pay load to delete
333     delete :delete, :id => way.id
334     assert_response :bad_request
335
336     # Now try without having a changeset
337     content "<osm><way id='#{way.id}'/></osm>"
338     delete :delete, :id => way.id
339     assert_response :bad_request
340
341     # try to delete with an invalid (closed) changeset
342     content update_changeset(way.to_xml, closed_changeset.id)
343     delete :delete, :id => way.id
344     assert_response :conflict
345
346     # try to delete with an invalid (non-existent) changeset
347     content update_changeset(way.to_xml, 0)
348     delete :delete, :id => way.id
349     assert_response :conflict
350
351     # Now try with a valid changeset
352     content way.to_xml
353     delete :delete, :id => way.id
354     assert_response :success
355
356     # check the returned value - should be the new version number
357     # valid delete should return the new version number, which should
358     # be greater than the old version number
359     assert @response.body.to_i > way.version,
360            "delete request should return a new version number for way"
361
362     # this won't work since the way is already deleted
363     content deleted_way.to_xml
364     delete :delete, :id => deleted_way.id
365     assert_response :gone
366
367     # this shouldn't work as the way is used in a relation
368     content used_way.to_xml
369     delete :delete, :id => used_way.id
370     assert_response :precondition_failed,
371                     "shouldn't be able to delete a way used in a relation (#{@response.body})"
372     assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
373
374     # this won't work since the way never existed
375     delete :delete, :id => 0
376     assert_response :not_found
377   end
378
379   ##
380   # tests whether the API works and prevents incorrect use while trying
381   # to update ways.
382   def test_update
383     private_user = create(:user, :data_public => false)
384     private_way = create(:way, :changeset => create(:changeset, :user => private_user))
385     user = create(:user)
386     way = create(:way, :changeset => create(:changeset, :user => user))
387     node = create(:node)
388     create(:way_node, :way => private_way, :node => node)
389     create(:way_node, :way => way, :node => node)
390
391     ## First test with no user credentials
392     # try and update a way without authorisation
393     content way.to_xml
394     put :update, :id => way.id
395     assert_response :unauthorized
396
397     ## Second test with the private user
398
399     # setup auth
400     basic_authorization(private_user.email, "test")
401
402     ## trying to break changesets
403
404     # try and update in someone else's changeset
405     content update_changeset(private_way.to_xml,
406                              create(:changeset).id)
407     put :update, :id => private_way.id
408     assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
409
410     # try and update in a closed changeset
411     content update_changeset(private_way.to_xml,
412                              create(:changeset, :closed, :user => private_user).id)
413     put :update, :id => private_way.id
414     assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
415
416     # try and update in a non-existant changeset
417     content update_changeset(private_way.to_xml, 0)
418     put :update, :id => private_way.id
419     assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
420
421     ## try and submit invalid updates
422     content xml_replace_node(private_way.to_xml, node.id, 9999)
423     put :update, :id => private_way.id
424     assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"
425
426     content xml_replace_node(private_way.to_xml, node.id, create(:node, :deleted).id)
427     put :update, :id => private_way.id
428     assert_require_public_data "way with deleted node should be forbidden, when data isn't public"
429
430     ## finally, produce a good request which will still not work
431     content private_way.to_xml
432     put :update, :id => private_way.id
433     assert_require_public_data "should have failed with a forbidden when data isn't public"
434
435     ## Finally test with the public user
436
437     # setup auth
438     basic_authorization(user.email, "test")
439
440     ## trying to break changesets
441
442     # try and update in someone else's changeset
443     content update_changeset(way.to_xml,
444                              create(:changeset).id)
445     put :update, :id => way.id
446     assert_response :conflict, "update with other user's changeset should be rejected"
447
448     # try and update in a closed changeset
449     content update_changeset(way.to_xml,
450                              create(:changeset, :closed, :user => user).id)
451     put :update, :id => way.id
452     assert_response :conflict, "update with closed changeset should be rejected"
453
454     # try and update in a non-existant changeset
455     content update_changeset(way.to_xml, 0)
456     put :update, :id => way.id
457     assert_response :conflict, "update with changeset=0 should be rejected"
458
459     ## try and submit invalid updates
460     content xml_replace_node(way.to_xml, node.id, 9999)
461     put :update, :id => way.id
462     assert_response :precondition_failed, "way with non-existent node should be rejected"
463
464     content xml_replace_node(way.to_xml, node.id, create(:node, :deleted).id)
465     put :update, :id => way.id
466     assert_response :precondition_failed, "way with deleted node should be rejected"
467
468     ## next, attack the versioning
469     current_way_version = way.version
470
471     # try and submit a version behind
472     content xml_attr_rewrite(way.to_xml,
473                              "version", current_way_version - 1)
474     put :update, :id => way.id
475     assert_response :conflict, "should have failed on old version number"
476
477     # try and submit a version ahead
478     content xml_attr_rewrite(way.to_xml,
479                              "version", current_way_version + 1)
480     put :update, :id => way.id
481     assert_response :conflict, "should have failed on skipped version number"
482
483     # try and submit total crap in the version field
484     content xml_attr_rewrite(way.to_xml,
485                              "version", "p1r4t3s!")
486     put :update, :id => way.id
487     assert_response :conflict,
488                     "should not be able to put 'p1r4at3s!' in the version field"
489
490     ## try an update with the wrong ID
491     content create(:way).to_xml
492     put :update, :id => way.id
493     assert_response :bad_request,
494                     "should not be able to update a way with a different ID from the XML"
495
496     ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
497     content "<update/>"
498     put :update, :id => way.id
499     assert_response :bad_request,
500                     "should not be able to update a way with non-OSM XML doc."
501
502     ## finally, produce a good request which should work
503     content way.to_xml
504     put :update, :id => way.id
505     assert_response :success, "a valid update request failed"
506   end
507
508   # ------------------------------------------------------------
509   # test tags handling
510   # ------------------------------------------------------------
511
512   ##
513   # Try adding a new tag to a way
514   def test_add_tags
515     private_user = create(:user, :data_public => false)
516     private_way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => private_user))
517     user = create(:user)
518     way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => user))
519
520     ## Try with the non-public user
521     # setup auth
522     basic_authorization(private_user.email, "test")
523
524     # add an identical tag to the way
525     tag_xml = XML::Node.new("tag")
526     tag_xml["k"] = "new"
527     tag_xml["v"] = "yes"
528
529     # add the tag into the existing xml
530     way_xml = private_way.to_xml
531     way_xml.find("//osm/way").first << tag_xml
532
533     # try and upload it
534     content way_xml
535     put :update, :id => private_way.id
536     assert_response :forbidden,
537                     "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
538
539     ## Now try with the public user
540     # setup auth
541     basic_authorization(user.email, "test")
542
543     # add an identical tag to the way
544     tag_xml = XML::Node.new("tag")
545     tag_xml["k"] = "new"
546     tag_xml["v"] = "yes"
547
548     # add the tag into the existing xml
549     way_xml = way.to_xml
550     way_xml.find("//osm/way").first << tag_xml
551
552     # try and upload it
553     content way_xml
554     put :update, :id => way.id
555     assert_response :success,
556                     "adding a new tag to a way should succeed"
557     assert_equal way.version + 1, @response.body.to_i
558   end
559
560   ##
561   # Try adding a duplicate of an existing tag to a way
562   def test_add_duplicate_tags
563     private_user = create(:user, :data_public => false)
564     private_way = create(:way, :changeset => create(:changeset, :user => private_user))
565     private_existing_tag = create(:way_tag, :way => private_way)
566     user = create(:user)
567     way = create(:way, :changeset => create(:changeset, :user => user))
568     existing_tag = create(:way_tag, :way => way)
569
570     ## Try with the non-public user
571     # setup auth
572     basic_authorization(private_user.email, "test")
573
574     # add an identical tag to the way
575     tag_xml = XML::Node.new("tag")
576     tag_xml["k"] = private_existing_tag.k
577     tag_xml["v"] = private_existing_tag.v
578
579     # add the tag into the existing xml
580     way_xml = private_way.to_xml
581     way_xml.find("//osm/way").first << tag_xml
582
583     # try and upload it
584     content way_xml
585     put :update, :id => private_way.id
586     assert_response :forbidden,
587                     "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
588
589     ## Now try with the public user
590     # setup auth
591     basic_authorization(user.email, "test")
592
593     # add an identical tag to the way
594     tag_xml = XML::Node.new("tag")
595     tag_xml["k"] = existing_tag.k
596     tag_xml["v"] = existing_tag.v
597
598     # add the tag into the existing xml
599     way_xml = way.to_xml
600     way_xml.find("//osm/way").first << tag_xml
601
602     # try and upload it
603     content way_xml
604     put :update, :id => way.id
605     assert_response :bad_request,
606                     "adding a duplicate tag to a way should fail with 'bad request'"
607     assert_equal "Element way/#{way.id} has duplicate tags with key #{existing_tag.k}", @response.body
608   end
609
610   ##
611   # Try adding a new duplicate tags to a way
612   def test_new_duplicate_tags
613     private_user = create(:user, :data_public => false)
614     private_way = create(:way, :changeset => create(:changeset, :user => private_user))
615     user = create(:user)
616     way = create(:way, :changeset => create(:changeset, :user => user))
617
618     ## First test with the non-public user so should be rejected
619     # setup auth
620     basic_authorization(private_user.email, "test")
621
622     # create duplicate tag
623     tag_xml = XML::Node.new("tag")
624     tag_xml["k"] = "i_am_a_duplicate"
625     tag_xml["v"] = "foobar"
626
627     # add the tag into the existing xml
628     way_xml = private_way.to_xml
629
630     # add two copies of the tag
631     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
632
633     # try and upload it
634     content way_xml
635     put :update, :id => private_way.id
636     assert_response :forbidden,
637                     "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
638
639     ## Now test with the public user
640     # setup auth
641     basic_authorization(user.email, "test")
642
643     # create duplicate tag
644     tag_xml = XML::Node.new("tag")
645     tag_xml["k"] = "i_am_a_duplicate"
646     tag_xml["v"] = "foobar"
647
648     # add the tag into the existing xml
649     way_xml = way.to_xml
650
651     # add two copies of the tag
652     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
653
654     # try and upload it
655     content way_xml
656     put :update, :id => way.id
657     assert_response :bad_request,
658                     "adding new duplicate tags to a way should fail with 'bad request'"
659     assert_equal "Element way/#{way.id} has duplicate tags with key i_am_a_duplicate", @response.body
660   end
661
662   ##
663   # Try adding a new duplicate tags to a way.
664   # But be a bit subtle - use unicode decoding ambiguities to use different
665   # binary strings which have the same decoding.
666   def test_invalid_duplicate_tags
667     private_user = create(:user, :data_public => false)
668     private_changeset = create(:changeset, :user => private_user)
669     user = create(:user)
670     changeset = create(:changeset, :user => user)
671
672     ## First make sure that you can't with a non-public user
673     # setup auth
674     basic_authorization(private_user.email, "test")
675
676     # add the tag into the existing xml
677     way_str = "<osm><way changeset='#{private_changeset.id}'>"
678     way_str << "<tag k='addr:housenumber' v='1'/>"
679     way_str << "<tag k='addr:housenumber' v='2'/>"
680     way_str << "</way></osm>"
681
682     # try and upload it
683     content way_str
684     put :create
685     assert_response :forbidden,
686                     "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
687
688     ## Now do it with a public user
689     # setup auth
690     basic_authorization(user.email, "test")
691
692     # add the tag into the existing xml
693     way_str = "<osm><way changeset='#{changeset.id}'>"
694     way_str << "<tag k='addr:housenumber' v='1'/>"
695     way_str << "<tag k='addr:housenumber' v='2'/>"
696     way_str << "</way></osm>"
697
698     # try and upload it
699     content way_str
700     put :create
701     assert_response :bad_request,
702                     "adding new duplicate tags to a way should fail with 'bad request'"
703     assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
704   end
705
706   ##
707   # test that a call to ways_for_node returns all ways that contain the node
708   # and none that don't.
709   def test_ways_for_node
710     node = create(:node)
711     way1 = create(:way)
712     way2 = create(:way)
713     create(:way_node, :way => way1, :node => node)
714     create(:way_node, :way => way2, :node => node)
715     # create an unrelated way
716     create(:way_with_nodes, :nodes_count => 2)
717     # create a way which used to use the node
718     way3_v1 = create(:old_way, :version => 1)
719     _way3_v2 = create(:old_way, :current_way => way3_v1.current_way, :version => 2)
720     create(:old_way_node, :old_way => way3_v1, :node => node)
721
722     get :ways_for_node, :id => node.id
723     assert_response :success
724     ways_xml = XML::Parser.string(@response.body).parse
725     assert_not_nil ways_xml, "failed to parse ways_for_node response"
726
727     # check that the set of IDs match expectations
728     expected_way_ids = [way1.id,
729                         way2.id]
730     found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
731     assert_equal expected_way_ids.sort, found_way_ids.sort,
732                  "expected ways for node #{node.id} did not match found"
733
734     # check the full ways to ensure we're not missing anything
735     expected_way_ids.each do |id|
736       way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
737       assert_ways_are_equal(Way.find(id),
738                             Way.from_xml_node(way_xml))
739     end
740   end
741
742   ##
743   # update the changeset_id of a way element
744   def update_changeset(xml, changeset_id)
745     xml_attr_rewrite(xml, "changeset", changeset_id)
746   end
747
748   ##
749   # update an attribute in the way element
750   def xml_attr_rewrite(xml, name, value)
751     xml.find("//osm/way").first[name] = value.to_s
752     xml
753   end
754
755   ##
756   # replace a node in a way element
757   def xml_replace_node(xml, old_node, new_node)
758     xml.find("//osm/way/nd[@ref='#{old_node}']").first["ref"] = new_node.to_s
759     xml
760   end
761 end