media_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Outgoing::Message::Media do
  4. let(:instance) { described_class.new(**params) }
  5. let(:params) do
  6. {
  7. access_token: Faker::Omniauth.unique.facebook[:credentials][:token],
  8. phone_number_id: Faker::Number.unique.number(digits: 15),
  9. recipient_number: Faker::PhoneNumber.unique.cell_phone_in_e164,
  10. }
  11. end
  12. describe '.supported_media_type?' do
  13. supported_mime_types = {
  14. audio: ['audio/aac', 'audio/mp4', 'audio/mpeg', 'audio/amr', 'audio/ogg'],
  15. document: ['text/plain', 'application/pdf', 'application/vnd.ms-powerpoint', 'application/msword', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
  16. image: ['image/jpeg', 'image/png'],
  17. sticker: ['image/webp'],
  18. video: ['video/mp4', 'video/3gp'],
  19. }
  20. supported_mime_types.each_value do |mime_types|
  21. mime_types.each do |mime_type|
  22. it "returns true for supported media type (#{mime_type})" do
  23. expect(instance.supported_media_type?(mime_type:)).to be(true)
  24. end
  25. end
  26. end
  27. it 'returns false for unsupported media type' do
  28. expect(instance.supported_media_type?(mime_type: 'application/octet-stream')).to be(false)
  29. end
  30. end
  31. shared_examples 'delivering media message type and handling response errors' do |mime_type:, mock_method:|
  32. let(:store) { create(:store, preferences: { 'Mime-Type' => mime_type }) }
  33. let(:media_id) { Faker::Number.unique.number(digits: 15) }
  34. let(:message_id) { "wamid.#{Faker::Crypto.unique.sha1}==" }
  35. let(:internal_response1) { Struct.new(:data, :error).new(Struct.new(:id).new(media_id), nil) }
  36. let(:internal_response2) { Struct.new(:data, :error).new(Struct.new(:messages).new([Struct.new(:id).new(message_id)]), nil) }
  37. before do
  38. allow_any_instance_of(WhatsappSdk::Api::Medias).to receive(:upload).and_return(internal_response1)
  39. allow_any_instance_of(WhatsappSdk::Api::Messages).to receive(mock_method).and_return(internal_response2)
  40. end
  41. context 'with successful response' do
  42. let(:response) { { id: message_id } }
  43. it 'returns sent message id' do
  44. expect(instance.deliver(store:)).to eq(response)
  45. end
  46. context 'with optional caption' do
  47. it 'returns sent message id' do
  48. expect(instance.deliver(store:, caption: 'foobar')).to eq(response)
  49. end
  50. end
  51. end
  52. context 'with unsuccessful response (1)' do
  53. let(:internal_response1) { Struct.new(:data, :error, :raw_response).new(nil, Struct.new(:message).new('error message'), '{}') }
  54. it 'raises an error' do
  55. expect { instance.deliver(store:) }.to raise_error(Whatsapp::Client::CloudAPIError, 'error message')
  56. end
  57. end
  58. context 'with unsuccessful response (2)' do
  59. let(:internal_response2) { Struct.new(:data, :error, :raw_response).new(nil, Struct.new(:message).new('error message'), '{}') }
  60. it 'raises an error' do
  61. expect { instance.deliver(store:) }.to raise_error(Whatsapp::Client::CloudAPIError, 'error message')
  62. end
  63. end
  64. end
  65. describe '.deliver' do
  66. context 'with audio media type' do
  67. it_behaves_like 'delivering media message type and handling response errors', mime_type: 'audio/ogg', mock_method: :send_audio
  68. end
  69. context 'with document media type' do
  70. it_behaves_like 'delivering media message type and handling response errors', mime_type: 'application/pdf', mock_method: :send_document
  71. end
  72. context 'with image media type' do
  73. it_behaves_like 'delivering media message type and handling response errors', mime_type: 'image/png', mock_method: :send_image
  74. end
  75. context 'with sticker media type' do
  76. it_behaves_like 'delivering media message type and handling response errors', mime_type: 'image/webp', mock_method: :send_sticker
  77. end
  78. context 'with video media type' do
  79. it_behaves_like 'delivering media message type and handling response errors', mime_type: 'video/mp4', mock_method: :send_video
  80. end
  81. end
  82. end