3 class BrowseControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/node/1", :method => :get },
9 { :controller => "browse", :action => "node", :id => "1" }
12 { :path => "/node/1/history", :method => :get },
13 { :controller => "browse", :action => "node_history", :id => "1" }
16 { :path => "/way/1", :method => :get },
17 { :controller => "browse", :action => "way", :id => "1" }
20 { :path => "/way/1/history", :method => :get },
21 { :controller => "browse", :action => "way_history", :id => "1" }
24 { :path => "/relation/1", :method => :get },
25 { :controller => "browse", :action => "relation", :id => "1" }
28 { :path => "/relation/1/history", :method => :get },
29 { :controller => "browse", :action => "relation_history", :id => "1" }
32 { :path => "/changeset/1", :method => :get },
33 { :controller => "browse", :action => "changeset", :id => "1" }
36 { :path => "/note/1", :method => :get },
37 { :controller => "browse", :action => "note", :id => "1" }
40 { :path => "/query", :method => :get },
41 { :controller => "browse", :action => "query" }
45 def test_read_relation
46 browse_check :relation_path, create(:relation).id, "browse/feature"
49 def test_read_relation_history
50 browse_check :relation_history_path, create(:relation, :with_history).id, "browse/history"
54 browse_check :way_path, create(:way).id, "browse/feature"
57 def test_read_way_history
58 browse_check :way_history_path, create(:way, :with_history).id, "browse/history"
63 browse_check :node_path, node.id, "browse/feature"
64 assert_select "a[href='#{api_node_path node}']", :count => 1
67 def test_read_deleted_node
68 node = create :node, :visible => false
69 browse_check :node_path, node.id, "browse/feature"
70 assert_select "a[href='#{api_node_path node}']", :count => 0
73 def test_read_node_history
74 browse_check :node_history_path, create(:node, :with_history).id, "browse/history"
77 def test_read_changeset
79 changeset = create(:changeset, :user => user)
80 create(:changeset, :user => user)
81 browse_check :changeset_path, changeset.id, "browse/changeset"
84 def test_read_private_changeset
86 changeset = create(:changeset, :user => create(:user, :data_public => false))
87 create(:changeset, :user => user)
88 browse_check :changeset_path, changeset.id, "browse/changeset"
91 def test_read_changeset_hidden_comments
92 changeset = create(:changeset)
93 create_list(:changeset_comment, 3, :changeset => changeset)
94 create(:changeset_comment, :visible => false, :changeset => changeset)
96 browse_check :changeset_path, changeset.id, "browse/changeset"
97 assert_select "div.changeset-comments ul li", :count => 3
99 session_for(create(:moderator_user))
101 browse_check :changeset_path, changeset.id, "browse/changeset"
102 assert_select "div.changeset-comments ul li", :count => 4
106 open_note = create(:note_with_comments)
108 browse_check :browse_note_path, open_note.id, "browse/note"
111 def test_read_hidden_note
112 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
114 get browse_note_path(:id => hidden_note_with_comment)
115 assert_response :not_found
116 assert_template "browse/not_found"
117 assert_template :layout => "map"
119 get browse_note_path(:id => hidden_note_with_comment), :xhr => true
120 assert_response :not_found
121 assert_template "browse/not_found"
122 assert_template :layout => "xhr"
124 session_for(create(:moderator_user))
126 browse_check :browse_note_path, hidden_note_with_comment.id, "browse/note"
129 def test_read_note_hidden_comments
130 note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
131 create(:note_comment, :note => note, :visible => false)
134 browse_check :browse_note_path, note_with_hidden_comment.id, "browse/note"
135 assert_select "div.note-comments ul li", :count => 1
137 session_for(create(:moderator_user))
139 browse_check :browse_note_path, note_with_hidden_comment.id, "browse/note"
140 assert_select "div.note-comments ul li", :count => 2
143 def test_read_note_hidden_user_comment
144 hidden_user = create(:user, :deleted)
145 note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
146 create(:note_comment, :note => note, :author => hidden_user)
149 browse_check :browse_note_path, note_with_hidden_user_comment.id, "browse/note"
150 assert_select "div.note-comments ul li", :count => 1
152 session_for(create(:moderator_user))
154 browse_check :browse_note_path, note_with_hidden_user_comment.id, "browse/note"
155 assert_select "div.note-comments ul li", :count => 1
158 def test_read_closed_note
160 closed_note = create(:note_with_comments, :status => "closed", :closed_at => Time.now.utc, :comments_count => 2) do |note|
161 create(:note_comment, :event => "closed", :note => note, :author => user)
164 browse_check :browse_note_path, closed_note.id, "browse/note"
165 assert_select "div.note-comments ul li", :count => 2
166 assert_select "div.details", /Resolved by #{user.display_name}/
172 browse_check :browse_note_path, closed_note.id, "browse/note"
173 assert_select "div.note-comments ul li", :count => 1
174 assert_select "div.details", /Resolved by deleted/
178 # Methods to check redaction.
180 # note that these are presently highly reliant on the structure of the
181 # page for the selection tests, which doesn't work out particularly
182 # well if that structure changes. so... if you change the page layout
183 # then please make it more easily (and robustly) testable!
185 def test_redacted_node
186 node = create(:node, :with_history, :deleted, :version => 2)
187 node_v1 = node.old_nodes.find_by(:version => 1)
188 node_v1.redact!(create(:redaction))
190 get node_path(:id => node)
191 assert_response :success
192 assert_template "feature"
194 # check that we don't show lat/lon for a redacted node.
195 assert_select ".browse-section", 1
196 assert_select ".browse-section.browse-node", 1
197 assert_select ".browse-section.browse-node .latitude", 0
198 assert_select ".browse-section.browse-node .longitude", 0
201 def test_redacted_node_history
202 node = create(:node, :with_history, :deleted, :version => 2)
203 node_v1 = node.old_nodes.find_by(:version => 1)
204 node_v1.redact!(create(:redaction))
206 get node_history_path(:id => node)
207 assert_response :success
208 assert_template "browse/history"
210 # there are 2 revisions of the redacted node, but only one
211 # should be showing details here.
212 assert_select ".browse-section", 2
213 assert_select ".browse-section.browse-redacted", 1
214 assert_select ".browse-section.browse-node", 1
215 assert_select ".browse-section.browse-node .latitude", 0
216 assert_select ".browse-section.browse-node .longitude", 0
219 def test_redacted_way_history
220 way = create(:way, :with_history, :version => 4)
221 way_v1 = way.old_ways.find_by(:version => 1)
222 way_v1.redact!(create(:redaction))
223 way_v3 = way.old_ways.find_by(:version => 3)
224 way_v3.redact!(create(:redaction))
226 get way_history_path(:id => way)
227 assert_response :success
228 assert_template "browse/history"
230 # there are 4 revisions of the redacted way, 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-way", 2
237 def test_redacted_relation_history
238 relation = create(:relation, :with_history, :version => 4)
239 relation_v1 = relation.old_relations.find_by(:version => 1)
240 relation_v1.redact!(create(:redaction))
241 relation_v3 = relation.old_relations.find_by(:version => 3)
242 relation_v3.redact!(create(:redaction))
244 get relation_history_path(:id => relation)
245 assert_response :success
246 assert_template "browse/history"
248 # there are 4 revisions of the redacted relation, but only 2
249 # should be showing details here.
250 assert_select ".browse-section", 4
251 assert_select ".browse-section.browse-redacted", 2
252 assert_select ".browse-section.browse-relation", 2
257 assert_response :success
258 assert_template "browse/query"
263 # This is a convenience method for most of the above checks
264 # First we check that when we don't have an id, it will correctly return a 404
265 # then we check that we get the correct 404 when a non-existant id is passed
266 # then we check that it will get a successful response, when we do pass an id
267 def browse_check(path, id, template)
268 path_method = method(path)
270 assert_raise ActionController::UrlGenerationError do
274 assert_raise ActionController::UrlGenerationError do
275 get path_method.call(:id => -10) # we won't have an id that's negative
278 get path_method.call(:id => 0)
279 assert_response :not_found
280 assert_template "browse/not_found"
281 assert_template :layout => "map"
283 get path_method.call(:id => 0), :xhr => true
284 assert_response :not_found
285 assert_template "browse/not_found"
286 assert_template :layout => "xhr"
288 get path_method.call(:id => id)
289 assert_response :success
290 assert_template template
291 assert_template :layout => "map"
293 get path_method.call(:id => id), :xhr => true
294 assert_response :success
295 assert_template template
296 assert_template :layout => "xhr"