]> git.openstreetmap.org Git - rails.git/blob - test/controllers/amf_controller_test.rb
2d358d0113efbb6bd0b1211b8fcdf3a2060e55c2
[rails.git] / test / controllers / amf_controller_test.rb
1 require "test_helper"
2 require "stringio"
3 include Potlatch
4
5 class AmfControllerTest < ActionController::TestCase
6   api_fixtures
7
8   ##
9   # test all routes which lead to this controller
10   def test_routes
11     assert_routing(
12       { :path => "/api/0.6/amf/read", :method => :post },
13       { :controller => "amf", :action => "amf_read" }
14     )
15     assert_routing(
16       { :path => "/api/0.6/amf/write", :method => :post },
17       { :controller => "amf", :action => "amf_write" }
18     )
19   end
20
21   def test_getpresets
22     user_en_de = create(:user, :languages => %w(en de))
23     user_de = create(:user, :languages => %w(de))
24     [user_en_de, user_de].each do |user|
25       amf_content "getpresets", "/1", ["#{user.email}:test", ""]
26       post :amf_read
27       assert_response :success
28       amf_parse_response
29       presets = amf_result("/1")
30
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"]
45     end
46   end
47
48   def test_getway
49     # check a visible way
50     id = current_ways(:visible_way).id
51     amf_content "getway", "/1", [id]
52     post :amf_read
53     assert_response :success
54     amf_parse_response
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]
63   end
64
65   def test_getway_invisible
66     # check an invisible way
67     id = current_ways(:invisible_way).id
68     amf_content "getway", "/1", [id]
69     post :amf_read
70     assert_response :success
71     amf_parse_response
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?
77   end
78
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]
83     post :amf_read
84     assert_response :success
85     amf_parse_response
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]
94   end
95
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]
100     post :amf_read
101     assert_response :success
102     amf_parse_response
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]
112   end
113
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]
118     post :amf_read
119     assert_response :success
120     amf_parse_response
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 11, way[3][2][2]
129     assert_equal 2, way[5]
130     assert_equal 2, way[6]
131   end
132
133   def test_getway_nonexistent
134     # check chat a non-existent way is not returned
135     amf_content "getway", "/1", [0]
136     post :amf_read
137     assert_response :success
138     amf_parse_response
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?
144   end
145
146   def test_whichways
147     node = create(:node, :lat => 3.0, :lon => 3.0)
148     way = create(:way)
149     deleted_way = create(:way, :deleted)
150     create(:way_node, :way => way, :node => node)
151     create(:way_node, :way => deleted_way, :node => node)
152     create(:way_tag, :way => way)
153
154     minlon = node.lon - 0.1
155     minlat = node.lat - 0.1
156     maxlon = node.lon + 0.1
157     maxlat = node.lat + 0.1
158     amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
159     post :amf_read
160     assert_response :success
161     amf_parse_response
162
163     # check contents of message
164     map = amf_result "/1"
165     assert_equal 0, map[0], "map error code should be 0"
166     assert_equal "", map[1], "map error text should be empty"
167
168     # check the formatting of the message
169     assert_equal 5, map.length, "map should have length 5"
170     assert_equal Array, map[2].class, 'map "ways" element should be an array'
171     assert_equal Array, map[3].class, 'map "nodes" element should be an array'
172     assert_equal Array, map[4].class, 'map "relations" element should be an array'
173     map[2].each do |w|
174       assert_equal 2, w.length, "way should be (id, version) pair"
175       assert w[0] == w[0].floor, "way ID should be an integer"
176       assert w[1] == w[1].floor, "way version should be an integer"
177     end
178
179     map[3].each do |n|
180       assert_equal 5, w.length, "node should be (id, lat, lon, [tags], version) tuple"
181       assert n[0] == n[0].floor, "node ID should be an integer"
182       assert n[1] >= minlat - 0.01, "node lat should be greater than min"
183       assert n[1] <= maxlat - 0.01, "node lat should be less than max"
184       assert n[2] >= minlon - 0.01, "node lon should be greater than min"
185       assert n[2] <= maxlon - 0.01, "node lon should be less than max"
186       assert_equal Array, a[3].class, "node tags should be array"
187       assert n[4] == n[4].floor, "node version should be an integer"
188     end
189
190     map[4].each do |r|
191       assert_equal 2, r.length, "relation should be (id, version) pair"
192       assert r[0] == r[0].floor, "relation ID should be an integer"
193       assert r[1] == r[1].floor, "relation version should be an integer"
194     end
195
196     # TODO: looks like amf_controller changed since this test was written
197     # so someone who knows what they're doing should check this!
198     ways = map[2].collect { |x| x[0] }
199     assert ways.include?(way.id),
200            "map should include used way"
201     assert !ways.include?(deleted_way.id),
202            "map should not include deleted way"
203   end
204
205   ##
206   # checks that too-large a bounding box will not be served.
207   def test_whichways_toobig
208     bbox = [-0.1, -0.1, 1.1, 1.1]
209     check_bboxes_are_bad [bbox] do |map, _bbox|
210       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"
211     end
212   end
213
214   ##
215   # checks that an invalid bounding box will not be served. in this case
216   # one with max < min latitudes.
217   #
218   # NOTE: the controller expands the bbox by 0.01 in each direction!
219   def test_whichways_badlat
220     bboxes = [[0, 0.1, 0.1, 0], [-0.1, 80, 0.1, 70], [0.24, 54.35, 0.25, 54.33]]
221     check_bboxes_are_bad bboxes do |map, bbox|
222       assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
223     end
224   end
225
226   ##
227   # same as test_whichways_badlat, but for longitudes
228   #
229   # NOTE: the controller expands the bbox by 0.01 in each direction!
230   def test_whichways_badlon
231     bboxes = [[80, -0.1, 70, 0.1], [54.35, 0.24, 54.33, 0.25]]
232     check_bboxes_are_bad bboxes do |map, bbox|
233       assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
234     end
235   end
236
237   def test_whichways_deleted
238     node = create(:node, :lat => 3.0, :lon => 3.0)
239     way = create(:way)
240     deleted_way = create(:way, :deleted)
241     create(:way_node, :way => way, :node => node)
242     create(:way_node, :way => deleted_way, :node => node)
243     create(:way_tag, :way => way)
244
245     minlon = node.lon - 0.1
246     minlat = node.lat - 0.1
247     maxlon = node.lon + 0.1
248     maxlat = node.lat + 0.1
249     amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
250     post :amf_read
251     assert_response :success
252     amf_parse_response
253
254     # check contents of message
255     map = amf_result "/1"
256     assert_equal 0, map[0], "first map element should be 0"
257     assert_equal "", map[1], "second map element should be an empty string"
258     assert_equal Array, map[2].class, "third map element should be an array"
259     # TODO: looks like amf_controller changed since this test was written
260     # so someone who knows what they're doing should check this!
261     assert !map[2].include?(current_ways(:used_way).id),
262            "map should not include used way"
263     assert map[2].include?(current_ways(:invisible_way).id),
264            "map should include deleted way"
265   end
266
267   def test_whichways_deleted_toobig
268     bbox = [-0.1, -0.1, 1.1, 1.1]
269     amf_content "whichways_deleted", "/1", bbox
270     post :amf_read
271     assert_response :success
272     amf_parse_response
273
274     map = amf_result "/1"
275     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"
276   end
277
278   def test_getrelation
279     id = current_relations(:visible_relation).id
280     amf_content "getrelation", "/1", [id]
281     post :amf_read
282     assert_response :success
283     amf_parse_response
284     rel = amf_result("/1")
285     assert_equal rel[0], 0
286     assert_equal rel[2], id
287   end
288
289   def test_getrelation_invisible
290     id = current_relations(:invisible_relation).id
291     amf_content "getrelation", "/1", [id]
292     post :amf_read
293     assert_response :success
294     amf_parse_response
295     rel = amf_result("/1")
296     assert_equal rel[0], -4
297     assert_equal rel[1], "relation"
298     assert_equal rel[2], id
299     assert(rel[3].nil?) && rel[4].nil?
300   end
301
302   def test_getrelation_nonexistent
303     id = 0
304     amf_content "getrelation", "/1", [id]
305     post :amf_read
306     assert_response :success
307     amf_parse_response
308     rel = amf_result("/1")
309     assert_equal rel[0], -4
310     assert_equal rel[1], "relation"
311     assert_equal rel[2], id
312     assert(rel[3].nil?) && rel[4].nil?
313   end
314
315   def test_getway_old
316     # try to get the last visible version (specified by <0) (should be current version)
317     latest = current_ways(:way_with_versions)
318     # NOTE: looks from the API changes that this now expects a timestamp
319     # instead of a version number...
320     # try to get version 1
321     v1 = ways(:way_with_versions_v2)
322     { latest.id => "",
323       v1.way_id => v1.timestamp.strftime("%d %b %Y, %H:%M:%S") }.each do |id, t|
324       amf_content "getway_old", "/1", [id, t]
325       post :amf_read
326       assert_response :success
327       amf_parse_response
328       returned_way = amf_result("/1")
329       assert_equal 0, returned_way[0]
330       assert_equal id, returned_way[2]
331       # API returns the *latest* version, even for old ways...
332       assert_equal latest.version, returned_way[5]
333     end
334   end
335
336   ##
337   # test that the server doesn't fall over when rubbish is passed
338   # into the method args.
339   def test_getway_old_invalid
340     way_id = current_ways(:way_with_versions).id
341     { "foo"  => "bar",
342       way_id => "not a date",
343       way_id => "2009-03-25 00:00:00",                   # <- wrong format
344       way_id => "0 Jan 2009 00:00:00",                   # <- invalid date
345       -1     => "1 Jan 2009 00:00:00" }.each do |id, t|  # <- invalid
346       amf_content "getway_old", "/1", [id, t]
347       post :amf_read
348       assert_response :success
349       amf_parse_response
350       returned_way = amf_result("/1")
351       assert_equal -1, returned_way[0]
352       assert returned_way[3].nil?
353       assert returned_way[4].nil?
354       assert returned_way[5].nil?
355     end
356   end
357
358   def test_getway_old_nonexistent
359     # try to get the last version-10 (shoudn't exist)
360     v1 = ways(:way_with_versions_v1)
361     # try to get last visible version of non-existent way
362     # try to get specific version of non-existent way
363     [[0, ""],
364      [0, "1 Jan 1970, 00:00:00"],
365      [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]].each do |id, t|
366       amf_content "getway_old", "/1", [id, t]
367       post :amf_read
368       assert_response :success
369       amf_parse_response
370       returned_way = amf_result("/1")
371       assert_equal -1, returned_way[0]
372       assert returned_way[3].nil?
373       assert returned_way[4].nil?
374       assert returned_way[5].nil?
375     end
376   end
377
378   def test_getway_old_invisible
379     v1 = ways(:invisible_way)
380     # try to get deleted version
381     [[v1.way_id, (v1.timestamp + 10).strftime("%d %b %Y, %H:%M:%S")]].each do |id, t|
382       amf_content "getway_old", "/1", [id, t]
383       post :amf_read
384       assert_response :success
385       amf_parse_response
386       returned_way = amf_result("/1")
387       assert_equal -1, returned_way[0]
388       assert returned_way[3].nil?
389       assert returned_way[4].nil?
390       assert returned_way[5].nil?
391     end
392   end
393
394   def test_getway_history
395     latest = current_ways(:way_with_versions)
396     oldest = ways(:way_with_versions_v1)
397
398     amf_content "getway_history", "/1", [latest.id]
399     post :amf_read
400     assert_response :success
401     amf_parse_response
402     history = amf_result("/1")
403
404     # ['way',wayid,history]
405     assert_equal "way", history[0]
406     assert_equal latest.id, history[1]
407     # We use dates rather than version numbers here, because you might
408     # have moved a node within a way (i.e. way version not incremented).
409     # The timestamp is +1 because we say "give me the revision of 15:33:02",
410     # but that might actually include changes at 15:33:02.457.
411     assert_equal (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
412     assert_equal (oldest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
413   end
414
415   def test_getway_history_nonexistent
416     amf_content "getway_history", "/1", [0]
417     post :amf_read
418     assert_response :success
419     amf_parse_response
420     history = amf_result("/1")
421
422     # ['way',wayid,history]
423     assert_equal history[0], "way"
424     assert_equal history[1], 0
425     assert history[2].empty?
426   end
427
428   def test_getnode_history
429     node = create(:node, :version => 2)
430     node_v1 = create(:old_node, :current_node => node, :version => 1, :timestamp => 3.days.ago)
431     _node_v2 = create(:old_node, :current_node => node, :version => 2, :timestamp => 2.days.ago)
432     node_v3 = create(:old_node, :current_node => node, :version => 3, :timestamp => 1.day.ago)
433
434     amf_content "getnode_history", "/1", [node.id]
435     post :amf_read
436     assert_response :success
437     amf_parse_response
438     history = amf_result("/1")
439
440     # ['node',nodeid,history]
441     # note that (as per getway_history) we actually round up
442     # to the next second
443     assert_equal history[0], "node",
444                  'first element should be "node"'
445     assert_equal history[1], node.id,
446                  "second element should be the input node ID"
447     assert_equal history[2].first[0],
448                  (node_v3.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
449                  "first element in third element (array) should be the latest version"
450     assert_equal history[2].last[0],
451                  (node_v1.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
452                  "last element in third element (array) should be the initial version"
453   end
454
455   def test_getnode_history_nonexistent
456     amf_content "getnode_history", "/1", [0]
457     post :amf_read
458     assert_response :success
459     amf_parse_response
460     history = amf_result("/1")
461
462     # ['node',nodeid,history]
463     assert_equal history[0], "node"
464     assert_equal history[1], 0
465     assert history[2].empty?
466   end
467
468   def test_findgpx_bad_user
469     amf_content "findgpx", "/1", [1, "test@example.com:wrong"]
470     post :amf_read
471     assert_response :success
472     amf_parse_response
473     result = amf_result("/1")
474
475     assert_equal 2, result.length
476     assert_equal -1, result[0]
477     assert_match /must be logged in/, result[1]
478
479     blocked_user = create(:user)
480     create(:user_block, :user => blocked_user)
481     amf_content "findgpx", "/1", [1, "#{blocked_user.email}:test"]
482     post :amf_read
483     assert_response :success
484     amf_parse_response
485     result = amf_result("/1")
486
487     assert_equal 2, result.length
488     assert_equal -1, result[0]
489     assert_match /access to the API has been blocked/, result[1]
490   end
491
492   def test_findgpx_by_id
493     user = create(:user)
494     trace = create(:trace, :visibility => "private", :user => user)
495
496     amf_content "findgpx", "/1", [trace.id, "#{user.email}:test"]
497     post :amf_read
498     assert_response :success
499     amf_parse_response
500     result = amf_result("/1")
501
502     assert_equal 3, result.length
503     assert_equal 0, result[0]
504     assert_equal "", result[1]
505     traces = result[2]
506     assert_equal 1, traces.length
507     assert_equal 3, traces[0].length
508     assert_equal trace.id, traces[0][0]
509     assert_equal trace.name, traces[0][1]
510     assert_equal trace.description, traces[0][2]
511   end
512
513   def test_findgpx_by_name
514     amf_content "findgpx", "/1", ["Trace", "test@example.com:test"]
515     post :amf_read
516     assert_response :success
517     amf_parse_response
518     result = amf_result("/1")
519
520     # find by name fails as it uses mysql text search syntax...
521     assert_equal 2, result.length
522     assert_equal -2, result[0]
523   end
524
525   def test_findrelations_by_id
526     relation = current_relations(:relation_with_versions)
527
528     amf_content "findrelations", "/1", [relation.id]
529     post :amf_read
530     assert_response :success
531     amf_parse_response
532     result = amf_result("/1")
533
534     assert_equal 1, result.length
535     assert_equal 4, result[0].length
536     assert_equal relation.id, result[0][0]
537     assert_equal relation.tags, result[0][1]
538     assert_equal relation.members, result[0][2]
539     assert_equal relation.version, result[0][3]
540
541     amf_content "findrelations", "/1", [999999]
542     post :amf_read
543     assert_response :success
544     amf_parse_response
545     result = amf_result("/1")
546
547     assert_equal 0, result.length
548   end
549
550   def test_findrelations_by_tags
551     visible_relation = current_relations(:visible_relation)
552     create(:relation_tag, :relation => visible_relation, :k => "test", :v => "yes")
553     used_relation = current_relations(:used_relation)
554     create(:relation_tag, :relation => used_relation, :k => "test", :v => "yes")
555     create(:relation_tag, :relation => used_relation, :k => "name", :v => "Test Relation")
556
557     amf_content "findrelations", "/1", ["yes"]
558     post :amf_read
559     assert_response :success
560     amf_parse_response
561     result = amf_result("/1").sort
562
563     assert_equal 2, result.length
564     assert_equal 4, result[0].length
565     assert_equal visible_relation.id, result[0][0]
566     assert_equal visible_relation.tags, result[0][1]
567     assert_equal visible_relation.members, result[0][2]
568     assert_equal visible_relation.version, result[0][3]
569     assert_equal 4, result[1].length
570     assert_equal used_relation.id, result[1][0]
571     assert_equal used_relation.tags, result[1][1]
572     assert_equal used_relation.members, result[1][2]
573     assert_equal used_relation.version, result[1][3]
574
575     amf_content "findrelations", "/1", ["no"]
576     post :amf_read
577     assert_response :success
578     amf_parse_response
579     result = amf_result("/1").sort
580
581     assert_equal 0, result.length
582   end
583
584   def test_getpoi_without_timestamp
585     node = create(:node, :with_history, :version => 4)
586     create(:node_tag, :node => node)
587
588     amf_content "getpoi", "/1", [node.id, ""]
589     post :amf_read
590     assert_response :success
591     amf_parse_response
592     result = amf_result("/1")
593
594     assert_equal 7, result.length
595     assert_equal 0, result[0]
596     assert_equal "", result[1]
597     assert_equal node.id, result[2]
598     assert_equal node.lon, result[3]
599     assert_equal node.lat, result[4]
600     assert_equal node.tags, result[5]
601     assert_equal node.version, result[6]
602
603     amf_content "getpoi", "/1", [999999, ""]
604     post :amf_read
605     assert_response :success
606     amf_parse_response
607     result = amf_result("/1")
608
609     assert_equal 3, result.length
610     assert_equal -4, result[0]
611     assert_equal "node", result[1]
612     assert_equal 999999, result[2]
613   end
614
615   def test_getpoi_with_timestamp
616     current_node = create(:node, :with_history, :version => 4)
617     node = current_node.old_nodes.find_by(:version => 2)
618
619     # Timestamps are stored with microseconds, but xmlschema truncates them to
620     # previous whole second, causing <= comparison to fail
621     timestamp = (node.timestamp + 1.second).xmlschema
622
623     amf_content "getpoi", "/1", [node.node_id, timestamp]
624     post :amf_read
625     assert_response :success
626     amf_parse_response
627     result = amf_result("/1")
628
629     assert_equal 7, result.length
630     assert_equal 0, result[0]
631     assert_equal "", result[1]
632     assert_equal node.node_id, result[2]
633     assert_equal node.lon, result[3]
634     assert_equal node.lat, result[4]
635     assert_equal node.tags, result[5]
636     assert_equal current_node.version, result[6]
637
638     amf_content "getpoi", "/1", [node.node_id, "2000-01-01T00:00:00Z"]
639     post :amf_read
640     assert_response :success
641     amf_parse_response
642     result = amf_result("/1")
643
644     assert_equal 3, result.length
645     assert_equal -4, result[0]
646     assert_equal "node", result[1]
647     assert_equal node.node_id, result[2]
648
649     amf_content "getpoi", "/1", [999999, Time.now.xmlschema]
650     post :amf_read
651     assert_response :success
652     amf_parse_response
653     result = amf_result("/1")
654
655     assert_equal 3, result.length
656     assert_equal -4, result[0]
657     assert_equal "node", result[1]
658     assert_equal 999999, result[2]
659   end
660
661   # ************************************************************
662   # AMF Write tests
663
664   # check that we can update a poi
665   def test_putpoi_update_valid
666     nd = create(:node)
667     cs_id = nd.changeset.id
668     user = nd.changeset.user
669     amf_content "putpoi", "/1", ["#{user.email}:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
670     post :amf_write
671     assert_response :success
672     amf_parse_response
673     result = amf_result("/1")
674
675     assert_equal 5, result.size
676     assert_equal 0, result[0]
677     assert_equal "", result[1]
678     assert_equal nd.id, result[2]
679     assert_equal nd.id, result[3]
680     assert_equal nd.version + 1, result[4]
681
682     # Now try to update again, with a different lat/lon, using the updated version number
683     lat = nd.lat + 0.1
684     lon = nd.lon - 0.1
685     amf_content "putpoi", "/2", ["#{user.email}:test", cs_id, nd.version + 1, nd.id, lon, lat, nd.tags, nd.visible]
686     post :amf_write
687     assert_response :success
688     amf_parse_response
689     result = amf_result("/2")
690
691     assert_equal 5, result.size
692     assert_equal 0, result[0]
693     assert_equal "", result[1]
694     assert_equal nd.id, result[2]
695     assert_equal nd.id, result[3]
696     assert_equal nd.version + 2, result[4]
697   end
698
699   # Check that we can create a no valid poi
700   # Using similar method for the node controller test
701   def test_putpoi_create_valid
702     # This node has no tags
703
704     # create a node with random lat/lon
705     lat = rand(100) - 50 + rand
706     lon = rand(100) - 50 + rand
707     # normal user has a changeset open
708     changeset = changesets(:public_user_first_change)
709
710     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
711     post :amf_write
712     assert_response :success
713     amf_parse_response
714     result = amf_result("/1")
715
716     # check the array returned by the amf
717     assert_equal 5, result.size
718     assert_equal 0, result[0], "expected to get the status ok from the amf"
719     assert_equal 0, result[2], "The old id should be 0"
720     assert result[3] > 0, "The new id should be greater than 0"
721     assert_equal 1, result[4], "The new version should be 1"
722
723     # Finally check that the node that was saved has saved the data correctly
724     # in both the current and history tables
725     # First check the current table
726     current_node = Node.find(result[3].to_i)
727     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
728     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
729     assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
730     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
731     # Now check the history table
732     historic_nodes = Node.where(:id => result[3])
733     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
734     first_historic_node = historic_nodes.first
735     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
736     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
737     assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
738     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
739
740     ####
741     # This node has some tags
742
743     # create a node with random lat/lon
744     lat = rand(100) - 50 + rand
745     lon = rand(100) - 50 + rand
746     # normal user has a changeset open
747     changeset = changesets(:public_user_first_change)
748
749     amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
750     post :amf_write
751     assert_response :success
752     amf_parse_response
753     result = amf_result("/2")
754
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"
761
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_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
767     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
768     assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
769     assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
770     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
771     # Now check the history table
772     historic_nodes = Node.where(:id => result[3])
773     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
774     first_historic_node = historic_nodes.first
775     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
776     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
777     assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
778     assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
779     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
780   end
781
782   # try creating a POI with rubbish in the tags
783   def test_putpoi_create_with_control_chars
784     # This node has no tags
785
786     # create a node with random lat/lon
787     lat = rand(100) - 50 + rand
788     lon = rand(100) - 50 + rand
789     # normal user has a changeset open
790     changeset = changesets(:public_user_first_change)
791
792     mostly_invalid = (0..31).to_a.map(&:chr).join
793     tags = { "something" => "foo#{mostly_invalid}bar" }
794
795     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
796     post :amf_write
797     assert_response :success
798     amf_parse_response
799     result = amf_result("/1")
800
801     # check the array returned by the amf
802     assert_equal 5, result.size
803     assert_equal 0, result[0], "Expected to get the status ok in the amf"
804     assert_equal 0, result[2], "The old id should be 0"
805     assert result[3] > 0, "The new id should be greater than 0"
806     assert_equal 1, result[4], "The new version should be 1"
807
808     # Finally check that the node that was saved has saved the data correctly
809     # in both the current and history tables
810     # First check the current table
811     current_node = Node.find(result[3].to_i)
812     assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
813     assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
814     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
815   end
816
817   # try creating a POI with rubbish in the tags
818   def test_putpoi_create_with_invalid_utf8
819     # This node has no tags
820
821     # create a node with random lat/lon
822     lat = rand(100) - 50 + rand
823     lon = rand(100) - 50 + rand
824     # normal user has a changeset open
825     changeset = changesets(:public_user_first_change)
826
827     invalid = "\xc0\xc0"
828     tags = { "something" => "foo#{invalid}bar" }
829
830     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
831     post :amf_write
832     assert_response :success
833     amf_parse_response
834     result = amf_result("/1")
835
836     assert_equal 2, result.size
837     assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
838     assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
839   end
840
841   # try deleting a node
842   def test_putpoi_delete_valid
843     nd = create(:node)
844     cs_id = nd.changeset.id
845     user = nd.changeset.user
846
847     amf_content "putpoi", "/1", ["#{user.email}:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
848     post :amf_write
849     assert_response :success
850     amf_parse_response
851     result = amf_result("/1")
852
853     assert_equal 5, result.size
854     assert_equal 0, result[0]
855     assert_equal "", result[1]
856     assert_equal nd.id, result[2]
857     assert_equal nd.id, result[3]
858     assert_equal nd.version + 1, result[4]
859
860     current_node = Node.find(result[3].to_i)
861     assert_equal false, current_node.visible
862   end
863
864   # try deleting a node that is already deleted
865   def test_putpoi_delete_already_deleted
866     nd = create(:node, :deleted)
867     cs_id = nd.changeset.id
868     user = nd.changeset.user
869
870     amf_content "putpoi", "/1", ["#{user.email}:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
871     post :amf_write
872     assert_response :success
873     amf_parse_response
874     result = amf_result("/1")
875
876     assert_equal 3, result.size
877     assert_equal -4, result[0]
878     assert_equal "node", result[1]
879     assert_equal nd.id, result[2]
880   end
881
882   # try deleting a node that has never existed
883   def test_putpoi_delete_not_found
884     changeset = create(:changeset)
885     cs_id = changeset.id
886     user = changeset.user
887
888     amf_content "putpoi", "/1", ["#{user.email}:test", cs_id, 1, 999999, 0, 0, {}, false]
889     post :amf_write
890     assert_response :success
891     amf_parse_response
892     result = amf_result("/1")
893
894     assert_equal 3, result.size
895     assert_equal -4, result[0]
896     assert_equal "node", result[1]
897     assert_equal 999999, result[2]
898   end
899
900   # try setting an invalid location on a node
901   def test_putpoi_invalid_latlon
902     nd = create(:node)
903     cs_id = nd.changeset.id
904     user = nd.changeset.user
905
906     amf_content "putpoi", "/1", ["#{user.email}:test", cs_id, nd.version, nd.id, 200, 100, nd.tags, true]
907     post :amf_write
908     assert_response :success
909     amf_parse_response
910     result = amf_result("/1")
911
912     assert_equal 2, result.size
913     assert_equal -2, result[0]
914     assert_match /Node is not in the world/, result[1]
915   end
916
917   # check that we can create a way
918   def test_putway_create_valid
919     cs_id = changesets(:public_user_first_change).id
920
921     amf_content "putway", "/1", ["test@example.com:test", cs_id, 0, -1, [1, 4, 7], { "test" => "new" }, [], {}]
922     post :amf_write
923     assert_response :success
924     amf_parse_response
925     result = amf_result("/1")
926     new_way_id = result[3].to_i
927
928     assert_equal 8, result.size
929     assert_equal 0, result[0]
930     assert_equal "", result[1]
931     assert_equal -1, result[2]
932     assert_not_equal -1, result[3]
933     assert_equal({}, result[4])
934     assert_equal 1, result[5]
935     assert_equal({}, result[6])
936     assert_equal({}, result[7])
937
938     new_way = Way.find(new_way_id)
939     assert_equal 1, new_way.version
940     assert_equal [1, 4, 7], new_way.nds
941     assert_equal({ "test" => "new" }, new_way.tags)
942
943     amf_content "putway", "/1", ["test@example.com:test", cs_id, 0, -1, [4, 6, 15, 1], { "test" => "newer" }, [], {}]
944     post :amf_write
945     assert_response :success
946     amf_parse_response
947     result = amf_result("/1")
948     new_way_id = result[3].to_i
949
950     assert_equal 8, result.size
951     assert_equal 0, result[0]
952     assert_equal "", result[1]
953     assert_equal -1, result[2]
954     assert_not_equal -1, result[3]
955     assert_equal({}, result[4])
956     assert_equal 1, result[5]
957     assert_equal({}, result[6])
958     assert_equal({}, result[7])
959
960     new_way = Way.find(new_way_id)
961     assert_equal 1, new_way.version
962     assert_equal [4, 6, 15, 1], new_way.nds
963     assert_equal({ "test" => "newer" }, new_way.tags)
964
965     amf_content "putway", "/1", ["test@example.com:test", cs_id, 0, -1, [4, -1, 6, 15], { "test" => "newest" }, [[4.56, 12.34, -1, 0, { "test" => "new" }], [12.34, 4.56, 6, 1, { "test" => "ok" }]], { 1 => 1 }]
966     post :amf_write
967     assert_response :success
968     amf_parse_response
969     result = amf_result("/1")
970     new_way_id = result[3].to_i
971     new_node_id = result[4]["-1"].to_i
972
973     assert_equal 8, result.size
974     assert_equal 0, result[0]
975     assert_equal "", result[1]
976     assert_equal -1, result[2]
977     assert_not_equal -1, result[3]
978     assert_equal({ "-1" => new_node_id }, result[4])
979     assert_equal 1, result[5]
980     assert_equal({ new_node_id.to_s => 1, "6" => 2 }, result[6])
981     assert_equal({ "1" => 1 }, result[7])
982
983     new_way = Way.find(new_way_id)
984     assert_equal 1, new_way.version
985     assert_equal [4, new_node_id, 6, 15], new_way.nds
986     assert_equal({ "test" => "newest" }, new_way.tags)
987
988     new_node = Node.find(new_node_id)
989     assert_equal 1, new_node.version
990     assert_equal true, new_node.visible
991     assert_equal 4.56, new_node.lon
992     assert_equal 12.34, new_node.lat
993     assert_equal({ "test" => "new" }, new_node.tags)
994
995     changed_node = Node.find(6)
996     assert_equal 2, changed_node.version
997     assert_equal true, changed_node.visible
998     assert_equal 12.34, changed_node.lon
999     assert_equal 4.56, changed_node.lat
1000     assert_equal({ "test" => "ok" }, changed_node.tags)
1001
1002     # node is not deleted because our other ways are using it
1003     deleted_node = Node.find(1)
1004     assert_equal 1, deleted_node.version
1005     assert_equal true, deleted_node.visible
1006   end
1007
1008   # check that we can update a way
1009   def test_putway_update_valid
1010     way = current_ways(:way_with_multiple_nodes)
1011     cs_id = changesets(:public_user_first_change).id
1012
1013     assert_not_equal({ "test" => "ok" }, way.tags)
1014     amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version, way.id, way.nds, { "test" => "ok" }, [], {}]
1015     post :amf_write
1016     assert_response :success
1017     amf_parse_response
1018     result = amf_result("/1")
1019
1020     assert_equal 8, result.size
1021     assert_equal 0, result[0]
1022     assert_equal "", result[1]
1023     assert_equal way.id, result[2]
1024     assert_equal way.id, result[3]
1025     assert_equal({}, result[4])
1026     assert_equal way.version + 1, result[5]
1027     assert_equal({}, result[6])
1028     assert_equal({}, result[7])
1029
1030     new_way = Way.find(way.id)
1031     assert_equal way.version + 1, new_way.version
1032     assert_equal way.nds, new_way.nds
1033     assert_equal({ "test" => "ok" }, new_way.tags)
1034
1035     assert_not_equal [4, 6, 15, 1], way.tags
1036     amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version + 1, way.id, [4, 6, 15, 1], way.tags, [], {}]
1037     post :amf_write
1038     assert_response :success
1039     amf_parse_response
1040     result = amf_result("/1")
1041
1042     assert_equal 8, result.size
1043     assert_equal 0, result[0]
1044     assert_equal "", result[1]
1045     assert_equal way.id, result[2]
1046     assert_equal way.id, result[3]
1047     assert_equal({}, result[4])
1048     assert_equal way.version + 2, result[5]
1049     assert_equal({}, result[6])
1050     assert_equal({}, result[7])
1051
1052     new_way = Way.find(way.id)
1053     assert_equal way.version + 2, new_way.version
1054     assert_equal [4, 6, 15, 1], new_way.nds
1055     assert_equal way.tags, new_way.tags
1056
1057     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 }]
1058     post :amf_write
1059     assert_response :success
1060     amf_parse_response
1061     result = amf_result("/1")
1062     new_node_id = result[4]["-1"].to_i
1063
1064     assert_equal 8, result.size
1065     assert_equal 0, result[0]
1066     assert_equal "", result[1]
1067     assert_equal way.id, result[2]
1068     assert_equal way.id, result[3]
1069     assert_equal({ "-1" => new_node_id }, result[4])
1070     assert_equal way.version + 3, result[5]
1071     assert_equal({ new_node_id.to_s => 1, "6" => 2 }, result[6])
1072     assert_equal({ "1" => 1 }, result[7])
1073
1074     new_way = Way.find(way.id)
1075     assert_equal way.version + 3, new_way.version
1076     assert_equal [4, new_node_id, 6, 15], new_way.nds
1077     assert_equal way.tags, new_way.tags
1078
1079     new_node = Node.find(new_node_id)
1080     assert_equal 1, new_node.version
1081     assert_equal true, new_node.visible
1082     assert_equal 4.56, new_node.lon
1083     assert_equal 12.34, new_node.lat
1084     assert_equal({ "test" => "new" }, new_node.tags)
1085
1086     changed_node = Node.find(6)
1087     assert_equal 2, changed_node.version
1088     assert_equal true, changed_node.visible
1089     assert_equal 12.34, changed_node.lon
1090     assert_equal 4.56, changed_node.lat
1091     assert_equal({ "test" => "ok" }, changed_node.tags)
1092
1093     deleted_node = Node.find(1)
1094     assert_equal 2, deleted_node.version
1095     assert_equal false, deleted_node.visible
1096   end
1097
1098   # check that we can delete a way
1099   def test_deleteway_valid
1100     way = current_ways(:way_with_multiple_nodes)
1101     nodes = way.nodes.each_with_object({}) { |n, ns| ns[n.id] = n.version }
1102     cs_id = changesets(:public_user_first_change).id
1103
1104     amf_content "deleteway", "/1", ["test@example.com:test", cs_id, way.id, way.version, nodes]
1105     post :amf_write
1106     assert_response :success
1107     amf_parse_response
1108     result = amf_result("/1")
1109
1110     assert_equal 5, result.size
1111     assert_equal 0, result[0]
1112     assert_equal "", result[1]
1113     assert_equal way.id, result[2]
1114     assert_equal way.version + 1, result[3]
1115     assert_equal({ "11" => 2 }, result[4])
1116
1117     new_way = Way.find(way.id)
1118     assert_equal way.version + 1, new_way.version
1119     assert_equal false, new_way.visible
1120
1121     way.nds.each do |node_id|
1122       assert_equal result[4][node_id.to_s].nil?, Node.find(node_id).visible
1123     end
1124   end
1125
1126   # check that we can't delete a way that is in use
1127   def test_deleteway_inuse
1128     way = current_ways(:used_way)
1129     nodes = way.nodes.each_with_object({}) { |n, ns| ns[n.id] = n.version }
1130     cs_id = changesets(:public_user_first_change).id
1131
1132     amf_content "deleteway", "/1", ["test@example.com:test", cs_id, way.id, way.version, nodes]
1133     post :amf_write
1134     assert_response :success
1135     amf_parse_response
1136     result = amf_result("/1")
1137
1138     assert_equal 2, result.size
1139     assert_equal -1, result[0]
1140     assert_match /Way #{way.id} is still used/, result[1]
1141
1142     new_way = Way.find(way.id)
1143     assert_equal way.version, new_way.version
1144     assert_equal true, new_way.visible
1145
1146     way.nds.each do |node_id|
1147       assert_equal true, Node.find(node_id).visible
1148     end
1149   end
1150
1151   # check that we can create a relation
1152   def test_putrelation_create_valid
1153     cs_id = changesets(:public_user_first_change).id
1154
1155     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, 0, -1, { "test" => "new" }, [["Node", 3, "node"], ["Way", 7, "way"], ["Relation", 1, "relation"]], true]
1156     post :amf_write
1157     assert_response :success
1158     amf_parse_response
1159     result = amf_result("/1")
1160     new_relation_id = result[3].to_i
1161
1162     assert_equal 5, result.size
1163     assert_equal 0, result[0]
1164     assert_equal "", result[1]
1165     assert_equal -1, result[2]
1166     assert_not_equal -1, result[3]
1167     assert_equal 1, result[4]
1168
1169     new_relation = Relation.find(new_relation_id)
1170     assert_equal 1, new_relation.version
1171     assert_equal [["Node", 3, "node"], ["Way", 7, "way"], ["Relation", 1, "relation"]], new_relation.members
1172     assert_equal({ "test" => "new" }, new_relation.tags)
1173     assert_equal true, new_relation.visible
1174   end
1175
1176   # check that we can update a relation
1177   def test_putrelation_update_valid
1178     relation = current_relations(:visible_relation)
1179     cs_id = changesets(:public_user_first_change).id
1180
1181     assert_not_equal({ "test" => "ok" }, relation.tags)
1182     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, relation.version, relation.id, { "test" => "ok" }, relation.members, true]
1183     post :amf_write
1184     assert_response :success
1185     amf_parse_response
1186     result = amf_result("/1")
1187
1188     assert_equal 5, result.size
1189     assert_equal 0, result[0]
1190     assert_equal "", result[1]
1191     assert_equal relation.id, result[2]
1192     assert_equal relation.id, result[3]
1193     assert_equal relation.version + 1, result[4]
1194
1195     new_relation = Relation.find(relation.id)
1196     assert_equal relation.version + 1, new_relation.version
1197     assert_equal relation.members, new_relation.members
1198     assert_equal({ "test" => "ok" }, new_relation.tags)
1199     assert_equal true, new_relation.visible
1200   end
1201
1202   # check that we can delete a relation
1203   def test_putrelation_delete_valid
1204     relation = current_relations(:visible_relation)
1205     cs_id = changesets(:public_user_first_change).id
1206
1207     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, relation.version, relation.id, relation.tags, relation.members, false]
1208     post :amf_write
1209     assert_response :success
1210     amf_parse_response
1211     result = amf_result("/1")
1212
1213     assert_equal 5, result.size
1214     assert_equal 0, result[0]
1215     assert_equal "", result[1]
1216     assert_equal relation.id, result[2]
1217     assert_equal relation.id, result[3]
1218     assert_equal relation.version + 1, result[4]
1219
1220     new_relation = Relation.find(relation.id)
1221     assert_equal relation.version + 1, new_relation.version
1222     assert_equal [], new_relation.members
1223     assert_equal({}, new_relation.tags)
1224     assert_equal false, new_relation.visible
1225   end
1226
1227   # check that we can't delete a relation that is in use
1228   def test_putrelation_delete_inuse
1229     relation = current_relations(:public_used_relation)
1230     cs_id = changesets(:public_user_first_change).id
1231
1232     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, relation.version, relation.id, relation.tags, relation.members, false]
1233     post :amf_write
1234     assert_response :success
1235     amf_parse_response
1236     result = amf_result("/1")
1237
1238     assert_equal 2, result.size
1239     assert_equal -1, result[0]
1240     assert_match /relation #{relation.id} is used in/, result[1]
1241
1242     new_relation = Relation.find(relation.id)
1243     assert_equal relation.version, new_relation.version
1244     assert_equal relation.members, new_relation.members
1245     assert_equal relation.tags, new_relation.tags
1246     assert_equal true, new_relation.visible
1247   end
1248
1249   # check that we can open a changeset
1250   def test_startchangeset_valid
1251     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "new" }, nil, "new", 1]
1252     post :amf_write
1253     assert_response :success
1254     amf_parse_response
1255     result = amf_result("/1")
1256     new_cs_id = result[2].to_i
1257
1258     assert_equal 3, result.size
1259     assert_equal 0, result[0]
1260     assert_equal "", result[1]
1261
1262     cs = Changeset.find(new_cs_id)
1263     assert_equal true, cs.is_open?
1264     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1265
1266     old_cs_id = new_cs_id
1267
1268     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "newer" }, old_cs_id, "newer", 1]
1269     post :amf_write
1270     assert_response :success
1271     amf_parse_response
1272     result = amf_result("/1")
1273     new_cs_id = result[2].to_i
1274
1275     assert_not_equal old_cs_id, new_cs_id
1276
1277     assert_equal 3, result.size
1278     assert_equal 0, result[0]
1279     assert_equal "", result[1]
1280
1281     cs = Changeset.find(old_cs_id)
1282     assert_equal false, cs.is_open?
1283     assert_equal({ "comment" => "newer", "source" => "new" }, cs.tags)
1284
1285     cs = Changeset.find(new_cs_id)
1286     assert_equal true, cs.is_open?
1287     assert_equal({ "comment" => "newer", "source" => "newer" }, cs.tags)
1288
1289     old_cs_id = new_cs_id
1290
1291     amf_content "startchangeset", "/1", ["test@example.com:test", {}, old_cs_id, "", 0]
1292     post :amf_write
1293     assert_response :success
1294     amf_parse_response
1295     result = amf_result("/1")
1296
1297     assert_equal 3, result.size
1298     assert_equal 0, result[0]
1299     assert_equal "", result[1]
1300     assert_nil result[2]
1301
1302     cs = Changeset.find(old_cs_id)
1303     assert_equal false, cs.is_open?
1304     assert_equal({ "comment" => "newer", "source" => "newer" }, cs.tags)
1305   end
1306
1307   # check that we can't close somebody elses changeset
1308   def test_startchangeset_invalid_wrong_user
1309     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "new" }, nil, "new", 1]
1310     post :amf_write
1311     assert_response :success
1312     amf_parse_response
1313     result = amf_result("/1")
1314     cs_id = result[2].to_i
1315
1316     assert_equal 3, result.size
1317     assert_equal 0, result[0]
1318     assert_equal "", result[1]
1319
1320     cs = Changeset.find(cs_id)
1321     assert_equal true, cs.is_open?
1322     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1323
1324     amf_content "startchangeset", "/1", ["test@openstreetmap.org:test", {}, cs_id, "delete", 0]
1325     post :amf_write
1326     assert_response :success
1327     amf_parse_response
1328     result = amf_result("/1")
1329
1330     assert_equal 2, result.size
1331     assert_equal -2, result[0]
1332     assert_equal "The user doesn't own that changeset", result[1]
1333
1334     cs = Changeset.find(cs_id)
1335     assert_equal true, cs.is_open?
1336     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1337   end
1338
1339   # check that invalid characters are stripped from changeset tags
1340   def test_startchangeset_invalid_xmlchar_comment
1341     invalid = "\035\022"
1342     comment = "foo#{invalid}bar"
1343
1344     amf_content "startchangeset", "/1", ["test@example.com:test", {}, nil, comment, 1]
1345     post :amf_write
1346     assert_response :success
1347     amf_parse_response
1348     result = amf_result("/1")
1349     new_cs_id = result[2].to_i
1350
1351     assert_equal 3, result.size
1352     assert_equal 0, result[0]
1353     assert_equal "", result[1]
1354
1355     cs = Changeset.find(new_cs_id)
1356     assert_equal true, cs.is_open?
1357     assert_equal({ "comment" => "foobar" }, cs.tags)
1358   end
1359
1360   private
1361
1362   # ************************************************************
1363   # AMF Helper functions
1364
1365   # Get the result record for the specified ID
1366   # It's an assertion FAIL if the record does not exist
1367   def amf_result(ref)
1368     assert @amf_result.key?("#{ref}/onResult")
1369     @amf_result["#{ref}/onResult"]
1370   end
1371
1372   # Encode the AMF message to invoke "target" with parameters as
1373   # the passed data. The ref is used to retrieve the results.
1374   def amf_content(target, ref, data)
1375     a, b = 1.divmod(256)
1376     c = StringIO.new
1377     c.write 0.chr + 0.chr   # version 0
1378     c.write 0.chr + 0.chr   # n headers
1379     c.write a.chr + b.chr   # n bodies
1380     c.write AMF.encodestring(target)
1381     c.write AMF.encodestring(ref)
1382     c.write [-1].pack("N")
1383     c.write AMF.encodevalue(data)
1384
1385     @request.env["RAW_POST_DATA"] = c.string
1386   end
1387
1388   # Parses the @response object as an AMF messsage.
1389   # The result is a hash of message_ref => data.
1390   # The attribute @amf_result is initialised to this hash.
1391   def amf_parse_response
1392     req = StringIO.new(@response.body)
1393
1394     req.read(2) # version
1395
1396     # parse through any headers
1397     headers = AMF.getint(req)        # Read number of headers
1398     headers.times do                 # Read each header
1399       AMF.getstring(req)             #  |
1400       req.getc                       #  | skip boolean
1401       AMF.getvalue(req)              #  |
1402     end
1403
1404     # parse through responses
1405     results = {}
1406     bodies = AMF.getint(req)         # Read number of bodies
1407     bodies.times do                  # Read each body
1408       message = AMF.getstring(req)   #  | get message name
1409       AMF.getstring(req)             #  | get index in response sequence
1410       AMF.getlong(req)               #  | get total size in bytes
1411       args = AMF.getvalue(req)       #  | get response (probably an array)
1412       results[message] = args
1413     end
1414     @amf_result = results
1415     results
1416   end
1417
1418   ##
1419   # given an array of bounding boxes (each an array of 4 floats), call the
1420   # AMF "whichways" controller for each and pass the result back to the
1421   # caller's block for assertion testing.
1422   def check_bboxes_are_bad(bboxes)
1423     bboxes.each do |bbox|
1424       amf_content "whichways", "/1", bbox
1425       post :amf_read
1426       assert_response :success
1427       amf_parse_response
1428
1429       # pass the response back to the caller's block to be tested
1430       # against what the caller expected.
1431       map = amf_result "/1"
1432       yield map, bbox
1433     end
1434   end
1435
1436   # this should be what AMF controller returns when the bbox of a
1437   # whichways request is invalid or too large.
1438   def assert_boundary_error(map, msg = nil, error_hint = nil)
1439     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1440     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1441   end
1442
1443   # this should be what AMF controller returns when the bbox of a
1444   # whichways_deleted request is invalid or too large.
1445   def assert_deleted_boundary_error(map, msg = nil, error_hint = nil)
1446     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1447     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1448   end
1449 end