scheduled_whatsapp_reminder_job_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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: a_string_starting_with('ScheduledWhatsappReminderJob/'),
  14. scheduled_at: reminder_time,
  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::DEFAULT_REMINDER_MESSAGE.truncate(100, omission: '…'),
  32. internal: false,
  33. body: described_class::DEFAULT_REMINDER_MESSAGE,
  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. context 'with customized reminder message' do
  70. let(:reminder_message) { Faker::Lorem.unique.sentence }
  71. let(:channel) { create(:whatsapp_channel, reminder_message:) }
  72. it 'adds a reminder article to the ticket' do
  73. expect(job.perform(ticket, locale)).to have_attributes(
  74. ticket_id: ticket.id,
  75. type_id: Ticket::Article::Type.lookup(name: 'whatsapp message').id,
  76. sender_id: Ticket::Article::Sender.lookup(name: 'System').id,
  77. from: "#{channel.options[:name]} (#{channel.options[:phone_number]})",
  78. to: article.from,
  79. subject: reminder_message.truncate(100, omission: '…'),
  80. internal: false,
  81. body: reminder_message,
  82. content_type: 'text/plain',
  83. )
  84. end
  85. end
  86. end
  87. end