trigger_webhook_job_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. require 'rails_helper'
  2. RSpec.describe TriggerWebhookJob, type: :job do
  3. let(:endpoint) { 'http://api.example.com/webhook' }
  4. let(:token) { 's3cr3t-t0k3n' }
  5. let(:webhook) { create(:webhook, endpoint: endpoint, signature_token: token) }
  6. let(:trigger) do
  7. create(:trigger,
  8. perform: {
  9. 'notification.webhook' => { 'webhook_id' => webhook.id }
  10. })
  11. end
  12. context 'when serialized model argument gets deleted' do
  13. subject!(:job) { described_class.perform_later(trigger, ticket, article) }
  14. let(:ticket) { create(:ticket) }
  15. let(:article) { create(:'ticket/article') }
  16. shared_examples 'handle deleted argument models' do
  17. it 'raises no error' do
  18. expect { ActiveJob::Base.execute job.serialize }.not_to raise_error
  19. end
  20. it "doesn't perform request" do
  21. allow(UserAgent).to receive(:post)
  22. ActiveJob::Base.execute job.serialize
  23. expect(UserAgent).not_to have_received(:post)
  24. end
  25. end
  26. context 'when Trigger gets deleted' do
  27. before { trigger.destroy! }
  28. include_examples 'handle deleted argument models'
  29. end
  30. context 'when Ticket gets deleted' do
  31. before { ticket.destroy! }
  32. include_examples 'handle deleted argument models'
  33. end
  34. context 'when Article gets deleted' do
  35. before { article.destroy! }
  36. include_examples 'handle deleted argument models'
  37. end
  38. end
  39. describe '#perform' do
  40. subject(:perform) { described_class.perform_now(trigger, ticket, article) }
  41. let(:payload_ticket) { TriggerWebhookJob::RecordPayload.generate(ticket) }
  42. let(:payload_article) { TriggerWebhookJob::RecordPayload.generate(article) }
  43. let!(:ticket) { create(:ticket) }
  44. let!(:article) { create(:'ticket/article') }
  45. let(:response_status) { 200 }
  46. let(:payload) do
  47. {
  48. ticket: payload_ticket,
  49. article: payload_article,
  50. }
  51. end
  52. let(:headers) do
  53. {
  54. 'Content-Type' => 'application/json',
  55. 'User-Agent' => 'Zammad User Agent',
  56. 'X-Zammad-Trigger' => trigger.name,
  57. }
  58. end
  59. let(:response_body) do
  60. {}.to_json
  61. end
  62. before do
  63. stub_request(:post, endpoint).to_return(status: response_status, body: response_body)
  64. perform
  65. end
  66. context 'with trigger token configured' do
  67. it 'includes X-Hub-Signature header' do
  68. expect(WebMock).to have_requested(:post, endpoint)
  69. .with( body: payload, headers: headers )
  70. .with { |req| req.headers['X-Zammad-Delivery'].is_a?(String) }
  71. .with { |req| req.headers['X-Hub-Signature'].is_a?(String) }
  72. end
  73. end
  74. context 'without trigger token configured' do
  75. let(:token) { nil }
  76. it "doesn't include X-Hub-Signature header" do
  77. expect(WebMock).to have_requested(:post, endpoint)
  78. .with( body: payload, headers: headers )
  79. .with { |req| req.headers['X-Zammad-Delivery'].is_a?(String) }
  80. .with { |req| !req.headers.key?('X-Hub-Signature') }
  81. end
  82. end
  83. context 'when response is not JSON' do
  84. let(:response_body) { 'Thanks!' }
  85. it 'succeeds anyway' do
  86. expect(described_class).not_to have_been_enqueued
  87. end
  88. end
  89. context "when request doesn't succeed" do
  90. let(:response_status) { 404 }
  91. it 'enqueues job again' do
  92. expect(described_class).to have_been_enqueued
  93. end
  94. end
  95. end
  96. end