]> git.openstreetmap.org Git - rails.git/blob - test/functional/old_node_controller_test.rb
Cleaned up some unreachable code. Added first test on the 'old' node controller.
[rails.git] / test / functional / old_node_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_node_controller'
3
4 # Re-raise errors caught by the controller.
5 class OldNodeController; def rescue_action(e) raise e end; end
6
7 class OldNodeControllerTest < Test::Unit::TestCase
8   api_fixtures
9
10   def setup
11     @controller = OldNodeController.new
12     @request    = ActionController::TestRequest.new
13     @response   = ActionController::TestResponse.new
14   end
15
16   #
17   # TODO: test history
18   #
19
20   ##
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.
24   def test_version
25     basic_authorization(users(:normal_user).email, "test")
26     changeset_id = changesets(:normal_user_first_change).id
27
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
32
33     # keep a hash of the versions => string, as we'll need something
34     # to test against later
35     versions = Hash.new
36
37     # save a version for later checking
38     versions[xml_node['version']] = xml_doc.to_s
39
40     # randomly move the node about
41     20.times do 
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
46         content xml_doc
47         put :update, :id => nodeid
48         assert_response :success
49         xml_node['version'] = @response.body.to_s
50       end
51       # save a version for later checking
52       versions[xml_node['version']] = xml_doc.to_s
53     end
54
55     # add a bunch of random tags
56     30.times do 
57       xml_tag = XML::Node.new("tag")
58       xml_tag['k'] = random_string
59       xml_tag['v'] = random_string
60       xml_node << xml_tag
61       with_controller(NodeController.new) do
62         content xml_doc
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
67       end
68       # save a version for later checking
69       versions[xml_node['version']] = xml_doc.to_s
70     end
71
72     # check all the versions
73     versions.keys.each do |key|
74       get :version, :id => nodeid, :version => key.to_i
75
76       assert_response :success,
77          "couldn't get version #{key.to_i} of node #{nodeid}"
78
79       check_node = Node.from_xml(versions[key])
80       api_node = Node.from_xml(@response.body.to_s)
81
82       assert_nodes_are_equal check_node, api_node
83     end
84   end
85
86   ##
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"
98   end
99
100   ##
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.
103   def random_string
104     letters = [['!','"','$','&',';','@'],
105                ('a'..'z').to_a,
106                ('A'..'Z').to_a,
107                ('0'..'9').to_a].flatten
108     (1..16).map { |i| letters[ rand(letters.length) ] }.join
109   end
110
111   ##
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.
115   def precision(f)
116     return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
117   end
118
119   def basic_authorization(user, pass)
120     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
121   end
122
123   def content(c)
124     @request.env["RAW_POST_DATA"] = c.to_s
125   end
126
127   ##
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
133     begin
134       @controller = new_controller
135       yield
136     ensure
137       @controller = controller_save
138     end
139   end
140
141 end