您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

settings_controller_test.rb 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 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. :bcc_recipients => '0',
  67. :notified_events => %w(issue_added issue_updated news_added),
  68. :emails_footer => 'Test footer'
  69. }
  70. }
  71. assert_redirected_to '/settings'
  72. assert_equal 'functional@test.foo', Setting.mail_from
  73. assert !Setting.bcc_recipients?
  74. assert_equal %w(issue_added issue_updated news_added), Setting.notified_events
  75. assert_equal 'Test footer', Setting.emails_footer
  76. end
  77. def test_edit_commit_update_keywords
  78. with_settings :commit_update_keywords => [
  79. {"keywords" => "fixes, resolves", "status_id" => "3"},
  80. {"keywords" => "closes", "status_id" => "5", "done_ratio" => "100", "if_tracker_id" => "2"}
  81. ] do
  82. get :edit
  83. end
  84. assert_response :success
  85. assert_select 'tr.commit-keywords', 2
  86. assert_select 'tr.commit-keywords:nth-child(1)' do
  87. assert_select 'input[name=?][value=?]', 'settings[commit_update_keywords][keywords][]', 'fixes, resolves'
  88. assert_select 'select[name=?]', 'settings[commit_update_keywords][status_id][]' do
  89. assert_select 'option[value="3"][selected=selected]'
  90. end
  91. end
  92. assert_select 'tr.commit-keywords:nth-child(2)' do
  93. assert_select 'input[name=?][value=?]', 'settings[commit_update_keywords][keywords][]', 'closes'
  94. assert_select 'select[name=?]', 'settings[commit_update_keywords][status_id][]' do
  95. assert_select 'option[value="5"][selected=selected]', :text => 'Closed'
  96. end
  97. assert_select 'select[name=?]', 'settings[commit_update_keywords][done_ratio][]' do
  98. assert_select 'option[value="100"][selected=selected]', :text => '100 %'
  99. end
  100. assert_select 'select[name=?]', 'settings[commit_update_keywords][if_tracker_id][]' do
  101. assert_select 'option[value="2"][selected=selected]', :text => 'Feature request'
  102. end
  103. end
  104. end
  105. def test_edit_without_commit_update_keywords_should_show_blank_line
  106. with_settings :commit_update_keywords => [] do
  107. get :edit
  108. end
  109. assert_response :success
  110. assert_select 'tr.commit-keywords', 1 do
  111. assert_select 'input[name=?]:not([value])', 'settings[commit_update_keywords][keywords][]'
  112. end
  113. end
  114. def test_post_edit_commit_update_keywords
  115. post :edit, :params => {
  116. :settings => {
  117. :commit_update_keywords => {
  118. :keywords => ["resolves", "closes"],
  119. :status_id => ["3", "5"],
  120. :done_ratio => ["", "100"],
  121. :if_tracker_id => ["", "2"]
  122. }
  123. }
  124. }
  125. assert_redirected_to '/settings'
  126. assert_equal([
  127. {"keywords" => "resolves", "status_id" => "3"},
  128. {"keywords" => "closes", "status_id" => "5", "done_ratio" => "100", "if_tracker_id" => "2"}
  129. ], Setting.commit_update_keywords)
  130. end
  131. def test_post_edit_with_invalid_setting_should_not_error
  132. post :edit, :params => {
  133. :settings => {
  134. :invalid_setting => '1'
  135. }
  136. }
  137. assert_redirected_to '/settings'
  138. end
  139. def test_post_edit_should_send_security_notification_for_notified_settings
  140. ActionMailer::Base.deliveries.clear
  141. post :edit, :params => {
  142. :settings => {
  143. :login_required => 1
  144. }
  145. }
  146. assert_not_nil (mail = ActionMailer::Base.deliveries.last)
  147. assert_mail_body_match '0.0.0.0', mail
  148. assert_mail_body_match I18n.t(:setting_login_required), mail
  149. assert_select_email do
  150. assert_select 'a[href^=?]', 'http://localhost:3000/settings'
  151. end
  152. # All admins should receive this
  153. recipients = [mail.bcc, mail.cc].flatten
  154. User.active.where(admin: true).each do |admin|
  155. assert_include admin.mail, recipients
  156. end
  157. end
  158. def test_post_edit_should_not_send_security_notification_for_non_notified_settings
  159. ActionMailer::Base.deliveries.clear
  160. post :edit, :params => {
  161. :settings => {
  162. :app_title => 'MineRed'
  163. }
  164. }
  165. assert_nil (mail = ActionMailer::Base.deliveries.last)
  166. end
  167. def test_post_edit_should_not_send_security_notification_for_unchanged_settings
  168. ActionMailer::Base.deliveries.clear
  169. post :edit, :params => {
  170. :settings => {
  171. :login_required => 0
  172. }
  173. }
  174. assert_nil (mail = ActionMailer::Base.deliveries.last)
  175. end
  176. def test_get_plugin_settings
  177. ActionController::Base.append_view_path(File.join(Rails.root, "test/fixtures/plugins"))
  178. Redmine::Plugin.register :foo do
  179. settings :partial => "foo_plugin/foo_plugin_settings"
  180. directory 'test/fixtures/plugins/foo_plugin'
  181. end
  182. Setting.plugin_foo = {'sample_setting' => 'Plugin setting value'}
  183. get :plugin, :params => {:id => 'foo'}
  184. assert_response :success
  185. assert_select 'form[action="/settings/plugin/foo"]' do
  186. assert_select 'input[name=?][value=?]', 'settings[sample_setting]', 'Plugin setting value'
  187. end
  188. ensure
  189. Redmine::Plugin.unregister(:foo)
  190. end
  191. def test_get_invalid_plugin_settings
  192. get :plugin, :params => {:id => 'none'}
  193. assert_response 404
  194. end
  195. def test_get_non_configurable_plugin_settings
  196. Redmine::Plugin.register(:foo) do
  197. directory 'test/fixtures/plugins/foo_plugin'
  198. end
  199. get :plugin, :params => {:id => 'foo'}
  200. assert_response 404
  201. ensure
  202. Redmine::Plugin.unregister(:foo)
  203. end
  204. def test_post_plugin_settings
  205. Redmine::Plugin.register(:foo) do
  206. settings :partial => 'not blank', # so that configurable? is true
  207. :default => {'sample_setting' => 'Plugin setting value'}
  208. directory 'test/fixtures/plugins/foo_plugin'
  209. end
  210. post :plugin, :params => {
  211. :id => 'foo',
  212. :settings => {'sample_setting' => 'Value'}
  213. }
  214. assert_redirected_to '/settings/plugin/foo'
  215. assert_equal({'sample_setting' => 'Value'}, Setting.plugin_foo)
  216. end
  217. def test_post_empty_plugin_settings
  218. Redmine::Plugin.register(:foo) do
  219. settings :partial => 'not blank', # so that configurable? is true
  220. :default => {'sample_setting' => 'Plugin setting value'}
  221. directory 'test/fixtures/plugins/foo_plugin'
  222. end
  223. post :plugin, :params => {
  224. :id => 'foo'
  225. }
  226. assert_redirected_to '/settings/plugin/foo'
  227. assert_equal({}, Setting.plugin_foo)
  228. end
  229. def test_post_non_configurable_plugin_settings
  230. Redmine::Plugin.register(:foo) do
  231. directory 'test/fixtures/plugins/foo_plugin'
  232. end
  233. post :plugin, :params => {
  234. :id => 'foo',
  235. :settings => {'sample_setting' => 'Value'}
  236. }
  237. assert_response 404
  238. ensure
  239. Redmine::Plugin.unregister(:foo)
  240. end
  241. def test_post_mail_handler_delimiters_should_not_save_invalid_regex_delimiters
  242. post :edit, :params => {
  243. :settings => {
  244. :mail_handler_enable_regex_delimiters => '1',
  245. :mail_handler_body_delimiters => 'Abc[',
  246. }
  247. }
  248. assert_response :success
  249. assert_equal '0', Setting.mail_handler_enable_regex_delimiters
  250. assert_equal '', Setting.mail_handler_body_delimiters
  251. assert_select_error /is not a valid regular expression/
  252. assert_select 'textarea[name=?]', 'settings[mail_handler_body_delimiters]', :text => 'Abc['
  253. end
  254. def test_post_mail_handler_delimiters_should_save_valid_regex_delimiters
  255. post :edit, :params => {
  256. :settings => {
  257. :mail_handler_enable_regex_delimiters => '1',
  258. :mail_handler_body_delimiters => 'On .*, .* at .*, .* <.*<mailto:.*>> wrote:',
  259. }
  260. }
  261. assert_redirected_to '/settings'
  262. assert_equal '1', Setting.mail_handler_enable_regex_delimiters
  263. assert_equal 'On .*, .* at .*, .* <.*<mailto:.*>> wrote:', Setting.mail_handler_body_delimiters
  264. end
  265. end