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