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