1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_node_controller'
4 # Re-raise errors caught by the controller.
5 class OldNodeController; def rescue_action(e) raise e end; end
7 class OldNodeControllerTest < Test::Unit::TestCase
11 @controller = OldNodeController.new
12 @request = ActionController::TestRequest.new
13 @response = ActionController::TestResponse.new
21 # test the version call by submitting several revisions of a new node
22 # to the API and ensuring that later calls to version return the
23 # matching versions of the object.
25 basic_authorization(users(:normal_user).email, "test")
26 changeset_id = changesets(:normal_user_first_change).id
28 # setup a simple XML node
29 xml_doc = current_nodes(:visible_node).to_xml
30 xml_node = xml_doc.find("//osm/node").first
31 nodeid = current_nodes(:visible_node).id
33 # keep a hash of the versions => string, as we'll need something
34 # to test against later
37 # save a version for later checking
38 versions[xml_node['version']] = xml_doc.to_s
40 # randomly move the node about
42 # move the node somewhere else
43 xml_node['lat'] = precision(rand * 180 - 90).to_s
44 xml_node['lon'] = precision(rand * 360 - 180).to_s
45 with_controller(NodeController.new) do
47 put :update, :id => nodeid
48 assert_response :success
49 xml_node['version'] = @response.body.to_s
51 # save a version for later checking
52 versions[xml_node['version']] = xml_doc.to_s
55 # add a bunch of random tags
57 xml_tag = XML::Node.new("tag")
58 xml_tag['k'] = random_string
59 xml_tag['v'] = random_string
61 with_controller(NodeController.new) do
63 put :update, :id => nodeid
64 assert_response :success,
65 "couldn't update node #{nodeid} (#{@response.body})"
66 xml_node['version'] = @response.body.to_s
68 # save a version for later checking
69 versions[xml_node['version']] = xml_doc.to_s
72 # check all the versions
73 versions.keys.each do |key|
74 get :version, :id => nodeid, :version => key.to_i
76 assert_response :success,
77 "couldn't get version #{key.to_i} of node #{nodeid}"
79 check_node = Node.from_xml(versions[key])
80 api_node = Node.from_xml(@response.body.to_s)
82 assert_nodes_are_equal check_node, api_node
87 # for some reason a==b is false, but there doesn't seem to be any
88 # difference between the nodes, so i'm checking all the attributes
89 # manually and blaming it on ActiveRecord
90 def assert_nodes_are_equal(a, b)
91 assert_equal a.id, b.id, "node IDs"
92 assert_equal a.latitude, b.latitude, "latitude"
93 assert_equal a.longitude, b.longitude, "longitude"
94 assert_equal a.changeset_id, b.changeset_id, "changeset ID"
95 assert_equal a.visible, b.visible, "visible"
96 assert_equal a.version, b.version, "version"
97 assert_equal a.tags, b.tags, "tags"
101 # returns a 16 character long string with some nasty characters in it.
102 # this ought to stress-test the tag handling as well as the versioning.
104 letters = [['!','"','$','&',';','@'],
107 ('0'..'9').to_a].flatten
108 (1..16).map { |i| letters[ rand(letters.length) ] }.join
112 # truncate a floating point number to the scale that it is stored in
113 # the database. otherwise rounding errors can produce failing unit
114 # tests when they shouldn't.
116 return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
119 def basic_authorization(user, pass)
120 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
124 @request.env["RAW_POST_DATA"] = c.to_s
128 # takes a block which is executed in the context of a different
129 # ActionController instance. this is used so that code can call methods
130 # on the node controller whilst testing the old_node controller.
131 def with_controller(new_controller)
132 controller_save = @controller
134 @controller = new_controller
137 @controller = controller_save