]> git.openstreetmap.org Git - rails.git/blob - test/functional/old_node_controller_test.rb
efef38ad18c19fb9c808f29db202cf12e2ca3b01
[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   ##
169   # test the redaction of an old version of a node, while not being
170   # authorised.
171   def test_redact_node_unauthorised
172     do_redact_node(nodes(:node_with_versions_v3),
173                    redactions(:example))
174     assert_response :unauthorized, "should need to be authenticated to redact."
175   end
176
177   ##
178   # test the redaction of an old version of a node, while being 
179   # authorised as a normal user.
180   def test_redact_node_normal_user
181     basic_authorization(users(:public_user).email, "test")
182
183     do_redact_node(nodes(:node_with_versions_v3),
184                    redactions(:example))
185     assert_response :forbidden, "should need to be moderator to redact."
186   end
187
188   ##
189   # test that, even as moderator, the current version of a node
190   # can't be redacted.
191   def test_redact_node_current_version
192     basic_authorization(users(:moderator_user).email, "test")
193
194     do_redact_node(nodes(:node_with_versions_v4),
195                    redactions(:example))
196     assert_response :forbidden, "shouldn't be OK to redact current version as moderator."
197   end    
198
199   ##
200   # test that redacted nodes aren't visible, regardless of 
201   # authorisation except as moderator...
202   def test_version_redacted
203     node = nodes(:redacted_node_redacted_version)
204
205     get :version, :id => node.node_id, :version => node.version
206     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
207
208     # not even to a logged-in user
209     basic_authorization(users(:public_user).email, "test")
210     get :version, :id => node.node_id, :version => node.version
211     assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
212   end
213
214   ##
215   # test that redacted nodes aren't visible in the history
216   def test_history_redacted
217     node = nodes(:redacted_node_redacted_version)
218
219     get :history, :id => node.node_id
220     assert_response :success, "Redaction shouldn't have stopped history working."
221     assert_select "osm node[id=#{node.node_id}][version=#{node.version}]", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history."
222
223     # not even to a logged-in user
224     basic_authorization(users(:public_user).email, "test")
225     get :version, :id => node.node_id, :version => node.version
226     get :history, :id => node.node_id
227     assert_response :success, "Redaction shouldn't have stopped history working."
228     assert_select "osm node[id=#{node.node_id}][version=#{node.version}]", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history, even when logged in."
229   end
230
231   ##
232   # test the redaction of an old version of a node, while being 
233   # authorised as a moderator.
234   def test_redact_node_moderator
235     node = nodes(:node_with_versions_v3)
236     basic_authorization(users(:moderator_user).email, "test")
237
238     do_redact_node(node, redactions(:example))
239     assert_response :success, "should be OK to redact old version as moderator."
240
241     # check moderator can still see the redacted data
242     get :version, :id => node.node_id, :version => node.version
243     assert_response :success, "After redaction, node should not be gone for moderator."
244     
245     # and when accessed via history
246     get :history, :id => node.node_id
247     assert_response :success, "Redaction shouldn't have stopped history working."
248     assert_select "osm node[id=#{node.node_id}][version=#{node.version}]", 1, "node #{node.node_id} version #{node.version} should still be present in the history for moderators."
249   end
250
251   # testing that if the moderator drops auth, he can't see the
252   # redacted stuff any more.
253   def test_redact_node_is_redacted
254     node = nodes(:node_with_versions_v3)
255     basic_authorization(users(:moderator_user).email, "test")
256
257     do_redact_node(node, redactions(:example))
258     assert_response :success, "should be OK to redact old version as moderator."
259
260     # re-auth as non-moderator
261     basic_authorization(users(:public_user).email, "test")
262
263     # check can't see the redacted data
264     get :version, :id => node.node_id, :version => node.version
265     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
266     
267     # and when accessed via history
268     get :version, :id => node.node_id, :version => node.version
269     get :history, :id => node.node_id
270     assert_response :success, "Redaction shouldn't have stopped history working."
271     assert_select "osm node[id=#{node.node_id}][version=#{node.version}]", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history."
272   end
273
274   def do_redact_node(node, redaction)
275     get :version, :id => node.node_id, :version => node.version
276     assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
277     
278     # now redact it
279     post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
280   end
281
282   def check_current_version(node_id)
283     # get the current version of the node
284     current_node = with_controller(NodeController.new) do
285       get :read, :id => node_id
286       assert_response :success, "cant get current node #{node_id}" 
287       Node.from_xml(@response.body)
288     end
289     assert_not_nil current_node, "getting node #{node_id} returned nil"
290
291     # get the "old" version of the node from the old_node interface
292     get :version, :id => node_id, :version => current_node.version
293     assert_response :success, "cant get old node #{node_id}, v#{current_node.version}" 
294     old_node = Node.from_xml(@response.body)
295
296     # check the nodes are the same
297     assert_nodes_are_equal current_node, old_node
298   end
299
300   ##
301   # returns a 16 character long string with some nasty characters in it.
302   # this ought to stress-test the tag handling as well as the versioning.
303   def random_string
304     letters = [['!','"','$','&',';','@'],
305                ('a'..'z').to_a,
306                ('A'..'Z').to_a,
307                ('0'..'9').to_a].flatten
308     (1..16).map { |i| letters[ rand(letters.length) ] }.join
309   end
310
311   ##
312   # truncate a floating point number to the scale that it is stored in
313   # the database. otherwise rounding errors can produce failing unit
314   # tests when they shouldn't.
315   def precision(f)
316     return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
317   end
318 end