ticket_article_communicate_test.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class TicketArticleCommunicateTest < ActiveSupport::TestCase
  4. test 'via config' do
  5. # via application server
  6. ApplicationHandleInfo.current = 'application_server'
  7. ticket1 = Ticket.create(
  8. title: 'com test 1',
  9. group: Group.lookup(name: 'Users'),
  10. customer_id: 2,
  11. state: Ticket::State.lookup(name: 'new'),
  12. priority: Ticket::Priority.lookup(name: '2 normal'),
  13. updated_by_id: 1,
  14. created_by_id: 1,
  15. )
  16. assert(ticket1, 'ticket created')
  17. article_email1_1 = Ticket::Article.create(
  18. ticket_id: ticket1.id,
  19. from: 'some_customer_com-1@example.com',
  20. to: 'some_zammad_com-1@example.com',
  21. subject: 'com test 1',
  22. message_id: 'some@id_com_1',
  23. body: 'some message 123',
  24. internal: false,
  25. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  26. type: Ticket::Article::Type.find_by(name: 'email'),
  27. updated_by_id: 1,
  28. created_by_id: 1,
  29. )
  30. assert_equal('some_customer_com-1@example.com', article_email1_1.from)
  31. assert_equal('some_zammad_com-1@example.com', article_email1_1.to)
  32. assert_equal(0, email_count('some_customer_com-1@example.com'))
  33. assert_equal(0, email_count('some_zammad_com-1@example.com'))
  34. Scheduler.worker(true)
  35. assert_equal('some_customer_com-1@example.com', article_email1_1.from)
  36. assert_equal('some_zammad_com-1@example.com', article_email1_1.to)
  37. assert_equal(0, email_count('some_customer_com-1@example.com'))
  38. assert_equal(0, email_count('some_zammad_com-1@example.com'))
  39. article_email1_2 = Ticket::Article.create(
  40. ticket_id: ticket1.id,
  41. from: 'some_zammad_com-1@example.com',
  42. to: 'some_customer_com-1@example.com',
  43. subject: 'com test 1',
  44. message_id: 'some@id_com_2',
  45. body: 'some message 123',
  46. internal: false,
  47. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  48. type: Ticket::Article::Type.find_by(name: 'email'),
  49. updated_by_id: 1,
  50. created_by_id: 1,
  51. )
  52. assert_equal('Zammad <zammad@localhost>', article_email1_2.from)
  53. assert_equal('some_customer_com-1@example.com', article_email1_2.to)
  54. assert_equal(0, email_count('some_customer_com-1@example.com'))
  55. assert_equal(0, email_count('some_zammad_com-1@example.com'))
  56. Scheduler.worker(true)
  57. assert_equal('Zammad <zammad@localhost>', article_email1_2.from)
  58. assert_equal('some_customer_com-1@example.com', article_email1_2.to)
  59. assert_equal(1, email_count('some_customer_com-1@example.com'))
  60. assert_equal(0, email_count('some_zammad_com-1@example.com'))
  61. # via scheduler (e. g. postmaster)
  62. ApplicationHandleInfo.current = 'scheduler.postmaster'
  63. ticket2 = Ticket.create(
  64. title: 'com test 2',
  65. group: Group.lookup(name: 'Users'),
  66. customer_id: 2,
  67. state: Ticket::State.lookup(name: 'new'),
  68. priority: Ticket::Priority.lookup(name: '2 normal'),
  69. updated_by_id: 1,
  70. created_by_id: 1,
  71. )
  72. assert(ticket2, 'ticket created')
  73. article_email2_1 = Ticket::Article.create(
  74. ticket_id: ticket2.id,
  75. from: 'some_customer_com-2@example.com',
  76. to: 'some_zammad_com-2@example.com',
  77. subject: 'com test 2',
  78. message_id: 'some@id_com_1',
  79. body: 'some message 123',
  80. internal: false,
  81. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  82. type: Ticket::Article::Type.find_by(name: 'email'),
  83. updated_by_id: 1,
  84. created_by_id: 1,
  85. )
  86. assert_equal('some_customer_com-2@example.com', article_email2_1.from)
  87. assert_equal('some_zammad_com-2@example.com', article_email2_1.to)
  88. assert_equal(0, email_count('some_customer_com-2@example.com'))
  89. assert_equal(0, email_count('some_zammad_com-2@example.com'))
  90. Scheduler.worker(true)
  91. assert_equal('some_customer_com-2@example.com', article_email2_1.from)
  92. assert_equal('some_zammad_com-2@example.com', article_email2_1.to)
  93. assert_equal(0, email_count('some_customer_com-2@example.com'))
  94. assert_equal(0, email_count('some_zammad_com-2@example.com'))
  95. ApplicationHandleInfo.current = 'scheduler.postmaster'
  96. article_email2_2 = Ticket::Article.create(
  97. ticket_id: ticket2.id,
  98. from: 'some_zammad_com-2@example.com',
  99. to: 'some_customer_com-2@example.com',
  100. subject: 'com test 2',
  101. message_id: 'some@id_com_2',
  102. body: 'some message 123',
  103. internal: false,
  104. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  105. type: Ticket::Article::Type.find_by(name: 'email'),
  106. updated_by_id: 1,
  107. created_by_id: 1,
  108. )
  109. assert_equal(0, email_count('some_customer_com-2@example.com'))
  110. assert_equal(0, email_count('some_zammad_com-2@example.com'))
  111. assert_equal('some_zammad_com-2@example.com', article_email2_2.from)
  112. assert_equal('some_customer_com-2@example.com', article_email2_2.to)
  113. Scheduler.worker(true)
  114. assert_equal(0, email_count('some_customer_com-2@example.com'))
  115. assert_equal(0, email_count('some_zammad_com-2@example.com'))
  116. assert_equal('some_zammad_com-2@example.com', article_email2_2.from)
  117. assert_equal('some_customer_com-2@example.com', article_email2_2.to)
  118. end
  119. test 'postmaster process - do not change from - verify article sender' do
  120. email_raw_string = "From: my_zammad@example.com
  121. To: customer@example.com
  122. Subject: some subject
  123. X-Zammad-Article-Sender: Agent
  124. X-Loop: yes
  125. Some Text"
  126. ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({ trusted: true }, email_raw_string)
  127. ticket = Ticket.find(ticket_p.id)
  128. article_email = Ticket::Article.find(article_p.id)
  129. assert_equal(0, email_count('my_zammad@example.com'))
  130. assert_equal(0, email_count('customer@example.com'))
  131. assert_equal('my_zammad@example.com', article_email.from)
  132. assert_equal('customer@example.com', article_email.to)
  133. assert_equal('Agent', article_email.sender.name)
  134. assert_equal('email', article_email.type.name)
  135. assert_equal(1, article_email.ticket.articles.count)
  136. Scheduler.worker(true)
  137. article_email = Ticket::Article.find(article_p.id)
  138. assert_equal(0, email_count('my_zammad@example.com'))
  139. assert_equal(0, email_count('customer@example.com'))
  140. assert_equal('my_zammad@example.com', article_email.from)
  141. assert_equal('customer@example.com', article_email.to)
  142. assert_equal('Agent', article_email.sender.name)
  143. assert_equal('email', article_email.type.name)
  144. assert_equal(1, article_email.ticket.articles.count)
  145. end
  146. end