failed_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Webhook::Message::Status::Failed, :aggregate_failures, current_user_id: 1 do
  4. describe '#process' do
  5. let(:channel) { create(:whatsapp_channel) }
  6. let(:from) do
  7. {
  8. phone: Faker::PhoneNumber.cell_phone_in_e164.delete('+'),
  9. name: Faker::Name.unique.name,
  10. }
  11. end
  12. let(:error_code) { 131_047 }
  13. let(:json) do
  14. {
  15. object: 'whatsapp_business_account',
  16. entry: [
  17. {
  18. id: '244742992051543',
  19. changes: [
  20. {
  21. value: {
  22. messaging_product: 'whatsapp',
  23. metadata: {
  24. display_phone_number: '15551340563',
  25. phone_number_id: channel.options[:phone_number_id],
  26. },
  27. statuses: [
  28. {
  29. id: message_id,
  30. status: 'failed',
  31. timestamp: '1708603746',
  32. recipient_id: '15551340563',
  33. errors: [
  34. {
  35. code: error_code,
  36. title: 'Re-engagement message',
  37. message: 'Re-engagement message',
  38. error_data: {
  39. details: 'Message failed to send because more than 24 hours have passed since the customer last replied to this number.'
  40. },
  41. href: 'https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes/'
  42. }
  43. ]
  44. }
  45. ]
  46. },
  47. field: 'messages'
  48. }
  49. ]
  50. }
  51. ]
  52. }.to_json
  53. end
  54. let(:data) { JSON.parse(json).deep_symbolize_keys }
  55. let(:article) { create(:whatsapp_article, :inbound, ticket: ticket) }
  56. let(:ticket) { create(:whatsapp_ticket, channel: channel) }
  57. let(:message_id) { article.message_id }
  58. context 'when all data is valid' do
  59. before { article }
  60. context 'with an recoverable error' do
  61. it 'creates an internal article with all error related information' do
  62. described_class.new(data:, channel:).process
  63. expect(Ticket::Article.last).to have_attributes(
  64. body: "Unable to handle WhatsApp message: Re-engagement message (131047)\n\nMessage failed to send because more than 24 hours have passed since the customer last replied to this number.\n\nhttps://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes/",
  65. content_type: 'text/plain',
  66. internal: true,
  67. )
  68. expect(channel.reload).to have_attributes(
  69. status_out: nil,
  70. last_log_out: nil,
  71. )
  72. end
  73. end
  74. context 'with an unrecoverable error' do
  75. let(:error_code) { 0 }
  76. it 'creates an internal article with all error related information and updates the channel as well' do
  77. described_class.new(data:, channel:).process
  78. expect(channel.reload).to have_attributes(
  79. status_out: 'error',
  80. last_log_out: 'Re-engagement message (0)',
  81. )
  82. end
  83. end
  84. end
  85. context 'when no related article exists' do
  86. before { article }
  87. let(:message_id) { "wamid.#{Faker::Number.unique.number}" }
  88. it 'raises an error' do
  89. expect { described_class.new(data:, channel:).process }
  90. .to raise_error(
  91. an_instance_of(Whatsapp::Webhook::Payload::ProcessableError)
  92. .and(
  93. having_attributes(
  94. reason: 'No related article found to process the status message on.'
  95. )
  96. )
  97. )
  98. end
  99. end
  100. end
  101. end