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 amf_content "getpresets", "/1", ["test@example.com:test", ""]
24 assert_response :success
26 presets = amf_result("/1")
28 assert_equal 15, presets.length
29 assert_equal POTLATCH_PRESETS[0], presets[0]
30 assert_equal POTLATCH_PRESETS[1], presets[1]
31 assert_equal POTLATCH_PRESETS[2], presets[2]
32 assert_equal POTLATCH_PRESETS[3], presets[3]
33 assert_equal POTLATCH_PRESETS[4], presets[4]
34 assert_equal POTLATCH_PRESETS[5], presets[5]
35 assert_equal POTLATCH_PRESETS[6], presets[6]
36 assert_equal POTLATCH_PRESETS[7], presets[7]
37 assert_equal POTLATCH_PRESETS[8], presets[8]
38 assert_equal POTLATCH_PRESETS[9], presets[9]
39 assert_equal POTLATCH_PRESETS[10], presets[10]
40 assert_equal POTLATCH_PRESETS[12], presets[12]
45 id = current_ways(:visible_way).id
46 amf_content "getway", "/1", [id]
48 assert_response :success
50 way = amf_result("/1")
51 assert_equal 0, way[0]
52 assert_equal "", way[1]
53 assert_equal id, way[2]
54 assert_equal 1, way[3].length
55 assert_equal 3, way[3][0][2]
56 assert_equal 1, way[5]
57 assert_equal 2, way[6]
60 def test_getway_invisible
61 # check an invisible way
62 id = current_ways(:invisible_way).id
63 amf_content "getway", "/1", [id]
65 assert_response :success
67 way = amf_result("/1")
68 assert_equal -4, way[0], -4
69 assert_equal "way", way[1]
70 assert_equal id, way[2]
71 assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
74 def test_getway_with_versions
75 # check a way with multiple versions
76 id = current_ways(:way_with_versions).id
77 amf_content "getway", "/1", [id]
79 assert_response :success
81 way = amf_result("/1")
82 assert_equal 0, way[0]
83 assert_equal "", way[1]
84 assert_equal id, way[2]
85 assert_equal 1, way[3].length
86 assert_equal 15, way[3][0][2]
87 assert_equal 4, way[5]
88 assert_equal 2, way[6]
91 def test_getway_with_duplicate_nodes
92 # check a way with duplicate nodes
93 id = current_ways(:way_with_duplicate_nodes).id
94 amf_content "getway", "/1", [id]
96 assert_response :success
98 way = amf_result("/1")
99 assert_equal 0, way[0]
100 assert_equal "", way[1]
101 assert_equal id, way[2]
102 assert_equal 2, way[3].length
103 assert_equal 4, way[3][0][2]
104 assert_equal 4, way[3][1][2]
105 assert_equal 1, way[5]
106 assert_equal 2, way[6]
109 def test_getway_with_multiple_nodes
110 # check a way with multiple nodes
111 id = current_ways(:way_with_multiple_nodes).id
112 amf_content "getway", "/1", [id]
114 assert_response :success
116 way = amf_result("/1")
117 assert_equal 0, way[0]
118 assert_equal "", way[1]
119 assert_equal id, way[2]
120 assert_equal 3, way[3].length
121 assert_equal 4, way[3][0][2]
122 assert_equal 15, way[3][1][2]
123 assert_equal 6, way[3][2][2]
124 assert_equal 2, way[5]
125 assert_equal 2, way[6]
128 def test_getway_nonexistent
129 # check chat a non-existent way is not returned
130 amf_content "getway", "/1", [0]
132 assert_response :success
134 way = amf_result("/1")
135 assert_equal -4, way[0]
136 assert_equal "way", way[1]
137 assert_equal 0, way[2]
138 assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
142 node = current_nodes(:used_node_1)
143 minlon = node.lon - 0.1
144 minlat = node.lat - 0.1
145 maxlon = node.lon + 0.1
146 maxlat = node.lat + 0.1
147 amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
149 assert_response :success
152 # check contents of message
153 map = amf_result "/1"
154 assert_equal 0, map[0], "map error code should be 0"
155 assert_equal "", map[1], "map error text should be empty"
157 # check the formatting of the message
158 assert_equal 5, map.length, "map should have length 5"
159 assert_equal Array, map[2].class, 'map "ways" element should be an array'
160 assert_equal Array, map[3].class, 'map "nodes" element should be an array'
161 assert_equal Array, map[4].class, 'map "relations" element should be an array'
163 assert_equal 2, w.length, "way should be (id, version) pair"
164 assert w[0] == w[0].floor, "way ID should be an integer"
165 assert w[1] == w[1].floor, "way version should be an integer"
169 assert_equal 5, w.length, "node should be (id, lat, lon, [tags], version) tuple"
170 assert n[0] == n[0].floor, "node ID should be an integer"
171 assert n[1] >= minlat - 0.01, "node lat should be greater than min"
172 assert n[1] <= maxlat - 0.01, "node lat should be less than max"
173 assert n[2] >= minlon - 0.01, "node lon should be greater than min"
174 assert n[2] <= maxlon - 0.01, "node lon should be less than max"
175 assert_equal Array, a[3].class, "node tags should be array"
176 assert n[4] == n[4].floor, "node version should be an integer"
180 assert_equal 2, r.length, "relation should be (id, version) pair"
181 assert r[0] == r[0].floor, "relation ID should be an integer"
182 assert r[1] == r[1].floor, "relation version should be an integer"
185 # TODO: looks like amf_controller changed since this test was written
186 # so someone who knows what they're doing should check this!
187 ways = map[2].collect { |x| x[0] }
188 assert ways.include?(current_ways(:used_way).id),
189 "map should include used way"
190 assert !ways.include?(current_ways(:invisible_way).id),
191 "map should not include deleted way"
195 # checks that too-large a bounding box will not be served.
196 def test_whichways_toobig
197 bbox = [-0.1, -0.1, 1.1, 1.1]
198 check_bboxes_are_bad [bbox] do |map, _bbox|
199 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"
204 # checks that an invalid bounding box will not be served. in this case
205 # one with max < min latitudes.
207 # NOTE: the controller expands the bbox by 0.01 in each direction!
208 def test_whichways_badlat
209 bboxes = [[0, 0.1, 0.1, 0], [-0.1, 80, 0.1, 70], [0.24, 54.35, 0.25, 54.33]]
210 check_bboxes_are_bad bboxes do |map, bbox|
211 assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
216 # same as test_whichways_badlat, but for longitudes
218 # NOTE: the controller expands the bbox by 0.01 in each direction!
219 def test_whichways_badlon
220 bboxes = [[80, -0.1, 70, 0.1], [54.35, 0.24, 54.33, 0.25]]
221 check_bboxes_are_bad bboxes do |map, bbox|
222 assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
226 def test_whichways_deleted
227 node = current_nodes(:used_node_1)
228 minlon = node.lon - 0.1
229 minlat = node.lat - 0.1
230 maxlon = node.lon + 0.1
231 maxlat = node.lat + 0.1
232 amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
234 assert_response :success
237 # check contents of message
238 map = amf_result "/1"
239 assert_equal 0, map[0], "first map element should be 0"
240 assert_equal "", map[1], "second map element should be an empty string"
241 assert_equal Array, map[2].class, "third map element should be an array"
242 # TODO: looks like amf_controller changed since this test was written
243 # so someone who knows what they're doing should check this!
244 assert !map[2].include?(current_ways(:used_way).id),
245 "map should not include used way"
246 assert map[2].include?(current_ways(:invisible_way).id),
247 "map should include deleted way"
250 def test_whichways_deleted_toobig
251 bbox = [-0.1, -0.1, 1.1, 1.1]
252 amf_content "whichways_deleted", "/1", bbox
254 assert_response :success
257 map = amf_result "/1"
258 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"
262 id = current_relations(:visible_relation).id
263 amf_content "getrelation", "/1", [id]
265 assert_response :success
267 rel = amf_result("/1")
268 assert_equal rel[0], 0
269 assert_equal rel[2], id
272 def test_getrelation_invisible
273 id = current_relations(:invisible_relation).id
274 amf_content "getrelation", "/1", [id]
276 assert_response :success
278 rel = amf_result("/1")
279 assert_equal rel[0], -4
280 assert_equal rel[1], "relation"
281 assert_equal rel[2], id
282 assert(rel[3].nil?) && rel[4].nil?
285 def test_getrelation_nonexistent
287 amf_content "getrelation", "/1", [id]
289 assert_response :success
291 rel = amf_result("/1")
292 assert_equal rel[0], -4
293 assert_equal rel[1], "relation"
294 assert_equal rel[2], id
295 assert(rel[3].nil?) && rel[4].nil?
299 # try to get the last visible version (specified by <0) (should be current version)
300 latest = current_ways(:way_with_versions)
301 # NOTE: looks from the API changes that this now expects a timestamp
302 # instead of a version number...
303 # try to get version 1
304 v1 = ways(:way_with_versions_v1)
306 v1.way_id => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
308 amf_content "getway_old", "/1", [id, t]
310 assert_response :success
312 returned_way = amf_result("/1")
313 assert_equal 0, returned_way[0]
314 assert_equal id, returned_way[2]
315 # API returns the *latest* version, even for old ways...
316 assert_equal latest.version, returned_way[5]
321 # test that the server doesn't fall over when rubbish is passed
322 # into the method args.
323 def test_getway_old_invalid
324 way_id = current_ways(:way_with_versions).id
326 way_id => "not a date",
327 way_id => "2009-03-25 00:00:00", # <- wrong format
328 way_id => "0 Jan 2009 00:00:00", # <- invalid date
329 -1 => "1 Jan 2009 00:00:00" # <- invalid ID
331 amf_content "getway_old", "/1", [id, t]
333 assert_response :success
335 returned_way = amf_result("/1")
336 assert_equal -1, returned_way[0]
337 assert returned_way[3].nil?
338 assert returned_way[4].nil?
339 assert returned_way[5].nil?
343 def test_getway_old_nonexistent
344 # try to get the last version+10 (shoudn't exist)
345 v1 = ways(:way_with_versions_v1)
346 # try to get last visible version of non-existent way
347 # try to get specific version of non-existent way
349 [0, "1 Jan 1970, 00:00:00"],
350 [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
352 amf_content "getway_old", "/1", [id, t]
354 assert_response :success
356 returned_way = amf_result("/1")
357 assert_equal -1, returned_way[0]
358 assert returned_way[3].nil?
359 assert returned_way[4].nil?
360 assert returned_way[5].nil?
364 def test_getway_history
365 latest = current_ways(:way_with_versions)
366 oldest = ways(:way_with_versions_v1)
368 amf_content "getway_history", "/1", [latest.id]
370 assert_response :success
372 history = amf_result("/1")
374 # ['way',wayid,history]
375 assert_equal "way", history[0]
376 assert_equal latest.id, history[1]
377 # We use dates rather than version numbers here, because you might
378 # have moved a node within a way (i.e. way version not incremented).
379 # The timestamp is +1 because we say "give me the revision of 15:33:02",
380 # but that might actually include changes at 15:33:02.457.
381 assert_equal (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
382 assert_equal (oldest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
385 def test_getway_history_nonexistent
386 amf_content "getway_history", "/1", [0]
388 assert_response :success
390 history = amf_result("/1")
392 # ['way',wayid,history]
393 assert_equal history[0], "way"
394 assert_equal history[1], 0
395 assert history[2].empty?
398 def test_getnode_history
399 latest = current_nodes(:node_with_versions)
400 amf_content "getnode_history", "/1", [latest.id]
402 assert_response :success
404 history = amf_result("/1")
406 # ['node',nodeid,history]
407 # note that (as per getway_history) we actually round up
409 assert_equal history[0], "node",
410 'first element should be "node"'
411 assert_equal history[1], latest.id,
412 "second element should be the input node ID"
413 assert_equal history[2].first[0],
414 (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
415 "first element in third element (array) should be the latest version"
416 assert_equal history[2].last[0],
417 (nodes(:node_with_versions_v1).timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
418 "last element in third element (array) should be the initial version"
421 def test_getnode_history_nonexistent
422 amf_content "getnode_history", "/1", [0]
424 assert_response :success
426 history = amf_result("/1")
428 # ['node',nodeid,history]
429 assert_equal history[0], "node"
430 assert_equal history[1], 0
431 assert history[2].empty?
434 def test_findgpx_bad_user
435 amf_content "findgpx", "/1", [1, "test@example.com:wrong"]
437 assert_response :success
439 result = amf_result("/1")
441 assert_equal 2, result.length
442 assert_equal -1, result[0]
443 assert_match /must be logged in/, result[1]
445 amf_content "findgpx", "/1", [1, "blocked@openstreetmap.org:test"]
447 assert_response :success
449 result = amf_result("/1")
451 assert_equal 2, result.length
452 assert_equal -1, result[0]
453 assert_match /access to the API has been blocked/, result[1]
456 def test_findgpx_by_id
457 trace = gpx_files(:anon_trace_file)
459 amf_content "findgpx", "/1", [trace.id, "test@example.com:test"]
461 assert_response :success
463 result = amf_result("/1")
465 assert_equal 3, result.length
466 assert_equal 0, result[0]
467 assert_equal "", result[1]
469 assert_equal 1, traces.length
470 assert_equal 3, traces[0].length
471 assert_equal trace.id, traces[0][0]
472 assert_equal trace.name, traces[0][1]
473 assert_equal trace.description, traces[0][2]
476 def test_findgpx_by_name
477 amf_content "findgpx", "/1", ["Trace", "test@example.com:test"]
479 assert_response :success
481 result = amf_result("/1")
483 # find by name fails as it uses mysql text search syntax...
484 assert_equal 2, result.length
485 assert_equal -2, result[0]
488 def test_findrelations_by_id
489 relation = current_relations(:relation_with_versions)
491 amf_content "findrelations", "/1", [relation.id]
493 assert_response :success
495 result = amf_result("/1")
497 assert_equal 1, result.length
498 assert_equal 4, result[0].length
499 assert_equal relation.id, result[0][0]
500 assert_equal relation.tags, result[0][1]
501 assert_equal relation.members, result[0][2]
502 assert_equal relation.version, result[0][3]
504 amf_content "findrelations", "/1", [999999]
506 assert_response :success
508 result = amf_result("/1")
510 assert_equal 0, result.length
513 def test_findrelations_by_tags
514 visible_relation = current_relations(:visible_relation)
515 used_relation = current_relations(:used_relation)
517 amf_content "findrelations", "/1", ["yes"]
519 assert_response :success
521 result = amf_result("/1").sort
523 assert_equal 2, result.length
524 assert_equal 4, result[0].length
525 assert_equal visible_relation.id, result[0][0]
526 assert_equal visible_relation.tags, result[0][1]
527 assert_equal visible_relation.members, result[0][2]
528 assert_equal visible_relation.version, result[0][3]
529 assert_equal 4, result[1].length
530 assert_equal used_relation.id, result[1][0]
531 assert_equal used_relation.tags, result[1][1]
532 assert_equal used_relation.members, result[1][2]
533 assert_equal used_relation.version, result[1][3]
535 amf_content "findrelations", "/1", ["no"]
537 assert_response :success
539 result = amf_result("/1").sort
541 assert_equal 0, result.length
544 def test_getpoi_without_timestamp
545 node = current_nodes(:node_with_versions)
547 amf_content "getpoi", "/1", [node.id, ""]
549 assert_response :success
551 result = amf_result("/1")
553 assert_equal 7, result.length
554 assert_equal 0, result[0]
555 assert_equal "", result[1]
556 assert_equal node.id, result[2]
557 assert_equal node.lon, result[3]
558 assert_equal node.lat, result[4]
559 assert_equal node.tags, result[5]
560 assert_equal node.version, result[6]
562 amf_content "getpoi", "/1", [999999, ""]
564 assert_response :success
566 result = amf_result("/1")
568 assert_equal 3, result.length
569 assert_equal -4, result[0]
570 assert_equal "node", result[1]
571 assert_equal 999999, result[2]
574 def test_getpoi_with_timestamp
575 node = nodes(:node_with_versions_v2)
576 current_node = current_nodes(:node_with_versions)
578 amf_content "getpoi", "/1", [node.node_id, node.timestamp.xmlschema]
580 assert_response :success
582 result = amf_result("/1")
584 assert_equal 7, result.length
585 assert_equal 0, result[0]
586 assert_equal "", result[1]
587 assert_equal node.node_id, result[2]
588 assert_equal node.lon, result[3]
589 assert_equal node.lat, result[4]
590 assert_equal node.tags, result[5]
591 assert_equal current_node.version, result[6]
593 amf_content "getpoi", "/1", [node.node_id, "2000-01-01T00:00:00Z"]
595 assert_response :success
597 result = amf_result("/1")
599 assert_equal 3, result.length
600 assert_equal -4, result[0]
601 assert_equal "node", result[1]
602 assert_equal node.node_id, result[2]
604 amf_content "getpoi", "/1", [999999, Time.now.xmlschema]
606 assert_response :success
608 result = amf_result("/1")
610 assert_equal 3, result.length
611 assert_equal -4, result[0]
612 assert_equal "node", result[1]
613 assert_equal 999999, result[2]
616 # ************************************************************
619 # check that we can update a poi
620 def test_putpoi_update_valid
621 nd = current_nodes(:visible_node)
622 cs_id = changesets(:public_user_first_change).id
623 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
625 assert_response :success
627 result = amf_result("/1")
629 assert_equal 5, result.size
630 assert_equal 0, result[0]
631 assert_equal "", result[1]
632 assert_equal nd.id, result[2]
633 assert_equal nd.id, result[3]
634 assert_equal nd.version + 1, result[4]
636 # Now try to update again, with a different lat/lon, using the updated version number
639 amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version + 1, nd.id, lon, lat, nd.tags, nd.visible]
641 assert_response :success
643 result = amf_result("/2")
645 assert_equal 5, result.size
646 assert_equal 0, result[0]
647 assert_equal "", result[1]
648 assert_equal nd.id, result[2]
649 assert_equal nd.id, result[3]
650 assert_equal nd.version + 2, result[4]
653 # Check that we can create a no valid poi
654 # Using similar method for the node controller test
655 def test_putpoi_create_valid
656 # This node has no tags
658 # create a node with random lat/lon
659 lat = rand(100) - 50 + rand
660 lon = rand(100) - 50 + rand
661 # normal user has a changeset open
662 changeset = changesets(:public_user_first_change)
664 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
666 assert_response :success
668 result = amf_result("/1")
670 # check the array returned by the amf
671 assert_equal 5, result.size
672 assert_equal 0, result[0], "expected to get the status ok from the amf"
673 assert_equal 0, result[2], "The old id should be 0"
674 assert result[3] > 0, "The new id should be greater than 0"
675 assert_equal 1, result[4], "The new version should be 1"
677 # Finally check that the node that was saved has saved the data correctly
678 # in both the current and history tables
679 # First check the current table
680 current_node = Node.find(result[3].to_i)
681 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
682 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
683 assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
684 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
685 # Now check the history table
686 historic_nodes = Node.where(:id => result[3])
687 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
688 first_historic_node = historic_nodes.first
689 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
690 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
691 assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
692 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
695 # This node has some tags
697 # create a node with random lat/lon
698 lat = rand(100) - 50 + rand
699 lon = rand(100) - 50 + rand
700 # normal user has a changeset open
701 changeset = changesets(:public_user_first_change)
703 amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
705 assert_response :success
707 result = amf_result("/2")
709 # check the array returned by the amf
710 assert_equal 5, result.size
711 assert_equal 0, result[0], "Expected to get the status ok in the amf"
712 assert_equal 0, result[2], "The old id should be 0"
713 assert result[3] > 0, "The new id should be greater than 0"
714 assert_equal 1, result[4], "The new version should be 1"
716 # Finally check that the node that was saved has saved the data correctly
717 # in both the current and history tables
718 # First check the current table
719 current_node = Node.find(result[3].to_i)
720 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
721 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
722 assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
723 assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
724 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
725 # Now check the history table
726 historic_nodes = Node.where(:id => result[3])
727 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
728 first_historic_node = historic_nodes.first
729 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
730 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
731 assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
732 assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
733 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
736 # try creating a POI with rubbish in the tags
737 def test_putpoi_create_with_control_chars
738 # This node has no tags
740 # create a node with random lat/lon
741 lat = rand(100) - 50 + rand
742 lon = rand(100) - 50 + rand
743 # normal user has a changeset open
744 changeset = changesets(:public_user_first_change)
746 mostly_invalid = (0..31).to_a.map(&:chr).join
747 tags = { "something" => "foo#{mostly_invalid}bar" }
749 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
751 assert_response :success
753 result = amf_result("/1")
755 # check the array returned by the amf
756 assert_equal 5, result.size
757 assert_equal 0, result[0], "Expected to get the status ok in the amf"
758 assert_equal 0, result[2], "The old id should be 0"
759 assert result[3] > 0, "The new id should be greater than 0"
760 assert_equal 1, result[4], "The new version should be 1"
762 # Finally check that the node that was saved has saved the data correctly
763 # in both the current and history tables
764 # First check the current table
765 current_node = Node.find(result[3].to_i)
766 assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
767 assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
768 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
771 # try creating a POI with rubbish in the tags
772 def test_putpoi_create_with_invalid_utf8
773 # This node has no tags
775 # create a node with random lat/lon
776 lat = rand(100) - 50 + rand
777 lon = rand(100) - 50 + rand
778 # normal user has a changeset open
779 changeset = changesets(:public_user_first_change)
782 tags = { "something" => "foo#{invalid}bar" }
784 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
786 assert_response :success
788 result = amf_result("/1")
790 assert_equal 2, result.size
791 assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
792 assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
795 # try deleting a node
796 def test_putpoi_delete_valid
797 nd = current_nodes(:visible_node)
798 cs_id = changesets(:public_user_first_change).id
799 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
801 assert_response :success
803 result = amf_result("/1")
805 assert_equal 5, result.size
806 assert_equal 0, result[0]
807 assert_equal "", result[1]
808 assert_equal nd.id, result[2]
809 assert_equal nd.id, result[3]
810 assert_equal nd.version + 1, result[4]
812 current_node = Node.find(result[3].to_i)
813 assert_equal false, current_node.visible
816 # try deleting a node that is already deleted
817 def test_putpoi_delete_already_deleted
818 nd = current_nodes(:invisible_node)
819 cs_id = changesets(:public_user_first_change).id
820 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
822 assert_response :success
824 result = amf_result("/1")
826 assert_equal 3, result.size
827 assert_equal -4, result[0]
828 assert_equal "node", result[1]
829 assert_equal nd.id, result[2]
832 # try deleting a node that has never existed
833 def test_putpoi_delete_not_found
834 cs_id = changesets(:public_user_first_change).id
835 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, 1, 999999, 0, 0, {}, false]
837 assert_response :success
839 result = amf_result("/1")
841 assert_equal 3, result.size
842 assert_equal -4, result[0]
843 assert_equal "node", result[1]
844 assert_equal 999999, result[2]
847 # try setting an invalid location on a node
848 def test_putpoi_invalid_latlon
849 nd = current_nodes(:visible_node)
850 cs_id = changesets(:public_user_first_change).id
851 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, 200, 100, nd.tags, true]
853 assert_response :success
855 result = amf_result("/1")
857 assert_equal 2, result.size
858 assert_equal -2, result[0]
859 assert_match /Node is not in the world/, result[1]
862 # check that we can update way
863 def test_putway_update_valid
864 way = current_ways(:way_with_multiple_nodes)
865 cs_id = changesets(:public_user_first_change).id
866 amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version, way.id, way.nds, { "test" => "ok" }, [], {}]
868 assert_response :success
870 result = amf_result("/1")
872 assert_equal 8, result.size
873 assert_equal 0, result[0]
874 assert_equal "", result[1]
875 assert_equal way.id, result[2]
876 assert_equal way.id, result[3]
877 assert_equal({}, result[4])
878 assert_equal way.version + 1, result[5]
879 assert_equal({}, result[6])
880 assert_equal({}, result[7])
882 new_way = Way.find(way.id)
883 assert_equal way.version + 1, new_way.version
884 assert_equal way.nds, new_way.nds
885 assert_equal({ "test" => "ok" }, new_way.tags)
887 amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version + 1, way.id, [4, 6, 15, 1], way.tags, [], {}]
889 assert_response :success
891 result = amf_result("/1")
893 assert_equal 8, result.size
894 assert_equal 0, result[0]
895 assert_equal "", result[1]
896 assert_equal way.id, result[2]
897 assert_equal way.id, result[3]
898 assert_equal({}, result[4])
899 assert_equal way.version + 2, result[5]
900 assert_equal({}, result[6])
901 assert_equal({}, result[7])
903 new_way = Way.find(way.id)
904 assert_equal way.version + 2, new_way.version
905 assert_equal [4, 6, 15, 1], new_way.nds
906 assert_equal way.tags, new_way.tags
909 def test_startchangeset_invalid_xmlchar_comment
911 comment = "foo#{invalid}bar"
913 amf_content "startchangeset", "/1", ["test@example.com:test", {}, nil, comment, 1]
915 assert_response :success
917 result = amf_result("/1")
919 assert_equal 3, result.size, result.inspect
920 assert_equal 0, result[0]
921 new_cs_id = result[2].to_i
923 cs = Changeset.find(new_cs_id)
924 assert_equal "foobar", cs.tags["comment"]
929 # ************************************************************
930 # AMF Helper functions
932 # Get the result record for the specified ID
933 # It's an assertion FAIL if the record does not exist
935 assert @amf_result.key?("#{ref}/onResult")
936 @amf_result["#{ref}/onResult"]
939 # Encode the AMF message to invoke "target" with parameters as
940 # the passed data. The ref is used to retrieve the results.
941 def amf_content(target, ref, data)
944 c.write 0.chr + 0.chr # version 0
945 c.write 0.chr + 0.chr # n headers
946 c.write a.chr + b.chr # n bodies
947 c.write AMF.encodestring(target)
948 c.write AMF.encodestring(ref)
949 c.write [-1].pack("N")
950 c.write AMF.encodevalue(data)
952 @request.env["RAW_POST_DATA"] = c.string
955 # Parses the @response object as an AMF messsage.
956 # The result is a hash of message_ref => data.
957 # The attribute @amf_result is initialised to this hash.
958 def amf_parse_response
959 req = StringIO.new(@response.body)
961 req.read(2) # version
963 # parse through any headers
964 headers = AMF.getint(req) # Read number of headers
965 headers.times do # Read each header
966 AMF.getstring(req) # |
967 req.getc # | skip boolean
968 AMF.getvalue(req) # |
971 # parse through responses
973 bodies = AMF.getint(req) # Read number of bodies
974 bodies.times do # Read each body
975 message = AMF.getstring(req) # | get message name
976 AMF.getstring(req) # | get index in response sequence
977 AMF.getlong(req) # | get total size in bytes
978 args = AMF.getvalue(req) # | get response (probably an array)
979 results[message] = args
981 @amf_result = results
986 # given an array of bounding boxes (each an array of 4 floats), call the
987 # AMF "whichways" controller for each and pass the result back to the
988 # caller's block for assertion testing.
989 def check_bboxes_are_bad(bboxes)
990 bboxes.each do |bbox|
991 amf_content "whichways", "/1", bbox
993 assert_response :success
996 # pass the response back to the caller's block to be tested
997 # against what the caller expected.
998 map = amf_result "/1"
1003 # this should be what AMF controller returns when the bbox of a
1004 # whichways request is invalid or too large.
1005 def assert_boundary_error(map, msg = nil, error_hint = nil)
1006 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1007 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1010 # this should be what AMF controller returns when the bbox of a
1011 # whichways_deleted request is invalid or too large.
1012 def assert_deleted_boundary_error(map, msg = nil, error_hint = nil)
1013 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1014 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"