trigger_spec.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. require 'rails_helper'
  2. require 'models/application_model_examples'
  3. RSpec.describe Trigger, type: :model do
  4. subject(:trigger) { create(:trigger, condition: condition, perform: perform) }
  5. it_behaves_like 'ApplicationModel', can_assets: { selectors: %i[condition perform] }
  6. describe 'Send-email triggers' do
  7. before do
  8. described_class.destroy_all # Default DB state includes three sample triggers
  9. trigger # create subject trigger
  10. end
  11. let(:perform) do
  12. {
  13. 'notification.email' => {
  14. 'recipient' => 'ticket_customer',
  15. 'subject' => 'foo',
  16. 'body' => 'some body with >snip<#{article.body_as_html}>/snip<', # rubocop:disable Lint/InterpolationCheck
  17. }
  18. }
  19. end
  20. context 'for condition "ticket created"' do
  21. let(:condition) do
  22. { 'ticket.action' => { 'operator' => 'is', 'value' => 'create' } }
  23. end
  24. context 'when ticket is created directly' do
  25. let!(:ticket) { create(:ticket) }
  26. it 'fires (without altering ticket state)' do
  27. expect { Observer::Transaction.commit }
  28. .to change(Ticket::Article, :count).by(1)
  29. .and not_change { ticket.reload.state.name }.from('new')
  30. end
  31. end
  32. context 'when ticket is created via Channel::EmailParser.process' do
  33. before { create(:email_address, groups: [Group.first]) }
  34. let(:raw_email) { File.read(Rails.root.join('test', 'data', 'mail', 'mail001.box')) }
  35. it 'fires (without altering ticket state)' do
  36. expect { Channel::EmailParser.new.process({}, raw_email) }
  37. .to change(Ticket, :count).by(1)
  38. .and change { Ticket::Article.count }.by(2)
  39. expect(Ticket.last.state.name).to eq('new')
  40. end
  41. end
  42. context 'when ticket is created via Channel::EmailParser.process with inline image' do
  43. before { create(:email_address, groups: [Group.first]) }
  44. let(:raw_email) { File.read(Rails.root.join('test', 'data', 'mail', 'mail010.box')) }
  45. it 'fires (without altering ticket state)' do
  46. expect { Channel::EmailParser.new.process({}, raw_email) }
  47. .to change(Ticket, :count).by(1)
  48. .and change { Ticket::Article.count }.by(2)
  49. expect(Ticket.last.state.name).to eq('new')
  50. article = Ticket::Article.last
  51. expect(article.type.name).to eq('email')
  52. expect(article.sender.name).to eq('System')
  53. expect(article.attachments.count).to eq(1)
  54. expect(article.attachments[0].filename).to eq('image001.jpg')
  55. expect(article.attachments[0].preferences['Content-ID']).to eq('image001.jpg@01CDB132.D8A510F0')
  56. expect(article.body).to eq(<<~RAW.chomp
  57. some body with &gt;snip&lt;<div>
  58. <p>Herzliche Grüße aus Oberalteich sendet Herrn Smith</p>
  59. <p> </p>
  60. <p>Sepp Smith - Dipl.Ing. agr. (FH)</p>
  61. <p>Geschäftsführer der example Straubing-Bogen</p>
  62. <p>Klosterhof 1 | 94327 Bogen-Oberalteich</p>
  63. <p>Tel: 09422-505601 | Fax: 09422-505620</p>
  64. <p>Internet: <a href="http://example-straubing-bogen.de/" rel="nofollow noreferrer noopener" target="_blank">http://example-straubing-bogen.de</a></p>
  65. <p>Facebook: <a href="http://facebook.de/examplesrbog" rel="nofollow noreferrer noopener" target="_blank">http://facebook.de/examplesrbog</a></p>
  66. <p><b><img border="0" src="cid:image001.jpg@01CDB132.D8A510F0" alt="Beschreibung: Beschreibung: efqmLogo" style="width:60px;height:19px;"></b><b> - European Foundation für Quality Management</b></p>
  67. <p> </p>
  68. </div>&gt;/snip&lt;
  69. RAW
  70. )
  71. end
  72. end
  73. end
  74. context 'for condition "ticket updated"' do
  75. let(:condition) do
  76. { 'ticket.action' => { 'operator' => 'is', 'value' => 'update' } }
  77. end
  78. let!(:ticket) { create(:ticket).tap { Observer::Transaction.commit } }
  79. context 'when new article is created directly' do
  80. context 'with empty #preferences hash' do
  81. let!(:article) { create(:ticket_article, ticket: ticket) }
  82. it 'fires (without altering ticket state)' do
  83. expect { Observer::Transaction.commit }
  84. .to change { ticket.reload.articles.count }.by(1)
  85. .and not_change { ticket.reload.state.name }.from('new')
  86. end
  87. end
  88. context 'with #preferences { "send-auto-response" => false }' do
  89. let!(:article) do
  90. create(:ticket_article,
  91. ticket: ticket,
  92. preferences: { 'send-auto-response' => false })
  93. end
  94. it 'does not fire' do
  95. expect { Observer::Transaction.commit }
  96. .not_to change { ticket.reload.articles.count }
  97. end
  98. end
  99. end
  100. context 'when new article is created via Channel::EmailParser.process' do
  101. context 'with a regular message' do
  102. let!(:article) do
  103. create(:ticket_article,
  104. ticket: ticket,
  105. message_id: raw_email[/(?<=^References: )\S*/],
  106. subject: raw_email[/(?<=^Subject: Re: ).*$/])
  107. end
  108. let(:raw_email) { File.read(Rails.root.join('test', 'data', 'mail', 'mail005.box')) }
  109. it 'fires (without altering ticket state)' do
  110. expect { Channel::EmailParser.new.process({}, raw_email) }
  111. .to not_change { Ticket.count }
  112. .and change { ticket.reload.articles.count }.by(2)
  113. .and not_change { ticket.reload.state.name }.from('new')
  114. end
  115. end
  116. context 'with delivery-failed "bounce message"' do
  117. let!(:article) do
  118. create(:ticket_article,
  119. ticket: ticket,
  120. message_id: raw_email[/(?<=^Message-ID: )\S*/])
  121. end
  122. let(:raw_email) { File.read(Rails.root.join('test', 'data', 'mail', 'mail055.box')) }
  123. it 'does not fire' do
  124. expect { Channel::EmailParser.new.process({}, raw_email) }
  125. .to change { ticket.reload.articles.count }.by(1)
  126. end
  127. end
  128. end
  129. end
  130. end
  131. end