enqueue_communicate_email_job_spec.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Ticket::Article::EnqueueCommunicateEmailJob, performs_jobs: true do
  4. before { allow(Delayed::Job).to receive(:enqueue).and_call_original }
  5. let(:article) { create(:ticket_article, **(try(:factory_options) || {})) }
  6. let(:channel) do
  7. create(:email_channel, outbound: {
  8. 'adapter' => 'smtp',
  9. 'options' => {
  10. 'host' => '10.1.1.1',
  11. 'port' => 25,
  12. 'ssl' => true,
  13. 'ssl_verify' => true,
  14. 'user' => 'other@example.com',
  15. 'password' => 'somepass',
  16. 'authentication' => nil
  17. }
  18. })
  19. end
  20. shared_examples 'for no-op' do
  21. it 'is a no-op' do
  22. expect { article }.not_to have_enqueued_job(TicketArticleCommunicateEmailJob)
  23. end
  24. end
  25. shared_examples 'for success' do
  26. it 'enqueues the Email background job' do
  27. expect { article }.to have_enqueued_job(TicketArticleCommunicateEmailJob)
  28. end
  29. end
  30. shared_examples 'for failure' do
  31. it 'executes the enqueued Email background job that will time out', :aggregate_failures do
  32. stub_const('Channel::Driver::Smtp::DEFAULT_OPEN_TIMEOUT', 0.01)
  33. stub_const('Channel::Driver::Smtp::DEFAULT_READ_TIMEOUT', 0.01)
  34. expect { article.ticket.group.email_address.update(channel:) }.to have_enqueued_job(TicketArticleCommunicateEmailJob)
  35. expect(TicketArticleCommunicateEmailJob).to have_been_enqueued
  36. expect { perform_enqueued_jobs commit_transaction: true }.not_to raise_error
  37. expect(article.reload.preferences).to include(
  38. delivery_status: 'fail',
  39. delivery_status_message: "Can't use Channel::Driver::Smtp: #<Net::OpenTimeout: execution expired>",
  40. )
  41. expect(channel.reload).to have_attributes(
  42. status_out: 'error',
  43. last_log_out: '#<Net::OpenTimeout: execution expired>',
  44. )
  45. end
  46. end
  47. context 'when in Import Mode' do
  48. before { Setting.set('import_mode', true) }
  49. include_examples 'for no-op'
  50. end
  51. context 'when article is created during Channel::EmailParser#process', application_handle: 'scheduler.postmaster' do
  52. include_examples 'for no-op'
  53. end
  54. context 'when article is from a customer' do
  55. let(:factory_options) { { sender_name: 'Customer' } }
  56. include_examples 'for no-op'
  57. end
  58. context 'when article is an email' do
  59. let(:factory_options) { { sender_name: 'Agent', type_name: 'email' } }
  60. include_examples 'for success'
  61. end
  62. context 'when article is an email but cannot be sent' do
  63. let(:factory_options) { { sender_name: 'Agent', type_name: 'email' } }
  64. include_examples 'for failure'
  65. end
  66. end