Merge pull request #6483 from betagouv/add_cnaf_adapter

ajout de l'adapter cnaf
This commit is contained in:
LeSim 2021-09-27 10:39:27 +02:00 committed by GitHub
commit ac1aba6c91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 414 additions and 26 deletions

View file

@ -2,6 +2,7 @@ class APIParticulier::API
include APIParticulier::Error
INTROSPECT_RESOURCE_NAME = "introspect"
COMPOSITION_FAMILIALE_RESOURCE_NAME = "v2/composition-familiale"
TIMEOUT = 20
@ -10,7 +11,13 @@ class APIParticulier::API
end
def scopes
get(INTROSPECT_RESOURCE_NAME)[:scopes]
get(INTROSPECT_RESOURCE_NAME)['scopes']
end
def composition_familiale(numero_allocataire, code_postal)
get(COMPOSITION_FAMILIALE_RESOURCE_NAME,
numeroAllocataire: numero_allocataire,
codePostal: code_postal)
end
private
@ -24,7 +31,7 @@ class APIParticulier::API
timeout: TIMEOUT)
if response.success?
JSON.parse(response.body, symbolize_names: true)
JSON.parse(response.body)
elsif response.code == 401
raise Unauthorized.new(response)
else

View file

@ -0,0 +1,47 @@
class APIParticulier::CNAFAdapter
class InvalidSchemaError < ::StandardError
def initialize(errors)
super(errors.map(&:to_json).join("\n"))
end
end
def initialize(api_particulier_token, numero_allocataire, code_postal, requested_sources)
@api = APIParticulier::API.new(api_particulier_token)
@numero_allocataire = numero_allocataire
@code_postal = code_postal
@requested_sources = requested_sources
end
def to_params
@api.composition_familiale(@numero_allocataire, @code_postal)
.tap { |d| ensure_valid_schema!(d) }
.then { |d| extract_requested_sources(d) }
end
private
def ensure_valid_schema!(data)
if !schemer.valid?(data)
errors = schemer.validate(data).to_a
raise InvalidSchemaError.new(errors)
end
end
def schemer
@schemer ||= JSONSchemer.schema(Rails.root.join('app/schemas/composition-familiale.json'))
end
def extract_requested_sources(data)
@requested_sources['cnaf']&.map do |(scope, sources)|
case scope
when 'enfants', 'allocataires'
{ scope => data[scope].map { |s| s.slice(*sources) } }
when 'quotient_familial'
{ scope => data.slice(*sources) }
else
{ scope => data[scope].slice(*sources) }
end
end
&.reduce(&:deep_merge) || {}
end
end

View file

@ -52,10 +52,10 @@ module APIParticulier
def providers
{
'cnaf' => {
'allocataires' => ['noms_prenoms', 'date_de_naissance', 'sexe'],
'enfants' => ['noms_prenoms', 'date_de_naissance', 'sexe'],
'adresse' => ['identite', 'complement_d_identite', 'complement_d_identite_geo', 'numero_et_rue', 'lieu_dit', 'code_postal_et_ville', 'pays'],
'quotient_familial' => ['quotient_familial', 'annee', 'mois']
'allocataires' => ['nomPrenom', 'dateDeNaissance', 'sexe'],
'enfants' => ['nomPrenom', 'dateDeNaissance', 'sexe'],
'adresse' => ['identite', 'complementIdentite', 'complementIdentiteGeo', 'numeroRue', 'lieuDit', 'codePostalVille', 'pays'],
'quotient_familial' => ['quotientFamilial', 'annee', 'mois']
}
}
end

View file

@ -0,0 +1,70 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://demarches-simplifiees.fr/composition-familiale.schema.json",
"title": "composition familiale",
"type": "object",
"properties": {
"adresse": {
"type": "object",
"properties": {
"codePostalVille": {
"type": "string"
},
"identite": {
"type": "string"
},
"complementIdentite": {
"type": "string"
},
"numeroRue": {
"type": "string"
},
"pays": {
"type": "string"
},
"complementIdentiteGeo": {
"type": "string"
},
"lieuDit": {
"type": "string"
}
}
},
"allocataires": {
"type": "array",
"items": { "$ref": "#/$defs/person" }
},
"enfants": {
"type": "array",
"items": { "$ref": "#/$defs/person" }
},
"quotientFamilial": {
"type": "integer"
},
"annee": {
"type": "integer"
},
"mois": {
"type": "integer",
"minimum": 1,
"maximum": 12
}
},
"$defs": {
"person": {
"type": "object",
"properties": {
"nomPrenom": {
"type": "string"
},
"dateDeNaissance": {
"type": "string",
"pattern": "^[0-9]{8}$"
},
"sexe": {
"enum": ["F", "M"]
}
}
}
}
}

View file

@ -14,6 +14,7 @@ ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'JSON'
inflect.acronym 'RNA'
inflect.acronym 'URL'
inflect.acronym 'CNAF'
inflect.irregular 'type_de_champ', 'types_de_champ'
inflect.irregular 'type_de_champ_private', 'types_de_champ_private'
inflect.irregular 'procedure_revision_type_de_champ', 'procedure_revision_types_de_champ'

View file

@ -5,8 +5,8 @@ fr:
libelle: Caisse dallocations familiales (CAF)
scopes:
personne: &personne
noms_prenoms: noms et prénoms
date_de_naissance: date de naissance
nomPrenom: noms et prénoms
dateDeNaissance: date de naissance
sexe: genre
allocataires:
libelle: allocataires
@ -17,14 +17,14 @@ fr:
adresse:
libelle: adresse
identite: identité
complement_d_identite: complément didentité
complement_d_identite_geo: complément didentité géographique
numero_et_rue: numéro et rue
lieu_dit: lieu-dit
code_postal_et_ville: code postal et ville
complementIdentite: complément didentité
complementIdentiteGeo: complément didentité géographique
numeroRue: numéro et rue
lieuDit: lieu-dit
codePostalVille: code postal et ville
pays: pays
quotient_familial:
libelle: quotient familial
quotient_familial: quotient familial
quotientFamilial: quotient familial
mois: mois
annee: année

View file

@ -40,7 +40,7 @@ describe NewAdministrateur::JetonParticulierController, type: :controller do
context "and the api response is a success" do
let(:cassette) { "api_particulier/success/introspect" }
let(:procedure) { create(:procedure, administrateur: admin, api_particulier_sources: { cnaf: { allocataires: ['noms_prenoms'] } }) }
let(:procedure) { create(:procedure, administrateur: admin, api_particulier_sources: { cnaf: { allocataires: ['nomPrenom'] } }) }
it 'saves the jeton' do
expect(flash.alert).to be_nil

View file

@ -4,18 +4,18 @@ describe NewAdministrateur::SourcesParticulierController, type: :controller do
before { sign_in(admin.user) }
describe "#show" do
let(:procedure) { create(:procedure, administrateur: admin, api_particulier_scopes: ['cnaf_enfants'], api_particulier_sources: { cnaf: { enfants: ['noms_prenoms'] } }) }
let(:procedure) { create(:procedure, administrateur: admin, api_particulier_scopes: ['cnaf_enfants'], api_particulier_sources: { cnaf: { enfants: ['nomPrenom'] } }) }
render_views
subject { get :show, params: { procedure_id: procedure.id } }
it 'renders the sources form' do
expect(subject.body).to include(I18n.t('api_particulier.providers.cnaf.scopes.enfants.date_de_naissance'))
expect(subject.body).to have_selector("input#api_particulier_sources_cnaf_enfants_[value=noms_prenoms][checked=checked]")
expect(subject.body).to include(I18n.t('api_particulier.providers.cnaf.scopes.enfants.dateDeNaissance'))
expect(subject.body).to have_selector("input#api_particulier_sources_cnaf_enfants_[value=nomPrenom][checked=checked]")
expect(subject.body).to have_selector("input#api_particulier_sources_cnaf_enfants_[value=date_de_naissance]")
expect(subject.body).not_to have_selector("input#api_particulier_sources_cnaf_enfants_[value=date_de_naissance][checked=checked]")
expect(subject.body).to have_selector("input#api_particulier_sources_cnaf_enfants_[value=dateDeNaissance]")
expect(subject.body).not_to have_selector("input#api_particulier_sources_cnaf_enfants_[value=dateDeNaissance][checked=checked]")
end
end
@ -47,12 +47,12 @@ describe NewAdministrateur::SourcesParticulierController, type: :controller do
context 'when an authorized source is requested' do
let(:requested_sources) do
{
api_particulier_sources: { cnaf: { enfants: ['noms_prenoms'] } }
api_particulier_sources: { cnaf: { enfants: ['nomPrenom'] } }
}
end
it 'saves the source' do
expect(procedure.api_particulier_sources).to eq("cnaf" => { "enfants" => ["noms_prenoms"] })
expect(procedure.api_particulier_sources).to eq("cnaf" => { "enfants" => ["nomPrenom"] })
expect(flash.notice).to eq(I18n.t(".new_administrateur.sources_particulier.update.sources_ok"))
end
end

View file

@ -0,0 +1,79 @@
---
http_interactions:
- request:
method: get
uri: https://particulier-test.api.gouv.fr/api/v2/composition-familiale?codePostal=92110&numeroAllocataire=5843972
body:
encoding: US-ASCII
string: ''
headers:
User-Agent:
- demarches-simplifiees.fr
Accept:
- application/json
X-Api-Key:
- 29eb50b65f64e8e00c0847a8bbcbd150e1f847
Content-Type:
- text/html; charset=utf-8
Expect:
- ''
response:
status:
code: 200
message: ''
headers:
Server:
- nginx
Date:
- Mon, 20 Sep 2021 20:01:39 GMT
Content-Type:
- application/json; charset=utf-8
Content-Length:
- '387'
X-Powered-By:
- Express
Vary:
- Origin
Etag:
- W/"183-5m9o/ng+PfdMOPeC0VBn5lqbziQ"
Strict-Transport-Security:
- max-age=15724800; includeSubdomains
body:
encoding: UTF-8
string: '
{
"adresse": {
"codePostalVille": "92110 Clichy",
"identite": "Mr SNOW Eric",
"complementIdentite": "ne connait rien",
"numeroRue": "109 rue La Boétie",
"pays": "FRANCE",
"complementIdentiteGeo": "au nord de paris",
"lieuDit": "glagla"
},
"allocataires": [
{
"nomPrenom": "ERIC SNOW",
"dateDeNaissance": "07011991",
"sexe": "M"
},
{
"nomPrenom": "SANSA SNOW",
"dateDeNaissance": "15011992",
"sexe": "F"
}
],
"enfants": [
{
"nomPrenom": "PAUL SNOW",
"dateDeNaissance": "04012018",
"sexe": "M"
}
],
"quotientFamilial": 1856,
"annee": 2021,
"mois": 6
}
'
recorded_at: Mon, 20 Sep 2021 20:01:39 GMT
recorded_with: VCR 6.0.0

View file

@ -0,0 +1,79 @@
---
http_interactions:
- request:
method: get
uri: https://particulier-test.api.gouv.fr/api/v2/composition-familiale?codePostal=92110&numeroAllocataire=5843972
body:
encoding: US-ASCII
string: ''
headers:
User-Agent:
- demarches-simplifiees.fr
Accept:
- application/json
X-Api-Key:
- 29eb50b65f64e8e00c0847a8bbcbd150e1f847
Content-Type:
- text/html; charset=utf-8
Expect:
- ''
response:
status:
code: 200
message: ''
headers:
Server:
- nginx
Date:
- Mon, 20 Sep 2021 20:01:39 GMT
Content-Type:
- application/json; charset=utf-8
Content-Length:
- '387'
X-Powered-By:
- Express
Vary:
- Origin
Etag:
- W/"183-5m9o/ng+PfdMOPeC0VBn5lqbziQ"
Strict-Transport-Security:
- max-age=15724800; includeSubdomains
body:
encoding: UTF-8
string: '
{
"adresse": {
"codePostalVille": "92110 Clichy",
"identite": "Mr SNOW Eric",
"complementIdentite": "ne connait rien",
"numeroRue": "109 rue La Boétie",
"pays": "FRANCE",
"complementIdentiteGeo": "au nord de paris",
"lieuDit": "glagla"
},
"allocataires": [
{
"nomPrenom": "ERIC SNOW",
"dateDeNaissance": "07011991",
"sexe": "M"
},
{
"nomPrenom": "SANSA SNOW",
"dateDeNaissance": "15011992",
"sexe": "F"
}
],
"enfants": [
{
"nomPrenom": "PAUL SNOW",
"dateDeNaissance": "04012018",
"sexe": "M"
}
],
"quotientFamilial": 1856,
"annee": 2021,
"mois": 13
}
'
recorded_at: Mon, 20 Sep 2021 20:01:39 GMT
recorded_with: VCR 6.0.0

View file

@ -0,0 +1,35 @@
{
"allocataires": [
{
"dateDeNaissance": "07011991",
"nomPrenom": "ERIC SNOW",
"sexe": "M"
},
{
"dateDeNaissance": "15011992",
"nomPrenom": "SANSA SNOW",
"sexe": "F"
}
],
"enfants": [
{
"nomPrenom": "PAUL SNOW",
"dateDeNaissance": "04012018",
"sexe": "M"
}
],
"quotient_familial" : {
"quotientFamilial": 1856,
"annee": 2021,
"mois": 6
},
"adresse": {
"codePostalVille": "92110 Clichy",
"identite": "Mr SNOW Eric",
"complementIdentite": "ne connait rien",
"numeroRue": "109 rue La Boétie",
"pays": "FRANCE",
"complementIdentiteGeo": "au nord de paris",
"lieuDit": "glagla"
}
}

View file

@ -0,0 +1,70 @@
describe APIParticulier::CNAFAdapter do
let(:adapter) { described_class.new(api_particulier_token, numero_allocataire, code_postal, requested_sources) }
before { stub_const("API_PARTICULIER_URL", "https://particulier-test.api.gouv.fr/api") }
describe '#to_params' do
let(:api_particulier_token) { '29eb50b65f64e8e00c0847a8bbcbd150e1f847' }
let(:numero_allocataire) { '5843972' }
let(:code_postal) { '92110' }
subject { VCR.use_cassette(cassette) { adapter.to_params } }
context 'when the api answer is valid' do
let(:cassette) { "api_particulier/success/composition_familiale" }
context 'when the token has all the cnaf scopes' do
context 'and all the sources are requested' do
let(:requested_sources) do
{
'cnaf' => {
'allocataires' => ['nomPrenom', 'dateDeNaissance', 'sexe'],
'enfants' => ['nomPrenom', 'dateDeNaissance', 'sexe'],
'adresse' => ['identite', 'complementIdentite', 'complementIdentiteGeo', 'numeroRue', 'lieuDit', 'codePostalVille', 'pays'],
'quotient_familial' => ['quotientFamilial', 'annee', 'mois']
}
}
end
let(:result) { JSON.parse(File.read('spec/fixtures/files/api_particulier/composition_familiale.json')) }
it { is_expected.to eq(result) }
end
context 'when no sources is requested' do
let(:requested_sources) { {} }
it { is_expected.to eq({}) }
end
context 'when a scalar is requested' do
let(:requested_sources) { { 'cnaf' => { 'adresse' => ['pays'] } } }
it { is_expected.to eq({ "adresse" => { "pays" => "FRANCE" } }) }
end
context 'when a quotient_familial is requested' do
let(:requested_sources) { { 'cnaf' => { 'quotient_familial' => ['annee'] } } }
it { is_expected.to eq({ "quotient_familial" => { "annee" => 2021 } }) }
end
context 'when a vector is requested' do
let(:requested_sources) { { 'cnaf' => { 'allocataires' => ['nomPrenom'] } } }
it { is_expected.to eq({ "allocataires" => [{ "nomPrenom" => "ERIC SNOW" }, { "nomPrenom" => "SANSA SNOW" }] }) }
end
end
end
context 'when the api answer is valid' do
let(:cassette) { "api_particulier/success/composition_familiale_invalid" }
context 'when no sources is requested' do
let(:requested_sources) { {} }
it { expect { subject }.to raise_error(APIParticulier::CNAFAdapter::InvalidSchemaError) }
end
end
end
end

View file

@ -23,8 +23,8 @@ describe APIParticulier::Services::SourcesService do
let(:cnaf_allocataires_and_enfants) do
{
'cnaf' => {
'allocataires' => ['noms_prenoms', 'date_de_naissance', 'sexe'],
'enfants' => ['noms_prenoms', 'date_de_naissance', 'sexe']
'allocataires' => ['nomPrenom', 'dateDeNaissance', 'sexe'],
'enfants' => ['nomPrenom', 'dateDeNaissance', 'sexe']
}
}
end
@ -40,7 +40,7 @@ describe APIParticulier::Services::SourcesService do
let(:requested_sources) do
{
'cnaf' => {
'allocataires' => ['noms_prenoms', 'forbidden_sources', { 'weird_object' => 1 }],
'allocataires' => ['nomPrenom', 'forbidden_sources', { 'weird_object' => 1 }],
'forbidden_scope' => ['any_source'],
'adresse' => { 'weird_object' => 1 }
},
@ -48,6 +48,6 @@ describe APIParticulier::Services::SourcesService do
}
end
it { is_expected.to eq({ 'cnaf' => { 'allocataires' => ['noms_prenoms'] } }) }
it { is_expected.to eq({ 'cnaf' => { 'allocataires' => ['nomPrenom'] } }) }
end
end