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