Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

settings_controller_test.rb 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 File.expand_path('../../test_helper', __FILE__)
  19. class SettingsControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :trackers, :issue_statuses, :issues,
  21. :users, :email_addresses
  22. def setup
  23. User.current = nil
  24. @request.session[:user_id] = 1 # admin
  25. end
  26. def teardown
  27. Setting.delete_all
  28. Setting.clear_cache
  29. end
  30. def test_index
  31. get :index
  32. assert_response :success
  33. assert_select 'input[name=?][value=?]', 'settings[app_title]', Setting.app_title
  34. end
  35. def test_get_edit
  36. get :edit
  37. assert_response :success
  38. assert_select 'input[name=?][value=""]', 'settings[enabled_scm][]'
  39. end
  40. def test_get_edit_should_preselect_default_issue_list_columns
  41. with_settings :issue_list_default_columns => %w(tracker subject status updated_on) do
  42. get :edit
  43. assert_response :success
  44. end
  45. assert_select 'select[name=?]', 'settings[issue_list_default_columns][]' do
  46. assert_select 'option', 4
  47. assert_select 'option[value=tracker]', :text => 'Tracker'
  48. assert_select 'option[value=subject]', :text => 'Subject'
  49. assert_select 'option[value=status]', :text => 'Status'
  50. assert_select 'option[value=updated_on]', :text => 'Updated'
  51. end
  52. assert_select 'select[name=?]', 'available_columns[]' do
  53. assert_select 'option[value=tracker]', 0
  54. assert_select 'option[value=priority]', :text => 'Priority'
  55. end
  56. end
  57. def test_get_edit_without_trackers_should_succeed
  58. Tracker.delete_all
  59. get :edit
  60. assert_response :success
  61. end
  62. def test_post_edit_notifications
  63. post :edit, :params => {
  64. :settings => {
  65. :mail_from => 'functional@test.foo',
  66. :notified_events => %w(issue_added issue_updated news_added),
  67. :emails_footer => 'Test footer'
  68. }
  69. }
  70. assert_redirected_to '/settings'
  71. assert_equal 'functional@test.foo', Setting.mail_from
  72. assert_equal %w(issue_added issue_updated news_added), Setting.notified_events
  73. assert_equal 'Test footer', Setting.emails_footer
  74. end
  75. def test_edit_commit_update_keywords
  76. with_settings :commit_update_keywords => [
  77. {"keywords" => "fixes, resolves", "status_id" => "3"},
  78. {"keywords" => "closes", "status_id" => "5", "done_ratio" => "100", "if_tracker_id" => "2"}
  79. ] do
  80. get :edit
  81. end
  82. assert_response :success
  83. assert_select 'tr.commit-keywords', 2
  84. assert_select 'tr.commit-keywords:nth-child(1)' do
  85. assert_select 'input[name=?][value=?]', 'settings[commit_update_keywords][keywords][]', 'fixes, resolves'
  86. assert_select 'select[name=?]', 'settings[commit_update_keywords][status_id][]' do
  87. assert_select 'option[value="3"][selected=selected]'
  88. end
  89. end
  90. assert_select 'tr.commit-keywords:nth-child(2)' do
  91. assert_select 'input[name=?][value=?]', 'settings[commit_update_keywords][keywords][]', 'closes'
  92. assert_select 'select[name=?]', 'settings[commit_update_keywords][status_id][]' do
  93. assert_select 'option[value="5"][selected=selected]', :text => 'Closed'
  94. end
  95. assert_select 'select[name=?]', 'settings[commit_update_keywords][done_ratio][]' do
  96. assert_select 'option[value="100"][selected=selected]', :text => '100 %'
  97. end
  98. assert_select 'select[name=?]', 'settings[commit_update_keywords][if_tracker_id][]' do
  99. assert_select 'option[value="2"][selected=selected]', :text => 'Feature request'
  100. end
  101. end
  102. end
  103. def test_edit_without_commit_update_keywords_should_show_blank_line
  104. with_settings :commit_update_keywords => [] do
  105. get :edit
  106. end
  107. assert_response :success
  108. assert_select 'tr.commit-keywords', 1 do
  109. assert_select 'input[name=?]:not([value])', 'settings[commit_update_keywords][keywords][]'
  110. end
  111. end
  112. def test_post_edit_commit_update_keywords
  113. post :edit, :params => {
  114. :settings => {
  115. :commit_update_keywords => {
  116. :keywords => ["resolves", "closes"],
  117. :status_id => ["3", "5"],
  118. :done_ratio => ["", "100"],
  119. :if_tracker_id => ["", "2"]
  120. }
  121. }
  122. }
  123. assert_redirected_to '/settings'
  124. assert_equal(
  125. [
  126. {"keywords" => "resolves", "status_id" => "3"},
  127. {"keywords" => "closes", "status_id" => "5",
  128. "done_ratio" => "100", "if_tracker_id" => "2"}
  129. ],
  130. Setting.commit_update_keywords
  131. )
  132. end
  133. def test_post_edit_with_invalid_setting_should_not_error
  134. post :edit, :params => {
  135. :settings => {
  136. :invalid_setting => '1'
  137. }
  138. }
  139. assert_redirected_to '/settings'
  140. end
  141. def test_post_edit_should_send_security_notification_for_notified_settings
  142. ActionMailer::Base.deliveries.clear
  143. post :edit, :params => {
  144. :settings => {
  145. :login_required => 1
  146. }
  147. }
  148. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  149. assert_mail_body_match '0.0.0.0', mail
  150. assert_mail_body_match I18n.t(:setting_login_required), mail
  151. assert_select_email do
  152. assert_select 'a[href^=?]', 'http://localhost:3000/settings'
  153. end
  154. # All admins should receive this
  155. User.active.where(admin: true).each do |admin|
  156. assert_include admin.mail, mail.to
  157. end
  158. end
  159. def test_post_edit_should_not_send_security_notification_for_non_notified_settings
  160. ActionMailer::Base.deliveries.clear
  161. post :edit, :params => {
  162. :settings => {
  163. :app_title => 'MineRed'
  164. }
  165. }
  166. assert_nil (mail = ActionMailer::Base.deliveries.last)
  167. end
  168. def test_post_edit_should_not_send_security_notification_for_unchanged_settings
  169. ActionMailer::Base.deliveries.clear
  170. post :edit, :params => {
  171. :settings => {
  172. :login_required => 0
  173. }
  174. }
  175. assert_nil (mail = ActionMailer::Base.deliveries.last)
  176. end
  177. def test_get_plugin_settings
  178. ActionController::Base.append_view_path(File.join(Rails.root, "test/fixtures/plugins"))
  179. Redmine::Plugin.register :foo do
  180. settings :partial => "foo_plugin/foo_plugin_settings"
  181. directory 'test/fixtures/plugins/foo_plugin'
  182. end
  183. Setting.plugin_foo = {'sample_setting' => 'Plugin setting value'}
  184. get :plugin, :params => {:id => 'foo'}
  185. assert_response :success
  186. assert_select '#settings.plugin.plugin-foo' do
  187. assert_select 'form[action="/settings/plugin/foo"]' do
  188. assert_select 'input[name=?][value=?]', 'settings[sample_setting]', 'Plugin setting value'
  189. end
  190. end
  191. ensure
  192. Redmine::Plugin.unregister(:foo)
  193. end
  194. def test_get_invalid_plugin_settings
  195. get :plugin, :params => {:id => 'none'}
  196. assert_response 404
  197. end
  198. def test_get_non_configurable_plugin_settings
  199. Redmine::Plugin.register(:foo) do
  200. directory 'test/fixtures/plugins/foo_plugin'
  201. end
  202. get :plugin, :params => {:id => 'foo'}
  203. assert_response 404
  204. ensure
  205. Redmine::Plugin.unregister(:foo)
  206. end
  207. def test_post_plugin_settings
  208. Redmine::Plugin.register(:foo) do
  209. settings :partial => 'not blank', # so that configurable? is true
  210. :default => {'sample_setting' => 'Plugin setting value'}
  211. directory 'test/fixtures/plugins/foo_plugin'
  212. end
  213. post :plugin, :params => {
  214. :id => 'foo',
  215. :settings => {'sample_setting' => 'Value'}
  216. }
  217. assert_redirected_to '/settings/plugin/foo'
  218. assert_equal({'sample_setting' => 'Value'}, Setting.plugin_foo)
  219. end
  220. def test_post_empty_plugin_settings
  221. Redmine::Plugin.register(:foo) do
  222. settings :partial => 'not blank', # so that configurable? is true
  223. :default => {'sample_setting' => 'Plugin setting value'}
  224. directory 'test/fixtures/plugins/foo_plugin'
  225. end
  226. post :plugin, :params => {
  227. :id => 'foo'
  228. }
  229. assert_redirected_to '/settings/plugin/foo'
  230. assert_equal({}, Setting.plugin_foo)
  231. end
  232. def test_post_non_configurable_plugin_settings
  233. Redmine::Plugin.register(:foo) do
  234. directory 'test/fixtures/plugins/foo_plugin'
  235. end
  236. post :plugin, :params => {
  237. :id => 'foo',
  238. :settings => {'sample_setting' => 'Value'}
  239. }
  240. assert_response 404
  241. ensure
  242. Redmine::Plugin.unregister(:foo)
  243. end
  244. def test_post_mail_handler_delimiters_should_not_save_invalid_regex_delimiters
  245. post :edit, :params => {
  246. :settings => {
  247. :mail_handler_enable_regex_delimiters => '1',
  248. :mail_handler_body_delimiters => 'Abc[',
  249. }
  250. }
  251. assert_response :success
  252. assert_equal '0', Setting.mail_handler_enable_regex_delimiters
  253. assert_equal '', Setting.mail_handler_body_delimiters
  254. assert_select_error /is not a valid regular expression/
  255. assert_select 'textarea[name=?]', 'settings[mail_handler_body_delimiters]', :text => 'Abc['
  256. end
  257. def test_post_mail_handler_delimiters_should_save_valid_regex_delimiters
  258. post :edit, :params => {
  259. :settings => {
  260. :mail_handler_enable_regex_delimiters => '1',
  261. :mail_handler_body_delimiters => 'On .*, .* at .*, .* <.*<mailto:.*>> wrote:',
  262. }
  263. }
  264. assert_redirected_to '/settings'
  265. assert_equal '1', Setting.mail_handler_enable_regex_delimiters
  266. assert_equal 'On .*, .* at .*, .* <.*<mailto:.*>> wrote:', Setting.mail_handler_body_delimiters
  267. end
  268. end