From b11eaaadf304cfd6b11d658d646ffd592347ee47 Mon Sep 17 00:00:00 2001 From: Gustavo Caso Date: Fri, 10 Mar 2023 08:23:38 +0100 Subject: [PATCH] use `respond_to?` rather than `defined?`. It allow us to better test the corner case --- lib/datadog/appsec/component.rb | 2 +- spec/datadog/appsec/component_spec.rb | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/datadog/appsec/component.rb b/lib/datadog/appsec/component.rb index fb2ca6960e..b8cd4705ba 100644 --- a/lib/datadog/appsec/component.rb +++ b/lib/datadog/appsec/component.rb @@ -8,7 +8,7 @@ module AppSec class Component class << self def build_appsec_component(settings) - return unless defined?(Datadog::AppSec::Configuration) && settings.appsec.enabled + return unless settings.respond_to?(:appsec) && settings.appsec.enabled processor = create_processor new(processor: processor) diff --git a/spec/datadog/appsec/component_spec.rb b/spec/datadog/appsec/component_spec.rb index 5b2b6cd6ed..397321b336 100644 --- a/spec/datadog/appsec/component_spec.rb +++ b/spec/datadog/appsec/component_spec.rb @@ -3,8 +3,11 @@ RSpec.describe Datadog::AppSec::Component do describe '.build_appsec_component' do - let(:settings) do + let(:seetings_without_appsec) { double(Datadog::Core::Configuration) } + + let(:settings_with_appsec) do double( + Datadog::Core::Configuration, appsec: Datadog::AppSec::Configuration::Settings.new.merge( Datadog::AppSec::Configuration::DSL.new.tap do |appsec| appsec.enabled = appsec_enabled @@ -16,14 +19,14 @@ context 'when appsec is enabled' do let(:appsec_enabled) { true } it 'returns a Datadog::AppSec::Component instance' do - component = described_class.build_appsec_component(settings) + component = described_class.build_appsec_component(settings_with_appsec) expect(component).to be_a(described_class) end context 'when processor is ready' do it 'returns a Datadog::AppSec::Component with a processor instance' do expect_any_instance_of(Datadog::AppSec::Processor).to receive(:ready?).and_return(true) - component = described_class.build_appsec_component(settings) + component = described_class.build_appsec_component(settings_with_appsec) expect(component.processor).to be_a(Datadog::AppSec::Processor) end @@ -32,7 +35,7 @@ context 'when processor fail to instanciate' do it 'returns a Datadog::AppSec::Component with a nil processor' do expect_any_instance_of(Datadog::AppSec::Processor).to receive(:ready?).and_return(false) - component = described_class.build_appsec_component(settings) + component = described_class.build_appsec_component(settings_with_appsec) expect(component.processor).to be_nil end @@ -43,7 +46,14 @@ let(:appsec_enabled) { false } it 'returns nil' do - component = described_class.build_appsec_component(settings) + component = described_class.build_appsec_component(settings_with_appsec) + expect(component).to be_nil + end + end + + context 'when appsec is not active' do + it 'returns nil' do + component = described_class.build_appsec_component(seetings_without_appsec) expect(component).to be_nil end end