whatsapp_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. fill_in 'reminder_message', with: Faker::Lorem.unique.sentence
  33. click_on 'Submit'
  34. end
  35. in_modal do
  36. check_input_field_value('callback_url', callback_url)
  37. check_copy_to_clipboard_text('callback_url', callback_url)
  38. check_input_field_value('verify_token', verify_token)
  39. check_copy_to_clipboard_text('verify_token', verify_token)
  40. click_on 'Finish'
  41. end
  42. expect(page).to have_text(phone_number_name).and have_text(phone_number)
  43. end
  44. end
  45. context 'when editing an account' do
  46. let(:channel) do
  47. create(:whatsapp_channel,
  48. business_id: business_id,
  49. access_token: access_token,
  50. phone_number_id: phone_number_id,
  51. phone_number: phone_number,
  52. reminder_active: false,
  53. reminder_message: '',
  54. name: phone_number_name,
  55. app_secret: app_secret,
  56. callback_url_uuid: callback_url_uuid,
  57. verify_token: verify_token)
  58. end
  59. before do
  60. channel
  61. visit '#channels/whatsapp'
  62. end
  63. it 'updates an existing account' do
  64. find('div.btn', text: 'Edit').click
  65. in_modal do
  66. expect(page)
  67. .to have_field('business_id', with: channel.options[:business_id])
  68. .and have_field('access_token', with: channel.options[:access_token])
  69. .and have_field('app_secret', with: channel.options[:app_secret])
  70. click_on 'Next'
  71. end
  72. in_modal do
  73. check_select_field_value('phone_number_id', phone_number_id)
  74. fill_in 'welcome', with: Faker::Lorem.unique.sentence
  75. check_switch_field_value('reminder_active', false)
  76. expect(page).to have_no_field('reminder_message')
  77. click_on 'Submit'
  78. end
  79. in_modal do
  80. check_input_field_value('callback_url', callback_url)
  81. check_copy_to_clipboard_text('callback_url', callback_url)
  82. check_input_field_value('verify_token', verify_token)
  83. check_copy_to_clipboard_text('verify_token', verify_token)
  84. click_on 'Finish'
  85. end
  86. expect(page).to have_text(phone_number_name).and have_text(phone_number)
  87. end
  88. end
  89. def check_copy_to_clipboard_text(field_name, clipboard_text)
  90. find(".js-copy[data-target-field='#{field_name}']").click
  91. # Add a temporary text input element to the page, so we can paste the clipboard text into it and compare the value.
  92. # Programmatic clipboard management requires extra browser permissions and does not work in all of them.
  93. page.execute_script "$('<input name=\"clipboard_#{field_name}\" type=\"text\" class=\"form-control\">').insertAfter($('input[name=#{field_name}]'));"
  94. input_field = find("input[name='clipboard_#{field_name}']")
  95. .send_keys('')
  96. .click
  97. .send_keys([magic_key, 'v'])
  98. expect(input_field.value).to eq(clipboard_text)
  99. page.execute_script "$('input[name=\"clipboard_#{field_name}\"]').addClass('is-hidden');"
  100. end
  101. end