]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/nodes_controller_test.rb
Speed up LoginTest by avoiding redundant tests
[rails.git] / test / controllers / api / nodes_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4 require_relative "elements_test_helper"
5
6 module Api
7   class NodesControllerTest < ActionDispatch::IntegrationTest
8     include ElementsTestHelper
9
10     ##
11     # test all routes which lead to this controller
12     def test_routes
13       assert_routing(
14         { :path => "/api/0.6/nodes", :method => :get },
15         { :controller => "api/nodes", :action => "index" }
16       )
17       assert_routing(
18         { :path => "/api/0.6/nodes.json", :method => :get },
19         { :controller => "api/nodes", :action => "index", :format => "json" }
20       )
21       assert_routing(
22         { :path => "/api/0.6/nodes", :method => :post },
23         { :controller => "api/nodes", :action => "create" }
24       )
25       assert_routing(
26         { :path => "/api/0.6/node/1", :method => :get },
27         { :controller => "api/nodes", :action => "show", :id => "1" }
28       )
29       assert_routing(
30         { :path => "/api/0.6/node/1.json", :method => :get },
31         { :controller => "api/nodes", :action => "show", :id => "1", :format => "json" }
32       )
33       assert_routing(
34         { :path => "/api/0.6/node/1", :method => :put },
35         { :controller => "api/nodes", :action => "update", :id => "1" }
36       )
37       assert_routing(
38         { :path => "/api/0.6/node/1", :method => :delete },
39         { :controller => "api/nodes", :action => "destroy", :id => "1" }
40       )
41
42       assert_recognizes(
43         { :controller => "api/nodes", :action => "create" },
44         { :path => "/api/0.6/node/create", :method => :put }
45       )
46     end
47
48     ##
49     # test fetching multiple nodes
50     def test_index
51       node1 = create(:node)
52       node2 = create(:node, :deleted)
53       node3 = create(:node)
54       node4 = create(:node, :with_history, :version => 2)
55       node5 = create(:node, :deleted, :with_history, :version => 2)
56
57       # check error when no parameter provided
58       get api_nodes_path
59       assert_response :bad_request
60
61       # check error when no parameter value provided
62       get api_nodes_path(:nodes => "")
63       assert_response :bad_request
64
65       # test a working call
66       get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}")
67       assert_response :success
68       assert_select "osm" do
69         assert_select "node", :count => 5
70         assert_select "node[id='#{node1.id}'][visible='true']", :count => 1
71         assert_select "node[id='#{node2.id}'][visible='false']", :count => 1
72         assert_select "node[id='#{node3.id}'][visible='true']", :count => 1
73         assert_select "node[id='#{node4.id}'][visible='true']", :count => 1
74         assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
75       end
76
77       # test a working call with json format
78       get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json")
79
80       js = ActiveSupport::JSON.decode(@response.body)
81       assert_not_nil js
82       assert_equal 5, js["elements"].count
83       assert_equal(5, js["elements"].count { |a| a["type"] == "node" })
84       assert_equal(1, js["elements"].count { |a| a["id"] == node1.id && a["visible"].nil? })
85       assert_equal(1, js["elements"].count { |a| a["id"] == node2.id && a["visible"] == false })
86       assert_equal(1, js["elements"].count { |a| a["id"] == node3.id && a["visible"].nil? })
87       assert_equal(1, js["elements"].count { |a| a["id"] == node4.id && a["visible"].nil? })
88       assert_equal(1, js["elements"].count { |a| a["id"] == node5.id && a["visible"] == false })
89
90       # check error when a non-existent node is included
91       get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0")
92       assert_response :not_found
93     end
94
95     def test_create_when_unauthorized
96       with_unchanging_request do |_headers, changeset|
97         osm = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
98
99         post api_nodes_path, :params => osm
100
101         assert_response :unauthorized
102       end
103     end
104
105     def test_create_by_private_user
106       with_unchanging_request([{ :data_public => false }]) do |headers, changeset|
107         osm = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
108
109         post api_nodes_path, :params => osm, :headers => headers
110
111         assert_require_public_data "node create did not return forbidden status"
112       end
113     end
114
115     def test_create
116       with_request do |headers, changeset|
117         lat = rand(-50..50) + rand
118         lon = rand(-50..50) + rand
119
120         assert_difference "Node.count", 1 do
121           osm = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
122
123           post api_nodes_path, :params => osm, :headers => headers
124
125           assert_response :success, "node upload did not return success status"
126         end
127
128         created_node_id = @response.body
129         node = Node.find(created_node_id)
130         assert_in_delta lat * 10000000, node.latitude, 1, "saved node does not match requested latitude"
131         assert_in_delta lon * 10000000, node.longitude, 1, "saved node does not match requested longitude"
132         assert_equal changeset.id, node.changeset_id, "saved node does not belong to changeset that it was created in"
133         assert node.visible, "saved node is not visible"
134
135         changeset.reload
136         assert_equal 1, changeset.num_changes
137         assert_predicate changeset, :num_type_changes_in_sync?
138         assert_equal 1, changeset.num_created_nodes
139       end
140     end
141
142     def test_create_in_missing_changeset
143       with_unchanging_request do |headers|
144         osm = "<osm><node lat='0' lon='0' changeset='0'/></osm>"
145
146         post api_nodes_path, :params => osm, :headers => headers
147
148         assert_response :conflict
149       end
150     end
151
152     def test_create_with_invalid_osm_structure
153       with_unchanging_request do |headers|
154         osm = "<create/>"
155
156         post api_nodes_path, :params => osm, :headers => headers
157
158         assert_response :bad_request, "node upload did not return bad_request status"
159         assert_equal "Cannot parse valid node from xml string <create/>. XML doesn't contain an osm/node element.", @response.body
160       end
161     end
162
163     def test_create_without_lat
164       with_unchanging_request do |headers, changeset|
165         osm = "<osm><node lon='3.23' changeset='#{changeset.id}'/></osm>"
166
167         post api_nodes_path, :params => osm, :headers => headers
168
169         assert_response :bad_request, "node upload did not return bad_request status"
170         assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
171       end
172     end
173
174     def test_create_without_lon
175       with_unchanging_request do |headers, changeset|
176         osm = "<osm><node lat='3.434' changeset='#{changeset.id}'/></osm>"
177
178         post api_nodes_path, :params => osm, :headers => headers
179
180         assert_response :bad_request, "node upload did not return bad_request status"
181         assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
182       end
183     end
184
185     def test_create_with_non_numeric_lat
186       with_unchanging_request do |headers, changeset|
187         osm = "<osm><node lat='abc' lon='3.23' changeset='#{changeset.id}'/></osm>"
188
189         post api_nodes_path, :params => osm, :headers => headers
190
191         assert_response :bad_request, "node upload did not return bad_request status"
192         assert_equal "Cannot parse valid node from xml string <node lat=\"abc\" lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat not a number", @response.body
193       end
194     end
195
196     def test_create_with_non_numeric_lon
197       with_unchanging_request do |headers, changeset|
198         osm = "<osm><node lat='3.434' lon='abc' changeset='#{changeset.id}'/></osm>"
199
200         post api_nodes_path, :params => osm, :headers => headers
201
202         assert_response :bad_request, "node upload did not return bad_request status"
203         assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" lon=\"abc\" changeset=\"#{changeset.id}\"/>. lon not a number", @response.body
204       end
205     end
206
207     def test_create_with_tag_too_long
208       with_unchanging_request do |headers, changeset|
209         osm = "<osm><node lat='3.434' lon='3.23' changeset='#{changeset.id}'><tag k='foo' v='#{'x' * 256}'/></node></osm>"
210
211         post api_nodes_path, :params => osm, :headers => headers
212
213         assert_response :bad_request, "node upload did not return bad_request status"
214         assert_match(/ v: is too long \(maximum is 255 characters\) /, @response.body)
215       end
216     end
217
218     ##
219     # try and put something into a string that the API might
220     # use unquoted and therefore allow code injection
221     def test_create_with_string_injection_by_private_user
222       with_unchanging_request([{ :data_public => false }]) do |headers, changeset|
223         osm = <<~OSM
224           <osm>
225             <node lat='0' lon='0' changeset='#{changeset.id}'>
226               <tag k='\#{@user.inspect}' v='0'/>
227             </node>
228           </osm>
229         OSM
230
231         post api_nodes_path, :params => osm, :headers => headers
232
233         assert_require_public_data "Shouldn't be able to create with non-public user"
234       end
235     end
236
237     ##
238     # try and put something into a string that the API might
239     # use unquoted and therefore allow code injection
240     def test_create_with_string_injection
241       with_request do |headers, changeset|
242         assert_difference "Node.count", 1 do
243           osm = <<~OSM
244             <osm>
245               <node lat='0' lon='0' changeset='#{changeset.id}'>
246                 <tag k='\#{@user.inspect}' v='0'/>
247               </node>
248             </osm>
249           OSM
250
251           post api_nodes_path, :params => osm, :headers => headers
252
253           assert_response :success
254         end
255
256         created_node_id = @response.body
257         db_node = Node.find(created_node_id)
258
259         get api_node_path(created_node_id)
260
261         assert_response :success
262
263         api_node = Node.from_xml(@response.body)
264         assert_not_nil api_node, "downloaded node is nil, but shouldn't be"
265         assert_equal db_node.tags, api_node.tags, "tags are corrupted"
266         assert_includes api_node.tags, "\#{@user.inspect}"
267       end
268     end
269
270     def test_create_race_condition
271       user = create(:user)
272       changeset = create(:changeset, :user => user)
273       auth_header = bearer_authorization_header user
274       path = api_nodes_path
275       concurrency_level = 16
276
277       threads = Array.new(concurrency_level) do
278         Thread.new do
279           osm = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
280           post path, :params => osm, :headers => auth_header
281         end
282       end
283       threads.each(&:join)
284
285       changeset.reload
286       assert_equal concurrency_level, changeset.num_changes
287       assert_predicate changeset, :num_type_changes_in_sync?
288       assert_equal concurrency_level, changeset.num_created_nodes
289     end
290
291     def test_show_not_found
292       get api_node_path(0)
293       assert_response :not_found
294     end
295
296     def test_show_deleted
297       get api_node_path(create(:node, :deleted))
298       assert_response :gone
299     end
300
301     def test_show
302       node = create(:node, :timestamp => "2021-02-03T00:00:00Z")
303
304       get api_node_path(node)
305
306       assert_response :success
307       assert_not_nil @response.header["Last-Modified"]
308       assert_equal "2021-02-03T00:00:00Z", Time.parse(@response.header["Last-Modified"]).utc.xmlschema
309     end
310
311     ##
312     # unexpected errors should not be caught, so that they can be
313     # reported and turned into an error response by rails
314     def test_show_unexpected_error
315       node = create(:node)
316
317       Node.stub(:includes, ->(*) { raise ActiveRecord::ConnectionNotEstablished, "database unavailable" }) do
318         assert_raises ActiveRecord::ConnectionNotEstablished do
319           get api_node_path(node)
320         end
321       end
322     end
323
324     def test_show_lat_lon_decimal_format
325       node = create(:node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
326
327       get api_node_path(node)
328
329       assert_match(/lat="0.0000400"/, response.body)
330       assert_match(/lon="0.0000800"/, response.body)
331     end
332
333     def test_destroy_when_unauthorized
334       with_unchanging(:node) do |node|
335         delete api_node_path(node)
336
337         assert_response :unauthorized
338       end
339     end
340
341     def test_destroy_in_closed_changeset_by_private_user
342       with_unchanging(:node) do |node|
343         with_unchanging_request([{ :data_public => false }], [:closed]) do |headers, changeset|
344           osm_xml = xml_for_node node
345           osm_xml = update_changeset osm_xml, changeset.id
346
347           delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
348
349           assert_require_public_data "non-public user shouldn't be able to delete node"
350         end
351       end
352     end
353
354     def test_destroy_in_missing_changeset_by_private_user
355       with_unchanging(:node) do |node|
356         with_unchanging_request([{ :data_public => false }]) do |headers|
357           osm_xml = xml_for_node node
358           osm_xml = update_changeset osm_xml, 0
359
360           delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
361
362           assert_require_public_data "shouldn't be able to delete node, when user's data is private"
363         end
364       end
365     end
366
367     def test_destroy_by_private_user
368       with_unchanging(:node) do |node|
369         with_unchanging_request([{ :data_public => false }]) do |headers, changeset|
370           osm_xml = xml_for_node node
371           osm_xml = update_changeset osm_xml, changeset.id
372
373           delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
374
375           assert_require_public_data "shouldn't be able to delete node when user's data isn't public'"
376         end
377       end
378     end
379
380     def test_destroy_deleted_node_by_private_user
381       with_unchanging(:node, :deleted) do |node|
382         with_unchanging_request([{ :data_public => false }]) do |headers, changeset|
383           osm_xml = "<osm><node id='#{node.id}' changeset='#{changeset.id}' version='1' lat='0' lon='0'/></osm>"
384
385           delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
386
387           assert_require_public_data
388         end
389       end
390     end
391
392     def test_destroy_missing_node_by_private_user
393       with_unchanging_request([{ :data_public => false }]) do |headers|
394         delete api_node_path(0), :headers => headers
395
396         assert_require_public_data
397       end
398     end
399
400     def test_destroy_node_in_way_by_private_user
401       with_unchanging(:node) do |node|
402         create(:way_node, :node => node)
403
404         with_unchanging_request([{ :data_public => false }]) do |headers, changeset|
405           osm_xml = xml_for_node node
406           osm_xml = update_changeset osm_xml, changeset.id
407
408           delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
409
410           assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
411         end
412       end
413     end
414
415     def test_destroy_node_in_relation_by_private_user
416       with_unchanging(:node) do |node|
417         create(:relation_member, :member => node)
418
419         with_unchanging_request([{ :data_public => false }]) do |headers, changeset|
420           osm_xml = xml_for_node node
421           osm_xml = update_changeset osm_xml, changeset.id
422
423           delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
424
425           assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
426         end
427       end
428     end
429
430     def test_destroy_in_closed_changeset
431       with_unchanging(:node) do |node|
432         with_unchanging_request([], [:closed]) do |headers, changeset|
433           osm_xml = xml_for_node node
434           osm_xml = update_changeset osm_xml, changeset.id
435
436           delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
437
438           assert_response :conflict
439         end
440       end
441     end
442
443     def test_destroy_in_missing_changeset
444       with_unchanging(:node) do |node|
445         with_unchanging_request do |headers|
446           osm_xml = xml_for_node node
447           osm_xml = update_changeset osm_xml, 0
448
449           delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
450
451           assert_response :conflict
452         end
453       end
454     end
455
456     def test_destroy_different_node
457       with_unchanging(:node) do |node|
458         with_unchanging(:node) do |other_node|
459           with_unchanging_request do |headers, changeset|
460             osm_xml = xml_for_node other_node
461             osm_xml = update_changeset osm_xml, changeset.id
462
463             delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
464
465             assert_response :bad_request, "should not be able to delete a node with a different ID from the XML"
466           end
467         end
468       end
469     end
470
471     def test_destroy_invalid_osm_structure
472       with_unchanging(:node) do |node|
473         with_unchanging_request do |headers|
474           osm = "<delete/>"
475
476           delete api_node_path(node), :params => osm, :headers => headers
477
478           assert_response :bad_request, "should not be able to delete a node without a valid XML payload"
479         end
480       end
481     end
482
483     def test_destroy
484       with_request do |headers, changeset|
485         node = create(:node)
486         osm_xml = xml_for_node node
487         osm_xml = update_changeset osm_xml, changeset.id
488
489         delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
490
491         assert_response :success
492
493         response_node_version = @response.body.to_i
494         assert_operator response_node_version, :>, node.version, "delete request should return a new version number for node"
495         node.reload
496         assert_not_predicate node, :visible?
497         assert_equal response_node_version, node.version
498
499         changeset.reload
500         assert_equal 1, changeset.num_changes
501         assert_predicate changeset, :num_type_changes_in_sync?
502         assert_equal 1, changeset.num_deleted_nodes
503       end
504     end
505
506     def test_destroy_twice
507       user = create(:user)
508       node = create(:node, :changeset => create(:changeset, :user => user))
509       osm_xml = xml_for_node node
510
511       delete api_node_path(node), :params => osm_xml.to_s, :headers => bearer_authorization_header(user)
512
513       assert_response :success
514
515       delete api_node_path(node), :params => osm_xml.to_s, :headers => bearer_authorization_header(user)
516
517       assert_response :gone
518     end
519
520     def test_destroy_deleted_node
521       with_unchanging(:node, :deleted) do |node|
522         with_unchanging_request do |headers, changeset|
523           osm = "<osm><node id='#{node.id}' changeset='#{changeset.id}' version='1' lat='0' lon='0'/></osm>"
524
525           delete api_node_path(node), :params => osm, :headers => headers
526
527           assert_response :gone
528         end
529       end
530     end
531
532     def test_destroy_missing_node
533       with_unchanging_request do |headers|
534         delete api_node_path(0), :headers => headers
535
536         assert_response :not_found
537       end
538     end
539
540     def test_destroy_node_in_ways
541       with_unchanging(:node) do |node|
542         way_node = create(:way_node, :node => node)
543         way_node2 = create(:way_node, :node => node)
544
545         with_unchanging_request do |headers, changeset|
546           osm_xml = xml_for_node node
547           osm_xml = update_changeset osm_xml, changeset.id
548
549           delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
550
551           assert_response :precondition_failed, "shouldn't be able to delete a node used in a way (#{@response.body})"
552           assert_equal "Precondition failed: Node #{node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
553         end
554       end
555     end
556
557     def test_destroy_node_in_relations
558       with_unchanging(:node) do |node|
559         relation_member = create(:relation_member, :member => node)
560         relation_member2 = create(:relation_member, :member => node)
561
562         with_unchanging_request do |headers, changeset|
563           osm_xml = xml_for_node node
564           osm_xml = update_changeset osm_xml, changeset.id
565
566           delete api_node_path(node), :params => osm_xml.to_s, :headers => headers
567
568           assert_response :precondition_failed, "shouldn't be able to delete a node used in a relation (#{@response.body})"
569           assert_equal "Precondition failed: Node #{node.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
570         end
571       end
572     end
573
574     def test_update_when_unauthorized
575       with_unchanging(:node) do |node|
576         osm_xml = xml_for_node node
577
578         put api_node_path(node), :params => osm_xml.to_s
579
580         assert_response :unauthorized
581       end
582     end
583
584     def test_update_in_changeset_of_other_user_by_private_user
585       with_unchanging(:node) do |node|
586         other_user = create(:user)
587
588         with_unchanging_request([{ :data_public => false }], [{ :user => other_user }]) do |headers, changeset|
589           osm_xml = xml_for_node node
590           osm_xml = update_changeset osm_xml, changeset.id
591
592           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
593
594           assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
595         end
596       end
597     end
598
599     def test_update_in_closed_changeset_by_private_user
600       with_unchanging(:node) do |node|
601         with_unchanging_request([{ :data_public => false }], [:closed]) do |headers, changeset|
602           osm_xml = xml_for_node node
603           osm_xml = update_changeset osm_xml, changeset.id
604
605           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
606
607           assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
608         end
609       end
610     end
611
612     def test_update_in_missing_changeset_by_private_user
613       with_unchanging(:node) do |node|
614         with_unchanging_request([{ :data_public => false }]) do |headers|
615           osm_xml = xml_for_node node
616           osm_xml = update_changeset osm_xml, 0
617
618           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
619
620           assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
621         end
622       end
623     end
624
625     def test_update_with_lat_too_large_by_private_user
626       check_update_with_invalid_attr_value "lat", 91.0, :data_public => false
627     end
628
629     def test_update_with_lat_too_small_by_private_user
630       check_update_with_invalid_attr_value "lat", -91.0, :data_public => false
631     end
632
633     def test_update_with_lon_too_large_by_private_user
634       check_update_with_invalid_attr_value "lon", 181.0, :data_public => false
635     end
636
637     def test_update_with_lon_too_small_by_private_user
638       check_update_with_invalid_attr_value "lon", -181.0, :data_public => false
639     end
640
641     def test_update_by_private_user
642       with_unchanging(:node) do |node|
643         with_unchanging_request([{ :data_public => false }]) do |headers, changeset|
644           osm_xml = xml_for_node node
645           osm_xml = update_changeset osm_xml, changeset.id
646
647           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
648
649           assert_require_public_data "should have failed with a forbidden when data isn't public"
650         end
651       end
652     end
653
654     def test_update_in_changeset_of_other_user
655       with_unchanging(:node) do |node|
656         other_user = create(:user)
657
658         with_unchanging_request([], [{ :user => other_user }]) do |headers, changeset|
659           osm_xml = xml_for_node node
660           osm_xml = update_changeset osm_xml, changeset.id
661
662           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
663
664           assert_response :conflict, "update with other user's changeset should be rejected"
665         end
666       end
667     end
668
669     def test_update_in_closed_changeset
670       with_unchanging(:node) do |node|
671         with_unchanging_request([], [:closed]) do |headers, changeset|
672           osm_xml = xml_for_node node
673           osm_xml = update_changeset osm_xml, changeset.id
674
675           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
676
677           assert_response :conflict, "update with closed changeset should be rejected"
678         end
679       end
680     end
681
682     def test_update_in_missing_changeset
683       with_unchanging(:node) do |node|
684         with_unchanging_request do |headers|
685           osm_xml = xml_for_node node
686           osm_xml = update_changeset osm_xml, 0
687
688           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
689
690           assert_response :conflict, "update with changeset=0 should be rejected"
691         end
692       end
693     end
694
695     def test_update_with_lat_too_large
696       check_update_with_invalid_attr_value "lat", 91.0
697     end
698
699     def test_update_with_lat_too_small
700       check_update_with_invalid_attr_value "lat", -91.0
701     end
702
703     def test_update_with_lon_too_large
704       check_update_with_invalid_attr_value "lon", 181.0
705     end
706
707     def test_update_with_lon_too_small
708       check_update_with_invalid_attr_value "lon", -181.0
709     end
710
711     def test_update_with_version_behind
712       with_unchanging(:node, :version => 2) do |node|
713         with_unchanging_request do |headers, changeset|
714           osm_xml = xml_for_node node
715           osm_xml = xml_attr_rewrite osm_xml, "version", node.version - 1
716           osm_xml = update_changeset osm_xml, changeset.id
717
718           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
719
720           assert_response :conflict, "should have failed on old version number"
721         end
722       end
723     end
724
725     def test_update_with_version_ahead
726       with_unchanging(:node, :version => 2) do |node|
727         with_unchanging_request do |headers, changeset|
728           osm_xml = xml_for_node node
729           osm_xml = xml_attr_rewrite osm_xml, "version", node.version + 1
730           osm_xml = update_changeset osm_xml, changeset.id
731
732           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
733
734           assert_response :conflict, "should have failed on skipped version number"
735         end
736       end
737     end
738
739     def test_update_with_invalid_version
740       with_unchanging(:node) do |node|
741         with_unchanging_request do |headers, changeset|
742           osm_xml = xml_for_node node
743           osm_xml = xml_attr_rewrite osm_xml, "version", "p1r4t3s!"
744           osm_xml = update_changeset osm_xml, changeset.id
745
746           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
747
748           assert_response :conflict, "should not be able to put 'p1r4at3s!' in the version field"
749         end
750       end
751     end
752
753     def test_update_other_node
754       with_unchanging(:node) do |node|
755         with_unchanging(:node) do |other_node|
756           with_unchanging_request do |headers, changeset|
757             osm_xml = xml_for_node other_node
758             osm_xml = update_changeset osm_xml, changeset.id
759
760             put api_node_path(node), :params => osm_xml.to_s, :headers => headers
761
762             assert_response :bad_request, "should not be able to update a node with a different ID from the XML"
763           end
764         end
765       end
766     end
767
768     def test_update_with_invalid_osm_structure
769       with_unchanging(:node) do |node|
770         with_unchanging_request do |headers|
771           osm = "<update/>"
772
773           put api_node_path(node), :params => osm, :headers => headers
774
775           assert_response :bad_request, "should not be able to update a node with non-OSM XML doc."
776         end
777       end
778     end
779
780     def test_update
781       with_request do |headers, changeset|
782         node = create(:node)
783         osm_xml = xml_for_node node
784         osm_xml = update_changeset osm_xml, changeset.id
785
786         put api_node_path(node), :params => osm_xml.to_s, :headers => headers
787
788         assert_response :success, "a valid update request failed"
789
790         changeset.reload
791         assert_equal 1, changeset.num_changes
792         assert_predicate changeset, :num_type_changes_in_sync?
793         assert_equal 1, changeset.num_modified_nodes
794       end
795     end
796
797     def test_update_with_duplicate_tags
798       with_unchanging(:node) do |node|
799         create(:node_tag, :node => node, :k => "key_to_duplicate", :v => "value_to_duplicate")
800
801         with_unchanging_request do |headers, changeset|
802           tag_xml = XML::Node.new("tag")
803           tag_xml["k"] = "key_to_duplicate"
804           tag_xml["v"] = "value_to_duplicate"
805
806           osm_xml = xml_for_node node
807           osm_xml.find("//osm/node").first << tag_xml
808           osm_xml = update_changeset osm_xml, changeset.id
809
810           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
811
812           assert_response :bad_request, "adding duplicate tags to a node should fail with 'bad request'"
813           assert_equal "Element node/#{node.id} has duplicate tags with key key_to_duplicate", @response.body
814         end
815       end
816     end
817
818     ##
819     # test initial rate limit
820     def test_initial_rate_limit
821       # create a user
822       user = create(:user)
823
824       # create a changeset that puts us near the initial rate limit
825       changeset = create(:changeset, :user => user,
826                                      :created_at => Time.now.utc - 5.minutes,
827                                      :num_changes => Settings.initial_changes_per_hour - 1)
828
829       # create authentication header
830       auth_header = bearer_authorization_header user
831
832       # try creating a node
833       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
834       post api_nodes_path, :params => xml, :headers => auth_header
835       assert_response :success, "node create did not return success status"
836
837       # get the id of the node we created
838       nodeid = @response.body
839
840       # try updating the node, which should be rate limited
841       xml = "<osm><node id='#{nodeid}' version='1' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
842       put api_node_path(nodeid), :params => xml, :headers => auth_header
843       assert_response :too_many_requests, "node update did not hit rate limit"
844
845       # try deleting the node, which should be rate limited
846       xml = "<osm><node id='#{nodeid}' version='2' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
847       delete api_node_path(nodeid), :params => xml, :headers => auth_header
848       assert_response :too_many_requests, "node delete did not hit rate limit"
849
850       # try creating a node, which should be rate limited
851       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
852       post api_nodes_path, :params => xml, :headers => auth_header
853       assert_response :too_many_requests, "node create did not hit rate limit"
854     end
855
856     ##
857     # test maximum rate limit
858     def test_maximum_rate_limit
859       # create a user
860       user = create(:user)
861
862       # create a changeset to establish our initial edit time
863       changeset = create(:changeset, :user => user,
864                                      :created_at => Time.now.utc - 28.days)
865
866       # create changeset to put us near the maximum rate limit
867       total_changes = Settings.max_changes_per_hour - 1
868       while total_changes.positive?
869         changes = [total_changes, Changeset::MAX_ELEMENTS].min
870         changeset = create(:changeset, :user => user,
871                                        :created_at => Time.now.utc - 5.minutes,
872                                        :num_changes => changes)
873         total_changes -= changes
874       end
875
876       # create authentication header
877       auth_header = bearer_authorization_header user
878
879       # try creating a node
880       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
881       post api_nodes_path, :params => xml, :headers => auth_header
882       assert_response :success, "node create did not return success status"
883
884       # get the id of the node we created
885       nodeid = @response.body
886
887       # try updating the node, which should be rate limited
888       xml = "<osm><node id='#{nodeid}' version='1' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
889       put api_node_path(nodeid), :params => xml, :headers => auth_header
890       assert_response :too_many_requests, "node update did not hit rate limit"
891
892       # try deleting the node, which should be rate limited
893       xml = "<osm><node id='#{nodeid}' version='2' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
894       delete api_node_path(nodeid), :params => xml, :headers => auth_header
895       assert_response :too_many_requests, "node delete did not hit rate limit"
896
897       # try creating a node, which should be rate limited
898       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
899       post api_nodes_path, :params => xml, :headers => auth_header
900       assert_response :too_many_requests, "node create did not hit rate limit"
901     end
902
903     private
904
905     def check_update_with_invalid_attr_value(name, value, data_public: true)
906       with_unchanging(:node) do |node|
907         with_unchanging_request([{ :data_public => data_public }]) do |headers, changeset|
908           osm_xml = xml_for_node node
909           osm_xml = xml_attr_rewrite osm_xml, name, value
910           osm_xml = update_changeset osm_xml, changeset.id
911
912           put api_node_path(node), :params => osm_xml.to_s, :headers => headers
913
914           if data_public
915             assert_response :bad_request, "node at #{name}=#{value} should be rejected"
916           else
917             assert_require_public_data "node at #{name}=#{value} should be forbidden, when data isn't public"
918           end
919         end
920       end
921     end
922
923     def affected_models
924       [Node, NodeTag,
925        OldNode, OldNodeTag]
926     end
927
928     ##
929     # update an attribute in the node element
930     def xml_attr_rewrite(xml, name, value)
931       xml.find("//osm/node").first[name] = value.to_s
932       xml
933     end
934   end
935 end