notification_factory_mailer_test.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class NotificationFactoryMailerTest < ActiveSupport::TestCase
  4. test 'notifications send' do
  5. result = NotificationFactory::Mailer.deliver(
  6. recipient: User.find(2),
  7. subject: 'some subject',
  8. body: 'some body',
  9. content_type: '',
  10. )
  11. assert_match('some body', result.to_s)
  12. assert_match('text/plain', result.to_s)
  13. assert_no_match('text/html', result.to_s)
  14. result = NotificationFactory::Mailer.deliver(
  15. recipient: User.find(2),
  16. subject: 'some subject',
  17. body: 'some body',
  18. content_type: 'text/plain',
  19. )
  20. assert_match('some body', result.to_s)
  21. assert_match('text/plain', result.to_s)
  22. assert_no_match('text/html', result.to_s)
  23. result = NotificationFactory::Mailer.deliver(
  24. recipient: User.find(2),
  25. subject: 'some subject',
  26. body: 'some <span>body</span>',
  27. content_type: 'text/html',
  28. )
  29. assert_match('some body', result.to_s)
  30. assert_match('text/plain', result.to_s)
  31. assert_match('<span>body</span>', result.to_s)
  32. assert_match('text/html', result.to_s)
  33. attachments = []
  34. attachments.push Store.create!(
  35. object: 'TestMailer',
  36. o_id: 1,
  37. data: 'content_file1_normally_should_be_an_image',
  38. filename: 'some_file1.jpg',
  39. preferences: {
  40. 'Content-Type' => 'image/jpeg',
  41. 'Mime-Type' => 'image/jpeg',
  42. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  43. 'Content-Disposition' => 'inline'
  44. },
  45. created_by_id: 1,
  46. )
  47. attachments.push Store.create!(
  48. object: 'TestMailer',
  49. o_id: 1,
  50. data: 'content_file2',
  51. filename: 'some_file2.txt',
  52. preferences: {
  53. 'Content-Type' => 'text/stream',
  54. 'Mime-Type' => 'text/stream',
  55. },
  56. created_by_id: 1,
  57. )
  58. result = NotificationFactory::Mailer.deliver(
  59. recipient: User.find(2),
  60. subject: 'some subject',
  61. body: 'some <span>body</span><img style="width: 85.5px; height: 49.5px" src="cid:15.274327094.140938@zammad.example.com">asdasd<br>',
  62. content_type: 'text/html',
  63. attachments: attachments,
  64. )
  65. assert_match('some body', result.to_s)
  66. assert_match('text/plain', result.to_s)
  67. assert_match('<span>body</span>', result.to_s)
  68. assert_match('text/html', result.to_s)
  69. assert_match('Content-Type: image/jpeg', result.to_s)
  70. assert_match('Content-Disposition: inline', result.to_s)
  71. assert_match('Content-ID: <15.274327094.140938@zammad.example.com>', result.to_s)
  72. assert_match('text/stream', result.to_s)
  73. assert_match('some_file2.txt', result.to_s)
  74. end
  75. test 'notifications settings' do
  76. groups = Group.all
  77. roles = Role.where(name: 'Agent')
  78. agent1 = User.create!(
  79. login: 'notification-settings-agent1@example.com',
  80. firstname: 'Notification<b>xxx</b>',
  81. lastname: 'Agent1',
  82. email: 'notification-settings-agent1@example.com',
  83. password: 'agentpw',
  84. active: true,
  85. roles: roles,
  86. groups: groups,
  87. updated_by_id: 1,
  88. created_by_id: 1,
  89. )
  90. agent2 = User.create!(
  91. login: 'notification-settings-agent2@example.com',
  92. firstname: 'Notification<b>xxx</b>',
  93. lastname: 'Agent2',
  94. email: 'notification-settings-agent2@example.com',
  95. password: 'agentpw',
  96. active: true,
  97. roles: roles,
  98. groups: groups,
  99. updated_by_id: 1,
  100. created_by_id: 1,
  101. )
  102. group_notification_setting = Group.create!(
  103. name: 'NotificationSetting',
  104. updated_by_id: 1,
  105. created_by_id: 1,
  106. )
  107. ticket1 = Ticket.create(
  108. group_id: Group.lookup(name: 'Users').id,
  109. customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
  110. owner_id: User.lookup(login: '-').id,
  111. title: 'Notification Settings Test 1!',
  112. state_id: Ticket::State.lookup(name: 'new').id,
  113. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  114. updated_by_id: 1,
  115. created_by_id: 1,
  116. )
  117. ticket2 = Ticket.create(
  118. group_id: Group.lookup(name: 'Users').id,
  119. customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
  120. owner_id: agent1.id,
  121. title: 'Notification Settings Test 2!',
  122. state_id: Ticket::State.lookup(name: 'new').id,
  123. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  124. updated_by_id: 1,
  125. created_by_id: 1,
  126. )
  127. ticket3 = Ticket.create(
  128. group_id: group_notification_setting.id,
  129. customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
  130. owner_id: User.lookup(login: '-').id,
  131. title: 'Notification Settings Test 1!',
  132. state_id: Ticket::State.lookup(name: 'new').id,
  133. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  134. updated_by_id: 1,
  135. created_by_id: 1,
  136. )
  137. ticket4 = Ticket.create(
  138. group_id: group_notification_setting.id,
  139. customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
  140. owner_id: agent1.id,
  141. title: 'Notification Settings Test 2!',
  142. state_id: Ticket::State.lookup(name: 'new').id,
  143. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  144. updated_by_id: 1,
  145. created_by_id: 1,
  146. )
  147. agent1.preferences[:notification_config][:group_ids] = nil
  148. agent1.save
  149. travel 30.seconds
  150. result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
  151. assert_equal(true, result[:channels][:online])
  152. assert_equal(true, result[:channels][:email])
  153. result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
  154. assert_equal(true, result[:channels][:online])
  155. assert_equal(true, result[:channels][:email])
  156. result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
  157. assert_equal(true, result[:channels][:online])
  158. assert_equal(true, result[:channels][:email])
  159. result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
  160. assert_equal(true, result[:channels][:online])
  161. assert_equal(true, result[:channels][:email])
  162. agent2.preferences[:notification_config][:group_ids] = nil
  163. agent2.save
  164. travel 30.seconds
  165. result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
  166. assert_equal(true, result[:channels][:online])
  167. assert_equal(true, result[:channels][:email])
  168. result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
  169. assert_nil(result)
  170. result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
  171. assert_equal(true, result[:channels][:online])
  172. assert_equal(true, result[:channels][:email])
  173. result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
  174. assert_nil(result)
  175. # no group selection
  176. agent1.preferences[:notification_config][:group_ids] = []
  177. agent1.save
  178. travel 30.seconds
  179. result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
  180. assert_equal(true, result[:channels][:online])
  181. assert_equal(true, result[:channels][:email])
  182. result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
  183. assert_equal(true, result[:channels][:online])
  184. assert_equal(true, result[:channels][:email])
  185. result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
  186. assert_equal(true, result[:channels][:online])
  187. assert_equal(true, result[:channels][:email])
  188. result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
  189. assert_equal(true, result[:channels][:online])
  190. assert_equal(true, result[:channels][:email])
  191. agent2.preferences[:notification_config][:group_ids] = []
  192. agent2.save
  193. travel 30.seconds
  194. result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
  195. assert_equal(true, result[:channels][:online])
  196. assert_equal(true, result[:channels][:email])
  197. result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
  198. assert_nil(result)
  199. result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
  200. assert_equal(true, result[:channels][:online])
  201. assert_equal(true, result[:channels][:email])
  202. result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
  203. assert_nil(result)
  204. agent1.preferences[:notification_config][:group_ids] = ['-']
  205. agent1.save
  206. travel 30.seconds
  207. result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
  208. assert_equal(true, result[:channels][:online])
  209. assert_equal(true, result[:channels][:email])
  210. result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
  211. assert_equal(true, result[:channels][:online])
  212. assert_equal(true, result[:channels][:email])
  213. result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
  214. assert_equal(true, result[:channels][:online])
  215. assert_equal(true, result[:channels][:email])
  216. result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
  217. assert_equal(true, result[:channels][:online])
  218. assert_equal(true, result[:channels][:email])
  219. agent2.preferences[:notification_config][:group_ids] = ['-']
  220. agent2.save
  221. travel 30.seconds
  222. result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
  223. assert_equal(true, result[:channels][:online])
  224. assert_equal(true, result[:channels][:email])
  225. result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
  226. assert_nil(result)
  227. result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
  228. assert_equal(true, result[:channels][:online])
  229. assert_equal(true, result[:channels][:email])
  230. result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
  231. assert_nil(result)
  232. # dedecated group selection
  233. agent1.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
  234. agent1.save
  235. travel 30.seconds
  236. if Rails.application.config.cache_store.first.eql? :mem_cache_store
  237. # External memcached does not support time travel, so clear the cache to avoid an outdated match.
  238. Rails.cache.clear
  239. end
  240. result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
  241. assert_equal(true, result[:channels][:online])
  242. assert_equal(true, result[:channels][:email])
  243. result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
  244. assert_equal(true, result[:channels][:online])
  245. assert_equal(true, result[:channels][:email])
  246. result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
  247. assert_nil(result)
  248. result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
  249. assert_equal(true, result[:channels][:online])
  250. assert_equal(true, result[:channels][:email])
  251. agent2.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
  252. agent2.save
  253. travel 30.seconds
  254. result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
  255. assert_equal(true, result[:channels][:online])
  256. assert_equal(true, result[:channels][:email])
  257. result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
  258. assert_nil(result)
  259. result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
  260. assert_nil(result)
  261. assert_nil(result)
  262. result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
  263. assert_nil(result)
  264. end
  265. end