error_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Whatsapp::Webhook::Payload::Error', :aggregate_failures, current_user_id: 1 do
  4. let(:channel) { create(:whatsapp_channel) }
  5. let(:wa_id) { Faker::PhoneNumber.unique.cell_phone_in_e164.delete('+') }
  6. let(:errors) { {} }
  7. let(:json) do
  8. {
  9. object: 'whatsapp_business_account',
  10. entry: [
  11. {
  12. id: '222259550976437',
  13. changes: [
  14. {
  15. value: {
  16. messaging_product: 'whatsapp',
  17. metadata: {
  18. display_phone_number: channel.options[:phone_number],
  19. phone_number_id: channel.options[:phone_number_id]
  20. },
  21. contacts: [
  22. {
  23. profile: {
  24. name: Faker::Name.unique.name
  25. },
  26. wa_id: wa_id
  27. }
  28. ],
  29. messages: [
  30. {
  31. from: wa_id,
  32. id: 'wamid.NDkxNTE1NTU1NTU5ODNBNTU3NkYyQTJCM0FGMUE1RjZECg==',
  33. timestamp: '1733484661',
  34. errors: [
  35. error
  36. ],
  37. type: 'any'
  38. }
  39. ]
  40. },
  41. field: 'messages'
  42. }
  43. ]
  44. }
  45. ]
  46. }.to_json
  47. end
  48. let(:uuid) { channel.options[:callback_url_uuid] }
  49. let(:signature) do
  50. OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), channel.options[:app_secret], json)
  51. end
  52. context 'when error is initialized by sender' do
  53. let(:error) do
  54. {
  55. code: 131_051,
  56. title: 'Message type unknown',
  57. message: 'Message type unknown',
  58. error_data: {
  59. details: 'Message type is currently not supported.'
  60. }
  61. }
  62. end
  63. let(:message) { instance_double(Whatsapp::Outgoing::Message::Text) }
  64. before do
  65. allow(Whatsapp::Outgoing::Message::Text)
  66. .to receive(:new)
  67. .and_return(message)
  68. allow(message)
  69. .to receive(:deliver)
  70. .with(body: "Apologies, we're unable to process this kind of message due to restrictions within WhatsApp Business.")
  71. .and_return(true)
  72. allow(Rails.logger).to receive(:error)
  73. begin
  74. Whatsapp::Webhook::Payload.new(json: json, signature: signature, uuid: uuid).process
  75. rescue Whatsapp::Webhook::Payload::ProcessableError
  76. # noop
  77. end
  78. end
  79. it 'does log error' do
  80. expect(Rails.logger).to have_received(:error).once
  81. end
  82. it 'does inform sender' do
  83. expect(message).to have_received(:deliver).once
  84. end
  85. it 'does not update channel status' do
  86. expect(channel.reload.status_out).to be_nil
  87. expect(channel.reload.last_log_out).to be_nil
  88. end
  89. end
  90. context 'when error is recoverable' do
  91. let(:error) do
  92. {
  93. code: 131_026,
  94. title: 'Message undeliverable',
  95. message: 'Message undeliverable',
  96. error_data: {
  97. details: 'Message could not be delivered.'
  98. }
  99. }
  100. end
  101. before do
  102. allow(Rails.logger).to receive(:error)
  103. begin
  104. Whatsapp::Webhook::Payload.new(json: json, signature: signature, uuid: uuid).process
  105. rescue Whatsapp::Webhook::Payload::ProcessableError
  106. # noop
  107. end
  108. end
  109. it 'does log error' do
  110. expect(Rails.logger).to have_received(:error).once
  111. end
  112. it 'does not update channel status' do
  113. expect(channel.reload.status_out).to be_nil
  114. expect(channel.reload.last_log_out).to be_nil
  115. end
  116. end
  117. context 'when error is not recoverable' do
  118. let(:error) do
  119. {
  120. code: 131_056,
  121. title: 'Pair rate limit hit',
  122. message: 'Pair rate limit hit',
  123. error_data: {
  124. details: 'Too many messages sent to this user.'
  125. }
  126. }
  127. end
  128. before do
  129. allow(Rails.logger).to receive(:error)
  130. begin
  131. Whatsapp::Webhook::Payload.new(json: json, signature: signature, uuid: uuid).process
  132. rescue Whatsapp::Webhook::Payload::ProcessableError
  133. # noop
  134. end
  135. end
  136. it 'does log error' do
  137. expect(Rails.logger).to have_received(:error).once
  138. end
  139. it 'does update channel status' do
  140. expect(channel.reload.status_out).to eq('error')
  141. expect(channel.reload.last_log_out).to eq('Pair rate limit hit (131056)')
  142. end
  143. end
  144. end