Skip to content

Commit

Permalink
Extract shared exmaples to a be able to share them
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Jun 5, 2023
1 parent 0ebdf7d commit 04e6ef0
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 402 deletions.
135 changes: 1 addition & 134 deletions spec/datadog/appsec/contrib/rack/integration_test_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'datadog/tracing/contrib/support/spec_helper'
require 'datadog/appsec/contrib/support/integration/shared_examples'
require 'rack/test'

require 'securerandom'
Expand Down Expand Up @@ -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
Expand Down
135 changes: 1 addition & 134 deletions spec/datadog/appsec/contrib/rails/integration_test_spec.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
{
Expand Down
Loading

0 comments on commit 04e6ef0

Please sign in to comment.