]> git.openstreetmap.org Git - rails.git/blob - test/controllers/browse_controller_test.rb
Move browse#new_note to notes#new
[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 => "/query", :method => :get },
41       { :controller => "browse", :action => "query" }
42     )
43   end
44
45   def test_read_relation
46     browse_check :relation_path, create(:relation).id, "browse/feature"
47   end
48
49   def test_read_relation_history
50     browse_check :relation_history_path, create(:relation, :with_history).id, "browse/history"
51   end
52
53   def test_read_way
54     browse_check :way_path, create(:way).id, "browse/feature"
55   end
56
57   def test_read_way_history
58     browse_check :way_history_path, create(:way, :with_history).id, "browse/history"
59   end
60
61   def test_read_node
62     node = create :node
63     browse_check :node_path, node.id, "browse/feature"
64     assert_select "a[href='#{api_node_path node}']", :count => 1
65   end
66
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
71   end
72
73   def test_read_node_history
74     browse_check :node_history_path, create(:node, :with_history).id, "browse/history"
75   end
76
77   def test_read_changeset
78     user = create(:user)
79     changeset = create(:changeset, :user => user)
80     create(:changeset, :user => user)
81     browse_check :changeset_path, changeset.id, "browse/changeset"
82   end
83
84   def test_read_private_changeset
85     user = create(:user)
86     changeset = create(:changeset, :user => create(:user, :data_public => false))
87     create(:changeset, :user => user)
88     browse_check :changeset_path, changeset.id, "browse/changeset"
89   end
90
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)
95
96     browse_check :changeset_path, changeset.id, "browse/changeset"
97     assert_select "div.changeset-comments ul li", :count => 3
98
99     session_for(create(:moderator_user))
100
101     browse_check :changeset_path, changeset.id, "browse/changeset"
102     assert_select "div.changeset-comments ul li", :count => 4
103   end
104
105   def test_read_note
106     open_note = create(:note_with_comments)
107
108     browse_check :browse_note_path, open_note.id, "browse/note"
109   end
110
111   def test_read_hidden_note
112     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
113
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"
118
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"
123
124     session_for(create(:moderator_user))
125
126     browse_check :browse_note_path, hidden_note_with_comment.id, "browse/note"
127   end
128
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)
132     end
133
134     browse_check :browse_note_path, note_with_hidden_comment.id, "browse/note"
135     assert_select "div.note-comments ul li", :count => 1
136
137     session_for(create(:moderator_user))
138
139     browse_check :browse_note_path, note_with_hidden_comment.id, "browse/note"
140     assert_select "div.note-comments ul li", :count => 2
141   end
142
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)
147     end
148
149     browse_check :browse_note_path, note_with_hidden_user_comment.id, "browse/note"
150     assert_select "div.note-comments ul li", :count => 1
151
152     session_for(create(:moderator_user))
153
154     browse_check :browse_note_path, note_with_hidden_user_comment.id, "browse/note"
155     assert_select "div.note-comments ul li", :count => 1
156   end
157
158   def test_read_closed_note
159     user = create(:user)
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)
162     end
163
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}/
167
168     user.soft_destroy!
169
170     reset!
171
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/
175   end
176
177   ##
178   #  Methods to check redaction.
179   #
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!
184   ##
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))
189
190     get node_path(:id => node)
191     assert_response :success
192     assert_template "feature"
193
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
199   end
200
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))
205
206     get node_history_path(:id => node)
207     assert_response :success
208     assert_template "browse/history"
209
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
217   end
218
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))
225
226     get way_history_path(:id => way)
227     assert_response :success
228     assert_template "browse/history"
229
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
235   end
236
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))
243
244     get relation_history_path(:id => relation)
245     assert_response :success
246     assert_template "browse/history"
247
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
253   end
254
255   def test_query
256     get query_path
257     assert_response :success
258     assert_template "browse/query"
259   end
260
261   private
262
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)
269
270     assert_raise ActionController::UrlGenerationError do
271       get path_method.call
272     end
273
274     assert_raise ActionController::UrlGenerationError do
275       get path_method.call(:id => -10) # we won't have an id that's negative
276     end
277
278     get path_method.call(:id => 0)
279     assert_response :not_found
280     assert_template "browse/not_found"
281     assert_template :layout => "map"
282
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"
287
288     get path_method.call(:id => id)
289     assert_response :success
290     assert_template template
291     assert_template :layout => "map"
292
293     get path_method.call(:id => id), :xhr => true
294     assert_response :success
295     assert_template template
296     assert_template :layout => "xhr"
297   end
298 end