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