]> git.openstreetmap.org Git - rails.git/blob - test/functional/amf_controller_test.rb
fbc0af8836552feb457a7317696faf17f021addf
[rails.git] / test / functional / amf_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'stringio'
3 include Potlatch
4
5 class AmfControllerTest < ActionController::TestCase
6   api_fixtures
7
8   def test_getway
9     # check a visible way
10     id = current_ways(:visible_way).id
11     amf_content "getway", "/1", [id]
12     post :amf_read
13     assert_response :success
14     amf_parse_response                                         
15     way = amf_result("/1")
16     assert_equal way[0], 0
17     assert_equal way[2], id
18   end
19
20   def test_getway_invisible
21     # check an invisible way
22     id = current_ways(:invisible_way).id
23     amf_content "getway", "/1", [id]
24     post :amf_read
25     assert_response :success
26     amf_parse_response
27     way = amf_result("/1")
28     assert_equal way[0], -4
29     assert_equal way[1], "way"
30     assert_equal way[2], id
31     assert way[3].empty? and way[4].empty?
32   end
33
34   def test_getway_nonexistent
35     # check chat a non-existent way is not returned
36     amf_content "getway", "/1", [0]
37     post :amf_read
38     assert_response :success
39     amf_parse_response
40     way = amf_result("/1")
41     assert_equal way[0], -4
42     assert_equal way[1], "way"
43     assert_equal way[2], 0
44     assert way[3].empty? and way[4].empty?
45   end
46
47   def test_whichways
48     node = current_nodes(:used_node_1)
49     minlon = node.lon-0.1
50     minlat = node.lat-0.1
51     maxlon = node.lon+0.1
52     maxlat = node.lat+0.1
53     amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
54     post :amf_read
55     assert_response :success
56     amf_parse_response 
57
58     # check contents of message
59     map = amf_result "/1"
60     assert_equal 0, map[0], 'map error code should be 0'
61     assert_equal "", map[1], 'map error text should be empty'
62
63     # check the formatting of the message
64     assert_equal 5, map.length, 'map should have length 5'
65     assert_equal Array, map[2].class, 'map "ways" element should be an array'
66     assert_equal Array, map[3].class, 'map "nodes" element should be an array'
67     assert_equal Array, map[4].class, 'map "relations" element should be an array'
68     map[2].each do |w|
69       assert_equal 2, w.length, 'way should be (id, version) pair'
70       assert w[0] == w[0].floor, 'way ID should be an integer'
71       assert w[1] == w[1].floor, 'way version should be an integer'
72     end
73
74     map[3].each do |n|
75       assert_equal 5, w.length, 'node should be (id, lat, lon, [tags], version) tuple'
76       assert n[0] == n[0].floor, 'node ID should be an integer'
77       assert n[1] >= minlat - 0.01, 'node lat should be greater than min'
78       assert n[1] <= maxlat - 0.01, 'node lat should be less than max'
79       assert n[2] >= minlon - 0.01, 'node lon should be greater than min'
80       assert n[2] <= maxlon - 0.01, 'node lon should be less than max'
81       assert_equal Array, a[3].class, 'node tags should be array'
82       assert n[4] == n[4].floor, 'node version should be an integer'
83     end
84
85     map[4].each do |r|
86       assert_equal 2, r.length, 'relation should be (id, version) pair'
87       assert r[0] == r[0].floor, 'relation ID should be an integer'
88       assert r[1] == r[1].floor, 'relation version should be an integer'
89     end
90
91     # TODO: looks like amf_controller changed since this test was written
92     # so someone who knows what they're doing should check this!
93     ways = map[2].collect { |x| x[0] }
94     assert ways.include?(current_ways(:used_way).id),
95       "map should include used way"
96     assert !ways.include?(current_ways(:invisible_way).id),
97       'map should not include deleted way'
98   end
99
100   ##
101   # checks that too-large a bounding box will not be served.
102   def test_whichways_toobig
103     bbox = [-0.1,-0.1,1.1,1.1]
104     check_bboxes_are_bad [bbox] do |map,bbox|
105       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"
106     end
107   end
108
109   ##
110   # checks that an invalid bounding box will not be served. in this case
111   # one with max < min latitudes.
112   #
113   # NOTE: the controller expands the bbox by 0.01 in each direction!
114   def test_whichways_badlat
115     bboxes = [[0,0.1,0.1,0], [-0.1,80,0.1,70], [0.24,54.35,0.25,54.33]]
116     check_bboxes_are_bad bboxes do |map, bbox|
117       assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
118     end
119   end
120
121   ##
122   # same as test_whichways_badlat, but for longitudes
123   #
124   # NOTE: the controller expands the bbox by 0.01 in each direction!
125   def test_whichways_badlon
126     bboxes = [[80,-0.1,70,0.1], [54.35,0.24,54.33,0.25]]
127     check_bboxes_are_bad bboxes do |map, bbox|
128       assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
129     end
130   end
131
132   def test_whichways_deleted
133     node = current_nodes(:used_node_1)
134     minlon = node.lon-0.1
135     minlat = node.lat-0.1
136     maxlon = node.lon+0.1
137     maxlat = node.lat+0.1
138     amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
139     post :amf_read
140     assert_response :success
141     amf_parse_response
142
143     # check contents of message
144     map = amf_result "/1"
145     assert_equal 0, map[0], 'first map element should be 0'
146     assert_equal "", map[1], 'second map element should be an empty string'
147     assert_equal Array, map[2].class, 'third map element should be an array'
148     # TODO: looks like amf_controller changed since this test was written
149     # so someone who knows what they're doing should check this!
150     assert !map[2].include?(current_ways(:used_way).id),
151       "map should not include used way"
152     assert map[2].include?(current_ways(:invisible_way).id),
153       'map should include deleted way'
154   end
155
156   def test_whichways_deleted_toobig
157     bbox = [-0.1,-0.1,1.1,1.1]
158     amf_content "whichways_deleted", "/1", bbox
159     post :amf_read
160     assert_response :success
161     amf_parse_response 
162
163     map = amf_result "/1"
164     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"
165   end
166
167   def test_getrelation
168     id = current_relations(:visible_relation).id
169     amf_content "getrelation", "/1", [id]
170     post :amf_read
171     assert_response :success
172     amf_parse_response
173     rel = amf_result("/1")
174     assert_equal rel[0], 0
175     assert_equal rel[2], id
176   end
177
178   def test_getrelation_invisible
179     id = current_relations(:invisible_relation).id
180     amf_content "getrelation", "/1", [id]
181     post :amf_read
182     assert_response :success
183     amf_parse_response
184     rel = amf_result("/1")
185     assert_equal rel[0], -4
186     assert_equal rel[1], "relation"
187     assert_equal rel[2], id
188     assert rel[3].empty? and rel[4].empty?
189   end
190
191   def test_getrelation_nonexistent
192     id = 0
193     amf_content "getrelation", "/1", [id]
194     post :amf_read
195     assert_response :success
196     amf_parse_response
197     rel = amf_result("/1")
198     assert_equal rel[0], -4
199     assert_equal rel[1], "relation"
200     assert_equal rel[2], id
201     assert rel[3].empty? and rel[4].empty?
202   end
203
204   def test_getway_old
205     # try to get the last visible version (specified by <0) (should be current version)
206     latest = current_ways(:way_with_versions)
207     # NOTE: looks from the API changes that this now expects a timestamp
208     # instead of a version number...
209     # try to get version 1
210     v1 = ways(:way_with_versions_v1)
211     { latest => '', 
212       v1 => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
213     }.each do |way, t|
214       amf_content "getway_old", "/1", [way.id, t]
215       post :amf_read      
216       assert_response :success
217       amf_parse_response
218       returned_way = amf_result("/1")
219       assert_equal 0, returned_way[0]
220       assert_equal way.id, returned_way[2]
221       # API returns the *latest* version, even for old ways...
222       assert_equal latest.version, returned_way[5]
223     end
224   end
225   
226   ##
227   # test that the server doesn't fall over when rubbish is passed
228   # into the method args.
229   def test_getway_old_invalid
230     way_id = current_ways(:way_with_versions).id
231     { "foo"  => "bar",
232       way_id => "not a date",
233       way_id => "2009-03-25 00:00:00", # <- wrong format
234       way_id => "0 Jan 2009 00:00:00", # <- invalid date
235       -1     => "1 Jan 2009 00:00:00"  # <- invalid ID
236     }.each do |id, t|
237       amf_content "getway_old", "/1", [id, t]
238       post :amf_read
239       assert_response :success
240       amf_parse_response
241       returned_way = amf_result("/1")
242       assert_equal -1, returned_way[0]
243       assert returned_way[3].empty?
244       assert returned_way[4].empty?
245       assert returned_way[5].nil?
246     end
247   end
248
249   def test_getway_old_nonexistent
250     # try to get the last version+10 (shoudn't exist)
251     v1 = ways(:way_with_versions_v1)
252     # try to get last visible version of non-existent way
253     # try to get specific version of non-existent way
254     [[nil, ''], 
255      [nil, '1 Jan 1970, 00:00:00'], 
256      [v1, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
257     ].each do |way, t|
258       amf_content "getway_old", "/1", [way.nil? ? 0 : way.id, t]
259       post :amf_read
260       assert_response :success
261       amf_parse_response
262       returned_way = amf_result("/1")
263       assert_equal -1, returned_way[0]
264       assert returned_way[3].empty?
265       assert returned_way[4].empty?
266       assert returned_way[5].nil?
267     end
268   end
269
270   def test_getway_history
271     latest = current_ways(:way_with_versions)
272     oldest = ways(:way_with_versions_v1)
273
274     amf_content "getway_history", "/1", [latest.id]
275     post :amf_read
276     assert_response :success
277     amf_parse_response
278     history = amf_result("/1")
279
280     # ['way',wayid,history]
281     assert_equal 'way', history[0]
282     assert_equal latest.id, history[1] 
283     # We use dates rather than version numbers here, because you might
284     # have moved a node within a way (i.e. way version not incremented).
285     # The timestamp is +1 (timestamp.succ) because we say "give me the
286     # revision of 15:33:02", but that might actually include changes at
287     # 15:33:02.457.
288     assert_equal latest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
289     assert_equal oldest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
290   end
291
292   def test_getway_history_nonexistent
293     amf_content "getway_history", "/1", [0]
294     post :amf_read
295     assert_response :success
296     amf_parse_response
297     history = amf_result("/1")
298
299     # ['way',wayid,history]
300     assert_equal history[0], 'way'
301     assert_equal history[1], 0
302     assert history[2].empty?
303   end
304
305   def test_getnode_history
306     latest = current_nodes(:node_with_versions)
307     amf_content "getnode_history", "/1", [latest.id]
308     post :amf_read
309     assert_response :success
310     amf_parse_response
311     history = amf_result("/1")
312
313     # ['node',nodeid,history]
314     assert_equal history[0], 'node', 
315       'first element should be "node"'
316     assert_equal history[1], latest.id,
317       'second element should be the input node ID'
318     # NOTE: changed this test to match what amf_controller actually 
319     # outputs - which may or may not be what potlatch is expecting.
320     # someone who knows potlatch (i.e: richard f) should review this.
321     # NOTE2: wow - this is the second time this has changed in the
322     # API and the tests are being patched up. 
323     assert_equal history[2].first[0], 
324       latest.timestamp.strftime("%d %b %Y, %H:%M:%S"),
325       'first part of third element should be the latest version'
326     assert_equal history[2].last[0], 
327       nodes(:node_with_versions_v1).timestamp.strftime("%d %b %Y, %H:%M:%S"),
328       'second part of third element should be the initial version'
329   end
330
331   def test_getnode_history_nonexistent
332     amf_content "getnode_history", "/1", [0]
333     post :amf_read
334     assert_response :success
335     amf_parse_response
336     history = amf_result("/1")
337
338     # ['node',nodeid,history]
339     assert_equal history[0], 'node'
340     assert_equal history[1], 0
341     assert history[2].empty?
342   end
343
344   # ************************************************************
345   # AMF Write tests
346   def test_putpoi_update_valid
347     nd = current_nodes(:visible_node)
348     cs_id = changesets(:public_user_first_change).id
349     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
350     post :amf_write
351     assert_response :success
352     amf_parse_response
353     result = amf_result("/1")
354     
355     assert_equal 0, result[0]
356     assert_equal "", result[1]
357     assert_equal nd.id, result[2]
358     assert_equal nd.id, result[3]
359     assert_equal nd.version+1, result[4]
360     
361     # Now try to update again, with a different lat/lon, using the updated version number
362     lat = nd.lat+0.1
363     lon = nd.lon-0.1
364     amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
365     post :amf_write
366     assert_response :success
367     amf_parse_response
368     result = amf_result("/2")
369     
370     assert_equal 0, result[0]
371     assert_equal "", result[1]
372     assert_equal nd.id, result[2]
373     assert_equal nd.id, result[3]
374     assert_equal nd.version+2, result[4]
375   end
376   
377   # Check that we can create a no valid poi
378   # Using similar method for the node controller test
379   def test_putpoi_create_valid
380     # This node has no tags
381     nd = Node.new
382     # create a node with random lat/lon
383     lat = rand(100)-50 + rand
384     lon = rand(100)-50 + rand
385     # normal user has a changeset open
386     changeset = changesets(:public_user_first_change)
387     
388     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
389     post :amf_write
390     assert_response :success
391     amf_parse_response
392     result = amf_result("/1")
393     
394     # check the array returned by the amf
395     assert_equal 5, result.size
396     assert_equal 0, result[0], "expected to get the status ok from the amf"
397     assert_equal 0, result[2], "The old id should be 0"
398     assert result[3] > 0, "The new id should be greater than 0"
399     assert_equal 1, result[4], "The new version should be 1"
400     
401     # Finally check that the node that was saved has saved the data correctly 
402     # in both the current and history tables
403     # First check the current table
404     current_node = Node.find(result[3])
405     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
406     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
407     assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
408     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
409     # Now check the history table
410     historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
411     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
412     first_historic_node = historic_nodes.first
413     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
414     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
415     assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
416     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
417     
418     ####
419     # This node has some tags
420     tnd = Node.new
421     # create a node with random lat/lon
422     lat = rand(100)-50 + rand
423     lon = rand(100)-50 + rand
424     # normal user has a changeset open
425     changeset = changesets(:public_user_first_change)
426     
427     amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
428     post :amf_write
429     assert_response :success
430     amf_parse_response
431     result = amf_result("/2")
432
433     # check the array returned by the amf
434     assert_equal 5, result.size
435     assert_equal 0, result[0], "Expected to get the status ok in the amf"
436     assert_equal 0, result[2], "The old id should be 0"
437     assert result[3] > 0, "The new id should be greater than 0"
438     assert_equal 1, result[4], "The new version should be 1"
439     
440     # Finally check that the node that was saved has saved the data correctly 
441     # in both the current and history tables
442     # First check the current table
443     current_node = Node.find(result[3])
444     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
445     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
446     assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
447     assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
448     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
449     # Now check the history table
450     historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
451     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
452     first_historic_node = historic_nodes.first
453     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
454     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
455     assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
456     assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
457     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
458   end
459   
460   def test_putpoi_delete_valid
461     
462   end
463   
464   def test_putpoi_delete_already_deleted
465     
466   end
467   
468   def test_putpoi_delete_not_found
469     
470   end
471   
472   def test_putpoi_invalid_latlon
473     
474   end
475
476   # ************************************************************
477   # AMF Helper functions
478
479   # Get the result record for the specified ID
480   # It's an assertion FAIL if the record does not exist
481   def amf_result ref
482     assert @amf_result.has_key?("#{ref}/onResult")
483     @amf_result["#{ref}/onResult"]
484   end
485
486   # Encode the AMF message to invoke "target" with parameters as
487   # the passed data. The ref is used to retrieve the results.
488   def amf_content(target, ref, data)
489     a,b=1.divmod(256)
490     c = StringIO.new()
491     c.write 0.chr+0.chr   # version 0
492     c.write 0.chr+0.chr   # n headers
493     c.write a.chr+b.chr   # n bodies
494     c.write AMF.encodestring(target)
495     c.write AMF.encodestring(ref)
496     c.write [-1].pack("N")
497     c.write AMF.encodevalue(data)
498
499     @request.env["RAW_POST_DATA"] = c.string
500   end
501
502   # Parses the @response object as an AMF messsage.
503   # The result is a hash of message_ref => data.
504   # The attribute @amf_result is initialised to this hash.
505   def amf_parse_response
506     if @response.body.class.to_s == 'Proc'
507       res = StringIO.new()
508       @response.body.call @response, res
509       req = StringIO.new(res.string)
510     else
511       req = StringIO.new(@response.body)
512     end
513     req.read(2)   # version
514
515     # parse through any headers
516         headers=AMF.getint(req)                                 # Read number of headers
517         headers.times do                                                # Read each header
518           name=AMF.getstring(req)                               #  |
519           req.getc                                                              #  | skip boolean
520           value=AMF.getvalue(req)                               #  |
521         end
522
523     # parse through responses
524     results = {}
525     bodies=AMF.getint(req)                                      # Read number of bodies
526         bodies.times do                                                 # Read each body
527           message=AMF.getstring(req)                    #  | get message name
528           index=AMF.getstring(req)                              #  | get index in response sequence
529           bytes=AMF.getlong(req)                                #  | get total size in bytes
530           args=AMF.getvalue(req)                                #  | get response (probably an array)
531       results[message] = args
532     end
533     @amf_result = results
534     results
535   end
536
537   ##
538   # given an array of bounding boxes (each an array of 4 floats), call the
539   # AMF "whichways" controller for each and pass the result back to the
540   # caller's block for assertion testing.
541   def check_bboxes_are_bad(bboxes)
542     bboxes.each do |bbox|
543       amf_content "whichways", "/1", bbox
544       post :amf_read
545       assert_response :success
546       amf_parse_response
547
548       # pass the response back to the caller's block to be tested
549       # against what the caller expected.
550       map = amf_result "/1"
551       yield map, bbox
552     end
553   end
554   
555   # this should be what AMF controller returns when the bbox of a
556   # whichways request is invalid or too large.
557   def assert_boundary_error(map, msg=nil, error_hint=nil)
558     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
559     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
560   end
561
562   # this should be what AMF controller returns when the bbox of a
563   # whichways_deleted request is invalid or too large.
564   def assert_deleted_boundary_error(map, msg=nil, error_hint=nil)
565     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
566     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
567   end
568 end