sent_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Webhook::Message::Status::Sent, :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(:json) do
  13. {
  14. object: 'whatsapp_business_account',
  15. entry: [
  16. {
  17. id: '260895230431646',
  18. changes: [
  19. {
  20. value: {
  21. messaging_product: 'whatsapp',
  22. metadata: {
  23. display_phone_number: '15551340563',
  24. phone_number_id: channel.options[:phone_number_id],
  25. },
  26. statuses: [
  27. {
  28. id: message_id,
  29. status: 'sent',
  30. timestamp: '1709577872',
  31. recipient_id: '15551340563',
  32. conversation: {
  33. id: '2f3568fab8879aa0194e66aac1a0618e',
  34. origin: {
  35. type: 'service'
  36. }
  37. },
  38. pricing: {
  39. billable: true,
  40. pricing_model: 'CBP',
  41. category: 'service'
  42. }
  43. }
  44. ]
  45. },
  46. field: 'messages'
  47. }
  48. ]
  49. }
  50. ]
  51. }.to_json
  52. end
  53. let(:data) { JSON.parse(json).deep_symbolize_keys }
  54. let(:article) { create(:whatsapp_article, :inbound, ticket: ticket) }
  55. let(:ticket) { create(:whatsapp_ticket, channel: channel) }
  56. let(:message_id) { article.message_id }
  57. context 'when all data is valid' do
  58. before { article }
  59. it 'updates the ticket and article preferences accordingly' do
  60. described_class.new(data:, channel:).process
  61. expect(Ticket.last.preferences).to include(
  62. whatsapp: include(
  63. timestamp_outgoing: '1709577872',
  64. )
  65. )
  66. expect(Ticket::Article.last.preferences).to include(
  67. whatsapp: include(
  68. timestamp_sent: '1709577872',
  69. ),
  70. )
  71. end
  72. end
  73. context 'when no related article exists' do
  74. before { article }
  75. let(:message_id) { "wamid.#{Faker::Number.unique.number}" }
  76. it 'raises an error' do
  77. expect { described_class.new(data:, channel:).process }
  78. .to raise_error(
  79. an_instance_of(Whatsapp::Webhook::Payload::ProcessableError)
  80. .and(
  81. having_attributes(
  82. reason: 'No related article found to process the status message on.'
  83. )
  84. )
  85. )
  86. end
  87. end
  88. end
  89. end