]> git.openstreetmap.org Git - rails.git/blob - test/controllers/browse_controller_test.rb
Merge remote-tracking branch 'upstream/pull/4324'
[rails.git] / test / controllers / browse_controller_test.rb
1 require "test_helper"
2
3 class BrowseControllerTest < ActionDispatch::IntegrationTest
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/node/1", :method => :get },
9       { :controller => "browse", :action => "node", :id => "1" }
10     )
11     assert_routing(
12       { :path => "/node/1/history", :method => :get },
13       { :controller => "browse", :action => "node_history", :id => "1" }
14     )
15     assert_routing(
16       { :path => "/way/1", :method => :get },
17       { :controller => "browse", :action => "way", :id => "1" }
18     )
19     assert_routing(
20       { :path => "/way/1/history", :method => :get },
21       { :controller => "browse", :action => "way_history", :id => "1" }
22     )
23     assert_routing(
24       { :path => "/relation/1", :method => :get },
25       { :controller => "browse", :action => "relation", :id => "1" }
26     )
27     assert_routing(
28       { :path => "/relation/1/history", :method => :get },
29       { :controller => "browse", :action => "relation_history", :id => "1" }
30     )
31     assert_routing(
32       { :path => "/query", :method => :get },
33       { :controller => "browse", :action => "query" }
34     )
35   end
36
37   def test_read_relation
38     relation = create(:relation)
39     sidebar_browse_check :relation_path, relation.id, "browse/feature"
40     assert_select "h4", /^Version/ do
41       assert_select "a[href='#{old_relation_path relation, 1}']", :text => "1", :count => 1
42     end
43     assert_select ".secondary-actions a[href='#{api_relation_path relation}']", :count => 1
44     assert_select ".secondary-actions a[href='#{relation_history_path relation}']", :count => 1
45     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 0
46   end
47
48   def test_multiple_version_relation_links
49     relation = create(:relation, :with_history, :version => 2)
50     sidebar_browse_check :relation_path, relation.id, "browse/feature"
51     assert_select ".secondary-actions a[href='#{relation_history_path relation}']", :count => 1
52     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 1
53     assert_select ".secondary-actions a[href='#{old_relation_path relation, 2}']", :count => 1
54   end
55
56   def test_read_relation_history
57     relation = create(:relation, :with_history)
58     sidebar_browse_check :relation_history_path, relation.id, "browse/history"
59     assert_select "h4", /^Version/ do
60       assert_select "a[href='#{old_relation_path relation, 1}']", :text => "1", :count => 1
61     end
62   end
63
64   def test_read_way
65     way = create(:way)
66     sidebar_browse_check :way_path, way.id, "browse/feature"
67     assert_select "h4", /^Version/ do
68       assert_select "a[href='#{old_way_path way, 1}']", :text => "1", :count => 1
69     end
70     assert_select ".secondary-actions a[href='#{api_way_path way}']", :count => 1
71     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
72     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 0
73   end
74
75   def test_multiple_version_way_links
76     way = create(:way, :with_history, :version => 2)
77     sidebar_browse_check :way_path, way.id, "browse/feature"
78     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
79     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 1
80     assert_select ".secondary-actions a[href='#{old_way_path way, 2}']", :count => 1
81   end
82
83   def test_read_way_history
84     way = create(:way, :with_history)
85     sidebar_browse_check :way_history_path, way.id, "browse/history"
86     assert_select "h4", /^Version/ do
87       assert_select "a[href='#{old_way_path way, 1}']", :text => "1", :count => 1
88     end
89   end
90
91   def test_read_node
92     node = create(:node)
93     sidebar_browse_check :node_path, node.id, "browse/feature"
94     assert_select "h4", /^Version/ do
95       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
96     end
97     assert_select ".secondary-actions a[href='#{api_node_path node}']", :count => 1
98     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
99     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 0
100   end
101
102   def test_multiple_version_node_links
103     node = create(:node, :with_history, :version => 2)
104     sidebar_browse_check :node_path, node.id, "browse/feature"
105     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
106     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 1
107     assert_select ".secondary-actions a[href='#{old_node_path node, 2}']", :count => 1
108   end
109
110   def test_read_deleted_node
111     node = create(:node, :visible => false)
112     sidebar_browse_check :node_path, node.id, "browse/feature"
113     assert_select "h4", /^Version/ do
114       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
115     end
116     assert_select "a[href='#{api_node_path node}']", :count => 0
117   end
118
119   def test_read_node_history
120     node = create(:node, :with_history)
121     sidebar_browse_check :node_history_path, node.id, "browse/history"
122     assert_select "h4", /^Version/ do
123       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
124     end
125   end
126
127   ##
128   #  Methods to check redaction.
129   #
130   # note that these are presently highly reliant on the structure of the
131   # page for the selection tests, which doesn't work out particularly
132   # well if that structure changes. so... if you change the page layout
133   # then please make it more easily (and robustly) testable!
134   ##
135   def test_redacted_node
136     node = create(:node, :with_history, :deleted, :version => 2)
137     node_v1 = node.old_nodes.find_by(:version => 1)
138     node_v1.redact!(create(:redaction))
139
140     get node_path(:id => node)
141     assert_response :success
142     assert_template "feature"
143
144     # check that we don't show lat/lon for a redacted node.
145     assert_select ".browse-section", 1
146     assert_select ".browse-section.browse-node", 1
147     assert_select ".browse-section.browse-node .latitude", 0
148     assert_select ".browse-section.browse-node .longitude", 0
149   end
150
151   def test_redacted_node_history
152     node = create(:node, :with_history, :deleted, :version => 2)
153     node_v1 = node.old_nodes.find_by(:version => 1)
154     node_v1.redact!(create(:redaction))
155
156     get node_history_path(:id => node)
157     assert_response :success
158     assert_template "browse/history"
159
160     # there are 2 revisions of the redacted node, but only one
161     # should be showing details here.
162     assert_select ".browse-section", 2
163     assert_select ".browse-section.browse-redacted", 1
164     assert_select ".browse-section.browse-node", 1
165     assert_select ".browse-section.browse-node .latitude", 0
166     assert_select ".browse-section.browse-node .longitude", 0
167   end
168
169   def test_redacted_node_unredacted_history
170     session_for(create(:moderator_user))
171     node = create(:node, :with_history, :deleted, :version => 2)
172     node_v1 = node.old_nodes.find_by(:version => 1)
173     node_v1.redact!(create(:redaction))
174
175     get node_history_path(:id => node, :params => { :show_redactions => true })
176     assert_response :success
177     assert_template "browse/history"
178
179     assert_select ".browse-section", 2
180     assert_select ".browse-section.browse-redacted", 0
181     assert_select ".browse-section.browse-node", 2
182   end
183
184   def test_redacted_way_history
185     way = create(:way, :with_history, :version => 4)
186     way_v1 = way.old_ways.find_by(:version => 1)
187     way_v1.redact!(create(:redaction))
188     way_v3 = way.old_ways.find_by(:version => 3)
189     way_v3.redact!(create(:redaction))
190
191     get way_history_path(:id => way)
192     assert_response :success
193     assert_template "browse/history"
194
195     # there are 4 revisions of the redacted way, but only 2
196     # should be showing details here.
197     assert_select ".browse-section", 4
198     assert_select ".browse-section.browse-redacted", 2
199     assert_select ".browse-section.browse-way", 2
200   end
201
202   def test_redacted_way_unredacted_history
203     session_for(create(:moderator_user))
204     way = create(:way, :with_history, :version => 4)
205     way_v1 = way.old_ways.find_by(:version => 1)
206     way_v1.redact!(create(:redaction))
207     way_v3 = way.old_ways.find_by(:version => 3)
208     way_v3.redact!(create(:redaction))
209
210     get way_history_path(:id => way, :params => { :show_redactions => true })
211     assert_response :success
212     assert_template "browse/history"
213
214     assert_select ".browse-section", 4
215     assert_select ".browse-section.browse-redacted", 0
216     assert_select ".browse-section.browse-way", 4
217   end
218
219   def test_redacted_relation_history
220     relation = create(:relation, :with_history, :version => 4)
221     relation_v1 = relation.old_relations.find_by(:version => 1)
222     relation_v1.redact!(create(:redaction))
223     relation_v3 = relation.old_relations.find_by(:version => 3)
224     relation_v3.redact!(create(:redaction))
225
226     get relation_history_path(:id => relation)
227     assert_response :success
228     assert_template "browse/history"
229
230     # there are 4 revisions of the redacted relation, but only 2
231     # should be showing details here.
232     assert_select ".browse-section", 4
233     assert_select ".browse-section.browse-redacted", 2
234     assert_select ".browse-section.browse-relation", 2
235   end
236
237   def test_redacted_relation_unredacted_history
238     session_for(create(:moderator_user))
239     relation = create(:relation, :with_history, :version => 4)
240     relation_v1 = relation.old_relations.find_by(:version => 1)
241     relation_v1.redact!(create(:redaction))
242     relation_v3 = relation.old_relations.find_by(:version => 3)
243     relation_v3.redact!(create(:redaction))
244
245     get relation_history_path(:id => relation, :params => { :show_redactions => true })
246     assert_response :success
247     assert_template "browse/history"
248
249     assert_select ".browse-section", 4
250     assert_select ".browse-section.browse-redacted", 0
251     assert_select ".browse-section.browse-relation", 4
252   end
253
254   def test_query
255     get query_path
256     assert_response :success
257     assert_template "browse/query"
258   end
259
260   def test_anonymous_user_feature_page_secondary_actions
261     node = create(:node, :with_history)
262     get node_path(:id => node)
263     assert_response :success
264     assert_select ".secondary-actions a", :text => "View Details", :count => 0
265     assert_select ".secondary-actions a", :text => "View History", :count => 1
266     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
267   end
268
269   def test_regular_user_feature_page_secondary_actions
270     session_for(create(:user))
271     node = create(:node, :with_history)
272     get node_path(:id => node)
273     assert_response :success
274     assert_select ".secondary-actions a", :text => "View Details", :count => 0
275     assert_select ".secondary-actions a", :text => "View History", :count => 1
276     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
277   end
278
279   def test_moderator_user_feature_page_secondary_actions
280     session_for(create(:moderator_user))
281     node = create(:node, :with_history)
282     get node_path(:id => node)
283     assert_response :success
284     assert_select ".secondary-actions a", :text => "View Details", :count => 0
285     assert_select ".secondary-actions a", :text => "View History", :count => 1
286     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 1
287   end
288
289   def test_anonymous_user_history_page_secondary_actions
290     node = create(:node, :with_history)
291     get node_history_path(:id => node)
292     assert_response :success
293     assert_select ".secondary-actions a", :text => "View Details", :count => 1
294     assert_select ".secondary-actions a", :text => "View History", :count => 0
295     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
296   end
297
298   def test_regular_user_history_page_secondary_actions
299     session_for(create(:user))
300     node = create(:node, :with_history)
301     get node_history_path(:id => node)
302     assert_response :success
303     assert_select ".secondary-actions a", :text => "View Details", :count => 1
304     assert_select ".secondary-actions a", :text => "View History", :count => 0
305     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
306   end
307
308   def test_moderator_user_history_page_secondary_actions
309     session_for(create(:moderator_user))
310     node = create(:node, :with_history)
311     get node_history_path(:id => node)
312     assert_response :success
313     assert_select ".secondary-actions a", :text => "View Details", :count => 1
314     assert_select ".secondary-actions a", :text => "View History", :count => 0
315     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 1
316   end
317
318   def test_anonymous_user_unredacted_history_page_secondary_actions
319     node = create(:node, :with_history)
320     get node_history_path(:id => node, :params => { :show_redactions => true })
321     assert_response :redirect
322   end
323
324   def test_regular_user_unredacted_history_page_secondary_actions
325     session_for(create(:user))
326     node = create(:node, :with_history)
327     get node_history_path(:id => node, :params => { :show_redactions => true })
328     assert_response :redirect
329   end
330
331   def test_moderator_user_unredacted_history_page_secondary_actions
332     session_for(create(:moderator_user))
333     node = create(:node, :with_history)
334     get node_history_path(:id => node, :params => { :show_redactions => true })
335     assert_response :success
336     assert_select ".secondary-actions a", :text => "View Details", :count => 1
337     assert_select ".secondary-actions a", :text => "View History", :count => 1
338     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
339   end
340 end