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