create_spec.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::Channel::Whatsapp::Create, :aggregate_failures, current_user_id: 1 do
  4. subject(:service) { described_class.new(params: params) }
  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(:phone_number_id) { Faker::Number.unique.number(digits: 15) }
  12. let(:params) do
  13. {
  14. group_id: 1,
  15. business_id: Faker::Number.unique.number(digits: 15),
  16. access_token: Faker::Omniauth.unique.facebook[:credentials][:token],
  17. app_secret: Faker::Crypto.unique.md5,
  18. phone_number_id:,
  19. welcome: Faker::Lorem.unique.sentence,
  20. }
  21. end
  22. context 'with all params' do
  23. let(:phone_number_info) do
  24. {
  25. name: Faker::Name.unique.name,
  26. phone_number: Faker::PhoneNumber.unique.cell_phone_with_country_code,
  27. }
  28. end
  29. let(:initial_options) do
  30. {
  31. adapter: 'whatsapp',
  32. callback_url_uuid: SecureRandom.uuid,
  33. verify_token: SecureRandom.urlsafe_base64(12),
  34. }
  35. end
  36. before do
  37. allow_any_instance_of(Whatsapp::Account::PhoneNumbers).to receive(:get).and_return(phone_number_info)
  38. allow_any_instance_of(described_class).to receive(:initial_options).and_return(initial_options)
  39. end
  40. it 'adds a new channel' do
  41. expect { service.execute }.to change(Channel, :count).by(1)
  42. expect(Channel.last).to have_attributes(
  43. group_id: params[:group_id],
  44. options: {
  45. **params.except(:group_id).stringify_keys,
  46. **phone_number_info.stringify_keys,
  47. **initial_options.stringify_keys,
  48. },
  49. )
  50. end
  51. end
  52. context 'when phone number metadata cannot be retrieved' do
  53. before do
  54. allow_any_instance_of(Whatsapp::Account::PhoneNumbers).to receive(:get).and_return(nil)
  55. end
  56. it_behaves_like 'raising an error', StandardError, 'Could not fetch WhatsApp phone number details.'
  57. end
  58. end
  59. end