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