image_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Webhook::Message::Image, :aggregate_failures, current_user_id: 1 do
  4. describe '#process' do
  5. let(:channel) { create(:whatsapp_channel, welcome: 'Hey there!') }
  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. image: {
  35. mime_type: 'image/jpeg',
  36. sha256: 'mi4AElysqJQ1Oc4pugVtrLKyiB+GLE/pzPEgtBsrcLk=',
  37. id: '1870770316696531',
  38. caption: 'My beautiful image',
  39. },
  40. type: 'image',
  41. }],
  42. },
  43. field: 'messages',
  44. }],
  45. }],
  46. }.to_json
  47. end
  48. let(:data) { JSON.parse(json).deep_symbolize_keys }
  49. context 'when all data is valid' do
  50. let(:url) { Faker::Internet.unique.url }
  51. let(:mime_type) { 'image/jpeg' }
  52. let(:media_content) { SecureRandom.uuid }
  53. let(:media_file) { Tempfile.create('social.jpg').tap { |f| File.write(f, media_content) } }
  54. let(:valid_checksum) { Digest::SHA2.new(256).hexdigest(media_content) }
  55. let(:internal_response1) do
  56. Struct.new(:data, :error).new(Struct.new(:url, :mime_type, :sha256).new(url, mime_type, valid_checksum), nil)
  57. end
  58. let(:internal_response2) do
  59. Struct.new(:data, :error).new(Struct.new(:success).new(true), nil)
  60. end
  61. before do
  62. allow_any_instance_of(WhatsappSdk::Api::Medias).to receive(:media).and_return(internal_response1)
  63. allow_any_instance_of(WhatsappSdk::Api::Medias).to receive(:download).and_return(internal_response2)
  64. allow_any_instance_of(Whatsapp::Incoming::Media).to receive(:with_tmpfile).and_yield(media_file)
  65. end
  66. after do
  67. File.unlink(media_file)
  68. end
  69. it 'creates a user' do
  70. expect { described_class.new(data:, channel:).process }.to change(User, :count).by(1)
  71. end
  72. it 'creates a ticket + an article' do
  73. described_class.new(data:, channel:).process
  74. expect(Ticket.last).to have_attributes(
  75. title: "#{from[:name]} (+#{from[:phone]}) via WhatsApp",
  76. group_id: channel.group_id,
  77. )
  78. expect(Ticket.last.preferences).to include(
  79. channel_id: channel.id,
  80. channel_area: channel.area,
  81. whatsapp: {
  82. from: {
  83. phone_number: from[:phone],
  84. display_name: from[:name],
  85. },
  86. timestamp_incoming: '1707921703',
  87. },
  88. )
  89. expect(Ticket::Article.second_to_last).to have_attributes(
  90. body: '<p>My beautiful image</p>',
  91. )
  92. expect(Ticket::Article.second_to_last.preferences).to include(
  93. whatsapp: {
  94. entry_id: '222259550976437',
  95. media_id: '1870770316696531',
  96. message_id: 'wamid.HBgNNDkxNTE1NjA4MDY5OBUCABIYFjNFQjBDMUM4M0I5NDRFNThBMUQyMjYA',
  97. type: 'image',
  98. }
  99. )
  100. expect(Store.last).to have_attributes(
  101. filename: "#{Ticket.last.number}-1870770316696531.jpeg",
  102. size: media_file.size.to_s,
  103. )
  104. expect(Store.last.preferences).to include(
  105. 'Mime-Type' => 'image/jpeg',
  106. )
  107. # Welcome article
  108. expect(Ticket::Article.last).to have_attributes(
  109. body: 'Hey there!',
  110. content_type: 'text/plain',
  111. )
  112. end
  113. end
  114. end
  115. end