]> git.openstreetmap.org Git - rails.git/blob - test/controllers/old_nodes_controller_test.rb
Simplify remaining link_to ... user_path in views
[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", :method => :get },
7       { :controller => "old_nodes", :action => "index", :id => "1" }
8     )
9     assert_routing(
10       { :path => "/node/1/history/2", :method => :get },
11       { :controller => "old_nodes", :action => "show", :id => "1", :version => "2" }
12     )
13   end
14
15   def test_history
16     node = create(:node, :with_history)
17     sidebar_browse_check :node_history_path, node.id, "browse/history"
18     assert_select "h4", /^Version/ do
19       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
20     end
21   end
22
23   def test_history_of_redacted
24     node = create(:node, :with_history, :deleted, :version => 2)
25     node_v1 = node.old_nodes.find_by(:version => 1)
26     node_v1.redact!(create(:redaction))
27
28     get node_history_path(:id => node)
29     assert_response :success
30     assert_template "browse/history"
31
32     # there are 2 revisions of the redacted node, but only one
33     # should be showing details here.
34     assert_select ".browse-section", 2
35     assert_select ".browse-section.browse-redacted", 1
36     assert_select ".browse-section.browse-node", 1
37     assert_select ".browse-section.browse-node .latitude", 0
38     assert_select ".browse-section.browse-node .longitude", 0
39   end
40
41   def test_unredacted_history_of_redacted
42     session_for(create(:moderator_user))
43     node = create(:node, :with_history, :deleted, :version => 2)
44     node_v1 = node.old_nodes.find_by(:version => 1)
45     node_v1.redact!(create(:redaction))
46
47     get node_history_path(:id => node, :params => { :show_redactions => true })
48     assert_response :success
49     assert_template "browse/history"
50
51     assert_select ".browse-section", 2
52     assert_select ".browse-section.browse-redacted", 0
53     assert_select ".browse-section.browse-node", 2
54   end
55
56   def test_anonymous_user_history_page_secondary_actions
57     node = create(:node, :with_history)
58     get node_history_path(:id => node)
59     assert_response :success
60     assert_select ".secondary-actions a", :text => "View Details", :count => 1
61     assert_select ".secondary-actions a", :text => "View History", :count => 0
62     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
63   end
64
65   def test_regular_user_history_page_secondary_actions
66     session_for(create(:user))
67     node = create(:node, :with_history)
68     get node_history_path(:id => node)
69     assert_response :success
70     assert_select ".secondary-actions a", :text => "View Details", :count => 1
71     assert_select ".secondary-actions a", :text => "View History", :count => 0
72     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
73   end
74
75   def test_moderator_user_history_page_secondary_actions
76     session_for(create(:moderator_user))
77     node = create(:node, :with_history)
78     get node_history_path(:id => node)
79     assert_response :success
80     assert_select ".secondary-actions a", :text => "View Details", :count => 1
81     assert_select ".secondary-actions a", :text => "View History", :count => 0
82     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 1
83   end
84
85   def test_anonymous_user_unredacted_history_page_secondary_actions
86     node = create(:node, :with_history)
87     get node_history_path(:id => node, :params => { :show_redactions => true })
88     assert_response :redirect
89   end
90
91   def test_regular_user_unredacted_history_page_secondary_actions
92     session_for(create(:user))
93     node = create(:node, :with_history)
94     get node_history_path(:id => node, :params => { :show_redactions => true })
95     assert_response :redirect
96   end
97
98   def test_moderator_user_unredacted_history_page_secondary_actions
99     session_for(create(:moderator_user))
100     node = create(:node, :with_history)
101     get node_history_path(:id => node, :params => { :show_redactions => true })
102     assert_response :success
103     assert_select ".secondary-actions a", :text => "View Details", :count => 1
104     assert_select ".secondary-actions a", :text => "View History", :count => 1
105     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
106   end
107
108   def test_visible_with_one_version
109     node = create(:node, :with_history)
110     get old_node_path(node, 1)
111     assert_response :success
112     assert_template "old_nodes/show"
113     assert_template :layout => "map"
114     assert_select "h4", /^Version/ do
115       assert_select "a[href='#{old_node_path node, 1}']", :count => 0
116     end
117     assert_select ".secondary-actions a[href='#{api_old_node_path node, 1}']", :count => 1
118     assert_select ".secondary-actions a[href='#{node_path node}']", :count => 1
119     assert_select ".secondary-actions a[href='#{old_node_path node, 1, :params => { :show_redactions => true }}']", :count => 0
120     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
121   end
122
123   def test_visible_with_two_versions
124     node = create(:node, :with_history, :version => 2)
125     get old_node_path(node, 1)
126     assert_response :success
127     assert_template "old_nodes/show"
128     assert_template :layout => "map"
129     assert_select "h4", /^Version/ do
130       assert_select "a[href='#{old_node_path node, 1}']", :count => 0
131     end
132     assert_select ".secondary-actions a[href='#{api_old_node_path node, 1}']", :count => 1
133     assert_select ".secondary-actions a[href='#{node_path node}']", :count => 1
134     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
135     assert_select ".secondary-actions a[href='#{old_node_path node, 2}']", :count => 1
136
137     get old_node_path(node, 2)
138     assert_response :success
139     assert_template "old_nodes/show"
140     assert_template :layout => "map"
141     assert_select "h4", /^Version/ do
142       assert_select "a[href='#{old_node_path node, 2}']", :count => 0
143     end
144     assert_select ".secondary-actions a[href='#{api_old_node_path node, 2}']", :count => 1
145     assert_select ".secondary-actions a[href='#{node_path node}']", :count => 1
146     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
147     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 1
148   end
149
150   test "show unrevealed redacted versions to anonymous users" do
151     node = create_redacted_node
152     get old_node_path(node, 1)
153     assert_response :success
154     assert_template "old_nodes/show"
155     assert_template :layout => "map"
156     assert_select "td", :text => "TOP SECRET", :count => 0
157     assert_select ".secondary-actions a[href='#{node_path node}']", :count => 1
158     assert_select ".secondary-actions a[href='#{old_node_path node, 1, :params => { :show_redactions => true }}']", :count => 0
159     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 0
160     assert_select ".secondary-actions a[href='#{api_old_node_path node, 1}']", :count => 0
161   end
162
163   test "show unrevealed redacted versions to regular users" do
164     session_for(create(:user))
165     node = create_redacted_node
166     get old_node_path(node, 1)
167     assert_response :success
168     assert_template "old_nodes/show"
169     assert_template :layout => "map"
170     assert_select "td", :text => "TOP SECRET", :count => 0
171     assert_select ".secondary-actions a[href='#{node_path node}']", :count => 1
172     assert_select ".secondary-actions a[href='#{old_node_path node, 1, :params => { :show_redactions => true }}']", :count => 0
173     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 0
174     assert_select ".secondary-actions a[href='#{api_old_node_path node, 1}']", :count => 0
175   end
176
177   test "show unrevealed redacted versions to moderators" do
178     session_for(create(:moderator_user))
179     node = create_redacted_node
180     get old_node_path(node, 1)
181     assert_response :success
182     assert_template "old_nodes/show"
183     assert_template :layout => "map"
184     assert_select "td", :text => "TOP SECRET", :count => 0
185     assert_select ".secondary-actions a[href='#{node_path node}']", :count => 1
186     assert_select ".secondary-actions a[href='#{old_node_path node, 1, :params => { :show_redactions => true }}']", :count => 1
187     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 0
188     assert_select ".secondary-actions a[href='#{api_old_node_path node, 1}']", :count => 0
189   end
190
191   test "don't reveal redacted versions to anonymous users" do
192     node = create_redacted_node
193     get old_node_path(node, 1, :params => { :show_redactions => true })
194     assert_response :redirect
195   end
196
197   test "don't reveal redacted versions to regular users" do
198     session_for(create(:user))
199     node = create_redacted_node
200     get old_node_path(node, 1, :params => { :show_redactions => true })
201     assert_response :redirect
202   end
203
204   test "reveal redacted versions to moderators" do
205     session_for(create(:moderator_user))
206     node = create_redacted_node
207     get old_node_path(node, 1, :params => { :show_redactions => true })
208     assert_response :success
209     assert_select "td", :text => "TOP SECRET", :count => 1
210     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 1
211   end
212
213   def test_not_found
214     get old_node_path(0, 0)
215     assert_response :not_found
216     assert_template "old_nodes/not_found"
217     assert_template :layout => "map"
218     assert_select "#sidebar_content", /node #0 version 0 could not be found/
219   end
220
221   private
222
223   def create_redacted_node
224     create(:node, :with_history, :version => 2) do |node|
225       node_v1 = node.old_nodes.find_by(:version => 1)
226       create(:old_node_tag, :old_node => node_v1, :k => "name", :v => "TOP SECRET")
227       node_v1.redact!(create(:redaction))
228     end
229   end
230 end