]> git.openstreetmap.org Git - rails.git/blob - test/controllers/browse_controller_test.rb
Use resourceful destroy method for messages
[rails.git] / test / controllers / browse_controller_test.rb
1 require "test_helper"
2 require "browse_controller"
3
4 class BrowseControllerTest < ActionController::TestCase
5   ##
6   # test all routes which lead to this controller
7   def test_routes
8     assert_routing(
9       { :path => "/node/1", :method => :get },
10       { :controller => "browse", :action => "node", :id => "1" }
11     )
12     assert_routing(
13       { :path => "/node/1/history", :method => :get },
14       { :controller => "browse", :action => "node_history", :id => "1" }
15     )
16     assert_routing(
17       { :path => "/way/1", :method => :get },
18       { :controller => "browse", :action => "way", :id => "1" }
19     )
20     assert_routing(
21       { :path => "/way/1/history", :method => :get },
22       { :controller => "browse", :action => "way_history", :id => "1" }
23     )
24     assert_routing(
25       { :path => "/relation/1", :method => :get },
26       { :controller => "browse", :action => "relation", :id => "1" }
27     )
28     assert_routing(
29       { :path => "/relation/1/history", :method => :get },
30       { :controller => "browse", :action => "relation_history", :id => "1" }
31     )
32     assert_routing(
33       { :path => "/changeset/1", :method => :get },
34       { :controller => "browse", :action => "changeset", :id => "1" }
35     )
36     assert_routing(
37       { :path => "/note/1", :method => :get },
38       { :controller => "browse", :action => "note", :id => "1" }
39     )
40     assert_routing(
41       { :path => "/note/new", :method => :get },
42       { :controller => "browse", :action => "new_note" }
43     )
44   end
45
46   def test_read_relation
47     browse_check "relation", create(:relation).id, "browse/feature"
48   end
49
50   def test_read_relation_history
51     browse_check "relation_history", create(:relation, :with_history).id, "browse/history"
52   end
53
54   def test_read_way
55     browse_check "way", create(:way).id, "browse/feature"
56   end
57
58   def test_read_way_history
59     browse_check "way_history", create(:way, :with_history).id, "browse/history"
60   end
61
62   def test_read_node
63     browse_check "node", create(:node).id, "browse/feature"
64   end
65
66   def test_read_node_history
67     browse_check "node_history", create(:node, :with_history).id, "browse/history"
68   end
69
70   def test_read_changeset
71     private_changeset = create(:changeset, :user => create(:user, :data_public => false))
72     changeset = create(:changeset)
73     browse_check "changeset", private_changeset.id, "browse/changeset"
74     browse_check "changeset", changeset.id, "browse/changeset"
75   end
76
77   def test_read_changeset_hidden_comments
78     changeset = create(:changeset)
79     create_list(:changeset_comment, 3, :changeset => changeset)
80     create(:changeset_comment, :visible => false, :changeset => changeset)
81
82     browse_check "changeset", changeset.id, "browse/changeset"
83     assert_select "div.changeset-comments ul li", :count => 3
84
85     session[:user] = create(:moderator_user).id
86
87     browse_check "changeset", changeset.id, "browse/changeset"
88     assert_select "div.changeset-comments ul li", :count => 4
89   end
90
91   def test_read_note
92     open_note = create(:note_with_comments)
93
94     browse_check "note", open_note.id, "browse/note"
95   end
96
97   def test_read_hidden_note
98     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
99
100     get :note, :params => { :id => hidden_note_with_comment.id }
101     assert_response :not_found
102     assert_template "browse/not_found"
103     assert_template :layout => "map"
104
105     get :note, :params => { :id => hidden_note_with_comment.id }, :xhr => true
106     assert_response :not_found
107     assert_template "browse/not_found"
108     assert_template :layout => "xhr"
109
110     session[:user] = create(:moderator_user).id
111
112     browse_check "note", hidden_note_with_comment.id, "browse/note"
113   end
114
115   def test_read_note_hidden_comments
116     note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
117       create(:note_comment, :note => note, :visible => false)
118     end
119
120     browse_check "note", note_with_hidden_comment.id, "browse/note"
121     assert_select "div.note-comments ul li", :count => 1
122
123     session[:user] = create(:moderator_user).id
124
125     browse_check "note", note_with_hidden_comment.id, "browse/note"
126     assert_select "div.note-comments ul li", :count => 2
127   end
128
129   def test_read_note_hidden_user_comment
130     hidden_user = create(:user, :status => "deleted")
131     note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
132       create(:note_comment, :note => note, :author => hidden_user)
133     end
134
135     browse_check "note", note_with_hidden_user_comment.id, "browse/note"
136     assert_select "div.note-comments ul li", :count => 1
137
138     session[:user] = create(:moderator_user).id
139
140     browse_check "note", note_with_hidden_user_comment.id, "browse/note"
141     assert_select "div.note-comments ul li", :count => 1
142   end
143
144   ##
145   #  Methods to check redaction.
146   #
147   # note that these are presently highly reliant on the structure of the
148   # page for the selection tests, which doesn't work out particularly
149   # well if that structure changes. so... if you change the page layout
150   # then please make it more easily (and robustly) testable!
151   ##
152   def test_redacted_node
153     node = create(:node, :with_history, :deleted, :version => 2)
154     node_v1 = node.old_nodes.find_by(:version => 1)
155     node_v1.redact!(create(:redaction))
156
157     get :node, :params => { :id => node.id }
158     assert_response :success
159     assert_template "feature"
160
161     # check that we don't show lat/lon for a redacted node.
162     assert_select ".browse-section", 1
163     assert_select ".browse-section.browse-node", 1
164     assert_select ".browse-section.browse-node .latitude", 0
165     assert_select ".browse-section.browse-node .longitude", 0
166   end
167
168   def test_redacted_node_history
169     node = create(:node, :with_history, :deleted, :version => 2)
170     node_v1 = node.old_nodes.find_by(:version => 1)
171     node_v1.redact!(create(:redaction))
172
173     get :node_history, :params => { :id => node.id }
174     assert_response :success
175     assert_template "browse/history"
176
177     # there are 2 revisions of the redacted node, but only one
178     # should be showing details here.
179     assert_select ".browse-section", 2
180     assert_select ".browse-section.browse-redacted", 1
181     assert_select ".browse-section.browse-node", 1
182     assert_select ".browse-section.browse-node .latitude", 0
183     assert_select ".browse-section.browse-node .longitude", 0
184   end
185
186   def test_redacted_way_history
187     way = create(:way, :with_history, :version => 4)
188     way_v1 = way.old_ways.find_by(:version => 1)
189     way_v1.redact!(create(:redaction))
190     way_v3 = way.old_ways.find_by(:version => 3)
191     way_v3.redact!(create(:redaction))
192
193     get :way_history, :params => { :id => way.id }
194     assert_response :success
195     assert_template "browse/history"
196
197     # there are 4 revisions of the redacted way, but only 2
198     # should be showing details here.
199     assert_select ".browse-section", 4
200     assert_select ".browse-section.browse-redacted", 2
201     assert_select ".browse-section.browse-way", 2
202   end
203
204   def test_redacted_relation_history
205     relation = create(:relation, :with_history, :version => 4)
206     relation_v1 = relation.old_relations.find_by(:version => 1)
207     relation_v1.redact!(create(:redaction))
208     relation_v3 = relation.old_relations.find_by(:version => 3)
209     relation_v3.redact!(create(:redaction))
210
211     get :relation_history, :params => { :id => relation.id }
212     assert_response :success
213     assert_template "browse/history"
214
215     # there are 4 revisions of the redacted relation, but only 2
216     # should be showing details here.
217     assert_select ".browse-section", 4
218     assert_select ".browse-section.browse-redacted", 2
219     assert_select ".browse-section.browse-relation", 2
220   end
221
222   private
223
224   # This is a convenience method for most of the above checks
225   # First we check that when we don't have an id, it will correctly return a 404
226   # then we check that we get the correct 404 when a non-existant id is passed
227   # then we check that it will get a successful response, when we do pass an id
228   def browse_check(type, id, template)
229     assert_raise ActionController::UrlGenerationError do
230       get type
231     end
232
233     assert_raise ActionController::UrlGenerationError do
234       get type, :params => { :id => -10 } # we won't have an id that's negative
235     end
236
237     get type, :params => { :id => 0 }
238     assert_response :not_found
239     assert_template "browse/not_found"
240     assert_template :layout => "map"
241
242     get type, :params => { :id => 0 }, :xhr => true
243     assert_response :not_found
244     assert_template "browse/not_found"
245     assert_template :layout => "xhr"
246
247     get type, :params => { :id => id }
248     assert_response :success
249     assert_template template
250     assert_template :layout => "map"
251
252     get type, :params => { :id => id }, :xhr => true
253     assert_response :success
254     assert_template template
255     assert_template :layout => "xhr"
256   end
257 end