3 class SiteControllerTest < ActionDispatch::IntegrationTest
9 Settings.id_key = create(:client_application).key
10 Settings.potlatch2_key = create(:client_application).key
17 Settings.potlatch2_key = nil
21 # test all routes which lead to this controller
24 { :path => "/", :method => :get },
25 { :controller => "site", :action => "index" }
28 { :path => "/", :method => :post },
29 { :controller => "site", :action => "index" }
32 { :path => "/edit", :method => :get },
33 { :controller => "site", :action => "edit" }
36 { :controller => "site", :action => "edit", :format => "html" },
37 { :path => "/edit.html", :method => :get }
40 { :path => "/copyright", :method => :get },
41 { :controller => "site", :action => "copyright" }
44 { :path => "/copyright/locale", :method => :get },
45 { :controller => "site", :action => "copyright", :copyright_locale => "locale" }
48 { :path => "/welcome", :method => :get },
49 { :controller => "site", :action => "welcome" }
52 { :path => "/fixthemap", :method => :get },
53 { :controller => "site", :action => "fixthemap" }
56 { :path => "/help", :method => :get },
57 { :controller => "site", :action => "help" }
60 { :path => "/about", :method => :get },
61 { :controller => "site", :action => "about" }
64 { :path => "/about/locale", :method => :get },
65 { :controller => "site", :action => "about", :about_locale => "locale" }
68 { :path => "/export", :method => :get },
69 { :controller => "site", :action => "export" }
72 { :controller => "site", :action => "export", :format => "html" },
73 { :path => "/export.html", :method => :get }
76 { :path => "/offline", :method => :get },
77 { :controller => "site", :action => "offline" }
80 { :path => "/key", :method => :get },
81 { :controller => "site", :action => "key" }
84 { :path => "/go/shortcode", :method => :get },
85 { :controller => "site", :action => "permalink", :code => "shortcode" }
88 { :path => "/preview/typename", :method => :post },
89 { :controller => "site", :action => "preview", :type => "typename" }
92 { :path => "/id", :method => :get },
93 { :controller => "site", :action => "id" }
101 assert_response :success
102 assert_template "index"
105 # Test the index page redirects
106 def test_index_redirect
107 get root_path(:node => 123)
108 assert_redirected_to :controller => :browse, :action => :node, :id => 123
110 get root_path(:way => 123)
111 assert_redirected_to :controller => :browse, :action => :way, :id => 123
113 get root_path(:relation => 123)
114 assert_redirected_to :controller => :browse, :action => :relation, :id => 123
116 get root_path(:note => 123)
117 assert_redirected_to :controller => :browse, :action => :note, :id => 123
119 get root_path(:query => "test")
120 assert_redirected_to :controller => :geocoder, :action => :search, :query => "test"
122 get root_path(:lat => 4, :lon => 5)
123 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5"
125 get root_path(:lat => 4, :lon => 5, :zoom => 3)
126 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5"
128 get root_path(:layers => "T")
129 assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T"
131 get root_path(:notes => "yes")
132 assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N"
134 get root_path(:lat => 4, :lon => 5, :zoom => 3, :layers => "T")
135 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5&layers=T"
138 # Test the permalink redirect
140 get permalink_path(:code => "wBz3--")
141 assert_response :redirect
142 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
144 get permalink_path(:code => "wBz3--", :m => "")
145 assert_response :redirect
146 assert_redirected_to :controller => :site, :action => :index, :mlat => "4.8779296875", :mlon => "3.955078125", :anchor => "map=3/4.8779296875/3.955078125"
148 get permalink_path(:code => "wBz3--", :layers => "T")
149 assert_response :redirect
150 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125&layers=T"
152 get permalink_path(:code => "wBz3--", :node => 1)
153 assert_response :redirect
154 assert_redirected_to :controller => :browse, :action => :node, :id => 1, :anchor => "map=3/4.8779296875/3.955078125"
156 get permalink_path(:code => "wBz3--", :way => 2)
157 assert_response :redirect
158 assert_redirected_to :controller => :browse, :action => :way, :id => 2, :anchor => "map=3/4.8779296875/3.955078125"
160 get permalink_path(:code => "wBz3--", :relation => 3)
161 assert_response :redirect
162 assert_redirected_to :controller => :browse, :action => :relation, :id => 3, :anchor => "map=3/4.8779296875/3.955078125"
164 get permalink_path(:code => "wBz3--", :changeset => 4)
165 assert_response :redirect
166 assert_redirected_to :controller => :browse, :action => :changeset, :id => 4, :anchor => "map=3/4.8779296875/3.955078125"
171 get key_path, :xhr => true
173 assert_response :success
174 assert_template "key"
175 assert_template :layout => false
178 # Test the edit page redirects when you aren't logged in
182 assert_response :redirect
183 assert_redirected_to :controller => :users, :action => :login, :referer => "/edit"
186 # Test the error when trying to edit without public edits
187 def test_edit_non_public
188 session_for(create(:user, :data_public => false))
192 assert_response :success
193 assert_template "edit"
194 assert_select "a[href='https://wiki.openstreetmap.org/wiki/Disabling_anonymous_edits']"
197 # Test the right editor gets used when the user hasn't set a preference
198 def test_edit_without_preference
199 session_for(create(:user))
203 assert_response :success
204 assert_template "edit"
205 assert_template :partial => "_#{Settings.default_editor}", :count => 1
208 # Test the right editor gets used when the user has set a preference
209 def test_edit_with_preference
211 user.preferred_editor = "id"
216 assert_response :success
217 assert_template "edit"
218 assert_template :partial => "_id", :count => 1
220 user.preferred_editor = "potlatch2"
224 assert_response :success
225 assert_template "edit"
226 assert_template :partial => "_potlatch2", :count => 1
228 user.preferred_editor = "potlatch"
232 assert_response :success
233 assert_template "edit"
234 assert_template :partial => "_potlatch", :count => 1
236 user.preferred_editor = "remote"
240 assert_response :success
241 assert_template "index"
244 # Test the right editor gets used when the URL has an override
245 def test_edit_with_override
246 session_for(create(:user))
248 get edit_path(:editor => "id")
249 assert_response :success
250 assert_template "edit"
251 assert_template :partial => "_id", :count => 1
253 get edit_path(:editor => "potlatch2")
254 assert_response :success
255 assert_template "edit"
256 assert_template :partial => "_potlatch2", :count => 1
258 get edit_path(:editor => "potlatch")
259 assert_response :success
260 assert_template "edit"
261 assert_template :partial => "_potlatch", :count => 1
263 get edit_path(:editor => "remote")
264 assert_response :success
265 assert_template "index"
268 # Test editing a specific node
269 def test_edit_with_node
271 node = create(:node, :lat => 1.0, :lon => 1.0)
274 get edit_path(:node => node.id)
276 assert_response :success
277 assert_template "edit"
278 assert_in_delta(1.0, assigns(:lat))
279 assert_in_delta(1.0, assigns(:lon))
280 assert_equal 18, assigns(:zoom)
283 # Test editing inaccessible nodes
284 def test_edit_with_inaccessible_nodes
286 deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
289 get edit_path(:node => 99999)
290 assert_response :success
291 assert_template "edit"
292 assert_nil assigns(:lat)
293 assert_nil assigns(:lon)
294 assert_nil assigns(:zoom)
296 get edit_path(:node => deleted_node.id)
297 assert_response :success
298 assert_template "edit"
299 assert_nil assigns(:lat)
300 assert_nil assigns(:lon)
301 assert_nil assigns(:zoom)
304 # Test editing a specific way
305 def test_edit_with_way
307 node = create(:node, :lat => 3, :lon => 3)
309 create(:way_node, :node => node, :way => way)
312 get edit_path(:way => way.id)
313 assert_response :success
314 assert_template "edit"
315 assert_in_delta(3.0, assigns(:lat))
316 assert_in_delta(3.0, assigns(:lon))
317 assert_equal 17, assigns(:zoom)
320 # Test editing inaccessible ways
321 def test_edit_with_inaccessible_ways
323 deleted_way = create(:way, :visible => false)
326 get edit_path(:way => 99999)
327 assert_response :success
328 assert_template "edit"
329 assert_nil assigns(:lat)
330 assert_nil assigns(:lon)
331 assert_nil assigns(:zoom)
333 get edit_path(:way => deleted_way.id)
334 assert_response :success
335 assert_template "edit"
336 assert_nil assigns(:lat)
337 assert_nil assigns(:lon)
338 assert_nil assigns(:zoom)
341 # Test editing a specific note
342 def test_edit_with_note
344 note = create(:note) do |n|
345 n.comments.create(:author_id => user.id)
349 get edit_path(:note => note.id)
350 assert_response :success
351 assert_template "edit"
352 assert_in_delta(1.0, assigns(:lat))
353 assert_in_delta(1.0, assigns(:lon))
354 assert_equal 17, assigns(:zoom)
357 # Test editing inaccessible notes
358 def test_edit_with_inaccessible_notes
360 deleted_note = create(:note, :status => "hidden") do |n|
361 n.comments.create(:author_id => user.id)
365 get edit_path(:note => 99999)
366 assert_response :success
367 assert_template "edit"
368 assert_nil assigns(:lat)
369 assert_nil assigns(:lon)
370 assert_nil assigns(:zoom)
372 get edit_path(:note => deleted_note.id)
373 assert_response :success
374 assert_template "edit"
375 assert_nil assigns(:lat)
376 assert_nil assigns(:lon)
377 assert_nil assigns(:zoom)
380 # Test editing a specific GPX trace
381 def test_edit_with_gpx
383 gpx = create(:trace, :latitude => 1, :longitude => 1)
386 get edit_path(:gpx => gpx.id)
387 assert_response :success
388 assert_template "edit"
389 assert_in_delta(1.0, assigns(:lat))
390 assert_in_delta(1.0, assigns(:lon))
391 assert_equal 16, assigns(:zoom)
394 # Test editing inaccessible GPX traces
395 def test_edit_with_inaccessible_gpxes
397 deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
398 private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "private")
401 get edit_path(:gpx => 99999)
402 assert_response :success
403 assert_template "edit"
404 assert_nil assigns(:lat)
405 assert_nil assigns(:lon)
406 assert_nil assigns(:zoom)
408 get edit_path(:gpx => deleted_gpx.id)
409 assert_response :success
410 assert_template "edit"
411 assert_nil assigns(:lat)
412 assert_nil assigns(:lon)
413 assert_nil assigns(:zoom)
415 get edit_path(:gpx => private_gpx.id)
416 assert_response :success
417 assert_template "edit"
418 assert_nil assigns(:lat)
419 assert_nil assigns(:lon)
420 assert_nil assigns(:zoom)
423 # Test the edit page redirects
424 def test_edit_redirect
425 get edit_path(:lat => 4, :lon => 5)
426 assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5"
428 get edit_path(:lat => 4, :lon => 5, :zoom => 3)
429 assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
431 get edit_path(:lat => 4, :lon => 5, :zoom => 3, :editor => "id")
432 assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5"
435 # Test the copyright page
438 assert_response :success
439 assert_template "copyright"
440 assert_select "div[lang='en'][dir='ltr']"
442 get copyright_path(:copyright_locale => "fr")
443 assert_response :success
444 assert_template "copyright"
445 assert_select "div[lang='fr'][dir='ltr']"
447 get copyright_path(:copyright_locale => "ar")
448 assert_response :success
449 assert_template "copyright"
450 assert_select "div[lang='ar'][dir='rtl']"
453 # Test the welcome page
456 assert_response :redirect
457 assert_redirected_to :controller => :users, :action => :login, :referer => "/welcome"
459 session_for(create(:user))
461 assert_response :success
462 assert_template "welcome"
465 # Test the fixthemap page
468 assert_response :success
469 assert_template "fixthemap"
475 assert_response :success
476 assert_template "help"
479 # Test the about page
482 assert_response :success
483 assert_template "about"
484 assert_select "div[lang='en'][dir='ltr']"
486 get about_path(:about_locale => "fr")
487 assert_response :success
488 assert_template "about"
489 assert_select "div[lang='fr'][dir='ltr']"
491 get about_path(:about_locale => "ar")
492 assert_response :success
493 assert_template "about"
494 assert_select "div[lang='ar'][dir='rtl']"
497 # Test the export page
500 assert_response :success
501 assert_template "export"
502 assert_template :layout => "map"
504 get export_path, :xhr => true
505 assert_response :success
506 assert_template "export"
507 assert_template :layout => "xhr"
510 # Test the offline page
513 assert_response :success
514 assert_template "offline"
517 # Test the rich text preview
519 post preview_path(:type => "html"), :xhr => true
520 assert_response :success
522 post preview_path(:type => "markdown"), :xhr => true
523 assert_response :success
525 post preview_path(:type => "text"), :xhr => true
526 assert_response :success
531 session_for(create(:user))
535 assert_response :success
537 assert_template :layout => false