]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/nodes_controller_test.rb
Test if api relation show/full responses contain required elements
[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_not_found
197       get api_node_path(0)
198       assert_response :not_found
199     end
200
201     def test_show_deleted
202       get api_node_path(create(:node, :deleted))
203       assert_response :gone
204     end
205
206     def test_show
207       get api_node_path(create(:node))
208       assert_response :success
209     end
210
211     # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
212     def test_lat_lon_xml_format
213       node = create(:node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
214
215       get api_node_path(node)
216       assert_match(/lat="0.0000400"/, response.body)
217       assert_match(/lon="0.0000800"/, response.body)
218     end
219
220     # this tests deletion restrictions - basic deletion is tested in the unit
221     # tests for node!
222     def test_destroy
223       private_user = create(:user, :data_public => false)
224       private_user_changeset = create(:changeset, :user => private_user)
225       private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
226       private_node = create(:node, :changeset => private_user_changeset)
227       private_deleted_node = create(:node, :deleted, :changeset => private_user_changeset)
228
229       ## first try to delete node without auth
230       delete api_node_path(private_node)
231       assert_response :unauthorized
232
233       ## now set auth for the non-data public user
234       auth_header = bearer_authorization_header private_user
235
236       # try to delete with an invalid (closed) changeset
237       xml = update_changeset(xml_for_node(private_node), private_user_closed_changeset.id)
238       delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
239       assert_require_public_data("non-public user shouldn't be able to delete node")
240
241       # try to delete with an invalid (non-existent) changeset
242       xml = update_changeset(xml_for_node(private_node), 0)
243       delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
244       assert_require_public_data("shouldn't be able to delete node, when user's data is private")
245
246       # valid delete now takes a payload
247       xml = xml_for_node(private_node)
248       delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
249       assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
250
251       # this won't work since the node is already deleted
252       xml = xml_for_node(private_deleted_node)
253       delete api_node_path(private_deleted_node), :params => xml.to_s, :headers => auth_header
254       assert_require_public_data
255
256       # this won't work since the node never existed
257       delete api_node_path(0), :headers => auth_header
258       assert_require_public_data
259
260       ## these test whether nodes which are in-use can be deleted:
261       # in a way...
262       private_used_node = create(:node, :changeset => private_user_changeset)
263       create(:way_node, :node => private_used_node)
264
265       xml = xml_for_node(private_used_node)
266       delete api_node_path(private_used_node), :params => xml.to_s, :headers => auth_header
267       assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
268
269       # in a relation...
270       private_used_node2 = create(:node, :changeset => private_user_changeset)
271       create(:relation_member, :member => private_used_node2)
272
273       xml = xml_for_node(private_used_node2)
274       delete api_node_path(private_used_node2), :params => xml.to_s, :headers => auth_header
275       assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
276
277       ## now setup for the public data user
278       user = create(:user, :data_public => true)
279       changeset = create(:changeset, :user => user)
280       closed_changeset = create(:changeset, :closed, :user => user)
281       node = create(:node, :changeset => changeset)
282       auth_header = bearer_authorization_header user
283
284       # try to delete with an invalid (closed) changeset
285       xml = update_changeset(xml_for_node(node), closed_changeset.id)
286       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
287       assert_response :conflict
288
289       # try to delete with an invalid (non-existent) changeset
290       xml = update_changeset(xml_for_node(node), 0)
291       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
292       assert_response :conflict
293
294       # try to delete a node with a different ID
295       other_node = create(:node)
296       xml = xml_for_node(other_node)
297       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
298       assert_response :bad_request,
299                       "should not be able to delete a node with a different ID from the XML"
300
301       # try to delete a node rubbish in the payloads
302       xml = "<delete/>"
303       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
304       assert_response :bad_request,
305                       "should not be able to delete a node without a valid XML payload"
306
307       # valid delete now takes a payload
308       xml = xml_for_node(node)
309       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
310       assert_response :success
311
312       # valid delete should return the new version number, which should
313       # be greater than the old version number
314       assert_operator @response.body.to_i, :>, node.version, "delete request should return a new version number for node"
315
316       # deleting the same node twice doesn't work
317       xml = xml_for_node(node)
318       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
319       assert_response :gone
320
321       # this won't work since the node never existed
322       delete api_node_path(0), :headers => auth_header
323       assert_response :not_found
324
325       ## these test whether nodes which are in-use can be deleted:
326       # in a way...
327       used_node = create(:node, :changeset => create(:changeset, :user => user))
328       way_node = create(:way_node, :node => used_node)
329       way_node2 = create(:way_node, :node => used_node)
330
331       xml = xml_for_node(used_node)
332       delete api_node_path(used_node), :params => xml.to_s, :headers => auth_header
333       assert_response :precondition_failed,
334                       "shouldn't be able to delete a node used in a way (#{@response.body})"
335       assert_equal "Precondition failed: Node #{used_node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
336
337       # in a relation...
338       used_node2 = create(:node, :changeset => create(:changeset, :user => user))
339       relation_member = create(:relation_member, :member => used_node2)
340       relation_member2 = create(:relation_member, :member => used_node2)
341
342       xml = xml_for_node(used_node2)
343       delete api_node_path(used_node2), :params => xml.to_s, :headers => auth_header
344       assert_response :precondition_failed,
345                       "shouldn't be able to delete a node used in a relation (#{@response.body})"
346       assert_equal "Precondition failed: Node #{used_node2.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
347     end
348
349     ##
350     # tests whether the API works and prevents incorrect use while trying
351     # to update nodes.
352     def test_update
353       invalid_attr_values = [["lat", 91.0], ["lat", -91.0], ["lon", 181.0], ["lon", -181.0]]
354
355       ## First test with no user credentials
356       # try and update a node without authorisation
357       # first try to delete node without auth
358       private_user = create(:user, :data_public => false)
359       private_node = create(:node, :changeset => create(:changeset, :user => private_user))
360       user = create(:user)
361       node = create(:node, :changeset => create(:changeset, :user => user))
362
363       xml = xml_for_node(node)
364       put api_node_path(node), :params => xml.to_s
365       assert_response :unauthorized
366
367       ## Second test with the private user
368
369       # setup auth
370       auth_header = bearer_authorization_header private_user
371
372       ## trying to break changesets
373
374       # try and update in someone else's changeset
375       xml = update_changeset(xml_for_node(private_node),
376                              create(:changeset).id)
377       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
378       assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
379
380       # try and update in a closed changeset
381       xml = update_changeset(xml_for_node(private_node),
382                              create(:changeset, :closed, :user => private_user).id)
383       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
384       assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
385
386       # try and update in a non-existant changeset
387       xml = update_changeset(xml_for_node(private_node), 0)
388       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
389       assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
390
391       ## try and submit invalid updates
392       invalid_attr_values.each do |name, value|
393         xml = xml_attr_rewrite(xml_for_node(private_node), name, value)
394         put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
395         assert_require_public_data "node at #{name}=#{value} should be forbidden, when data isn't public"
396       end
397
398       ## finally, produce a good request which still won't work
399       xml = xml_for_node(private_node)
400       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
401       assert_require_public_data "should have failed with a forbidden when data isn't public"
402
403       ## Finally test with the public user
404
405       # try and update a node without authorisation
406       # first try to update node without auth
407       xml = xml_for_node(node)
408       put api_node_path(node), :params => xml.to_s, :headers => auth_header
409       assert_response :forbidden
410
411       # setup auth
412       auth_header = bearer_authorization_header user
413
414       ## trying to break changesets
415
416       # try and update in someone else's changeset
417       xml = update_changeset(xml_for_node(node),
418                              create(:changeset).id)
419       put api_node_path(node), :params => xml.to_s, :headers => auth_header
420       assert_response :conflict, "update with other user's changeset should be rejected"
421
422       # try and update in a closed changeset
423       xml = update_changeset(xml_for_node(node),
424                              create(:changeset, :closed, :user => user).id)
425       put api_node_path(node), :params => xml.to_s, :headers => auth_header
426       assert_response :conflict, "update with closed changeset should be rejected"
427
428       # try and update in a non-existant changeset
429       xml = update_changeset(xml_for_node(node), 0)
430       put api_node_path(node), :params => xml.to_s, :headers => auth_header
431       assert_response :conflict, "update with changeset=0 should be rejected"
432
433       ## try and submit invalid updates
434       invalid_attr_values.each do |name, value|
435         xml = xml_attr_rewrite(xml_for_node(node), name, value)
436         put api_node_path(node), :params => xml.to_s, :headers => auth_header
437         assert_response :bad_request, "node at #{name}=#{value} should be rejected"
438       end
439
440       ## next, attack the versioning
441       current_node_version = node.version
442
443       # try and submit a version behind
444       xml = xml_attr_rewrite(xml_for_node(node),
445                              "version", current_node_version - 1)
446       put api_node_path(node), :params => xml.to_s, :headers => auth_header
447       assert_response :conflict, "should have failed on old version number"
448
449       # try and submit a version ahead
450       xml = xml_attr_rewrite(xml_for_node(node),
451                              "version", current_node_version + 1)
452       put api_node_path(node), :params => xml.to_s, :headers => auth_header
453       assert_response :conflict, "should have failed on skipped version number"
454
455       # try and submit total crap in the version field
456       xml = xml_attr_rewrite(xml_for_node(node),
457                              "version", "p1r4t3s!")
458       put api_node_path(node), :params => xml.to_s, :headers => auth_header
459       assert_response :conflict,
460                       "should not be able to put 'p1r4at3s!' in the version field"
461
462       ## try an update with the wrong ID
463       xml = xml_for_node(create(:node))
464       put api_node_path(node), :params => xml.to_s, :headers => auth_header
465       assert_response :bad_request,
466                       "should not be able to update a node with a different ID from the XML"
467
468       ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
469       xml = "<update/>"
470       put api_node_path(node), :params => xml.to_s, :headers => auth_header
471       assert_response :bad_request,
472                       "should not be able to update a node with non-OSM XML doc."
473
474       ## finally, produce a good request which should work
475       xml = xml_for_node(node)
476       put api_node_path(node), :params => xml.to_s, :headers => auth_header
477       assert_response :success, "a valid update request failed"
478     end
479
480     ##
481     # test adding tags to a node
482     def test_duplicate_tags
483       existing_tag = create(:node_tag)
484       assert existing_tag.node.changeset.user.data_public
485       # setup auth
486       auth_header = bearer_authorization_header existing_tag.node.changeset.user
487
488       # add an identical tag to the node
489       tag_xml = XML::Node.new("tag")
490       tag_xml["k"] = existing_tag.k
491       tag_xml["v"] = existing_tag.v
492
493       # add the tag into the existing xml
494       node_xml = xml_for_node(existing_tag.node)
495       node_xml.find("//osm/node").first << tag_xml
496
497       # try and upload it
498       put api_node_path(existing_tag.node), :params => node_xml.to_s, :headers => auth_header
499       assert_response :bad_request,
500                       "adding duplicate tags to a node should fail with 'bad request'"
501       assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
502     end
503
504     # test whether string injection is possible
505     def test_string_injection
506       private_user = create(:user, :data_public => false)
507       private_changeset = create(:changeset, :user => private_user)
508       user = create(:user)
509       changeset = create(:changeset, :user => user)
510
511       ## First try with the non-data public user
512       auth_header = bearer_authorization_header private_user
513
514       # try and put something into a string that the API might
515       # use unquoted and therefore allow code injection...
516       xml = "<osm><node lat='0' lon='0' changeset='#{private_changeset.id}'>" \
517             "<tag k='\#{@user.inspect}' v='0'/>" \
518             "</node></osm>"
519       post api_nodes_path, :params => xml, :headers => auth_header
520       assert_require_public_data "Shouldn't be able to create with non-public user"
521
522       ## Then try with the public data user
523       auth_header = bearer_authorization_header user
524
525       # try and put something into a string that the API might
526       # use unquoted and therefore allow code injection...
527       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'>" \
528             "<tag k='\#{@user.inspect}' v='0'/>" \
529             "</node></osm>"
530       post api_nodes_path, :params => xml, :headers => auth_header
531       assert_response :success
532       nodeid = @response.body
533
534       # find the node in the database
535       checknode = Node.find(nodeid)
536       assert_not_nil checknode, "node not found in data base after upload"
537
538       # and grab it using the api
539       get api_node_path(nodeid)
540       assert_response :success
541       apinode = Node.from_xml(@response.body)
542       assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
543
544       # check the tags are not corrupted
545       assert_equal checknode.tags, apinode.tags
546       assert_includes apinode.tags, "\#{@user.inspect}"
547     end
548
549     ##
550     # test initial rate limit
551     def test_initial_rate_limit
552       # create a user
553       user = create(:user)
554
555       # create a changeset that puts us near the initial rate limit
556       changeset = create(:changeset, :user => user,
557                                      :created_at => Time.now.utc - 5.minutes,
558                                      :num_changes => Settings.initial_changes_per_hour - 1)
559
560       # create authentication header
561       auth_header = bearer_authorization_header user
562
563       # try creating a node
564       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
565       post api_nodes_path, :params => xml, :headers => auth_header
566       assert_response :success, "node create did not return success status"
567
568       # get the id of the node we created
569       nodeid = @response.body
570
571       # try updating the node, which should be rate limited
572       xml = "<osm><node id='#{nodeid}' version='1' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
573       put api_node_path(nodeid), :params => xml, :headers => auth_header
574       assert_response :too_many_requests, "node update did not hit rate limit"
575
576       # try deleting the node, which should be rate limited
577       xml = "<osm><node id='#{nodeid}' version='2' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
578       delete api_node_path(nodeid), :params => xml, :headers => auth_header
579       assert_response :too_many_requests, "node delete did not hit rate limit"
580
581       # try creating a node, which should be rate limited
582       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
583       post api_nodes_path, :params => xml, :headers => auth_header
584       assert_response :too_many_requests, "node create did not hit rate limit"
585     end
586
587     ##
588     # test maximum rate limit
589     def test_maximum_rate_limit
590       # create a user
591       user = create(:user)
592
593       # create a changeset to establish our initial edit time
594       changeset = create(:changeset, :user => user,
595                                      :created_at => Time.now.utc - 28.days)
596
597       # create changeset to put us near the maximum rate limit
598       total_changes = Settings.max_changes_per_hour - 1
599       while total_changes.positive?
600         changes = [total_changes, Changeset::MAX_ELEMENTS].min
601         changeset = create(:changeset, :user => user,
602                                        :created_at => Time.now.utc - 5.minutes,
603                                        :num_changes => changes)
604         total_changes -= changes
605       end
606
607       # create authentication header
608       auth_header = bearer_authorization_header user
609
610       # try creating a node
611       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
612       post api_nodes_path, :params => xml, :headers => auth_header
613       assert_response :success, "node create did not return success status"
614
615       # get the id of the node we created
616       nodeid = @response.body
617
618       # try updating the node, which should be rate limited
619       xml = "<osm><node id='#{nodeid}' version='1' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
620       put api_node_path(nodeid), :params => xml, :headers => auth_header
621       assert_response :too_many_requests, "node update did not hit rate limit"
622
623       # try deleting the node, which should be rate limited
624       xml = "<osm><node id='#{nodeid}' version='2' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
625       delete api_node_path(nodeid), :params => xml, :headers => auth_header
626       assert_response :too_many_requests, "node delete did not hit rate limit"
627
628       # try creating a node, which should be rate limited
629       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
630       post api_nodes_path, :params => xml, :headers => auth_header
631       assert_response :too_many_requests, "node create did not hit rate limit"
632     end
633
634     private
635
636     ##
637     # update the changeset_id of a node element
638     def update_changeset(xml, changeset_id)
639       xml_attr_rewrite(xml, "changeset", changeset_id)
640     end
641
642     ##
643     # update an attribute in the node element
644     def xml_attr_rewrite(xml, name, value)
645       xml.find("//osm/node").first[name] = value.to_s
646       xml
647     end
648   end
649 end