3 class SiteControllerTest < ActionDispatch::IntegrationTest
 
   5   # test all routes which lead to this controller
 
   8       { :path => "/", :method => :get },
 
   9       { :controller => "site", :action => "index" }
 
  12       { :path => "/", :method => :post },
 
  13       { :controller => "site", :action => "index" }
 
  16       { :path => "/edit", :method => :get },
 
  17       { :controller => "site", :action => "edit" }
 
  20       { :controller => "site", :action => "edit", :format => "html" },
 
  21       { :path => "/edit.html", :method => :get }
 
  24       { :path => "/copyright", :method => :get },
 
  25       { :controller => "site", :action => "copyright" }
 
  28       { :path => "/copyright/locale", :method => :get },
 
  29       { :controller => "site", :action => "copyright", :copyright_locale => "locale" }
 
  32       { :path => "/welcome", :method => :get },
 
  33       { :controller => "site", :action => "welcome" }
 
  36       { :path => "/fixthemap", :method => :get },
 
  37       { :controller => "site", :action => "fixthemap" }
 
  40       { :path => "/help", :method => :get },
 
  41       { :controller => "site", :action => "help" }
 
  44       { :path => "/about", :method => :get },
 
  45       { :controller => "site", :action => "about" }
 
  48       { :path => "/about/locale", :method => :get },
 
  49       { :controller => "site", :action => "about", :about_locale => "locale" }
 
  52       { :path => "/export", :method => :get },
 
  53       { :controller => "site", :action => "export" }
 
  56       { :controller => "site", :action => "export", :format => "html" },
 
  57       { :path => "/export.html", :method => :get }
 
  60       { :path => "/offline", :method => :get },
 
  61       { :controller => "site", :action => "offline" }
 
  64       { :path => "/go/shortcode", :method => :get },
 
  65       { :controller => "site", :action => "permalink", :code => "shortcode" }
 
  68       { :path => "/preview/typename", :method => :post },
 
  69       { :controller => "site", :action => "preview", :type => "typename" }
 
  72       { :path => "/id", :method => :get },
 
  73       { :controller => "site", :action => "id" }
 
  81     assert_response :success
 
  82     assert_template "index"
 
  85   # Test the index page redirects
 
  86   def test_index_redirect
 
  87     get root_path(:node => 123)
 
  88     assert_redirected_to node_path(123)
 
  90     get root_path(:way => 123)
 
  91     assert_redirected_to way_path(123)
 
  93     get root_path(:relation => 123)
 
  94     assert_redirected_to relation_path(123)
 
  96     get root_path(:note => 123)
 
  97     assert_redirected_to :controller => :notes, :action => :show, :id => 123
 
  99     get root_path(:query => "test")
 
 100     assert_redirected_to search_path(:query => "test")
 
 102     get root_path(:lat => 4, :lon => 5)
 
 103     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5"
 
 105     get root_path(:lat => 4, :lon => 5, :zoom => 3)
 
 106     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5"
 
 108     get root_path(:layers => "T")
 
 109     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T"
 
 111     get root_path(:notes => "yes")
 
 112     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N"
 
 114     get root_path(:lat => 4, :lon => 5, :zoom => 3, :layers => "T")
 
 115     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5&layers=T"
 
 118   # Test the permalink redirect
 
 120     get permalink_path(:code => "wBz3--")
 
 121     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
 
 123     get permalink_path(:code => "wBz3--", :m => "")
 
 124     assert_redirected_to :controller => :site, :action => :index, :mlat => "4.8779296875", :mlon => "3.955078125", :anchor => "map=3/4.8779296875/3.955078125"
 
 126     get permalink_path(:code => "wBz3--", :layers => "T")
 
 127     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125&layers=T"
 
 129     get permalink_path(:code => "wBz3--", :node => 1)
 
 130     assert_redirected_to node_path(1, :anchor => "map=3/4.8779296875/3.955078125")
 
 132     get permalink_path(:code => "wBz3--", :way => 2)
 
 133     assert_redirected_to way_path(2, :anchor => "map=3/4.8779296875/3.955078125")
 
 135     get permalink_path(:code => "wBz3--", :relation => 3)
 
 136     assert_redirected_to relation_path(3, :anchor => "map=3/4.8779296875/3.955078125")
 
 138     get permalink_path(:code => "wBz3--", :changeset => 4)
 
 139     assert_redirected_to changeset_path(4, :anchor => "map=3/4.8779296875/3.955078125")
 
 142   # Test the edit page redirects when you aren't logged in
 
 146     assert_redirected_to login_path(:referer => "/edit")
 
 149   # Test the error when trying to edit without public edits
 
 150   def test_edit_non_public
 
 151     session_for(create(:user, :data_public => false))
 
 155     assert_response :success
 
 156     assert_template "edit"
 
 157     assert_select "a[href='https://wiki.openstreetmap.org/wiki/Anonymous_edits']"
 
 160   # Test the right editor gets used when the user hasn't set a preference
 
 161   def test_edit_without_preference
 
 162     session_for(create(:user))
 
 166     assert_response :success
 
 167     assert_template "edit"
 
 168     assert_template :partial => "_#{Settings.default_editor}", :count => 1
 
 171   # Test the right editor gets used when the user has set a preference
 
 172   def test_edit_with_preference
 
 174     user.preferred_editor = "id"
 
 179     assert_response :success
 
 180     assert_template "edit"
 
 181     assert_template :partial => "_id", :count => 1
 
 183     user.preferred_editor = "potlatch2"
 
 187     assert_response :success
 
 188     assert_template "edit"
 
 189     assert_template :partial => "_potlatch2", :count => 1
 
 191     user.preferred_editor = "potlatch"
 
 195     assert_response :success
 
 196     assert_template "edit"
 
 197     assert_template :partial => "_potlatch", :count => 1
 
 199     user.preferred_editor = "remote"
 
 203     assert_response :success
 
 204     assert_template "index"
 
 207   # Test the right editor gets used when the URL has an override
 
 208   def test_edit_with_override
 
 209     session_for(create(:user))
 
 211     get edit_path(:editor => "id")
 
 212     assert_response :success
 
 213     assert_template "edit"
 
 214     assert_template :partial => "_id", :count => 1
 
 216     get edit_path(:editor => "potlatch2")
 
 217     assert_response :success
 
 218     assert_template "edit"
 
 219     assert_template :partial => "_potlatch2", :count => 1
 
 221     get edit_path(:editor => "potlatch")
 
 222     assert_response :success
 
 223     assert_template "edit"
 
 224     assert_template :partial => "_potlatch", :count => 1
 
 226     get edit_path(:editor => "remote")
 
 227     assert_response :success
 
 228     assert_template "index"
 
 231   # Test editing a specific node
 
 232   def test_edit_with_node
 
 234     node = create(:node, :lat => 1.0, :lon => 1.0)
 
 237     get edit_path(:node => node.id)
 
 239     assert_response :success
 
 240     assert_template "edit"
 
 241     assert_in_delta(1.0, assigns(:lat))
 
 242     assert_in_delta(1.0, assigns(:lon))
 
 243     assert_equal 18, assigns(:zoom)
 
 246   # Test editing inaccessible nodes
 
 247   def test_edit_with_inaccessible_nodes
 
 249     deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
 
 252     get edit_path(:node => 99999)
 
 253     assert_response :success
 
 254     assert_template "edit"
 
 255     assert_nil assigns(:lat)
 
 256     assert_nil assigns(:lon)
 
 257     assert_nil assigns(:zoom)
 
 259     get edit_path(:node => deleted_node.id)
 
 260     assert_response :success
 
 261     assert_template "edit"
 
 262     assert_nil assigns(:lat)
 
 263     assert_nil assigns(:lon)
 
 264     assert_nil assigns(:zoom)
 
 267   # Test editing a specific way
 
 268   def test_edit_with_way
 
 270     node = create(:node, :lat => 3, :lon => 3)
 
 272     create(:way_node, :node => node, :way => way)
 
 275     get edit_path(:way => way.id)
 
 276     assert_response :success
 
 277     assert_template "edit"
 
 278     assert_in_delta(3.0, assigns(:lat))
 
 279     assert_in_delta(3.0, assigns(:lon))
 
 280     assert_equal 17, assigns(:zoom)
 
 283   # Test editing inaccessible ways
 
 284   def test_edit_with_inaccessible_ways
 
 286     deleted_way = create(:way, :visible => false)
 
 289     get edit_path(:way => 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(:way => deleted_way.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 note
 
 305   def test_edit_with_note
 
 307     note = create(:note) do |n|
 
 308       n.comments.create(:author_id => user.id)
 
 312     get edit_path(:note => note.id)
 
 313     assert_response :success
 
 314     assert_template "edit"
 
 315     assert_in_delta(1.0, assigns(:lat))
 
 316     assert_in_delta(1.0, assigns(:lon))
 
 317     assert_equal 17, assigns(:zoom)
 
 320   # Test editing inaccessible notes
 
 321   def test_edit_with_inaccessible_notes
 
 323     deleted_note = create(:note, :status => "hidden") do |n|
 
 324       n.comments.create(:author_id => user.id)
 
 328     get edit_path(:note => 99999)
 
 329     assert_response :success
 
 330     assert_template "edit"
 
 331     assert_nil assigns(:lat)
 
 332     assert_nil assigns(:lon)
 
 333     assert_nil assigns(:zoom)
 
 335     get edit_path(:note => deleted_note.id)
 
 336     assert_response :success
 
 337     assert_template "edit"
 
 338     assert_nil assigns(:lat)
 
 339     assert_nil assigns(:lon)
 
 340     assert_nil assigns(:zoom)
 
 343   # Test editing a specific GPX trace
 
 344   def test_edit_with_gpx
 
 346     gpx = create(:trace, :latitude => 1, :longitude => 1)
 
 349     get edit_path(:gpx => gpx.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 16, assigns(:zoom)
 
 357   # Test editing inaccessible GPX traces
 
 358   def test_edit_with_inaccessible_gpxes
 
 360     deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
 
 361     private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "private")
 
 364     get edit_path(:gpx => 99999)
 
 365     assert_response :success
 
 366     assert_template "edit"
 
 367     assert_nil assigns(:lat)
 
 368     assert_nil assigns(:lon)
 
 369     assert_nil assigns(:zoom)
 
 371     get edit_path(:gpx => deleted_gpx.id)
 
 372     assert_response :success
 
 373     assert_template "edit"
 
 374     assert_nil assigns(:lat)
 
 375     assert_nil assigns(:lon)
 
 376     assert_nil assigns(:zoom)
 
 378     get edit_path(:gpx => private_gpx.id)
 
 379     assert_response :success
 
 380     assert_template "edit"
 
 381     assert_nil assigns(:lat)
 
 382     assert_nil assigns(:lon)
 
 383     assert_nil assigns(:zoom)
 
 386   # Test the edit page redirects
 
 387   def test_edit_redirect
 
 388     get edit_path(:lat => 4, :lon => 5)
 
 389     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5"
 
 391     get edit_path(:lat => 4, :lon => 5, :zoom => 3)
 
 392     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
 
 394     get edit_path(:lat => 4, :lon => 5, :zoom => 3, :editor => "id")
 
 395     assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5"
 
 398   # Test the copyright page
 
 401     assert_response :success
 
 402     assert_template "copyright"
 
 403     assert_select "div[lang='en'][dir='ltr']"
 
 405     get copyright_path(:copyright_locale => "fr")
 
 406     assert_response :success
 
 407     assert_template "copyright"
 
 408     assert_select "div[lang='fr'][dir='ltr']"
 
 410     get copyright_path(:copyright_locale => "ar")
 
 411     assert_response :success
 
 412     assert_template "copyright"
 
 413     assert_select "div[lang='ar'][dir='rtl']"
 
 416   # Test the welcome page
 
 419     assert_redirected_to login_path(:referer => "/welcome")
 
 421     session_for(create(:user))
 
 423     assert_response :success
 
 424     assert_template "welcome"
 
 427   # Test the fixthemap page
 
 430     assert_response :success
 
 431     assert_template "fixthemap"
 
 437     assert_response :success
 
 438     assert_template "help"
 
 441   # Test the about page
 
 444     assert_response :success
 
 445     assert_template "about"
 
 446     assert_select "div[lang='en'][dir='ltr']"
 
 448     get about_path(:about_locale => "fr")
 
 449     assert_response :success
 
 450     assert_template "about"
 
 451     assert_select "div[lang='fr'][dir='ltr']"
 
 453     get about_path(:about_locale => "ar")
 
 454     assert_response :success
 
 455     assert_template "about"
 
 456     assert_select "div[lang='ar'][dir='rtl']"
 
 458     # Page should still render even with incorrect locale
 
 459     get about_path(:about_locale => "zzz")
 
 460     assert_response :success
 
 461     assert_template "about"
 
 464   # Test the export page
 
 467     assert_response :success
 
 468     assert_template "export"
 
 469     assert_template :layout => "map"
 
 471     get export_path, :xhr => true
 
 472     assert_response :success
 
 473     assert_template "export"
 
 474     assert_template :layout => "xhr"
 
 477   # Test the offline page
 
 480     assert_response :success
 
 481     assert_select ".alert-warning"
 
 484   # Test the rich text preview
 
 486     post preview_path(:type => "html"), :xhr => true
 
 487     assert_response :success
 
 489     post preview_path(:type => "markdown"), :xhr => true
 
 490     assert_response :success
 
 492     post preview_path(:type => "text"), :xhr => true
 
 493     assert_response :success
 
 498     session_for(create(:user))
 
 502     assert_response :success
 
 504     assert_template :layout => false
 
 507   # Test the id frame when not logged in
 
 508   def test_id_without_login
 
 511     assert_redirected_to login_path(:referer => "/id")