]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_nodes_controller_test.rb
Don't show unredacted element versions to non-moderators
[rails.git] / test / controllers / old_nodes_controller_test.rb
1 require "test_helper"
2
3 class OldNodesControllerTest < ActionDispatch::IntegrationTest
4   def test_routes
5     assert_routing(
6       { :path => "/node/1/history/2", :method => :get },
7       { :controller => "old_nodes", :action => "show", :id => "1", :version => "2" }
8     )
9   end
10
11   def test_visible_with_one_version
12     node = create(:node, :with_history)
13     get old_node_path(node, 1)
14     assert_response :success
15     assert_template "old_nodes/show"
16     assert_template :layout => "map"
17     assert_select "h4", /^Version/ do
18       assert_select "a[href='#{old_node_path node, 1}']", :count => 0
19     end
20     assert_select ".secondary-actions a[href='#{node_version_path node, 1}']", :count => 1
21     assert_select ".secondary-actions a[href='#{node_path node}']", :count => 1
22     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
23   end
24
25   def test_visible_with_two_versions
26     node = create(:node, :with_history, :version => 2)
27     get old_node_path(node, 1)
28     assert_response :success
29     assert_template "old_nodes/show"
30     assert_template :layout => "map"
31     assert_select "h4", /^Version/ do
32       assert_select "a[href='#{old_node_path node, 1}']", :count => 0
33     end
34     assert_select ".secondary-actions a[href='#{node_version_path node, 1}']", :count => 1
35     assert_select ".secondary-actions a[href='#{node_path node}']", :count => 1
36     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
37     assert_select ".secondary-actions a[href='#{old_node_path node, 2}']", :count => 1
38
39     get old_node_path(node, 2)
40     assert_response :success
41     assert_template "old_nodes/show"
42     assert_template :layout => "map"
43     assert_select "h4", /^Version/ do
44       assert_select "a[href='#{old_node_path node, 2}']", :count => 0
45     end
46     assert_select ".secondary-actions a[href='#{node_version_path node, 2}']", :count => 1
47     assert_select ".secondary-actions a[href='#{node_path node}']", :count => 1
48     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
49     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 1
50   end
51
52   def test_redacted
53     node = create_redacted_node
54     get old_node_path(node, 1)
55     assert_response :success
56     assert_template "old_nodes/show"
57     assert_template :layout => "map"
58     assert_select ".secondary-actions a[href='#{node_path node}']", :count => 1
59     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 0
60     assert_select ".secondary-actions a[href='#{node_version_path node, 1}']", :count => 0
61   end
62
63   test "don't show redacted versions to anonymous users" do
64     node = create_redacted_node
65     get old_node_path(node, 1, :params => { :show_redactions => true })
66     assert_response :redirect
67   end
68
69   test "don't show redacted versions to regular users" do
70     session_for(create(:user))
71     node = create_redacted_node
72     get old_node_path(node, 1, :params => { :show_redactions => true })
73     assert_response :redirect
74   end
75
76   test "show redacted versions to moderators" do
77     session_for(create(:moderator_user))
78     node = create_redacted_node
79     get old_node_path(node, 1, :params => { :show_redactions => true })
80     assert_response :success
81   end
82
83   def test_not_found
84     get old_node_path(0, 0)
85     assert_response :not_found
86     assert_template "old_nodes/not_found"
87     assert_template :layout => "map"
88     assert_select "#sidebar_content", /node #0 version 0 could not be found/
89   end
90
91   private
92
93   def create_redacted_node
94     create(:node, :with_history, :deleted, :version => 2) do |node|
95       node_v1 = node.old_nodes.find_by(:version => 1)
96       node_v1.redact!(create(:redaction))
97     end
98   end
99 end