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.

email_addresses_controller_test.rb 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 EmailAddressesControllerTest < Redmine::ControllerTest
  20. fixtures :users, :email_addresses
  21. def setup
  22. User.current = nil
  23. end
  24. def test_index_with_no_additional_emails
  25. @request.session[:user_id] = 2
  26. get(:index, :params => {:user_id => 2})
  27. assert_response :success
  28. end
  29. def test_index_with_additional_emails
  30. @request.session[:user_id] = 2
  31. EmailAddress.create!(:user_id => 2, :address => 'another@somenet.foo')
  32. get(:index, :params => {:user_id => 2})
  33. assert_response :success
  34. assert_select '.email', :text => 'another@somenet.foo'
  35. end
  36. def test_index_with_additional_emails_as_js
  37. @request.session[:user_id] = 2
  38. EmailAddress.create!(:user_id => 2, :address => 'another@somenet.foo')
  39. get(:index, :params => {:user_id => 2}, :xhr => true)
  40. assert_response :success
  41. assert_include 'another@somenet.foo', response.body
  42. end
  43. def test_index_by_admin_should_be_allowed
  44. @request.session[:user_id] = 1
  45. get(:index, :params => {:user_id => 2})
  46. assert_response :success
  47. end
  48. def test_index_by_another_user_should_be_denied
  49. @request.session[:user_id] = 3
  50. get(:index, :params => {:user_id => 2})
  51. assert_response 403
  52. end
  53. def test_create
  54. @request.session[:user_id] = 2
  55. assert_difference 'EmailAddress.count' do
  56. post(
  57. :create,
  58. :params => {
  59. :user_id => 2,
  60. :email_address => {
  61. :address => 'another@somenet.foo'
  62. }
  63. }
  64. )
  65. assert_response 302
  66. assert_redirected_to '/users/2/email_addresses'
  67. end
  68. email = EmailAddress.order('id DESC').first
  69. assert_equal 2, email.user_id
  70. assert_equal 'another@somenet.foo', email.address
  71. end
  72. def test_create_as_js
  73. @request.session[:user_id] = 2
  74. assert_difference 'EmailAddress.count' do
  75. post(
  76. :create,
  77. :params => {
  78. :user_id => 2,
  79. :email_address => {
  80. :address => 'another@somenet.foo'
  81. }
  82. },
  83. :xhr => true
  84. )
  85. assert_response 200
  86. end
  87. end
  88. def test_create_with_failure
  89. @request.session[:user_id] = 2
  90. assert_no_difference 'EmailAddress.count' do
  91. post(
  92. :create,
  93. :params => {
  94. :user_id => 2,
  95. :email_address => {
  96. :address => 'invalid'
  97. }
  98. }
  99. )
  100. assert_response :success
  101. assert_select_error /email is invalid/i
  102. end
  103. end
  104. def test_create_with_disallowed_domain_should_fail
  105. @request.session[:user_id] = 2
  106. with_settings :email_domains_denied => 'black.example' do
  107. assert_no_difference 'EmailAddress.count' do
  108. post(
  109. :create,
  110. :params => {
  111. :user_id => 2,
  112. :email_address => {
  113. :address => 'another@black.example'
  114. }
  115. }
  116. )
  117. assert_response :success
  118. assert_select_error 'Email is invalid'
  119. end
  120. end
  121. with_settings :email_domains_allowed => 'white.example' do
  122. assert_no_difference 'EmailAddress.count' do
  123. post(
  124. :create,
  125. :params => {
  126. :user_id => 2,
  127. :email_address => {
  128. :address => 'something@example.fr'
  129. }
  130. }
  131. )
  132. assert_response :success
  133. assert_select_error 'Email is invalid'
  134. end
  135. end
  136. end
  137. def test_create_should_send_security_notification
  138. @request.session[:user_id] = 2
  139. ActionMailer::Base.deliveries.clear
  140. post(
  141. :create,
  142. :params => {
  143. :user_id => 2,
  144. :email_address => {
  145. :address => 'something@example.fr'
  146. }
  147. }
  148. )
  149. mail = ActionMailer::Base.deliveries.last
  150. assert_not_nil mail
  151. assert_mail_body_match '0.0.0.0', mail
  152. assert_mail_body_match I18n.t(:mail_body_security_notification_add, field: I18n.t(:field_mail), value: 'something@example.fr'), mail
  153. assert_select_email do
  154. assert_select 'a[href^=?]', 'http://localhost:3000/my/account', :text => 'My account'
  155. end
  156. # The old email address should be notified about a new address for security purposes
  157. assert mail.to.include?(User.find(2).mail)
  158. assert mail.to.include?('something@example.fr')
  159. end
  160. def test_update
  161. @request.session[:user_id] = 2
  162. email = EmailAddress.create!(:user_id => 2, :address => 'another@somenet.foo')
  163. put(
  164. :update,
  165. :params => {
  166. :user_id => 2,
  167. :id => email.id,
  168. :notify => '0'
  169. }
  170. )
  171. assert_response 302
  172. assert_equal false, email.reload.notify
  173. end
  174. def test_update_as_js
  175. @request.session[:user_id] = 2
  176. email = EmailAddress.create!(:user_id => 2, :address => 'another@somenet.foo')
  177. put(
  178. :update,
  179. :params => {
  180. :user_id => 2,
  181. :id => email.id,
  182. :notify => '0'
  183. },
  184. :xhr => true
  185. )
  186. assert_response 200
  187. assert_equal false, email.reload.notify
  188. end
  189. def test_update_should_send_security_notification
  190. @request.session[:user_id] = 2
  191. email = EmailAddress.create!(:user_id => 2, :address => 'another@somenet.foo')
  192. ActionMailer::Base.deliveries.clear
  193. put(
  194. :update,
  195. :params => {
  196. :user_id => 2,
  197. :id => email.id,
  198. :notify => '0'
  199. },
  200. :xhr => true
  201. )
  202. mail = ActionMailer::Base.deliveries.last
  203. assert_not_nil mail
  204. assert_mail_body_match I18n.t(:mail_body_security_notification_notify_disabled, value: 'another@somenet.foo'), mail
  205. # The changed address should be notified for security purposes
  206. assert mail.to.include?('another@somenet.foo')
  207. end
  208. def test_destroy
  209. @request.session[:user_id] = 2
  210. email = EmailAddress.create!(:user_id => 2, :address => 'another@somenet.foo')
  211. assert_difference 'EmailAddress.count', -1 do
  212. delete(
  213. :destroy,
  214. :params => {
  215. :user_id => 2,
  216. :id => email.id
  217. }
  218. )
  219. assert_response 302
  220. assert_redirected_to '/users/2/email_addresses'
  221. end
  222. end
  223. def test_destroy_as_js
  224. @request.session[:user_id] = 2
  225. email = EmailAddress.create!(:user_id => 2, :address => 'another@somenet.foo')
  226. assert_difference 'EmailAddress.count', -1 do
  227. delete(
  228. :destroy,
  229. :params => {
  230. :user_id => 2,
  231. :id => email.id
  232. },
  233. :xhr => true
  234. )
  235. assert_response 200
  236. end
  237. end
  238. def test_should_not_destroy_default
  239. @request.session[:user_id] = 2
  240. assert_no_difference 'EmailAddress.count' do
  241. delete(
  242. :destroy,
  243. :params => {
  244. :user_id => 2,
  245. :id => User.find(2).email_address.id
  246. }
  247. )
  248. assert_response 404
  249. end
  250. end
  251. def test_destroy_should_send_security_notification
  252. @request.session[:user_id] = 2
  253. email = EmailAddress.create!(:user_id => 2, :address => 'another@somenet.foo')
  254. ActionMailer::Base.deliveries.clear
  255. delete(
  256. :destroy,
  257. :params => {
  258. :user_id => 2,
  259. :id => email.id
  260. },
  261. :xhr => true
  262. )
  263. mail = ActionMailer::Base.deliveries.last
  264. assert_not_nil mail
  265. assert_mail_body_match I18n.t(:mail_body_security_notification_remove, field: I18n.t(:field_mail), value: 'another@somenet.foo'), mail
  266. # The removed address should be notified for security purposes
  267. assert mail.to.include?('another@somenet.foo')
  268. end
  269. end