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