3 class SiteControllerTest < ActionController::TestCase
9 Settings.id_key = create(:client_application).key
10 Settings.potlatch2_key = create(:client_application).key
19 Settings.potlatch2_key = nil
23 # test all routes which lead to this controller
26 { :path => "/", :method => :get },
27 { :controller => "site", :action => "index" }
30 { :path => "/", :method => :post },
31 { :controller => "site", :action => "index" }
34 { :path => "/edit", :method => :get },
35 { :controller => "site", :action => "edit" }
38 { :controller => "site", :action => "edit", :format => "html" },
39 { :path => "/edit.html", :method => :get }
42 { :path => "/copyright", :method => :get },
43 { :controller => "site", :action => "copyright" }
46 { :path => "/copyright/locale", :method => :get },
47 { :controller => "site", :action => "copyright", :copyright_locale => "locale" }
50 { :path => "/welcome", :method => :get },
51 { :controller => "site", :action => "welcome" }
54 { :path => "/fixthemap", :method => :get },
55 { :controller => "site", :action => "fixthemap" }
58 { :path => "/export", :method => :get },
59 { :controller => "site", :action => "export" }
62 { :controller => "site", :action => "export", :format => "html" },
63 { :path => "/export.html", :method => :get }
66 { :path => "/offline", :method => :get },
67 { :controller => "site", :action => "offline" }
70 { :path => "/key", :method => :get },
71 { :controller => "site", :action => "key" }
74 { :path => "/go/shortcode", :method => :get },
75 { :controller => "site", :action => "permalink", :code => "shortcode" }
78 { :path => "/preview/typename", :method => :post },
79 { :controller => "site", :action => "preview", :type => "typename" }
82 { :path => "/id", :method => :get },
83 { :controller => "site", :action => "id" }
90 assert_response :success
91 assert_template "index"
94 # Test the index page redirects
95 def test_index_redirect
96 get :index, :params => { :node => 123 }
97 assert_redirected_to :controller => :browse, :action => :node, :id => 123
99 get :index, :params => { :way => 123 }
100 assert_redirected_to :controller => :browse, :action => :way, :id => 123
102 get :index, :params => { :relation => 123 }
103 assert_redirected_to :controller => :browse, :action => :relation, :id => 123
105 get :index, :params => { :note => 123 }
106 assert_redirected_to :controller => :browse, :action => :note, :id => 123
108 get :index, :params => { :query => "test" }
109 assert_redirected_to :controller => :geocoder, :action => :search, :query => "test"
111 get :index, :params => { :lat => 4, :lon => 5 }
112 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5"
114 get :index, :params => { :lat => 4, :lon => 5, :zoom => 3 }
115 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5"
117 get :index, :params => { :layers => "T" }
118 assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T"
120 get :index, :params => { :notes => "yes" }
121 assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N"
123 get :index, :params => { :lat => 4, :lon => 5, :zoom => 3, :layers => "T" }
124 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5&layers=T"
127 # Test the permalink redirect
129 get :permalink, :params => { :code => "wBz3--" }
130 assert_response :redirect
131 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
133 get :permalink, :params => { :code => "wBz3--", :m => "" }
134 assert_response :redirect
135 assert_redirected_to :controller => :site, :action => :index, :mlat => "4.8779296875", :mlon => "3.955078125", :anchor => "map=3/4.8779296875/3.955078125"
137 get :permalink, :params => { :code => "wBz3--", :layers => "T" }
138 assert_response :redirect
139 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125&layers=T"
141 get :permalink, :params => { :code => "wBz3--", :node => 1 }
142 assert_response :redirect
143 assert_redirected_to :controller => :browse, :action => :node, :id => 1, :anchor => "map=3/4.8779296875/3.955078125"
145 get :permalink, :params => { :code => "wBz3--", :way => 2 }
146 assert_response :redirect
147 assert_redirected_to :controller => :browse, :action => :way, :id => 2, :anchor => "map=3/4.8779296875/3.955078125"
149 get :permalink, :params => { :code => "wBz3--", :relation => 3 }
150 assert_response :redirect
151 assert_redirected_to :controller => :browse, :action => :relation, :id => 3, :anchor => "map=3/4.8779296875/3.955078125"
153 get :permalink, :params => { :code => "wBz3--", :changeset => 4 }
154 assert_response :redirect
155 assert_redirected_to :controller => :browse, :action => :changeset, :id => 4, :anchor => "map=3/4.8779296875/3.955078125"
160 get :key, :xhr => true
161 assert_response :success
162 assert_template "key"
163 assert_template :layout => false
166 # Test the edit page redirects when you aren't logged in
169 assert_response :redirect
170 assert_redirected_to :controller => :users, :action => :login, :referer => "/edit"
173 # Test the error when trying to edit without public edits
174 def test_edit_non_public
175 get :edit, :session => { :user => create(:user, :data_public => false) }
176 assert_response :success
177 assert_template "edit"
178 assert_select "a[href='https://wiki.openstreetmap.org/wiki/Disabling_anonymous_edits']"
181 # Test the right editor gets used when the user hasn't set a preference
182 def test_edit_without_preference
183 get :edit, :session => { :user => create(:user) }
184 assert_response :success
185 assert_template "edit"
186 assert_template :partial => "_#{Settings.default_editor}", :count => 1
189 # Test the right editor gets used when the user has set a preference
190 def test_edit_with_preference
192 user.preferred_editor = "id"
195 get :edit, :session => { :user => user }
196 assert_response :success
197 assert_template "edit"
198 assert_template :partial => "_id", :count => 1
201 user.preferred_editor = "potlatch2"
204 get :edit, :session => { :user => user }
205 assert_response :success
206 assert_template "edit"
207 assert_template :partial => "_potlatch2", :count => 1
210 user.preferred_editor = "potlatch"
213 get :edit, :session => { :user => user }
214 assert_response :success
215 assert_template "edit"
216 assert_template :partial => "_potlatch", :count => 1
219 user.preferred_editor = "remote"
222 get :edit, :session => { :user => user }
223 assert_response :success
224 assert_template "index"
227 # Test the right editor gets used when the URL has an override
228 def test_edit_with_override
229 get :edit, :params => { :editor => "id" }, :session => { :user => create(:user) }
230 assert_response :success
231 assert_template "edit"
232 assert_template :partial => "_id", :count => 1
234 get :edit, :params => { :editor => "potlatch2" }, :session => { :user => create(:user) }
235 assert_response :success
236 assert_template "edit"
237 assert_template :partial => "_potlatch2", :count => 1
239 get :edit, :params => { :editor => "potlatch" }, :session => { :user => create(:user) }
240 assert_response :success
241 assert_template "edit"
242 assert_template :partial => "_potlatch", :count => 1
244 get :edit, :params => { :editor => "remote" }, :session => { :user => create(:user) }
245 assert_response :success
246 assert_template "index"
249 # Test editing a specific node
250 def test_edit_with_node
252 node = create(:node, :lat => 1.0, :lon => 1.0)
254 get :edit, :params => { :node => node.id }, :session => { :user => user }
255 assert_response :success
256 assert_template "edit"
257 assert_equal 1.0, assigns(:lat)
258 assert_equal 1.0, assigns(:lon)
259 assert_equal 18, assigns(:zoom)
262 # Test editing inaccessible nodes
263 def test_edit_with_inaccessible_nodes
265 deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
267 get :edit, :params => { :node => 99999 }, :session => { :user => user }
268 assert_response :success
269 assert_template "edit"
270 assert_nil assigns(:lat)
271 assert_nil assigns(:lon)
272 assert_nil assigns(:zoom)
274 get :edit, :params => { :node => deleted_node.id }, :session => { :user => user }
275 assert_response :success
276 assert_template "edit"
277 assert_nil assigns(:lat)
278 assert_nil assigns(:lon)
279 assert_nil assigns(:zoom)
282 # Test editing a specific way
283 def test_edit_with_way
285 node = create(:node, :lat => 3, :lon => 3)
287 create(:way_node, :node => node, :way => way)
289 get :edit, :params => { :way => way.id }, :session => { :user => user }
290 assert_response :success
291 assert_template "edit"
292 assert_equal 3.0, assigns(:lat)
293 assert_equal 3.0, assigns(:lon)
294 assert_equal 17, assigns(:zoom)
297 # Test editing inaccessible ways
298 def test_edit_with_inaccessible_ways
300 deleted_way = create(:way, :visible => false)
302 get :edit, :params => { :way => 99999 }, :session => { :user => user }
303 assert_response :success
304 assert_template "edit"
305 assert_nil assigns(:lat)
306 assert_nil assigns(:lon)
307 assert_nil assigns(:zoom)
309 get :edit, :params => { :way => deleted_way.id }, :session => { :user => user }
310 assert_response :success
311 assert_template "edit"
312 assert_nil assigns(:lat)
313 assert_nil assigns(:lon)
314 assert_nil assigns(:zoom)
317 # Test editing a specific note
318 def test_edit_with_note
320 note = create(:note) do |n|
321 n.comments.create(:author_id => user.id)
324 get :edit, :params => { :note => note.id }, :session => { :user => user }
325 assert_response :success
326 assert_template "edit"
327 assert_equal 1.0, assigns(:lat)
328 assert_equal 1.0, assigns(:lon)
329 assert_equal 17, assigns(:zoom)
332 # Test editing inaccessible notes
333 def test_edit_with_inaccessible_notes
335 deleted_note = create(:note, :status => "hidden") do |n|
336 n.comments.create(:author_id => user.id)
339 get :edit, :params => { :note => 99999 }, :session => { :user => user }
340 assert_response :success
341 assert_template "edit"
342 assert_nil assigns(:lat)
343 assert_nil assigns(:lon)
344 assert_nil assigns(:zoom)
346 get :edit, :params => { :note => deleted_note.id }, :session => { :user => user }
347 assert_response :success
348 assert_template "edit"
349 assert_nil assigns(:lat)
350 assert_nil assigns(:lon)
351 assert_nil assigns(:zoom)
354 # Test editing a specific GPX trace
355 def test_edit_with_gpx
357 gpx = create(:trace, :latitude => 1, :longitude => 1)
359 get :edit, :params => { :gpx => gpx.id }, :session => { :user => user }
360 assert_response :success
361 assert_template "edit"
362 assert_equal 1.0, assigns(:lat)
363 assert_equal 1.0, assigns(:lon)
364 assert_equal 16, assigns(:zoom)
367 # Test editing inaccessible GPX traces
368 def test_edit_with_inaccessible_gpxes
370 deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
371 private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "private")
373 get :edit, :params => { :gpx => 99999 }, :session => { :user => user }
374 assert_response :success
375 assert_template "edit"
376 assert_nil assigns(:lat)
377 assert_nil assigns(:lon)
378 assert_nil assigns(:zoom)
380 get :edit, :params => { :gpx => deleted_gpx.id }, :session => { :user => user }
381 assert_response :success
382 assert_template "edit"
383 assert_nil assigns(:lat)
384 assert_nil assigns(:lon)
385 assert_nil assigns(:zoom)
387 get :edit, :params => { :gpx => private_gpx.id }, :session => { :user => user }
388 assert_response :success
389 assert_template "edit"
390 assert_nil assigns(:lat)
391 assert_nil assigns(:lon)
392 assert_nil assigns(:zoom)
395 # Test the edit page redirects
396 def test_edit_redirect
397 get :edit, :params => { :lat => 4, :lon => 5 }
398 assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5"
400 get :edit, :params => { :lat => 4, :lon => 5, :zoom => 3 }
401 assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
403 get :edit, :params => { :lat => 4, :lon => 5, :zoom => 3, :editor => "id" }
404 assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5"
407 # Test the copyright page
410 assert_response :success
411 assert_template "copyright"
414 # Test the welcome page
417 assert_response :redirect
418 assert_redirected_to :controller => :users, :action => :login, :referer => "/welcome"
420 get :welcome, :session => { :user => create(:user) }
421 assert_response :success
422 assert_template "welcome"
425 # Test the fixthemap page
428 assert_response :success
429 assert_template "fixthemap"
435 assert_response :success
436 assert_template "help"
439 # Test the about page
442 assert_response :success
443 assert_template "about"
446 # Test the export page
449 assert_response :success
450 assert_template "export"
451 assert_template :layout => "map"
453 get :export, :xhr => true
454 assert_response :success
455 assert_template "export"
456 assert_template :layout => "xhr"
459 # Test the offline page
462 assert_response :success
463 assert_template "offline"
466 # Test the rich text preview
468 post :preview, :xhr => true, :params => { :type => "html" }
469 assert_response :success
471 post :preview, :xhr => true, :params => { :type => "markdown" }
472 assert_response :success
474 post :preview, :xhr => true, :params => { :type => "text" }
475 assert_response :success
480 get :id, :session => { :user => create(:user) }
481 assert_response :success
483 assert_template :layout => false