diff --git a/spec/datadog/appsec/contrib/rack/integration_test_spec.rb b/spec/datadog/appsec/contrib/rack/integration_test_spec.rb index 5aa093b93e4..1bdb74d3f2f 100644 --- a/spec/datadog/appsec/contrib/rack/integration_test_spec.rb +++ b/spec/datadog/appsec/contrib/rack/integration_test_spec.rb @@ -1,4 +1,5 @@ require 'datadog/tracing/contrib/support/spec_helper' +require 'datadog/appsec/contrib/support/integration/shared_examples' require 'rack/test' require 'securerandom' @@ -180,140 +181,6 @@ span end - shared_examples 'a GET 200 span' do - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a GET 403 span' do - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('403') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a GET 404 span' do - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('404') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('404') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a POST 200 span' do - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a POST 403 span' do - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('403') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a trace without AppSec tags' do - it do - expect(service_span.send(:metrics)['_dd.appsec.enabled']).to be_nil - expect(service_span.send(:meta)['_dd.runtime_family']).to be_nil - expect(service_span.send(:meta)['_dd.appsec.waf.version']).to be_nil - expect(span.send(:meta)['http.client_ip']).to eq nil - end - end - - shared_examples 'a trace with AppSec tags' do - it do - expect(service_span.send(:metrics)['_dd.appsec.enabled']).to eq(1.0) - expect(service_span.send(:meta)['_dd.runtime_family']).to eq('ruby') - expect(service_span.send(:meta)['_dd.appsec.waf.version']).to match(/^\d+\.\d+\.\d+/) - expect(span.send(:meta)['http.client_ip']).to eq client_ip - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it_behaves_like 'a trace without AppSec tags' - end - end - - shared_examples 'a trace without AppSec events' do - it do - expect(spans.select { |s| s.get_tag('appsec.event') }).to be_empty - expect(service_span.send(:meta)['_dd.appsec.triggers']).to be_nil - end - end - - shared_examples 'a trace with AppSec events' do - it do - expect(spans.select { |s| s.get_tag('appsec.event') }).to_not be_empty - expect(service_span.send(:meta)['_dd.appsec.json']).to be_a String - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it_behaves_like 'a trace without AppSec events' - end - end - context 'with a basic route' do let(:routes) do proc do diff --git a/spec/datadog/appsec/contrib/rails/integration_test_spec.rb b/spec/datadog/appsec/contrib/rails/integration_test_spec.rb index 6e3c51916d8..ba4c183af10 100644 --- a/spec/datadog/appsec/contrib/rails/integration_test_spec.rb +++ b/spec/datadog/appsec/contrib/rails/integration_test_spec.rb @@ -1,4 +1,5 @@ require 'datadog/tracing/contrib/rails/rails_helper' +require 'datadog/appsec/contrib/support/integration/shared_examples' require 'rack/test' require 'datadog/tracing' @@ -153,140 +154,6 @@ def set_user let(:span) { rack_span } - shared_examples 'a GET 200 span' do - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a GET 403 span' do - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('403') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a GET 404 span' do - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('404') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('404') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a POST 200 span' do - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a POST 403 span' do - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('403') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a trace without AppSec tags' do - it do - expect(service_span.send(:metrics)['_dd.appsec.enabled']).to be_nil - expect(service_span.send(:meta)['_dd.runtime_family']).to be_nil - expect(service_span.send(:meta)['_dd.appsec.waf.version']).to be_nil - expect(span.send(:meta)['http.client_ip']).to eq nil - end - end - - shared_examples 'a trace with AppSec tags' do - it do - expect(service_span.send(:metrics)['_dd.appsec.enabled']).to eq(1.0) - expect(service_span.send(:meta)['_dd.runtime_family']).to eq('ruby') - expect(service_span.send(:meta)['_dd.appsec.waf.version']).to match(/^\d+\.\d+\.\d+/) - expect(span.send(:meta)['http.client_ip']).to eq client_ip - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it_behaves_like 'a trace without AppSec tags' - end - end - - shared_examples 'a trace without AppSec events' do - it do - expect(spans.select { |s| s.get_tag('appsec.event') }).to be_empty - expect(service_span.send(:meta)['_dd.appsec.triggers']).to be_nil - end - end - - shared_examples 'a trace with AppSec events' do - it do - expect(spans.select { |s| s.get_tag('appsec.event') }).to_not be_empty - expect(service_span.send(:meta)['_dd.appsec.json']).to be_a String - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it_behaves_like 'a trace without AppSec events' - end - end - context 'with a basic route' do let(:routes) do { diff --git a/spec/datadog/appsec/contrib/sinatra/integration_test_spec.rb b/spec/datadog/appsec/contrib/sinatra/integration_test_spec.rb index 5cc8f1eaad2..38f33ba2d1b 100644 --- a/spec/datadog/appsec/contrib/sinatra/integration_test_spec.rb +++ b/spec/datadog/appsec/contrib/sinatra/integration_test_spec.rb @@ -1,4 +1,5 @@ require 'datadog/tracing/contrib/support/spec_helper' +require 'datadog/appsec/contrib/support/integration/shared_examples' require 'rack/test' require 'securerandom' @@ -148,140 +149,6 @@ let(:span) { rack_span } - shared_examples 'a GET 200 span' do - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a GET 403 span' do - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('403') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a GET 404 span' do - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('404') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('GET') - expect(span.get_tag('http.status_code')).to eq('404') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a POST 200 span' do - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a POST 403 span' do - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('403') - expect(span.status).to eq(0) - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it do - expect(span.get_tag('http.method')).to eq('POST') - expect(span.get_tag('http.status_code')).to eq('200') - expect(span.status).to eq(0) - end - end - end - - shared_examples 'a trace without AppSec tags' do - it do - expect(service_span.send(:metrics)['_dd.appsec.enabled']).to be_nil - expect(service_span.send(:meta)['_dd.runtime_family']).to be_nil - expect(service_span.send(:meta)['_dd.appsec.waf.version']).to be_nil - expect(span.send(:meta)['http.client_ip']).to eq nil - end - end - - shared_examples 'a trace with AppSec tags' do - it do - expect(service_span.send(:metrics)['_dd.appsec.enabled']).to eq(1.0) - expect(service_span.send(:meta)['_dd.runtime_family']).to eq('ruby') - expect(service_span.send(:meta)['_dd.appsec.waf.version']).to match(/^\d+\.\d+\.\d+/) - expect(span.send(:meta)['http.client_ip']).to eq client_ip - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it_behaves_like 'a trace without AppSec tags' - end - end - - shared_examples 'a trace without AppSec events' do - it do - expect(spans.select { |s| s.get_tag('appsec.event') }).to be_empty - expect(service_span.send(:meta)['_dd.appsec.triggers']).to be_nil - end - end - - shared_examples 'a trace with AppSec events' do - it do - expect(spans.select { |s| s.get_tag('appsec.event') }).to_not be_empty - expect(service_span.send(:meta)['_dd.appsec.json']).to be_a String - end - - context 'with appsec disabled' do - let(:appsec_enabled) { false } - - it_behaves_like 'a trace without AppSec events' - end - end - context 'with a basic route' do let(:routes) do lambda do diff --git a/spec/datadog/appsec/contrib/support/integration/shared_examples.rb b/spec/datadog/appsec/contrib/support/integration/shared_examples.rb new file mode 100644 index 00000000000..6a90c01cdd4 --- /dev/null +++ b/spec/datadog/appsec/contrib/support/integration/shared_examples.rb @@ -0,0 +1,133 @@ +RSpec.shared_examples 'a GET 200 span' do + it do + expect(span.get_tag('http.method')).to eq('GET') + expect(span.get_tag('http.status_code')).to eq('200') + expect(span.status).to eq(0) + end + + context 'with appsec disabled' do + let(:appsec_enabled) { false } + + it do + expect(span.get_tag('http.method')).to eq('GET') + expect(span.get_tag('http.status_code')).to eq('200') + expect(span.status).to eq(0) + end + end +end + +RSpec.shared_examples 'a GET 403 span' do + it do + expect(span.get_tag('http.method')).to eq('GET') + expect(span.get_tag('http.status_code')).to eq('403') + expect(span.status).to eq(0) + end + + context 'with appsec disabled' do + let(:appsec_enabled) { false } + + it do + expect(span.get_tag('http.method')).to eq('GET') + expect(span.get_tag('http.status_code')).to eq('200') + expect(span.status).to eq(0) + end + end +end + +RSpec.shared_examples 'a GET 404 span' do + it do + expect(span.get_tag('http.method')).to eq('GET') + expect(span.get_tag('http.status_code')).to eq('404') + expect(span.status).to eq(0) + end + + context 'with appsec disabled' do + let(:appsec_enabled) { false } + + it do + expect(span.get_tag('http.method')).to eq('GET') + expect(span.get_tag('http.status_code')).to eq('404') + expect(span.status).to eq(0) + end + end +end + +RSpec.shared_examples 'a POST 200 span' do + it do + expect(span.get_tag('http.method')).to eq('POST') + expect(span.get_tag('http.status_code')).to eq('200') + expect(span.status).to eq(0) + end + + context 'with appsec disabled' do + let(:appsec_enabled) { false } + + it do + expect(span.get_tag('http.method')).to eq('POST') + expect(span.get_tag('http.status_code')).to eq('200') + expect(span.status).to eq(0) + end + end +end + +RSpec.shared_examples 'a POST 403 span' do + it do + expect(span.get_tag('http.method')).to eq('POST') + expect(span.get_tag('http.status_code')).to eq('403') + expect(span.status).to eq(0) + end + + context 'with appsec disabled' do + let(:appsec_enabled) { false } + + it do + expect(span.get_tag('http.method')).to eq('POST') + expect(span.get_tag('http.status_code')).to eq('200') + expect(span.status).to eq(0) + end + end +end + +RSpec.shared_examples 'a trace without AppSec tags' do + it do + expect(service_span.send(:metrics)['_dd.appsec.enabled']).to be_nil + expect(service_span.send(:meta)['_dd.runtime_family']).to be_nil + expect(service_span.send(:meta)['_dd.appsec.waf.version']).to be_nil + expect(span.send(:meta)['http.client_ip']).to eq nil + end +end + +RSpec.shared_examples 'a trace with AppSec tags' do + it do + expect(service_span.send(:metrics)['_dd.appsec.enabled']).to eq(1.0) + expect(service_span.send(:meta)['_dd.runtime_family']).to eq('ruby') + expect(service_span.send(:meta)['_dd.appsec.waf.version']).to match(/^\d+\.\d+\.\d+/) + expect(span.send(:meta)['http.client_ip']).to eq client_ip + end + + context 'with appsec disabled' do + let(:appsec_enabled) { false } + + it_behaves_like 'a trace without AppSec tags' + end +end + +RSpec.shared_examples 'a trace without AppSec events' do + it do + expect(spans.select { |s| s.get_tag('appsec.event') }).to be_empty + expect(service_span.send(:meta)['_dd.appsec.triggers']).to be_nil + end +end + +RSpec.shared_examples 'a trace with AppSec events' do + it do + expect(spans.select { |s| s.get_tag('appsec.event') }).to_not be_empty + expect(service_span.send(:meta)['_dd.appsec.json']).to be_a String + end + + context 'with appsec disabled' do + let(:appsec_enabled) { false } + + it_behaves_like 'a trace without AppSec events' + end +end