webhook_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (C) 2012-2023 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 '#destroy' do
  47. subject(:webhook) { create(:webhook) }
  48. context 'when no dependencies' do
  49. it 'removes the object' do
  50. expect { webhook.destroy }.to change(webhook, :destroyed?).to true
  51. end
  52. end
  53. context 'when related object exists' do
  54. let!(:trigger) { create(:trigger, perform: { 'notification.webhook' => { 'webhook_id' => webhook.id.to_s } }) }
  55. it 'raises error with details' do
  56. expect { webhook.destroy }.to raise_error(Exceptions::UnprocessableEntity, %r{#{Regexp.escape("Trigger: #{trigger.name} (##{trigger.id})")}})
  57. end
  58. end
  59. end
  60. end