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