]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/relations_controller_test.rb
Replace multiline strings with heredocs api elements test xml
[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
222         <osm>
223           <relation changeset='#{private_changeset.id}'>
224             <tag k='test' v='yes' />
225           </relation>
226         </osm>
227       OSM
228       post api_relations_path, :params => xml, :headers => auth_header
229       # hope for forbidden, due to user
230       assert_response :forbidden,
231                       "relation upload should have failed with forbidden"
232
233       ###
234       # create an relation with a node as member
235       # This time try with a role attribute in the relation
236       xml = <<~OSM
237         <osm>
238           <relation changeset='#{private_changeset.id}'>
239             <member ref='#{node.id}' type='node' role='some'/>
240             <tag k='test' v='yes' />
241           </relation>
242         </osm>
243       OSM
244       post api_relations_path, :params => xml, :headers => auth_header
245       # hope for forbidden due to user
246       assert_response :forbidden,
247                       "relation upload did not return forbidden status"
248
249       ###
250       # create an relation with a node as member, this time test that we don't
251       # need a role attribute to be included
252       xml = <<~OSM
253         <osm>
254           <relation changeset='#{private_changeset.id}'>
255             <member ref='#{node.id}' type='node'/>
256             <tag k='test' v='yes' />
257           </relation>
258         </osm>
259       OSM
260       post api_relations_path, :params => xml, :headers => auth_header
261       # hope for forbidden due to user
262       assert_response :forbidden,
263                       "relation upload did not return forbidden status"
264
265       ###
266       # create an relation with a way and a node as members
267       xml = <<~OSM
268         <osm>
269           <relation changeset='#{private_changeset.id}'>
270             <member type='node' ref='#{node.id}' role='some'/>
271             <member type='way' ref='#{way.id}' role='other'/>
272             <tag k='test' v='yes' />
273           </relation>
274         </osm>
275       OSM
276       post api_relations_path, :params => xml, :headers => auth_header
277       # hope for forbidden, due to user
278       assert_response :forbidden,
279                       "relation upload did not return success status"
280
281       ## Now try with the public user
282       auth_header = bearer_authorization_header user
283
284       # create an relation without members
285       xml = <<~OSM
286         <osm>
287           <relation changeset='#{changeset.id}'>
288             <tag k='test' v='yes' />
289           </relation>
290         </osm>
291       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(0, checkrelation.members.length, "saved relation contains members but should not")
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       get api_relation_path(relationid)
312       assert_response :success
313
314       ###
315       # create an relation with a node as member
316       # This time try with a role attribute in the relation
317       xml = <<~OSM
318         <osm>
319           <relation changeset='#{changeset.id}'>
320             <member ref='#{node.id}' type='node' role='some'/>
321             <tag k='test' v='yes' />
322           </relation>
323         </osm>
324       OSM
325       post api_relations_path, :params => xml, :headers => auth_header
326       # hope for success
327       assert_response :success,
328                       "relation upload did not return success status"
329       # read id of created relation and search for it
330       relationid = @response.body
331       checkrelation = Relation.find(relationid)
332       assert_not_nil checkrelation,
333                      "uploaded relation not found in data base after upload"
334       # compare values
335       assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
336       assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
337       assert_equal changeset.id, checkrelation.changeset.id,
338                    "saved relation does not belong in the changeset it was assigned to"
339       assert_equal user.id, checkrelation.changeset.user_id,
340                    "saved relation does not belong to user that created it"
341       assert checkrelation.visible,
342              "saved relation is not visible"
343       # ok the relation is there but can we also retrieve it?
344
345       get api_relation_path(relationid)
346       assert_response :success
347
348       ###
349       # create an relation with a node as member, this time test that we don't
350       # need a role attribute to be included
351       xml = <<~OSM
352         <osm>
353           <relation changeset='#{changeset.id}'>
354             <member ref='#{node.id}' type='node'/>
355             <tag k='test' v='yes' />
356           </relation>
357         </osm>
358       OSM
359       post api_relations_path, :params => xml, :headers => auth_header
360       # hope for success
361       assert_response :success,
362                       "relation upload did not return success status"
363       # read id of created relation and search for it
364       relationid = @response.body
365       checkrelation = Relation.find(relationid)
366       assert_not_nil checkrelation,
367                      "uploaded relation not found in data base after upload"
368       # compare values
369       assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
370       assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
371       assert_equal changeset.id, checkrelation.changeset.id,
372                    "saved relation does not belong in the changeset it was assigned to"
373       assert_equal user.id, checkrelation.changeset.user_id,
374                    "saved relation does not belong to user that created it"
375       assert checkrelation.visible,
376              "saved relation is not visible"
377       # ok the relation is there but can we also retrieve it?
378
379       get api_relation_path(relationid)
380       assert_response :success
381
382       ###
383       # create an relation with a way and a node as members
384       xml = <<~OSM
385         <osm>
386           <relation changeset='#{changeset.id}'>
387             <member type='node' ref='#{node.id}' role='some'/>
388             <member type='way' ref='#{way.id}' role='other'/>
389             <tag k='test' v='yes' />
390           </relation>
391         </osm>
392       OSM
393       post api_relations_path, :params => xml, :headers => auth_header
394       # hope for success
395       assert_response :success,
396                       "relation upload did not return success status"
397       # read id of created relation and search for it
398       relationid = @response.body
399       checkrelation = Relation.find(relationid)
400       assert_not_nil checkrelation,
401                      "uploaded relation not found in data base after upload"
402       # compare values
403       assert_equal(2, checkrelation.members.length, "saved relation does not have exactly two members")
404       assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
405       assert_equal changeset.id, checkrelation.changeset.id,
406                    "saved relation does not belong in the changeset it was assigned to"
407       assert_equal user.id, checkrelation.changeset.user_id,
408                    "saved relation does not belong to user that created it"
409       assert checkrelation.visible,
410              "saved relation is not visible"
411       # ok the relation is there but can we also retrieve it?
412       get api_relation_path(relationid)
413       assert_response :success
414     end
415
416     # ------------------------------------
417     # Test updating relations
418     # ------------------------------------
419
420     def test_update_wrong_id
421       user = create(:user)
422       changeset = create(:changeset, :user => user)
423       relation = create(:relation)
424       other_relation = create(:relation)
425
426       auth_header = bearer_authorization_header user
427       get api_relation_path(relation)
428       assert_response :success
429       rel = XML::Parser.string(@response.body).parse
430
431       update_changeset(rel, changeset.id)
432       put api_relation_path(other_relation), :params => rel.to_s, :headers => auth_header
433       assert_response :bad_request
434     end
435
436     # -------------------------------------
437     # Test creating some invalid relations.
438     # -------------------------------------
439
440     def test_create_invalid
441       user = create(:user)
442       changeset = create(:changeset, :user => user)
443
444       auth_header = bearer_authorization_header user
445
446       # create a relation with non-existing node as member
447       xml = <<~OSM
448         <osm>
449           <relation changeset='#{changeset.id}'>
450             <member type='node' ref='0'/>
451           </relation>
452         </osm>
453       OSM
454       post api_relations_path, :params => xml, :headers => auth_header
455       # expect failure
456       assert_response :precondition_failed,
457                       "relation upload with invalid node did not return 'precondition failed'"
458       assert_equal "Precondition failed: Relation with id  cannot be saved due to Node with id 0", @response.body
459     end
460
461     # -------------------------------------
462     # Test creating a relation, with some invalid XML
463     # -------------------------------------
464     def test_create_invalid_xml
465       user = create(:user)
466       changeset = create(:changeset, :user => user)
467       node = create(:node)
468
469       auth_header = bearer_authorization_header user
470
471       # create some xml that should return an error
472       xml = <<~OSM
473         <osm>
474           <relation changeset='#{changeset.id}'>
475             <member type='type' ref='#{node.id}' role=''/>
476           </relation>
477         </osm>
478       OSM
479       post api_relations_path, :params => xml, :headers => auth_header
480       # expect failure
481       assert_response :bad_request
482       assert_match(/Cannot parse valid relation from xml string/, @response.body)
483       assert_match(/The type is not allowed only, /, @response.body)
484     end
485
486     # -------------------------------------
487     # Test deleting relations.
488     # -------------------------------------
489
490     def test_destroy
491       private_user = create(:user, :data_public => false)
492       private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
493       user = create(:user)
494       closed_changeset = create(:changeset, :closed, :user => user)
495       changeset = create(:changeset, :user => user)
496       relation = create(:relation)
497       used_relation = create(:relation)
498       super_relation = create(:relation_member, :member => used_relation).relation
499       deleted_relation = create(:relation, :deleted)
500       multi_tag_relation = create(:relation)
501       create_list(:relation_tag, 4, :relation => multi_tag_relation)
502
503       ## First try to delete relation without auth
504       delete api_relation_path(relation)
505       assert_response :unauthorized
506
507       ## Then try with the private user, to make sure that you get a forbidden
508       auth_header = bearer_authorization_header private_user
509
510       # this shouldn't work, as we should need the payload...
511       delete api_relation_path(relation), :headers => auth_header
512       assert_response :forbidden
513
514       # try to delete without specifying a changeset
515       xml = "<osm><relation id='#{relation.id}'/></osm>"
516       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
517       assert_response :forbidden
518
519       # try to delete with an invalid (closed) changeset
520       xml = update_changeset(xml_for_relation(relation),
521                              private_user_closed_changeset.id)
522       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
523       assert_response :forbidden
524
525       # try to delete with an invalid (non-existent) changeset
526       xml = update_changeset(xml_for_relation(relation), 0)
527       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
528       assert_response :forbidden
529
530       # this won't work because the relation is in-use by another relation
531       xml = xml_for_relation(used_relation)
532       delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
533       assert_response :forbidden
534
535       # this should work when we provide the appropriate payload...
536       xml = xml_for_relation(relation)
537       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
538       assert_response :forbidden
539
540       # this won't work since the relation is already deleted
541       xml = xml_for_relation(deleted_relation)
542       delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
543       assert_response :forbidden
544
545       # this won't work since the relation never existed
546       delete api_relation_path(0), :headers => auth_header
547       assert_response :forbidden
548
549       ## now set auth for the public user
550       auth_header = bearer_authorization_header user
551
552       # this shouldn't work, as we should need the payload...
553       delete api_relation_path(relation), :headers => auth_header
554       assert_response :bad_request
555
556       # try to delete without specifying a changeset
557       xml = "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
558       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
559       assert_response :bad_request
560       assert_match(/Changeset id is missing/, @response.body)
561
562       # try to delete with an invalid (closed) changeset
563       xml = update_changeset(xml_for_relation(relation),
564                              closed_changeset.id)
565       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
566       assert_response :conflict
567
568       # try to delete with an invalid (non-existent) changeset
569       xml = update_changeset(xml_for_relation(relation), 0)
570       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
571       assert_response :conflict
572
573       # this won't work because the relation is in a changeset owned by someone else
574       xml = update_changeset(xml_for_relation(relation), create(:changeset).id)
575       delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
576       assert_response :conflict,
577                       "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
578
579       # this won't work because the relation in the payload is different to that passed
580       xml = update_changeset(xml_for_relation(relation), changeset.id)
581       delete api_relation_path(create(:relation)), :params => xml.to_s, :headers => auth_header
582       assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
583
584       # this won't work because the relation is in-use by another relation
585       xml = update_changeset(xml_for_relation(used_relation), changeset.id)
586       delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
587       assert_response :precondition_failed,
588                       "shouldn't be able to delete a relation used in a relation (#{@response.body})"
589       assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
590
591       # this should work when we provide the appropriate payload...
592       xml = update_changeset(xml_for_relation(multi_tag_relation), changeset.id)
593       delete api_relation_path(multi_tag_relation), :params => xml.to_s, :headers => auth_header
594       assert_response :success
595
596       # valid delete should return the new version number, which should
597       # be greater than the old version number
598       assert_operator @response.body.to_i, :>, multi_tag_relation.version, "delete request should return a new version number for relation"
599
600       # this won't work since the relation is already deleted
601       xml = update_changeset(xml_for_relation(deleted_relation), changeset.id)
602       delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
603       assert_response :gone
604
605       # Public visible relation needs to be deleted
606       xml = update_changeset(xml_for_relation(super_relation), changeset.id)
607       delete api_relation_path(super_relation), :params => xml.to_s, :headers => auth_header
608       assert_response :success
609
610       # this works now because the relation which was using this one
611       # has been deleted.
612       xml = update_changeset(xml_for_relation(used_relation), changeset.id)
613       delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
614       assert_response :success,
615                       "should be able to delete a relation used in an old relation (#{@response.body})"
616
617       # this won't work since the relation never existed
618       delete api_relation_path(0), :headers => auth_header
619       assert_response :not_found
620     end
621
622     ##
623     # test initial rate limit
624     def test_initial_rate_limit
625       # create a user
626       user = create(:user)
627
628       # create some nodes
629       node1 = create(:node)
630       node2 = create(:node)
631
632       # create a changeset that puts us near the initial rate limit
633       changeset = create(:changeset, :user => user,
634                                      :created_at => Time.now.utc - 5.minutes,
635                                      :num_changes => Settings.initial_changes_per_hour - 1)
636
637       # create authentication header
638       auth_header = bearer_authorization_header user
639
640       # try creating a relation
641       xml = <<~OSM
642         <osm>
643           <relation changeset='#{changeset.id}'>
644             <member ref='#{node1.id}' type='node' role='some'/>
645             <member ref='#{node2.id}' type='node' role='some'/>
646           </relation>
647         </osm>
648       OSM
649       post api_relations_path, :params => xml, :headers => auth_header
650       assert_response :success, "relation create did not return success status"
651
652       # get the id of the relation we created
653       relationid = @response.body
654
655       # try updating the relation, which should be rate limited
656       xml = <<~OSM
657         <osm>
658           <relation id='#{relationid}' version='1' changeset='#{changeset.id}'>
659             <member ref='#{node2.id}' type='node' role='some'/>
660             <member ref='#{node1.id}' type='node' role='some'/>
661           </relation>
662         </osm>
663       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
674         <osm>
675           <relation changeset='#{changeset.id}'>
676             <member ref='#{node1.id}' type='node' role='some'/>
677             <member ref='#{node2.id}' type='node' role='some'/>
678           </relation>
679         </osm>
680       OSM
681       post api_relations_path, :params => xml, :headers => auth_header
682       assert_response :too_many_requests, "relation create did not hit rate limit"
683     end
684
685     ##
686     # test maximum rate limit
687     def test_maximum_rate_limit
688       # create a user
689       user = create(:user)
690
691       # create some nodes
692       node1 = create(:node)
693       node2 = create(:node)
694
695       # create a changeset to establish our initial edit time
696       changeset = create(:changeset, :user => user,
697                                      :created_at => Time.now.utc - 28.days)
698
699       # create changeset to put us near the maximum rate limit
700       total_changes = Settings.max_changes_per_hour - 1
701       while total_changes.positive?
702         changes = [total_changes, Changeset::MAX_ELEMENTS].min
703         changeset = create(:changeset, :user => user,
704                                        :created_at => Time.now.utc - 5.minutes,
705                                        :num_changes => changes)
706         total_changes -= changes
707       end
708
709       # create authentication header
710       auth_header = bearer_authorization_header user
711
712       # try creating a relation
713       xml = <<~OSM
714         <osm>
715           <relation changeset='#{changeset.id}'>
716             <member ref='#{node1.id}' type='node' role='some'/>
717             <member ref='#{node2.id}' type='node' role='some'/>
718           </relation>
719         </osm>
720       OSM
721       post api_relations_path, :params => xml, :headers => auth_header
722       assert_response :success, "relation create did not return success status"
723
724       # get the id of the relation we created
725       relationid = @response.body
726
727       # try updating the relation, which should be rate limited
728       xml = <<~OSM
729         <osm>
730           <relation id='#{relationid}' version='1' changeset='#{changeset.id}'>
731             <member ref='#{node2.id}' type='node' role='some'/>
732             <member ref='#{node1.id}' type='node' role='some'/>
733           </relation>
734         </osm>
735       OSM
736       put api_relation_path(relationid), :params => xml, :headers => auth_header
737       assert_response :too_many_requests, "relation update did not hit rate limit"
738
739       # try deleting the relation, which should be rate limited
740       xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
741       delete api_relation_path(relationid), :params => xml, :headers => auth_header
742       assert_response :too_many_requests, "relation delete did not hit rate limit"
743
744       # try creating a relation, which should be rate limited
745       xml = <<~OSM
746         <osm>
747           <relation changeset='#{changeset.id}'>
748             <member ref='#{node1.id}' type='node' role='some'/>
749             <member ref='#{node2.id}' type='node' role='some'/>
750           </relation>
751         </osm>
752       OSM
753       post api_relations_path, :params => xml, :headers => auth_header
754       assert_response :too_many_requests, "relation create did not hit rate limit"
755     end
756
757     private
758
759     ##
760     # update the changeset_id of a node element
761     def update_changeset(xml, changeset_id)
762       xml_attr_rewrite(xml, "changeset", changeset_id)
763     end
764
765     ##
766     # update an attribute in the node element
767     def xml_attr_rewrite(xml, name, value)
768       xml.find("//osm/relation").first[name] = value.to_s
769       xml
770     end
771   end
772 end