]> git.openstreetmap.org Git - rails.git/blob - test/controllers/browse_controller_test.rb
Merge remote-tracking branch 'upstream/pull/3419'
[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 => "/note/1", :method => :get },
37       { :controller => "browse", :action => "note", :id => "1" }
38     )
39     assert_routing(
40       { :path => "/note/new", :method => :get },
41       { :controller => "browse", :action => "new_note" }
42     )
43     assert_routing(
44       { :path => "/query", :method => :get },
45       { :controller => "browse", :action => "query" }
46     )
47   end
48
49   def test_read_relation
50     browse_check :relation_path, create(:relation).id, "browse/feature"
51   end
52
53   def test_read_relation_history
54     browse_check :relation_history_path, create(:relation, :with_history).id, "browse/history"
55   end
56
57   def test_read_way
58     browse_check :way_path, create(:way).id, "browse/feature"
59   end
60
61   def test_read_way_history
62     browse_check :way_history_path, create(:way, :with_history).id, "browse/history"
63   end
64
65   def test_read_node
66     browse_check :node_path, create(:node).id, "browse/feature"
67   end
68
69   def test_read_node_history
70     browse_check :node_history_path, create(:node, :with_history).id, "browse/history"
71   end
72
73   def test_read_changeset
74     user = create(:user)
75     changeset = create(:changeset, :user => user)
76     create(:changeset, :user => user)
77     browse_check :changeset_path, changeset.id, "browse/changeset"
78   end
79
80   def test_read_private_changeset
81     user = create(:user)
82     changeset = create(:changeset, :user => create(:user, :data_public => false))
83     create(:changeset, :user => user)
84     browse_check :changeset_path, changeset.id, "browse/changeset"
85   end
86
87   def test_read_changeset_hidden_comments
88     changeset = create(:changeset)
89     create_list(:changeset_comment, 3, :changeset => changeset)
90     create(:changeset_comment, :visible => false, :changeset => changeset)
91
92     browse_check :changeset_path, changeset.id, "browse/changeset"
93     assert_select "div.changeset-comments ul li", :count => 3
94
95     session_for(create(:moderator_user))
96
97     browse_check :changeset_path, changeset.id, "browse/changeset"
98     assert_select "div.changeset-comments ul li", :count => 4
99   end
100
101   def test_read_note
102     open_note = create(:note_with_comments)
103
104     browse_check :browse_note_path, open_note.id, "browse/note"
105   end
106
107   def test_read_hidden_note
108     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
109
110     get browse_note_path(:id => hidden_note_with_comment)
111     assert_response :not_found
112     assert_template "browse/not_found"
113     assert_template :layout => "map"
114
115     get browse_note_path(:id => hidden_note_with_comment), :xhr => true
116     assert_response :not_found
117     assert_template "browse/not_found"
118     assert_template :layout => "xhr"
119
120     session_for(create(:moderator_user))
121
122     browse_check :browse_note_path, hidden_note_with_comment.id, "browse/note"
123   end
124
125   def test_read_note_hidden_comments
126     note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
127       create(:note_comment, :note => note, :visible => false)
128     end
129
130     browse_check :browse_note_path, note_with_hidden_comment.id, "browse/note"
131     assert_select "div.note-comments ul li", :count => 1
132
133     session_for(create(:moderator_user))
134
135     browse_check :browse_note_path, note_with_hidden_comment.id, "browse/note"
136     assert_select "div.note-comments ul li", :count => 2
137   end
138
139   def test_read_note_hidden_user_comment
140     hidden_user = create(:user, :deleted)
141     note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
142       create(:note_comment, :note => note, :author => hidden_user)
143     end
144
145     browse_check :browse_note_path, note_with_hidden_user_comment.id, "browse/note"
146     assert_select "div.note-comments ul li", :count => 1
147
148     session_for(create(:moderator_user))
149
150     browse_check :browse_note_path, note_with_hidden_user_comment.id, "browse/note"
151     assert_select "div.note-comments ul li", :count => 1
152   end
153
154   def test_read_closed_note
155     user = create(:user)
156     closed_note = create(:note_with_comments, :status => "closed", :closed_at => Time.now, :comments_count => 2) do |note|
157       create(:note_comment, :event => "closed", :note => note, :author => user)
158     end
159
160     browse_check :browse_note_path, closed_note.id, "browse/note"
161     assert_select "div.note-comments ul li", :count => 2
162     assert_select "div.details", /Resolved by #{user.display_name}/
163
164     user.soft_destroy!
165
166     reset!
167
168     browse_check :browse_note_path, closed_note.id, "browse/note"
169     assert_select "div.note-comments ul li", :count => 1
170     assert_select "div.details", /Resolved by deleted/
171   end
172
173   ##
174   #  Methods to check redaction.
175   #
176   # note that these are presently highly reliant on the structure of the
177   # page for the selection tests, which doesn't work out particularly
178   # well if that structure changes. so... if you change the page layout
179   # then please make it more easily (and robustly) testable!
180   ##
181   def test_redacted_node
182     node = create(:node, :with_history, :deleted, :version => 2)
183     node_v1 = node.old_nodes.find_by(:version => 1)
184     node_v1.redact!(create(:redaction))
185
186     get node_path(:id => node)
187     assert_response :success
188     assert_template "feature"
189
190     # check that we don't show lat/lon for a redacted node.
191     assert_select ".browse-section", 1
192     assert_select ".browse-section.browse-node", 1
193     assert_select ".browse-section.browse-node .latitude", 0
194     assert_select ".browse-section.browse-node .longitude", 0
195   end
196
197   def test_redacted_node_history
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)
203     assert_response :success
204     assert_template "browse/history"
205
206     # there are 2 revisions of the redacted node, but only one
207     # should be showing details here.
208     assert_select ".browse-section", 2
209     assert_select ".browse-section.browse-redacted", 1
210     assert_select ".browse-section.browse-node", 1
211     assert_select ".browse-section.browse-node .latitude", 0
212     assert_select ".browse-section.browse-node .longitude", 0
213   end
214
215   def test_redacted_way_history
216     way = create(:way, :with_history, :version => 4)
217     way_v1 = way.old_ways.find_by(:version => 1)
218     way_v1.redact!(create(:redaction))
219     way_v3 = way.old_ways.find_by(:version => 3)
220     way_v3.redact!(create(:redaction))
221
222     get way_history_path(:id => way)
223     assert_response :success
224     assert_template "browse/history"
225
226     # there are 4 revisions of the redacted way, but only 2
227     # should be showing details here.
228     assert_select ".browse-section", 4
229     assert_select ".browse-section.browse-redacted", 2
230     assert_select ".browse-section.browse-way", 2
231   end
232
233   def test_redacted_relation_history
234     relation = create(:relation, :with_history, :version => 4)
235     relation_v1 = relation.old_relations.find_by(:version => 1)
236     relation_v1.redact!(create(:redaction))
237     relation_v3 = relation.old_relations.find_by(:version => 3)
238     relation_v3.redact!(create(:redaction))
239
240     get relation_history_path(:id => relation)
241     assert_response :success
242     assert_template "browse/history"
243
244     # there are 4 revisions of the redacted relation, but only 2
245     # should be showing details here.
246     assert_select ".browse-section", 4
247     assert_select ".browse-section.browse-redacted", 2
248     assert_select ".browse-section.browse-relation", 2
249   end
250
251   def test_new_note
252     get note_new_path
253     assert_response :success
254     assert_template "browse/new_note"
255   end
256
257   def test_query
258     get query_path
259     assert_response :success
260     assert_template "browse/query"
261   end
262
263   private
264
265   # This is a convenience method for most of the above checks
266   # First we check that when we don't have an id, it will correctly return a 404
267   # then we check that we get the correct 404 when a non-existant id is passed
268   # then we check that it will get a successful response, when we do pass an id
269   def browse_check(path, id, template)
270     path_method = method(path)
271
272     assert_raise ActionController::UrlGenerationError do
273       get path_method.call
274     end
275
276     assert_raise ActionController::UrlGenerationError do
277       get path_method.call(:id => -10) # we won't have an id that's negative
278     end
279
280     get path_method.call(:id => 0)
281     assert_response :not_found
282     assert_template "browse/not_found"
283     assert_template :layout => "map"
284
285     get path_method.call(:id => 0), :xhr => true
286     assert_response :not_found
287     assert_template "browse/not_found"
288     assert_template :layout => "xhr"
289
290     get path_method.call(:id => id)
291     assert_response :success
292     assert_template template
293     assert_template :layout => "map"
294
295     get path_method.call(:id => id), :xhr => true
296     assert_response :success
297     assert_template template
298     assert_template :layout => "xhr"
299   end
300 end