]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_node_controller_test.rb
Use redactions factory for old_way controller tests
[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                    create(:redaction))
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     user = create(:user)
200     basic_authorization(user.email, "test")
201
202     do_redact_node(nodes(:node_with_versions_v3),
203                    create(:redaction, :user => user))
204     assert_response :forbidden, "should need to be moderator to redact."
205   end
206
207   ##
208   # test that, even as moderator, the current version of a node
209   # can't be redacted.
210   def test_redact_node_current_version
211     moderator_user = create(:moderator_user)
212     basic_authorization(moderator_user.email, "test")
213
214     do_redact_node(nodes(:node_with_versions_v4),
215                    create(:redaction, :user => moderator_user))
216     assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
217   end
218
219   ##
220   # test that redacted nodes aren't visible, regardless of
221   # authorisation except as moderator...
222   def test_version_redacted
223     node = nodes(:redacted_node_redacted_version)
224
225     get :version, :id => node.node_id, :version => node.version
226     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
227
228     # not even to a logged-in user
229     basic_authorization(users(:public_user).email, "test")
230     get :version, :id => node.node_id, :version => node.version
231     assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
232   end
233
234   ##
235   # test that redacted nodes aren't visible in the history
236   def test_history_redacted
237     node = nodes(:redacted_node_redacted_version)
238
239     get :history, :id => node.node_id
240     assert_response :success, "Redaction shouldn't have stopped history working."
241     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."
242
243     # not even to a logged-in user
244     basic_authorization(users(:public_user).email, "test")
245     get :history, :id => node.node_id
246     assert_response :success, "Redaction shouldn't have stopped history working."
247     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."
248   end
249
250   ##
251   # test the redaction of an old version of a node, while being
252   # authorised as a moderator.
253   def test_redact_node_moderator
254     moderator_user = create(:moderator_user)
255     node = nodes(:node_with_versions_v3)
256     basic_authorization(moderator_user.email, "test")
257
258     do_redact_node(node, create(:redaction, :user => moderator_user))
259     assert_response :success, "should be OK to redact old version as moderator."
260
261     # check moderator can still see the redacted data, when passing
262     # the appropriate flag
263     get :version, :id => node.node_id, :version => node.version
264     assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
265     get :version, :id => node.node_id, :version => node.version, :show_redactions => "true"
266     assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
267
268     # and when accessed via history
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, "node #{node.node_id} version #{node.version} should not be present in the history for moderators when not passing flag."
272     get :history, :id => node.node_id, :show_redactions => "true"
273     assert_response :success, "Redaction shouldn't have stopped history working."
274     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."
275   end
276
277   # testing that if the moderator drops auth, he can't see the
278   # redacted stuff any more.
279   def test_redact_node_is_redacted
280     moderator_user = create(:moderator_user)
281     node = nodes(:node_with_versions_v3)
282     basic_authorization(moderator_user.email, "test")
283
284     do_redact_node(node, create(:redaction, :user => moderator_user))
285     assert_response :success, "should be OK to redact old version as moderator."
286
287     # re-auth as non-moderator
288     basic_authorization(create(:user).email, "test")
289
290     # check can't see the redacted data
291     get :version, :id => node.node_id, :version => node.version
292     assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
293
294     # and when accessed via history
295     get :history, :id => node.node_id
296     assert_response :success, "Redaction shouldn't have stopped history working."
297     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."
298   end
299
300   ##
301   # test the unredaction of an old version of a node, while not being
302   # authorised.
303   def test_unredact_node_unauthorised
304     node = nodes(:redacted_node_redacted_version)
305
306     post :redact, :id => node.node_id, :version => node.version
307     assert_response :unauthorized, "should need to be authenticated to unredact."
308   end
309
310   ##
311   # test the unredaction of an old version of a node, while being
312   # authorised as a normal user.
313   def test_unredact_node_normal_user
314     user = create(:user)
315     node = nodes(:redacted_node_redacted_version)
316     basic_authorization(user.email, "test")
317
318     post :redact, :id => node.node_id, :version => node.version
319     assert_response :forbidden, "should need to be moderator to unredact."
320   end
321
322   ##
323   # test the unredaction of an old version of a node, while being
324   # authorised as a moderator.
325   def test_unredact_node_moderator
326     moderator_user = create(:moderator_user)
327     node = nodes(:redacted_node_redacted_version)
328     basic_authorization(moderator_user.email, "test")
329
330     post :redact, :id => node.node_id, :version => node.version
331     assert_response :success, "should be OK to redact old version as moderator."
332
333     # check moderator can now see the redacted data, when not
334     # passing the aspecial flag
335     get :version, :id => node.node_id, :version => node.version
336     assert_response :success, "After unredaction, node should not be gone for moderator."
337
338     # and when accessed via history
339     get :history, :id => node.node_id
340     assert_response :success, "Unredaction shouldn't have stopped history working."
341     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."
342
343     basic_authorization(create(:user).email, "test")
344
345     # check normal user can now see the redacted data
346     get :version, :id => node.node_id, :version => node.version
347     assert_response :success, "After unredaction, node should be visible to normal users."
348
349     # and when accessed via history
350     get :history, :id => node.node_id
351     assert_response :success, "Unredaction shouldn't have stopped history working."
352     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 normal users without passing flag."
353   end
354
355   private
356
357   def do_redact_node(node, redaction)
358     get :version, :id => node.node_id, :version => node.version
359     assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
360
361     # now redact it
362     post :redact, :id => node.node_id, :version => node.version, :redaction => redaction.id
363   end
364
365   def check_current_version(node_id)
366     # get the current version of the node
367     current_node = with_controller(NodeController.new) do
368       get :read, :id => node_id
369       assert_response :success, "cant get current node #{node_id}"
370       Node.from_xml(@response.body)
371     end
372     assert_not_nil current_node, "getting node #{node_id} returned nil"
373
374     # get the "old" version of the node from the old_node interface
375     get :version, :id => node_id, :version => current_node.version
376     assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
377     old_node = Node.from_xml(@response.body)
378
379     # check the nodes are the same
380     assert_nodes_are_equal current_node, old_node
381   end
382
383   ##
384   # returns a 16 character long string with some nasty characters in it.
385   # this ought to stress-test the tag handling as well as the versioning.
386   def random_string
387     letters = [["!", '"', "$", "&", ";", "@"],
388                ("a".."z").to_a,
389                ("A".."Z").to_a,
390                ("0".."9").to_a].flatten
391     (1..16).map { |_i| letters[rand(letters.length)] }.join
392   end
393
394   ##
395   # truncate a floating point number to the scale that it is stored in
396   # the database. otherwise rounding errors can produce failing unit
397   # tests when they shouldn't.
398   def precision(f)
399     (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
400   end
401
402   def propagate_tags(node, old_node)
403     node.tags.each do |k, v|
404       create(:old_node_tag, :old_node => old_node, :k => k, :v => v)
405     end
406   end
407 end