Mercurial > hg > soundsoftware-site
diff test/functional/welcome_controller_test.rb @ 1526:404aa68d4227
Merge from live branch
author | Chris Cannam |
---|---|
date | Thu, 11 Sep 2014 12:46:20 +0100 |
parents | dffacf8a6908 |
children |
line wrap: on
line diff
--- a/test/functional/welcome_controller_test.rb Mon Mar 17 08:57:04 2014 +0000 +++ b/test/functional/welcome_controller_test.rb Thu Sep 11 12:46:20 2014 +0100 @@ -1,5 +1,5 @@ # Redmine - project management software -# Copyright (C) 2006-2012 Jean-Philippe Lang +# Copyright (C) 2006-2014 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -16,18 +16,11 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. require File.expand_path('../../test_helper', __FILE__) -require 'welcome_controller' - -# Re-raise errors caught by the controller. -class WelcomeController; def rescue_action(e) raise e end; end class WelcomeControllerTest < ActionController::TestCase fixtures :projects, :news, :users, :members def setup - @controller = WelcomeController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new User.current = nil end @@ -37,7 +30,7 @@ assert_template 'index' assert_not_nil assigns(:news) assert_not_nil assigns(:projects) - assert !assigns(:projects).include?(Project.find(:first, :conditions => {:is_public => false})) + assert !assigns(:projects).include?(Project.where(:is_public => false).first) end def test_browser_language @@ -61,6 +54,35 @@ assert_equal :fr, @controller.current_language end + def test_browser_language_should_be_ignored_with_force_default_language_for_anonymous + Setting.default_language = 'en' + @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' + with_settings :force_default_language_for_anonymous => '1' do + get :index + assert_equal :en, @controller.current_language + end + end + + def test_user_language_should_be_used + Setting.default_language = 'fi' + user = User.find(2).update_attribute :language, 'it' + @request.session[:user_id] = 2 + @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' + get :index + assert_equal :it, @controller.current_language + end + + def test_user_language_should_be_ignored_if_force_default_language_for_loggedin + Setting.default_language = 'fi' + user = User.find(2).update_attribute :language, 'it' + @request.session[:user_id] = 2 + @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' + with_settings :force_default_language_for_loggedin => '1' do + get :index + assert_equal :fi, @controller.current_language + end + end + def test_robots get :robots assert_response :success @@ -92,6 +114,13 @@ :content => %r{warnLeavingUnsaved} end + def test_logout_link_should_post + @request.session[:user_id] = 2 + + get :index + assert_select 'a[href=/logout][data-method=post]', :text => 'Sign out' + end + def test_call_hook_mixed_in assert @controller.respond_to?(:call_hook) end @@ -106,60 +135,50 @@ end end - context "test_api_offset_and_limit" do - context "without params" do - should "return 0, 25" do - assert_equal [0, 25], @controller.api_offset_and_limit({}) - end - end + def test_api_offset_and_limit_without_params + assert_equal [0, 25], @controller.api_offset_and_limit({}) + end - context "with limit" do - should "return 0, limit" do - assert_equal [0, 30], @controller.api_offset_and_limit({:limit => 30}) - end + def test_api_offset_and_limit_with_limit + assert_equal [0, 30], @controller.api_offset_and_limit({:limit => 30}) + assert_equal [0, 100], @controller.api_offset_and_limit({:limit => 120}) + assert_equal [0, 25], @controller.api_offset_and_limit({:limit => -10}) + end - should "not exceed 100" do - assert_equal [0, 100], @controller.api_offset_and_limit({:limit => 120}) - end + def test_api_offset_and_limit_with_offset + assert_equal [10, 25], @controller.api_offset_and_limit({:offset => 10}) + assert_equal [0, 25], @controller.api_offset_and_limit({:offset => -10}) + end - should "not be negative" do - assert_equal [0, 25], @controller.api_offset_and_limit({:limit => -10}) - end - end + def test_api_offset_and_limit_with_offset_and_limit + assert_equal [10, 50], @controller.api_offset_and_limit({:offset => 10, :limit => 50}) + end - context "with offset" do - should "return offset, 25" do - assert_equal [10, 25], @controller.api_offset_and_limit({:offset => 10}) - end + def test_api_offset_and_limit_with_page + assert_equal [0, 25], @controller.api_offset_and_limit({:page => 1}) + assert_equal [50, 25], @controller.api_offset_and_limit({:page => 3}) + assert_equal [0, 25], @controller.api_offset_and_limit({:page => 0}) + assert_equal [0, 25], @controller.api_offset_and_limit({:page => -2}) + end - should "not be negative" do - assert_equal [0, 25], @controller.api_offset_and_limit({:offset => -10}) - end + def test_api_offset_and_limit_with_page_and_limit + assert_equal [0, 100], @controller.api_offset_and_limit({:page => 1, :limit => 100}) + assert_equal [200, 100], @controller.api_offset_and_limit({:page => 3, :limit => 100}) + end - context "and limit" do - should "return offset, limit" do - assert_equal [10, 50], @controller.api_offset_and_limit({:offset => 10, :limit => 50}) - end - end - end + def test_unhautorized_exception_with_anonymous_should_redirect_to_login + WelcomeController.any_instance.stubs(:index).raises(::Unauthorized) - context "with page" do - should "return offset, 25" do - assert_equal [0, 25], @controller.api_offset_and_limit({:page => 1}) - assert_equal [50, 25], @controller.api_offset_and_limit({:page => 3}) - end + get :index + assert_response 302 + assert_redirected_to('/login?back_url='+CGI.escape('http://test.host/')) + end - should "not be negative" do - assert_equal [0, 25], @controller.api_offset_and_limit({:page => 0}) - assert_equal [0, 25], @controller.api_offset_and_limit({:page => -2}) - end + def test_unhautorized_exception_with_anonymous_and_xmlhttprequest_should_respond_with_401_to_anonymous + WelcomeController.any_instance.stubs(:index).raises(::Unauthorized) - context "and limit" do - should "return offset, limit" do - assert_equal [0, 100], @controller.api_offset_and_limit({:page => 1, :limit => 100}) - assert_equal [200, 100], @controller.api_offset_and_limit({:page => 3, :limit => 100}) - end - end - end + @request.env["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" + get :index + assert_response 401 end end