Skip to content

Commit

Permalink
use respond_to? rather than defined?. It allow us to better test …
Browse files Browse the repository at this point in the history
…the corner case
  • Loading branch information
GustavoCaso committed Mar 10, 2023
1 parent 397a1f3 commit b11eaaa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/appsec/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 15 additions & 5 deletions spec/datadog/appsec/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b11eaaa

Please sign in to comment.