media_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Incoming::Media do
  4. let(:instance) { described_class.new(**params) }
  5. let(:media_content) { SecureRandom.uuid }
  6. let(:media_file) { Tempfile.create('social.jpg').tap { |f| File.write(f, media_content) } }
  7. let(:valid_checksum) { Digest::SHA2.new(256).hexdigest(media_content) }
  8. let(:params) do
  9. {
  10. access_token: Faker::Omniauth.unique.facebook[:credentials][:token],
  11. }
  12. end
  13. describe '.download' do
  14. let(:media_id) { Faker::Number.unique.number(digits: 15) }
  15. before do
  16. allow_any_instance_of(WhatsappSdk::Api::Medias).to receive(:media).and_return(internal_response1)
  17. allow_any_instance_of(WhatsappSdk::Api::Medias).to receive(:download).and_return(internal_response2)
  18. end
  19. context 'with successful response' do
  20. let(:url) { Faker::Internet.unique.url }
  21. let(:mime_type) { 'image/jpeg' }
  22. let(:internal_response1) do
  23. Struct.new(:data, :error).new(Struct.new(:url, :mime_type, :sha256).new(url, mime_type, valid_checksum), nil)
  24. end
  25. let(:internal_response2) do
  26. Struct.new(:data, :error).new(Struct.new(:success).new(true), nil)
  27. end
  28. before do
  29. allow_any_instance_of(described_class).to receive(:with_tmpfile).and_yield(media_file)
  30. end
  31. after do
  32. File.unlink(media_file)
  33. end
  34. it 'returns downloaded media in base64 encoding' do
  35. expect(instance.download(media_id:)).to eq([File.read(media_file), mime_type])
  36. end
  37. end
  38. context 'with unsuccessful response (1)' do
  39. let(:internal_response1) { Struct.new(:data, :error, :raw_response).new(nil, Struct.new(:message).new('error message'), '{}') }
  40. let(:internal_response2) do
  41. Struct.new(:data, :error).new(Struct.new(:success).new(true), nil)
  42. end
  43. it 'raises an error' do
  44. expect { instance.download(media_id:) }.to raise_error(Whatsapp::Client::CloudAPIError, 'error message')
  45. end
  46. end
  47. context 'with unsuccessful response (2)' do
  48. let(:url) { Faker::Internet.unique.url }
  49. let(:mime_type) { 'image/jpeg' }
  50. let(:internal_response1) do
  51. Struct.new(:data, :error).new(Struct.new(:url, :mime_type, :sha256).new(url, mime_type, valid_checksum), nil)
  52. end
  53. let(:internal_response2) { Struct.new(:data, :error, :raw_response).new(nil, Struct.new(:message).new('error message'), '{}') }
  54. it 'raises an error' do
  55. expect { instance.download(media_id:) }.to raise_error(Whatsapp::Client::CloudAPIError, 'error message')
  56. end
  57. end
  58. context 'with an invalid checksum' do
  59. let(:url) { Faker::Internet.unique.url }
  60. let(:mime_type) { 'image/jpeg' }
  61. let(:internal_response1) do
  62. Struct.new(:data, :error).new(Struct.new(:url, :mime_type, :sha256).new(url, mime_type, Faker::Crypto.unique.sha256), nil)
  63. end
  64. let(:internal_response2) do
  65. Struct.new(:data, :error).new(Struct.new(:success).new(true), nil)
  66. end
  67. before do
  68. allow_any_instance_of(described_class).to receive(:with_tmpfile).and_yield(media_file)
  69. end
  70. after do
  71. File.unlink(media_file)
  72. end
  73. it 'raises an error' do
  74. expect { instance.download(media_id:) }.to raise_error(Whatsapp::Incoming::Media::InvalidChecksumError, 'Integrity verification of the downloaded WhatsApp media failed.')
  75. end
  76. end
  77. end
  78. end