X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/93fb360a08e388997e402faa6d5804580b11b1c5..64b3e289ac14533c6fb2b1223b6417ad0446878c:/test/controllers/site_controller_test.rb diff --git a/test/controllers/site_controller_test.rb b/test/controllers/site_controller_test.rb index 776e9448a..4dc07f996 100644 --- a/test/controllers/site_controller_test.rb +++ b/test/controllers/site_controller_test.rb @@ -1,20 +1,20 @@ require "test_helper" class SiteControllerTest < ActionController::TestCase - api_fixtures - ## # setup oauth keys def setup - Object.const_set("ID_KEY", client_applications(:oauth_web_app).key) - Object.const_set("POTLATCH2_KEY", client_applications(:oauth_web_app).key) + super + + Settings.id_key = create(:client_application).key + Settings.potlatch2_key = create(:client_application).key end ## # clear oauth keys def teardown - Object.send("remove_const", "ID_KEY") - Object.send("remove_const", "POTLATCH2_KEY") + Settings.id_key = nil + Settings.potlatch2_key = nil end ## @@ -52,6 +52,18 @@ class SiteControllerTest < ActionController::TestCase { :path => "/fixthemap", :method => :get }, { :controller => "site", :action => "fixthemap" } ) + assert_routing( + { :path => "/help", :method => :get }, + { :controller => "site", :action => "help" } + ) + assert_routing( + { :path => "/about", :method => :get }, + { :controller => "site", :action => "about" } + ) + assert_routing( + { :path => "/about/locale", :method => :get }, + { :controller => "site", :action => "about", :about_locale => "locale" } + ) assert_routing( { :path => "/export", :method => :get }, { :controller => "site", :action => "export" } @@ -73,8 +85,8 @@ class SiteControllerTest < ActionController::TestCase { :controller => "site", :action => "permalink", :code => "shortcode" } ) assert_routing( - { :path => "/preview/formatname", :method => :post }, - { :controller => "site", :action => "preview", :format => "formatname" } + { :path => "/preview/typename", :method => :post }, + { :controller => "site", :action => "preview", :type => "typename" } ) assert_routing( { :path => "/id", :method => :get }, @@ -91,56 +103,71 @@ class SiteControllerTest < ActionController::TestCase # Test the index page redirects def test_index_redirect - get :index, :lat => 4, :lon => 5 + get :index, :params => { :node => 123 } + assert_redirected_to :controller => :browse, :action => :node, :id => 123 + + get :index, :params => { :way => 123 } + assert_redirected_to :controller => :browse, :action => :way, :id => 123 + + get :index, :params => { :relation => 123 } + assert_redirected_to :controller => :browse, :action => :relation, :id => 123 + + get :index, :params => { :note => 123 } + assert_redirected_to :controller => :browse, :action => :note, :id => 123 + + get :index, :params => { :query => "test" } + assert_redirected_to :controller => :geocoder, :action => :search, :query => "test" + + get :index, :params => { :lat => 4, :lon => 5 } assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5" - get :index, :lat => 4, :lon => 5, :zoom => 3 + get :index, :params => { :lat => 4, :lon => 5, :zoom => 3 } assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5" - get :index, :layers => "T" + get :index, :params => { :layers => "T" } assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T" - get :index, :notes => "yes" + get :index, :params => { :notes => "yes" } assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N" - get :index, :lat => 4, :lon => 5, :zoom => 3, :layers => "T" + get :index, :params => { :lat => 4, :lon => 5, :zoom => 3, :layers => "T" } assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5&layers=T" end # Test the permalink redirect def test_permalink - get :permalink, :code => "wBz3--" + get :permalink, :params => { :code => "wBz3--" } assert_response :redirect assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125" - get :permalink, :code => "wBz3--", :m => "" + get :permalink, :params => { :code => "wBz3--", :m => "" } assert_response :redirect assert_redirected_to :controller => :site, :action => :index, :mlat => "4.8779296875", :mlon => "3.955078125", :anchor => "map=3/4.8779296875/3.955078125" - get :permalink, :code => "wBz3--", :layers => "T" + get :permalink, :params => { :code => "wBz3--", :layers => "T" } assert_response :redirect assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125&layers=T" - get :permalink, :code => "wBz3--", :node => 1 + get :permalink, :params => { :code => "wBz3--", :node => 1 } assert_response :redirect assert_redirected_to :controller => :browse, :action => :node, :id => 1, :anchor => "map=3/4.8779296875/3.955078125" - get :permalink, :code => "wBz3--", :way => 2 + get :permalink, :params => { :code => "wBz3--", :way => 2 } assert_response :redirect assert_redirected_to :controller => :browse, :action => :way, :id => 2, :anchor => "map=3/4.8779296875/3.955078125" - get :permalink, :code => "wBz3--", :relation => 3 + get :permalink, :params => { :code => "wBz3--", :relation => 3 } assert_response :redirect assert_redirected_to :controller => :browse, :action => :relation, :id => 3, :anchor => "map=3/4.8779296875/3.955078125" - get :permalink, :code => "wBz3--", :changeset => 4 + get :permalink, :params => { :code => "wBz3--", :changeset => 4 } assert_response :redirect assert_redirected_to :controller => :browse, :action => :changeset, :id => 4, :anchor => "map=3/4.8779296875/3.955078125" end # Test the key page def test_key - xhr :get, :key + get :key, :xhr => true assert_response :success assert_template "key" assert_template :layout => false @@ -150,123 +177,126 @@ class SiteControllerTest < ActionController::TestCase def test_edit get :edit assert_response :redirect - assert_redirected_to :controller => :user, :action => :login, :referer => "/edit" + assert_redirected_to :controller => :users, :action => :login, :referer => "/edit" + end + + # Test the error when trying to edit without public edits + def test_edit_non_public + get :edit, :session => { :user => create(:user, :data_public => false) } + assert_response :success + assert_template "edit" + assert_select "a[href='https://wiki.openstreetmap.org/wiki/Disabling_anonymous_edits']" end # Test the right editor gets used when the user hasn't set a preference def test_edit_without_preference - get :edit, nil, :user => users(:public_user).id + get :edit, :session => { :user => create(:user) } assert_response :success assert_template "edit" - assert_template :partial => "_#{DEFAULT_EDITOR}", :count => 1 + assert_template :partial => "_#{Settings.default_editor}", :count => 1 end # Test the right editor gets used when the user has set a preference def test_edit_with_preference - user = users(:public_user) + user = create(:user) user.preferred_editor = "id" user.save! - get :edit, nil, :user => user.id + get :edit, :session => { :user => user } assert_response :success assert_template "edit" assert_template :partial => "_id", :count => 1 - user = users(:public_user) + user = create(:user) user.preferred_editor = "potlatch2" user.save! - get :edit, nil, :user => user.id + get :edit, :session => { :user => user } assert_response :success assert_template "edit" assert_template :partial => "_potlatch2", :count => 1 - user = users(:public_user) + user = create(:user) user.preferred_editor = "potlatch" user.save! - get :edit, nil, :user => user.id + get :edit, :session => { :user => user } assert_response :success assert_template "edit" assert_template :partial => "_potlatch", :count => 1 - user = users(:public_user) + user = create(:user) user.preferred_editor = "remote" user.save! - get :edit, nil, :user => user.id + get :edit, :session => { :user => user } assert_response :success assert_template "index" end # Test the right editor gets used when the URL has an override def test_edit_with_override - get :edit, { :editor => "id" }, { :user => users(:public_user).id } + get :edit, :params => { :editor => "id" }, :session => { :user => create(:user) } assert_response :success assert_template "edit" assert_template :partial => "_id", :count => 1 - get :edit, { :editor => "potlatch2" }, { :user => users(:public_user).id } + get :edit, :params => { :editor => "potlatch2" }, :session => { :user => create(:user) } assert_response :success assert_template "edit" assert_template :partial => "_potlatch2", :count => 1 - get :edit, { :editor => "potlatch" }, { :user => users(:public_user).id } + get :edit, :params => { :editor => "potlatch" }, :session => { :user => create(:user) } assert_response :success assert_template "edit" assert_template :partial => "_potlatch", :count => 1 - get :edit, { :editor => "remote" }, { :user => users(:public_user).id } + get :edit, :params => { :editor => "remote" }, :session => { :user => create(:user) } assert_response :success assert_template "index" end - # Test the right editor gets used when the browser is IE - def test_edit_with_ie - @request.env["HTTP_USER_AGENT"] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)" - - get :edit, {}, { :user => users(:public_user).id } - assert_response :success - assert_template "edit" - assert_template :partial => "_potlatch2", :count => 1 + # Test editing a specific node + def test_edit_with_node + user = create(:user) + node = create(:node, :lat => 1.0, :lon => 1.0) - get :edit, { :editor => "id" }, { :user => users(:public_user).id } + get :edit, :params => { :node => node.id }, :session => { :user => user } assert_response :success assert_template "edit" - assert_template :partial => "_potlatch2", :count => 1 - - @request.env["HTTP_USER_AGENT"] = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko" + assert_equal 1.0, assigns(:lat) + assert_equal 1.0, assigns(:lon) + assert_equal 18, assigns(:zoom) + end - get :edit, {}, { :user => users(:public_user).id } - assert_response :success - assert_template "edit" - assert_template :partial => "_potlatch2", :count => 1 + # Test editing inaccessible nodes + def test_edit_with_inaccessible_nodes + user = create(:user) + deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false) - get :edit, { :editor => "id" }, { :user => users(:public_user).id } + get :edit, :params => { :node => 99999 }, :session => { :user => user } assert_response :success assert_template "edit" - assert_template :partial => "_potlatch2", :count => 1 - end - - # Test editing a specific node - def test_edit_with_node - user = users(:public_user) - node = current_nodes(:visible_node) + assert_nil assigns(:lat) + assert_nil assigns(:lon) + assert_nil assigns(:zoom) - get :edit, { :node => node.id }, { :user => user.id } + get :edit, :params => { :node => deleted_node.id }, :session => { :user => user } assert_response :success assert_template "edit" - assert_equal 1.0, assigns(:lat) - assert_equal 1.0, assigns(:lon) - assert_equal 18, assigns(:zoom) + assert_nil assigns(:lat) + assert_nil assigns(:lon) + assert_nil assigns(:zoom) end # Test editing a specific way def test_edit_with_way - user = users(:public_user) - way = current_ways(:visible_way) + user = create(:user) + node = create(:node, :lat => 3, :lon => 3) + way = create(:way) + create(:way_node, :node => node, :way => way) - get :edit, { :way => way.id }, { :user => user.id } + get :edit, :params => { :way => way.id }, :session => { :user => user } assert_response :success assert_template "edit" assert_equal 3.0, assigns(:lat) @@ -274,12 +304,34 @@ class SiteControllerTest < ActionController::TestCase assert_equal 17, assigns(:zoom) end + # Test editing inaccessible ways + def test_edit_with_inaccessible_ways + user = create(:user) + deleted_way = create(:way, :visible => false) + + get :edit, :params => { :way => 99999 }, :session => { :user => user } + assert_response :success + assert_template "edit" + assert_nil assigns(:lat) + assert_nil assigns(:lon) + assert_nil assigns(:zoom) + + get :edit, :params => { :way => deleted_way.id }, :session => { :user => user } + assert_response :success + assert_template "edit" + assert_nil assigns(:lat) + assert_nil assigns(:lon) + assert_nil assigns(:zoom) + end + # Test editing a specific note def test_edit_with_note - user = users(:public_user) - note = notes(:open_note) + user = create(:user) + note = create(:note) do |n| + n.comments.create(:author_id => user.id) + end - get :edit, { :note => note.id }, { :user => user.id } + get :edit, :params => { :note => note.id }, :session => { :user => user } assert_response :success assert_template "edit" assert_equal 1.0, assigns(:lat) @@ -287,12 +339,34 @@ class SiteControllerTest < ActionController::TestCase assert_equal 17, assigns(:zoom) end + # Test editing inaccessible notes + def test_edit_with_inaccessible_notes + user = create(:user) + deleted_note = create(:note, :status => "hidden") do |n| + n.comments.create(:author_id => user.id) + end + + get :edit, :params => { :note => 99999 }, :session => { :user => user } + assert_response :success + assert_template "edit" + assert_nil assigns(:lat) + assert_nil assigns(:lon) + assert_nil assigns(:zoom) + + get :edit, :params => { :note => deleted_note.id }, :session => { :user => user } + assert_response :success + assert_template "edit" + assert_nil assigns(:lat) + assert_nil assigns(:lon) + assert_nil assigns(:zoom) + end + # Test editing a specific GPX trace def test_edit_with_gpx - user = users(:public_user) - gpx = gpx_files(:public_trace_file) + user = create(:user) + gpx = create(:trace, :latitude => 1, :longitude => 1) - get :edit, { :gpx => gpx.id }, { :user => user.id } + get :edit, :params => { :gpx => gpx.id }, :session => { :user => user } assert_response :success assert_template "edit" assert_equal 1.0, assigns(:lat) @@ -300,15 +374,43 @@ class SiteControllerTest < ActionController::TestCase assert_equal 16, assigns(:zoom) end + # Test editing inaccessible GPX traces + def test_edit_with_inaccessible_gpxes + user = create(:user) + deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1) + private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "private") + + get :edit, :params => { :gpx => 99999 }, :session => { :user => user } + assert_response :success + assert_template "edit" + assert_nil assigns(:lat) + assert_nil assigns(:lon) + assert_nil assigns(:zoom) + + get :edit, :params => { :gpx => deleted_gpx.id }, :session => { :user => user } + assert_response :success + assert_template "edit" + assert_nil assigns(:lat) + assert_nil assigns(:lon) + assert_nil assigns(:zoom) + + get :edit, :params => { :gpx => private_gpx.id }, :session => { :user => user } + assert_response :success + assert_template "edit" + assert_nil assigns(:lat) + assert_nil assigns(:lon) + assert_nil assigns(:zoom) + end + # Test the edit page redirects def test_edit_redirect - get :edit, :lat => 4, :lon => 5 + get :edit, :params => { :lat => 4, :lon => 5 } assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5" - get :edit, :lat => 4, :lon => 5, :zoom => 3 + get :edit, :params => { :lat => 4, :lon => 5, :zoom => 3 } assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5" - get :edit, :lat => 4, :lon => 5, :zoom => 3, :editor => "id" + get :edit, :params => { :lat => 4, :lon => 5, :zoom => 3, :editor => "id" } assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5" end @@ -317,15 +419,26 @@ class SiteControllerTest < ActionController::TestCase get :copyright assert_response :success assert_template "copyright" + assert_select "div[lang='en'][dir='ltr']" + + get :copyright, :params => { :copyright_locale => "fr" } + assert_response :success + assert_template "copyright" + assert_select "div[lang='fr'][dir='ltr']" + + get :copyright, :params => { :copyright_locale => "ar" } + assert_response :success + assert_template "copyright" + assert_select "div[lang='ar'][dir='rtl']" end # Test the welcome page def test_welcome get :welcome assert_response :redirect - assert_redirected_to :controller => :user, :action => :login, :referer => "/welcome" + assert_redirected_to :controller => :users, :action => :login, :referer => "/welcome" - get :welcome, nil, :user => users(:public_user).id + get :welcome, :session => { :user => create(:user) } assert_response :success assert_template "welcome" end @@ -349,6 +462,17 @@ class SiteControllerTest < ActionController::TestCase get :about assert_response :success assert_template "about" + assert_select "div[lang='en'][dir='ltr']" + + get :about, :params => { :about_locale => "fr" } + assert_response :success + assert_template "about" + assert_select "div[lang='fr'][dir='ltr']" + + get :about, :params => { :about_locale => "ar" } + assert_response :success + assert_template "about" + assert_select "div[lang='ar'][dir='rtl']" end # Test the export page @@ -358,7 +482,7 @@ class SiteControllerTest < ActionController::TestCase assert_template "export" assert_template :layout => "map" - xhr :get, :export + get :export, :xhr => true assert_response :success assert_template "export" assert_template :layout => "xhr" @@ -373,19 +497,19 @@ class SiteControllerTest < ActionController::TestCase # Test the rich text preview def test_preview - xhr :post, :preview, :format => "html" + post :preview, :xhr => true, :params => { :type => "html" } assert_response :success - xhr :post, :preview, :format => "markdown" + post :preview, :xhr => true, :params => { :type => "markdown" } assert_response :success - xhr :post, :preview, :format => "text" + post :preview, :xhr => true, :params => { :type => "text" } assert_response :success end # Test the id frame def test_id - get :id, nil, :user => users(:public_user).id + get :id, :session => { :user => create(:user) } assert_response :success assert_template "id" assert_template :layout => false