text_reminder_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Webhook::Message::Text, :aggregate_failures, current_user_id: 1 do
  4. subject(:message) { described_class.new(data:, channel:) }
  5. let(:channel) { create(:whatsapp_channel) }
  6. let(:timestamp) { Time.zone.now.change(usec: 0) } # omit milliseconds
  7. let(:message_id) { 'wamid.HBgNNDkxNTE1NjA4MDY5OBUCABIYFjNFQjBDMUM4M0I5NDRFNThBMUQyMjYA' }
  8. let(:from) do
  9. {
  10. phone: Faker::PhoneNumber.cell_phone_in_e164.delete('+'),
  11. name: Faker::Name.unique.name,
  12. }
  13. end
  14. let(:json) do
  15. {
  16. object: 'whatsapp_business_account',
  17. entry: [{
  18. id: '222259550976437',
  19. changes: [{
  20. value: {
  21. messaging_product: 'whatsapp',
  22. metadata: {
  23. display_phone_number: '15551340563',
  24. phone_number_id: channel.options[:phone_number_id],
  25. },
  26. contacts: [{
  27. profile: {
  28. name: from[:name],
  29. },
  30. wa_id: from[:phone],
  31. }],
  32. messages: [{
  33. from: from[:phone],
  34. id: message_id,
  35. timestamp: timestamp.to_i,
  36. text: {
  37. body: 'Hello, world!',
  38. },
  39. type: 'text',
  40. }],
  41. },
  42. field: 'messages',
  43. }],
  44. }],
  45. }.to_json
  46. end
  47. let(:data) { JSON.parse(json).deep_symbolize_keys }
  48. context 'when an initial incoming message is received' do
  49. before do
  50. message.process
  51. end
  52. it 'schedules a reminder job for 23 hours in the future' do
  53. expect(Ticket.last.preferences).to include(
  54. whatsapp: include(
  55. last_reminder_job_id: Delayed::Job.last.id,
  56. ),
  57. )
  58. expect(Delayed::Job.last).to have_attributes(run_at: timestamp + 23.hours)
  59. end
  60. end
  61. context 'when an additional incoming message is received' do
  62. let(:article) { create(:whatsapp_article, ticket: ticket) }
  63. let(:ticket) { create(:whatsapp_ticket, channel: channel, customer: customer) }
  64. let(:customer) { create(:customer, mobile: "+#{from[:phone]}") }
  65. let(:job_id) { ScheduledWhatsappReminderJob.perform_at(Time.zone.now, ticket, Locale.default).provider_job_id }
  66. before do
  67. travel_to 12.hours.ago
  68. article
  69. preferences = ticket.preferences
  70. preferences[:whatsapp][:last_reminder_job_id] = job_id
  71. ticket.update!(preferences:)
  72. travel_back
  73. message.process
  74. end
  75. it 'cancels the last reminder job' do
  76. expect(Delayed::Job.exists?(job_id)).to be(false)
  77. end
  78. it 'schedules a new reminder job for 23 hours in the future' do
  79. expect(ticket.reload.preferences).to include(
  80. whatsapp: include(
  81. last_reminder_job_id: Delayed::Job.last.id,
  82. ),
  83. )
  84. expect(Delayed::Job.last).to have_attributes(run_at: timestamp + 23.hours)
  85. end
  86. end
  87. context 'when automatic reminders are turned off' do
  88. let(:channel) { create(:whatsapp_channel, reminder_active: false) }
  89. before do
  90. message.process
  91. end
  92. it 'does not schedule a reminder job' do
  93. expect(Ticket.last.preferences).to include(
  94. whatsapp: not_include(
  95. last_reminder_offset: 23.hours,
  96. ),
  97. )
  98. end
  99. end
  100. end