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