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