scheduled_whatsapp_reminder_job_spec.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ScheduledWhatsappReminderJob, type: :job do
  4. let(:channel) { create(:whatsapp_channel) }
  5. let(:ticket) { create(:whatsapp_ticket, channel: channel) }
  6. let(:reminder_time) { 12.hours.from_now }
  7. let(:locale) { Locale.default }
  8. describe '#perform_at' do
  9. it 'schedules ticket unique delayed job at appointed time' do
  10. expect(described_class.perform_at(reminder_time, ticket, locale))
  11. .to have_attributes(
  12. arguments: [ticket, locale],
  13. lock_key: "ScheduledWhatsappReminderJob/#{ticket.id}",
  14. scheduled_at: reminder_time.to_f,
  15. )
  16. end
  17. end
  18. describe '.perform' do
  19. subject(:job) { described_class.new(reminder_time, ticket, locale) }
  20. let(:article) { create(:whatsapp_article, ticket: ticket) }
  21. before do
  22. article
  23. end
  24. it 'adds a reminder article to the ticket' do
  25. expect(job.perform(ticket, locale)).to have_attributes(
  26. ticket_id: ticket.id,
  27. type_id: Ticket::Article::Type.lookup(name: 'whatsapp message').id,
  28. sender_id: Ticket::Article::Sender.lookup(name: 'System').id,
  29. from: "#{channel.options[:name]} (#{channel.options[:phone_number]})",
  30. to: article.from,
  31. subject: described_class::REMINDER_TEMPLATE.truncate(100, omission: '…'),
  32. internal: false,
  33. body: described_class::REMINDER_TEMPLATE,
  34. content_type: 'text/plain',
  35. )
  36. end
  37. context 'with locale other than default' do
  38. let(:locale) { 'de-de' }
  39. let(:source) { 'Hello, the customer service window for this conversation is about to expire, please reply to keep it open.' }
  40. let(:target) { "[translated] #{source}" }
  41. before do
  42. allow(Translation).to receive(:translate).with(locale, source).and_return(target)
  43. end
  44. it 'translates the reminder text' do
  45. expect(job.perform(ticket, locale)).to have_attributes(
  46. subject: target.truncate(100, omission: '…'),
  47. body: target,
  48. )
  49. end
  50. end
  51. context 'when automatic reminders are turned off' do
  52. let(:channel) { create(:whatsapp_channel, reminder_active: false) }
  53. it 'skips the reminder article' do
  54. expect(job.perform(ticket, locale)).to be_falsey
  55. end
  56. end
  57. context 'with another channel' do
  58. let(:channel) { create(:facebook_channel) }
  59. it 'skips the reminder article' do
  60. expect(job.perform(ticket, locale)).to be_falsey
  61. end
  62. end
  63. context 'with closed ticket' do
  64. let(:ticket) { create(:whatsapp_ticket, channel: channel, state: Ticket::State.find_by(name: 'closed')) }
  65. it 'skips the reminder article' do
  66. expect(job.perform(ticket, locale)).to be_falsey
  67. end
  68. end
  69. end
  70. end