sticker_spec.rb 4.3 KB

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