trigger_webhook_job_spec.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # Copyright (C) 2012-2024 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) do
  15. described_class.perform_later(
  16. trigger,
  17. ticket,
  18. article,
  19. changes: nil,
  20. user_id: nil,
  21. execution_type: nil,
  22. event_type: nil,
  23. )
  24. end
  25. let(:ticket) { create(:ticket) }
  26. let(:article) { create(:'ticket/article') }
  27. shared_examples 'handle deleted argument models' do
  28. it 'raises no error' do
  29. expect { ActiveJob::Base.execute job.serialize }.not_to raise_error
  30. end
  31. it "doesn't perform request" do
  32. allow(UserAgent).to receive(:post)
  33. ActiveJob::Base.execute job.serialize
  34. expect(UserAgent).not_to have_received(:post)
  35. end
  36. end
  37. context 'when Trigger gets deleted' do
  38. before { trigger.destroy! }
  39. include_examples 'handle deleted argument models'
  40. end
  41. context 'when Ticket gets deleted' do
  42. before { ticket.destroy! }
  43. include_examples 'handle deleted argument models'
  44. end
  45. context 'when Article gets deleted' do
  46. before { article.destroy! }
  47. include_examples 'handle deleted argument models'
  48. end
  49. end
  50. describe '#perform' do
  51. subject(:perform) do
  52. described_class.perform_now(
  53. trigger,
  54. ticket,
  55. article,
  56. changes: nil,
  57. user_id: nil,
  58. execution_type: nil,
  59. event_type: nil,
  60. )
  61. end
  62. let(:payload_ticket) { TriggerWebhookJob::RecordPayload.generate(ticket) }
  63. let(:payload_article) { TriggerWebhookJob::RecordPayload.generate(article) }
  64. let!(:ticket) { create(:ticket) }
  65. let!(:article) { create(:'ticket/article') }
  66. let(:response_status) { 200 }
  67. let(:payload) do
  68. {
  69. ticket: payload_ticket,
  70. article: payload_article,
  71. }
  72. end
  73. let(:headers) do
  74. {
  75. 'Content-Type' => 'application/json; charset=utf-8',
  76. 'User-Agent' => 'Zammad User Agent',
  77. 'X-Zammad-Trigger' => trigger.name,
  78. }
  79. end
  80. let(:response_body) do
  81. {}.to_json
  82. end
  83. before do
  84. stub_request(:post, endpoint).to_return(status: response_status, body: response_body)
  85. perform
  86. end
  87. context 'with trigger token configured' do
  88. it 'includes X-Hub-Signature header' do
  89. expect(WebMock).to have_requested(:post, endpoint)
  90. .with(body: payload, headers: headers)
  91. .with { |req| req.headers['X-Zammad-Delivery'].is_a?(String) }
  92. .with { |req| req.headers['X-Hub-Signature'].is_a?(String) }
  93. end
  94. end
  95. context 'without trigger token configured' do
  96. let(:token) { nil }
  97. it "doesn't include X-Hub-Signature header" do
  98. expect(WebMock).to have_requested(:post, endpoint)
  99. .with(body: payload, headers: headers)
  100. .with { |req| req.headers['X-Zammad-Delivery'].is_a?(String) }
  101. .with { |req| !req.headers.key?('X-Hub-Signature') }
  102. end
  103. end
  104. context 'with HTTP BasicAuth configured' do
  105. let(:webhook) { create(:webhook, endpoint: endpoint, basic_auth_username: 'user', basic_auth_password: 'passw0rd') }
  106. it 'generates a request with Authorization header' do
  107. expect(WebMock).to have_requested(:post, endpoint)
  108. .with(body: payload, headers: headers)
  109. .with { |req| req.headers['Authorization'] == "Basic #{Base64.strict_encode64('user:passw0rd')}" }
  110. end
  111. end
  112. context 'without HTTP BasicAuth configured' do
  113. let(:webhook) { create(:webhook, endpoint: endpoint) }
  114. it 'generates a request without Authorization header' do
  115. expect(WebMock).to have_requested(:post, endpoint)
  116. .with(body: payload, headers: headers)
  117. .with { |req| !req.headers.key?('Authorization') }
  118. end
  119. end
  120. context 'when response is not JSON' do
  121. let(:response_body) { 'Thanks!' }
  122. it 'succeeds anyway' do
  123. expect(described_class).not_to have_been_enqueued
  124. end
  125. end
  126. context "when request doesn't succeed" do
  127. let(:response_status) { 404 }
  128. it 'enqueues job again' do
  129. expect(described_class).to have_been_enqueued
  130. end
  131. end
  132. context 'with different payloads' do
  133. subject(:perform) do
  134. described_class.perform_now(
  135. trigger,
  136. ticket,
  137. article,
  138. changes: nil,
  139. user_id: nil,
  140. execution_type: 'trigger',
  141. event_type: 'info',
  142. )
  143. end
  144. let(:webhook) { create(:webhook, endpoint: endpoint, customized_payload: customized_payload, custom_payload: custom_payload, pre_defined_webhook_type: pre_defined_webhook_type) }
  145. let(:customized_payload) { false }
  146. let(:custom_payload) { nil }
  147. let(:pre_defined_webhook_type) { nil }
  148. def pre_defined_webhook_payload
  149. tracks = { ticket: ticket, article: article }
  150. data = {
  151. event: {
  152. type: 'info',
  153. execution: 'trigger',
  154. changes: nil,
  155. user_id: nil,
  156. },
  157. webhook: webhook
  158. }
  159. TriggerWebhookJob::CustomPayload.tracks.select { |t| t.respond_to?(:generate) }.each do |klass|
  160. klass.generate(tracks, data)
  161. end
  162. predefined_payload = TriggerWebhookJob::CustomPayload::Track::PreDefinedWebhook.payload('Mattermost')
  163. TriggerWebhookJob::CustomPayload.generate(predefined_payload, tracks)
  164. end
  165. shared_examples 'including correct payload' do
  166. it 'includes correct payload' do
  167. expect(WebMock).to have_requested(:post, endpoint)
  168. .with(body: payload, headers: headers)
  169. end
  170. end
  171. context 'with non-customized payload' do
  172. it_behaves_like 'including correct payload'
  173. context 'with pre-defined webhook' do
  174. let(:webhook) { create(:mattermost_webhook, endpoint: endpoint) }
  175. let(:payload) { pre_defined_webhook_payload }
  176. it_behaves_like 'including correct payload'
  177. end
  178. end
  179. context 'with customized payload' do
  180. let(:customized_payload) { true }
  181. let(:custom_payload) { '{"ticket":"\#{ticket.title}"}' }
  182. let(:payload) do
  183. {
  184. ticket: ticket.title,
  185. }
  186. end
  187. it_behaves_like 'including correct payload'
  188. context 'with pre-defined webhook' do
  189. let(:webhook) { create(:mattermost_webhook, endpoint: endpoint, customized_payload:, custom_payload:) }
  190. it_behaves_like 'including correct payload'
  191. end
  192. context 'with empty custom payload' do
  193. let(:custom_payload) { nil }
  194. let(:payload) do
  195. {
  196. ticket: payload_ticket,
  197. article: payload_article,
  198. }
  199. end
  200. it_behaves_like 'including correct payload'
  201. context 'with pre-defined webhook' do
  202. let(:webhook) { create(:mattermost_webhook, endpoint: endpoint) }
  203. let(:payload) { pre_defined_webhook_payload }
  204. it_behaves_like 'including correct payload'
  205. end
  206. end
  207. end
  208. end
  209. end
  210. end