From 6f113615d7db7b154237b6e3c7898d4243d867a2 Mon Sep 17 00:00:00 2001 From: Markus Koller Date: Tue, 27 Jul 2021 22:34:43 +0200 Subject: [PATCH] Accept all 2xx HTTP responses when testing the Datadog integration The Datadog API sometimes responds with a HTTP 202 response status, which results in an error message when testing the integration settings even though the request shows up as successful in the delivery log. This changes the test to accept all 2xx HTTP status codes. Changelog: fixed --- app/models/integrations/datadog.rb | 14 +++++------- spec/models/integrations/datadog_spec.rb | 28 +++++++++++++++--------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/app/models/integrations/datadog.rb b/app/models/integrations/datadog.rb index 8a356cf8abc0aa..08cd451db94e47 100644 --- a/app/models/integrations/datadog.rb +++ b/app/models/integrations/datadog.rb @@ -129,14 +129,12 @@ def execute(data) end def test(data) - begin - result = execute(data) - return { success: false, result: result[:message] } if result[:http_status] != 200 - rescue StandardError => error - return { success: false, result: error } - end - - { success: true, result: result[:message] } + result = execute(data) + + { + success: (200..299).cover?(result[:http_status]), + result: result[:message] + } end private diff --git a/spec/models/integrations/datadog_spec.rb b/spec/models/integrations/datadog_spec.rb index 0920302e386575..677bd4c5e48a8c 100644 --- a/spec/models/integrations/datadog_spec.rb +++ b/spec/models/integrations/datadog_spec.rb @@ -140,21 +140,29 @@ end describe '#test' do - context 'when request is succesful' do - subject { saved_instance.test(pipeline_data) } + subject(:result) { saved_instance.test(pipeline_data) } - before do - stub_request(:post, expected_hook_url).to_return(body: 'OK') - end + let(:body) { 'OK' } + let(:status) { 200 } + + before do + stub_request(:post, expected_hook_url).to_return(body: body, status: status) + end + + context 'when request is successful with a HTTP 200 status' do it { is_expected.to eq({ success: true, result: 'OK' }) } end - context 'when request fails' do - subject { saved_instance.test(pipeline_data) } + context 'when request is successful with a HTTP 202 status' do + let(:status) { 202 } + + it { is_expected.to eq({ success: true, result: 'OK' }) } + end + + context 'when request fails with a HTTP 500 status' do + let(:status) { 500 } + let(:body) { 'CRASH!!!' } - before do - stub_request(:post, expected_hook_url).to_return(body: 'CRASH!!!', status: 500) - end it { is_expected.to eq({ success: false, result: 'CRASH!!!' }) } end end -- GitLab