]> git.openstreetmap.org Git - rails.git/blob - test/functional/old_node_controller_test.rb
Update Potlatch 2 to 2.3-468-ge4a2053 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     assert_routing(
23       { :path => "/api/0.6/node/1/2/redact", :method => :post },
24       { :controller => "old_node", :action => "redact", :id => "1", :version => "2" }
25     )
26   end
27
28   ##
29   # test the version call by submitting several revisions of a new node
30   # to the API and ensuring that later calls to version return the 
31   # matching versions of the object.
32   #
33   ##
34   # FIXME Move this test to being an integration test since it spans multiple controllers 
35   def test_version
36     ## First try this with a non-public user
37     basic_authorization(users(:normal_user).email, "test")
38     changeset_id = changesets(:normal_user_first_change).id
39     
40     # setup a simple XML node
41     xml_doc = current_nodes(:visible_node).to_xml
42     xml_node = xml_doc.find("//osm/node").first
43     nodeid = current_nodes(:visible_node).id
44
45     # keep a hash of the versions => string, as we'll need something
46     # to test against later
47     versions = Hash.new
48
49     # save a version for later checking
50     versions[xml_node['version']] = xml_doc.to_s
51
52     # randomly move the node about
53     20.times do 
54       # move the node somewhere else
55       xml_node['lat'] = precision(rand * 180 -  90).to_s
56       xml_node['lon'] = precision(rand * 360 - 180).to_s
57       with_controller(NodeController.new) do
58         content xml_doc
59         put :update, :id => nodeid
60         assert_response :forbidden, "Should have rejected node update"
61         xml_node['version'] = @response.body.to_s
62       end
63       # save a version for later checking
64       versions[xml_node['version']] = xml_doc.to_s
65     end
66
67     # add a bunch of random tags
68     30.times do 
69       xml_tag = XML::Node.new("tag")
70       xml_tag['k'] = random_string
71       xml_tag['v'] = random_string
72       xml_node << xml_tag
73       with_controller(NodeController.new) do
74         content xml_doc
75         put :update, :id => nodeid
76         assert_response :forbidden,
77         "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
78         xml_node['version'] = @response.body.to_s
79       end
80       # save a version for later checking
81       versions[xml_node['version']] = xml_doc.to_s
82     end
83
84     # probably should check that they didn't get written to the database
85
86     
87     ## Now do it with the public user
88     basic_authorization(users(:public_user).email, "test")
89     changeset_id = changesets(:public_user_first_change).id
90
91     # setup a simple XML node
92     xml_doc = current_nodes(:node_with_versions).to_xml
93     xml_node = xml_doc.find("//osm/node").first
94     nodeid = current_nodes(:node_with_versions).id
95
96     # keep a hash of the versions => string, as we'll need something
97     # to test against later
98     versions = Hash.new
99
100     # save a version for later checking
101     versions[xml_node['version']] = xml_doc.to_s
102
103     # randomly move the node about
104     20.times do 
105       # move the node somewhere else
106       xml_node['lat'] = precision(rand * 180 -  90).to_s
107       xml_node['lon'] = precision(rand * 360 - 180).to_s
108       with_controller(NodeController.new) do
109         content xml_doc
110         put :update, :id => nodeid
111         assert_response :success
112         xml_node['version'] = @response.body.to_s
113       end
114       # save a version for later checking
115       versions[xml_node['version']] = xml_doc.to_s
116     end
117
118     # add a bunch of random tags
119     30.times do 
120       xml_tag = XML::Node.new("tag")
121       xml_tag['k'] = random_string
122       xml_tag['v'] = random_string
123       xml_node << xml_tag
124       with_controller(NodeController.new) do
125         content xml_doc
126         put :update, :id => nodeid
127         assert_response :success,
128         "couldn't update node #{nodeid} (#{@response.body})"
129         xml_node['version'] = @response.body.to_s
130       end
131       # save a version for later checking
132       versions[xml_node['version']] = xml_doc.to_s
133     end
134
135     # check all the versions
136     versions.keys.each do |key|
137       get :version, :id => nodeid, :version => key.to_i
138
139       assert_response :success,
140          "couldn't get version #{key.to_i} of node #{nodeid}"
141
142       check_node = Node.from_xml(versions[key])
143       api_node = Node.from_xml(@response.body.to_s)
144
145       assert_nodes_are_equal check_node, api_node
146     end
147   end
148   
149   def test_not_found_version
150     check_not_found_id_version(70000,312344)
151     check_not_found_id_version(-1, -13)
152     check_not_found_id_version(nodes(:visible_node).id, 24354)
153     check_not_found_id_version(24356,   nodes(:visible_node).version)
154   end
155   
156   def check_not_found_id_version(id, version)
157     get :version, :id => id, :version => version
158     assert_response :not_found
159   rescue ActionController::RoutingError => ex
160     assert_match /No route matches/, ex.to_s
161   end
162   
163   ##
164   # Test that getting the current version is identical to picking
165   # that version with the version URI call.
166   def test_current_version
167     check_current_version(current_nodes(:visible_node))
168     check_current_version(current_nodes(:used_node_1))
169     check_current_version(current_nodes(:used_node_2))
170     check_current_version(current_nodes(:node_used_by_relationship))
171     check_current_version(current_nodes(:node_with_versions))
172   end
173
174   ##
175   # test the redaction of an old version of a node, while not being
176   # authorised.
177   def test_redact_node_unauthorised
178     do_redact_node(nodes(:node_with_versions_v3),
179                    redactions(:example))
180     assert_response :unauthorized, "should need to be authenticated to redact."
181   end
182
183   ##
184   # test the redaction of an old version of a node, while being 
185   # authorised as a normal user.
186   def test_redact_node_normal_user
187     basic_authorization(users(:public_user).email, "test")
188
189     do_redact_node(nodes(:node_with_versions_v3),
190                    redactions(:example))
191     assert_response :forbidden, "should need to be moderator to redact."
192   end
193
194   ##
195   # test that, even as moderator, the current version of a node
196   # can't be redacted.
197   def test_redact_node_current_version
198     basic_authorization(users(:moderator_user).email, "test")
199
200     do_redact_node(nodes(:node_with_versions_v4),
201                    redactions(:example))
202     assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
203   end    
204
205   ##
206   # test that redacted nodes aren't visible, regardless of 
207   # authorisation except as moderator...
208   def test_version_redacted
209     node = nodes(:redacted_node_redacted_version)
210
211     get :version, :id => node.node_id, :version => node.version
212     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
213
214     # not even to a logged-in user
215     basic_authorization(users(:public_user).email, "test")
216     get :version, :id => node.node_id, :version => node.version
217     assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
218   end
219
220   ##
221   # test that redacted nodes aren't visible in the history
222   def test_history_redacted
223     node = nodes(:redacted_node_redacted_version)
224
225     get :history, :id => node.node_id
226     assert_response :success, "Redaction shouldn't have stopped history working."
227     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."
228
229     # not even to a logged-in user
230     basic_authorization(users(:public_user).email, "test")
231     get :history, :id => node.node_id
232     assert_response :success, "Redaction shouldn't have stopped history working."
233     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."
234   end
235
236   ##
237   # test the redaction of an old version of a node, while being 
238   # authorised as a moderator.
239   def test_redact_node_moderator
240     node = nodes(:node_with_versions_v3)
241     basic_authorization(users(:moderator_user).email, "test")
242
243     do_redact_node(node, redactions(:example))
244     assert_response :success, "should be OK to redact old version as moderator."
245
246     # check moderator can still see the redacted data, when passing
247     # the appropriate flag
248     get :version, :id => node.node_id, :version => node.version
249     assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
250     get :version, :id => node.node_id, :version => node.version, :show_redactions => 'true'
251     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
252     
253     # and when accessed via history
254     get :history, :id => node.node_id
255     assert_response :success, "Redaction shouldn't have stopped history working."
256     assert_select "osm node[id=#{node.node_id}][version=#{node.version}]", 0, "node #{node.node_id} version #{node.version} should not be present in the history for moderators when not passing flag."
257     get :history, :id => node.node_id, :show_redactions => 'true'
258     assert_response :success, "Redaction shouldn't have stopped history working."
259     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 when passing flag."
260   end
261
262   # testing that if the moderator drops auth, he can't see the
263   # redacted stuff any more.
264   def test_redact_node_is_redacted
265     node = nodes(:node_with_versions_v3)
266     basic_authorization(users(:moderator_user).email, "test")
267
268     do_redact_node(node, redactions(:example))
269     assert_response :success, "should be OK to redact old version as moderator."
270
271     # re-auth as non-moderator
272     basic_authorization(users(:public_user).email, "test")
273
274     # check can't see the redacted data
275     get :version, :id => node.node_id, :version => node.version
276     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
277     
278     # and when accessed via history
279     get :history, :id => node.node_id
280     assert_response :success, "Redaction shouldn't have stopped history working."
281     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."
282   end
283
284   def do_redact_node(node, redaction)
285     get :version, :id => node.node_id, :version => node.version
286     assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
287     
288     # now redact it
289     post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
290   end
291
292   def check_current_version(node_id)
293     # get the current version of the node
294     current_node = with_controller(NodeController.new) do
295       get :read, :id => node_id
296       assert_response :success, "cant get current node #{node_id}" 
297       Node.from_xml(@response.body)
298     end
299     assert_not_nil current_node, "getting node #{node_id} returned nil"
300
301     # get the "old" version of the node from the old_node interface
302     get :version, :id => node_id, :version => current_node.version
303     assert_response :success, "cant get old node #{node_id}, v#{current_node.version}" 
304     old_node = Node.from_xml(@response.body)
305
306     # check the nodes are the same
307     assert_nodes_are_equal current_node, old_node
308   end
309
310   ##
311   # returns a 16 character long string with some nasty characters in it.
312   # this ought to stress-test the tag handling as well as the versioning.
313   def random_string
314     letters = [['!','"','$','&',';','@'],
315                ('a'..'z').to_a,
316                ('A'..'Z').to_a,
317                ('0'..'9').to_a].flatten
318     (1..16).map { |i| letters[ rand(letters.length) ] }.join
319   end
320
321   ##
322   # truncate a floating point number to the scale that it is stored in
323   # the database. otherwise rounding errors can produce failing unit
324   # tests when they shouldn't.
325   def precision(f)
326     return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
327   end
328 end