]> git.openstreetmap.org Git - rails.git/blob - test/controllers/amf_controller_test.rb
Refactor the user list test to use factory-built users and explicit assertions.
[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     way = create(:way_with_nodes, :nodes_count => 1)
51     node = way.nodes.first
52     user = way.changeset.user
53
54     amf_content "getway", "/1", [way.id]
55     post :amf_read
56     assert_response :success
57     amf_parse_response
58     result = amf_result("/1")
59     assert_equal 0, result[0]
60     assert_equal "", result[1]
61     assert_equal way.id, result[2]
62     assert_equal 1, result[3].length
63     assert_equal node.id, result[3][0][2]
64     assert_equal way.version, result[5]
65     assert_equal user.id, result[6]
66   end
67
68   def test_getway_invisible
69     # check an invisible way
70     id = create(:way, :deleted).id
71
72     amf_content "getway", "/1", [id]
73     post :amf_read
74     assert_response :success
75     amf_parse_response
76     result = amf_result("/1")
77     assert_equal -4, result[0]
78     assert_equal "way", result[1]
79     assert_equal id, result[2]
80     assert(result[3].nil? && result[4].nil? && result[5].nil? && result[6].nil?)
81   end
82
83   def test_getway_with_versions
84     # check a way with multiple versions
85     way = create(:way, :with_history, :version => 4)
86     create(:way_node, :way => way)
87     node = way.nodes.first
88     user = way.changeset.user
89
90     amf_content "getway", "/1", [way.id]
91     post :amf_read
92     assert_response :success
93     amf_parse_response
94     result = amf_result("/1")
95     assert_equal 0, result[0]
96     assert_equal "", result[1]
97     assert_equal way.id, result[2]
98     assert_equal 1, result[3].length
99     assert_equal node.id, result[3][0][2]
100     assert_equal way.version, result[5]
101     assert_equal user.id, result[6]
102   end
103
104   def test_getway_with_duplicate_nodes
105     # check a way with duplicate nodes
106     way = create(:way)
107     node = create(:node)
108     create(:way_node, :way => way, :node => node, :sequence_id => 1)
109     create(:way_node, :way => way, :node => node, :sequence_id => 2)
110     user = way.changeset.user
111
112     amf_content "getway", "/1", [way.id]
113     post :amf_read
114     assert_response :success
115     amf_parse_response
116     result = amf_result("/1")
117     assert_equal 0, result[0]
118     assert_equal "", result[1]
119     assert_equal way.id, result[2]
120     assert_equal 2, result[3].length
121     assert_equal node.id, result[3][0][2]
122     assert_equal node.id, result[3][1][2]
123     assert_equal way.version, result[5]
124     assert_equal user.id, result[6]
125   end
126
127   def test_getway_with_multiple_nodes
128     # check a way with multiple nodes
129     way = create(:way_with_nodes, :nodes_count => 3)
130     a = way.nodes[0].id
131     b = way.nodes[1].id
132     c = way.nodes[2].id
133     user = way.changeset.user
134
135     amf_content "getway", "/1", [way.id]
136     post :amf_read
137     assert_response :success
138     amf_parse_response
139     result = amf_result("/1")
140     assert_equal 0, result[0]
141     assert_equal "", result[1]
142     assert_equal way.id, result[2]
143     assert_equal 3, result[3].length
144     assert_equal a, result[3][0][2]
145     assert_equal b, result[3][1][2]
146     assert_equal c, result[3][2][2]
147     assert_equal way.version, result[5]
148     assert_equal user.id, result[6]
149   end
150
151   def test_getway_nonexistent
152     # check chat a non-existent way is not returned
153     amf_content "getway", "/1", [0]
154     post :amf_read
155     assert_response :success
156     amf_parse_response
157     way = amf_result("/1")
158     assert_equal -4, way[0]
159     assert_equal "way", way[1]
160     assert_equal 0, way[2]
161     assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
162   end
163
164   def test_whichways
165     node = create(:node, :lat => 3.0, :lon => 3.0)
166     way = create(:way)
167     deleted_way = create(:way, :deleted)
168     create(:way_node, :way => way, :node => node)
169     create(:way_node, :way => deleted_way, :node => node)
170     create(:way_tag, :way => way)
171
172     minlon = node.lon - 0.1
173     minlat = node.lat - 0.1
174     maxlon = node.lon + 0.1
175     maxlat = node.lat + 0.1
176     amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
177     post :amf_read
178     assert_response :success
179     amf_parse_response
180
181     # check contents of message
182     map = amf_result "/1"
183     assert_equal 0, map[0], "map error code should be 0"
184     assert_equal "", map[1], "map error text should be empty"
185
186     # check the formatting of the message
187     assert_equal 5, map.length, "map should have length 5"
188     assert_equal Array, map[2].class, 'map "ways" element should be an array'
189     assert_equal Array, map[3].class, 'map "nodes" element should be an array'
190     assert_equal Array, map[4].class, 'map "relations" element should be an array'
191     map[2].each do |w|
192       assert_equal 2, w.length, "way should be (id, version) pair"
193       assert w[0] == w[0].floor, "way ID should be an integer"
194       assert w[1] == w[1].floor, "way version should be an integer"
195     end
196
197     map[3].each do |n|
198       assert_equal 5, w.length, "node should be (id, lat, lon, [tags], version) tuple"
199       assert n[0] == n[0].floor, "node ID should be an integer"
200       assert n[1] >= minlat - 0.01, "node lat should be greater than min"
201       assert n[1] <= maxlat - 0.01, "node lat should be less than max"
202       assert n[2] >= minlon - 0.01, "node lon should be greater than min"
203       assert n[2] <= maxlon - 0.01, "node lon should be less than max"
204       assert_equal Array, a[3].class, "node tags should be array"
205       assert n[4] == n[4].floor, "node version should be an integer"
206     end
207
208     map[4].each do |r|
209       assert_equal 2, r.length, "relation should be (id, version) pair"
210       assert r[0] == r[0].floor, "relation ID should be an integer"
211       assert r[1] == r[1].floor, "relation version should be an integer"
212     end
213
214     # TODO: looks like amf_controller changed since this test was written
215     # so someone who knows what they're doing should check this!
216     ways = map[2].collect { |x| x[0] }
217     assert ways.include?(way.id),
218            "map should include used way"
219     assert !ways.include?(deleted_way.id),
220            "map should not include deleted way"
221   end
222
223   ##
224   # checks that too-large a bounding box will not be served.
225   def test_whichways_toobig
226     bbox = [-0.1, -0.1, 1.1, 1.1]
227     check_bboxes_are_bad [bbox] do |map, _bbox|
228       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"
229     end
230   end
231
232   ##
233   # checks that an invalid bounding box will not be served. in this case
234   # one with max < min latitudes.
235   #
236   # NOTE: the controller expands the bbox by 0.01 in each direction!
237   def test_whichways_badlat
238     bboxes = [[0, 0.1, 0.1, 0], [-0.1, 80, 0.1, 70], [0.24, 54.35, 0.25, 54.33]]
239     check_bboxes_are_bad bboxes do |map, bbox|
240       assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
241     end
242   end
243
244   ##
245   # same as test_whichways_badlat, but for longitudes
246   #
247   # NOTE: the controller expands the bbox by 0.01 in each direction!
248   def test_whichways_badlon
249     bboxes = [[80, -0.1, 70, 0.1], [54.35, 0.24, 54.33, 0.25]]
250     check_bboxes_are_bad bboxes do |map, bbox|
251       assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
252     end
253   end
254
255   def test_whichways_deleted
256     node = create(:node, :with_history, :lat => 24.0, :lon => 24.0)
257     way = create(:way, :with_history)
258     way_v1 = way.old_ways.find_by(:version => 1)
259     deleted_way = create(:way, :with_history, :deleted)
260     deleted_way_v1 = deleted_way.old_ways.find_by(:version => 1)
261     create(:way_node, :way => way, :node => node)
262     create(:way_node, :way => deleted_way, :node => node)
263     create(:old_way_node, :old_way => way_v1, :node => node)
264     create(:old_way_node, :old_way => deleted_way_v1, :node => node)
265
266     minlon = node.lon - 0.1
267     minlat = node.lat - 0.1
268     maxlon = node.lon + 0.1
269     maxlat = node.lat + 0.1
270     amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
271     post :amf_read
272     assert_response :success
273     amf_parse_response
274
275     # check contents of message
276     map = amf_result "/1"
277     assert_equal 0, map[0], "first map element should be 0"
278     assert_equal "", map[1], "second map element should be an empty string"
279     assert_equal Array, map[2].class, "third map element should be an array"
280     # TODO: looks like amf_controller changed since this test was written
281     # so someone who knows what they're doing should check this!
282     assert !map[2].include?(way.id),
283            "map should not include visible way"
284     assert map[2].include?(deleted_way.id),
285            "map should include deleted way"
286   end
287
288   def test_whichways_deleted_toobig
289     bbox = [-0.1, -0.1, 1.1, 1.1]
290     amf_content "whichways_deleted", "/1", bbox
291     post :amf_read
292     assert_response :success
293     amf_parse_response
294
295     map = amf_result "/1"
296     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"
297   end
298
299   def test_getrelation
300     id = create(:relation).id
301     amf_content "getrelation", "/1", [id]
302     post :amf_read
303     assert_response :success
304     amf_parse_response
305     rel = amf_result("/1")
306     assert_equal rel[0], 0
307     assert_equal rel[2], id
308   end
309
310   def test_getrelation_invisible
311     id = create(:relation, :deleted).id
312     amf_content "getrelation", "/1", [id]
313     post :amf_read
314     assert_response :success
315     amf_parse_response
316     rel = amf_result("/1")
317     assert_equal rel[0], -4
318     assert_equal rel[1], "relation"
319     assert_equal rel[2], id
320     assert(rel[3].nil?) && rel[4].nil?
321   end
322
323   def test_getrelation_nonexistent
324     id = 0
325     amf_content "getrelation", "/1", [id]
326     post :amf_read
327     assert_response :success
328     amf_parse_response
329     rel = amf_result("/1")
330     assert_equal rel[0], -4
331     assert_equal rel[1], "relation"
332     assert_equal rel[2], id
333     assert(rel[3].nil?) && rel[4].nil?
334   end
335
336   def test_getway_old
337     latest = create(:way, :version => 2)
338     v1 = create(:old_way, :current_way => latest, :version => 1, :timestamp => Time.now.utc - 2.minutes)
339     _v2 = create(:old_way, :current_way => latest, :version => 2, :timestamp => Time.now.utc - 1.minute)
340
341     # try to get the last visible version (specified by <0) (should be current version)
342     # NOTE: looks from the API changes that this now expects a timestamp
343     # instead of a version number...
344     # try to get version 1
345     { latest.id => "",
346       v1.way_id => (v1.timestamp + 1).strftime("%d %b %Y, %H:%M:%S") }.each do |id, t|
347       amf_content "getway_old", "/1", [id, t]
348       post :amf_read
349       assert_response :success
350       amf_parse_response
351       returned_way = amf_result("/1")
352       assert_equal 0, returned_way[0]
353       assert_equal id, returned_way[2]
354       # API returns the *latest* version, even for old ways...
355       assert_equal latest.version, returned_way[5]
356     end
357   end
358
359   ##
360   # test that the server doesn't fall over when rubbish is passed
361   # into the method args.
362   def test_getway_old_invalid
363     way_id = create(:way, :with_history, :version => 2).id
364     { "foo"  => "bar",
365       way_id => "not a date",
366       way_id => "2009-03-25 00:00:00",                   # <- wrong format
367       way_id => "0 Jan 2009 00:00:00",                   # <- invalid date
368       -1     => "1 Jan 2009 00:00:00" }.each do |id, t|  # <- invalid
369       amf_content "getway_old", "/1", [id, t]
370       post :amf_read
371       assert_response :success
372       amf_parse_response
373       returned_way = amf_result("/1")
374       assert_equal -1, returned_way[0]
375       assert returned_way[3].nil?
376       assert returned_way[4].nil?
377       assert returned_way[5].nil?
378     end
379   end
380
381   def test_getway_old_nonexistent
382     # try to get the last version-10 (shoudn't exist)
383     way = create(:way, :with_history, :version => 2)
384     v1 = way.old_ways.find_by(:version => 1)
385     # try to get last visible version of non-existent way
386     # try to get specific version of non-existent way
387     [[0, ""],
388      [0, "1 Jan 1970, 00:00:00"],
389      [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]].each do |id, t|
390       amf_content "getway_old", "/1", [id, t]
391       post :amf_read
392       assert_response :success
393       amf_parse_response
394       returned_way = amf_result("/1")
395       assert_equal -1, returned_way[0]
396       assert returned_way[3].nil?
397       assert returned_way[4].nil?
398       assert returned_way[5].nil?
399     end
400   end
401
402   def test_getway_old_invisible
403     way = create(:way, :deleted, :with_history, :version => 1)
404     v1 = way.old_ways.find_by(:version => 1)
405     # try to get deleted version
406     [[v1.way_id, (v1.timestamp + 10).strftime("%d %b %Y, %H:%M:%S")]].each do |id, t|
407       amf_content "getway_old", "/1", [id, t]
408       post :amf_read
409       assert_response :success
410       amf_parse_response
411       returned_way = amf_result("/1")
412       assert_equal -1, returned_way[0]
413       assert returned_way[3].nil?
414       assert returned_way[4].nil?
415       assert returned_way[5].nil?
416     end
417   end
418
419   def test_getway_history
420     latest = create(:way, :version => 2)
421     oldest = create(:old_way, :current_way => latest, :version => 1, :timestamp => latest.timestamp - 2.minutes)
422     create(:old_way, :current_way => latest, :version => 2, :timestamp => latest.timestamp)
423
424     amf_content "getway_history", "/1", [latest.id]
425     post :amf_read
426     assert_response :success
427     amf_parse_response
428     history = amf_result("/1")
429
430     # ['way',wayid,history]
431     assert_equal "way", history[0]
432     assert_equal latest.id, history[1]
433     # We use dates rather than version numbers here, because you might
434     # have moved a node within a way (i.e. way version not incremented).
435     # The timestamp is +1 because we say "give me the revision of 15:33:02",
436     # but that might actually include changes at 15:33:02.457.
437     assert_equal (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
438     assert_equal (oldest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
439   end
440
441   def test_getway_history_nonexistent
442     amf_content "getway_history", "/1", [0]
443     post :amf_read
444     assert_response :success
445     amf_parse_response
446     history = amf_result("/1")
447
448     # ['way',wayid,history]
449     assert_equal history[0], "way"
450     assert_equal history[1], 0
451     assert history[2].empty?
452   end
453
454   def test_getnode_history
455     node = create(:node, :version => 2)
456     node_v1 = create(:old_node, :current_node => node, :version => 1, :timestamp => 3.days.ago)
457     _node_v2 = create(:old_node, :current_node => node, :version => 2, :timestamp => 2.days.ago)
458     node_v3 = create(:old_node, :current_node => node, :version => 3, :timestamp => 1.day.ago)
459
460     amf_content "getnode_history", "/1", [node.id]
461     post :amf_read
462     assert_response :success
463     amf_parse_response
464     history = amf_result("/1")
465
466     # ['node',nodeid,history]
467     # note that (as per getway_history) we actually round up
468     # to the next second
469     assert_equal history[0], "node",
470                  'first element should be "node"'
471     assert_equal history[1], node.id,
472                  "second element should be the input node ID"
473     assert_equal history[2].first[0],
474                  (node_v3.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
475                  "first element in third element (array) should be the latest version"
476     assert_equal history[2].last[0],
477                  (node_v1.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
478                  "last element in third element (array) should be the initial version"
479   end
480
481   def test_getnode_history_nonexistent
482     amf_content "getnode_history", "/1", [0]
483     post :amf_read
484     assert_response :success
485     amf_parse_response
486     history = amf_result("/1")
487
488     # ['node',nodeid,history]
489     assert_equal history[0], "node"
490     assert_equal history[1], 0
491     assert history[2].empty?
492   end
493
494   def test_findgpx_bad_user
495     amf_content "findgpx", "/1", [1, "test@example.com:wrong"]
496     post :amf_read
497     assert_response :success
498     amf_parse_response
499     result = amf_result("/1")
500
501     assert_equal 2, result.length
502     assert_equal -1, result[0]
503     assert_match /must be logged in/, result[1]
504
505     blocked_user = create(:user)
506     create(:user_block, :user => blocked_user)
507     amf_content "findgpx", "/1", [1, "#{blocked_user.email}:test"]
508     post :amf_read
509     assert_response :success
510     amf_parse_response
511     result = amf_result("/1")
512
513     assert_equal 2, result.length
514     assert_equal -1, result[0]
515     assert_match /access to the API has been blocked/, result[1]
516   end
517
518   def test_findgpx_by_id
519     user = create(:user)
520     trace = create(:trace, :visibility => "private", :user => user)
521
522     amf_content "findgpx", "/1", [trace.id, "#{user.email}:test"]
523     post :amf_read
524     assert_response :success
525     amf_parse_response
526     result = amf_result("/1")
527
528     assert_equal 3, result.length
529     assert_equal 0, result[0]
530     assert_equal "", result[1]
531     traces = result[2]
532     assert_equal 1, traces.length
533     assert_equal 3, traces[0].length
534     assert_equal trace.id, traces[0][0]
535     assert_equal trace.name, traces[0][1]
536     assert_equal trace.description, traces[0][2]
537   end
538
539   def test_findgpx_by_name
540     amf_content "findgpx", "/1", ["Trace", "test@example.com: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     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "new" }, nil, "new", 1]
1322     post :amf_write
1323     assert_response :success
1324     amf_parse_response
1325     result = amf_result("/1")
1326     new_cs_id = result[2].to_i
1327
1328     assert_equal 3, result.size
1329     assert_equal 0, result[0]
1330     assert_equal "", result[1]
1331
1332     cs = Changeset.find(new_cs_id)
1333     assert_equal true, cs.is_open?
1334     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1335
1336     old_cs_id = new_cs_id
1337
1338     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "newer" }, old_cs_id, "newer", 1]
1339     post :amf_write
1340     assert_response :success
1341     amf_parse_response
1342     result = amf_result("/1")
1343     new_cs_id = result[2].to_i
1344
1345     assert_not_equal old_cs_id, new_cs_id
1346
1347     assert_equal 3, result.size
1348     assert_equal 0, result[0]
1349     assert_equal "", result[1]
1350
1351     cs = Changeset.find(old_cs_id)
1352     assert_equal false, cs.is_open?
1353     assert_equal({ "comment" => "newer", "source" => "new" }, cs.tags)
1354
1355     cs = Changeset.find(new_cs_id)
1356     assert_equal true, cs.is_open?
1357     assert_equal({ "comment" => "newer", "source" => "newer" }, cs.tags)
1358
1359     old_cs_id = new_cs_id
1360
1361     amf_content "startchangeset", "/1", ["test@example.com:test", {}, old_cs_id, "", 0]
1362     post :amf_write
1363     assert_response :success
1364     amf_parse_response
1365     result = amf_result("/1")
1366
1367     assert_equal 3, result.size
1368     assert_equal 0, result[0]
1369     assert_equal "", result[1]
1370     assert_nil result[2]
1371
1372     cs = Changeset.find(old_cs_id)
1373     assert_equal false, cs.is_open?
1374     assert_equal({ "comment" => "newer", "source" => "newer" }, cs.tags)
1375   end
1376
1377   # check that we can't close somebody elses changeset
1378   def test_startchangeset_invalid_wrong_user
1379     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "new" }, nil, "new", 1]
1380     post :amf_write
1381     assert_response :success
1382     amf_parse_response
1383     result = amf_result("/1")
1384     cs_id = result[2].to_i
1385
1386     assert_equal 3, result.size
1387     assert_equal 0, result[0]
1388     assert_equal "", result[1]
1389
1390     cs = Changeset.find(cs_id)
1391     assert_equal true, cs.is_open?
1392     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1393
1394     amf_content "startchangeset", "/1", ["test@openstreetmap.org:test", {}, cs_id, "delete", 0]
1395     post :amf_write
1396     assert_response :success
1397     amf_parse_response
1398     result = amf_result("/1")
1399
1400     assert_equal 2, result.size
1401     assert_equal -2, result[0]
1402     assert_equal "The user doesn't own that changeset", result[1]
1403
1404     cs = Changeset.find(cs_id)
1405     assert_equal true, cs.is_open?
1406     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1407   end
1408
1409   # check that invalid characters are stripped from changeset tags
1410   def test_startchangeset_invalid_xmlchar_comment
1411     invalid = "\035\022"
1412     comment = "foo#{invalid}bar"
1413
1414     amf_content "startchangeset", "/1", ["test@example.com:test", {}, nil, comment, 1]
1415     post :amf_write
1416     assert_response :success
1417     amf_parse_response
1418     result = amf_result("/1")
1419     new_cs_id = result[2].to_i
1420
1421     assert_equal 3, result.size
1422     assert_equal 0, result[0]
1423     assert_equal "", result[1]
1424
1425     cs = Changeset.find(new_cs_id)
1426     assert_equal true, cs.is_open?
1427     assert_equal({ "comment" => "foobar" }, cs.tags)
1428   end
1429
1430   private
1431
1432   # ************************************************************
1433   # AMF Helper functions
1434
1435   # Get the result record for the specified ID
1436   # It's an assertion FAIL if the record does not exist
1437   def amf_result(ref)
1438     assert @amf_result.key?("#{ref}/onResult")
1439     @amf_result["#{ref}/onResult"]
1440   end
1441
1442   # Encode the AMF message to invoke "target" with parameters as
1443   # the passed data. The ref is used to retrieve the results.
1444   def amf_content(target, ref, data)
1445     a, b = 1.divmod(256)
1446     c = StringIO.new
1447     c.write 0.chr + 0.chr   # version 0
1448     c.write 0.chr + 0.chr   # n headers
1449     c.write a.chr + b.chr   # n bodies
1450     c.write AMF.encodestring(target)
1451     c.write AMF.encodestring(ref)
1452     c.write [-1].pack("N")
1453     c.write AMF.encodevalue(data)
1454
1455     @request.env["RAW_POST_DATA"] = c.string
1456   end
1457
1458   # Parses the @response object as an AMF messsage.
1459   # The result is a hash of message_ref => data.
1460   # The attribute @amf_result is initialised to this hash.
1461   def amf_parse_response
1462     req = StringIO.new(@response.body)
1463
1464     req.read(2) # version
1465
1466     # parse through any headers
1467     headers = AMF.getint(req)        # Read number of headers
1468     headers.times do                 # Read each header
1469       AMF.getstring(req)             #  |
1470       req.getc                       #  | skip boolean
1471       AMF.getvalue(req)              #  |
1472     end
1473
1474     # parse through responses
1475     results = {}
1476     bodies = AMF.getint(req)         # Read number of bodies
1477     bodies.times do                  # Read each body
1478       message = AMF.getstring(req)   #  | get message name
1479       AMF.getstring(req)             #  | get index in response sequence
1480       AMF.getlong(req)               #  | get total size in bytes
1481       args = AMF.getvalue(req)       #  | get response (probably an array)
1482       results[message] = args
1483     end
1484     @amf_result = results
1485     results
1486   end
1487
1488   ##
1489   # given an array of bounding boxes (each an array of 4 floats), call the
1490   # AMF "whichways" controller for each and pass the result back to the
1491   # caller's block for assertion testing.
1492   def check_bboxes_are_bad(bboxes)
1493     bboxes.each do |bbox|
1494       amf_content "whichways", "/1", bbox
1495       post :amf_read
1496       assert_response :success
1497       amf_parse_response
1498
1499       # pass the response back to the caller's block to be tested
1500       # against what the caller expected.
1501       map = amf_result "/1"
1502       yield map, bbox
1503     end
1504   end
1505
1506   # this should be what AMF controller returns when the bbox of a
1507   # whichways request is invalid or too large.
1508   def assert_boundary_error(map, msg = nil, error_hint = nil)
1509     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1510     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1511   end
1512
1513   # this should be what AMF controller returns when the bbox of a
1514   # whichways_deleted request is invalid or too large.
1515   def assert_deleted_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 end