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 6.0KB

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