demarches-normaliennes/spec/models/concern/dossier_prefillable_concern_spec.rb
Sébastien Carceles 20136b7ac8
feat(demarche): create and prefill a dossier with POST request (#8233)
* add base controller for public api

* add dossiers controller with basic checks

* create the dossier

* ensure content-type is json

* prefill dossier with given values

* mark a dossier as prefilled

When a dossier is prefilled, it's allowed not to have a user.

Plus, we add a secure token to the dossier, which we will need later to set a
user after sign in / sign up.

* set user as owner of an orphan prefilled dossier

When a visitor comes from the dossier_url answered by the public api,
the dossier is orphan:
- when the user is already authenticated: they become the owner
- when the user is not authenticated: they can sign in / sign up / france_connect
and then they become the owner

So here is the procedure:
- allow to sign in / sign up / france connect when user is unauthenticated
- set dossier ownership when the dossier is orphan
- check dossier ownership when the dossier is not
- redirect to brouillon path when user is signed in and owner

* mark the dossier as prefilled when it's prefilled
(even with a GET request, because it will be useful later on, for
exmample in order to cleanup the unused prefilled dossiers)

* system spec: prefilling dossier with post request
2023-01-03 14:46:10 +01:00

78 lines
2.9 KiB
Ruby

# frozen_string_literal: true
RSpec.describe DossierPrefillableConcern do
describe '.prefill!' do
let(:procedure) { create(:procedure, :published, types_de_champ_public:) }
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
let(:types_de_champ_public) { [] }
subject(:fill) { dossier.prefill!(values); dossier.reload }
shared_examples 'a dossier marked as prefilled' do
it 'marks the dossier as prefilled' do
expect { fill }.to change { dossier.reload.prefilled }.from(nil).to(true)
end
end
context 'when champs_public_attributes is empty' do
let(:values) { [] }
it_behaves_like 'a dossier marked as prefilled'
it "doesn't change champs_public" do
expect { fill }.not_to change { dossier.champs_public.to_a }
end
end
context 'when champs_public_attributes has values' do
context 'when the champs are valid' do
let(:types_de_champ_public) { [{ type: :text }, { type: :phone }] }
let(:type_de_champ_1) { procedure.published_revision.types_de_champ_public.first }
let(:value_1) { "any value" }
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
let(:type_de_champ_2) { procedure.published_revision.types_de_champ_public.second }
let(:value_2) { "33612345678" }
let(:champ_id_2) { find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).id }
let(:values) { [{ id: champ_id_1, value: value_1 }, { id: champ_id_2, value: value_2 }] }
it_behaves_like 'a dossier marked as prefilled'
it "updates the champs with the new values and mark them as prefilled" do
fill
expect(dossier.champs_public.first.value).to eq(value_1)
expect(dossier.champs_public.first.prefilled).to eq(true)
expect(dossier.champs_public.last.value).to eq(value_2)
expect(dossier.champs_public.last.prefilled).to eq(true)
end
end
context 'when a champ is invalid' do
let(:types_de_champ_public) { [{ type: :phone }] }
let(:type_de_champ_1) { procedure.published_revision.types_de_champ_public.first }
let(:value) { "a non phone value" }
let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
let(:values) { [{ id: champ_id, value: value }] }
it_behaves_like 'a dossier marked as prefilled'
it "still updates the champ" do
expect { fill }.to change { dossier.champs_public.first.value }.from(nil).to(value)
end
it "still marks it as prefilled" do
expect { fill }.to change { dossier.champs_public.first.prefilled }.from(nil).to(true)
end
end
end
end
private
def find_champ_by_stable_id(dossier, stable_id)
dossier.champs.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
end
end