4 class WaysControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/ways", :method => :get },
10 { :controller => "api/ways", :action => "index" }
13 { :path => "/api/0.6/ways.json", :method => :get },
14 { :controller => "api/ways", :action => "index", :format => "json" }
17 { :path => "/api/0.6/ways", :method => :post },
18 { :controller => "api/ways", :action => "create" }
21 { :path => "/api/0.6/way/1/full", :method => :get },
22 { :controller => "api/ways", :action => "full", :id => "1" }
25 { :path => "/api/0.6/way/1/full.json", :method => :get },
26 { :controller => "api/ways", :action => "full", :id => "1", :format => "json" }
29 { :path => "/api/0.6/way/1", :method => :get },
30 { :controller => "api/ways", :action => "show", :id => "1" }
33 { :path => "/api/0.6/way/1.json", :method => :get },
34 { :controller => "api/ways", :action => "show", :id => "1", :format => "json" }
37 { :path => "/api/0.6/way/1", :method => :put },
38 { :controller => "api/ways", :action => "update", :id => "1" }
41 { :path => "/api/0.6/way/1", :method => :delete },
42 { :controller => "api/ways", :action => "destroy", :id => "1" }
46 { :controller => "api/ways", :action => "create" },
47 { :path => "/api/0.6/way/create", :method => :put }
52 # test fetching multiple ways
55 way2 = create(:way, :deleted)
59 # check error when no parameter provided
61 assert_response :bad_request
63 # check error when no parameter value provided
64 get api_ways_path(:ways => "")
65 assert_response :bad_request
68 get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}")
69 assert_response :success
70 assert_select "osm" do
71 assert_select "way", :count => 4
72 assert_select "way[id='#{way1.id}'][visible='true']", :count => 1
73 assert_select "way[id='#{way2.id}'][visible='false']", :count => 1
74 assert_select "way[id='#{way3.id}'][visible='true']", :count => 1
75 assert_select "way[id='#{way4.id}'][visible='true']", :count => 1
78 # test a working call with json format
79 get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}", :format => "json")
81 js = ActiveSupport::JSON.decode(@response.body)
83 assert_equal 4, js["elements"].count
84 assert_equal 4, (js["elements"].count { |a| a["type"] == "way" })
85 assert_equal 1, (js["elements"].count { |a| a["id"] == way1.id && a["visible"].nil? })
86 assert_equal 1, (js["elements"].count { |a| a["id"] == way2.id && a["visible"] == false })
87 assert_equal 1, (js["elements"].count { |a| a["id"] == way3.id && a["visible"].nil? })
88 assert_equal 1, (js["elements"].count { |a| a["id"] == way4.id && a["visible"].nil? })
90 # check error when a non-existent way is included
91 get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0")
92 assert_response :not_found
95 # -------------------------------------
97 # -------------------------------------
100 # check that a visible way is returned properly
101 get api_way_path(create(:way))
102 assert_response :success
104 # check that an invisible way is not returned
105 get api_way_path(create(:way, :deleted))
106 assert_response :gone
108 # check chat a non-existent way is not returned
110 assert_response :not_found
114 # check the "full" mode
116 way = create(:way_with_nodes, :nodes_count => 3)
118 get way_full_path(way)
120 assert_response :success
122 # Check the way is correctly returned
123 assert_select "osm way[id='#{way.id}'][version='1'][visible='true']", 1
125 # check that each node in the way appears once in the output as a
126 # reference and as the node element.
127 way.nodes.each do |n|
128 assert_select "osm way nd[ref='#{n.id}']", 1
129 assert_select "osm node[id='#{n.id}'][version='1'][lat='#{format('%<lat>.7f', :lat => n.lat)}'][lon='#{format('%<lon>.7f', :lon => n.lon)}']", 1
133 def test_full_deleted
134 way = create(:way, :deleted)
136 get way_full_path(way)
138 assert_response :gone
141 # -------------------------------------
142 # Test simple way creation.
143 # -------------------------------------
146 node1 = create(:node)
147 node2 = create(:node)
148 private_user = create(:user, :data_public => false)
149 private_changeset = create(:changeset, :user => private_user)
151 changeset = create(:changeset, :user => user)
153 ## First check that it fails when creating a way using a non-public user
154 auth_header = bearer_authorization_header private_user
156 # use the first user's open changeset
157 changeset_id = private_changeset.id
159 # create a way with pre-existing nodes
160 xml = "<osm><way changeset='#{changeset_id}'>" \
161 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
162 "<tag k='test' v='yes' /></way></osm>"
163 post api_ways_path, :params => xml, :headers => auth_header
165 assert_response :forbidden,
166 "way upload did not return forbidden status"
168 ## Now use a public user
169 auth_header = bearer_authorization_header user
171 # use the first user's open changeset
172 changeset_id = changeset.id
174 # create a way with pre-existing nodes
175 xml = "<osm><way changeset='#{changeset_id}'>" \
176 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
177 "<tag k='test' v='yes' /></way></osm>"
178 post api_ways_path, :params => xml, :headers => auth_header
180 assert_response :success,
181 "way upload did not return success status"
182 # read id of created way and search for it
183 wayid = @response.body
184 checkway = Way.find(wayid)
185 assert_not_nil checkway,
186 "uploaded way not found in data base after upload"
188 assert_equal(2, checkway.nds.length, "saved way does not contain exactly one node")
189 assert_equal checkway.nds[0], node1.id,
190 "saved way does not contain the right node on pos 0"
191 assert_equal checkway.nds[1], node2.id,
192 "saved way does not contain the right node on pos 1"
193 assert_equal checkway.changeset_id, changeset_id,
194 "saved way does not belong to the correct changeset"
195 assert_equal user.id, checkway.changeset.user_id,
196 "saved way does not belong to user that created it"
197 assert checkway.visible,
198 "saved way is not visible"
201 # -------------------------------------
202 # Test creating some invalid ways.
203 # -------------------------------------
205 def test_create_invalid
207 private_user = create(:user, :data_public => false)
208 private_open_changeset = create(:changeset, :user => private_user)
209 private_closed_changeset = create(:changeset, :closed, :user => private_user)
211 open_changeset = create(:changeset, :user => user)
212 closed_changeset = create(:changeset, :closed, :user => user)
214 ## First test with a private user to make sure that they are not authorized
215 auth_header = bearer_authorization_header private_user
217 # use the first user's open changeset
218 # create a way with non-existing node
219 xml = "<osm><way changeset='#{private_open_changeset.id}'>" \
220 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
221 post api_ways_path, :params => xml, :headers => auth_header
223 assert_response :forbidden,
224 "way upload with invalid node using a private user did not return 'forbidden'"
226 # create a way with no nodes
227 xml = "<osm><way changeset='#{private_open_changeset.id}'>" \
228 "<tag k='test' v='yes' /></way></osm>"
229 post api_ways_path, :params => xml, :headers => auth_header
231 assert_response :forbidden,
232 "way upload with no node using a private userdid not return 'forbidden'"
234 # create a way inside a closed changeset
235 xml = "<osm><way changeset='#{private_closed_changeset.id}'>" \
236 "<nd ref='#{node.id}'/></way></osm>"
237 post api_ways_path, :params => xml, :headers => auth_header
239 assert_response :forbidden,
240 "way upload to closed changeset with a private user did not return 'forbidden'"
242 ## Now test with a public user
243 auth_header = bearer_authorization_header user
245 # use the first user's open changeset
246 # create a way with non-existing node
247 xml = "<osm><way changeset='#{open_changeset.id}'>" \
248 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
249 post api_ways_path, :params => xml, :headers => auth_header
251 assert_response :precondition_failed,
252 "way upload with invalid node did not return 'precondition failed'"
253 assert_equal "Precondition failed: Way requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
255 # create a way with no nodes
256 xml = "<osm><way changeset='#{open_changeset.id}'>" \
257 "<tag k='test' v='yes' /></way></osm>"
258 post api_ways_path, :params => xml, :headers => auth_header
260 assert_response :precondition_failed,
261 "way upload with no node did not return 'precondition failed'"
262 assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
264 # create a way inside a closed changeset
265 xml = "<osm><way changeset='#{closed_changeset.id}'>" \
266 "<nd ref='#{node.id}'/></way></osm>"
267 post api_ways_path, :params => xml, :headers => auth_header
269 assert_response :conflict,
270 "way upload to closed changeset did not return 'conflict'"
272 # create a way with a tag which is too long
273 xml = "<osm><way changeset='#{open_changeset.id}'>" \
274 "<nd ref='#{node.id}'/>" \
275 "<tag k='foo' v='#{'x' * 256}'/>" \
277 post api_ways_path, :params => xml, :headers => auth_header
279 assert_response :bad_request,
280 "way upload to with too long tag did not return 'bad_request'"
283 # -------------------------------------
284 # Test deleting ways.
285 # -------------------------------------
288 private_user = create(:user, :data_public => false)
289 private_open_changeset = create(:changeset, :user => private_user)
290 private_closed_changeset = create(:changeset, :closed, :user => private_user)
291 private_way = create(:way, :changeset => private_open_changeset)
292 private_deleted_way = create(:way, :deleted, :changeset => private_open_changeset)
293 private_used_way = create(:way, :changeset => private_open_changeset)
294 create(:relation_member, :member => private_used_way)
296 open_changeset = create(:changeset, :user => user)
297 closed_changeset = create(:changeset, :closed, :user => user)
298 way = create(:way, :changeset => open_changeset)
299 deleted_way = create(:way, :deleted, :changeset => open_changeset)
300 used_way = create(:way, :changeset => open_changeset)
301 relation_member = create(:relation_member, :member => used_way)
302 relation = relation_member.relation
304 # first try to delete way without auth
305 delete api_way_path(way)
306 assert_response :unauthorized
308 # now set auth using the private user
309 auth_header = bearer_authorization_header private_user
311 # this shouldn't work as with the 0.6 api we need pay load to delete
312 delete api_way_path(private_way), :headers => auth_header
313 assert_response :forbidden
315 # Now try without having a changeset
316 xml = "<osm><way id='#{private_way.id}'/></osm>"
317 delete api_way_path(private_way), :params => xml.to_s, :headers => auth_header
318 assert_response :forbidden
320 # try to delete with an invalid (closed) changeset
321 xml = update_changeset(xml_for_way(private_way), private_closed_changeset.id)
322 delete api_way_path(private_way), :params => xml.to_s, :headers => auth_header
323 assert_response :forbidden
325 # try to delete with an invalid (non-existent) changeset
326 xml = update_changeset(xml_for_way(private_way), 0)
327 delete api_way_path(private_way), :params => xml.to_s, :headers => auth_header
328 assert_response :forbidden
330 # Now try with a valid changeset
331 xml = xml_for_way(private_way)
332 delete api_way_path(private_way), :params => xml.to_s, :headers => auth_header
333 assert_response :forbidden
335 # check the returned value - should be the new version number
336 # valid delete should return the new version number, which should
337 # be greater than the old version number
338 # assert @response.body.to_i > current_ways(:visible_way).version,
339 # "delete request should return a new version number for way"
341 # this won't work since the way is already deleted
342 xml = xml_for_way(private_deleted_way)
343 delete api_way_path(private_deleted_way), :params => xml.to_s, :headers => auth_header
344 assert_response :forbidden
346 # this shouldn't work as the way is used in a relation
347 xml = xml_for_way(private_used_way)
348 delete api_way_path(private_used_way), :params => xml.to_s, :headers => auth_header
349 assert_response :forbidden,
350 "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
352 # this won't work since the way never existed
353 delete api_way_path(0), :headers => auth_header
354 assert_response :forbidden
356 ### Now check with a public user
358 auth_header = bearer_authorization_header user
360 # this shouldn't work as with the 0.6 api we need pay load to delete
361 delete api_way_path(way), :headers => auth_header
362 assert_response :bad_request
364 # Now try without having a changeset
365 xml = "<osm><way id='#{way.id}'/></osm>"
366 delete api_way_path(way), :params => xml.to_s, :headers => auth_header
367 assert_response :bad_request
369 # try to delete with an invalid (closed) changeset
370 xml = update_changeset(xml_for_way(way), closed_changeset.id)
371 delete api_way_path(way), :params => xml.to_s, :headers => auth_header
372 assert_response :conflict
374 # try to delete with an invalid (non-existent) changeset
375 xml = update_changeset(xml_for_way(way), 0)
376 delete api_way_path(way), :params => xml.to_s, :headers => auth_header
377 assert_response :conflict
379 # Now try with a valid changeset
380 xml = xml_for_way(way)
381 delete api_way_path(way), :params => xml.to_s, :headers => auth_header
382 assert_response :success
384 # check the returned value - should be the new version number
385 # valid delete should return the new version number, which should
386 # be greater than the old version number
387 assert_operator @response.body.to_i, :>, way.version, "delete request should return a new version number for way"
389 # this won't work since the way is already deleted
390 xml = xml_for_way(deleted_way)
391 delete api_way_path(deleted_way), :params => xml.to_s, :headers => auth_header
392 assert_response :gone
394 # this shouldn't work as the way is used in a relation
395 xml = xml_for_way(used_way)
396 delete api_way_path(used_way), :params => xml.to_s, :headers => auth_header
397 assert_response :precondition_failed,
398 "shouldn't be able to delete a way used in a relation (#{@response.body})"
399 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
401 # this won't work since the way never existed
402 delete api_way_path(0), :params => xml.to_s, :headers => auth_header
403 assert_response :not_found
407 # tests whether the API works and prevents incorrect use while trying
410 private_user = create(:user, :data_public => false)
411 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
413 way = create(:way, :changeset => create(:changeset, :user => user))
415 create(:way_node, :way => private_way, :node => node)
416 create(:way_node, :way => way, :node => node)
418 ## First test with no user credentials
419 # try and update a way without authorisation
420 xml = xml_for_way(way)
421 put api_way_path(way), :params => xml.to_s
422 assert_response :unauthorized
424 ## Second test with the private user
427 auth_header = bearer_authorization_header private_user
429 ## trying to break changesets
431 # try and update in someone else's changeset
432 xml = update_changeset(xml_for_way(private_way),
433 create(:changeset).id)
434 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
435 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
437 # try and update in a closed changeset
438 xml = update_changeset(xml_for_way(private_way),
439 create(:changeset, :closed, :user => private_user).id)
440 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
441 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
443 # try and update in a non-existant changeset
444 xml = update_changeset(xml_for_way(private_way), 0)
445 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
446 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
448 ## try and submit invalid updates
449 xml = xml_replace_node(xml_for_way(private_way), node.id, 9999)
450 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
451 assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"
453 xml = xml_replace_node(xml_for_way(private_way), node.id, create(:node, :deleted).id)
454 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
455 assert_require_public_data "way with deleted node should be forbidden, when data isn't public"
457 ## finally, produce a good request which will still not work
458 xml = xml_for_way(private_way)
459 put api_way_path(private_way), :params => xml.to_s, :headers => auth_header
460 assert_require_public_data "should have failed with a forbidden when data isn't public"
462 ## Finally test with the public user
465 auth_header = bearer_authorization_header user
467 ## trying to break changesets
469 # try and update in someone else's changeset
470 xml = update_changeset(xml_for_way(way),
471 create(:changeset).id)
472 put api_way_path(way), :params => xml.to_s, :headers => auth_header
473 assert_response :conflict, "update with other user's changeset should be rejected"
475 # try and update in a closed changeset
476 xml = update_changeset(xml_for_way(way),
477 create(:changeset, :closed, :user => user).id)
478 put api_way_path(way), :params => xml.to_s, :headers => auth_header
479 assert_response :conflict, "update with closed changeset should be rejected"
481 # try and update in a non-existant changeset
482 xml = update_changeset(xml_for_way(way), 0)
483 put api_way_path(way), :params => xml.to_s, :headers => auth_header
484 assert_response :conflict, "update with changeset=0 should be rejected"
486 ## try and submit invalid updates
487 xml = xml_replace_node(xml_for_way(way), node.id, 9999)
488 put api_way_path(way), :params => xml.to_s, :headers => auth_header
489 assert_response :precondition_failed, "way with non-existent node should be rejected"
491 xml = xml_replace_node(xml_for_way(way), node.id, create(:node, :deleted).id)
492 put api_way_path(way), :params => xml.to_s, :headers => auth_header
493 assert_response :precondition_failed, "way with deleted node should be rejected"
495 ## next, attack the versioning
496 current_way_version = way.version
498 # try and submit a version behind
499 xml = xml_attr_rewrite(xml_for_way(way),
500 "version", current_way_version - 1)
501 put api_way_path(way), :params => xml.to_s, :headers => auth_header
502 assert_response :conflict, "should have failed on old version number"
504 # try and submit a version ahead
505 xml = xml_attr_rewrite(xml_for_way(way),
506 "version", current_way_version + 1)
507 put api_way_path(way), :params => xml.to_s, :headers => auth_header
508 assert_response :conflict, "should have failed on skipped version number"
510 # try and submit total crap in the version field
511 xml = xml_attr_rewrite(xml_for_way(way),
512 "version", "p1r4t3s!")
513 put api_way_path(way), :params => xml.to_s, :headers => auth_header
514 assert_response :conflict,
515 "should not be able to put 'p1r4at3s!' in the version field"
517 ## try an update with the wrong ID
518 xml = xml_for_way(create(:way))
519 put api_way_path(way), :params => xml.to_s, :headers => auth_header
520 assert_response :bad_request,
521 "should not be able to update a way with a different ID from the XML"
523 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
525 put api_way_path(way), :params => xml.to_s, :headers => auth_header
526 assert_response :bad_request,
527 "should not be able to update a way with non-OSM XML doc."
529 ## finally, produce a good request which should work
530 xml = xml_for_way(way)
531 put api_way_path(way), :params => xml.to_s, :headers => auth_header
532 assert_response :success, "a valid update request failed"
535 # ------------------------------------------------------------
537 # ------------------------------------------------------------
540 # Try adding a new tag to a way
542 private_user = create(:user, :data_public => false)
543 private_way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => private_user))
545 way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => user))
547 ## Try with the non-public user
549 auth_header = bearer_authorization_header private_user
551 # add an identical tag to the way
552 tag_xml = XML::Node.new("tag")
556 # add the tag into the existing xml
557 way_xml = xml_for_way(private_way)
558 way_xml.find("//osm/way").first << tag_xml
561 put api_way_path(private_way), :params => way_xml.to_s, :headers => auth_header
562 assert_response :forbidden,
563 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
565 ## Now try with the public user
567 auth_header = bearer_authorization_header user
569 # add an identical tag to the way
570 tag_xml = XML::Node.new("tag")
574 # add the tag into the existing xml
575 way_xml = xml_for_way(way)
576 way_xml.find("//osm/way").first << tag_xml
579 put api_way_path(way), :params => way_xml.to_s, :headers => auth_header
580 assert_response :success,
581 "adding a new tag to a way should succeed"
582 assert_equal way.version + 1, @response.body.to_i
586 # Try adding a duplicate of an existing tag to a way
587 def test_add_duplicate_tags
588 private_user = create(:user, :data_public => false)
589 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
590 private_existing_tag = create(:way_tag, :way => private_way)
592 way = create(:way, :changeset => create(:changeset, :user => user))
593 existing_tag = create(:way_tag, :way => way)
595 ## Try with the non-public user
597 auth_header = bearer_authorization_header private_user
599 # add an identical tag to the way
600 tag_xml = XML::Node.new("tag")
601 tag_xml["k"] = private_existing_tag.k
602 tag_xml["v"] = private_existing_tag.v
604 # add the tag into the existing xml
605 way_xml = xml_for_way(private_way)
606 way_xml.find("//osm/way").first << tag_xml
609 put api_way_path(private_way), :params => way_xml.to_s, :headers => auth_header
610 assert_response :forbidden,
611 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
613 ## Now try with the public user
615 auth_header = bearer_authorization_header user
617 # add an identical tag to the way
618 tag_xml = XML::Node.new("tag")
619 tag_xml["k"] = existing_tag.k
620 tag_xml["v"] = existing_tag.v
622 # add the tag into the existing xml
623 way_xml = xml_for_way(way)
624 way_xml.find("//osm/way").first << tag_xml
627 put api_way_path(way), :params => way_xml.to_s, :headers => auth_header
628 assert_response :bad_request,
629 "adding a duplicate tag to a way should fail with 'bad request'"
630 assert_equal "Element way/#{way.id} has duplicate tags with key #{existing_tag.k}", @response.body
634 # Try adding a new duplicate tags to a way
635 def test_new_duplicate_tags
636 private_user = create(:user, :data_public => false)
637 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
639 way = create(:way, :changeset => create(:changeset, :user => user))
641 ## First test with the non-public user so should be rejected
643 auth_header = bearer_authorization_header private_user
645 # create duplicate tag
646 tag_xml = XML::Node.new("tag")
647 tag_xml["k"] = "i_am_a_duplicate"
648 tag_xml["v"] = "foobar"
650 # add the tag into the existing xml
651 way_xml = xml_for_way(private_way)
653 # add two copies of the tag
654 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
657 put api_way_path(private_way), :params => way_xml.to_s, :headers => auth_header
658 assert_response :forbidden,
659 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
661 ## Now test with the public user
663 auth_header = bearer_authorization_header user
665 # create duplicate tag
666 tag_xml = XML::Node.new("tag")
667 tag_xml["k"] = "i_am_a_duplicate"
668 tag_xml["v"] = "foobar"
670 # add the tag into the existing xml
671 way_xml = xml_for_way(way)
673 # add two copies of the tag
674 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
677 put api_way_path(way), :params => way_xml.to_s, :headers => auth_header
678 assert_response :bad_request,
679 "adding new duplicate tags to a way should fail with 'bad request'"
680 assert_equal "Element way/#{way.id} has duplicate tags with key i_am_a_duplicate", @response.body
684 # Try adding a new duplicate tags to a way.
685 # But be a bit subtle - use unicode decoding ambiguities to use different
686 # binary strings which have the same decoding.
687 def test_invalid_duplicate_tags
688 private_user = create(:user, :data_public => false)
689 private_changeset = create(:changeset, :user => private_user)
691 changeset = create(:changeset, :user => user)
693 ## First make sure that you can't with a non-public user
695 auth_header = bearer_authorization_header private_user
697 # add the tag into the existing xml
698 way_str = "<osm><way changeset='#{private_changeset.id}'>"
699 way_str << "<tag k='addr:housenumber' v='1'/>"
700 way_str << "<tag k='addr:housenumber' v='2'/>"
701 way_str << "</way></osm>"
704 post api_ways_path, :params => way_str, :headers => auth_header
705 assert_response :forbidden,
706 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
708 ## Now do it with a public user
710 auth_header = bearer_authorization_header user
712 # add the tag into the existing xml
713 way_str = "<osm><way changeset='#{changeset.id}'>"
714 way_str << "<tag k='addr:housenumber' v='1'/>"
715 way_str << "<tag k='addr:housenumber' v='2'/>"
716 way_str << "</way></osm>"
719 post api_ways_path, :params => way_str, :headers => auth_header
720 assert_response :bad_request,
721 "adding new duplicate tags to a way should fail with 'bad request'"
722 assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
726 # test that a call to ways_for_node returns all ways that contain the node
727 # and none that don't.
728 def test_ways_for_node
732 create(:way_node, :way => way1, :node => node)
733 create(:way_node, :way => way2, :node => node)
734 # create an unrelated way
735 create(:way_with_nodes, :nodes_count => 2)
736 # create a way which used to use the node
737 way3_v1 = create(:old_way, :version => 1)
738 _way3_v2 = create(:old_way, :current_way => way3_v1.current_way, :version => 2)
739 create(:old_way_node, :old_way => way3_v1, :node => node)
741 get node_ways_path(node)
742 assert_response :success
743 ways_xml = XML::Parser.string(@response.body).parse
744 assert_not_nil ways_xml, "failed to parse ways_for_node response"
746 # check that the set of IDs match expectations
747 expected_way_ids = [way1.id,
749 found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
750 assert_equal expected_way_ids.sort, found_way_ids.sort,
751 "expected ways for node #{node.id} did not match found"
753 # check the full ways to ensure we're not missing anything
754 expected_way_ids.each do |id|
755 way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
756 assert_ways_are_equal(Way.find(id),
757 Way.from_xml_node(way_xml))
762 # test initial rate limit
763 def test_initial_rate_limit
768 node1 = create(:node)
769 node2 = create(:node)
771 # create a changeset that puts us near the initial rate limit
772 changeset = create(:changeset, :user => user,
773 :created_at => Time.now.utc - 5.minutes,
774 :num_changes => Settings.initial_changes_per_hour - 1)
776 # create authentication header
777 auth_header = bearer_authorization_header user
780 xml = "<osm><way changeset='#{changeset.id}'>" \
781 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
782 "<tag k='test' v='yes' /></way></osm>"
783 post api_ways_path, :params => xml, :headers => auth_header
784 assert_response :success, "way create did not return success status"
786 # get the id of the way we created
787 wayid = @response.body
789 # try updating the way, which should be rate limited
790 xml = "<osm><way id='#{wayid}' version='1' changeset='#{changeset.id}'>" \
791 "<nd ref='#{node2.id}'/><nd ref='#{node1.id}'/>" \
792 "<tag k='test' v='yes' /></way></osm>"
793 put api_way_path(wayid), :params => xml, :headers => auth_header
794 assert_response :too_many_requests, "way update did not hit rate limit"
796 # try deleting the way, which should be rate limited
797 xml = "<osm><way id='#{wayid}' version='2' changeset='#{changeset.id}'/></osm>"
798 delete api_way_path(wayid), :params => xml, :headers => auth_header
799 assert_response :too_many_requests, "way delete did not hit rate limit"
801 # try creating a way, which should be rate limited
802 xml = "<osm><way changeset='#{changeset.id}'>" \
803 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
804 "<tag k='test' v='yes' /></way></osm>"
805 post api_ways_path, :params => xml, :headers => auth_header
806 assert_response :too_many_requests, "way create did not hit rate limit"
810 # test maximum rate limit
811 def test_maximum_rate_limit
816 node1 = create(:node)
817 node2 = create(:node)
819 # create a changeset to establish our initial edit time
820 changeset = create(:changeset, :user => user,
821 :created_at => Time.now.utc - 28.days)
823 # create changeset to put us near the maximum rate limit
824 total_changes = Settings.max_changes_per_hour - 1
825 while total_changes.positive?
826 changes = [total_changes, Changeset::MAX_ELEMENTS].min
827 changeset = create(:changeset, :user => user,
828 :created_at => Time.now.utc - 5.minutes,
829 :num_changes => changes)
830 total_changes -= changes
833 # create authentication header
834 auth_header = bearer_authorization_header user
837 xml = "<osm><way changeset='#{changeset.id}'>" \
838 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
839 "<tag k='test' v='yes' /></way></osm>"
840 post api_ways_path, :params => xml, :headers => auth_header
841 assert_response :success, "way create did not return success status"
843 # get the id of the way we created
844 wayid = @response.body
846 # try updating the way, which should be rate limited
847 xml = "<osm><way id='#{wayid}' version='1' changeset='#{changeset.id}'>" \
848 "<nd ref='#{node2.id}'/><nd ref='#{node1.id}'/>" \
849 "<tag k='test' v='yes' /></way></osm>"
850 put api_way_path(wayid), :params => xml, :headers => auth_header
851 assert_response :too_many_requests, "way update did not hit rate limit"
853 # try deleting the way, which should be rate limited
854 xml = "<osm><way id='#{wayid}' version='2' changeset='#{changeset.id}'/></osm>"
855 delete api_way_path(wayid), :params => xml, :headers => auth_header
856 assert_response :too_many_requests, "way delete did not hit rate limit"
858 # try creating a way, which should be rate limited
859 xml = "<osm><way changeset='#{changeset.id}'>" \
860 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
861 "<tag k='test' v='yes' /></way></osm>"
862 post api_ways_path, :params => xml, :headers => auth_header
863 assert_response :too_many_requests, "way create did not hit rate limit"
869 # update the changeset_id of a way element
870 def update_changeset(xml, changeset_id)
871 xml_attr_rewrite(xml, "changeset", changeset_id)
875 # update an attribute in the way element
876 def xml_attr_rewrite(xml, name, value)
877 xml.find("//osm/way").first[name] = value.to_s
882 # replace a node in a way element
883 def xml_replace_node(xml, old_node, new_node)
884 xml.find("//osm/way/nd[@ref='#{old_node}']").first["ref"] = new_node.to_s