2 require "browse_controller"
4 class BrowseControllerTest < ActionController::TestCase
6 # test all routes which lead to this controller
9 { :path => "/node/1", :method => :get },
10 { :controller => "browse", :action => "node", :id => "1" }
13 { :path => "/node/1/history", :method => :get },
14 { :controller => "browse", :action => "node_history", :id => "1" }
17 { :path => "/way/1", :method => :get },
18 { :controller => "browse", :action => "way", :id => "1" }
21 { :path => "/way/1/history", :method => :get },
22 { :controller => "browse", :action => "way_history", :id => "1" }
25 { :path => "/relation/1", :method => :get },
26 { :controller => "browse", :action => "relation", :id => "1" }
29 { :path => "/relation/1/history", :method => :get },
30 { :controller => "browse", :action => "relation_history", :id => "1" }
33 { :path => "/changeset/1", :method => :get },
34 { :controller => "browse", :action => "changeset", :id => "1" }
37 { :path => "/note/1", :method => :get },
38 { :controller => "browse", :action => "note", :id => "1" }
41 { :path => "/note/new", :method => :get },
42 { :controller => "browse", :action => "new_note" }
46 def test_read_relation
47 browse_check "relation", create(:relation).id, "browse/feature"
50 def test_read_relation_history
51 browse_check "relation_history", create(:relation, :with_history).id, "browse/history"
55 browse_check "way", create(:way).id, "browse/feature"
58 def test_read_way_history
59 browse_check "way_history", create(:way, :with_history).id, "browse/history"
63 browse_check "node", create(:node).id, "browse/feature"
66 def test_read_node_history
67 browse_check "node_history", create(:node, :with_history).id, "browse/history"
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"
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)
82 browse_check "changeset", changeset.id, "browse/changeset"
83 assert_select "div.changeset-comments ul li", :count => 3
85 session[:user] = create(:moderator_user).id
87 browse_check "changeset", changeset.id, "browse/changeset"
88 assert_select "div.changeset-comments ul li", :count => 4
92 open_note = create(:note_with_comments)
94 browse_check "note", open_note.id, "browse/note"
97 def test_read_hidden_note
98 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
100 get :note, :id => hidden_note_with_comment.id
101 assert_response :not_found
102 assert_template "browse/not_found"
103 assert_template :layout => "map"
105 xhr :get, :note, :id => hidden_note_with_comment.id
106 assert_response :not_found
107 assert_template "browse/not_found"
108 assert_template :layout => "xhr"
110 session[:user] = create(:moderator_user).id
112 browse_check "note", hidden_note_with_comment.id, "browse/note"
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)
120 browse_check "note", note_with_hidden_comment.id, "browse/note"
121 assert_select "div.note-comments ul li", :count => 1
123 session[:user] = create(:moderator_user).id
125 browse_check "note", note_with_hidden_comment.id, "browse/note"
126 assert_select "div.note-comments ul li", :count => 2
130 # Methods to check redaction.
132 # note that these are presently highly reliant on the structure of the
133 # page for the selection tests, which doesn't work out particularly
134 # well if that structure changes. so... if you change the page layout
135 # then please make it more easily (and robustly) testable!
137 def test_redacted_node
138 node = create(:node, :with_history, :deleted, :version => 2)
139 node_v1 = node.old_nodes.find_by(:version => 1)
140 node_v1.redact!(create(:redaction))
142 get :node, :id => node.id
143 assert_response :success
144 assert_template "feature"
146 # check that we don't show lat/lon for a redacted node.
147 assert_select ".browse-section", 1
148 assert_select ".browse-section.browse-node", 1
149 assert_select ".browse-section.browse-node .latitude", 0
150 assert_select ".browse-section.browse-node .longitude", 0
153 def test_redacted_node_history
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))
158 get :node_history, :id => node.id
159 assert_response :success
160 assert_template "browse/history"
162 # there are 2 revisions of the redacted node, but only one
163 # should be showing details here.
164 assert_select ".browse-section", 2
165 assert_select ".browse-section.browse-redacted", 1
166 assert_select ".browse-section.browse-node", 1
167 assert_select ".browse-section.browse-node .latitude", 0
168 assert_select ".browse-section.browse-node .longitude", 0
171 def test_redacted_way_history
172 way = create(:way, :with_history, :version => 4)
173 way_v1 = way.old_ways.find_by(:version => 1)
174 way_v1.redact!(create(:redaction))
175 way_v3 = way.old_ways.find_by(:version => 3)
176 way_v3.redact!(create(:redaction))
178 get :way_history, :id => way.id
179 assert_response :success
180 assert_template "browse/history"
182 # there are 4 revisions of the redacted way, but only 2
183 # should be showing details here.
184 assert_select ".browse-section", 4
185 assert_select ".browse-section.browse-redacted", 2
186 assert_select ".browse-section.browse-way", 2
189 def test_redacted_relation_history
190 relation = create(:relation, :with_history, :version => 4)
191 relation_v1 = relation.old_relations.find_by(:version => 1)
192 relation_v1.redact!(create(:redaction))
193 relation_v3 = relation.old_relations.find_by(:version => 3)
194 relation_v3.redact!(create(:redaction))
196 get :relation_history, :id => relation.id
197 assert_response :success
198 assert_template "browse/history"
200 # there are 4 revisions of the redacted relation, but only 2
201 # should be showing details here.
202 assert_select ".browse-section", 4
203 assert_select ".browse-section.browse-redacted", 2
204 assert_select ".browse-section.browse-relation", 2
209 # This is a convenience method for most of the above checks
210 # First we check that when we don't have an id, it will correctly return a 404
211 # then we check that we get the correct 404 when a non-existant id is passed
212 # then we check that it will get a successful response, when we do pass an id
213 def browse_check(type, id, template)
214 assert_raise ActionController::UrlGenerationError do
218 assert_raise ActionController::UrlGenerationError do
219 get type, :id => -10 # we won't have an id that's negative
223 assert_response :not_found
224 assert_template "browse/not_found"
225 assert_template :layout => "map"
227 xhr :get, type, :id => 0
228 assert_response :not_found
229 assert_template "browse/not_found"
230 assert_template :layout => "xhr"
233 assert_response :success
234 assert_template template
235 assert_template :layout => "map"
237 xhr :get, type, :id => id
238 assert_response :success
239 assert_template template
240 assert_template :layout => "xhr"