]> git.openstreetmap.org Git - rails.git/blob - test/controllers/ways_controller_test.rb
Merge remote-tracking branch 'upstream/pull/2134'
[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 => "show", :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 => "index" }
30     )
31   end
32
33   # -------------------------------------
34   # Test showing ways.
35   # -------------------------------------
36
37   def test_show
38     # check that a visible way is returned properly
39     get :show, :params => { :id => create(:way).id }
40     assert_response :success
41
42     # check that an invisible way is not returned
43     get :show, :params => { :id => create(:way, :deleted).id }
44     assert_response :gone
45
46     # check chat a non-existent way is not returned
47     get :show, :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_index
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 :index
89     assert_response :bad_request
90
91     # check error when no parameter value provided
92     get :index, :params => { :ways => "" }
93     assert_response :bad_request
94
95     # test a working call
96     get :index, :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 :index, :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     xml = "<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, :body => xml
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     xml = "<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, :body => xml
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     xml = "<osm><way changeset='#{private_open_changeset.id}'>" \
191           "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
192     put :create, :body => xml
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     xml = "<osm><way changeset='#{private_open_changeset.id}'>" \
199           "<tag k='test' v='yes' /></way></osm>"
200     put :create, :body => xml
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     xml = "<osm><way changeset='#{private_closed_changeset.id}'>" \
207           "<nd ref='#{node.id}'/></way></osm>"
208     put :create, :body => xml
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     xml = "<osm><way changeset='#{open_changeset.id}'>" \
219           "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
220     put :create, :body => xml
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     xml = "<osm><way changeset='#{open_changeset.id}'>" \
228           "<tag k='test' v='yes' /></way></osm>"
229     put :create, :body => xml
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     xml = "<osm><way changeset='#{closed_changeset.id}'>" \
237           "<nd ref='#{node.id}'/></way></osm>"
238     put :create, :body => xml
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     xml = "<osm><way changeset='#{open_changeset.id}'>" \
245           "<nd ref='#{node.id}'/>" \
246           "<tag k='foo' v='#{'x' * 256}'/>" \
247           "</way></osm>"
248     put :create, :body => xml
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     xml = "<osm><way id='#{private_way.id}'/></osm>"
288     delete :delete, :params => { :id => private_way.id }, :body => xml.to_s
289     assert_response :forbidden
290
291     # try to delete with an invalid (closed) changeset
292     xml = update_changeset(private_way.to_xml, private_closed_changeset.id)
293     delete :delete, :params => { :id => private_way.id }, :body => xml.to_s
294     assert_response :forbidden
295
296     # try to delete with an invalid (non-existent) changeset
297     xml = update_changeset(private_way.to_xml, 0)
298     delete :delete, :params => { :id => private_way.id }, :body => xml.to_s
299     assert_response :forbidden
300
301     # Now try with a valid changeset
302     xml = private_way.to_xml
303     delete :delete, :params => { :id => private_way.id }, :body => xml.to_s
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     xml = private_deleted_way.to_xml
314     delete :delete, :params => { :id => private_deleted_way.id }, :body => xml.to_s
315     assert_response :forbidden
316
317     # this shouldn't work as the way is used in a relation
318     xml = private_used_way.to_xml
319     delete :delete, :params => { :id => private_used_way.id }, :body => xml.to_s
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     xml = "<osm><way id='#{way.id}'/></osm>"
337     delete :delete, :params => { :id => way.id }, :body => xml.to_s
338     assert_response :bad_request
339
340     # try to delete with an invalid (closed) changeset
341     xml = update_changeset(way.to_xml, closed_changeset.id)
342     delete :delete, :params => { :id => way.id }, :body => xml.to_s
343     assert_response :conflict
344
345     # try to delete with an invalid (non-existent) changeset
346     xml = update_changeset(way.to_xml, 0)
347     delete :delete, :params => { :id => way.id }, :body => xml.to_s
348     assert_response :conflict
349
350     # Now try with a valid changeset
351     xml = way.to_xml
352     delete :delete, :params => { :id => way.id }, :body => xml.to_s
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     xml = deleted_way.to_xml
363     delete :delete, :params => { :id => deleted_way.id }, :body => xml.to_s
364     assert_response :gone
365
366     # this shouldn't work as the way is used in a relation
367     xml = used_way.to_xml
368     delete :delete, :params => { :id => used_way.id }, :body => xml.to_s
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     xml = way.to_xml
393     put :update, :params => { :id => way.id }, :body => xml.to_s
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     xml = update_changeset(private_way.to_xml,
405                            create(:changeset).id)
406     put :update, :params => { :id => private_way.id }, :body => xml.to_s
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     xml = update_changeset(private_way.to_xml,
411                            create(:changeset, :closed, :user => private_user).id)
412     put :update, :params => { :id => private_way.id }, :body => xml.to_s
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     xml = update_changeset(private_way.to_xml, 0)
417     put :update, :params => { :id => private_way.id }, :body => xml.to_s
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     xml = xml_replace_node(private_way.to_xml, node.id, 9999)
422     put :update, :params => { :id => private_way.id }, :body => xml.to_s
423     assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"
424
425     xml = xml_replace_node(private_way.to_xml, node.id, create(:node, :deleted).id)
426     put :update, :params => { :id => private_way.id }, :body => xml.to_s
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     xml = private_way.to_xml
431     put :update, :params => { :id => private_way.id }, :body => xml.to_s
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     xml = update_changeset(way.to_xml,
443                            create(:changeset).id)
444     put :update, :params => { :id => way.id }, :body => xml.to_s
445     assert_response :conflict, "update with other user's changeset should be rejected"
446
447     # try and update in a closed changeset
448     xml = update_changeset(way.to_xml,
449                            create(:changeset, :closed, :user => user).id)
450     put :update, :params => { :id => way.id }, :body => xml.to_s
451     assert_response :conflict, "update with closed changeset should be rejected"
452
453     # try and update in a non-existant changeset
454     xml = update_changeset(way.to_xml, 0)
455     put :update, :params => { :id => way.id }, :body => xml.to_s
456     assert_response :conflict, "update with changeset=0 should be rejected"
457
458     ## try and submit invalid updates
459     xml = xml_replace_node(way.to_xml, node.id, 9999)
460     put :update, :params => { :id => way.id }, :body => xml.to_s
461     assert_response :precondition_failed, "way with non-existent node should be rejected"
462
463     xml = xml_replace_node(way.to_xml, node.id, create(:node, :deleted).id)
464     put :update, :params => { :id => way.id }, :body => xml.to_s
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     xml = xml_attr_rewrite(way.to_xml,
472                            "version", current_way_version - 1)
473     put :update, :params => { :id => way.id }, :body => xml.to_s
474     assert_response :conflict, "should have failed on old version number"
475
476     # try and submit a version ahead
477     xml = xml_attr_rewrite(way.to_xml,
478                            "version", current_way_version + 1)
479     put :update, :params => { :id => way.id }, :body => xml.to_s
480     assert_response :conflict, "should have failed on skipped version number"
481
482     # try and submit total crap in the version field
483     xml = xml_attr_rewrite(way.to_xml,
484                            "version", "p1r4t3s!")
485     put :update, :params => { :id => way.id }, :body => xml.to_s
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     xml = create(:way).to_xml
491     put :update, :params => { :id => way.id }, :body => xml.to_s
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     xml = "<update/>"
497     put :update, :params => { :id => way.id }, :body => xml.to_s
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     xml = way.to_xml
503     put :update, :params => { :id => way.id }, :body => xml.to_s
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     put :update, :params => { :id => private_way.id }, :body => way_xml.to_s
534     assert_response :forbidden,
535                     "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
536
537     ## Now try with the public user
538     # setup auth
539     basic_authorization user.email, "test"
540
541     # add an identical tag to the way
542     tag_xml = XML::Node.new("tag")
543     tag_xml["k"] = "new"
544     tag_xml["v"] = "yes"
545
546     # add the tag into the existing xml
547     way_xml = way.to_xml
548     way_xml.find("//osm/way").first << tag_xml
549
550     # try and upload it
551     put :update, :params => { :id => way.id }, :body => way_xml.to_s
552     assert_response :success,
553                     "adding a new tag to a way should succeed"
554     assert_equal way.version + 1, @response.body.to_i
555   end
556
557   ##
558   # Try adding a duplicate of an existing tag to a way
559   def test_add_duplicate_tags
560     private_user = create(:user, :data_public => false)
561     private_way = create(:way, :changeset => create(:changeset, :user => private_user))
562     private_existing_tag = create(:way_tag, :way => private_way)
563     user = create(:user)
564     way = create(:way, :changeset => create(:changeset, :user => user))
565     existing_tag = create(:way_tag, :way => way)
566
567     ## Try with the non-public user
568     # setup auth
569     basic_authorization private_user.email, "test"
570
571     # add an identical tag to the way
572     tag_xml = XML::Node.new("tag")
573     tag_xml["k"] = private_existing_tag.k
574     tag_xml["v"] = private_existing_tag.v
575
576     # add the tag into the existing xml
577     way_xml = private_way.to_xml
578     way_xml.find("//osm/way").first << tag_xml
579
580     # try and upload it
581     put :update, :params => { :id => private_way.id }, :body => way_xml.to_s
582     assert_response :forbidden,
583                     "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
584
585     ## Now try with the public user
586     # setup auth
587     basic_authorization user.email, "test"
588
589     # add an identical tag to the way
590     tag_xml = XML::Node.new("tag")
591     tag_xml["k"] = existing_tag.k
592     tag_xml["v"] = existing_tag.v
593
594     # add the tag into the existing xml
595     way_xml = way.to_xml
596     way_xml.find("//osm/way").first << tag_xml
597
598     # try and upload it
599     put :update, :params => { :id => way.id }, :body => way_xml.to_s
600     assert_response :bad_request,
601                     "adding a duplicate tag to a way should fail with 'bad request'"
602     assert_equal "Element way/#{way.id} has duplicate tags with key #{existing_tag.k}", @response.body
603   end
604
605   ##
606   # Try adding a new duplicate tags to a way
607   def test_new_duplicate_tags
608     private_user = create(:user, :data_public => false)
609     private_way = create(:way, :changeset => create(:changeset, :user => private_user))
610     user = create(:user)
611     way = create(:way, :changeset => create(:changeset, :user => user))
612
613     ## First test with the non-public user so should be rejected
614     # setup auth
615     basic_authorization private_user.email, "test"
616
617     # create duplicate tag
618     tag_xml = XML::Node.new("tag")
619     tag_xml["k"] = "i_am_a_duplicate"
620     tag_xml["v"] = "foobar"
621
622     # add the tag into the existing xml
623     way_xml = private_way.to_xml
624
625     # add two copies of the tag
626     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
627
628     # try and upload it
629     put :update, :params => { :id => private_way.id }, :body => way_xml.to_s
630     assert_response :forbidden,
631                     "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
632
633     ## Now test with the public user
634     # setup auth
635     basic_authorization user.email, "test"
636
637     # create duplicate tag
638     tag_xml = XML::Node.new("tag")
639     tag_xml["k"] = "i_am_a_duplicate"
640     tag_xml["v"] = "foobar"
641
642     # add the tag into the existing xml
643     way_xml = way.to_xml
644
645     # add two copies of the tag
646     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
647
648     # try and upload it
649     put :update, :params => { :id => way.id }, :body => way_xml.to_s
650     assert_response :bad_request,
651                     "adding new duplicate tags to a way should fail with 'bad request'"
652     assert_equal "Element way/#{way.id} has duplicate tags with key i_am_a_duplicate", @response.body
653   end
654
655   ##
656   # Try adding a new duplicate tags to a way.
657   # But be a bit subtle - use unicode decoding ambiguities to use different
658   # binary strings which have the same decoding.
659   def test_invalid_duplicate_tags
660     private_user = create(:user, :data_public => false)
661     private_changeset = create(:changeset, :user => private_user)
662     user = create(:user)
663     changeset = create(:changeset, :user => user)
664
665     ## First make sure that you can't with a non-public user
666     # setup auth
667     basic_authorization private_user.email, "test"
668
669     # add the tag into the existing xml
670     way_str = "<osm><way changeset='#{private_changeset.id}'>"
671     way_str << "<tag k='addr:housenumber' v='1'/>"
672     way_str << "<tag k='addr:housenumber' v='2'/>"
673     way_str << "</way></osm>"
674
675     # try and upload it
676     put :create, :body => way_str
677     assert_response :forbidden,
678                     "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
679
680     ## Now do it with a public user
681     # setup auth
682     basic_authorization user.email, "test"
683
684     # add the tag into the existing xml
685     way_str = "<osm><way changeset='#{changeset.id}'>"
686     way_str << "<tag k='addr:housenumber' v='1'/>"
687     way_str << "<tag k='addr:housenumber' v='2'/>"
688     way_str << "</way></osm>"
689
690     # try and upload it
691     put :create, :body => way_str
692     assert_response :bad_request,
693                     "adding new duplicate tags to a way should fail with 'bad request'"
694     assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
695   end
696
697   ##
698   # test that a call to ways_for_node returns all ways that contain the node
699   # and none that don't.
700   def test_ways_for_node
701     node = create(:node)
702     way1 = create(:way)
703     way2 = create(:way)
704     create(:way_node, :way => way1, :node => node)
705     create(:way_node, :way => way2, :node => node)
706     # create an unrelated way
707     create(:way_with_nodes, :nodes_count => 2)
708     # create a way which used to use the node
709     way3_v1 = create(:old_way, :version => 1)
710     _way3_v2 = create(:old_way, :current_way => way3_v1.current_way, :version => 2)
711     create(:old_way_node, :old_way => way3_v1, :node => node)
712
713     get :ways_for_node, :params => { :id => node.id }
714     assert_response :success
715     ways_xml = XML::Parser.string(@response.body).parse
716     assert_not_nil ways_xml, "failed to parse ways_for_node response"
717
718     # check that the set of IDs match expectations
719     expected_way_ids = [way1.id,
720                         way2.id]
721     found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
722     assert_equal expected_way_ids.sort, found_way_ids.sort,
723                  "expected ways for node #{node.id} did not match found"
724
725     # check the full ways to ensure we're not missing anything
726     expected_way_ids.each do |id|
727       way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
728       assert_ways_are_equal(Way.find(id),
729                             Way.from_xml_node(way_xml))
730     end
731   end
732
733   ##
734   # update the changeset_id of a way element
735   def update_changeset(xml, changeset_id)
736     xml_attr_rewrite(xml, "changeset", changeset_id)
737   end
738
739   ##
740   # update an attribute in the way element
741   def xml_attr_rewrite(xml, name, value)
742     xml.find("//osm/way").first[name] = value.to_s
743     xml
744   end
745
746   ##
747   # replace a node in a way element
748   def xml_replace_node(xml, old_node, new_node)
749     xml.find("//osm/way/nd[@ref='#{old_node}']").first["ref"] = new_node.to_s
750     xml
751   end
752 end