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