webhook_spec.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. require 'rails_helper'
  2. require 'models/concerns/has_xss_sanitized_note_examples'
  3. RSpec.describe Webhook, type: :model do
  4. it_behaves_like 'HasXssSanitizedNote', model_factory: :webhook
  5. describe 'check endpoint' do
  6. subject(:webhook) { build(:webhook, endpoint: endpoint) }
  7. before { webhook.valid? }
  8. let(:endpoint_errors) { webhook.errors.messages[:endpoint] }
  9. context 'with missing http type' do
  10. let(:endpoint) { 'example.com' }
  11. it { is_expected.not_to be_valid }
  12. it 'has an error' do
  13. expect(endpoint_errors).to include 'Invalid endpoint (no http/https)!'
  14. end
  15. end
  16. context 'with spaces in invalid hostname' do
  17. let(:endpoint) { 'http:// example.com' }
  18. it { is_expected.not_to be_valid }
  19. it 'has an error' do
  20. expect(endpoint_errors).to include 'Invalid endpoint!'
  21. end
  22. end
  23. context 'with ? in hostname' do
  24. let(:endpoint) { 'http://?example.com' }
  25. it { is_expected.not_to be_valid }
  26. it 'has an error' do
  27. expect(endpoint_errors).to include 'Invalid endpoint (no hostname)!'
  28. end
  29. end
  30. context 'with nil in endpoint' do
  31. let(:endpoint) { nil }
  32. it { is_expected.not_to be_valid }
  33. it 'has an error' do
  34. expect(endpoint_errors).to include 'Invalid endpoint!'
  35. end
  36. end
  37. context 'with a valid endpoint' do
  38. let(:endpoint) { 'https://example.com/endpoint' }
  39. it { is_expected.to be_valid }
  40. it 'has no errors' do
  41. expect(endpoint_errors).to be_empty
  42. end
  43. end
  44. end
  45. describe '#destroy' do
  46. subject(:webhook) { create(:webhook) }
  47. context 'when no dependencies' do
  48. it 'removes the object' do
  49. expect { webhook.destroy }.to change(webhook, :destroyed?).to true
  50. end
  51. end
  52. context 'when related object exists' do
  53. let!(:trigger) { create(:trigger, perform: { 'notification.webhook' => { 'webhook_id' => webhook.id.to_s } }) }
  54. it 'raises error with details' do
  55. expect { webhook.destroy }.to raise_error(Exceptions::UnprocessableEntity, %r{#{Regexp.escape("Trigger: #{trigger.name} (##{trigger.id})")}})
  56. end
  57. end
  58. end
  59. end