1 # frozen_string_literal: true
 
   5 class SiteControllerTest < ActionDispatch::IntegrationTest
 
   7   # test all routes which lead to this controller
 
  10       { :path => "/", :method => :get },
 
  11       { :controller => "site", :action => "index" }
 
  14       { :path => "/", :method => :post },
 
  15       { :controller => "site", :action => "index" }
 
  18       { :path => "/edit", :method => :get },
 
  19       { :controller => "site", :action => "edit" }
 
  22       { :controller => "site", :action => "edit", :format => "html" },
 
  23       { :path => "/edit.html", :method => :get }
 
  26       { :path => "/copyright", :method => :get },
 
  27       { :controller => "site", :action => "copyright" }
 
  30       { :path => "/copyright/locale", :method => :get },
 
  31       { :controller => "site", :action => "copyright", :copyright_locale => "locale" }
 
  34       { :path => "/welcome", :method => :get },
 
  35       { :controller => "site", :action => "welcome" }
 
  38       { :path => "/fixthemap", :method => :get },
 
  39       { :controller => "site", :action => "fixthemap" }
 
  42       { :path => "/help", :method => :get },
 
  43       { :controller => "site", :action => "help" }
 
  46       { :path => "/about", :method => :get },
 
  47       { :controller => "site", :action => "about" }
 
  50       { :path => "/about/locale", :method => :get },
 
  51       { :controller => "site", :action => "about", :about_locale => "locale" }
 
  54       { :path => "/export", :method => :get },
 
  55       { :controller => "site", :action => "export" }
 
  58       { :controller => "site", :action => "export", :format => "html" },
 
  59       { :path => "/export.html", :method => :get }
 
  62       { :path => "/offline", :method => :get },
 
  63       { :controller => "site", :action => "offline" }
 
  66       { :path => "/go/shortcode", :method => :get },
 
  67       { :controller => "site", :action => "permalink", :code => "shortcode" }
 
  70       { :path => "/preview/typename", :method => :post },
 
  71       { :controller => "site", :action => "preview", :type => "typename" }
 
  74       { :path => "/id", :method => :get },
 
  75       { :controller => "site", :action => "id" }
 
  83     assert_response :success
 
  84     assert_template "index"
 
  87   # Test the index page redirects
 
  88   def test_index_redirect
 
  89     get root_path(:node => 123)
 
  90     assert_redirected_to node_path(123)
 
  92     get root_path(:way => 123)
 
  93     assert_redirected_to way_path(123)
 
  95     get root_path(:relation => 123)
 
  96     assert_redirected_to relation_path(123)
 
  98     get root_path(:note => 123)
 
  99     assert_redirected_to :controller => :notes, :action => :show, :id => 123
 
 101     get root_path(:query => "test")
 
 102     assert_redirected_to search_path(:query => "test")
 
 104     get root_path(:lat => 4, :lon => 5)
 
 105     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5"
 
 107     get root_path(:lat => 4, :lon => 5, :zoom => 3)
 
 108     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5"
 
 110     get root_path(:layers => "T")
 
 111     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T"
 
 113     get root_path(:notes => "yes")
 
 114     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N"
 
 116     get root_path(:lat => 4, :lon => 5, :zoom => 3, :layers => "T")
 
 117     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5&layers=T"
 
 120   # Test the permalink redirect
 
 122     get permalink_path(:code => "wBz3--")
 
 123     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
 
 125     get permalink_path(:code => "wBz3--", :m => "")
 
 126     assert_redirected_to :controller => :site, :action => :index, :mlat => "4.8779296875", :mlon => "3.955078125", :anchor => "map=3/4.8779296875/3.955078125"
 
 128     get permalink_path(:code => "wBz3--", :layers => "T")
 
 129     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125&layers=T"
 
 131     get permalink_path(:code => "wBz3--", :node => 1)
 
 132     assert_redirected_to node_path(1, :anchor => "map=3/4.8779296875/3.955078125")
 
 134     get permalink_path(:code => "wBz3--", :way => 2)
 
 135     assert_redirected_to way_path(2, :anchor => "map=3/4.8779296875/3.955078125")
 
 137     get permalink_path(:code => "wBz3--", :relation => 3)
 
 138     assert_redirected_to relation_path(3, :anchor => "map=3/4.8779296875/3.955078125")
 
 140     get permalink_path(:code => "wBz3--", :changeset => 4)
 
 141     assert_redirected_to changeset_path(4, :anchor => "map=3/4.8779296875/3.955078125")
 
 144   # Test the edit page redirects when you aren't logged in
 
 148     assert_redirected_to login_path(:referer => "/edit")
 
 151   # Test the error when trying to edit without public edits
 
 152   def test_edit_non_public
 
 153     session_for(create(:user, :data_public => false))
 
 157     assert_response :success
 
 158     assert_template "edit"
 
 159     assert_select "a[href='https://wiki.openstreetmap.org/wiki/Anonymous_edits']"
 
 162   # Test the right editor gets used when the user hasn't set a preference
 
 163   def test_edit_without_preference
 
 164     session_for(create(:user))
 
 168     assert_response :success
 
 169     assert_template "edit"
 
 170     assert_template :partial => "_#{Settings.default_editor}", :count => 1
 
 173   # Test the right editor gets used when the user has set a preference
 
 174   def test_edit_with_preference
 
 176     user.preferred_editor = "id"
 
 181     assert_response :success
 
 182     assert_template "edit"
 
 183     assert_template :partial => "_id", :count => 1
 
 185     user.preferred_editor = "potlatch2"
 
 189     assert_response :success
 
 190     assert_template "edit"
 
 191     assert_template :partial => "_potlatch2", :count => 1
 
 193     user.preferred_editor = "potlatch"
 
 197     assert_response :success
 
 198     assert_template "edit"
 
 199     assert_template :partial => "_potlatch", :count => 1
 
 201     user.preferred_editor = "remote"
 
 205     assert_response :success
 
 206     assert_template "index"
 
 209   # Test the right editor gets used when the URL has an override
 
 210   def test_edit_with_override
 
 211     session_for(create(:user))
 
 213     get edit_path(:editor => "id")
 
 214     assert_response :success
 
 215     assert_template "edit"
 
 216     assert_template :partial => "_id", :count => 1
 
 218     get edit_path(:editor => "potlatch2")
 
 219     assert_response :success
 
 220     assert_template "edit"
 
 221     assert_template :partial => "_potlatch2", :count => 1
 
 223     get edit_path(:editor => "potlatch")
 
 224     assert_response :success
 
 225     assert_template "edit"
 
 226     assert_template :partial => "_potlatch", :count => 1
 
 228     get edit_path(:editor => "remote")
 
 229     assert_response :success
 
 230     assert_template "index"
 
 233   # Test editing a specific node
 
 234   def test_edit_with_node
 
 236     node = create(:node, :lat => 1.0, :lon => 1.0)
 
 239     get edit_path(:node => node.id)
 
 241     assert_response :success
 
 242     assert_template "edit"
 
 243     assert_in_delta(1.0, assigns(:lat))
 
 244     assert_in_delta(1.0, assigns(:lon))
 
 245     assert_equal 18, assigns(:zoom)
 
 248   # Test editing inaccessible nodes
 
 249   def test_edit_with_inaccessible_nodes
 
 251     deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
 
 254     get edit_path(:node => 99999)
 
 255     assert_response :success
 
 256     assert_template "edit"
 
 257     assert_nil assigns(:lat)
 
 258     assert_nil assigns(:lon)
 
 259     assert_nil assigns(:zoom)
 
 261     get edit_path(:node => deleted_node.id)
 
 262     assert_response :success
 
 263     assert_template "edit"
 
 264     assert_nil assigns(:lat)
 
 265     assert_nil assigns(:lon)
 
 266     assert_nil assigns(:zoom)
 
 269   # Test editing a specific way
 
 270   def test_edit_with_way
 
 272     node = create(:node, :lat => 3, :lon => 3)
 
 274     create(:way_node, :node => node, :way => way)
 
 277     get edit_path(:way => way.id)
 
 278     assert_response :success
 
 279     assert_template "edit"
 
 280     assert_in_delta(3.0, assigns(:lat))
 
 281     assert_in_delta(3.0, assigns(:lon))
 
 282     assert_equal 17, assigns(:zoom)
 
 285   # Test editing inaccessible ways
 
 286   def test_edit_with_inaccessible_ways
 
 288     deleted_way = create(:way, :visible => false)
 
 291     get edit_path(:way => 99999)
 
 292     assert_response :success
 
 293     assert_template "edit"
 
 294     assert_nil assigns(:lat)
 
 295     assert_nil assigns(:lon)
 
 296     assert_nil assigns(:zoom)
 
 298     get edit_path(:way => deleted_way.id)
 
 299     assert_response :success
 
 300     assert_template "edit"
 
 301     assert_nil assigns(:lat)
 
 302     assert_nil assigns(:lon)
 
 303     assert_nil assigns(:zoom)
 
 306   # Test editing a specific note
 
 307   def test_edit_with_note
 
 309     note = create(:note) do |n|
 
 310       n.comments.create(:author_id => user.id)
 
 314     get edit_path(:note => note.id)
 
 315     assert_response :success
 
 316     assert_template "edit"
 
 317     assert_in_delta(1.0, assigns(:lat))
 
 318     assert_in_delta(1.0, assigns(:lon))
 
 319     assert_equal 17, assigns(:zoom)
 
 322   # Test editing inaccessible notes
 
 323   def test_edit_with_inaccessible_notes
 
 325     deleted_note = create(:note, :status => "hidden") do |n|
 
 326       n.comments.create(:author_id => user.id)
 
 330     get edit_path(:note => 99999)
 
 331     assert_response :success
 
 332     assert_template "edit"
 
 333     assert_nil assigns(:lat)
 
 334     assert_nil assigns(:lon)
 
 335     assert_nil assigns(:zoom)
 
 337     get edit_path(:note => deleted_note.id)
 
 338     assert_response :success
 
 339     assert_template "edit"
 
 340     assert_nil assigns(:lat)
 
 341     assert_nil assigns(:lon)
 
 342     assert_nil assigns(:zoom)
 
 345   # Test editing a specific GPX trace
 
 346   def test_edit_with_gpx
 
 348     gpx = create(:trace, :latitude => 1, :longitude => 1)
 
 351     get edit_path(:gpx => gpx.id)
 
 352     assert_response :success
 
 353     assert_template "edit"
 
 354     assert_in_delta(1.0, assigns(:lat))
 
 355     assert_in_delta(1.0, assigns(:lon))
 
 356     assert_equal 16, assigns(:zoom)
 
 359   # Test editing inaccessible GPX traces
 
 360   def test_edit_with_inaccessible_gpxes
 
 362     deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
 
 363     private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "private")
 
 366     get edit_path(:gpx => 99999)
 
 367     assert_response :success
 
 368     assert_template "edit"
 
 369     assert_nil assigns(:lat)
 
 370     assert_nil assigns(:lon)
 
 371     assert_nil assigns(:zoom)
 
 373     get edit_path(:gpx => deleted_gpx.id)
 
 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_path(:gpx => private_gpx.id)
 
 381     assert_response :success
 
 382     assert_template "edit"
 
 383     assert_nil assigns(:lat)
 
 384     assert_nil assigns(:lon)
 
 385     assert_nil assigns(:zoom)
 
 388   # Test the edit page redirects
 
 389   def test_edit_redirect
 
 390     get edit_path(:lat => 4, :lon => 5)
 
 391     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5"
 
 393     get edit_path(:lat => 4, :lon => 5, :zoom => 3)
 
 394     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
 
 396     get edit_path(:lat => 4, :lon => 5, :zoom => 3, :editor => "id")
 
 397     assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5"
 
 400   # Test the copyright page
 
 403     assert_response :success
 
 404     assert_template "copyright"
 
 405     assert_select "div[lang='en'][dir='ltr']"
 
 407     get copyright_path(:copyright_locale => "fr")
 
 408     assert_response :success
 
 409     assert_template "copyright"
 
 410     assert_select "div[lang='fr'][dir='ltr']"
 
 412     get copyright_path(:copyright_locale => "ar")
 
 413     assert_response :success
 
 414     assert_template "copyright"
 
 415     assert_select "div[lang='ar'][dir='rtl']"
 
 418   # Test the welcome page
 
 421     assert_redirected_to login_path(:referer => "/welcome")
 
 423     session_for(create(:user))
 
 425     assert_response :success
 
 426     assert_template "welcome"
 
 429   # Test the fixthemap page
 
 432     assert_response :success
 
 433     assert_template "fixthemap"
 
 439     assert_response :success
 
 440     assert_template "help"
 
 443   # Test the about page
 
 446     assert_response :success
 
 447     assert_template "about"
 
 448     assert_select "div[lang='en'][dir='ltr']"
 
 450     get about_path(:about_locale => "fr")
 
 451     assert_response :success
 
 452     assert_template "about"
 
 453     assert_select "div[lang='fr'][dir='ltr']"
 
 455     get about_path(:about_locale => "ar")
 
 456     assert_response :success
 
 457     assert_template "about"
 
 458     assert_select "div[lang='ar'][dir='rtl']"
 
 460     # Page should still render even with incorrect locale
 
 461     get about_path(:about_locale => "zzz")
 
 462     assert_response :success
 
 463     assert_template "about"
 
 466   # Test the export page
 
 469     assert_response :success
 
 470     assert_template "export"
 
 471     assert_template :layout => "map"
 
 473     get export_path, :xhr => true
 
 474     assert_response :success
 
 475     assert_template "export"
 
 476     assert_template :layout => "xhr"
 
 479   # Test the offline page
 
 482     assert_response :success
 
 483     assert_select ".alert-warning"
 
 486   # Test the rich text preview
 
 488     post preview_path(:type => "html"), :xhr => true
 
 489     assert_response :success
 
 491     post preview_path(:type => "markdown"), :xhr => true
 
 492     assert_response :success
 
 494     post preview_path(:type => "text"), :xhr => true
 
 495     assert_response :success
 
 500     session_for(create(:user))
 
 504     assert_response :success
 
 506     assert_template :layout => false
 
 509   # Test the id frame when not logged in
 
 510   def test_id_without_login
 
 513     assert_redirected_to login_path(:referer => "/id")