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
2 changes: 1 addition & 1 deletion site/app/clients/hubee_api_authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class HubEEAPIAuthentication < AbstractHubEEAPIClient
def access_token
http_connection.post(
auth_url,
'grant_type=client_credentials&scope=ADMIN',
'grant_type=client_credentials&scope=DATAPASS',
{
'Authorization' => "Basic #{encoded_client_id_and_secret}"
}
Expand Down
36 changes: 4 additions & 32 deletions site/app/clients/hubee_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def create_organization(organization, email)
raise
end

def create_subscription(authorization_request, organization_payload, process_code, editor_payload = {})
subscription_payload = find_or_create_inactive_subscription(authorization_request, organization_payload, process_code)
activate_subscription(subscription_payload, editor_payload)
subscription_payload
def create_subscription(authorization_request, organization_payload, process_code)
create_inactive_subscription(authorization_request, organization_payload, process_code)
rescue AlreadyExists
find_subscription(authorization_request, organization_payload, process_code)
end

def find_subscription(_authorization_request, organization_payload, process_code)
Expand Down Expand Up @@ -73,28 +73,6 @@ def http_connection(&block)

private

def activate_subscription(subscription_payload, editor_payload = {}) # rubocop:disable Metrics/AbcSize
subscription_id = Hash(subscription_payload)['id']
return if subscription_id.blank?

payload = subscription_payload.with_indifferent_access.merge({
status: 'Actif',
activateDateTime: DateTime.now.iso8601,
accessMode: 'API',
notificationFrequency: 'Aucune'
}.with_indifferent_access)

payload.delete('id')
payload.delete('creationDateTime')
payload.merge!(editor_payload.with_indifferent_access)

http_connection.put(
"#{host}/referential/v1/subscriptions/#{subscription_id}",
payload.to_json,
'Content-Type' => 'application/json'
).body
end

def create_inactive_subscription(authorization_request, organization_payload, process_code) # rubocop:disable Metrics/AbcSize
http_connection.post(
"#{host}/referential/v1/subscriptions",
Expand Down Expand Up @@ -124,10 +102,4 @@ def create_inactive_subscription(authorization_request, organization_payload, pr

raise
end

def find_or_create_inactive_subscription(authorization_request, organization_payload, process_code)
create_inactive_subscription(authorization_request, organization_payload, process_code)
rescue HubEEAPIClient::AlreadyExists
find_subscription(authorization_request, organization_payload, process_code)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,7 @@ def call
private

def create_subscription_on_hubee
hubee_api_client.create_subscription(authorization_request, hubee_organization_payload, process_code, editor_payload)
end

def editor_organization
@editor_organization ||= begin
organization = Organization.find_or_create_by(siret: service_provider['siret'])

UpdateOrganizationINSEEPayloadJob.new.perform(organization.id)
organization.reload

organization
end
end

def editor_payload
return {} unless editor_subscription?

{
delegationActor: {
branchCode: editor_organization.code_commune_etablissement,
companyRegister: editor_organization.siret,
type: 'EDT'
},
accessMode: 'API'
}
end

def editor_subscription?
service_provider['type'] == 'editor'
hubee_api_client.create_subscription(authorization_request, hubee_organization_payload, process_code)
end

def hubee_api_client
Expand All @@ -52,8 +24,4 @@ def save_hubee_subscription_id_to_authorization_request
authorization_request.extra_infos['hubee_subscription_id'] = hubee_subscription_payload['id']
authorization_request.save!
end

def service_provider
@service_provider ||= Hash(authorization_request.extra_infos['service_provider'])
end
end
21 changes: 21 additions & 0 deletions site/spec/clients/hubee_api_authentication_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
RSpec.describe HubEEAPIAuthentication do
describe '#access_token' do
subject(:access_token) { described_class.new.access_token }

let(:auth_url) { Rails.application.credentials.hubee_auth_url }

before do
stub_request(:post, auth_url)
.with(body: 'grant_type=client_credentials&scope=DATAPASS')
.to_return(
status: 200,
headers: { 'Content-Type' => 'application/json' },
body: { access_token: 'hubee_access_token' }.to_json
)
end

it 'requests a token with the datapass scope' do
expect(access_token).to eq('hubee_access_token')
end
end
end
57 changes: 57 additions & 0 deletions site/spec/clients/hubee_api_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,61 @@
end
end
end

describe '#create_subscription' do
subject(:create_subscription) { described_class.new.create_subscription(authorization_request, organization_payload, process_code) }

let(:authorization_request) { create(:authorization_request, :with_demandeur, api: 'particulier') }
let(:organization_payload) { hubee_organization_payload }
let(:process_code) { 'FormulaireQF' }
let(:host) { Rails.application.credentials.hubee_api_url }
let(:subscription_payload) { hubee_subscription_payload(authorization_request:, process_code:) }

context 'when the subscription does not exist yet' do
before do
stub_request(:post, "#{host}/referential/v1/subscriptions").to_return(
status: 201,
headers: { 'Content-Type' => 'application/json' },
body: subscription_payload.to_json
)
end

it 'returns the created subscription payload' do
expect(create_subscription).to eq(subscription_payload)
end

it 'does not activate the subscription' do
create_subscription

expect(a_request(:put, %r{#{host}/referential/v1/subscriptions/})).not_to have_been_made
end
end

context 'when the subscription already exists' do
before do
stub_request(:post, "#{host}/referential/v1/subscriptions").to_return(
status: 400,
headers: { 'Content-Type' => 'application/json' },
body: { 'errors' => [{ 'message' => 'Subscription already exists' }] }.to_json
)
stub_request(:get, "#{host}/referential/v1/subscriptions")
.with(query: { companyRegister: organization_payload['companyRegister'], processCode: process_code })
.to_return(
status: 200,
headers: { 'Content-Type' => 'application/json' },
body: [subscription_payload].to_json
)
end

it 'returns the existing subscription payload' do
expect(create_subscription).to eq(subscription_payload)
end

it 'does not activate the subscription' do
create_subscription

expect(a_request(:put, %r{#{host}/referential/v1/subscriptions/})).not_to have_been_made
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,16 @@
allow(hubee_api_client).to receive(:create_subscription).and_return(stripped_hubee_subscription_payload)
end

context 'when the authorization request has a service provider' do
let(:authorization_request) { create(:authorization_request, extra_infos: { 'service_provider' => service_provider }) }
let(:editor_siret) { '13002526500013' }
let(:service_provider) { { 'type' => 'editor', 'siret' => editor_siret } }

context 'when the service provider is an editor' do
let(:insee_api_authentication) { instance_double(INSEEAPIAuthentication, access_token: 'access_token') }
let(:insee_payload) { insee_sirene_api_etablissement_valid_payload(siret: editor_siret, full: true) }

before do
allow(INSEEAPIAuthentication).to receive(:new).and_return(insee_api_authentication)
stub_request(:get, "https://api.insee.fr/api-sirene/prive/3.11/siret/#{editor_siret}").to_return(
status: 200,
headers: { 'Content-Type' => 'application/json' },
body: insee_payload.to_json
)
end

it 'creates a subscription on HubEE with the editor payload' do
expect(hubee_api_client).to receive(:create_subscription).with(authorization_request, stripped_hubee_organization_payload, 'FormulaireQF', { delegationActor: { branchCode: '75107', companyRegister: '13002526500013', type: 'EDT' }, accessMode: 'API' })
interactor
end
end

context 'when the service provider is not an editor' do
let(:service_provider) { { 'type' => 'service', 'siret' => '123456' } }

it 'creates a subscription on HubEE without the editor payload' do
expect(hubee_api_client).to receive(:create_subscription).with(authorization_request, stripped_hubee_organization_payload, 'FormulaireQF', {})
interactor
end
end
it 'creates a subscription on HubEE' do
expect(hubee_api_client).to receive(:create_subscription).with(authorization_request, stripped_hubee_organization_payload, 'FormulaireQF')
interactor
end

context 'when the authorization request does not have a service provider' do
it 'creates a subscription on HubEE' do
expect(hubee_api_client).to receive(:create_subscription).with(authorization_request, stripped_hubee_organization_payload, 'FormulaireQF', {})
context 'when the service provider is an editor' do
let(:authorization_request) { create(:authorization_request, extra_infos: { 'service_provider' => { 'type' => 'editor', 'siret' => '13002526500013' } }) }

it 'creates the subscription without any editor delegation' do
expect(hubee_api_client).to receive(:create_subscription).with(authorization_request, stripped_hubee_organization_payload, 'FormulaireQF')
interactor
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
subject(:interactor) { described_class.call(authorization_request:) }

let(:hubee_api_client) { instance_double(HubEEAPIClient) }
let(:insee_api_authentication) { instance_double(INSEEAPIAuthentication, access_token: 'access_token') }
let(:insee_payload) { insee_sirene_api_etablissement_valid_payload(siret: editor_siret, full: true) }
let(:authorization_request) { create(:authorization_request, :with_demandeur, api: 'particulier') }
let(:subscription_payload) { hubee_subscription_payload(authorization_request:) }
let(:editor_siret) { '13002526500013' }
let(:service_provider) { { 'type' => 'editor', 'siret' => editor_siret } }
let(:formulaire_qf_api_client) { instance_double(FormulaireQFAPIClient) }
let(:siret) { '12345678901234' }
let(:code_commune) { '12345' }
Expand All @@ -19,12 +15,6 @@
before do
allow(HubEEAPIClient).to receive(:new).and_return(hubee_api_client)
allow(hubee_api_client).to receive_messages(find_or_create_organization: organization_payload, create_subscription: subscription_payload)
allow(INSEEAPIAuthentication).to receive(:new).and_return(insee_api_authentication)
stub_request(:get, "https://api.insee.fr/entreprises/sirene/V3.11/siret/#{editor_siret}").to_return(
status: 200,
headers: { 'Content-Type' => 'application/json' },
body: insee_payload.to_json
)
allow(FormulaireQFAPIClient).to receive(:new).and_return(formulaire_qf_api_client)
allow(formulaire_qf_api_client).to receive(:create_collectivity)
end
Expand Down
4 changes: 2 additions & 2 deletions site/spec/support/hubee_api_mocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def hubee_subscription_payload(authorization_request:, organization_payload: hub
{
'id' => SecureRandom.uuid,
'datapassId' => authorization_request.external_id.to_i,
'notificationFrequency' => 'unitaire',
'notificationFrequency' => 'Aucune',
'processCode' => process_code,
'email' => authorization_request.demandeur.email,
'localAdministrator' => {
'email' => authorization_request.demandeur.email,
'firstName' => authorization_request.demandeur.first_name,
'lastName' => authorization_request.demandeur.last_name
},
'status' => 'Actif',
'status' => 'Inactif',
'subscriber' => {
'branchCode' => organization_payload['branchCode'],
'companyRegister' => organization_payload['companyRegister'],
Expand Down
26 changes: 9 additions & 17 deletions site/spec/support/insee_sirene_api_mocks.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
# frozen_string_literal: true

module INSEESireneAPIMocks
def insee_sirene_api_etablissement_valid_payload(siret:, full: false)
if full
read_json_fixture("insee/#{siret}.json")
else
{
'header' => {
'statut' => 200,
'message' => 'OK'
},
'etablissement' => {
'siren' => siret.first(9)
}
def insee_sirene_api_etablissement_valid_payload(siret:)
{
'header' => {
'statut' => 200,
'message' => 'OK'
},
'etablissement' => {
'siren' => siret.first(9)
}
end
}
end

def insee_sirene_api_not_found_payload
Expand All @@ -25,8 +21,4 @@ def insee_sirene_api_not_found_payload
}
}
end

def read_json_fixture(file)
JSON.parse(Rails.root.join('spec', 'fixtures', file).read)
end
end