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