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