whatsapp_spec.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'requests/channel_admin/base_examples'
  4. RSpec.describe 'WhatsApp channel admin API endpoints', aggregate_failures: true, authenticated_as: :user, type: :request do
  5. let(:user) { create(:admin) }
  6. it_behaves_like 'base channel management', factory: :whatsapp_channel, path: :whatsapp
  7. describe 'POST /api/v1/channels_admin/whatsapp' do
  8. it 'creates a channel' do
  9. params = attributes_for(:whatsapp_channel)[:options]
  10. allow_any_instance_of(Service::Channel::Whatsapp::Create).to receive(:execute)
  11. allow(Service::Channel::Whatsapp::Create).to receive(:new).and_call_original
  12. post '/api/v1/channels/admin/whatsapp', params: params
  13. expect(response).to have_http_status(:ok)
  14. expect(Service::Channel::Whatsapp::Create).to have_received(:new)
  15. end
  16. end
  17. describe 'PUT /api/v1/channels_admin/whatsapp/ID' do
  18. let(:channel) { create(:whatsapp_channel) }
  19. it 'updates a channel' do
  20. params = attributes_for(:whatsapp_channel)[:options]
  21. allow_any_instance_of(Service::Channel::Whatsapp::Update).to receive(:execute)
  22. allow(Service::Channel::Whatsapp::Update).to receive(:new).and_call_original
  23. put "/api/v1/channels/admin/whatsapp/#{channel.id}", params: params
  24. expect(response).to have_http_status(:ok)
  25. expect(Service::Channel::Whatsapp::Update).to have_received(:new)
  26. end
  27. end
  28. describe 'POST /api/v1/channels_admin/whatsapp/preload' do
  29. it 'returns phone numbers to show in a form' do
  30. params = { business_id: '123', access_token: 'token' }
  31. output = {
  32. 'phone_numbers' => [
  33. { 'name' => 'phone', 'value' => 123 }
  34. ]
  35. }
  36. allow_any_instance_of(Service::Channel::Whatsapp::Preload)
  37. .to receive(:execute)
  38. .and_return(output)
  39. allow(Service::Channel::Whatsapp::Preload).to receive(:new).and_call_original
  40. post '/api/v1/channels/admin/whatsapp/preload', params: params
  41. expect(response).to have_http_status(:ok)
  42. expect(json_response).to include('data' => output)
  43. expect(Service::Channel::Whatsapp::Preload).to have_received(:new).with(**params)
  44. end
  45. end
  46. end