Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ def initialize
end

def handle_request(args)
return {} unless args[:params]['collection_name']

datasource = ForestAdminRpcAgent::Facades::Container.datasource
collection = get_collection_safe(datasource, args[:params]['collection_name'])
filter = FilterFactory.from_plain_object(args[:params]['filter'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ def initialize
end

def handle_request(args)
return {} unless args[:params]['collection_name']

datasource = ForestAdminRpcAgent::Facades::Container.datasource
collection = get_collection_safe(datasource, args[:params]['collection_name'])
filter = FilterFactory.from_plain_object(args[:params]['filter'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ def initialize
end

def handle_request(args)
return {} unless args[:params]['collection_name']

datasource = ForestAdminRpcAgent::Facades::Container.datasource
collection = get_collection_safe(datasource, args[:params]['collection_name'])

aggregation = Aggregation.new(
operation: args[:params]['aggregation']['operation'],
field: args[:params]['aggregation']['field'],
groups: args[:params]['aggregation']['groups']
groups: args[:params]['aggregation']['groups'] || []
Comment thread
arnaud-moncel marked this conversation as resolved.
)
filter = FilterFactory.from_plain_object(args[:params]['filter'])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ def register_rails(router)

# Skip authentication for health check (root path)
if @url == '/'
params = deep_symbolize_keys(request.query_parameters.merge(request.request_parameters))
result = handle_request({ params: params.with_indifferent_access, caller: nil, request: request })
params = extract_request_params(request)
result = handle_request({ params: params, caller: nil, request: request })
build_rails_response(result)
else
auth_middleware = ForestAdminRpcAgent::Middleware::Authentication.new(->(_env) { [200, {}, ['OK']] })
status, headers, response = auth_middleware.call(request.env)

if status == 200
params = deep_symbolize_keys(request.query_parameters.merge(request.request_parameters))
result = handle_request({ params: params.with_indifferent_access, caller: headers[:caller], request: request })
params = extract_request_params(request)
result = handle_request({ params: params, caller: headers[:caller], request: request })
build_rails_response(result)
else
[status, headers, response]
Expand Down Expand Up @@ -87,15 +87,15 @@ def get_collection_safe(datasource, collection_name)

private

def deep_symbolize_keys(obj)
case obj
when Hash
obj.transform_keys(&:to_sym).transform_values { |v| deep_symbolize_keys(v) }
when Array
obj.map { |v| deep_symbolize_keys(v) }
else
obj
end
# Merge path params (e.g. :collection_name from the URL) with query and body params so
# consumers that don't duplicate `collection_name` in the body (the Node datasource-rpc)
# still resolve the route correctly.
def extract_request_params(request)
request.path_parameters
.except(:controller, :action, :format)
.merge(request.query_parameters)
.merge(request.request_parameters)
.with_indifferent_access
end

def serialize_response(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ def initialize
end

def handle_request(args)
return {} unless args[:params]['collection_name']

chart_name = args[:params]['chart']
parameters = args[:params]['parameters']
datasource = ForestAdminRpcAgent::Facades::Container.datasource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ def initialize
end

def handle_request(args)
return {} unless args[:params]['collection_name']

datasource = ForestAdminRpcAgent::Facades::Container.datasource
collection = get_collection_safe(datasource, args[:params]['collection_name'])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ def initialize
end

def handle_request(args)
return {} unless args[:params]['collection_name']

datasource = ForestAdminRpcAgent::Facades::Container.datasource
collection = get_collection_safe(datasource, args[:params]['collection_name'])
filter = FilterFactory.from_plain_object(args[:params]['filter'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ def initialize
end

def handle_request(args)
return {} unless args[:params]['collection_name']

datasource = ForestAdminRpcAgent::Facades::Container.datasource
collection = get_collection_safe(datasource, args[:params]['collection_name'])
projection = Projection.new(args[:params]['projection'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ def initialize
end

def handle_request(args)
return {} unless args[:params]['collection_name']

datasource = ForestAdminRpcAgent::Facades::Container.datasource
collection = get_collection_safe(datasource, args[:params]['collection_name'])
filter = FilterFactory.from_plain_object(args[:params]['filter'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ module Routes
end
end

context 'when collection_name is missing' do
let(:params) { {} }
context 'when the collection is unknown' do
let(:params) { { 'collection_name' => 'unknown', 'action' => 'noop', 'data' => {} } }

it 'returns an empty hash' do
response = route.handle_request(args)
expect(response).to eq({})
it 'raises NotFoundError so the controller maps it to a 404 response' do
expect { route.handle_request(args) }
.to raise_error(ForestAdminAgent::Http::Exceptions::NotFoundError)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ module Routes
end
end

context 'when collection_name is missing' do
let(:params) { {} }
context 'when the collection is unknown' do
let(:params) { { 'collection_name' => 'unknown', 'action' => 'noop' } }

it 'returns an empty hash' do
response = route.handle_request(args)
expect(response).to eq({})
it 'raises NotFoundError so the controller maps it to a 404 response' do
expect { route.handle_request(args) }
.to raise_error(ForestAdminAgent::Http::Exceptions::NotFoundError)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,24 @@ module Routes
end
end

context 'when collection_name is missing' do
it 'returns an empty hash' do
result = route.handle_request(params: {})
expect(result).to eq({})
context 'when the collection is unknown' do
it 'raises NotFoundError so the controller maps it to a 404 response' do
expect do
route.handle_request(
params: { 'collection_name' => 'unknown', 'aggregation' => { 'operation' => 'Count' } }
)
end.to raise_error(ForestAdminAgent::Http::Exceptions::NotFoundError)
end
end

context 'when the aggregation payload omits :groups' do
it 'defaults groups to [] so Aggregation.new keeps its default' do
route.handle_request(
params: params.merge('aggregation' => { 'operation' => 'Count' })
)

expect(ForestAdminDatasourceToolkit::Components::Query::Aggregation)
.to have_received(:new).with(operation: 'Count', field: nil, groups: [])
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'
require 'rails'
require 'action_dispatch'
require 'active_support/core_ext/hash/indifferent_access'

module ForestAdminRpcAgent
module Routes
Expand Down Expand Up @@ -178,6 +179,45 @@ def handle_request(_params)
end
end
end

describe '#extract_request_params' do
let(:request) do
instance_double(
ActionDispatch::Request,
path_parameters: { controller: 'forest_admin_rails/forest', action: 'index',
format: 'json', collection_name: 'books' },
query_parameters: { 'page' => '2' },
request_parameters: { 'filter' => { 'search' => 'hello' } }
)
end

it 'merges path, query and body params with indifferent access' do
params = route.send(:extract_request_params, request)

expect(params['collection_name']).to eq('books')
expect(params[:collection_name]).to eq('books')
expect(params['page']).to eq('2')
expect(params['filter']['search']).to eq('hello')
end

it 'strips Rails-internal path keys (controller, action, format)' do
params = route.send(:extract_request_params, request)

expect(params).not_to have_key('controller')
expect(params).not_to have_key('action')
expect(params).not_to have_key('format')
end

it 'lets body params override path params on key collision' do
allow(request).to receive_messages(
path_parameters: { collection_name: 'from_path' },
query_parameters: {},
request_parameters: { 'collection_name' => 'from_body' }
)

expect(route.send(:extract_request_params, request)['collection_name']).to eq('from_body')
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ module Routes
end
end

context 'when collection_name is missing' do
it 'returns an empty hash' do
result = route.handle_request(params: {})
expect(result).to eq({})
context 'when the collection is unknown' do
it 'raises NotFoundError so the controller maps it to a 404 response' do
expect do
route.handle_request(params: { 'collection_name' => 'unknown', 'chart' => 'foo' })
end.to raise_error(ForestAdminAgent::Http::Exceptions::NotFoundError)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ module Routes
end
end

context 'when collection_name is missing' do
it 'returns an empty hash' do
result = route.handle_request(params: {})
expect(result).to eq({})
context 'when the collection is unknown' do
it 'raises NotFoundError so the controller maps it to a 404 response' do
expect do
route.handle_request(params: { 'collection_name' => 'unknown', 'data' => [{}] })
end.to raise_error(ForestAdminAgent::Http::Exceptions::NotFoundError)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ module Routes
end
end

context 'when collection_name is missing' do
it 'returns an empty hash' do
result = route.handle_request(params: {})
expect(result).to eq({})
context 'when the collection is unknown' do
it 'raises NotFoundError so the controller maps it to a 404 response' do
expect do
route.handle_request(params: { 'collection_name' => 'unknown', 'filter' => {} })
end.to raise_error(ForestAdminAgent::Http::Exceptions::NotFoundError)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ module Routes
end
end

context 'when collection_name is missing' do
it 'returns an empty hash' do
result = route.handle_request(params: {})
expect(result).to eq({})
context 'when the collection is unknown' do
it 'raises NotFoundError so the controller maps it to a 404 response' do
expect do
route.handle_request(params: { 'collection_name' => 'unknown' })
end.to raise_error(ForestAdminAgent::Http::Exceptions::NotFoundError)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ module Routes
end
end

context 'when collection_name is missing' do
it 'returns an empty hash' do
result = route.handle_request(params: {})
expect(result).to eq({})
context 'when the collection is unknown' do
it 'raises NotFoundError so the controller maps it to a 404 response' do
expect do
route.handle_request(params: { 'collection_name' => 'unknown', 'filter' => {}, 'patch' => {} })
end.to raise_error(ForestAdminAgent::Http::Exceptions::NotFoundError)
end
end
end
Expand Down
Loading