webhook_spec.rb 2.0 KB

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