]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/relations_controller_test.rb
Move tests comparing relation tags with response to integration tests
[rails.git] / test / controllers / api / relations_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class RelationsControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/relations", :method => :get },
10         { :controller => "api/relations", :action => "index" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/relations.json", :method => :get },
14         { :controller => "api/relations", :action => "index", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/relations", :method => :post },
18         { :controller => "api/relations", :action => "create" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/relation/1", :method => :get },
22         { :controller => "api/relations", :action => "show", :id => "1" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/relation/1.json", :method => :get },
26         { :controller => "api/relations", :action => "show", :id => "1", :format => "json" }
27       )
28       assert_routing(
29         { :path => "/api/0.6/relation/1/full", :method => :get },
30         { :controller => "api/relations", :action => "show", :full => true, :id => "1" }
31       )
32       assert_routing(
33         { :path => "/api/0.6/relation/1/full.json", :method => :get },
34         { :controller => "api/relations", :action => "show", :full => true, :id => "1", :format => "json" }
35       )
36       assert_routing(
37         { :path => "/api/0.6/relation/1", :method => :put },
38         { :controller => "api/relations", :action => "update", :id => "1" }
39       )
40       assert_routing(
41         { :path => "/api/0.6/relation/1", :method => :delete },
42         { :controller => "api/relations", :action => "destroy", :id => "1" }
43       )
44
45       assert_recognizes(
46         { :controller => "api/relations", :action => "create" },
47         { :path => "/api/0.6/relation/create", :method => :put }
48       )
49     end
50
51     ##
52     # test fetching multiple relations
53     def test_index
54       relation1 = create(:relation)
55       relation2 = create(:relation, :deleted)
56       relation3 = create(:relation, :with_history, :version => 2)
57       relation4 = create(:relation, :with_history, :version => 2)
58       relation4.old_relations.find_by(:version => 1).redact!(create(:redaction))
59
60       # check error when no parameter provided
61       get api_relations_path
62       assert_response :bad_request
63
64       # check error when no parameter value provided
65       get api_relations_path(:relations => "")
66       assert_response :bad_request
67
68       # test a working call
69       get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}")
70       assert_response :success
71       assert_select "osm" do
72         assert_select "relation", :count => 4
73         assert_select "relation[id='#{relation1.id}'][visible='true']", :count => 1
74         assert_select "relation[id='#{relation2.id}'][visible='false']", :count => 1
75         assert_select "relation[id='#{relation3.id}'][visible='true']", :count => 1
76         assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1
77       end
78
79       # test a working call with json format
80       get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}", :format => "json")
81
82       js = ActiveSupport::JSON.decode(@response.body)
83       assert_not_nil js
84       assert_equal 4, js["elements"].count
85       assert_equal 4, (js["elements"].count { |a| a["type"] == "relation" })
86       assert_equal 1, (js["elements"].count { |a| a["id"] == relation1.id && a["visible"].nil? })
87       assert_equal 1, (js["elements"].count { |a| a["id"] == relation2.id && a["visible"] == false })
88       assert_equal 1, (js["elements"].count { |a| a["id"] == relation3.id && a["visible"].nil? })
89       assert_equal 1, (js["elements"].count { |a| a["id"] == relation4.id && a["visible"].nil? })
90
91       # check error when a non-existent relation is included
92       get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0")
93       assert_response :not_found
94     end
95
96     # -------------------------------------
97     # Test showing relations.
98     # -------------------------------------
99
100     def test_show_not_found
101       get api_relation_path(0)
102       assert_response :not_found
103     end
104
105     def test_show_deleted
106       get api_relation_path(create(:relation, :deleted))
107       assert_response :gone
108     end
109
110     def test_show
111       relation = create(:relation, :timestamp => "2021-02-03T00:00:00Z")
112       node = create(:node, :timestamp => "2021-04-05T00:00:00Z")
113       create(:relation_member, :relation => relation, :member => node)
114
115       get api_relation_path(relation)
116
117       assert_response :success
118       assert_not_nil @response.header["Last-Modified"]
119       assert_equal "2021-02-03T00:00:00Z", Time.parse(@response.header["Last-Modified"]).utc.xmlschema
120       assert_dom "node", :count => 0
121       assert_dom "relation", :count => 1 do
122         assert_dom "> @id", :text => relation.id.to_s
123       end
124     end
125
126     def test_full_not_found
127       get api_relation_path(999999, :full => true)
128       assert_response :not_found
129     end
130
131     def test_full_deleted
132       get api_relation_path(create(:relation, :deleted), :full => true)
133       assert_response :gone
134     end
135
136     def test_full_empty
137       relation = create(:relation)
138
139       get api_relation_path(relation, :full => true)
140
141       assert_response :success
142       assert_dom "relation", :count => 1 do
143         assert_dom "> @id", :text => relation.id.to_s
144       end
145     end
146
147     def test_full_with_node_member
148       relation = create(:relation)
149       node = create(:node)
150       create(:relation_member, :relation => relation, :member => node)
151
152       get api_relation_path(relation, :full => true)
153
154       assert_response :success
155       assert_dom "node", :count => 1 do
156         assert_dom "> @id", :text => node.id.to_s
157       end
158       assert_dom "relation", :count => 1 do
159         assert_dom "> @id", :text => relation.id.to_s
160       end
161     end
162
163     def test_full_with_way_member
164       relation = create(:relation)
165       way = create(:way_with_nodes)
166       create(:relation_member, :relation => relation, :member => way)
167
168       get api_relation_path(relation, :full => true)
169
170       assert_response :success
171       assert_dom "node", :count => 1 do
172         assert_dom "> @id", :text => way.nodes[0].id.to_s
173       end
174       assert_dom "way", :count => 1 do
175         assert_dom "> @id", :text => way.id.to_s
176       end
177       assert_dom "relation", :count => 1 do
178         assert_dom "> @id", :text => relation.id.to_s
179       end
180     end
181
182     def test_full_with_node_member_json
183       relation = create(:relation)
184       node = create(:node)
185       create(:relation_member, :relation => relation, :member => node)
186
187       get api_relation_path(relation, :full => true, :format => "json")
188
189       assert_response :success
190       js = ActiveSupport::JSON.decode(@response.body)
191       assert_not_nil js
192       assert_equal 2, js["elements"].count
193
194       js_relations = js["elements"].filter { |e| e["type"] == "relation" }
195       assert_equal 1, js_relations.count
196       assert_equal relation.id, js_relations[0]["id"]
197       assert_equal 1, js_relations[0]["members"].count
198       assert_equal "node", js_relations[0]["members"][0]["type"]
199       assert_equal node.id, js_relations[0]["members"][0]["ref"]
200
201       js_nodes = js["elements"].filter { |e| e["type"] == "node" }
202       assert_equal 1, js_nodes.count
203       assert_equal node.id, js_nodes[0]["id"]
204     end
205
206     # -------------------------------------
207     # Test simple relation creation.
208     # -------------------------------------
209
210     def test_create
211       private_user = create(:user, :data_public => false)
212       private_changeset = create(:changeset, :user => private_user)
213       user = create(:user)
214       changeset = create(:changeset, :user => user)
215       node = create(:node)
216       way = create(:way_with_nodes, :nodes_count => 2)
217
218       auth_header = bearer_authorization_header private_user
219
220       # create an relation without members
221       xml = "<osm><relation changeset='#{private_changeset.id}'><tag k='test' v='yes' /></relation></osm>"
222       post api_relations_path, :params => xml, :headers => auth_header
223       # hope for forbidden, due to user
224       assert_response :forbidden,
225                       "relation upload should have failed with forbidden"
226
227       ###
228       # create an relation with a node as member
229       # This time try with a role attribute in the relation
230       xml = "<osm><relation changeset='#{private_changeset.id}'>" \
231             "<member  ref='#{node.id}' type='node' role='some'/>" \
232             "<tag k='test' v='yes' /></relation></osm>"
233       post api_relations_path, :params => xml, :headers => auth_header
234       # hope for forbidden due to user
235       assert_response :forbidden,
236                       "relation upload did not return forbidden status"
237
238       ###
239       # create an relation with a node as member, this time test that we don't
240       # need a role attribute to be included
241       xml = "<osm><relation changeset='#{private_changeset.id}'>" \
242             "<member  ref='#{node.id}' type='node'/><tag k='test' v='yes' /></relation></osm>"
243       post api_relations_path, :params => xml, :headers => auth_header
244       # hope for forbidden due to user
245       assert_response :forbidden,
246                       "relation upload did not return forbidden status"
247
248       ###
249       # create an relation with a way and a node as members
250       xml = "<osm><relation changeset='#{private_changeset.id}'>" \
251             "<member type='node' ref='#{node.id}' role='some'/>" \
252             "<member type='way' ref='#{way.id}' role='other'/>" \
253             "<tag k='test' v='yes' /></relation></osm>"
254       post api_relations_path, :params => xml, :headers => auth_header
255       # hope for forbidden, due to user
256       assert_response :forbidden,
257                       "relation upload did not return success status"
258
259       ## Now try with the public user
260       auth_header = bearer_authorization_header user
261
262       # create an relation without members
263       xml = "<osm><relation changeset='#{changeset.id}'><tag k='test' v='yes' /></relation></osm>"
264       post api_relations_path, :params => xml, :headers => auth_header
265       # hope for success
266       assert_response :success,
267                       "relation upload did not return success status"
268       # read id of created relation and search for it
269       relationid = @response.body
270       checkrelation = Relation.find(relationid)
271       assert_not_nil checkrelation,
272                      "uploaded relation not found in data base after upload"
273       # compare values
274       assert_equal(0, checkrelation.members.length, "saved relation contains members but should not")
275       assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
276       assert_equal changeset.id, checkrelation.changeset.id,
277                    "saved relation does not belong in the changeset it was assigned to"
278       assert_equal user.id, checkrelation.changeset.user_id,
279                    "saved relation does not belong to user that created it"
280       assert checkrelation.visible,
281              "saved relation is not visible"
282       # ok the relation is there but can we also retrieve it?
283       get api_relation_path(relationid)
284       assert_response :success
285
286       ###
287       # create an relation with a node as member
288       # This time try with a role attribute in the relation
289       xml = "<osm><relation changeset='#{changeset.id}'>" \
290             "<member  ref='#{node.id}' type='node' role='some'/>" \
291             "<tag k='test' v='yes' /></relation></osm>"
292       post api_relations_path, :params => xml, :headers => auth_header
293       # hope for success
294       assert_response :success,
295                       "relation upload did not return success status"
296       # read id of created relation and search for it
297       relationid = @response.body
298       checkrelation = Relation.find(relationid)
299       assert_not_nil checkrelation,
300                      "uploaded relation not found in data base after upload"
301       # compare values
302       assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
303       assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
304       assert_equal changeset.id, checkrelation.changeset.id,
305                    "saved relation does not belong in the changeset it was assigned to"
306       assert_equal user.id, checkrelation.changeset.user_id,
307                    "saved relation does not belong to user that created it"
308       assert checkrelation.visible,
309              "saved relation is not visible"
310       # ok the relation is there but can we also retrieve it?
311
312       get api_relation_path(relationid)
313       assert_response :success
314
315       ###
316       # create an relation with a node as member, this time test that we don't
317       # need a role attribute to be included
318       xml = "<osm><relation changeset='#{changeset.id}'>" \
319             "<member  ref='#{node.id}' type='node'/><tag k='test' v='yes' /></relation></osm>"
320       post api_relations_path, :params => xml, :headers => auth_header
321       # hope for success
322       assert_response :success,
323                       "relation upload did not return success status"
324       # read id of created relation and search for it
325       relationid = @response.body
326       checkrelation = Relation.find(relationid)
327       assert_not_nil checkrelation,
328                      "uploaded relation not found in data base after upload"
329       # compare values
330       assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
331       assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
332       assert_equal changeset.id, checkrelation.changeset.id,
333                    "saved relation does not belong in the changeset it was assigned to"
334       assert_equal user.id, checkrelation.changeset.user_id,
335                    "saved relation does not belong to user that created it"
336       assert checkrelation.visible,
337              "saved relation is not visible"
338       # ok the relation is there but can we also retrieve it?
339
340       get api_relation_path(relationid)
341       assert_response :success
342
343       ###
344       # create an relation with a way and a node as members
345       xml = "<osm><relation changeset='#{changeset.id}'>" \
346             "<member type='node' ref='#{node.id}' role='some'/>" \
347             "<member type='way' ref='#{way.id}' role='other'/>" \
348             "<tag k='test' v='yes' /></relation></osm>"
349       post api_relations_path, :params => xml, :headers => auth_header
350       # hope for success
351       assert_response :success,
352                       "relation upload did not return success status"
353       # read id of created relation and search for it
354       relationid = @response.body
355       checkrelation = Relation.find(relationid)
356       assert_not_nil checkrelation,
357                      "uploaded relation not found in data base after upload"
358       # compare values
359       assert_equal(2, checkrelation.members.length, "saved relation does not have exactly two members")
360       assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
361       assert_equal changeset.id, checkrelation.changeset.id,
362                    "saved relation does not belong in the changeset it was assigned to"
363       assert_equal user.id, checkrelation.changeset.user_id,
364                    "saved relation does not belong to user that created it"
365       assert checkrelation.visible,
366              "saved relation is not visible"
367       # ok the relation is there but can we also retrieve it?
368       get api_relation_path(relationid)
369       assert_response :success
370     end
371
372     # ------------------------------------
373     # Test updating relations
374     # ------------------------------------
375
376     def test_update_wrong_id
377       user = create(:user)
378       changeset = create(:changeset, :user => user)
379       relation = create(:relation)
380       other_relation = create(:relation)
381
382       auth_header = bearer_authorization_header user
383       get api_relation_path(relation)
384       assert_response :success
385       rel = XML::Parser.string(@response.body).parse
386
387       update_changeset(rel, changeset.id)
388       put api_relation_path(other_relation), :params => rel.to_s, :headers => auth_header
389       assert_response :bad_request
390     end
391
392     # -------------------------------------
393     # Test creating some invalid relations.
394     # -------------------------------------
395
396     def test_create_invalid
397       user = create(:user)
398       changeset = create(:changeset, :user => user)
399
400       auth_header = bearer_authorization_header user
401
402       # create a relation with non-existing node as member
403       xml = "<osm><relation changeset='#{changeset.id}'>" \
404             "<member type='node' ref='0'/><tag k='test' v='yes' />" \
405             "</relation></osm>"
406       post api_relations_path, :params => xml, :headers => auth_header
407       # expect failure
408       assert_response :precondition_failed,
409                       "relation upload with invalid node did not return 'precondition failed'"
410       assert_equal "Precondition failed: Relation with id  cannot be saved due to Node with id 0", @response.body
411     end
412
413     # -------------------------------------
414     # Test creating a relation, with some invalid XML
415     # -------------------------------------
416     def test_create_invalid_xml
417       user = create(:user)
418       changeset = create(:changeset, :user => user)
419       node = create(:node)
420
421       auth_header = bearer_authorization_header user
422
423       # create some xml that should return an error
424       xml = "<osm><relation changeset='#{changeset.id}'>" \
425             "<member type='type' ref='#{node.id}' role=''/>" \
426             "<tag k='tester' v='yep'/></relation></osm>"
427       post api_relations_path, :params => xml, :headers => auth_header
428       # expect failure
429       assert_response :bad_request
430       assert_match(/Cannot parse valid relation from xml string/, @response.body)
431       assert_match(/The type is not allowed only, /, @response.body)
432     end
433
434     # -------------------------------------
435     # Test deleting relations.
436     # -------------------------------------
437
438     def test_destroy
439       private_user = create(:user, :data_public => false)
440       private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
441       user = create(:user)
442       closed_changeset = create(:changeset, :closed, :user => user)
443       changeset = create(:changeset, :user => user)
444       relation = create(:relation)
445       used_relation = create(:relation)
446       super_relation = create(:relation_member, :member => used_relation).relation
447       deleted_relation = create(:relation, :deleted)
448       multi_tag_relation = create(:relation)
449       create_list(:relation_tag, 4, :relation => multi_tag_relation)
450
451       ## First try to delete relation without auth
452       delete api_relation_path(relation)
453       assert_response :unauthorized
454
455       ## Then try with the private user, to make sure that you get a forbidden
456       auth_header = bearer_authorization_header private_user
457
458       # this shouldn't work, as we should need the payload...
459       delete api_relation_path(relation), :headers => auth_header
460       assert_response :forbidden
461
462       # try to delete without specifying a changeset
463       xml = "<osm><relation id='#{relation.id}'/></osm>"
464       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
465       assert_response :forbidden
466
467       # try to delete with an invalid (closed) changeset
468       xml = update_changeset(xml_for_relation(relation),
469                              private_user_closed_changeset.id)
470       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
471       assert_response :forbidden
472
473       # try to delete with an invalid (non-existent) changeset
474       xml = update_changeset(xml_for_relation(relation), 0)
475       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
476       assert_response :forbidden
477
478       # this won't work because the relation is in-use by another relation
479       xml = xml_for_relation(used_relation)
480       delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
481       assert_response :forbidden
482
483       # this should work when we provide the appropriate payload...
484       xml = xml_for_relation(relation)
485       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
486       assert_response :forbidden
487
488       # this won't work since the relation is already deleted
489       xml = xml_for_relation(deleted_relation)
490       delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
491       assert_response :forbidden
492
493       # this won't work since the relation never existed
494       delete api_relation_path(0), :headers => auth_header
495       assert_response :forbidden
496
497       ## now set auth for the public user
498       auth_header = bearer_authorization_header user
499
500       # this shouldn't work, as we should need the payload...
501       delete api_relation_path(relation), :headers => auth_header
502       assert_response :bad_request
503
504       # try to delete without specifying a changeset
505       xml = "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
506       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
507       assert_response :bad_request
508       assert_match(/Changeset id is missing/, @response.body)
509
510       # try to delete with an invalid (closed) changeset
511       xml = update_changeset(xml_for_relation(relation),
512                              closed_changeset.id)
513       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
514       assert_response :conflict
515
516       # try to delete with an invalid (non-existent) changeset
517       xml = update_changeset(xml_for_relation(relation), 0)
518       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
519       assert_response :conflict
520
521       # this won't work because the relation is in a changeset owned by someone else
522       xml = update_changeset(xml_for_relation(relation), create(:changeset).id)
523       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
524       assert_response :conflict,
525                       "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
526
527       # this won't work because the relation in the payload is different to that passed
528       xml = update_changeset(xml_for_relation(relation), changeset.id)
529       delete api_relation_path(create(:relation)), :params => xml.to_s, :headers => auth_header
530       assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
531
532       # this won't work because the relation is in-use by another relation
533       xml = update_changeset(xml_for_relation(used_relation), changeset.id)
534       delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
535       assert_response :precondition_failed,
536                       "shouldn't be able to delete a relation used in a relation (#{@response.body})"
537       assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
538
539       # this should work when we provide the appropriate payload...
540       xml = update_changeset(xml_for_relation(multi_tag_relation), changeset.id)
541       delete api_relation_path(multi_tag_relation), :params => xml.to_s, :headers => auth_header
542       assert_response :success
543
544       # valid delete should return the new version number, which should
545       # be greater than the old version number
546       assert_operator @response.body.to_i, :>, multi_tag_relation.version, "delete request should return a new version number for relation"
547
548       # this won't work since the relation is already deleted
549       xml = update_changeset(xml_for_relation(deleted_relation), changeset.id)
550       delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
551       assert_response :gone
552
553       # Public visible relation needs to be deleted
554       xml = update_changeset(xml_for_relation(super_relation), changeset.id)
555       delete api_relation_path(super_relation), :params => xml.to_s, :headers => auth_header
556       assert_response :success
557
558       # this works now because the relation which was using this one
559       # has been deleted.
560       xml = update_changeset(xml_for_relation(used_relation), changeset.id)
561       delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
562       assert_response :success,
563                       "should be able to delete a relation used in an old relation (#{@response.body})"
564
565       # this won't work since the relation never existed
566       delete api_relation_path(0), :headers => auth_header
567       assert_response :not_found
568     end
569
570     ##
571     # test initial rate limit
572     def test_initial_rate_limit
573       # create a user
574       user = create(:user)
575
576       # create some nodes
577       node1 = create(:node)
578       node2 = create(:node)
579
580       # create a changeset that puts us near the initial rate limit
581       changeset = create(:changeset, :user => user,
582                                      :created_at => Time.now.utc - 5.minutes,
583                                      :num_changes => Settings.initial_changes_per_hour - 1)
584
585       # create authentication header
586       auth_header = bearer_authorization_header user
587
588       # try creating a relation
589       xml = "<osm><relation changeset='#{changeset.id}'>" \
590             "<member  ref='#{node1.id}' type='node' role='some'/>" \
591             "<member  ref='#{node2.id}' type='node' role='some'/>" \
592             "<tag k='test' v='yes' /></relation></osm>"
593       post api_relations_path, :params => xml, :headers => auth_header
594       assert_response :success, "relation create did not return success status"
595
596       # get the id of the relation we created
597       relationid = @response.body
598
599       # try updating the relation, which should be rate limited
600       xml = "<osm><relation id='#{relationid}' version='1' changeset='#{changeset.id}'>" \
601             "<member  ref='#{node2.id}' type='node' role='some'/>" \
602             "<member  ref='#{node1.id}' type='node' role='some'/>" \
603             "<tag k='test' v='yes' /></relation></osm>"
604       put api_relation_path(relationid), :params => xml, :headers => auth_header
605       assert_response :too_many_requests, "relation update did not hit rate limit"
606
607       # try deleting the relation, which should be rate limited
608       xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
609       delete api_relation_path(relationid), :params => xml, :headers => auth_header
610       assert_response :too_many_requests, "relation delete did not hit rate limit"
611
612       # try creating a relation, which should be rate limited
613       xml = "<osm><relation changeset='#{changeset.id}'>" \
614             "<member  ref='#{node1.id}' type='node' role='some'/>" \
615             "<member  ref='#{node2.id}' type='node' role='some'/>" \
616             "<tag k='test' v='yes' /></relation></osm>"
617       post api_relations_path, :params => xml, :headers => auth_header
618       assert_response :too_many_requests, "relation create did not hit rate limit"
619     end
620
621     ##
622     # test maximum rate limit
623     def test_maximum_rate_limit
624       # create a user
625       user = create(:user)
626
627       # create some nodes
628       node1 = create(:node)
629       node2 = create(:node)
630
631       # create a changeset to establish our initial edit time
632       changeset = create(:changeset, :user => user,
633                                      :created_at => Time.now.utc - 28.days)
634
635       # create changeset to put us near the maximum rate limit
636       total_changes = Settings.max_changes_per_hour - 1
637       while total_changes.positive?
638         changes = [total_changes, Changeset::MAX_ELEMENTS].min
639         changeset = create(:changeset, :user => user,
640                                        :created_at => Time.now.utc - 5.minutes,
641                                        :num_changes => changes)
642         total_changes -= changes
643       end
644
645       # create authentication header
646       auth_header = bearer_authorization_header user
647
648       # try creating a relation
649       xml = "<osm><relation changeset='#{changeset.id}'>" \
650             "<member  ref='#{node1.id}' type='node' role='some'/>" \
651             "<member  ref='#{node2.id}' type='node' role='some'/>" \
652             "<tag k='test' v='yes' /></relation></osm>"
653       post api_relations_path, :params => xml, :headers => auth_header
654       assert_response :success, "relation create did not return success status"
655
656       # get the id of the relation we created
657       relationid = @response.body
658
659       # try updating the relation, which should be rate limited
660       xml = "<osm><relation id='#{relationid}' version='1' changeset='#{changeset.id}'>" \
661             "<member  ref='#{node2.id}' type='node' role='some'/>" \
662             "<member  ref='#{node1.id}' type='node' role='some'/>" \
663             "<tag k='test' v='yes' /></relation></osm>"
664       put api_relation_path(relationid), :params => xml, :headers => auth_header
665       assert_response :too_many_requests, "relation update did not hit rate limit"
666
667       # try deleting the relation, which should be rate limited
668       xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
669       delete api_relation_path(relationid), :params => xml, :headers => auth_header
670       assert_response :too_many_requests, "relation delete did not hit rate limit"
671
672       # try creating a relation, which should be rate limited
673       xml = "<osm><relation changeset='#{changeset.id}'>" \
674             "<member  ref='#{node1.id}' type='node' role='some'/>" \
675             "<member  ref='#{node2.id}' type='node' role='some'/>" \
676             "<tag k='test' v='yes' /></relation></osm>"
677       post api_relations_path, :params => xml, :headers => auth_header
678       assert_response :too_many_requests, "relation create did not hit rate limit"
679     end
680
681     private
682
683     ##
684     # update the changeset_id of a node element
685     def update_changeset(xml, changeset_id)
686       xml_attr_rewrite(xml, "changeset", changeset_id)
687     end
688
689     ##
690     # update an attribute in the node element
691     def xml_attr_rewrite(xml, name, value)
692       xml.find("//osm/relation").first[name] = value.to_s
693       xml
694     end
695   end
696 end