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