Add tests for prefill identity

This commit is contained in:
simon lehericey 2023-09-07 10:10:07 +02:00
parent be02dd965f
commit d319385d17
11 changed files with 93 additions and 59 deletions

View file

@ -47,11 +47,16 @@ RSpec.describe API::Public::V1::DossiersController, type: :controller do
let!(:type_de_champ_2) { create(:type_de_champ_textarea, procedure: procedure) }
let(:value_2) { "another value" }
let(:prenom_value) { "John" }
let(:genre_value) { "M." }
let(:params) {
{
id: procedure.id,
"champ_#{type_de_champ_1.to_typed_id_for_query}" => value_1,
"champ_#{type_de_champ_2.to_typed_id_for_query}" => value_2
"champ_#{type_de_champ_2.to_typed_id_for_query}" => value_2,
"identite_prenom" => prenom_value,
"identite_genre" => genre_value
}
}
@ -61,6 +66,8 @@ RSpec.describe API::Public::V1::DossiersController, type: :controller do
dossier = Dossier.last
expect(find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).value).to eq(value_1)
expect(find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).value).to eq(value_2)
expect(dossier.individual.prenom).to eq(prenom_value)
expect(dossier.individual.gender).to eq(genre_value)
end
end
end
@ -92,19 +99,19 @@ RSpec.describe API::Public::V1::DossiersController, type: :controller do
context 'when the procedure is found' do
context 'when the procedure is publiee' do
it_behaves_like 'the procedure is found' do
let(:procedure) { create(:procedure, :published) }
let(:procedure) { create(:procedure, :for_individual, :published) }
end
end
context 'when the procedure is brouillon' do
it_behaves_like 'the procedure is found' do
let(:procedure) { create(:procedure, :draft) }
let(:procedure) { create(:procedure, :for_individual, :draft) }
end
end
context 'when the procedure is not publiee and not brouillon' do
it_behaves_like 'the procedure is not found' do
let(:procedure) { create(:procedure, :closed) }
let(:procedure) { create(:procedure, :for_individual, :closed) }
end
end
end

View file

@ -50,16 +50,32 @@ describe PrefillDescriptionsController, type: :controller do
describe "#update" do
render_views
let(:procedure) { create(:procedure, :published, opendata: true) }
let(:procedure) { create(:procedure, :for_individual, :published, opendata: true) }
let(:type_de_champ) { create(:type_de_champ_text, procedure: procedure) }
let(:type_de_champ2) { create(:type_de_champ_text, procedure: procedure) }
subject(:update_request) do
patch :update, params: { path: procedure.path, type_de_champ: params }, format: :turbo_stream
patch :update, params: { path: procedure.path, procedure: params }, format: :turbo_stream
end
before { update_request }
context 'when adding identity information' do
let(:params) { { identity_items_selected: "prenom" } }
it { expect(response).to render_template(:update) }
it "includes the prefill URL" do
expect(response.body).to include(commencer_path(path: procedure.path))
expect(response.body).to include("identite_prenom=#{I18n.t("views.prefill_descriptions.edit.examples.prenom")}")
end
it "includes the prefill query" do
expect(response.body).to include(api_public_v1_dossiers_path(procedure))
expect(response.body).to include(""identite_prenom":"#{I18n.t("views.prefill_descriptions.edit.examples.prenom")}"")
end
end
context 'when adding a type_de_champ_id' do
let(:type_de_champ_to_add) { create(:type_de_champ_text, procedure: procedure) }
let(:params) { { selected_type_de_champ_ids: [type_de_champ.id, type_de_champ_to_add.id].join(' ') } }
@ -113,7 +129,6 @@ describe PrefillDescriptionsController, type: :controller do
end
context 'when removing the last type de champ' do
let(:type_de_champ_to_remove) { type_de_champ }
let(:params) { { selected_type_de_champ_ids: '' } }
it { expect(response).to render_template(:update) }

View file

@ -1,6 +1,6 @@
describe Users::CommencerController, type: :controller do
let(:user) { create(:user) }
let(:published_procedure) { create(:procedure, :published) }
let(:published_procedure) { create(:procedure, :for_individual, :published) }
let(:draft_procedure) { create(:procedure, :with_path) }
describe '#commencer' do
@ -151,15 +151,16 @@ describe Users::CommencerController, type: :controller do
let(:user) { create(:user) }
context "when the dossier does not exists yet" do
subject { get :commencer, params: { path: path, "champ_#{type_de_champ_text.to_typed_id}" => "blabla" } }
subject { get :commencer, params: { path: path, "champ_#{type_de_champ_text.to_typed_id}" => "blabla", "identite_nom" => "Dupont" } }
shared_examples 'a prefilled brouillon dossier creator' do
it 'creates a dossier' do
subject
expect(Dossier.count).to eq(1)
expect(session[:prefill_token]).to eq(Dossier.last.prefill_token)
expect(session[:prefill_params_digest]).to eq(PrefillParams.digest({ "champ_#{type_de_champ_text.to_typed_id}" => "blabla" }))
expect(session[:prefill_params_digest]).to eq(PrefillChamps.digest({ "champ_#{type_de_champ_text.to_typed_id}" => "blabla" }))
expect(Dossier.last.champs.where(type_de_champ: type_de_champ_text).first.value).to eq("blabla")
expect(Dossier.last.individual.nom).to eq("Dupont")
end
end
@ -208,7 +209,7 @@ describe Users::CommencerController, type: :controller do
before do
session[:prefill_token] = "token"
session[:prefill_params_digest] = PrefillParams.digest({ "champ_#{type_de_champ_text.to_typed_id}" => "blabla" })
session[:prefill_params_digest] = PrefillChamps.digest({ "champ_#{type_de_champ_text.to_typed_id}" => "blabla" })
end
context "when the associated dossier exists" do

View file

@ -2,8 +2,8 @@
RSpec.describe DossierPrefillableConcern do
describe '.prefill!' do
let(:procedure) { create(:procedure, :published, types_de_champ_public:, types_de_champ_private:) }
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
let(:procedure) { create(:procedure, :published, :for_individual, types_de_champ_public:, types_de_champ_private:) }
let(:dossier) { create(:dossier, :brouillon, :with_individual, procedure: procedure) }
let(:types_de_champ_public) { [] }
let(:types_de_champ_private) { [] }
let(:identity_attributes) { {} }
@ -101,22 +101,6 @@ RSpec.describe DossierPrefillableConcern do
end
end
context "when dossier is for entreprise" do
let(:procedure) { create(:procedure, :published, types_de_champ_public:) }
let(:dossier) { create(:dossier, :brouillon, :with_entreprise, procedure: procedure) }
context "when identity_attributes is present" do
let(:identity_attributes) { { "siret" => "50000123456789", id: dossier.user.id } }
it_behaves_like 'a dossier marked as prefilled'
it "updates the dossier user" do
fill
expect(dossier.user.siret).to eq("50000123456789")
end
end
end
private
def find_champ_by_stable_id(dossier, stable_id)

View file

@ -51,10 +51,12 @@ RSpec.describe PrefillDescription, type: :model do
describe '#include?' do
let(:prefill_description) { described_class.new(build(:procedure)) }
let(:type_de_champ_id) { 1 }
subject(:included) { prefill_description.include?(type_de_champ_id) }
context 'when the id has been added to the prefill_description' do
context "type_de_champ id" do
let(:type_de_champ_id) { 1 }
subject(:included) { prefill_description.include?(type_de_champ_id.to_s) }
context 'when the id of a type_de_champ has been added to the prefill_description' do
before { prefill_description.update(selected_type_de_champ_ids: '1') }
it { expect(included).to eq(true) }
@ -65,6 +67,21 @@ RSpec.describe PrefillDescription, type: :model do
end
end
context "identity information" do
subject(:included) { prefill_description.include?('prenom') }
context 'when the first_name has been added to the prefill_description' do
before { prefill_description.update(identity_items_selected: 'prenom') }
it { expect(included).to eq(true) }
end
context 'when the first_name has not been added to the prefill_description' do
it { expect(included).to eq(false) }
end
end
end
describe '#link_too_long?' do
let(:procedure) { create(:procedure) }
let(:prefill_description) { described_class.new(procedure) }
@ -96,7 +113,7 @@ RSpec.describe PrefillDescription, type: :model do
let(:prefill_description) { described_class.new(procedure) }
before do
prefill_description.update(selected_type_de_champ_ids: [type_de_champ_text.id, type_de_champ_epci.id, type_de_champ_repetition.id])
prefill_description.update(selected_type_de_champ_ids: [type_de_champ_text.id, type_de_champ_epci.id, type_de_champ_repetition.id], identity_items_selected: "prenom")
end
it "builds the URL to create a new prefilled dossier" do
@ -106,7 +123,8 @@ RSpec.describe PrefillDescription, type: :model do
path: procedure.path,
"champ_#{type_de_champ_text.to_typed_id_for_query}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_text, procedure.active_revision).example_value,
"champ_#{type_de_champ_epci.to_typed_id_for_query}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_epci, procedure.active_revision).example_value,
"champ_#{type_de_champ_repetition.to_typed_id_for_query}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_repetition, procedure.active_revision).example_value
"champ_#{type_de_champ_repetition.to_typed_id_for_query}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_repetition, procedure.active_revision).example_value,
"identite_prenom" => I18n.t("views.prefill_descriptions.edit.examples.prenom")
)
)
)
@ -127,12 +145,12 @@ RSpec.describe PrefillDescription, type: :model do
<<~TEXT
curl --request POST '#{api_public_v1_dossiers_url(procedure)}' \\
--header 'Content-Type: application/json' \\
--data '{"champ_#{type_de_champ_text.to_typed_id_for_query}":"Texte court","champ_#{type_de_champ_epci.to_typed_id_for_query}":["01","200042935"],"champ_#{type_de_champ_repetition.to_typed_id_for_query}":[{"champ_#{text_repetition.to_typed_id_for_query}":"Texte court","champ_#{integer_repetition.to_typed_id_for_query}":"42","champ_#{region_repetition.to_typed_id_for_query}":"53"},{"champ_#{text_repetition.to_typed_id_for_query}":"Texte court","champ_#{integer_repetition.to_typed_id_for_query}":"42","champ_#{region_repetition.to_typed_id_for_query}":"53"}]}'
--data '{"identite_prenom":"#{I18n.t("views.prefill_descriptions.edit.examples.prenom")}","champ_#{type_de_champ_text.to_typed_id_for_query}":"Texte court","champ_#{type_de_champ_epci.to_typed_id_for_query}":["01","200042935"],"champ_#{type_de_champ_repetition.to_typed_id_for_query}":[{"champ_#{text_repetition.to_typed_id_for_query}":"Texte court","champ_#{integer_repetition.to_typed_id_for_query}":"42","champ_#{region_repetition.to_typed_id_for_query}":"53"},{"champ_#{text_repetition.to_typed_id_for_query}":"Texte court","champ_#{integer_repetition.to_typed_id_for_query}":"42","champ_#{region_repetition.to_typed_id_for_query}":"53"}]}'
TEXT
end
before do
prefill_description.update(selected_type_de_champ_ids: [type_de_champ_text.id, type_de_champ_epci.id, type_de_champ_repetition.id])
prefill_description.update(selected_type_de_champ_ids: [type_de_champ_text.id, type_de_champ_epci.id, type_de_champ_repetition.id], identity_items_selected: "prenom")
end
it "builds the query to create a new prefilled dossier" do

View file

@ -9,7 +9,7 @@ RSpec.describe PrefillIdentity do
{
"identite_prenom" => "Prénom",
"identite_nom" => "Nom",
"identite_genre" => "Mme",
"identite_genre" => "Mme"
}
}
@ -23,7 +23,7 @@ RSpec.describe PrefillIdentity do
{
"identite_prenom" => "Prénom",
"identite_nom" => "Nom",
"identite_genre" => "error",
"identite_genre" => "error"
}
}

View file

@ -2,13 +2,10 @@ shared_examples "the user has got a prefilled dossier, owned by themselves" do
scenario "the user has got a prefilled dossier, owned by themselves" do
expect(dossier.user).to eq(user)
expect(page).to have_current_path siret_dossier_path(procedure.dossiers.last)
fill_in 'Numéro SIRET', with: siret_value
click_on 'Valider'
expect(page).to have_current_path(etablissement_dossier_path(dossier))
expect(page).to have_content('DIRECTION INTERMINISTERIELLE DU NUMERIQUE')
click_on 'Continuer avec ces informations'
expect(page).to have_current_path identite_dossier_path(procedure.dossiers.last)
expect(page).to have_field('Prénom', with: prenom_value)
expect(page).to have_field('Nom', with: nom_value)
click_on 'Continuer'
expect(page).to have_current_path(brouillon_dossier_path(dossier))
expect(page).to have_field(type_de_champ_text.libelle, with: text_value)

View file

@ -1,10 +1,11 @@
describe 'As an integrator:', js: true, retry: 3 do
let(:procedure) { create(:procedure, :published, opendata: true) }
let(:procedure) { create(:procedure, :for_individual, :published, opendata: true) }
let!(:type_de_champ) { create(:type_de_champ_text, procedure: procedure) }
before { visit "/preremplir/#{procedure.path}" }
scenario 'I can read the procedure prefilling (aka public champs)' do
expect(page).to have_content(I18n.t("views.prefill_descriptions.edit.title.nom"))
expect(page).to have_content(type_de_champ.to_typed_id_for_query)
expect(page).to have_content(I18n.t("activerecord.attributes.type_de_champ.type_champs.#{type_de_champ.type_champ}"))
expect(page).to have_content(type_de_champ.libelle)
@ -12,8 +13,7 @@ describe 'As an integrator:', js: true, retry: 3 do
end
scenario 'I can select champs to prefill and get prefill link and prefill query' do
click_on 'Ajouter'
page.find_by_id("#{type_de_champ.id}_add_button", match: :first).click
prefill_description = PrefillDescription.new(procedure)
prefill_description.update(selected_type_de_champ_ids: [type_de_champ.id.to_s])
expect(page).to have_content(prefill_description.prefill_link)

View file

@ -1,7 +1,7 @@
describe 'Prefilling a dossier (with a GET request):', js: true, retry: 3 do
let(:password) { SECURE_PASSWORD }
let(:procedure) { create(:procedure, :published, opendata: true) }
let(:procedure) { create(:procedure, :for_individual, :published, opendata: true) }
let(:dossier) { procedure.dossiers.last }
let(:type_de_champ_text) { create(:type_de_champ_text, procedure: procedure) }
@ -39,6 +39,9 @@ describe 'Prefilling a dossier (with a GET request):', js: true, retry: 3 do
let(:text_repetition_value) { "First repetition text" }
let(:integer_repetition_value) { "42" }
let(:annuaire_education_value) { '0050009H' }
let(:prenom_value) { 'Jean' }
let(:nom_value) { 'Dupont' }
let(:genre_value) { 'M.' }
let(:entry_path) {
commencer_path(
@ -59,7 +62,10 @@ describe 'Prefilling a dossier (with a GET request):', js: true, retry: 3 do
"champ_#{sub_type_de_champs_repetition.second.to_typed_id_for_query}": integer_repetition_value
}
],
"champ_#{type_de_champ_annuaire_education.to_typed_id_for_query}" => annuaire_education_value
"champ_#{type_de_champ_annuaire_education.to_typed_id_for_query}" => annuaire_education_value,
"identite_prenom" => prenom_value,
"identite_nom" => nom_value,
"identite_genre" => genre_value
)
}
@ -96,7 +102,7 @@ describe 'Prefilling a dossier (with a GET request):', js: true, retry: 3 do
create(:champ_text, dossier: dossier, type_de_champ: type_de_champ_text, value: text_value)
page.set_rack_session(prefill_token: "token")
page.set_rack_session(prefill_params_digest: PrefillParams.digest({ "champ_#{type_de_champ_text.to_typed_id}" => text_value }))
page.set_rack_session(prefill_params_digest: PrefillChamps.digest({ "champ_#{type_de_champ_text.to_typed_id}" => text_value }))
visit "/users/sign_in"
sign_in_with user.email, password

View file

@ -1,7 +1,7 @@
describe 'Prefilling a dossier (with a POST request):', js: true, retry: 3 do
let(:password) { SECURE_PASSWORD }
let(:procedure) { create(:procedure, :published) }
let(:procedure) { create(:procedure, :for_individual, :published) }
let(:dossier) { procedure.dossiers.last }
let(:type_de_champ_text) { create(:type_de_champ_text, procedure: procedure) }
@ -39,6 +39,9 @@ describe 'Prefilling a dossier (with a POST request):', js: true, retry: 3 do
let(:integer_repetition_value) { "42" }
let(:dossier_link_value) { '42' }
let(:annuaire_education_value) { '0050009H' }
let(:prenom_value) { 'Jean' }
let(:nom_value) { 'Dupont' }
let(:genre_value) { 'M.' }
before do
stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/etablissements\/#{siret_value}/)
@ -152,7 +155,10 @@ describe 'Prefilling a dossier (with a POST request):', js: true, retry: 3 do
"champ_#{type_de_champ_dossier_link.to_typed_id_for_query}" => dossier_link_value,
"champ_#{type_de_champ_commune.to_typed_id_for_query}" => commune_value,
"champ_#{type_de_champ_address.to_typed_id_for_query}" => address_value,
"champ_#{type_de_champ_annuaire_education.to_typed_id_for_query}" => annuaire_education_value
"champ_#{type_de_champ_annuaire_education.to_typed_id_for_query}" => annuaire_education_value,
"identite_prenom" => prenom_value,
"identite_nom" => nom_value,
"identite_genre" => genre_value
}.to_json
JSON.parse(session.response.body)["dossier_url"].gsub("http://www.example.com", "")
end

View file

@ -1,8 +1,8 @@
describe 'prefill_descriptions/types_de_champs', type: :view do
describe 'prefill_descriptions/prefillable_entities.html.haml', type: :view do
let(:prefill_description) { PrefillDescription.new(create(:procedure)) }
let!(:type_de_champ) { create(:type_de_champ_drop_down_list, procedure: prefill_description, drop_down_options: options) }
subject { render('prefill_descriptions/types_de_champs', prefill_description: prefill_description) }
subject { render('prefill_descriptions/prefillable_entities', prefill_description: prefill_description) }
context 'when a type de champ has too many values' do
let(:options) { (1..20).map(&:to_s) }