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