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 # Test that getting the current version is identical to picking
88 # that version with the version URI call.
89 def test_current_version
90 check_current_version(current_nodes(:visible_node))
91 check_current_version(current_nodes(:used_node_1))
92 check_current_version(current_nodes(:used_node_2))
93 check_current_version(current_nodes(:node_used_by_relationship))
94 check_current_version(current_nodes(:node_with_versions))
97 def check_current_version(node_id)
98 # get the current version of the node
99 current_node = with_controller(NodeController.new) do
100 get :read, :id => node_id
101 assert_response :success, "cant get current node #{node_id}"
102 Node.from_xml(@response.body)
104 assert_not_nil current_node, "getting node #{node_id} returned nil"
106 # get the "old" version of the node from the old_node interface
107 get :version, :id => node_id, :version => current_node.version
108 assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
109 old_node = Node.from_xml(@response.body)
111 # check the nodes are the same
112 assert_nodes_are_equal current_node, old_node
116 # returns a 16 character long string with some nasty characters in it.
117 # this ought to stress-test the tag handling as well as the versioning.
119 letters = [['!','"','$','&',';','@'],
122 ('0'..'9').to_a].flatten
123 (1..16).map { |i| letters[ rand(letters.length) ] }.join
127 # truncate a floating point number to the scale that it is stored in
128 # the database. otherwise rounding errors can produce failing unit
129 # tests when they shouldn't.
131 return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
134 def basic_authorization(user, pass)
135 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
139 @request.env["RAW_POST_DATA"] = c.to_s