read_spec.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Webhook::Message::Status::Read, :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: 'read',
  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::Article.last.preferences).to include(
  62. whatsapp: include(
  63. timestamp_read: '1709577872',
  64. ),
  65. )
  66. end
  67. end
  68. context 'when no related article exists' do
  69. before { article }
  70. let(:message_id) { "wamid.#{Faker::Number.unique.number}" }
  71. it 'raises an error' do
  72. expect { described_class.new(data:, channel:).process }
  73. .to raise_error(
  74. an_instance_of(Whatsapp::Webhook::Payload::ProcessableError)
  75. .and(
  76. having_attributes(
  77. reason: 'No related article found to process the status message on.'
  78. )
  79. )
  80. )
  81. end
  82. end
  83. end
  84. end