]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/nodes_controller_test.rb
6ec9085600572c39cbf367cc1797a09e18b839e4
[rails.git] / test / controllers / api / nodes_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class NodesControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/nodes", :method => :get },
10         { :controller => "api/nodes", :action => "index" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/nodes.json", :method => :get },
14         { :controller => "api/nodes", :action => "index", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/nodes", :method => :post },
18         { :controller => "api/nodes", :action => "create" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/node/1", :method => :get },
22         { :controller => "api/nodes", :action => "show", :id => "1" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/node/1.json", :method => :get },
26         { :controller => "api/nodes", :action => "show", :id => "1", :format => "json" }
27       )
28       assert_routing(
29         { :path => "/api/0.6/node/1", :method => :put },
30         { :controller => "api/nodes", :action => "update", :id => "1" }
31       )
32       assert_routing(
33         { :path => "/api/0.6/node/1", :method => :delete },
34         { :controller => "api/nodes", :action => "destroy", :id => "1" }
35       )
36
37       assert_recognizes(
38         { :controller => "api/nodes", :action => "create" },
39         { :path => "/api/0.6/node/create", :method => :put }
40       )
41     end
42
43     ##
44     # test fetching multiple nodes
45     def test_index
46       node1 = create(:node)
47       node2 = create(:node, :deleted)
48       node3 = create(:node)
49       node4 = create(:node, :with_history, :version => 2)
50       node5 = create(:node, :deleted, :with_history, :version => 2)
51
52       # check error when no parameter provided
53       get api_nodes_path
54       assert_response :bad_request
55
56       # check error when no parameter value provided
57       get api_nodes_path(:nodes => "")
58       assert_response :bad_request
59
60       # test a working call
61       get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}")
62       assert_response :success
63       assert_select "osm" do
64         assert_select "node", :count => 5
65         assert_select "node[id='#{node1.id}'][visible='true']", :count => 1
66         assert_select "node[id='#{node2.id}'][visible='false']", :count => 1
67         assert_select "node[id='#{node3.id}'][visible='true']", :count => 1
68         assert_select "node[id='#{node4.id}'][visible='true']", :count => 1
69         assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
70       end
71
72       # test a working call with json format
73       get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json")
74
75       js = ActiveSupport::JSON.decode(@response.body)
76       assert_not_nil js
77       assert_equal 5, js["elements"].count
78       assert_equal 5, (js["elements"].count { |a| a["type"] == "node" })
79       assert_equal 1, (js["elements"].count { |a| a["id"] == node1.id && a["visible"].nil? })
80       assert_equal 1, (js["elements"].count { |a| a["id"] == node2.id && a["visible"] == false })
81       assert_equal 1, (js["elements"].count { |a| a["id"] == node3.id && a["visible"].nil? })
82       assert_equal 1, (js["elements"].count { |a| a["id"] == node4.id && a["visible"].nil? })
83       assert_equal 1, (js["elements"].count { |a| a["id"] == node5.id && a["visible"] == false })
84
85       # check error when a non-existent node is included
86       get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0")
87       assert_response :not_found
88     end
89
90     def test_create
91       private_user = create(:user, :data_public => false)
92       private_changeset = create(:changeset, :user => private_user)
93       user = create(:user)
94       changeset = create(:changeset, :user => user)
95
96       # create a node with random lat/lon
97       lat = rand(-50..50) + rand
98       lon = rand(-50..50) + rand
99
100       ## First try with no auth
101       # create a minimal xml file
102       xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
103       assert_difference("OldNode.count", 0) do
104         post api_nodes_path, :params => xml
105       end
106       # hope for unauthorized
107       assert_response :unauthorized, "node upload did not return unauthorized status"
108
109       ## Now try with the user which doesn't have their data public
110       auth_header = bearer_authorization_header private_user
111
112       # create a minimal xml file
113       xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{private_changeset.id}'/></osm>"
114       assert_difference("Node.count", 0) do
115         post api_nodes_path, :params => xml, :headers => auth_header
116       end
117       # hope for success
118       assert_require_public_data "node create did not return forbidden status"
119
120       ## Now try with the user that has the public data
121       auth_header = bearer_authorization_header user
122
123       # create a minimal xml file
124       xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
125       post api_nodes_path, :params => xml, :headers => auth_header
126       # hope for success
127       assert_response :success, "node upload did not return success status"
128
129       # read id of created node and search for it
130       nodeid = @response.body
131       checknode = Node.find(nodeid)
132       assert_not_nil checknode, "uploaded node not found in data base after upload"
133       # compare values
134       assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
135       assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
136       assert_equal changeset.id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
137       assert checknode.visible, "saved node is not visible"
138     end
139
140     def test_create_invalid_xml
141       ## Only test public user here, as test_create should cover what's the forbidden
142       ## that would occur here
143
144       user = create(:user)
145       changeset = create(:changeset, :user => user)
146
147       auth_header = bearer_authorization_header user
148       lat = 3.434
149       lon = 3.23
150
151       # test that the upload is rejected when xml is valid, but osm doc isn't
152       xml = "<create/>"
153       post api_nodes_path, :params => xml, :headers => auth_header
154       assert_response :bad_request, "node upload did not return bad_request status"
155       assert_equal "Cannot parse valid node from xml string <create/>. XML doesn't contain an osm/node element.", @response.body
156
157       # test that the upload is rejected when no lat is supplied
158       # create a minimal xml file
159       xml = "<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>"
160       post api_nodes_path, :params => xml, :headers => auth_header
161       # hope for success
162       assert_response :bad_request, "node upload did not return bad_request status"
163       assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
164
165       # test that the upload is rejected when no lon is supplied
166       # create a minimal xml file
167       xml = "<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>"
168       post api_nodes_path, :params => xml, :headers => auth_header
169       # hope for success
170       assert_response :bad_request, "node upload did not return bad_request status"
171       assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
172
173       # test that the upload is rejected when lat is non-numeric
174       # create a minimal xml file
175       xml = "<osm><node lat='abc' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
176       post api_nodes_path, :params => xml, :headers => auth_header
177       # hope for success
178       assert_response :bad_request, "node upload did not return bad_request status"
179       assert_equal "Cannot parse valid node from xml string <node lat=\"abc\" lon=\"#{lon}\" changeset=\"#{changeset.id}\"/>. lat not a number", @response.body
180
181       # test that the upload is rejected when lon is non-numeric
182       # create a minimal xml file
183       xml = "<osm><node lat='#{lat}' lon='abc' changeset='#{changeset.id}'/></osm>"
184       post api_nodes_path, :params => xml, :headers => auth_header
185       # hope for success
186       assert_response :bad_request, "node upload did not return bad_request status"
187       assert_equal "Cannot parse valid node from xml string <node lat=\"#{lat}\" lon=\"abc\" changeset=\"#{changeset.id}\"/>. lon not a number", @response.body
188
189       # test that the upload is rejected when we have a tag which is too long
190       xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x' * 256}'/></node></osm>"
191       post api_nodes_path, :params => xml, :headers => auth_header
192       assert_response :bad_request, "node upload did not return bad_request status"
193       assert_match(/ v: is too long \(maximum is 255 characters\) /, @response.body)
194     end
195
196     def test_show
197       # check that a visible node is returned properly
198       get api_node_path(create(:node))
199       assert_response :success
200
201       # check that an deleted node is not returned
202       get api_node_path(create(:node, :deleted))
203       assert_response :gone
204
205       # check chat a non-existent node is not returned
206       get api_node_path(0)
207       assert_response :not_found
208     end
209
210     # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
211     def test_lat_lon_xml_format
212       node = create(:node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
213
214       get api_node_path(node)
215       assert_match(/lat="0.0000400"/, response.body)
216       assert_match(/lon="0.0000800"/, response.body)
217     end
218
219     # this tests deletion restrictions - basic deletion is tested in the unit
220     # tests for node!
221     def test_destroy
222       private_user = create(:user, :data_public => false)
223       private_user_changeset = create(:changeset, :user => private_user)
224       private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
225       private_node = create(:node, :changeset => private_user_changeset)
226       private_deleted_node = create(:node, :deleted, :changeset => private_user_changeset)
227
228       ## first try to delete node without auth
229       delete api_node_path(private_node)
230       assert_response :unauthorized
231
232       ## now set auth for the non-data public user
233       auth_header = bearer_authorization_header private_user
234
235       # try to delete with an invalid (closed) changeset
236       xml = update_changeset(xml_for_node(private_node), private_user_closed_changeset.id)
237       delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
238       assert_require_public_data("non-public user shouldn't be able to delete node")
239
240       # try to delete with an invalid (non-existent) changeset
241       xml = update_changeset(xml_for_node(private_node), 0)
242       delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
243       assert_require_public_data("shouldn't be able to delete node, when user's data is private")
244
245       # valid delete now takes a payload
246       xml = xml_for_node(private_node)
247       delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
248       assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
249
250       # this won't work since the node is already deleted
251       xml = xml_for_node(private_deleted_node)
252       delete api_node_path(private_deleted_node), :params => xml.to_s, :headers => auth_header
253       assert_require_public_data
254
255       # this won't work since the node never existed
256       delete api_node_path(0), :headers => auth_header
257       assert_require_public_data
258
259       ## these test whether nodes which are in-use can be deleted:
260       # in a way...
261       private_used_node = create(:node, :changeset => private_user_changeset)
262       create(:way_node, :node => private_used_node)
263
264       xml = xml_for_node(private_used_node)
265       delete api_node_path(private_used_node), :params => xml.to_s, :headers => auth_header
266       assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
267
268       # in a relation...
269       private_used_node2 = create(:node, :changeset => private_user_changeset)
270       create(:relation_member, :member => private_used_node2)
271
272       xml = xml_for_node(private_used_node2)
273       delete api_node_path(private_used_node2), :params => xml.to_s, :headers => auth_header
274       assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
275
276       ## now setup for the public data user
277       user = create(:user, :data_public => true)
278       changeset = create(:changeset, :user => user)
279       closed_changeset = create(:changeset, :closed, :user => user)
280       node = create(:node, :changeset => changeset)
281       auth_header = bearer_authorization_header user
282
283       # try to delete with an invalid (closed) changeset
284       xml = update_changeset(xml_for_node(node), closed_changeset.id)
285       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
286       assert_response :conflict
287
288       # try to delete with an invalid (non-existent) changeset
289       xml = update_changeset(xml_for_node(node), 0)
290       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
291       assert_response :conflict
292
293       # try to delete a node with a different ID
294       other_node = create(:node)
295       xml = xml_for_node(other_node)
296       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
297       assert_response :bad_request,
298                       "should not be able to delete a node with a different ID from the XML"
299
300       # try to delete a node rubbish in the payloads
301       xml = "<delete/>"
302       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
303       assert_response :bad_request,
304                       "should not be able to delete a node without a valid XML payload"
305
306       # valid delete now takes a payload
307       xml = xml_for_node(node)
308       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
309       assert_response :success
310
311       # valid delete should return the new version number, which should
312       # be greater than the old version number
313       assert_operator @response.body.to_i, :>, node.version, "delete request should return a new version number for node"
314
315       # deleting the same node twice doesn't work
316       xml = xml_for_node(node)
317       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
318       assert_response :gone
319
320       # this won't work since the node never existed
321       delete api_node_path(0), :headers => auth_header
322       assert_response :not_found
323
324       ## these test whether nodes which are in-use can be deleted:
325       # in a way...
326       used_node = create(:node, :changeset => create(:changeset, :user => user))
327       way_node = create(:way_node, :node => used_node)
328       way_node2 = create(:way_node, :node => used_node)
329
330       xml = xml_for_node(used_node)
331       delete api_node_path(used_node), :params => xml.to_s, :headers => auth_header
332       assert_response :precondition_failed,
333                       "shouldn't be able to delete a node used in a way (#{@response.body})"
334       assert_equal "Precondition failed: Node #{used_node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
335
336       # in a relation...
337       used_node2 = create(:node, :changeset => create(:changeset, :user => user))
338       relation_member = create(:relation_member, :member => used_node2)
339       relation_member2 = create(:relation_member, :member => used_node2)
340
341       xml = xml_for_node(used_node2)
342       delete api_node_path(used_node2), :params => xml.to_s, :headers => auth_header
343       assert_response :precondition_failed,
344                       "shouldn't be able to delete a node used in a relation (#{@response.body})"
345       assert_equal "Precondition failed: Node #{used_node2.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
346     end
347
348     ##
349     # tests whether the API works and prevents incorrect use while trying
350     # to update nodes.
351     def test_update
352       invalid_attr_values = [["lat", 91.0], ["lat", -91.0], ["lon", 181.0], ["lon", -181.0]]
353
354       ## First test with no user credentials
355       # try and update a node without authorisation
356       # first try to delete node without auth
357       private_user = create(:user, :data_public => false)
358       private_node = create(:node, :changeset => create(:changeset, :user => private_user))
359       user = create(:user)
360       node = create(:node, :changeset => create(:changeset, :user => user))
361
362       xml = xml_for_node(node)
363       put api_node_path(node), :params => xml.to_s
364       assert_response :unauthorized
365
366       ## Second test with the private user
367
368       # setup auth
369       auth_header = bearer_authorization_header private_user
370
371       ## trying to break changesets
372
373       # try and update in someone else's changeset
374       xml = update_changeset(xml_for_node(private_node),
375                              create(:changeset).id)
376       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
377       assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
378
379       # try and update in a closed changeset
380       xml = update_changeset(xml_for_node(private_node),
381                              create(:changeset, :closed, :user => private_user).id)
382       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
383       assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
384
385       # try and update in a non-existant changeset
386       xml = update_changeset(xml_for_node(private_node), 0)
387       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
388       assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
389
390       ## try and submit invalid updates
391       invalid_attr_values.each do |name, value|
392         xml = xml_attr_rewrite(xml_for_node(private_node), name, value)
393         put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
394         assert_require_public_data "node at #{name}=#{value} should be forbidden, when data isn't public"
395       end
396
397       ## finally, produce a good request which still won't work
398       xml = xml_for_node(private_node)
399       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
400       assert_require_public_data "should have failed with a forbidden when data isn't public"
401
402       ## Finally test with the public user
403
404       # try and update a node without authorisation
405       # first try to update node without auth
406       xml = xml_for_node(node)
407       put api_node_path(node), :params => xml.to_s, :headers => auth_header
408       assert_response :forbidden
409
410       # setup auth
411       auth_header = bearer_authorization_header user
412
413       ## trying to break changesets
414
415       # try and update in someone else's changeset
416       xml = update_changeset(xml_for_node(node),
417                              create(:changeset).id)
418       put api_node_path(node), :params => xml.to_s, :headers => auth_header
419       assert_response :conflict, "update with other user's changeset should be rejected"
420
421       # try and update in a closed changeset
422       xml = update_changeset(xml_for_node(node),
423                              create(:changeset, :closed, :user => user).id)
424       put api_node_path(node), :params => xml.to_s, :headers => auth_header
425       assert_response :conflict, "update with closed changeset should be rejected"
426
427       # try and update in a non-existant changeset
428       xml = update_changeset(xml_for_node(node), 0)
429       put api_node_path(node), :params => xml.to_s, :headers => auth_header
430       assert_response :conflict, "update with changeset=0 should be rejected"
431
432       ## try and submit invalid updates
433       invalid_attr_values.each do |name, value|
434         xml = xml_attr_rewrite(xml_for_node(node), name, value)
435         put api_node_path(node), :params => xml.to_s, :headers => auth_header
436         assert_response :bad_request, "node at #{name}=#{value} should be rejected"
437       end
438
439       ## next, attack the versioning
440       current_node_version = node.version
441
442       # try and submit a version behind
443       xml = xml_attr_rewrite(xml_for_node(node),
444                              "version", current_node_version - 1)
445       put api_node_path(node), :params => xml.to_s, :headers => auth_header
446       assert_response :conflict, "should have failed on old version number"
447
448       # try and submit a version ahead
449       xml = xml_attr_rewrite(xml_for_node(node),
450                              "version", current_node_version + 1)
451       put api_node_path(node), :params => xml.to_s, :headers => auth_header
452       assert_response :conflict, "should have failed on skipped version number"
453
454       # try and submit total crap in the version field
455       xml = xml_attr_rewrite(xml_for_node(node),
456                              "version", "p1r4t3s!")
457       put api_node_path(node), :params => xml.to_s, :headers => auth_header
458       assert_response :conflict,
459                       "should not be able to put 'p1r4at3s!' in the version field"
460
461       ## try an update with the wrong ID
462       xml = xml_for_node(create(:node))
463       put api_node_path(node), :params => xml.to_s, :headers => auth_header
464       assert_response :bad_request,
465                       "should not be able to update a node with a different ID from the XML"
466
467       ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
468       xml = "<update/>"
469       put api_node_path(node), :params => xml.to_s, :headers => auth_header
470       assert_response :bad_request,
471                       "should not be able to update a node with non-OSM XML doc."
472
473       ## finally, produce a good request which should work
474       xml = xml_for_node(node)
475       put api_node_path(node), :params => xml.to_s, :headers => auth_header
476       assert_response :success, "a valid update request failed"
477     end
478
479     ##
480     # test adding tags to a node
481     def test_duplicate_tags
482       existing_tag = create(:node_tag)
483       assert existing_tag.node.changeset.user.data_public
484       # setup auth
485       auth_header = bearer_authorization_header existing_tag.node.changeset.user
486
487       # add an identical tag to the node
488       tag_xml = XML::Node.new("tag")
489       tag_xml["k"] = existing_tag.k
490       tag_xml["v"] = existing_tag.v
491
492       # add the tag into the existing xml
493       node_xml = xml_for_node(existing_tag.node)
494       node_xml.find("//osm/node").first << tag_xml
495
496       # try and upload it
497       put api_node_path(existing_tag.node), :params => node_xml.to_s, :headers => auth_header
498       assert_response :bad_request,
499                       "adding duplicate tags to a node should fail with 'bad request'"
500       assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
501     end
502
503     # test whether string injection is possible
504     def test_string_injection
505       private_user = create(:user, :data_public => false)
506       private_changeset = create(:changeset, :user => private_user)
507       user = create(:user)
508       changeset = create(:changeset, :user => user)
509
510       ## First try with the non-data public user
511       auth_header = bearer_authorization_header private_user
512
513       # try and put something into a string that the API might
514       # use unquoted and therefore allow code injection...
515       xml = "<osm><node lat='0' lon='0' changeset='#{private_changeset.id}'>" \
516             "<tag k='\#{@user.inspect}' v='0'/>" \
517             "</node></osm>"
518       post api_nodes_path, :params => xml, :headers => auth_header
519       assert_require_public_data "Shouldn't be able to create with non-public user"
520
521       ## Then try with the public data user
522       auth_header = bearer_authorization_header user
523
524       # try and put something into a string that the API might
525       # use unquoted and therefore allow code injection...
526       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'>" \
527             "<tag k='\#{@user.inspect}' v='0'/>" \
528             "</node></osm>"
529       post api_nodes_path, :params => xml, :headers => auth_header
530       assert_response :success
531       nodeid = @response.body
532
533       # find the node in the database
534       checknode = Node.find(nodeid)
535       assert_not_nil checknode, "node not found in data base after upload"
536
537       # and grab it using the api
538       get api_node_path(nodeid)
539       assert_response :success
540       apinode = Node.from_xml(@response.body)
541       assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
542
543       # check the tags are not corrupted
544       assert_equal checknode.tags, apinode.tags
545       assert_includes apinode.tags, "\#{@user.inspect}"
546     end
547
548     ##
549     # test initial rate limit
550     def test_initial_rate_limit
551       # create a user
552       user = create(:user)
553
554       # create a changeset that puts us near the initial rate limit
555       changeset = create(:changeset, :user => user,
556                                      :created_at => Time.now.utc - 5.minutes,
557                                      :num_changes => Settings.initial_changes_per_hour - 1)
558
559       # create authentication header
560       auth_header = bearer_authorization_header user
561
562       # try creating a node
563       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
564       post api_nodes_path, :params => xml, :headers => auth_header
565       assert_response :success, "node create did not return success status"
566
567       # get the id of the node we created
568       nodeid = @response.body
569
570       # try updating the node, which should be rate limited
571       xml = "<osm><node id='#{nodeid}' version='1' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
572       put api_node_path(nodeid), :params => xml, :headers => auth_header
573       assert_response :too_many_requests, "node update did not hit rate limit"
574
575       # try deleting the node, which should be rate limited
576       xml = "<osm><node id='#{nodeid}' version='2' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
577       delete api_node_path(nodeid), :params => xml, :headers => auth_header
578       assert_response :too_many_requests, "node delete did not hit rate limit"
579
580       # try creating a node, which should be rate limited
581       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
582       post api_nodes_path, :params => xml, :headers => auth_header
583       assert_response :too_many_requests, "node create did not hit rate limit"
584     end
585
586     ##
587     # test maximum rate limit
588     def test_maximum_rate_limit
589       # create a user
590       user = create(:user)
591
592       # create a changeset to establish our initial edit time
593       changeset = create(:changeset, :user => user,
594                                      :created_at => Time.now.utc - 28.days)
595
596       # create changeset to put us near the maximum rate limit
597       total_changes = Settings.max_changes_per_hour - 1
598       while total_changes.positive?
599         changes = [total_changes, Changeset::MAX_ELEMENTS].min
600         changeset = create(:changeset, :user => user,
601                                        :created_at => Time.now.utc - 5.minutes,
602                                        :num_changes => changes)
603         total_changes -= changes
604       end
605
606       # create authentication header
607       auth_header = bearer_authorization_header user
608
609       # try creating a node
610       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
611       post api_nodes_path, :params => xml, :headers => auth_header
612       assert_response :success, "node create did not return success status"
613
614       # get the id of the node we created
615       nodeid = @response.body
616
617       # try updating the node, which should be rate limited
618       xml = "<osm><node id='#{nodeid}' version='1' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
619       put api_node_path(nodeid), :params => xml, :headers => auth_header
620       assert_response :too_many_requests, "node update did not hit rate limit"
621
622       # try deleting the node, which should be rate limited
623       xml = "<osm><node id='#{nodeid}' version='2' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
624       delete api_node_path(nodeid), :params => xml, :headers => auth_header
625       assert_response :too_many_requests, "node delete did not hit rate limit"
626
627       # try creating a node, which should be rate limited
628       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
629       post api_nodes_path, :params => xml, :headers => auth_header
630       assert_response :too_many_requests, "node create did not hit rate limit"
631     end
632
633     private
634
635     ##
636     # update the changeset_id of a node element
637     def update_changeset(xml, changeset_id)
638       xml_attr_rewrite(xml, "changeset", changeset_id)
639     end
640
641     ##
642     # update an attribute in the node element
643     def xml_attr_rewrite(xml, name, value)
644       xml.find("//osm/node").first[name] = value.to_s
645       xml
646     end
647   end
648 end