]> git.openstreetmap.org Git - rails.git/blob - test/controllers/browse_controller_test.rb
d6bde97d6add65bdcbaa9dc90088875f1a625640
[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     user = create(:user)
72     private_changeset = create(:changeset, :user => create(:user, :data_public => false))
73     changeset = create(:changeset, :user => user)
74     create(:changeset, :user => user)
75     browse_check "changeset", private_changeset.id, "browse/changeset"
76     browse_check "changeset", changeset.id, "browse/changeset"
77   end
78
79   def test_read_changeset_hidden_comments
80     changeset = create(:changeset)
81     create_list(:changeset_comment, 3, :changeset => changeset)
82     create(:changeset_comment, :visible => false, :changeset => changeset)
83
84     browse_check "changeset", changeset.id, "browse/changeset"
85     assert_select "div.changeset-comments ul li", :count => 3
86
87     session[:user] = create(:moderator_user).id
88
89     browse_check "changeset", changeset.id, "browse/changeset"
90     assert_select "div.changeset-comments ul li", :count => 4
91   end
92
93   def test_read_note
94     open_note = create(:note_with_comments)
95
96     browse_check "note", open_note.id, "browse/note"
97   end
98
99   def test_read_hidden_note
100     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
101
102     get :note, :params => { :id => hidden_note_with_comment.id }
103     assert_response :not_found
104     assert_template "browse/not_found"
105     assert_template :layout => "map"
106
107     get :note, :params => { :id => hidden_note_with_comment.id }, :xhr => true
108     assert_response :not_found
109     assert_template "browse/not_found"
110     assert_template :layout => "xhr"
111
112     session[:user] = create(:moderator_user).id
113
114     browse_check "note", hidden_note_with_comment.id, "browse/note"
115   end
116
117   def test_read_note_hidden_comments
118     note_with_hidden_comment = create(:note_with_comments, :comments_count => 2) do |note|
119       create(:note_comment, :note => note, :visible => false)
120     end
121
122     browse_check "note", note_with_hidden_comment.id, "browse/note"
123     assert_select "div.note-comments ul li", :count => 1
124
125     session[:user] = create(:moderator_user).id
126
127     browse_check "note", note_with_hidden_comment.id, "browse/note"
128     assert_select "div.note-comments ul li", :count => 2
129   end
130
131   def test_read_note_hidden_user_comment
132     hidden_user = create(:user, :status => "deleted")
133     note_with_hidden_user_comment = create(:note_with_comments, :comments_count => 2) do |note|
134       create(:note_comment, :note => note, :author => hidden_user)
135     end
136
137     browse_check "note", note_with_hidden_user_comment.id, "browse/note"
138     assert_select "div.note-comments ul li", :count => 1
139
140     session[:user] = create(:moderator_user).id
141
142     browse_check "note", note_with_hidden_user_comment.id, "browse/note"
143     assert_select "div.note-comments ul li", :count => 1
144   end
145
146   ##
147   #  Methods to check redaction.
148   #
149   # note that these are presently highly reliant on the structure of the
150   # page for the selection tests, which doesn't work out particularly
151   # well if that structure changes. so... if you change the page layout
152   # then please make it more easily (and robustly) testable!
153   ##
154   def test_redacted_node
155     node = create(:node, :with_history, :deleted, :version => 2)
156     node_v1 = node.old_nodes.find_by(:version => 1)
157     node_v1.redact!(create(:redaction))
158
159     get :node, :params => { :id => node.id }
160     assert_response :success
161     assert_template "feature"
162
163     # check that we don't show lat/lon for a redacted node.
164     assert_select ".browse-section", 1
165     assert_select ".browse-section.browse-node", 1
166     assert_select ".browse-section.browse-node .latitude", 0
167     assert_select ".browse-section.browse-node .longitude", 0
168   end
169
170   def test_redacted_node_history
171     node = create(:node, :with_history, :deleted, :version => 2)
172     node_v1 = node.old_nodes.find_by(:version => 1)
173     node_v1.redact!(create(:redaction))
174
175     get :node_history, :params => { :id => node.id }
176     assert_response :success
177     assert_template "browse/history"
178
179     # there are 2 revisions of the redacted node, but only one
180     # should be showing details here.
181     assert_select ".browse-section", 2
182     assert_select ".browse-section.browse-redacted", 1
183     assert_select ".browse-section.browse-node", 1
184     assert_select ".browse-section.browse-node .latitude", 0
185     assert_select ".browse-section.browse-node .longitude", 0
186   end
187
188   def test_redacted_way_history
189     way = create(:way, :with_history, :version => 4)
190     way_v1 = way.old_ways.find_by(:version => 1)
191     way_v1.redact!(create(:redaction))
192     way_v3 = way.old_ways.find_by(:version => 3)
193     way_v3.redact!(create(:redaction))
194
195     get :way_history, :params => { :id => way.id }
196     assert_response :success
197     assert_template "browse/history"
198
199     # there are 4 revisions of the redacted way, but only 2
200     # should be showing details here.
201     assert_select ".browse-section", 4
202     assert_select ".browse-section.browse-redacted", 2
203     assert_select ".browse-section.browse-way", 2
204   end
205
206   def test_redacted_relation_history
207     relation = create(:relation, :with_history, :version => 4)
208     relation_v1 = relation.old_relations.find_by(:version => 1)
209     relation_v1.redact!(create(:redaction))
210     relation_v3 = relation.old_relations.find_by(:version => 3)
211     relation_v3.redact!(create(:redaction))
212
213     get :relation_history, :params => { :id => relation.id }
214     assert_response :success
215     assert_template "browse/history"
216
217     # there are 4 revisions of the redacted relation, but only 2
218     # should be showing details here.
219     assert_select ".browse-section", 4
220     assert_select ".browse-section.browse-redacted", 2
221     assert_select ".browse-section.browse-relation", 2
222   end
223
224   private
225
226   # This is a convenience method for most of the above checks
227   # First we check that when we don't have an id, it will correctly return a 404
228   # then we check that we get the correct 404 when a non-existant id is passed
229   # then we check that it will get a successful response, when we do pass an id
230   def browse_check(type, id, template)
231     assert_raise ActionController::UrlGenerationError do
232       get type
233     end
234
235     assert_raise ActionController::UrlGenerationError do
236       get type, :params => { :id => -10 } # we won't have an id that's negative
237     end
238
239     get type, :params => { :id => 0 }
240     assert_response :not_found
241     assert_template "browse/not_found"
242     assert_template :layout => "map"
243
244     get type, :params => { :id => 0 }, :xhr => true
245     assert_response :not_found
246     assert_template "browse/not_found"
247     assert_template :layout => "xhr"
248
249     get type, :params => { :id => id }
250     assert_response :success
251     assert_template template
252     assert_template :layout => "map"
253
254     get type, :params => { :id => id }, :xhr => true
255     assert_response :success
256     assert_template template
257     assert_template :layout => "xhr"
258   end
259 end