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

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