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.

journal_observer_test.rb 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  19. class JournalObserverTest < ActiveSupport::TestCase
  20. fixtures :issues, :issue_statuses, :journals, :journal_details, :projects,
  21. :projects_trackers, :trackers, :enabled_modules, :enumerations,
  22. :users, :user_preferences, :email_addresses, :roles, :members, :member_roles,
  23. :versions, :issue_categories, :attachments
  24. def setup
  25. User.current = nil
  26. ActionMailer::Base.deliveries.clear
  27. end
  28. # context: issue_updated notified_events
  29. def test_create_should_send_email_notification_with_issue_updated
  30. issue = Issue.first
  31. user = User.first
  32. journal = issue.init_journal(user, "some notes")
  33. with_settings :notified_events => %w(issue_updated) do
  34. assert journal.save
  35. end
  36. assert_equal 2, ActionMailer::Base.deliveries.size
  37. end
  38. def test_create_should_not_send_email_notification_with_notify_set_to_false
  39. issue = Issue.first
  40. user = User.first
  41. journal = issue.init_journal(user, "some notes")
  42. journal.notify = false
  43. with_settings :notified_events => %w(issue_updated) do
  44. assert journal.save
  45. end
  46. assert_equal 0, ActionMailer::Base.deliveries.size
  47. end
  48. def test_create_should_not_send_email_notification_without_issue_updated
  49. issue = Issue.first
  50. user = User.first
  51. journal = issue.init_journal(user, "some notes")
  52. with_settings :notified_events => [] do
  53. assert journal.save
  54. end
  55. assert_equal 0, ActionMailer::Base.deliveries.size
  56. end
  57. def test_create_should_send_email_notification_with_issue_note_added
  58. issue = Issue.first
  59. user = User.first
  60. journal = issue.init_journal(user)
  61. journal.notes = 'This update has a note'
  62. with_settings :notified_events => %w(issue_note_added) do
  63. assert journal.save
  64. end
  65. assert_equal 2, ActionMailer::Base.deliveries.size
  66. end
  67. def test_create_should_not_send_email_notification_without_issue_note_added
  68. issue = Issue.first
  69. user = User.first
  70. journal = issue.init_journal(user)
  71. journal.notes = 'This update has a note'
  72. with_settings :notified_events => [] do
  73. assert journal.save
  74. end
  75. assert_equal 0, ActionMailer::Base.deliveries.size
  76. end
  77. def test_create_should_send_email_notification_with_issue_status_updated
  78. issue = Issue.first
  79. user = User.first
  80. issue.init_journal(user)
  81. issue.status = IssueStatus.last
  82. with_settings :notified_events => %w(issue_status_updated) do
  83. assert issue.save
  84. end
  85. assert_equal 2, ActionMailer::Base.deliveries.size
  86. end
  87. def test_create_should_not_send_email_notification_without_issue_status_updated
  88. issue = Issue.first
  89. user = User.first
  90. issue.init_journal(user)
  91. issue.status = IssueStatus.last
  92. with_settings :notified_events => [] do
  93. assert issue.save
  94. end
  95. assert_equal 0, ActionMailer::Base.deliveries.size
  96. end
  97. def test_create_without_status_update_should_not_send_email_notification_with_issue_status_updated
  98. issue = Issue.first
  99. user = User.first
  100. issue.init_journal(user)
  101. issue.subject = "No status update"
  102. with_settings :notified_events => %w(issue_status_updated) do
  103. assert issue.save
  104. end
  105. assert_equal 0, ActionMailer::Base.deliveries.size
  106. end
  107. def test_create_should_send_email_notification_with_issue_assignee_updated
  108. issue = Issue.generate!(:assigned_to_id => 2)
  109. ActionMailer::Base.deliveries.clear
  110. user = User.first
  111. issue.init_journal(user)
  112. issue.assigned_to = User.find(3)
  113. with_settings :notified_events => %w(issue_assigned_to_updated) do
  114. assert issue.save
  115. end
  116. assert_equal 2, ActionMailer::Base.deliveries.size
  117. end
  118. def test_create_should_not_send_email_notification_without_issue_assignee_updated
  119. issue = Issue.generate!(:assigned_to_id => 2)
  120. ActionMailer::Base.deliveries.clear
  121. user = User.first
  122. issue.init_journal(user)
  123. issue.assigned_to = User.find(3)
  124. with_settings :notified_events => [] do
  125. assert issue.save
  126. end
  127. assert_equal 0, ActionMailer::Base.deliveries.size
  128. end
  129. def test_create_should_send_email_notification_with_issue_priority_updated
  130. issue = Issue.first
  131. user = User.first
  132. issue.init_journal(user)
  133. issue.priority = IssuePriority.last
  134. with_settings :notified_events => %w(issue_priority_updated) do
  135. assert issue.save
  136. end
  137. assert_equal 2, ActionMailer::Base.deliveries.size
  138. end
  139. def test_create_should_not_send_email_notification_without_issue_priority_updated
  140. issue = Issue.first
  141. user = User.first
  142. issue.init_journal(user)
  143. issue.priority = IssuePriority.last
  144. with_settings :notified_events => [] do
  145. assert issue.save
  146. end
  147. assert_equal 0, ActionMailer::Base.deliveries.size
  148. end
  149. def test_create_should_send_email_notification_with_issue_fixed_version_updated
  150. with_settings :notified_events => %w(issue_fixed_version_updated) do
  151. user = User.find_by_login('jsmith')
  152. issue = issues(:issues_001)
  153. issue.init_journal(user)
  154. issue.fixed_version = versions(:versions_003)
  155. assert issue.save
  156. assert_equal 2, ActionMailer::Base.deliveries.size
  157. end
  158. end
  159. def test_create_should_not_send_email_notification_without_issue_fixed_version_updated
  160. with_settings :notified_events => [] do
  161. user = User.find_by_login('jsmith')
  162. issue = issues(:issues_001)
  163. issue.init_journal(user)
  164. issue.fixed_version = versions(:versions_003)
  165. assert issue.save
  166. assert_equal 0, ActionMailer::Base.deliveries.size
  167. end
  168. end
  169. def test_create_should_send_email_notification_with_issue_attachment_added
  170. set_tmp_attachments_directory
  171. with_settings :notified_events => %w(issue_attachment_added) do
  172. user = User.find_by_login('jsmith')
  173. issue = issues(:issues_001)
  174. issue.init_journal(user)
  175. issue.save_attachments(
  176. { 'p0' => {'file' => mock_file_with_options(:original_filename => 'upload')} }
  177. )
  178. assert issue.save
  179. assert_equal 2, ActionMailer::Base.deliveries.size
  180. end
  181. end
  182. def test_create_should_not_send_email_notification_without_issue_attachment_added
  183. set_tmp_attachments_directory
  184. with_settings :notified_events => [] do
  185. user = User.find_by_login('jsmith')
  186. issue = issues(:issues_001)
  187. issue.init_journal(user)
  188. issue.save_attachments(
  189. { 'p0' => {'file' => mock_file_with_options(:original_filename => 'upload')} }
  190. )
  191. assert issue.save
  192. assert_equal 0, ActionMailer::Base.deliveries.size
  193. end
  194. end
  195. end