notification_factory_mailer_test.rb 11 KB

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