whatsapp_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Manage > Channels > WhatsApp', :use_vcr, required_envs: %w[WHATSAPP_ACCESS_TOKEN WHATSAPP_APP_SECRET WHATSAPP_BUSINESS_ID WHATSAPP_PHONE_NUMBER WHATSAPP_PHONE_NUMBER_ID WHATSAPP_PHONE_NUMBER_NAME], type: :system do
  4. let(:phone_number_id) { ENV['WHATSAPP_PHONE_NUMBER_ID'] }
  5. let(:phone_number) { ENV['WHATSAPP_PHONE_NUMBER'] }
  6. let(:phone_number_name) { ENV['WHATSAPP_PHONE_NUMBER_NAME'] }
  7. let(:business_id) { ENV['WHATSAPP_BUSINESS_ID'] }
  8. let(:access_token) { ENV['WHATSAPP_ACCESS_TOKEN'] }
  9. let(:app_secret) { ENV['WHATSAPP_APP_SECRET'] }
  10. let(:callback_url_uuid) { SecureRandom.uuid }
  11. let(:verify_token) { SecureRandom.urlsafe_base64(12) }
  12. let(:callback_url) { "#{Setting.get('http_type')}://#{Setting.get('fqdn')}#{Rails.configuration.api_path}/channels_whatsapp_webhook/#{callback_url_uuid}" }
  13. context 'when adding an account' do
  14. before do
  15. allow_any_instance_of(Service::Channel::Whatsapp::Create)
  16. .to receive(:initial_options)
  17. .and_return({ adapter: 'whatsapp', callback_url_uuid:, verify_token: })
  18. visit '#channels/whatsapp'
  19. end
  20. it 'creates a new account' do
  21. click_on 'Add Account'
  22. in_modal do
  23. fill_in 'business_id', with: business_id
  24. fill_in 'access_token', with: access_token
  25. fill_in 'app_secret', with: app_secret
  26. click_on 'Next'
  27. end
  28. in_modal do
  29. check_select_field_value('phone_number_id', phone_number_id)
  30. fill_in 'welcome', with: Faker::Lorem.unique.sentence
  31. check_switch_field_value('reminder_active', true)
  32. click_on 'Submit'
  33. end
  34. in_modal do
  35. check_input_field_value('callback_url', callback_url)
  36. check_copy_to_clipboard_text('callback_url', callback_url)
  37. check_input_field_value('verify_token', verify_token)
  38. check_copy_to_clipboard_text('verify_token', verify_token)
  39. click_on 'Finish'
  40. end
  41. expect(page).to have_text(phone_number_name).and have_text(phone_number)
  42. end
  43. end
  44. context 'when editing an account' do
  45. let(:channel) do
  46. create(:whatsapp_channel,
  47. business_id: business_id,
  48. access_token: access_token,
  49. phone_number_id: phone_number_id,
  50. phone_number: phone_number,
  51. reminder_active: false,
  52. name: phone_number_name,
  53. app_secret: app_secret,
  54. callback_url_uuid: callback_url_uuid,
  55. verify_token: verify_token)
  56. end
  57. before do
  58. channel
  59. visit '#channels/whatsapp'
  60. end
  61. it 'updates an existing account' do
  62. find('div.btn', text: 'Edit').click
  63. in_modal do
  64. expect(page)
  65. .to have_field('business_id', with: channel.options[:business_id])
  66. .and have_field('access_token', with: channel.options[:access_token])
  67. .and have_field('app_secret', with: channel.options[:app_secret])
  68. click_on 'Next'
  69. end
  70. in_modal do
  71. check_select_field_value('phone_number_id', phone_number_id)
  72. fill_in 'welcome', with: Faker::Lorem.unique.sentence
  73. check_switch_field_value('reminder_active', false)
  74. click_on 'Submit'
  75. end
  76. in_modal do
  77. check_input_field_value('callback_url', callback_url)
  78. check_copy_to_clipboard_text('callback_url', callback_url)
  79. check_input_field_value('verify_token', verify_token)
  80. check_copy_to_clipboard_text('verify_token', verify_token)
  81. click_on 'Finish'
  82. end
  83. expect(page).to have_text(phone_number_name).and have_text(phone_number)
  84. end
  85. end
  86. def check_copy_to_clipboard_text(field_name, clipboard_text)
  87. find(".js-copy[data-target-field='#{field_name}']").click
  88. # Add a temporary text input element to the page, so we can paste the clipboard text into it and compare the value.
  89. # Programmatic clipboard management requires extra browser permissions and does not work in all of them.
  90. page.execute_script "$('<input name=\"clipboard_#{field_name}\" type=\"text\" class=\"form-control\">').insertAfter($('input[name=#{field_name}]'));"
  91. input_field = find("input[name='clipboard_#{field_name}']")
  92. .send_keys('')
  93. .click
  94. .send_keys([magic_key, 'v'])
  95. expect(input_field.value).to eq(clipboard_text)
  96. page.execute_script "$('input[name=\"clipboard_#{field_name}\"]').addClass('is-hidden');"
  97. end
  98. end