configuration_spec.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Webhook::Configuration do
  4. describe '.verify!' do
  5. let(:channel) do
  6. options = {
  7. secret: Faker::Crypto.sha256,
  8. verify_token: Faker::Crypto.sha256,
  9. callback_url_uuid: Faker::Number.unique.number(digits: 15),
  10. }
  11. create(:channel, area: 'WhatsApp::Business', options: options)
  12. end
  13. let(:options) do
  14. {
  15. callback_url_uuid: channel.options['callback_url_uuid'],
  16. 'hub.mode': 'subscribe',
  17. 'hub.challenge': Faker::Number.unique.number(digits: 10),
  18. 'hub.verify_token': channel.options['verify_token'],
  19. }
  20. end
  21. context 'when channel not exists' do
  22. it 'raises NoChannelError' do
  23. options[:callback_url_uuid] = 0
  24. expect { described_class.new(options:).verify! }.to raise_error(Whatsapp::Webhook::NoChannelError)
  25. end
  26. end
  27. context 'when no WhatsApp channel is referenced' do
  28. it 'raises NoChannelError' do
  29. options.delete(:callback_url_uuid)
  30. expect { described_class.new(options:).verify! }.to raise_error(Whatsapp::Webhook::NoChannelError)
  31. end
  32. end
  33. context 'when existing channel is using wrong area' do
  34. it 'raises NoChannelError' do
  35. channel.update!(area: 'foobar')
  36. expect { described_class.new(options:).verify! }.to raise_error(Whatsapp::Webhook::NoChannelError)
  37. end
  38. end
  39. context 'when hub.mode is not subscribe' do
  40. it 'raises VerificationError' do
  41. options[:'hub.mode'] = 'unsubscribe'
  42. expect { described_class.new(options:).verify! }.to raise_error(described_class::VerificationError)
  43. end
  44. end
  45. context 'when hub.challenge is not a number' do
  46. it 'raises VerificationError' do
  47. options[:'hub.challenge'] = 'foobar'
  48. expect { described_class.new(options:).verify! }.to raise_error(described_class::VerificationError)
  49. end
  50. end
  51. context 'when hub.verify_token is not valid' do
  52. it 'raises VerificationError' do
  53. options[:'hub.verify_token'] = 'foobar'
  54. expect { described_class.new(options:).verify! }.to raise_error(described_class::VerificationError)
  55. end
  56. end
  57. context 'when all options are valid' do
  58. it 'returns hub.challenge' do
  59. expect(described_class.new(options:).verify!).to eq(options[:'hub.challenge'])
  60. end
  61. end
  62. end
  63. end