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.

settings_controller_test.rb 9.5KB

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