update_spec.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::Channel::Whatsapp::Update, current_user_id: 1 do
  4. subject(:service) { described_class.new(params:, channel_id: channel.id) }
  5. shared_examples 'raising an error' do |klass, message|
  6. it 'raises an error' do
  7. expect { service.execute }.to raise_error(klass, message)
  8. end
  9. end
  10. describe '#execute' do
  11. let(:channel) { create(:whatsapp_channel) }
  12. let(:phone_number_id) { Faker::Number.unique.number(digits: 15) }
  13. let(:params) do
  14. {
  15. group_id: 1,
  16. business_id: Faker::Number.unique.number(digits: 15),
  17. access_token: Faker::Omniauth.unique.facebook[:credentials][:token],
  18. app_secret: Faker::Crypto.unique.md5,
  19. phone_number_id:,
  20. welcome: Faker::Lorem.unique.sentence,
  21. }
  22. end
  23. context 'with all params' do
  24. let(:phone_number_info) do
  25. {
  26. name: Faker::Name.unique.name,
  27. phone_number: Faker::PhoneNumber.unique.cell_phone_with_country_code,
  28. }
  29. end
  30. before do
  31. allow_any_instance_of(Whatsapp::Account::PhoneNumbers).to receive(:get).and_return(phone_number_info)
  32. end
  33. it 'updates the existing channel' do
  34. expect { service.execute }
  35. .to change { channel.reload.options['business_id'] }.to(params[:business_id])
  36. .and change { channel.options['access_token'] }.to(params[:access_token])
  37. .and change { channel.options['app_secret'] }.to(params[:app_secret])
  38. .and change { channel.options['welcome'] }.to(params[:welcome])
  39. .and change { channel.options['name'] }.to(phone_number_info[:name])
  40. .and change { channel.options['phone_number'] }.to(phone_number_info[:phone_number])
  41. .and not_change { channel.options['adapter'] }
  42. .and not_change { channel.options['callback_url_uuid'] }
  43. .and not_change { channel.options['verify_token'] }
  44. end
  45. end
  46. context 'when phone number metadata cannot be retrieved' do
  47. before do
  48. allow_any_instance_of(Whatsapp::Account::PhoneNumbers).to receive(:get).and_return(nil)
  49. end
  50. it_behaves_like 'raising an error', StandardError, 'Could not fetch WhatsApp phone number details.'
  51. end
  52. end
  53. end