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 22, 2024
1 parent 2279292 commit 5e3a4b0
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 101 deletions.
19 changes: 10 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,14 +4,15 @@
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/engine'
require 'datadog/appsec/reactive/shared_examples'

RSpec.describe Datadog::AppSec::Contrib::GraphQL::Reactive::Multiplex do
include_context 'with GraphQL multiplex'

let(:engine) { Datadog::AppSec::Reactive::Engine.new }
let(:expected_arguments) do
{
'user' => [{ 'id' => 1 }, { 'id' => 10 }],
Expand All @@ -20,10 +21,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 +33,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
20 changes: 10 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,15 @@
# 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/engine'
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 +20,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 +32,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 +50,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
22 changes: 11 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,13 @@

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/engine'
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 +24,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 +73,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
24 changes: 13 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,15 @@
# 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/engine'
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 +26,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 +39,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 +60,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
Loading

0 comments on commit 5e3a4b0

Please sign in to comment.