Skip to content

Commit

Permalink
concern for modalities
Browse files Browse the repository at this point in the history
  • Loading branch information
JeSuisUnCaillou committed Nov 29, 2024
1 parent 980da32 commit 22b34c8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
5 changes: 1 addition & 4 deletions app/models/authorization_request/api_impot_particulier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ class AuthorizationRequest::APIImpotParticulier < AuthorizationRequest
include AuthorizationExtensions::OperationalAcceptance
include AuthorizationExtensions::SafetyCertification
include AuthorizationExtensions::Volumetrie
include AuthorizationExtensions::Modalities

MODALITIES = %i[with_france_connect with_spi with_etat_civil].freeze

include DGFIPExtensions::APIImpotParticulierScopes
include DGFIPExtensions::APIImpotParticulierModalities

VOLUMETRIES = {
'50 appels / minute': 50,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ class AuthorizationRequest::APIImpotParticulierSandbox < AuthorizationRequest
include AuthorizationExtensions::PersonalData
include AuthorizationExtensions::CadreJuridique
include AuthorizationExtensions::GDPRContacts
include AuthorizationExtensions::Modalities

MODALITIES = %i[with_france_connect with_spi with_etat_civil].freeze

include DGFIPExtensions::APIImpotParticulierScopes
include DGFIPExtensions::APIImpotParticulierModalities

add_document :maquette_projet, content_type: ['application/pdf'], size: { less_than: 10.megabytes }

Expand Down
24 changes: 15 additions & 9 deletions app/models/concerns/authorization_extensions/modalities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ module AuthorizationExtensions::Modalities

included do
add_attributes :modalities
add_attributes :france_connect_authorization_id
validates :modalities, presence: true, if: -> { need_complete_validation?(:modalities) }
validates :france_connect_authorization_id, presence: true, if: -> { modalities == 'with_france_connect' && need_complete_validation?(:modalities) }

validates :modalities,
presence: true,
if: -> { need_complete_validation?(:modalities) }

validate :modalities_in_available_values,
if: -> { need_complete_validation?(:modalities) }
end

def modalities_in_available_values
if modalities.is_a? Array
errors.add(:modalities) unless (modalities - available_modalities).empty?
elsif available_modalities.exclude?(modalities)
errors.add(:modalities)
end
end

def available_modalities
self.class::MODALITIES
rescue NameError
raise "Must declare a constant MODALITIES in the model #{self.class}, for example %w[with_france_connect with_spi]"
end

def associated_france_connect_authorization
return nil if france_connect_authorization_id.blank?

Authorization.find(france_connect_authorization_id)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module DGFIPExtensions::APIImpotParticulierModalities
extend ActiveSupport::Concern

MODALITIES = %w[with_france_connect with_spi with_etat_civil].freeze

included do
include AuthorizationExtensions::Modalities

add_attributes :france_connect_authorization_id
validates :france_connect_authorization_id,
presence: true,
if: -> { modalities == 'with_france_connect' && need_complete_validation?(:modalities) }
end

def associated_france_connect_authorization
return nil if france_connect_authorization_id.blank?

Authorization.find(france_connect_authorization_id)
end
end
42 changes: 42 additions & 0 deletions spec/models/authorization_request/api_impot_particulier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
:api_impot_particulier_editeur,
fill_all_attributes: true,
skip_scopes_build: true,
modalities:,
france_connect_authorization_id:,
scopes:,
specific_requirements:,
volumetrie_appels_par_minute:,
Expand All @@ -14,6 +16,8 @@
)
end

let(:modalities) { nil }
let(:france_connect_authorization_id) { nil }
let(:scopes) { [] }
let(:specific_requirements) { nil }
let(:volumetrie_appels_par_minute) { nil }
Expand Down Expand Up @@ -67,6 +71,44 @@
end
end

describe 'modalities validation' do
before { authorization_request.current_build_step = 'modalities' }

context 'with no value' do
before { authorization_request.modalities = nil }

it { is_expected.not_to be_valid }
end

context 'with bad value' do
let(:modalities) { 'bad_value' }

it { is_expected.not_to be_valid }
end

context 'with good value' do
let(:modalities) { 'with_spi' }

it { is_expected.to be_valid }
end

context 'with france connect' do
let(:modalities) { 'with_france_connect' }

context 'without any france connect authorization id' do
let(:france_connect_authorization_id) { nil }

it { is_expected.not_to be_valid }
end

context 'without a france connect authorization id' do
let(:france_connect_authorization_id) { 12 }

it { is_expected.to be_valid }
end
end
end

describe 'scopes validation' do
before { authorization_request.current_build_step = 'scopes' }

Expand Down

0 comments on commit 22b34c8

Please sign in to comment.