Merge pull request #5092 from betagouv/5084-jeton-demarche

#5084 appelle api entreprise avec un jeton spécifique à une démarche
This commit is contained in:
krichtof 2020-04-28 15:38:04 +02:00 committed by GitHub
commit db4cd5f031
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 112 additions and 17 deletions

View file

@ -1,6 +1,6 @@
module NewAdministrateur module NewAdministrateur
class ProceduresController < AdministrateurController class ProceduresController < AdministrateurController
before_action :retrieve_procedure, only: [:champs, :annotations, :edit, :monavis, :update_monavis] before_action :retrieve_procedure, only: [:champs, :annotations, :edit, :monavis, :update_monavis, :jeton, :update_jeton]
before_action :procedure_locked?, only: [:champs, :annotations] before_action :procedure_locked?, only: [:champs, :annotations]
def apercu def apercu
@ -57,6 +57,18 @@ module NewAdministrateur
render 'monavis' render 'monavis'
end end
def jeton
end
def update_jeton
if !@procedure.update(procedure_params)
flash.now.alert = @procedure.errors.full_messages
else
flash.notice = 'Le jeton a bien été mis à jour'
end
render 'jeton'
end
private private
def apercu_tab def apercu_tab
@ -68,7 +80,7 @@ module NewAdministrateur
end end
def procedure_params def procedure_params
editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :cadre_juridique, :deliberation, :notice, :web_hook_url, :declarative_with_state, :euro_flag, :logo, :auto_archive_on, :monavis_embed] editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :cadre_juridique, :deliberation, :notice, :web_hook_url, :declarative_with_state, :euro_flag, :logo, :auto_archive_on, :monavis_embed, :api_entreprise_token]
permited_params = if @procedure&.locked? permited_params = if @procedure&.locked?
params.require(:procedure).permit(*editable_params) params.require(:procedure).permit(*editable_params)
else else

View file

@ -74,11 +74,12 @@ class ApiEntreprise::API
recipient: siret_or_siren, recipient: siret_or_siren,
object: "procedure_id: #{procedure_id}", object: "procedure_id: #{procedure_id}",
non_diffusables: true, non_diffusables: true,
token: token token: token_for_procedure(procedure_id)
} }
end end
def self.token def self.token_for_procedure(procedure_id)
Rails.application.secrets.api_entreprise[:key] procedure = Procedure.find(procedure_id)
procedure.api_entreprise_token.presence || Rails.application.secrets.api_entreprise[:key]
end end
end end

View file

@ -68,6 +68,10 @@
.procedure-list-element{ class: ('active' if active == 'MonAvis') } .procedure-list-element{ class: ('active' if active == 'MonAvis') }
MonAvis MonAvis
%a#onglet-description{ href: url_for(admin_procedure_jeton_path(@procedure)) }
.procedure-list-element
Jeton
%a#onglet-description{ href: url_for(admin_procedures_path()) } %a#onglet-description{ href: url_for(admin_procedures_path()) }
.procedure-list-element .procedure-list-element
Sortir Sortir

View file

@ -0,0 +1,24 @@
= render partial: 'new_administrateur/breadcrumbs',
locals: { steps: [link_to('Démarches', admin_procedures_path),
link_to(@procedure.libelle, admin_procedure_path(@procedure)),
'Jeton'] }
.container
%h1.page-title
Configurer le jeton API Entreprise
.container
%h1
= form_with model: @procedure, url: url_for({ controller: 'new_administrateur/procedures', action: :update_jeton }), html: { class: 'form' } do |f|
%p.explication
Démarches Simplifiées utilise
= link_to 'API Entreprise', "https://entreprise.api.gouv.fr/"
qui permet de récupérer les informations administratives des entreprises et des associations.
Si votre démarche nécessite des autorisations spécifiques que Démarches Simplifiées n'a pas par défaut, merci de renseigner ici le jeton
= link_to 'API Entreprise', "https://entreprise.api.gouv.fr/demander_un_acces/"
propre à votre démarche.
= f.label :api_entreprise_token, "Jeton"
= f.password_field :api_entreprise_token, class: 'form-control'
.text-right
= f.button 'Enregistrer', class: 'button primary send'

View file

@ -166,6 +166,8 @@ Rails.application.routes.draw do
post 'admin/procedures' => 'new_administrateur/procedures#create' post 'admin/procedures' => 'new_administrateur/procedures#create'
get 'admin/procedures/:id/monavis' => 'new_administrateur/procedures#monavis', as: :admin_procedure_monavis get 'admin/procedures/:id/monavis' => 'new_administrateur/procedures#monavis', as: :admin_procedure_monavis
patch 'admin/procedures/:id/monavis' => 'new_administrateur/procedures#update_monavis', as: :update_monavis patch 'admin/procedures/:id/monavis' => 'new_administrateur/procedures#update_monavis', as: :update_monavis
get 'admin/procedures/:id/jeton' => 'new_administrateur/procedures#jeton', as: :admin_procedure_jeton
patch 'admin/procedures/:id/jeton' => 'new_administrateur/procedures#update_jeton', as: :update_jeton
namespace :admin do namespace :admin do
get 'activate' => '/administrateurs/activate#new' get 'activate' => '/administrateurs/activate#new'

View file

@ -0,0 +1,5 @@
class AddAPIEntrepriseTokenToProcedures < ActiveRecord::Migration[5.2]
def change
add_column :procedures, :api_entreprise_token, :string
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_04_22_090426) do ActiveRecord::Schema.define(version: 2020_04_23_171759) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -515,6 +515,7 @@ ActiveRecord::Schema.define(version: 2020_04_22_090426) do
t.datetime "closed_at" t.datetime "closed_at"
t.datetime "unpublished_at" t.datetime "unpublished_at"
t.bigint "canonical_procedure_id" t.bigint "canonical_procedure_id"
t.string "api_entreprise_token"
t.index ["declarative_with_state"], name: "index_procedures_on_declarative_with_state" t.index ["declarative_with_state"], name: "index_procedures_on_declarative_with_state"
t.index ["hidden_at"], name: "index_procedures_on_hidden_at" t.index ["hidden_at"], name: "index_procedures_on_hidden_at"
t.index ["parent_procedure_id"], name: "index_procedures_on_parent_procedure_id" t.index ["parent_procedure_id"], name: "index_procedures_on_parent_procedure_id"

View file

@ -301,4 +301,21 @@ describe NewAdministrateur::ProceduresController, type: :controller do
end end
end end
end end
describe 'GET #jeton' do
let(:procedure) { create(:procedure, administrateur: admin) }
subject { get :jeton, params: { id: procedure.id } }
it { is_expected.to have_http_status(:success) }
end
describe 'PATCH #jeton' do
let(:procedure) { create(:procedure, administrateur: admin) }
it "update api_entreprise_token" do
patch :update_jeton, params: { id: procedure.id, procedure: { api_entreprise_token: 'ceci-est-un-jeton' } }
expect(procedure.reload.api_entreprise_token).to eq('ceci-est-un-jeton')
end
end
end end

View file

@ -1,11 +1,13 @@
describe ApiEntreprise::API do describe ApiEntreprise::API do
let(:procedure_id) { 12 } let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:token) { Rails.application.secrets.api_entreprise[:key] }
describe '.entreprise' do describe '.entreprise' do
subject { described_class.entreprise(siren, procedure_id) } subject { described_class.entreprise(siren, procedure_id) }
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/)
.to_return(status: status, body: body) .to_return(status: status, body: body)
end end
@ -47,6 +49,27 @@ describe ApiEntreprise::API do
it 'returns response body' do it 'returns response body' do
expect(subject).to eq(JSON.parse(body, symbolize_names: true)) expect(subject).to eq(JSON.parse(body, symbolize_names: true))
end end
context 'with specific token for procedure' do
let(:token) { 'token-for-demarche' }
let(:procedure) { create(:procedure, api_entreprise_token: token) }
let(:procedure_id) { procedure.id }
it 'call api-entreprise with specfic token' do
subject
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=token-for-demarche/)
end
end
context 'without specific token for procedure' do
let(:procedure) { create(:procedure, api_entreprise_token: nil) }
let(:procedure_id) { procedure.id }
it 'call api-entreprise with specfic token' do
subject
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/entreprises\/#{siren}?.*token=#{token}/)
end
end
end end
end end

View file

@ -1,6 +1,7 @@
describe ApiEntreprise::EffectifsAdapter do describe ApiEntreprise::EffectifsAdapter do
let(:siren) { '418166096' } let(:siren) { '418166096' }
let(:procedure_id) { 22 } let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:annee) { "2020" } let(:annee) { "2020" }
let(:mois) { "02" } let(:mois) { "02" }
let(:adapter) { described_class.new(siren, procedure_id, annee, mois) } let(:adapter) { described_class.new(siren, procedure_id, annee, mois) }

View file

@ -1,6 +1,7 @@
describe ApiEntreprise::EffectifsAnnuelsAdapter do describe ApiEntreprise::EffectifsAnnuelsAdapter do
let(:siren) { '418166096' } let(:siren) { '418166096' }
let(:procedure_id) { 22 } let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:adapter) { described_class.new(siren, procedure_id) } let(:adapter) { described_class.new(siren, procedure_id) }
subject { adapter.to_params } subject { adapter.to_params }

View file

@ -1,6 +1,7 @@
describe ApiEntreprise::EntrepriseAdapter do describe ApiEntreprise::EntrepriseAdapter do
let(:siren) { '418166096' } let(:siren) { '418166096' }
let(:procedure_id) { 22 } let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:adapter) { described_class.new(siren, procedure_id) } let(:adapter) { described_class.new(siren, procedure_id) }
subject { adapter.to_params } subject { adapter.to_params }

View file

@ -1,5 +1,6 @@
describe ApiEntreprise::EtablissementAdapter do describe ApiEntreprise::EtablissementAdapter do
let(:procedure_id) { 33 } let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
context 'SIRET valide avec infos diffusables' do context 'SIRET valide avec infos diffusables' do
let(:siret) { '41816609600051' } let(:siret) { '41816609600051' }
@ -88,7 +89,7 @@ describe ApiEntreprise::EtablissementAdapter do
context 'when siret is not found' do context 'when siret is not found' do
let(:bad_siret) { 11_111_111_111_111 } let(:bad_siret) { 11_111_111_111_111 }
subject { described_class.new(bad_siret, 12).to_params } subject { described_class.new(bad_siret, procedure_id).to_params }
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{bad_siret}?.*token=/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/etablissements\/#{bad_siret}?.*token=/)

View file

@ -1,7 +1,7 @@
describe ApiEntreprise::ExercicesAdapter do describe ApiEntreprise::ExercicesAdapter do
let(:siret) { '41816609600051' } let(:siret) { '41816609600051' }
let(:procedure_id) { 11 } let(:procedure) { create(:procedure) }
subject { described_class.new(siret, procedure_id).to_params } subject { described_class.new(siret, procedure.id).to_params }
before do before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/.*token=/) stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/exercices\/.*token=/)

View file

@ -1,6 +1,7 @@
describe ApiEntreprise::RNAAdapter do describe ApiEntreprise::RNAAdapter do
let(:siret) { '50480511000013' } let(:siret) { '50480511000013' }
let(:procedure_id) { 22 } let(:procedure) { create(:procedure) }
let(:procedure_id) { procedure.id }
let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') } let(:body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
let(:status) { 200 } let(:status) { 200 }
let(:adapter) { described_class.new(siret, procedure_id) } let(:adapter) { described_class.new(siret, procedure_id) }

View file

@ -44,7 +44,8 @@ describe ApiEntrepriseService do
let(:associations_status) { 200 } let(:associations_status) { 200 }
let(:associations_body) { File.read('spec/fixtures/files/api_entreprise/associations.json') } let(:associations_body) { File.read('spec/fixtures/files/api_entreprise/associations.json') }
let(:result) { ApiEntrepriseService.get_etablissement_params_for_siret(siret, '1') } let(:procedure) { create(:procedure) }
let(:result) { ApiEntrepriseService.get_etablissement_params_for_siret(siret, procedure.id) }
context 'when service is up' do context 'when service is up' do
it 'should fetch etablissement params' do it 'should fetch etablissement params' do