]> git.openstreetmap.org Git - rails.git/blob - test/functional/old_node_controller_test.rb
Update Potlatch 2 to 2.3-236-ge27f26d build
[rails.git] / test / functional / old_node_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_node_controller'
3
4 class OldNodeControllerTest < ActionController::TestCase
5   api_fixtures
6
7   #
8   # TODO: test history
9   #
10
11   ##
12   # test all routes which lead to this controller
13   def test_routes
14     assert_routing(
15       { :path => "/api/0.6/node/1/history", :method => :get },
16       { :controller => "old_node", :action => "history", :id => "1" }
17     )
18     assert_routing(
19       { :path => "/api/0.6/node/1/2", :method => :get },
20       { :controller => "old_node", :action => "version", :id => "1", :version => "2" }
21     )
22   end
23
24   ##
25   # test the version call by submitting several revisions of a new node
26   # to the API and ensuring that later calls to version return the 
27   # matching versions of the object.
28   #
29   ##
30   # FIXME Move this test to being an integration test since it spans multiple controllers 
31   def test_version
32     ## First try this with a non-public user
33     basic_authorization(users(:normal_user).email, "test")
34     changeset_id = changesets(:normal_user_first_change).id
35     
36     # setup a simple XML node
37     xml_doc = current_nodes(:visible_node).to_xml
38     xml_node = xml_doc.find("//osm/node").first
39     nodeid = current_nodes(:visible_node).id
40
41     # keep a hash of the versions => string, as we'll need something
42     # to test against later
43     versions = Hash.new
44
45     # save a version for later checking
46     versions[xml_node['version']] = xml_doc.to_s
47
48     # randomly move the node about
49     20.times do 
50       # move the node somewhere else
51       xml_node['lat'] = precision(rand * 180 -  90).to_s
52       xml_node['lon'] = precision(rand * 360 - 180).to_s
53       with_controller(NodeController.new) do
54         content xml_doc
55         put :update, :id => nodeid
56         assert_response :forbidden, "Should have rejected node update"
57         xml_node['version'] = @response.body.to_s
58       end
59       # save a version for later checking
60       versions[xml_node['version']] = xml_doc.to_s
61     end
62
63     # add a bunch of random tags
64     30.times do 
65       xml_tag = XML::Node.new("tag")
66       xml_tag['k'] = random_string
67       xml_tag['v'] = random_string
68       xml_node << xml_tag
69       with_controller(NodeController.new) do
70         content xml_doc
71         put :update, :id => nodeid
72         assert_response :forbidden,
73         "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
74         xml_node['version'] = @response.body.to_s
75       end
76       # save a version for later checking
77       versions[xml_node['version']] = xml_doc.to_s
78     end
79
80     # probably should check that they didn't get written to the database
81
82     
83     ## Now do it with the public user
84     basic_authorization(users(:public_user).email, "test")
85     changeset_id = changesets(:public_user_first_change).id
86
87     # setup a simple XML node
88     xml_doc = current_nodes(:node_with_versions).to_xml
89     xml_node = xml_doc.find("//osm/node").first
90     nodeid = current_nodes(:node_with_versions).id
91
92     # keep a hash of the versions => string, as we'll need something
93     # to test against later
94     versions = Hash.new
95
96     # save a version for later checking
97     versions[xml_node['version']] = xml_doc.to_s
98
99     # randomly move the node about
100     20.times do 
101       # move the node somewhere else
102       xml_node['lat'] = precision(rand * 180 -  90).to_s
103       xml_node['lon'] = precision(rand * 360 - 180).to_s
104       with_controller(NodeController.new) do
105         content xml_doc
106         put :update, :id => nodeid
107         assert_response :success
108         xml_node['version'] = @response.body.to_s
109       end
110       # save a version for later checking
111       versions[xml_node['version']] = xml_doc.to_s
112     end
113
114     # add a bunch of random tags
115     30.times do 
116       xml_tag = XML::Node.new("tag")
117       xml_tag['k'] = random_string
118       xml_tag['v'] = random_string
119       xml_node << xml_tag
120       with_controller(NodeController.new) do
121         content xml_doc
122         put :update, :id => nodeid
123         assert_response :success,
124         "couldn't update node #{nodeid} (#{@response.body})"
125         xml_node['version'] = @response.body.to_s
126       end
127       # save a version for later checking
128       versions[xml_node['version']] = xml_doc.to_s
129     end
130
131     # check all the versions
132     versions.keys.each do |key|
133       get :version, :id => nodeid, :version => key.to_i
134
135       assert_response :success,
136          "couldn't get version #{key.to_i} of node #{nodeid}"
137
138       check_node = Node.from_xml(versions[key])
139       api_node = Node.from_xml(@response.body.to_s)
140
141       assert_nodes_are_equal check_node, api_node
142     end
143   end
144   
145   def test_not_found_version
146     check_not_found_id_version(70000,312344)
147     check_not_found_id_version(-1, -13)
148     check_not_found_id_version(nodes(:visible_node).id, 24354)
149     check_not_found_id_version(24356,   nodes(:visible_node).version)
150   end
151   
152   def check_not_found_id_version(id, version)
153     get :version, :id => id, :version => version
154     assert_response :not_found
155   end
156   
157   ##
158   # Test that getting the current version is identical to picking
159   # that version with the version URI call.
160   def test_current_version
161     check_current_version(current_nodes(:visible_node))
162     check_current_version(current_nodes(:used_node_1))
163     check_current_version(current_nodes(:used_node_2))
164     check_current_version(current_nodes(:node_used_by_relationship))
165     check_current_version(current_nodes(:node_with_versions))
166   end
167   
168   def check_current_version(node_id)
169     # get the current version of the node
170     current_node = with_controller(NodeController.new) do
171       get :read, :id => node_id
172       assert_response :success, "cant get current node #{node_id}" 
173       Node.from_xml(@response.body)
174     end
175     assert_not_nil current_node, "getting node #{node_id} returned nil"
176
177     # get the "old" version of the node from the old_node interface
178     get :version, :id => node_id, :version => current_node.version
179     assert_response :success, "cant get old node #{node_id}, v#{current_node.version}" 
180     old_node = Node.from_xml(@response.body)
181
182     # check the nodes are the same
183     assert_nodes_are_equal current_node, old_node
184   end
185
186   ##
187   # returns a 16 character long string with some nasty characters in it.
188   # this ought to stress-test the tag handling as well as the versioning.
189   def random_string
190     letters = [['!','"','$','&',';','@'],
191                ('a'..'z').to_a,
192                ('A'..'Z').to_a,
193                ('0'..'9').to_a].flatten
194     (1..16).map { |i| letters[ rand(letters.length) ] }.join
195   end
196
197   ##
198   # truncate a floating point number to the scale that it is stored in
199   # the database. otherwise rounding errors can produce failing unit
200   # tests when they shouldn't.
201   def precision(f)
202     return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
203   end
204 end