5 class AmfControllerTest < ActionController::TestCase
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/amf/read", :method => :post },
13 { :controller => "amf", :action => "amf_read" }
16 { :path => "/api/0.6/amf/write", :method => :post },
17 { :controller => "amf", :action => "amf_write" }
22 [:public_user, :german_user].each do |id|
25 amf_content "getpresets", "/1", ["#{user.email}:test", ""]
27 assert_response :success
29 presets = amf_result("/1")
31 assert_equal 15, presets.length
32 assert_equal POTLATCH_PRESETS[0], presets[0]
33 assert_equal POTLATCH_PRESETS[1], presets[1]
34 assert_equal POTLATCH_PRESETS[2], presets[2]
35 assert_equal POTLATCH_PRESETS[3], presets[3]
36 assert_equal POTLATCH_PRESETS[4], presets[4]
37 assert_equal POTLATCH_PRESETS[5], presets[5]
38 assert_equal POTLATCH_PRESETS[6], presets[6]
39 assert_equal POTLATCH_PRESETS[7], presets[7]
40 assert_equal POTLATCH_PRESETS[8], presets[8]
41 assert_equal POTLATCH_PRESETS[9], presets[9]
42 assert_equal POTLATCH_PRESETS[10], presets[10]
43 assert_equal POTLATCH_PRESETS[12], presets[12]
44 assert_equal user.languages.first, presets[13]["__potlatch_locale"]
50 id = current_ways(:visible_way).id
51 amf_content "getway", "/1", [id]
53 assert_response :success
55 way = amf_result("/1")
56 assert_equal 0, way[0]
57 assert_equal "", way[1]
58 assert_equal id, way[2]
59 assert_equal 1, way[3].length
60 assert_equal 3, way[3][0][2]
61 assert_equal 1, way[5]
62 assert_equal 2, way[6]
65 def test_getway_invisible
66 # check an invisible way
67 id = current_ways(:invisible_way).id
68 amf_content "getway", "/1", [id]
70 assert_response :success
72 way = amf_result("/1")
73 assert_equal -4, way[0], -4
74 assert_equal "way", way[1]
75 assert_equal id, way[2]
76 assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
79 def test_getway_with_versions
80 # check a way with multiple versions
81 id = current_ways(:way_with_versions).id
82 amf_content "getway", "/1", [id]
84 assert_response :success
86 way = amf_result("/1")
87 assert_equal 0, way[0]
88 assert_equal "", way[1]
89 assert_equal id, way[2]
90 assert_equal 1, way[3].length
91 assert_equal 15, way[3][0][2]
92 assert_equal 4, way[5]
93 assert_equal 2, way[6]
96 def test_getway_with_duplicate_nodes
97 # check a way with duplicate nodes
98 id = current_ways(:way_with_duplicate_nodes).id
99 amf_content "getway", "/1", [id]
101 assert_response :success
103 way = amf_result("/1")
104 assert_equal 0, way[0]
105 assert_equal "", way[1]
106 assert_equal id, way[2]
107 assert_equal 2, way[3].length
108 assert_equal 4, way[3][0][2]
109 assert_equal 4, way[3][1][2]
110 assert_equal 1, way[5]
111 assert_equal 2, way[6]
114 def test_getway_with_multiple_nodes
115 # check a way with multiple nodes
116 id = current_ways(:way_with_multiple_nodes).id
117 amf_content "getway", "/1", [id]
119 assert_response :success
121 way = amf_result("/1")
122 assert_equal 0, way[0]
123 assert_equal "", way[1]
124 assert_equal id, way[2]
125 assert_equal 3, way[3].length
126 assert_equal 4, way[3][0][2]
127 assert_equal 15, way[3][1][2]
128 assert_equal 6, way[3][2][2]
129 assert_equal 2, way[5]
130 assert_equal 2, way[6]
133 def test_getway_nonexistent
134 # check chat a non-existent way is not returned
135 amf_content "getway", "/1", [0]
137 assert_response :success
139 way = amf_result("/1")
140 assert_equal -4, way[0]
141 assert_equal "way", way[1]
142 assert_equal 0, way[2]
143 assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
147 node = current_nodes(:used_node_1)
148 minlon = node.lon - 0.1
149 minlat = node.lat - 0.1
150 maxlon = node.lon + 0.1
151 maxlat = node.lat + 0.1
152 amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
154 assert_response :success
157 # check contents of message
158 map = amf_result "/1"
159 assert_equal 0, map[0], "map error code should be 0"
160 assert_equal "", map[1], "map error text should be empty"
162 # check the formatting of the message
163 assert_equal 5, map.length, "map should have length 5"
164 assert_equal Array, map[2].class, 'map "ways" element should be an array'
165 assert_equal Array, map[3].class, 'map "nodes" element should be an array'
166 assert_equal Array, map[4].class, 'map "relations" element should be an array'
168 assert_equal 2, w.length, "way should be (id, version) pair"
169 assert w[0] == w[0].floor, "way ID should be an integer"
170 assert w[1] == w[1].floor, "way version should be an integer"
174 assert_equal 5, w.length, "node should be (id, lat, lon, [tags], version) tuple"
175 assert n[0] == n[0].floor, "node ID should be an integer"
176 assert n[1] >= minlat - 0.01, "node lat should be greater than min"
177 assert n[1] <= maxlat - 0.01, "node lat should be less than max"
178 assert n[2] >= minlon - 0.01, "node lon should be greater than min"
179 assert n[2] <= maxlon - 0.01, "node lon should be less than max"
180 assert_equal Array, a[3].class, "node tags should be array"
181 assert n[4] == n[4].floor, "node version should be an integer"
185 assert_equal 2, r.length, "relation should be (id, version) pair"
186 assert r[0] == r[0].floor, "relation ID should be an integer"
187 assert r[1] == r[1].floor, "relation version should be an integer"
190 # TODO: looks like amf_controller changed since this test was written
191 # so someone who knows what they're doing should check this!
192 ways = map[2].collect { |x| x[0] }
193 assert ways.include?(current_ways(:used_way).id),
194 "map should include used way"
195 assert !ways.include?(current_ways(:invisible_way).id),
196 "map should not include deleted way"
200 # checks that too-large a bounding box will not be served.
201 def test_whichways_toobig
202 bbox = [-0.1, -0.1, 1.1, 1.1]
203 check_bboxes_are_bad [bbox] do |map, _bbox|
204 assert_boundary_error map, " The server said: The maximum bbox size is 0.25, and your request was too large. Either request a smaller area, or use planet.osm"
209 # checks that an invalid bounding box will not be served. in this case
210 # one with max < min latitudes.
212 # NOTE: the controller expands the bbox by 0.01 in each direction!
213 def test_whichways_badlat
214 bboxes = [[0, 0.1, 0.1, 0], [-0.1, 80, 0.1, 70], [0.24, 54.35, 0.25, 54.33]]
215 check_bboxes_are_bad bboxes do |map, bbox|
216 assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
221 # same as test_whichways_badlat, but for longitudes
223 # NOTE: the controller expands the bbox by 0.01 in each direction!
224 def test_whichways_badlon
225 bboxes = [[80, -0.1, 70, 0.1], [54.35, 0.24, 54.33, 0.25]]
226 check_bboxes_are_bad bboxes do |map, bbox|
227 assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
231 def test_whichways_deleted
232 node = current_nodes(:used_node_1)
233 minlon = node.lon - 0.1
234 minlat = node.lat - 0.1
235 maxlon = node.lon + 0.1
236 maxlat = node.lat + 0.1
237 amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
239 assert_response :success
242 # check contents of message
243 map = amf_result "/1"
244 assert_equal 0, map[0], "first map element should be 0"
245 assert_equal "", map[1], "second map element should be an empty string"
246 assert_equal Array, map[2].class, "third map element should be an array"
247 # TODO: looks like amf_controller changed since this test was written
248 # so someone who knows what they're doing should check this!
249 assert !map[2].include?(current_ways(:used_way).id),
250 "map should not include used way"
251 assert map[2].include?(current_ways(:invisible_way).id),
252 "map should include deleted way"
255 def test_whichways_deleted_toobig
256 bbox = [-0.1, -0.1, 1.1, 1.1]
257 amf_content "whichways_deleted", "/1", bbox
259 assert_response :success
262 map = amf_result "/1"
263 assert_deleted_boundary_error map, " The server said: The maximum bbox size is 0.25, and your request was too large. Either request a smaller area, or use planet.osm"
267 id = current_relations(:visible_relation).id
268 amf_content "getrelation", "/1", [id]
270 assert_response :success
272 rel = amf_result("/1")
273 assert_equal rel[0], 0
274 assert_equal rel[2], id
277 def test_getrelation_invisible
278 id = current_relations(:invisible_relation).id
279 amf_content "getrelation", "/1", [id]
281 assert_response :success
283 rel = amf_result("/1")
284 assert_equal rel[0], -4
285 assert_equal rel[1], "relation"
286 assert_equal rel[2], id
287 assert(rel[3].nil?) && rel[4].nil?
290 def test_getrelation_nonexistent
292 amf_content "getrelation", "/1", [id]
294 assert_response :success
296 rel = amf_result("/1")
297 assert_equal rel[0], -4
298 assert_equal rel[1], "relation"
299 assert_equal rel[2], id
300 assert(rel[3].nil?) && rel[4].nil?
304 # try to get the last visible version (specified by <0) (should be current version)
305 latest = current_ways(:way_with_versions)
306 # NOTE: looks from the API changes that this now expects a timestamp
307 # instead of a version number...
308 # try to get version 1
309 v1 = ways(:way_with_versions_v1)
311 v1.way_id => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
313 amf_content "getway_old", "/1", [id, t]
315 assert_response :success
317 returned_way = amf_result("/1")
318 assert_equal 0, returned_way[0]
319 assert_equal id, returned_way[2]
320 # API returns the *latest* version, even for old ways...
321 assert_equal latest.version, returned_way[5]
326 # test that the server doesn't fall over when rubbish is passed
327 # into the method args.
328 def test_getway_old_invalid
329 way_id = current_ways(:way_with_versions).id
331 way_id => "not a date",
332 way_id => "2009-03-25 00:00:00", # <- wrong format
333 way_id => "0 Jan 2009 00:00:00", # <- invalid date
334 -1 => "1 Jan 2009 00:00:00" # <- invalid ID
336 amf_content "getway_old", "/1", [id, t]
338 assert_response :success
340 returned_way = amf_result("/1")
341 assert_equal -1, returned_way[0]
342 assert returned_way[3].nil?
343 assert returned_way[4].nil?
344 assert returned_way[5].nil?
348 def test_getway_old_nonexistent
349 # try to get the last version+10 (shoudn't exist)
350 v1 = ways(:way_with_versions_v1)
351 # try to get last visible version of non-existent way
352 # try to get specific version of non-existent way
354 [0, "1 Jan 1970, 00:00:00"],
355 [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
357 amf_content "getway_old", "/1", [id, t]
359 assert_response :success
361 returned_way = amf_result("/1")
362 assert_equal -1, returned_way[0]
363 assert returned_way[3].nil?
364 assert returned_way[4].nil?
365 assert returned_way[5].nil?
369 def test_getway_history
370 latest = current_ways(:way_with_versions)
371 oldest = ways(:way_with_versions_v1)
373 amf_content "getway_history", "/1", [latest.id]
375 assert_response :success
377 history = amf_result("/1")
379 # ['way',wayid,history]
380 assert_equal "way", history[0]
381 assert_equal latest.id, history[1]
382 # We use dates rather than version numbers here, because you might
383 # have moved a node within a way (i.e. way version not incremented).
384 # The timestamp is +1 because we say "give me the revision of 15:33:02",
385 # but that might actually include changes at 15:33:02.457.
386 assert_equal (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
387 assert_equal (oldest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
390 def test_getway_history_nonexistent
391 amf_content "getway_history", "/1", [0]
393 assert_response :success
395 history = amf_result("/1")
397 # ['way',wayid,history]
398 assert_equal history[0], "way"
399 assert_equal history[1], 0
400 assert history[2].empty?
403 def test_getnode_history
404 latest = current_nodes(:node_with_versions)
405 amf_content "getnode_history", "/1", [latest.id]
407 assert_response :success
409 history = amf_result("/1")
411 # ['node',nodeid,history]
412 # note that (as per getway_history) we actually round up
414 assert_equal history[0], "node",
415 'first element should be "node"'
416 assert_equal history[1], latest.id,
417 "second element should be the input node ID"
418 assert_equal history[2].first[0],
419 (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
420 "first element in third element (array) should be the latest version"
421 assert_equal history[2].last[0],
422 (nodes(:node_with_versions_v1).timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
423 "last element in third element (array) should be the initial version"
426 def test_getnode_history_nonexistent
427 amf_content "getnode_history", "/1", [0]
429 assert_response :success
431 history = amf_result("/1")
433 # ['node',nodeid,history]
434 assert_equal history[0], "node"
435 assert_equal history[1], 0
436 assert history[2].empty?
439 def test_findgpx_bad_user
440 amf_content "findgpx", "/1", [1, "test@example.com:wrong"]
442 assert_response :success
444 result = amf_result("/1")
446 assert_equal 2, result.length
447 assert_equal -1, result[0]
448 assert_match /must be logged in/, result[1]
450 amf_content "findgpx", "/1", [1, "blocked@openstreetmap.org:test"]
452 assert_response :success
454 result = amf_result("/1")
456 assert_equal 2, result.length
457 assert_equal -1, result[0]
458 assert_match /access to the API has been blocked/, result[1]
461 def test_findgpx_by_id
462 trace = gpx_files(:anon_trace_file)
464 amf_content "findgpx", "/1", [trace.id, "test@example.com:test"]
466 assert_response :success
468 result = amf_result("/1")
470 assert_equal 3, result.length
471 assert_equal 0, result[0]
472 assert_equal "", result[1]
474 assert_equal 1, traces.length
475 assert_equal 3, traces[0].length
476 assert_equal trace.id, traces[0][0]
477 assert_equal trace.name, traces[0][1]
478 assert_equal trace.description, traces[0][2]
481 def test_findgpx_by_name
482 amf_content "findgpx", "/1", ["Trace", "test@example.com:test"]
484 assert_response :success
486 result = amf_result("/1")
488 # find by name fails as it uses mysql text search syntax...
489 assert_equal 2, result.length
490 assert_equal -2, result[0]
493 def test_findrelations_by_id
494 relation = current_relations(:relation_with_versions)
496 amf_content "findrelations", "/1", [relation.id]
498 assert_response :success
500 result = amf_result("/1")
502 assert_equal 1, result.length
503 assert_equal 4, result[0].length
504 assert_equal relation.id, result[0][0]
505 assert_equal relation.tags, result[0][1]
506 assert_equal relation.members, result[0][2]
507 assert_equal relation.version, result[0][3]
509 amf_content "findrelations", "/1", [999999]
511 assert_response :success
513 result = amf_result("/1")
515 assert_equal 0, result.length
518 def test_findrelations_by_tags
519 visible_relation = current_relations(:visible_relation)
520 used_relation = current_relations(:used_relation)
522 amf_content "findrelations", "/1", ["yes"]
524 assert_response :success
526 result = amf_result("/1").sort
528 assert_equal 2, result.length
529 assert_equal 4, result[0].length
530 assert_equal visible_relation.id, result[0][0]
531 assert_equal visible_relation.tags, result[0][1]
532 assert_equal visible_relation.members, result[0][2]
533 assert_equal visible_relation.version, result[0][3]
534 assert_equal 4, result[1].length
535 assert_equal used_relation.id, result[1][0]
536 assert_equal used_relation.tags, result[1][1]
537 assert_equal used_relation.members, result[1][2]
538 assert_equal used_relation.version, result[1][3]
540 amf_content "findrelations", "/1", ["no"]
542 assert_response :success
544 result = amf_result("/1").sort
546 assert_equal 0, result.length
549 def test_getpoi_without_timestamp
550 node = current_nodes(:node_with_versions)
552 amf_content "getpoi", "/1", [node.id, ""]
554 assert_response :success
556 result = amf_result("/1")
558 assert_equal 7, result.length
559 assert_equal 0, result[0]
560 assert_equal "", result[1]
561 assert_equal node.id, result[2]
562 assert_equal node.lon, result[3]
563 assert_equal node.lat, result[4]
564 assert_equal node.tags, result[5]
565 assert_equal node.version, result[6]
567 amf_content "getpoi", "/1", [999999, ""]
569 assert_response :success
571 result = amf_result("/1")
573 assert_equal 3, result.length
574 assert_equal -4, result[0]
575 assert_equal "node", result[1]
576 assert_equal 999999, result[2]
579 def test_getpoi_with_timestamp
580 node = nodes(:node_with_versions_v2)
581 current_node = current_nodes(:node_with_versions)
583 amf_content "getpoi", "/1", [node.node_id, node.timestamp.xmlschema]
585 assert_response :success
587 result = amf_result("/1")
589 assert_equal 7, result.length
590 assert_equal 0, result[0]
591 assert_equal "", result[1]
592 assert_equal node.node_id, result[2]
593 assert_equal node.lon, result[3]
594 assert_equal node.lat, result[4]
595 assert_equal node.tags, result[5]
596 assert_equal current_node.version, result[6]
598 amf_content "getpoi", "/1", [node.node_id, "2000-01-01T00:00:00Z"]
600 assert_response :success
602 result = amf_result("/1")
604 assert_equal 3, result.length
605 assert_equal -4, result[0]
606 assert_equal "node", result[1]
607 assert_equal node.node_id, result[2]
609 amf_content "getpoi", "/1", [999999, Time.now.xmlschema]
611 assert_response :success
613 result = amf_result("/1")
615 assert_equal 3, result.length
616 assert_equal -4, result[0]
617 assert_equal "node", result[1]
618 assert_equal 999999, result[2]
621 # ************************************************************
624 # check that we can update a poi
625 def test_putpoi_update_valid
626 nd = current_nodes(:visible_node)
627 cs_id = changesets(:public_user_first_change).id
628 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
630 assert_response :success
632 result = amf_result("/1")
634 assert_equal 5, result.size
635 assert_equal 0, result[0]
636 assert_equal "", result[1]
637 assert_equal nd.id, result[2]
638 assert_equal nd.id, result[3]
639 assert_equal nd.version + 1, result[4]
641 # Now try to update again, with a different lat/lon, using the updated version number
644 amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version + 1, nd.id, lon, lat, nd.tags, nd.visible]
646 assert_response :success
648 result = amf_result("/2")
650 assert_equal 5, result.size
651 assert_equal 0, result[0]
652 assert_equal "", result[1]
653 assert_equal nd.id, result[2]
654 assert_equal nd.id, result[3]
655 assert_equal nd.version + 2, result[4]
658 # Check that we can create a no valid poi
659 # Using similar method for the node controller test
660 def test_putpoi_create_valid
661 # This node has no tags
663 # create a node with random lat/lon
664 lat = rand(100) - 50 + rand
665 lon = rand(100) - 50 + rand
666 # normal user has a changeset open
667 changeset = changesets(:public_user_first_change)
669 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
671 assert_response :success
673 result = amf_result("/1")
675 # check the array returned by the amf
676 assert_equal 5, result.size
677 assert_equal 0, result[0], "expected to get the status ok from the amf"
678 assert_equal 0, result[2], "The old id should be 0"
679 assert result[3] > 0, "The new id should be greater than 0"
680 assert_equal 1, result[4], "The new version should be 1"
682 # Finally check that the node that was saved has saved the data correctly
683 # in both the current and history tables
684 # First check the current table
685 current_node = Node.find(result[3].to_i)
686 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
687 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
688 assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
689 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
690 # Now check the history table
691 historic_nodes = Node.where(:id => result[3])
692 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
693 first_historic_node = historic_nodes.first
694 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
695 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
696 assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
697 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
700 # This node has some tags
702 # create a node with random lat/lon
703 lat = rand(100) - 50 + rand
704 lon = rand(100) - 50 + rand
705 # normal user has a changeset open
706 changeset = changesets(:public_user_first_change)
708 amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
710 assert_response :success
712 result = amf_result("/2")
714 # check the array returned by the amf
715 assert_equal 5, result.size
716 assert_equal 0, result[0], "Expected to get the status ok in the amf"
717 assert_equal 0, result[2], "The old id should be 0"
718 assert result[3] > 0, "The new id should be greater than 0"
719 assert_equal 1, result[4], "The new version should be 1"
721 # Finally check that the node that was saved has saved the data correctly
722 # in both the current and history tables
723 # First check the current table
724 current_node = Node.find(result[3].to_i)
725 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
726 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
727 assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
728 assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
729 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
730 # Now check the history table
731 historic_nodes = Node.where(:id => result[3])
732 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
733 first_historic_node = historic_nodes.first
734 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
735 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
736 assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
737 assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
738 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
741 # try creating a POI with rubbish in the tags
742 def test_putpoi_create_with_control_chars
743 # This node has no tags
745 # create a node with random lat/lon
746 lat = rand(100) - 50 + rand
747 lon = rand(100) - 50 + rand
748 # normal user has a changeset open
749 changeset = changesets(:public_user_first_change)
751 mostly_invalid = (0..31).to_a.map(&:chr).join
752 tags = { "something" => "foo#{mostly_invalid}bar" }
754 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
756 assert_response :success
758 result = amf_result("/1")
760 # check the array returned by the amf
761 assert_equal 5, result.size
762 assert_equal 0, result[0], "Expected to get the status ok in the amf"
763 assert_equal 0, result[2], "The old id should be 0"
764 assert result[3] > 0, "The new id should be greater than 0"
765 assert_equal 1, result[4], "The new version should be 1"
767 # Finally check that the node that was saved has saved the data correctly
768 # in both the current and history tables
769 # First check the current table
770 current_node = Node.find(result[3].to_i)
771 assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
772 assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
773 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
776 # try creating a POI with rubbish in the tags
777 def test_putpoi_create_with_invalid_utf8
778 # This node has no tags
780 # create a node with random lat/lon
781 lat = rand(100) - 50 + rand
782 lon = rand(100) - 50 + rand
783 # normal user has a changeset open
784 changeset = changesets(:public_user_first_change)
787 tags = { "something" => "foo#{invalid}bar" }
789 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
791 assert_response :success
793 result = amf_result("/1")
795 assert_equal 2, result.size
796 assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
797 assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
800 # try deleting a node
801 def test_putpoi_delete_valid
802 nd = current_nodes(:visible_node)
803 cs_id = changesets(:public_user_first_change).id
804 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
806 assert_response :success
808 result = amf_result("/1")
810 assert_equal 5, result.size
811 assert_equal 0, result[0]
812 assert_equal "", result[1]
813 assert_equal nd.id, result[2]
814 assert_equal nd.id, result[3]
815 assert_equal nd.version + 1, result[4]
817 current_node = Node.find(result[3].to_i)
818 assert_equal false, current_node.visible
821 # try deleting a node that is already deleted
822 def test_putpoi_delete_already_deleted
823 nd = current_nodes(:invisible_node)
824 cs_id = changesets(:public_user_first_change).id
825 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
827 assert_response :success
829 result = amf_result("/1")
831 assert_equal 3, result.size
832 assert_equal -4, result[0]
833 assert_equal "node", result[1]
834 assert_equal nd.id, result[2]
837 # try deleting a node that has never existed
838 def test_putpoi_delete_not_found
839 cs_id = changesets(:public_user_first_change).id
840 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, 1, 999999, 0, 0, {}, false]
842 assert_response :success
844 result = amf_result("/1")
846 assert_equal 3, result.size
847 assert_equal -4, result[0]
848 assert_equal "node", result[1]
849 assert_equal 999999, result[2]
852 # try setting an invalid location on a node
853 def test_putpoi_invalid_latlon
854 nd = current_nodes(:visible_node)
855 cs_id = changesets(:public_user_first_change).id
856 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, 200, 100, nd.tags, true]
858 assert_response :success
860 result = amf_result("/1")
862 assert_equal 2, result.size
863 assert_equal -2, result[0]
864 assert_match /Node is not in the world/, result[1]
867 # check that we can update way
868 def test_putway_update_valid
869 way = current_ways(:way_with_multiple_nodes)
870 cs_id = changesets(:public_user_first_change).id
872 amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version, way.id, way.nds, { "test" => "ok" }, [], {}]
874 assert_response :success
876 result = amf_result("/1")
878 assert_equal 8, result.size
879 assert_equal 0, result[0]
880 assert_equal "", result[1]
881 assert_equal way.id, result[2]
882 assert_equal way.id, result[3]
883 assert_equal({}, result[4])
884 assert_equal way.version + 1, result[5]
885 assert_equal({}, result[6])
886 assert_equal({}, result[7])
888 new_way = Way.find(way.id)
889 assert_equal way.version + 1, new_way.version
890 assert_equal way.nds, new_way.nds
891 assert_equal({ "test" => "ok" }, new_way.tags)
893 amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version + 1, way.id, [4, 6, 15, 1], way.tags, [], {}]
895 assert_response :success
897 result = amf_result("/1")
899 assert_equal 8, result.size
900 assert_equal 0, result[0]
901 assert_equal "", result[1]
902 assert_equal way.id, result[2]
903 assert_equal way.id, result[3]
904 assert_equal({}, result[4])
905 assert_equal way.version + 2, result[5]
906 assert_equal({}, result[6])
907 assert_equal({}, result[7])
909 new_way = Way.find(way.id)
910 assert_equal way.version + 2, new_way.version
911 assert_equal [4, 6, 15, 1], new_way.nds
912 assert_equal way.tags, new_way.tags
914 amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version + 2, way.id, [4, -1, 6, 15], way.tags, [[4.56, 12.34, -1, 0, { "test" => "new" }], [12.34, 4.56, 6, 1, { "test" => "ok" }]], { 1 => 1 }]
916 assert_response :success
918 result = amf_result("/1")
919 new_node_id = result[4]["-1"].to_i
921 assert_equal 8, result.size
922 assert_equal 0, result[0]
923 assert_equal "", result[1]
924 assert_equal way.id, result[2]
925 assert_equal way.id, result[3]
926 assert_equal({ "-1" => new_node_id }, result[4])
927 assert_equal way.version + 3, result[5]
928 assert_equal({ new_node_id.to_s => 1, "6" => 2 }, result[6])
929 assert_equal({ "1" => 1 }, result[7])
931 new_way = Way.find(way.id)
932 assert_equal way.version + 3, new_way.version
933 assert_equal [4, new_node_id, 6, 15], new_way.nds
934 assert_equal way.tags, new_way.tags
936 new_node = Node.find(new_node_id)
937 assert_equal 1, new_node.version
938 assert_equal true, new_node.visible
939 assert_equal 4.56, new_node.lon
940 assert_equal 12.34, new_node.lat
941 assert_equal({ "test" => "new" }, new_node.tags)
943 changed_node = Node.find(6)
944 assert_equal 2, changed_node.version
945 assert_equal true, changed_node.visible
946 assert_equal 12.34, changed_node.lon
947 assert_equal 4.56, changed_node.lat
948 assert_equal({ "test" => "ok" }, changed_node.tags)
950 deleted_node = Node.find(1)
951 assert_equal 2, deleted_node.version
952 assert_equal false, deleted_node.visible
955 def test_startchangeset_invalid_xmlchar_comment
957 comment = "foo#{invalid}bar"
959 amf_content "startchangeset", "/1", ["test@example.com:test", {}, nil, comment, 1]
961 assert_response :success
963 result = amf_result("/1")
965 assert_equal 3, result.size, result.inspect
966 assert_equal 0, result[0]
967 new_cs_id = result[2].to_i
969 cs = Changeset.find(new_cs_id)
970 assert_equal "foobar", cs.tags["comment"]
975 # ************************************************************
976 # AMF Helper functions
978 # Get the result record for the specified ID
979 # It's an assertion FAIL if the record does not exist
981 assert @amf_result.key?("#{ref}/onResult")
982 @amf_result["#{ref}/onResult"]
985 # Encode the AMF message to invoke "target" with parameters as
986 # the passed data. The ref is used to retrieve the results.
987 def amf_content(target, ref, data)
990 c.write 0.chr + 0.chr # version 0
991 c.write 0.chr + 0.chr # n headers
992 c.write a.chr + b.chr # n bodies
993 c.write AMF.encodestring(target)
994 c.write AMF.encodestring(ref)
995 c.write [-1].pack("N")
996 c.write AMF.encodevalue(data)
998 @request.env["RAW_POST_DATA"] = c.string
1001 # Parses the @response object as an AMF messsage.
1002 # The result is a hash of message_ref => data.
1003 # The attribute @amf_result is initialised to this hash.
1004 def amf_parse_response
1005 req = StringIO.new(@response.body)
1007 req.read(2) # version
1009 # parse through any headers
1010 headers = AMF.getint(req) # Read number of headers
1011 headers.times do # Read each header
1012 AMF.getstring(req) # |
1013 req.getc # | skip boolean
1014 AMF.getvalue(req) # |
1017 # parse through responses
1019 bodies = AMF.getint(req) # Read number of bodies
1020 bodies.times do # Read each body
1021 message = AMF.getstring(req) # | get message name
1022 AMF.getstring(req) # | get index in response sequence
1023 AMF.getlong(req) # | get total size in bytes
1024 args = AMF.getvalue(req) # | get response (probably an array)
1025 results[message] = args
1027 @amf_result = results
1032 # given an array of bounding boxes (each an array of 4 floats), call the
1033 # AMF "whichways" controller for each and pass the result back to the
1034 # caller's block for assertion testing.
1035 def check_bboxes_are_bad(bboxes)
1036 bboxes.each do |bbox|
1037 amf_content "whichways", "/1", bbox
1039 assert_response :success
1042 # pass the response back to the caller's block to be tested
1043 # against what the caller expected.
1044 map = amf_result "/1"
1049 # this should be what AMF controller returns when the bbox of a
1050 # whichways request is invalid or too large.
1051 def assert_boundary_error(map, msg = nil, error_hint = nil)
1052 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1053 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1056 # this should be what AMF controller returns when the bbox of a
1057 # whichways_deleted request is invalid or too large.
1058 def assert_deleted_boundary_error(map, msg = nil, error_hint = nil)
1059 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1060 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"