webhook_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/concerns/has_xss_sanitized_note_examples'
  4. RSpec.describe Webhook, type: :model do
  5. it_behaves_like 'HasXssSanitizedNote', model_factory: :webhook
  6. describe 'check endpoint' do
  7. subject(:webhook) { build(:webhook, endpoint: endpoint) }
  8. before { webhook.valid? }
  9. let(:endpoint_errors) { webhook.errors.messages[:endpoint] }
  10. context 'with missing http type' do
  11. let(:endpoint) { 'example.com' }
  12. it { is_expected.not_to be_valid }
  13. it 'has an error' do
  14. expect(endpoint_errors).to include 'The provided endpoint is invalid, no http or https protocol was specified.'
  15. end
  16. end
  17. context 'with spaces in invalid hostname' do
  18. let(:endpoint) { 'http:// example.com' }
  19. it { is_expected.not_to be_valid }
  20. it 'has an error' do
  21. expect(endpoint_errors).to include 'The provided endpoint is invalid.'
  22. end
  23. end
  24. context 'with ? in hostname' do
  25. let(:endpoint) { 'http://?example.com' }
  26. it { is_expected.not_to be_valid }
  27. it 'has an error' do
  28. expect(endpoint_errors).to include 'The provided endpoint is invalid, no hostname was specified.'
  29. end
  30. end
  31. context 'with nil in endpoint' do
  32. let(:endpoint) { nil }
  33. it { is_expected.not_to be_valid }
  34. it 'has an error' do
  35. expect(endpoint_errors).to include 'The provided endpoint is invalid.'
  36. end
  37. end
  38. context 'with a valid endpoint' do
  39. let(:endpoint) { 'https://example.com/endpoint' }
  40. it { is_expected.to be_valid }
  41. it 'has no errors' do
  42. expect(endpoint_errors).to be_empty
  43. end
  44. end
  45. end
  46. describe 'check custom payload' do
  47. subject(:webhook) { build(:webhook, custom_payload: custom_payload) }
  48. before { webhook.valid? }
  49. let(:custom_payload_errors) { webhook.errors.messages[:custom_payload] }
  50. context 'with valid JSON' do
  51. let(:custom_payload) { '{"foo": "bar"}' }
  52. it { is_expected.to be_valid }
  53. it 'has no errors' do
  54. expect(custom_payload_errors).to be_empty
  55. end
  56. end
  57. context 'with invalid JSON' do
  58. let(:custom_payload) { '{"foo": bar}' }
  59. it { is_expected.not_to be_valid }
  60. it 'has an error' do
  61. expect(custom_payload_errors).to include 'The provided payload is invalid. Please check your syntax.'
  62. end
  63. end
  64. end
  65. describe 'reset custom payload' do
  66. subject(:webhook) { create(:webhook, customized_payload: customized_payload, custom_payload: custom_payload) }
  67. context 'with customized payload' do
  68. let(:customized_payload) { true }
  69. let(:custom_payload) { '{"foo": "bar"}' }
  70. it 'saves custom payload' do
  71. expect(webhook).to have_attributes(
  72. customized_payload: customized_payload,
  73. custom_payload: custom_payload,
  74. )
  75. end
  76. end
  77. context 'without customized payload' do
  78. let(:customized_payload) { false }
  79. let(:custom_payload) { '{"foo": "bar"}' }
  80. it 'resets custom payload' do
  81. expect(webhook).to have_attributes(
  82. customized_payload: customized_payload,
  83. custom_payload: nil,
  84. )
  85. end
  86. end
  87. end
  88. describe 'check preferences' do
  89. subject(:webhook) { build(:webhook, preferences: preferences) }
  90. let(:preferences) { { pre_defined: { class_name: 'Webhook::PreDefined::Example' } } }
  91. it 'has preferences' do
  92. expect(webhook.preferences).to include({ 'pre_defined' => { 'class_name' => 'Webhook::PreDefined::Example' } })
  93. end
  94. end
  95. describe '#destroy' do
  96. subject(:webhook) { create(:webhook) }
  97. context 'when no dependencies' do
  98. it 'removes the object' do
  99. expect { webhook.destroy }.to change(webhook, :destroyed?).to true
  100. end
  101. end
  102. context 'when related object exists' do
  103. let!(:trigger) { create(:trigger, perform: { 'notification.webhook' => { 'webhook_id' => webhook.id.to_s } }) }
  104. it 'raises error with details' do
  105. expect { webhook.destroy }.to raise_error(Exceptions::UnprocessableEntity, %r{#{Regexp.escape("Trigger: #{trigger.name} (##{trigger.id})")}})
  106. end
  107. end
  108. end
  109. end