]> git.openstreetmap.org Git - rails.git/blob - test/controllers/browse_controller_test.rb
Show unredacted element history
[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 => "/changeset/1", :method => :get },
33       { :controller => "browse", :action => "changeset", :id => "1" }
34     )
35     assert_routing(
36       { :path => "/query", :method => :get },
37       { :controller => "browse", :action => "query" }
38     )
39   end
40
41   def test_read_relation
42     relation = create(:relation)
43     browse_check :relation_path, relation.id, "browse/feature"
44     assert_select "h4", /^Version/ do
45       assert_select "a[href='#{old_relation_path relation, 1}']", :text => "1", :count => 1
46     end
47     assert_select ".secondary-actions a[href='#{api_relation_path relation}']", :count => 1
48     assert_select ".secondary-actions a[href='#{relation_history_path relation}']", :count => 1
49     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 0
50   end
51
52   def test_multiple_version_relation_links
53     relation = create(:relation, :with_history, :version => 2)
54     browse_check :relation_path, relation.id, "browse/feature"
55     assert_select ".secondary-actions a[href='#{relation_history_path relation}']", :count => 1
56     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 1
57     assert_select ".secondary-actions a[href='#{old_relation_path relation, 2}']", :count => 1
58   end
59
60   def test_read_relation_history
61     relation = create(:relation, :with_history)
62     browse_check :relation_history_path, relation.id, "browse/history"
63     assert_select "h4", /^Version/ do
64       assert_select "a[href='#{old_relation_path relation, 1}']", :text => "1", :count => 1
65     end
66   end
67
68   def test_read_way
69     way = create(:way)
70     browse_check :way_path, way.id, "browse/feature"
71     assert_select "h4", /^Version/ do
72       assert_select "a[href='#{old_way_path way, 1}']", :text => "1", :count => 1
73     end
74     assert_select ".secondary-actions a[href='#{api_way_path way}']", :count => 1
75     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
76     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 0
77   end
78
79   def test_multiple_version_way_links
80     way = create(:way, :with_history, :version => 2)
81     browse_check :way_path, way.id, "browse/feature"
82     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
83     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 1
84     assert_select ".secondary-actions a[href='#{old_way_path way, 2}']", :count => 1
85   end
86
87   def test_read_way_history
88     way = create(:way, :with_history)
89     browse_check :way_history_path, way.id, "browse/history"
90     assert_select "h4", /^Version/ do
91       assert_select "a[href='#{old_way_path way, 1}']", :text => "1", :count => 1
92     end
93   end
94
95   def test_read_node
96     node = create(:node)
97     browse_check :node_path, node.id, "browse/feature"
98     assert_select "h4", /^Version/ do
99       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
100     end
101     assert_select ".secondary-actions a[href='#{api_node_path node}']", :count => 1
102     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
103     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 0
104   end
105
106   def test_multiple_version_node_links
107     node = create(:node, :with_history, :version => 2)
108     browse_check :node_path, node.id, "browse/feature"
109     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
110     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 1
111     assert_select ".secondary-actions a[href='#{old_node_path node, 2}']", :count => 1
112   end
113
114   def test_read_deleted_node
115     node = create(:node, :visible => false)
116     browse_check :node_path, node.id, "browse/feature"
117     assert_select "h4", /^Version/ do
118       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
119     end
120     assert_select "a[href='#{api_node_path node}']", :count => 0
121   end
122
123   def test_read_node_history
124     node = create(:node, :with_history)
125     browse_check :node_history_path, node.id, "browse/history"
126     assert_select "h4", /^Version/ do
127       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
128     end
129   end
130
131   def test_read_changeset
132     user = create(:user)
133     changeset = create(:changeset, :user => user)
134     create(:changeset, :user => user)
135     browse_check :changeset_path, changeset.id, "browse/changeset"
136   end
137
138   def test_read_private_changeset
139     user = create(:user)
140     changeset = create(:changeset, :user => create(:user, :data_public => false))
141     create(:changeset, :user => user)
142     browse_check :changeset_path, changeset.id, "browse/changeset"
143   end
144
145   def test_read_changeset_element_links
146     changeset = create(:changeset)
147     node = create(:node, :with_history, :changeset => changeset)
148
149     browse_check :changeset_path, changeset.id, "browse/changeset"
150     assert_dom "a[href='#{node_path node}']", :count => 1
151     assert_dom "a[href='#{old_node_path node, 1}']", :count => 1
152   end
153
154   ##
155   #  Methods to check redaction.
156   #
157   # note that these are presently highly reliant on the structure of the
158   # page for the selection tests, which doesn't work out particularly
159   # well if that structure changes. so... if you change the page layout
160   # then please make it more easily (and robustly) testable!
161   ##
162   def test_redacted_node
163     node = create(:node, :with_history, :deleted, :version => 2)
164     node_v1 = node.old_nodes.find_by(:version => 1)
165     node_v1.redact!(create(:redaction))
166
167     get node_path(:id => node)
168     assert_response :success
169     assert_template "feature"
170
171     # check that we don't show lat/lon for a redacted node.
172     assert_select ".browse-section", 1
173     assert_select ".browse-section.browse-node", 1
174     assert_select ".browse-section.browse-node .latitude", 0
175     assert_select ".browse-section.browse-node .longitude", 0
176   end
177
178   def test_redacted_node_history
179     node = create(:node, :with_history, :deleted, :version => 2)
180     node_v1 = node.old_nodes.find_by(:version => 1)
181     node_v1.redact!(create(:redaction))
182
183     get node_history_path(:id => node)
184     assert_response :success
185     assert_template "browse/history"
186
187     # there are 2 revisions of the redacted node, but only one
188     # should be showing details here.
189     assert_select ".browse-section", 2
190     assert_select ".browse-section.browse-redacted", 1
191     assert_select ".browse-section.browse-node", 1
192     assert_select ".browse-section.browse-node .latitude", 0
193     assert_select ".browse-section.browse-node .longitude", 0
194   end
195
196   def test_redacted_node_unredacted_history
197     session_for(create(:moderator_user))
198     node = create(:node, :with_history, :deleted, :version => 2)
199     node_v1 = node.old_nodes.find_by(:version => 1)
200     node_v1.redact!(create(:redaction))
201
202     get node_history_path(:id => node, :params => { :show_redactions => true })
203     assert_response :success
204     assert_template "browse/history"
205
206     assert_select ".browse-section", 2
207     assert_select ".browse-section.browse-redacted", 0
208     assert_select ".browse-section.browse-node", 2
209   end
210
211   def test_redacted_way_history
212     way = create(:way, :with_history, :version => 4)
213     way_v1 = way.old_ways.find_by(:version => 1)
214     way_v1.redact!(create(:redaction))
215     way_v3 = way.old_ways.find_by(:version => 3)
216     way_v3.redact!(create(:redaction))
217
218     get way_history_path(:id => way)
219     assert_response :success
220     assert_template "browse/history"
221
222     # there are 4 revisions of the redacted way, but only 2
223     # should be showing details here.
224     assert_select ".browse-section", 4
225     assert_select ".browse-section.browse-redacted", 2
226     assert_select ".browse-section.browse-way", 2
227   end
228
229   def test_redacted_way_unredacted_history
230     session_for(create(:moderator_user))
231     way = create(:way, :with_history, :version => 4)
232     way_v1 = way.old_ways.find_by(:version => 1)
233     way_v1.redact!(create(:redaction))
234     way_v3 = way.old_ways.find_by(:version => 3)
235     way_v3.redact!(create(:redaction))
236
237     get way_history_path(:id => way, :params => { :show_redactions => true })
238     assert_response :success
239     assert_template "browse/history"
240
241     assert_select ".browse-section", 4
242     assert_select ".browse-section.browse-redacted", 0
243     assert_select ".browse-section.browse-way", 4
244   end
245
246   def test_redacted_relation_history
247     relation = create(:relation, :with_history, :version => 4)
248     relation_v1 = relation.old_relations.find_by(:version => 1)
249     relation_v1.redact!(create(:redaction))
250     relation_v3 = relation.old_relations.find_by(:version => 3)
251     relation_v3.redact!(create(:redaction))
252
253     get relation_history_path(:id => relation)
254     assert_response :success
255     assert_template "browse/history"
256
257     # there are 4 revisions of the redacted relation, but only 2
258     # should be showing details here.
259     assert_select ".browse-section", 4
260     assert_select ".browse-section.browse-redacted", 2
261     assert_select ".browse-section.browse-relation", 2
262   end
263
264   def test_redacted_relation_unredacted_history
265     session_for(create(:moderator_user))
266     relation = create(:relation, :with_history, :version => 4)
267     relation_v1 = relation.old_relations.find_by(:version => 1)
268     relation_v1.redact!(create(:redaction))
269     relation_v3 = relation.old_relations.find_by(:version => 3)
270     relation_v3.redact!(create(:redaction))
271
272     get relation_history_path(:id => relation, :params => { :show_redactions => true })
273     assert_response :success
274     assert_template "browse/history"
275
276     assert_select ".browse-section", 4
277     assert_select ".browse-section.browse-redacted", 0
278     assert_select ".browse-section.browse-relation", 4
279   end
280
281   def test_query
282     get query_path
283     assert_response :success
284     assert_template "browse/query"
285   end
286
287   def test_anonymous_user_feature_page_secondary_actions
288     node = create(:node, :with_history)
289     get node_path(:id => node)
290     assert_response :success
291     assert_select ".secondary-actions a", :text => "View Details", :count => 0
292     assert_select ".secondary-actions a", :text => "View History", :count => 1
293     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
294   end
295
296   def test_regular_user_feature_page_secondary_actions
297     session_for(create(:user))
298     node = create(:node, :with_history)
299     get node_path(:id => node)
300     assert_response :success
301     assert_select ".secondary-actions a", :text => "View Details", :count => 0
302     assert_select ".secondary-actions a", :text => "View History", :count => 1
303     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
304   end
305
306   def test_moderator_user_feature_page_secondary_actions
307     session_for(create(:moderator_user))
308     node = create(:node, :with_history)
309     get node_path(:id => node)
310     assert_response :success
311     assert_select ".secondary-actions a", :text => "View Details", :count => 0
312     assert_select ".secondary-actions a", :text => "View History", :count => 1
313     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 1
314   end
315
316   def test_anonymous_user_history_page_secondary_actions
317     node = create(:node, :with_history)
318     get node_history_path(:id => node)
319     assert_response :success
320     assert_select ".secondary-actions a", :text => "View Details", :count => 1
321     assert_select ".secondary-actions a", :text => "View History", :count => 0
322     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
323   end
324
325   def test_regular_user_history_page_secondary_actions
326     session_for(create(:user))
327     node = create(:node, :with_history)
328     get node_history_path(:id => node)
329     assert_response :success
330     assert_select ".secondary-actions a", :text => "View Details", :count => 1
331     assert_select ".secondary-actions a", :text => "View History", :count => 0
332     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
333   end
334
335   def test_moderator_user_history_page_secondary_actions
336     session_for(create(:moderator_user))
337     node = create(:node, :with_history)
338     get node_history_path(:id => node)
339     assert_response :success
340     assert_select ".secondary-actions a", :text => "View Details", :count => 1
341     assert_select ".secondary-actions a", :text => "View History", :count => 0
342     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 1
343   end
344
345   def test_anonymous_user_unredacted_history_page_secondary_actions
346     node = create(:node, :with_history)
347     get node_history_path(:id => node, :params => { :show_redactions => true })
348     assert_response :redirect
349   end
350
351   def test_regular_user_unredacted_history_page_secondary_actions
352     session_for(create(:user))
353     node = create(:node, :with_history)
354     get node_history_path(:id => node, :params => { :show_redactions => true })
355     assert_response :redirect
356   end
357
358   def test_moderator_user_unredacted_history_page_secondary_actions
359     session_for(create(:moderator_user))
360     node = create(:node, :with_history)
361     get node_history_path(:id => node, :params => { :show_redactions => true })
362     assert_response :success
363     assert_select ".secondary-actions a", :text => "View Details", :count => 1
364     assert_select ".secondary-actions a", :text => "View History", :count => 1
365     assert_select ".secondary-actions a", :text => "View Unredacted History", :count => 0
366   end
367
368   private
369
370   # This is a convenience method for most of the above checks
371   # First we check that when we don't have an id, it will correctly return a 404
372   # then we check that we get the correct 404 when a non-existant id is passed
373   # then we check that it will get a successful response, when we do pass an id
374   def browse_check(path, id, template)
375     path_method = method(path)
376
377     assert_raise ActionController::UrlGenerationError do
378       get path_method.call
379     end
380
381     assert_raise ActionController::UrlGenerationError do
382       get path_method.call(:id => -10) # we won't have an id that's negative
383     end
384
385     get path_method.call(:id => 0)
386     assert_response :not_found
387     assert_template "browse/not_found"
388     assert_template :layout => "map"
389
390     get path_method.call(:id => 0), :xhr => true
391     assert_response :not_found
392     assert_template "browse/not_found"
393     assert_template :layout => "xhr"
394
395     get path_method.call(:id => id)
396     assert_response :success
397     assert_template template
398     assert_template :layout => "map"
399
400     get path_method.call(:id => id), :xhr => true
401     assert_response :success
402     assert_template template
403     assert_template :layout => "xhr"
404   end
405 end