trigger_webhook_job_spec.rb 3.4 KB

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