You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

welcome_controller_test.rb 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class WelcomeControllerTest < Redmine::ControllerTest
  19. fixtures :projects, :news, :users, :members
  20. def setup
  21. Setting.default_language = 'en'
  22. User.current = nil
  23. end
  24. def test_index
  25. get :index
  26. assert_response :success
  27. assert_select 'h3', :text => 'Latest news'
  28. end
  29. def test_browser_language
  30. @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'
  31. get :index
  32. assert_select 'html[lang=fr]'
  33. end
  34. def test_browser_language_alternate
  35. @request.env['HTTP_ACCEPT_LANGUAGE'] = 'zh-TW'
  36. get :index
  37. assert_select 'html[lang=zh-TW]'
  38. end
  39. def test_browser_language_alternate_not_valid
  40. @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr-CA'
  41. get :index
  42. assert_select 'html[lang=fr]'
  43. end
  44. def test_browser_language_should_be_ignored_with_force_default_language_for_anonymous
  45. @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'
  46. with_settings :force_default_language_for_anonymous => '1' do
  47. get :index
  48. assert_select 'html[lang=en]'
  49. end
  50. end
  51. def test_user_language_should_be_used
  52. user = User.find(2).update_attribute :language, 'it'
  53. @request.session[:user_id] = 2
  54. @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'
  55. with_settings :default_language => 'fi' do
  56. get :index
  57. assert_select 'html[lang=it]'
  58. end
  59. end
  60. def test_user_language_should_be_ignored_if_force_default_language_for_loggedin
  61. user = User.find(2).update_attribute :language, 'it'
  62. @request.session[:user_id] = 2
  63. @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'
  64. with_settings :force_default_language_for_loggedin => '1', :default_language => 'fi' do
  65. get :index
  66. assert_select 'html[lang=fi]'
  67. end
  68. end
  69. def test_warn_on_leaving_unsaved_turn_on
  70. user = User.find(2)
  71. user.pref.warn_on_leaving_unsaved = '1'
  72. user.pref.save!
  73. @request.session[:user_id] = 2
  74. get :index
  75. assert_select 'script', :text => %r{warnLeavingUnsaved}
  76. end
  77. def test_warn_on_leaving_unsaved_turn_off
  78. user = User.find(2)
  79. user.pref.warn_on_leaving_unsaved = '0'
  80. user.pref.save!
  81. @request.session[:user_id] = 2
  82. get :index
  83. assert_select 'script', :text => %r{warnLeavingUnsaved}, :count => 0
  84. end
  85. def test_textarea_font_set_to_monospace
  86. user = User.find(1)
  87. user.pref.textarea_font = 'monospace'
  88. user.pref.save!
  89. @request.session[:user_id] = 1
  90. get :index
  91. assert_select 'body.textarea-monospace'
  92. end
  93. def test_textarea_font_set_to_proportional
  94. user = User.find(1)
  95. user.pref.textarea_font = 'proportional'
  96. user.pref.save!
  97. @request.session[:user_id] = 1
  98. get :index
  99. assert_select 'body.textarea-proportional'
  100. end
  101. def test_logout_link_should_post
  102. @request.session[:user_id] = 2
  103. get :index
  104. assert_select 'a[href="/logout"][data-method=post]', :text => 'Sign out'
  105. end
  106. def test_call_hook_mixed_in
  107. assert @controller.respond_to?(:call_hook)
  108. end
  109. def test_project_jump_box_should_escape_names_once
  110. Project.find(1).update_attribute :name, 'Foo & Bar'
  111. @request.session[:user_id] = 2
  112. get :index
  113. assert_select "#header #project-jump" do
  114. assert_select "a", :text => 'Foo & Bar'
  115. end
  116. end
  117. def test_api_offset_and_limit_without_params
  118. assert_equal [0, 25], @controller.api_offset_and_limit({})
  119. end
  120. def test_api_offset_and_limit_with_limit
  121. assert_equal [0, 30], @controller.api_offset_and_limit({:limit => 30})
  122. assert_equal [0, 100], @controller.api_offset_and_limit({:limit => 120})
  123. assert_equal [0, 25], @controller.api_offset_and_limit({:limit => -10})
  124. end
  125. def test_api_offset_and_limit_with_offset
  126. assert_equal [10, 25], @controller.api_offset_and_limit({:offset => 10})
  127. assert_equal [0, 25], @controller.api_offset_and_limit({:offset => -10})
  128. end
  129. def test_api_offset_and_limit_with_offset_and_limit
  130. assert_equal [10, 50], @controller.api_offset_and_limit({:offset => 10, :limit => 50})
  131. end
  132. def test_api_offset_and_limit_with_page
  133. assert_equal [0, 25], @controller.api_offset_and_limit({:page => 1})
  134. assert_equal [50, 25], @controller.api_offset_and_limit({:page => 3})
  135. assert_equal [0, 25], @controller.api_offset_and_limit({:page => 0})
  136. assert_equal [0, 25], @controller.api_offset_and_limit({:page => -2})
  137. end
  138. def test_api_offset_and_limit_with_page_and_limit
  139. assert_equal [0, 100], @controller.api_offset_and_limit({:page => 1, :limit => 100})
  140. assert_equal [200, 100], @controller.api_offset_and_limit({:page => 3, :limit => 100})
  141. end
  142. def test_unhautorized_exception_with_anonymous_should_redirect_to_login
  143. WelcomeController.any_instance.stubs(:index).raises(::Unauthorized)
  144. get :index
  145. assert_response 302
  146. assert_redirected_to('/login?back_url='+CGI.escape('http://test.host/'))
  147. end
  148. def test_unhautorized_exception_with_anonymous_and_xmlhttprequest_should_respond_with_401_to_anonymous
  149. WelcomeController.any_instance.stubs(:index).raises(::Unauthorized)
  150. @request.env["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"
  151. get :index
  152. assert_response 401
  153. end
  154. end