text_spec.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Outgoing::Message::Text 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 '.deliver' do
  13. let(:body) { 'foobar' }
  14. before do
  15. allow_any_instance_of(WhatsappSdk::Api::Messages).to receive(:send_text).and_return(internal_response)
  16. end
  17. context 'with successful response' do
  18. let(:message_id) { "wamid.#{Faker::Crypto.unique.sha1}==" }
  19. let(:response) { { id: message_id } }
  20. let(:internal_response) do
  21. Struct.new(:data, :error).new(Struct.new(:messages).new([Struct.new(:id).new(message_id)]), nil)
  22. end
  23. it 'returns sent message id' do
  24. expect(instance.deliver(body:)).to eq(response)
  25. end
  26. end
  27. context 'with unsuccessful response' do
  28. let(:internal_response) { Struct.new(:data, :error, :raw_response).new(nil, Struct.new(:message).new('error message'), '{}') }
  29. it 'raises an error' do
  30. expect { instance.deliver(body:) }.to raise_error(Whatsapp::Client::CloudAPIError, 'error message')
  31. end
  32. end
  33. end
  34. end