From 8aa31522b610de3f631faf052ee85f316335c222 Mon Sep 17 00:00:00 2001 From: sebastiencarceles Date: Mon, 6 Feb 2023 15:12:34 +0100 Subject: [PATCH] cover feature with specs --- spec/models/prefill_description_spec.rb | 40 ++++++++++++++++--- .../shared_examples_for_prefilled_dossier.rb | 1 + spec/system/users/dossier_prefill_get_spec.rb | 23 ++++++++++- .../system/users/dossier_prefill_post_spec.rb | 20 +++++++++- 4 files changed, 76 insertions(+), 8 deletions(-) diff --git a/spec/models/prefill_description_spec.rb b/spec/models/prefill_description_spec.rb index 5c5846965..6a1dc13be 100644 --- a/spec/models/prefill_description_spec.rb +++ b/spec/models/prefill_description_spec.rb @@ -103,13 +103,28 @@ RSpec.describe PrefillDescription, type: :model do end context 'when the type de champ can have multiple values' do - let(:type_de_champ) { create(:type_de_champ_multiple_drop_down_list, procedure: procedure) } + let(:type_de_champ) { TypesDeChamp::PrefillTypeDeChamp.build(create(:type_de_champ_epci, procedure: procedure)) } + + let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) } + + before do + allow(Rails).to receive(:cache).and_return(memory_store) + Rails.cache.clear + + VCR.insert_cassette('api_geo_departements') + VCR.insert_cassette('api_geo_epcis') + end + + after do + VCR.eject_cassette('api_geo_departements') + VCR.eject_cassette('api_geo_epcis') + end it 'builds the URL with array parameter' do expect(prefill_description.prefill_link).to eq( commencer_url( path: procedure.path, - "champ_#{type_de_champ.to_typed_id}": TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp.new(type_de_champ).example_value + "champ_#{type_de_champ.to_typed_id}": type_de_champ.example_value ) ) end @@ -128,19 +143,34 @@ RSpec.describe PrefillDescription, type: :model do TEXT end - before { prefill_description.update(selected_type_de_champ_ids: [type_de_champ.id]) } + let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) } + + before do + allow(Rails).to receive(:cache).and_return(memory_store) + Rails.cache.clear + + VCR.insert_cassette('api_geo_departements') + VCR.insert_cassette('api_geo_epcis') + + prefill_description.update(selected_type_de_champ_ids: [type_de_champ.id]) + end + + after do + VCR.eject_cassette('api_geo_departements') + VCR.eject_cassette('api_geo_epcis') + end it "builds the query to create a new prefilled dossier" do expect(prefill_description.prefill_query).to eq(expected_query) end context 'when the type de champ can have multiple values' do - let(:type_de_champ) { create(:type_de_champ_multiple_drop_down_list, procedure: procedure) } + let(:type_de_champ) { TypesDeChamp::PrefillTypeDeChamp.build(create(:type_de_champ_epci, procedure: procedure)) } let(:expected_query) do <<~TEXT curl --request POST '#{api_public_v1_dossiers_url(procedure)}' \\ --header 'Content-Type: application/json' \\ - --data '{"champ_#{type_de_champ.to_typed_id}": #{TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp.new(type_de_champ).example_value}}' + --data '{"champ_#{type_de_champ.to_typed_id}": #{type_de_champ.example_value}}' TEXT end diff --git a/spec/support/shared_examples_for_prefilled_dossier.rb b/spec/support/shared_examples_for_prefilled_dossier.rb index 20dd7dbe0..5ed67c49c 100644 --- a/spec/support/shared_examples_for_prefilled_dossier.rb +++ b/spec/support/shared_examples_for_prefilled_dossier.rb @@ -19,5 +19,6 @@ shared_examples "the user has got a prefilled dossier, owned by themselves" do expect(page).to have_field(type_de_champ_phone.libelle, with: phone_value) expect(page).to have_css('label', text: type_de_champ_phone.libelle) expect(page).to have_field(type_de_champ_datetime.libelle, with: datetime_value) + expect(page).to have_field(type_de_champ_epci.libelle, with: epci_value.last) end end diff --git a/spec/system/users/dossier_prefill_get_spec.rb b/spec/system/users/dossier_prefill_get_spec.rb index 62aca4e4a..41fc21403 100644 --- a/spec/system/users/dossier_prefill_get_spec.rb +++ b/spec/system/users/dossier_prefill_get_spec.rb @@ -1,4 +1,6 @@ describe 'Prefilling a dossier (with a GET request):' do + let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) } + let(:password) { 'my-s3cure-p4ssword' } let(:procedure) { create(:procedure, :published, opendata: true) } @@ -7,9 +9,24 @@ describe 'Prefilling a dossier (with a GET request):' do let(:type_de_champ_text) { create(:type_de_champ_text, procedure: procedure) } let(:type_de_champ_phone) { create(:type_de_champ_phone, procedure: procedure) } let(:type_de_champ_datetime) { create(:type_de_champ_datetime, procedure: procedure) } + let(:type_de_champ_epci) { create(:type_de_champ_epci, procedure: procedure) } let(:text_value) { "My Neighbor Totoro is the best movie ever" } let(:phone_value) { "invalid phone value" } let(:datetime_value) { "2023-02-01T10:32" } + let(:epci_value) { ['01', '200029999'] } + + before do + allow(Rails).to receive(:cache).and_return(memory_store) + Rails.cache.clear + + VCR.insert_cassette('api_geo_departements') + VCR.insert_cassette('api_geo_epcis') + end + + after do + VCR.eject_cassette('api_geo_departements') + VCR.eject_cassette('api_geo_epcis') + end context 'when authenticated' do it_behaves_like "the user has got a prefilled dossier, owned by themselves" do @@ -23,7 +40,8 @@ describe 'Prefilling a dossier (with a GET request):' do path: procedure.path, "champ_#{type_de_champ_text.to_typed_id}" => text_value, "champ_#{type_de_champ_phone.to_typed_id}" => phone_value, - "champ_#{type_de_champ_datetime.to_typed_id}" => datetime_value + "champ_#{type_de_champ_datetime.to_typed_id}" => datetime_value, + "champ_#{type_de_champ_epci.to_typed_id}" => epci_value ) click_on "Poursuivre mon dossier prérempli" @@ -37,7 +55,8 @@ describe 'Prefilling a dossier (with a GET request):' do path: procedure.path, "champ_#{type_de_champ_text.to_typed_id}" => text_value, "champ_#{type_de_champ_phone.to_typed_id}" => phone_value, - "champ_#{type_de_champ_datetime.to_typed_id}" => datetime_value + "champ_#{type_de_champ_datetime.to_typed_id}" => datetime_value, + "champ_#{type_de_champ_epci.to_typed_id}" => epci_value ) end diff --git a/spec/system/users/dossier_prefill_post_spec.rb b/spec/system/users/dossier_prefill_post_spec.rb index 6e484795f..c0eaa03fe 100644 --- a/spec/system/users/dossier_prefill_post_spec.rb +++ b/spec/system/users/dossier_prefill_post_spec.rb @@ -1,4 +1,6 @@ describe 'Prefilling a dossier (with a POST request):' do + let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) } + let(:password) { 'my-s3cure-p4ssword' } let(:procedure) { create(:procedure, :published) } @@ -7,9 +9,24 @@ describe 'Prefilling a dossier (with a POST request):' do let(:type_de_champ_text) { create(:type_de_champ_text, procedure: procedure) } let(:type_de_champ_phone) { create(:type_de_champ_phone, procedure: procedure) } let(:type_de_champ_datetime) { create(:type_de_champ_datetime, procedure: procedure) } + let(:type_de_champ_epci) { create(:type_de_champ_epci, procedure: procedure) } let(:text_value) { "My Neighbor Totoro is the best movie ever" } let(:phone_value) { "invalid phone value" } let(:datetime_value) { "2023-02-01T10:32" } + let(:epci_value) { ['01', '200029999'] } + + before do + allow(Rails).to receive(:cache).and_return(memory_store) + Rails.cache.clear + + VCR.insert_cassette('api_geo_departements') + VCR.insert_cassette('api_geo_epcis') + end + + after do + VCR.eject_cassette('api_geo_departements') + VCR.eject_cassette('api_geo_epcis') + end scenario "the user get the URL of a prefilled orphan brouillon dossier" do dossier_url = create_and_prefill_dossier_with_post_request @@ -98,7 +115,8 @@ describe 'Prefilling a dossier (with a POST request):' do params: { "champ_#{type_de_champ_text.to_typed_id}" => text_value, "champ_#{type_de_champ_phone.to_typed_id}" => phone_value, - "champ_#{type_de_champ_datetime.to_typed_id}" => datetime_value + "champ_#{type_de_champ_datetime.to_typed_id}" => datetime_value, + "champ_#{type_de_champ_epci.to_typed_id}" => epci_value }.to_json JSON.parse(session.response.body)["dossier_url"].gsub("http://www.example.com", "") end