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