2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2020-03-30 15:34:56 +02:00
|
|
|
|
describe Champs::PieceJustificativeController, type: :controller do
|
|
|
|
|
let(:user) { create(:user) }
|
2024-01-23 09:52:20 +01:00
|
|
|
|
let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :piece_justificative }]) }
|
2020-03-30 15:34:56 +02:00
|
|
|
|
let(:dossier) { create(:dossier, user: user, procedure: procedure) }
|
2022-11-10 22:21:14 +01:00
|
|
|
|
let(:champ) { dossier.champs_public.first }
|
2020-03-30 15:34:56 +02:00
|
|
|
|
|
|
|
|
|
describe '#update' do
|
|
|
|
|
render_views
|
|
|
|
|
before { sign_in user }
|
|
|
|
|
|
|
|
|
|
subject do
|
|
|
|
|
put :update, params: {
|
2024-04-22 10:47:47 +02:00
|
|
|
|
dossier_id: champ.dossier_id,
|
|
|
|
|
stable_id: champ.stable_id,
|
2024-03-25 15:13:14 +01:00
|
|
|
|
blob_signed_id: file
|
2022-12-13 15:22:18 +01:00
|
|
|
|
}.compact, format: :turbo_stream
|
2020-03-30 15:34:56 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the file is valid' do
|
2020-06-30 17:53:54 +02:00
|
|
|
|
let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') }
|
2020-03-30 15:34:56 +02:00
|
|
|
|
|
|
|
|
|
it 'attach the file' do
|
|
|
|
|
subject
|
|
|
|
|
champ.reload
|
|
|
|
|
expect(champ.piece_justificative_file.attached?).to be true
|
2022-10-29 16:08:59 +02:00
|
|
|
|
expect(champ.piece_justificative_file[0].filename).to eq('piece_justificative_0.pdf')
|
2020-03-30 15:34:56 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'renders the attachment template as Javascript' do
|
|
|
|
|
subject
|
|
|
|
|
expect(response.status).to eq(200)
|
2023-01-16 21:31:07 +01:00
|
|
|
|
expect(response.body).to include("<turbo-stream action=\"replace\" target=\"#{champ.input_group_id}\">")
|
2020-03-30 15:34:56 +02:00
|
|
|
|
end
|
2022-01-05 18:02:43 +01:00
|
|
|
|
|
|
|
|
|
it 'updates dossier.last_champ_updated_at' do
|
|
|
|
|
expect { subject }.to change { dossier.reload.last_champ_updated_at }
|
|
|
|
|
end
|
2020-03-30 15:34:56 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the file is invalid' do
|
2020-06-30 17:53:54 +02:00
|
|
|
|
let(:file) { fixture_file_upload('spec/fixtures/files/invalid_file_format.json', 'application/json') }
|
2020-03-30 15:34:56 +02:00
|
|
|
|
|
|
|
|
|
# TODO: for now there are no validators on the champ piece_justificative_file,
|
|
|
|
|
# so we have to mock a failing validation.
|
|
|
|
|
# Once the validators will be enabled, remove those mocks, and let the usual
|
|
|
|
|
# validation fail naturally.
|
|
|
|
|
#
|
|
|
|
|
# See https://github.com/betagouv/demarches-simplifiees.fr/issues/4926
|
|
|
|
|
before do
|
|
|
|
|
champ
|
2024-05-13 16:53:33 +02:00
|
|
|
|
expect_any_instance_of(Champs::PieceJustificativeChamp).to receive(:save).and_return(false)
|
2020-03-30 15:34:56 +02:00
|
|
|
|
expect_any_instance_of(Champs::PieceJustificativeChamp).to receive(:errors)
|
|
|
|
|
.and_return(double(full_messages: ['La pièce justificative n’est pas d’un type accepté']))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'doesn’t attach the file' do
|
|
|
|
|
subject
|
|
|
|
|
expect(champ.reload.piece_justificative_file.attached?).to be false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'renders an error' do
|
|
|
|
|
subject
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
expect(response.header['Content-Type']).to include('application/json')
|
2023-04-19 11:00:18 +02:00
|
|
|
|
expect(response.parsed_body).to eq({ 'errors' => ['La pièce justificative n’est pas d’un type accepté'] })
|
2020-03-30 15:34:56 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-12-01 17:57:53 +01:00
|
|
|
|
|
|
|
|
|
describe '#template' do
|
|
|
|
|
before { Timecop.freeze }
|
|
|
|
|
after { Timecop.return }
|
|
|
|
|
|
|
|
|
|
subject do
|
|
|
|
|
get :template, params: {
|
2024-04-22 10:47:47 +02:00
|
|
|
|
dossier_id: champ.dossier_id,
|
|
|
|
|
stable_id: champ.stable_id
|
2022-12-01 17:57:53 +01:00
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "user signed in" do
|
|
|
|
|
before { sign_in user }
|
|
|
|
|
|
|
|
|
|
it 'redirects to the template' do
|
|
|
|
|
subject
|
|
|
|
|
expect(response).to redirect_to(champ.type_de_champ.piece_justificative_template.blob)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "another user signed in" do
|
|
|
|
|
before { sign_in create(:user) }
|
|
|
|
|
|
|
|
|
|
it "should not share template url" do
|
|
|
|
|
expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "user anonymous" do
|
|
|
|
|
it 'does not redirect to the template' do
|
|
|
|
|
subject
|
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-03-30 15:34:56 +02:00
|
|
|
|
end
|