text_spec.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. describe '#process' do
  5. let(:channel) { create(:whatsapp_channel, welcome: 'W' * 120) }
  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. id: '222259550976437',
  17. changes: [{
  18. value: {
  19. messaging_product: 'whatsapp',
  20. metadata: {
  21. display_phone_number: '15551340563',
  22. phone_number_id: channel.options[:phone_number_id],
  23. },
  24. contacts: [{
  25. profile: {
  26. name: from[:name],
  27. },
  28. wa_id: from[:phone],
  29. }],
  30. messages: [{
  31. from: from[:phone],
  32. id: 'wamid.HBgNNDkxNTE1NjA4MDY5OBUCABIYFjNFQjBDMUM4M0I5NDRFNThBMUQyMjYA',
  33. timestamp: '1707921703',
  34. text: {
  35. body: 'Hello, world!',
  36. },
  37. type: 'text',
  38. }],
  39. },
  40. field: 'messages',
  41. }],
  42. }],
  43. }.to_json
  44. end
  45. let(:data) { JSON.parse(json).deep_symbolize_keys }
  46. context 'when all data is valid' do
  47. it 'creates a user' do
  48. expect { described_class.new(data:, channel:).process }.to change(User, :count).by(1)
  49. end
  50. it 'creates a ticket + an article' do
  51. described_class.new(data:, channel:).process
  52. expect(Ticket.last).to have_attributes(
  53. title: "#{from[:name]} (+#{from[:phone]}) via WhatsApp",
  54. group_id: channel.group_id,
  55. )
  56. expect(Ticket.last.preferences).to include(
  57. channel_id: channel.id,
  58. channel_area: channel.area,
  59. whatsapp: {
  60. from: {
  61. phone_number: from[:phone],
  62. display_name: from[:name],
  63. },
  64. timestamp_incoming: '1707921703',
  65. },
  66. )
  67. expect(Ticket::Article.second_to_last).to have_attributes(
  68. body: 'Hello, world!',
  69. content_type: 'text/plain',
  70. )
  71. expect(Ticket::Article.second_to_last.preferences).to include(
  72. whatsapp: {
  73. entry_id: '222259550976437',
  74. message_id: 'wamid.HBgNNDkxNTE1NjA4MDY5OBUCABIYFjNFQjBDMUM4M0I5NDRFNThBMUQyMjYA',
  75. type: 'text',
  76. }
  77. )
  78. # Welcome article
  79. expect(Ticket::Article.last).to have_attributes(
  80. # truncated subject
  81. subject: "#{'W' * 99}…",
  82. body: 'W' * 120,
  83. content_type: 'text/plain',
  84. )
  85. end
  86. end
  87. end
  88. end