notification_factory_mailer_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class NotificationFactoryMailerTest < ActiveSupport::TestCase
  4. test 'notifications send' do
  5. result = NotificationFactory::Mailer.send(
  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.send(
  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.send(
  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.add(
  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.add(
  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.send(
  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_or_update(
  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_or_update(
  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_or_update(
  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. result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
  150. assert_equal(true, result[:channels][:online])
  151. assert_equal(true, result[:channels][:email])
  152. result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
  153. assert_equal(true, result[:channels][:online])
  154. assert_equal(true, result[:channels][:email])
  155. result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
  156. assert_equal(true, result[:channels][:online])
  157. assert_equal(true, result[:channels][:email])
  158. result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
  159. assert_equal(true, result[:channels][:online])
  160. assert_equal(true, result[:channels][:email])
  161. agent2.preferences[:notification_config][:group_ids] = nil
  162. agent2.save
  163. result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
  164. assert_equal(true, result[:channels][:online])
  165. assert_equal(true, result[:channels][:email])
  166. result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
  167. assert_equal(nil, result)
  168. result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
  169. assert_equal(true, result[:channels][:online])
  170. assert_equal(true, result[:channels][:email])
  171. result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
  172. assert_equal(nil, result)
  173. # no group selection
  174. agent1.preferences[:notification_config][:group_ids] = []
  175. agent1.save
  176. result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
  177. assert_equal(true, result[:channels][:online])
  178. assert_equal(true, result[:channels][:email])
  179. result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
  180. assert_equal(true, result[:channels][:online])
  181. assert_equal(true, result[:channels][:email])
  182. result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
  183. assert_equal(true, result[:channels][:online])
  184. assert_equal(true, result[:channels][:email])
  185. result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
  186. assert_equal(true, result[:channels][:online])
  187. assert_equal(true, result[:channels][:email])
  188. agent2.preferences[:notification_config][:group_ids] = []
  189. agent2.save
  190. result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
  191. assert_equal(true, result[:channels][:online])
  192. assert_equal(true, result[:channels][:email])
  193. result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
  194. assert_equal(nil, result)
  195. result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
  196. assert_equal(true, result[:channels][:online])
  197. assert_equal(true, result[:channels][:email])
  198. result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
  199. assert_equal(nil, result)
  200. agent1.preferences[:notification_config][:group_ids] = ['-']
  201. agent1.save
  202. result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
  203. assert_equal(true, result[:channels][:online])
  204. assert_equal(true, result[:channels][:email])
  205. result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
  206. assert_equal(true, result[:channels][:online])
  207. assert_equal(true, result[:channels][:email])
  208. result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
  209. assert_equal(true, result[:channels][:online])
  210. assert_equal(true, result[:channels][:email])
  211. result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
  212. assert_equal(true, result[:channels][:online])
  213. assert_equal(true, result[:channels][:email])
  214. agent2.preferences[:notification_config][:group_ids] = ['-']
  215. agent2.save
  216. result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
  217. assert_equal(true, result[:channels][:online])
  218. assert_equal(true, result[:channels][:email])
  219. result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
  220. assert_equal(nil, result)
  221. result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
  222. assert_equal(true, result[:channels][:online])
  223. assert_equal(true, result[:channels][:email])
  224. result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
  225. assert_equal(nil, result)
  226. # dedecated group selection
  227. agent1.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
  228. agent1.save
  229. result = NotificationFactory::Mailer.notification_settings(agent1, ticket1, 'create')
  230. assert_equal(true, result[:channels][:online])
  231. assert_equal(true, result[:channels][:email])
  232. result = NotificationFactory::Mailer.notification_settings(agent1, ticket2, 'create')
  233. assert_equal(true, result[:channels][:online])
  234. assert_equal(true, result[:channels][:email])
  235. result = NotificationFactory::Mailer.notification_settings(agent1, ticket3, 'create')
  236. assert_equal(nil, result)
  237. result = NotificationFactory::Mailer.notification_settings(agent1, ticket4, 'create')
  238. assert_equal(true, result[:channels][:online])
  239. assert_equal(true, result[:channels][:email])
  240. agent2.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
  241. agent2.save
  242. result = NotificationFactory::Mailer.notification_settings(agent2, ticket1, 'create')
  243. assert_equal(true, result[:channels][:online])
  244. assert_equal(true, result[:channels][:email])
  245. result = NotificationFactory::Mailer.notification_settings(agent2, ticket2, 'create')
  246. assert_equal(nil, result)
  247. result = NotificationFactory::Mailer.notification_settings(agent2, ticket3, 'create')
  248. assert_equal(nil, result)
  249. assert_equal(nil, result)
  250. result = NotificationFactory::Mailer.notification_settings(agent2, ticket4, 'create')
  251. assert_equal(nil, result)
  252. end
  253. end