Skip to content

Commit

Permalink
Remove use of Reactive::Operation in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Strech committed Nov 21, 2024
1 parent 5bc0632 commit 332b8fc
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 100 deletions.
17 changes: 8 additions & 9 deletions spec/datadog/appsec/contrib/graphql/reactive/multiplex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require 'datadog/tracing/contrib/graphql/support/application'

require 'datadog/appsec/spec_helper'
require 'datadog/appsec/reactive/operation'
require 'datadog/appsec/contrib/graphql/gateway/multiplex'
require 'datadog/appsec/contrib/graphql/reactive/multiplex'
require 'datadog/appsec/reactive/shared_examples'
Expand All @@ -20,10 +19,10 @@
end

describe '.publish' do
it 'propagates multiplex attributes to the operation' do
expect(operation).to receive(:publish).with('graphql.server.all_resolvers', expected_arguments)
it 'propagates multiplex attributes to the engine' do
expect(engine).to receive(:publish).with('graphql.server.all_resolvers', expected_arguments)
gateway_multiplex = Datadog::AppSec::Contrib::GraphQL::Gateway::Multiplex.new(multiplex)
described_class.publish(operation, gateway_multiplex)
described_class.publish(engine, gateway_multiplex)
end
end

Expand All @@ -32,27 +31,27 @@

context 'not all addresses have been published' do
it 'does not call the waf context' do
expect(operation).to receive(:subscribe).with(
expect(engine).to receive(:subscribe).with(
'graphql.server.all_resolvers'
).and_call_original
expect(waf_context).to_not receive(:run)
described_class.subscribe(operation, waf_context)
described_class.subscribe(engine, waf_context)
end
end

context 'all addresses have been published' do
it 'does call the waf context with the right arguments' do
expect(operation).to receive(:subscribe).and_call_original
expect(engine).to receive(:subscribe).and_call_original

waf_result = double(:waf_result, status: :ok, timeout: false)
expect(waf_context).to receive(:run).with(
{ 'graphql.server.all_resolvers' => expected_arguments },
{},
Datadog.configuration.appsec.waf_timeout
).and_return(waf_result)
described_class.subscribe(operation, waf_context)
described_class.subscribe(engine, waf_context)
gateway_multiplex = Datadog::AppSec::Contrib::GraphQL::Gateway::Multiplex.new(multiplex)
result = described_class.publish(operation, gateway_multiplex)
result = described_class.publish(engine, gateway_multiplex)
expect(result).to be_nil
end
end
Expand Down
19 changes: 9 additions & 10 deletions spec/datadog/appsec/contrib/rack/reactive/request_body_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# frozen_string_literal: true

require 'datadog/appsec/spec_helper'
require 'datadog/appsec/reactive/operation'
require 'datadog/appsec/contrib/rack/gateway/request'
require 'datadog/appsec/contrib/rack/reactive/request_body'
require 'datadog/appsec/reactive/shared_examples'

require 'rack'

RSpec.describe Datadog::AppSec::Contrib::Rack::Reactive::RequestBody do
let(:operation) { Datadog::AppSec::Reactive::Operation.new('test') }
let(:engine) { Datadog::AppSec::Reactive::Engine.new }
let(:request) do
Datadog::AppSec::Contrib::Rack::Gateway::Request.new(
Rack::MockRequest.env_for(
Expand All @@ -20,10 +19,10 @@
end

describe '.publish' do
it 'propagates request body attributes to the operation' do
expect(operation).to receive(:publish).with('request.body', { 'foo' => 'bar' })
it 'propagates request body attributes to the engine' do
expect(engine).to receive(:publish).with('request.body', { 'foo' => 'bar' })

described_class.publish(operation, request)
described_class.publish(engine, request)
end
end

Expand All @@ -32,15 +31,15 @@

context 'not all addresses have been published' do
it 'does not call the waf context' do
expect(operation).to receive(:subscribe).with('request.body').and_call_original
expect(engine).to receive(:subscribe).with('request.body').and_call_original
expect(waf_context).to_not receive(:run)
described_class.subscribe(operation, waf_context)
described_class.subscribe(engine, waf_context)
end
end

context 'all addresses have been published' do
it 'does call the waf context with the right arguments' do
expect(operation).to receive(:subscribe).and_call_original
expect(engine).to receive(:subscribe).and_call_original

expected_waf_arguments = { 'server.request.body' => { 'foo' => 'bar' } }

Expand All @@ -50,8 +49,8 @@
{},
Datadog.configuration.appsec.waf_timeout
).and_return(waf_result)
described_class.subscribe(operation, waf_context)
result = described_class.publish(operation, request)
described_class.subscribe(engine, waf_context)
result = described_class.publish(engine, request)
expect(result).to be_nil
end
end
Expand Down
30 changes: 15 additions & 15 deletions spec/datadog/appsec/contrib/rack/reactive/request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

require 'datadog/appsec/spec_helper'
require 'datadog/appsec/reactive/operation'
require 'datadog/appsec/reactive/engine'
require 'datadog/appsec/contrib/rack/gateway/request'
require 'datadog/appsec/contrib/rack/reactive/request'
require 'datadog/appsec/reactive/shared_examples'

require 'rack'

RSpec.describe Datadog::AppSec::Contrib::Rack::Reactive::Request do
let(:operation) { Datadog::AppSec::Reactive::Operation.new('test') }
let(:engine) { Datadog::AppSec::Reactive::Engine.new }
let(:request) do
Datadog::AppSec::Contrib::Rack::Gateway::Request.new(
Rack::MockRequest.env_for(
Expand All @@ -34,15 +34,15 @@
end

describe '.publish' do
it 'propagates request attributes to the operation' do
expect(operation).to receive(:publish).with('server.request.method', 'GET')
expect(operation).to receive(:publish).with('request.query', { 'a' => ['foo'] })
expect(operation).to receive(:publish).with('request.headers', expected_headers_with_cookies)
expect(operation).to receive(:publish).with('request.uri.raw', '/?a=foo')
expect(operation).to receive(:publish).with('request.cookies', { 'foo' => 'bar' })
expect(operation).to receive(:publish).with('request.client_ip', '10.10.10.10')
it 'propagates request attributes to the engine' do
expect(engine).to receive(:publish).with('server.request.method', 'GET')
expect(engine).to receive(:publish).with('request.query', { 'a' => ['foo'] })
expect(engine).to receive(:publish).with('request.headers', expected_headers_with_cookies)
expect(engine).to receive(:publish).with('request.uri.raw', '/?a=foo')
expect(engine).to receive(:publish).with('request.cookies', { 'foo' => 'bar' })
expect(engine).to receive(:publish).with('request.client_ip', '10.10.10.10')

described_class.publish(operation, request)
described_class.publish(engine, request)
end
end

Expand All @@ -51,7 +51,7 @@

context 'not all addresses have been published' do
it 'does not call the waf context' do
expect(operation).to receive(:subscribe).with(
expect(engine).to receive(:subscribe).with(
'request.headers',
'request.uri.raw',
'request.query',
Expand All @@ -60,13 +60,13 @@
'server.request.method',
).and_call_original
expect(waf_context).to_not receive(:run)
described_class.subscribe(operation, waf_context)
described_class.subscribe(engine, waf_context)
end
end

context 'all addresses have been published' do
it 'does call the waf context with the right arguments' do
expect(operation).to receive(:subscribe).and_call_original
expect(engine).to receive(:subscribe).and_call_original

expected_waf_arguments = {
'server.request.cookies' => { 'foo' => 'bar' },
Expand All @@ -84,8 +84,8 @@
{},
Datadog.configuration.appsec.waf_timeout
).and_return(waf_result)
described_class.subscribe(operation, waf_context)
result = described_class.publish(operation, request)
described_class.subscribe(engine, waf_context)
result = described_class.publish(engine, request)
expect(result).to be_nil
end
end
Expand Down
21 changes: 10 additions & 11 deletions spec/datadog/appsec/contrib/rack/reactive/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

require 'datadog/appsec/spec_helper'
require 'datadog/appsec/scope'
require 'datadog/appsec/reactive/operation'
require 'datadog/appsec/contrib/rack/gateway/response'
require 'datadog/appsec/contrib/rack/reactive/response'
require 'datadog/appsec/reactive/shared_examples'

RSpec.describe Datadog::AppSec::Contrib::Rack::Reactive::Response do
let(:operation) { Datadog::AppSec::Reactive::Operation.new('test') }
let(:engine) { Datadog::AppSec::Reactive::Engine.new }
let(:processor_context) { instance_double(Datadog::AppSec::Processor::Context) }
let(:scope) { instance_double(Datadog::AppSec::Scope, processor_context: processor_context) }
let(:body) { ['Ok'] }
Expand All @@ -24,31 +23,31 @@
end

describe '.publish' do
it 'propagates response attributes to the operation' do
expect(operation).to receive(:publish).with('response.status', 200)
expect(operation).to receive(:publish).with(
it 'propagates response attributes to the engine' do
expect(engine).to receive(:publish).with('response.status', 200)
expect(engine).to receive(:publish).with(
'response.headers',
headers,
)
described_class.publish(operation, response)
described_class.publish(engine, response)
end
end

describe '.subscribe' do
context 'not all addresses have been published' do
it 'does not call the waf context' do
expect(operation).to receive(:subscribe).with(
expect(engine).to receive(:subscribe).with(
'response.status',
'response.headers',
).and_call_original
expect(processor_context).to_not receive(:run)
described_class.subscribe(operation, processor_context)
described_class.subscribe(engine, processor_context)
end
end

context 'waf arguments' do
before do
expect(operation).to receive(:subscribe).and_call_original
expect(engine).to receive(:subscribe).and_call_original
end

let(:waf_result) { double(:waf_result, status: :ok, timeout: false) }
Expand All @@ -73,8 +72,8 @@
{},
Datadog.configuration.appsec.waf_timeout
).and_return(waf_result)
described_class.subscribe(operation, processor_context)
result = described_class.publish(operation, response)
described_class.subscribe(engine, processor_context)
result = described_class.publish(engine, response)
expect(result).to be_nil
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/datadog/appsec/contrib/rails/integration_test_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'datadog/tracing/contrib/rails/rails_helper'
require 'datadog/appsec/contrib/support/integration/shared_examples'
require 'datadog/appsec/spec_helper'
Expand Down
23 changes: 12 additions & 11 deletions spec/datadog/appsec/contrib/rails/reactive/action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

require 'datadog/appsec/spec_helper'
require 'datadog/appsec/reactive/operation'
require 'datadog/appsec/contrib/rails/reactive/action'
require 'datadog/appsec/contrib/rails/gateway/request'
require 'datadog/appsec/reactive/shared_examples'

require 'action_dispatch'

RSpec.describe Datadog::AppSec::Contrib::Rails::Reactive::Action do
let(:operation) { Datadog::AppSec::Reactive::Operation.new('test') }
let(:engine) { Datadog::AppSec::Reactive::Engine.new }
let(:request) do
request_env = Rack::MockRequest.env_for(
'http://example.com:8080/?a=foo',
Expand All @@ -24,11 +25,11 @@
end

describe '.publish' do
it 'propagates request attributes to the operation' do
expect(operation).to receive(:publish).with('rails.request.body', { 'foo' => 'bar' })
expect(operation).to receive(:publish).with('rails.request.route_params', { id: '1234' })
it 'propagates request attributes to the engine' do
expect(engine).to receive(:publish).with('rails.request.body', { 'foo' => 'bar' })
expect(engine).to receive(:publish).with('rails.request.route_params', { id: '1234' })

described_class.publish(operation, request)
described_class.publish(engine, request)
end
end

Expand All @@ -37,15 +38,15 @@

context 'not all addresses have been published' do
it 'does not call the waf context' do
expect(operation).to receive(:subscribe).with('rails.request.body', 'rails.request.route_params').and_call_original
expect(engine).to receive(:subscribe).with('rails.request.body', 'rails.request.route_params').and_call_original
expect(waf_context).to_not receive(:run)
described_class.subscribe(operation, waf_context)
described_class.subscribe(engine, waf_context)
end
end

context 'all addresses have been published' do
it 'does call the waf context with the right arguments' do
expect(operation).to receive(:subscribe).and_call_original
expect(engine).to receive(:subscribe).and_call_original

expected_waf_arguments = {
'server.request.body' => { 'foo' => 'bar' },
Expand All @@ -58,8 +59,8 @@
{},
Datadog.configuration.appsec.waf_timeout
).and_return(waf_result)
described_class.subscribe(operation, waf_context)
result = described_class.publish(operation, request)
described_class.subscribe(engine, waf_context)
result = described_class.publish(engine, request)
expect(result).to be_nil
end
end
Expand Down
19 changes: 9 additions & 10 deletions spec/datadog/appsec/contrib/sinatra/reactive/routed_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'datadog/appsec/spec_helper'
require 'datadog/appsec/reactive/operation'
require 'datadog/appsec/contrib/sinatra/reactive/routed'
require 'datadog/appsec/contrib/rack/gateway/request'
require 'datadog/appsec/contrib/sinatra/gateway/route_params'
Expand All @@ -10,7 +9,7 @@
require 'rack'

RSpec.describe Datadog::AppSec::Contrib::Sinatra::Reactive::Routed do
let(:operation) { Datadog::AppSec::Reactive::Operation.new('test') }
let(:engine) { Datadog::AppSec::Reactive::Engine.new }
let(:request) do
Datadog::AppSec::Contrib::Rack::Gateway::Request.new(
Rack::MockRequest.env_for(
Expand All @@ -22,10 +21,10 @@
let(:routed_params) { Datadog::AppSec::Contrib::Sinatra::Gateway::RouteParams.new({ id: '1234' }) }

describe '.publish' do
it 'propagates routed params attributes to the operation' do
expect(operation).to receive(:publish).with('sinatra.request.route_params', { id: '1234' })
it 'propagates routed params attributes to the engine' do
expect(engine).to receive(:publish).with('sinatra.request.route_params', { id: '1234' })

described_class.publish(operation, [request, routed_params])
described_class.publish(engine, [request, routed_params])
end
end

Expand All @@ -34,15 +33,15 @@

context 'not all addresses have been published' do
it 'does not call the waf context' do
expect(operation).to receive(:subscribe).with('sinatra.request.route_params').and_call_original
expect(engine).to receive(:subscribe).with('sinatra.request.route_params').and_call_original
expect(waf_context).to_not receive(:run)
described_class.subscribe(operation, waf_context)
described_class.subscribe(engine, waf_context)
end
end

context 'all addresses have been published' do
it 'does call the waf context with the right arguments' do
expect(operation).to receive(:subscribe).and_call_original
expect(engine).to receive(:subscribe).and_call_original

expected_waf_arguments = {
'server.request.path_params' => { id: '1234' }
Expand All @@ -54,8 +53,8 @@
{},
Datadog.configuration.appsec.waf_timeout
).and_return(waf_result)
described_class.subscribe(operation, waf_context)
result = described_class.publish(operation, [request, routed_params])
described_class.subscribe(engine, waf_context)
result = described_class.publish(engine, [request, routed_params])
expect(result).to be_nil
end
end
Expand Down
Loading

0 comments on commit 332b8fc

Please sign in to comment.