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