document_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Webhook::Message::Document, :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. document: {
  35. mime_type: 'application/pdf',
  36. sha256: 'mi4AElysqJQ1Oc4pugVtrLKyiB+GLE/pzPEgtBsrcLk=',
  37. id: '1870770316696531',
  38. filename: 'lorem-ipsum.pdf',
  39. },
  40. type: 'document',
  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) { 'application/pdf' }
  52. let(:media_content) { SecureRandom.uuid }
  53. let(:media_file) { Tempfile.create('lipsum.pdf').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: '',
  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: 'document',
  98. filename: 'lorem-ipsum.pdf',
  99. }
  100. )
  101. expect(Store.last).to have_attributes(
  102. filename: 'lorem-ipsum.pdf',
  103. size: media_file.size.to_s,
  104. )
  105. expect(Store.last.preferences).to include(
  106. 'Mime-Type' => 'application/pdf',
  107. )
  108. # Welcome article
  109. expect(Ticket::Article.last).to have_attributes(
  110. body: 'Hey there!',
  111. content_type: 'text/plain',
  112. )
  113. end
  114. end
  115. context 'when the media type is not supported' do
  116. let(:url) { Faker::Internet.unique.url }
  117. let(:mime_type) { 'application/zip' }
  118. let(:media_content) { SecureRandom.uuid }
  119. let(:media_file) { Tempfile.create('lipsum.zip').tap { |f| File.write(f, media_content) } }
  120. let(:valid_checksum) { Digest::SHA2.new(256).hexdigest(media_content) }
  121. before do
  122. allow_any_instance_of(Whatsapp::Incoming::Media).to receive(:download).and_raise(Whatsapp::Incoming::Media::InvalidMediaTypeError, 'Invalid Media Type application/zip')
  123. end
  124. after do
  125. File.unlink(media_file)
  126. end
  127. it 'creates an article with error information' do
  128. described_class.new(data:, channel:).process
  129. expect(Ticket::Article.second_to_last.body).to include('Invalid Media Type application/zip')
  130. expect(Ticket::Article.second_to_last.internal).to be_truthy
  131. end
  132. end
  133. end
  134. end