ticket_article_communicate_test.rb 6.4 KB

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