phone_numbers_spec.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Account::PhoneNumbers, :aggregate_failures do
  4. let(:options) { { access_token: '1234', business_id: '1234' } }
  5. let(:instance) { described_class.new(**options) }
  6. describe '.all' do
  7. before do
  8. allow_any_instance_of(WhatsappSdk::Api::PhoneNumbers).to receive(:registered_numbers).and_return(internal_response)
  9. end
  10. let(:internal_response) do
  11. Struct.new(:data).new(Struct.new(:phone_numbers).new(internal_response_data))
  12. end
  13. let(:internal_response_data) do
  14. [
  15. Struct.new(:id, :display_phone_number, :verified_name).new('888', '+49 888 888', 'Test Corp 8'),
  16. Struct.new(:id, :display_phone_number, :verified_name).new('999', '+49 999 999', 'Test Corp 9'),
  17. ]
  18. end
  19. it 'returns numbers' do
  20. expect(instance.all).to eq({ '888' => 'Test Corp 8 (+49 888 888)', '999' => 'Test Corp 9 (+49 999 999)' })
  21. end
  22. context 'with unsuccessful response' do
  23. let(:internal_response) { Struct.new(:data).new(nil) }
  24. it 'returns empty array' do
  25. expect(instance.all).to eq([])
  26. end
  27. end
  28. context 'without business_id' do
  29. let(:options) { { access_token: '1234' } }
  30. it 'fails with an error' do
  31. expect { instance.all }.to raise_error(ArgumentError, "The required parameter 'business_id' is missing.")
  32. end
  33. end
  34. end
  35. describe '.get' do
  36. before do
  37. allow_any_instance_of(WhatsappSdk::Api::PhoneNumbers).to receive(:registered_number).with(1234).and_return(internal_response)
  38. end
  39. let(:internal_response) do
  40. Struct.new(:data).new(Struct.new(:display_phone_number, :verified_name).new('+49 888 888', 'Test Corp 8'),)
  41. end
  42. it 'returns numbers' do
  43. expect(instance.get(1234)).to eq({ phone_number: '+49 888 888', name: 'Test Corp 8' })
  44. end
  45. context 'with unsuccessful response' do
  46. let(:internal_response) { Struct.new(:data).new(nil) }
  47. it 'returns nil' do
  48. expect(instance.get(1234)).to be_nil
  49. end
  50. end
  51. end
  52. end