add sanitize method to service
This commit is contained in:
parent
61642f834f
commit
8d747ff16e
2 changed files with 50 additions and 9 deletions
|
@ -12,8 +12,30 @@ module APIParticulier
|
|||
.reduce({}) { |acc, el| acc.deep_merge(el) }
|
||||
end
|
||||
|
||||
# Remove sources not available for the procedure
|
||||
def sanitize(requested_sources)
|
||||
requested_sources_a = h_to_a(requested_sources)
|
||||
available_sources_a = h_to_a(available_sources)
|
||||
|
||||
filtered_sources_a = requested_sources_a.intersection(available_sources_a)
|
||||
|
||||
a_to_h(filtered_sources_a)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# { 'cnaf' => { 'scope' => ['a', 'b'] }} => [['cnaf', 'scope', 'a'], ['cnaf', 'scope', 'b']]
|
||||
def h_to_a(h)
|
||||
h.reduce([]) { |acc, (provider, scopes)| scopes.each { |scope, values| values.each { |s, _| acc << [provider, scope, s] } }; acc }
|
||||
end
|
||||
|
||||
# [['cnaf', 'scope', 'a'], ['cnaf', 'scope', 'b']] => { 'cnaf' => { 'scope' => ['a', 'b'] }}
|
||||
def a_to_h(a)
|
||||
h = Hash.new { |h, k| h[k] = Hash.new { |h2, k2| h2[k2] = [] } }
|
||||
|
||||
a.reduce(h) { |acc, (provider, scope, source)| h[provider][scope] << source; acc }
|
||||
end
|
||||
|
||||
def extract_sources(provider, scope)
|
||||
{ provider => { scope => providers[provider][scope] } }
|
||||
end
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
describe APIParticulier::Services::SourcesService do
|
||||
describe "#sources" do
|
||||
let(:service) { described_class.new(procedure) }
|
||||
let(:procedure) { create(:procedure) }
|
||||
let(:api_particulier_scopes) { [] }
|
||||
let(:api_particulier_sources) { {} }
|
||||
let(:service) { described_class.new(procedure) }
|
||||
|
||||
before do
|
||||
procedure.update(api_particulier_scopes: api_particulier_scopes)
|
||||
procedure.update(api_particulier_sources: api_particulier_sources)
|
||||
end
|
||||
let(:procedure) { create(:procedure) }
|
||||
let(:api_particulier_scopes) { [] }
|
||||
let(:api_particulier_sources) { {} }
|
||||
|
||||
before do
|
||||
procedure.update(api_particulier_scopes: api_particulier_scopes)
|
||||
procedure.update(api_particulier_sources: api_particulier_sources)
|
||||
end
|
||||
|
||||
describe "#available_sources" do
|
||||
subject { service.available_sources }
|
||||
|
||||
context 'when the procedure doesn’t have any available scopes' do
|
||||
|
@ -31,4 +32,22 @@ describe APIParticulier::Services::SourcesService do
|
|||
it { is_expected.to match(cnaf_allocataires_and_enfants) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#sanitize' do
|
||||
subject { service.sanitize(requested_sources) }
|
||||
|
||||
let(:api_particulier_scopes) { ['cnaf_allocataires', 'cnaf_adresse'] }
|
||||
let(:requested_sources) do
|
||||
{
|
||||
'cnaf' => {
|
||||
'allocataires' => ['noms_prenoms', 'forbidden_sources', { 'weird_object' => 1 }],
|
||||
'forbidden_scope' => ['any_source'],
|
||||
'adresse' => { 'weird_object' => 1 }
|
||||
},
|
||||
'forbidden_provider' => { 'anything_scope' => ['any_source'] }
|
||||
}
|
||||
end
|
||||
|
||||
it { is_expected.to eq({ 'cnaf' => { 'allocataires' => ['noms_prenoms'] } }) }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue