From a24980145c7345631a5ad7338314b04fc07db627 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 6 Mar 2017 14:29:01 +0100 Subject: [PATCH 1/3] Block dossier initiation if procedure is archived --- .../users/description_controller.rb | 15 +++---- app/views/users/description/_show.html.haml | 21 +++++----- config/locales/fr.yml | 1 + config/routes.rb | 5 +-- .../description_controller_shared_example.rb | 40 ++++++++++++++----- .../users/description_controller_spec.rb | 22 +++++++++- 6 files changed, 72 insertions(+), 32 deletions(-) diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index 4dcd7f287..fb4bedda7 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -17,20 +17,21 @@ class Users::DescriptionController < UsersController acc end + if @procedure.archived? + flash[:alert] = t('errors.messages.procedure_archived') + end + rescue ActiveRecord::RecordNotFound flash.alert = t('errors.messages.dossier_not_found') redirect_to url_for(root_path) end - # def error - # show - # flash.now.alert = 'Un ou plusieurs attributs obligatoires sont manquants ou incorrects.' - # render 'show' - # end - - def create + def update @dossier = current_user_dossier @procedure = @dossier.procedure + + return head :forbidden if @procedure.archived? + @champs = @dossier.ordered_champs mandatory = true diff --git a/app/views/users/description/_show.html.haml b/app/views/users/description/_show.html.haml index f216b3d7a..13bb982e9 100644 --- a/app/views/users/description/_show.html.haml +++ b/app/views/users/description/_show.html.haml @@ -15,7 +15,7 @@ = @dossier.procedure.libelle -#TODO use form_for - = form_tag(url_for({controller: 'users/description', action: :create, dossier_id: @dossier.id}), class: 'form', method: 'POST', multipart: true) do + = form_tag(url_for({controller: 'users/description', action: :update, dossier_id: @dossier.id}), class: 'form', method: 'POST', multipart: true) do -unless @champs.nil? #liste_champs =render partial: 'users/description/champs', locals:{private: false} @@ -41,16 +41,17 @@ Vous pourrez dans tous les cas les compléter plus tard si vous ne les possédez pas de suite. - -route = Rails.application.routes.recognize_path(request.referrer) + -route = Rails.application.routes.recognize_path(request.referrer) # WTF ? - unless route[:controller].match('admin') %div{style: 'text-align:right'} %h6 Tous les champs portant un * sont obligatoires. - -if !@dossier.draft? - =render partial: '/layouts/modifications_terminees' - -else - = submit_tag 'Soumettre mon dossier', id: 'suivant', name: 'submit[nouveaux]', class: %w(btn btn btn-success), style: 'float:right', data: { disable_with: 'Soumettre votre dossier', submit: true} - = submit_tag 'Enregistrer un brouillon', id: 'brouillon', name: 'submit[brouillon]', class: %w(btn btn-xs btn-default), style: 'float:right; margin-right: 10px; margin-top: 6px', data: {disable_with: 'Enregistrer un brouillon', submit: true} - - %br - %br + - if @procedure.archived? + .alert.alert-danger + = t('errors.messages.procedure_archived') + - else + - if !@dossier.draft? + = render partial: '/layouts/modifications_terminees' + - else + = submit_tag 'Soumettre mon dossier', id: 'suivant', name: 'submit[nouveaux]', class: 'btn btn btn-success', style: 'float:right', disabled: @procedure.archived?, data: { disable_with: 'Soumettre votre dossier', submit: true} + = submit_tag 'Enregistrer un brouillon', id: 'brouillon', name: 'submit[brouillon]', class: 'btn btn-xs btn-default', style: 'float:right; margin-right: 10px; margin-top: 6px', disabled: @procedure.archived?, data: {disable_with: 'Enregistrer un brouillon', submit: true} diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 795c51d50..5f8a4ad9e 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -147,6 +147,7 @@ fr: france_connect: connexion: "Erreur lors de la connexion à France Connect." extension_white_list_error: "Le format de fichier de la pièce jointe n'est pas valide." + procedure_archived: "Cette démarche en ligne a été fermée, il n'est plus possible de déposer de dossier." datetime: distance_in_words: diff --git a/config/routes.rb b/config/routes.rb index 9735cadae..2f96e0031 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,9 +63,8 @@ Rails.application.routes.draw do resources :dossiers do get '/add_siret' => 'dossiers/add_siret#show' - get '/description' => 'description#show' - # get '/description/error' => 'description#error' - post 'description' => 'description#create' + get 'description' => 'description#show' + post 'description' => 'description#update' patch 'pieces_justificatives' => 'description#pieces_justificatives' diff --git a/spec/controllers/users/description_controller_shared_example.rb b/spec/controllers/users/description_controller_shared_example.rb index fa90d0824..89ebfa094 100644 --- a/spec/controllers/users/description_controller_shared_example.rb +++ b/spec/controllers/users/description_controller_shared_example.rb @@ -17,12 +17,20 @@ shared_examples 'description_controller_spec' do context 'when all is ok' do before do dossier.entreprise = create :entreprise + get :show, params: {dossier_id: dossier_id} end it 'returns http success' do - get :show, params: {dossier_id: dossier_id} expect(response).to have_http_status(:success) end + + context 'but procedure is archived' do + let(:archived) { true } + render_views + + it { expect(response).to have_http_status(:success) } + it { expect(response.body).to have_content(I18n.t('errors.messages.procedure_archived')) } + end end it 'redirection vers start si mauvais dossier ID' do @@ -90,7 +98,7 @@ shared_examples 'description_controller_spec' do end end - describe 'POST #create' do + describe 'POST #update' do let(:timestamp) { Time.now } let(:description) { 'Description de test Coucou, je suis un saut à la ligne Je suis un double saut la ligne.' } @@ -98,7 +106,7 @@ shared_examples 'description_controller_spec' do describe 'Premier enregistrement des données' do let(:submit) { {nouveaux: 'nouveaux'} } - subject { post :create, params: {dossier_id: dossier_id, submit: submit} } + subject { post :update, params: {dossier_id: dossier_id, submit: submit} } before do dossier.draft! @@ -130,7 +138,7 @@ shared_examples 'description_controller_spec' do context 'En train de manipuler un dossier non brouillon' do before do dossier.initiated! - post :create, params: {dossier_id: dossier_id} + post :update, params: {dossier_id: dossier_id} dossier.reload end @@ -145,7 +153,7 @@ shared_examples 'description_controller_spec' do end context 'Quand la procédure accepte les CERFA' do - subject { post :create, params: {dossier_id: dossier_id, + subject { post :update, params: {dossier_id: dossier_id, cerfa_pdf: cerfa_pdf} } it 'Notification interne is create' do @@ -154,7 +162,7 @@ shared_examples 'description_controller_spec' do context 'Sauvegarde du CERFA PDF', vcr: {cassette_name: 'controllers_users_description_controller_save_cerfa'} do before do - post :create, params: {dossier_id: dossier_id, + post :update, params: {dossier_id: dossier_id, cerfa_pdf: cerfa_pdf} dossier.reload end @@ -181,7 +189,7 @@ shared_examples 'description_controller_spec' do let(:cerfas) { Cerfa.where(dossier_id: dossier_id) } before do - post :create, params: {dossier_id: dossier_id, cerfa_pdf: cerfa_pdf} + post :update, params: {dossier_id: dossier_id, cerfa_pdf: cerfa_pdf} end it "il y a deux CERFA PDF pour ce dossier" do @@ -195,7 +203,7 @@ shared_examples 'description_controller_spec' do context 'Sauvegarde du CERFA PDF' do let!(:procedure) { create(:procedure) } before do - post :create, params: {dossier_id: dossier_id, + post :update, params: {dossier_id: dossier_id, cerfa_pdf: cerfa_pdf} dossier.reload end @@ -214,7 +222,7 @@ shared_examples 'description_controller_spec' do let(:dossier_minute_value) { '00' } before do - post :create, params: {dossier_id: dossier_id, + post :update, params: {dossier_id: dossier_id, champs: { "'#{dossier.champs.first.id}'" => dossier_champs_first, "'#{dossier.champs.second.id}'" => dossier_date_value @@ -254,7 +262,7 @@ shared_examples 'description_controller_spec' do context 'Sauvegarde des pièces justificatives', vcr: {cassette_name: 'controllers_users_description_controller_sauvegarde_pj'} do let(:all_pj_type) { dossier.procedure.type_de_piece_justificative_ids } before do - post :create, params: {dossier_id: dossier_id, + post :update, params: {dossier_id: dossier_id, 'piece_justificative_'+all_pj_type[0].to_s => piece_justificative_0, 'piece_justificative_'+all_pj_type[1].to_s => piece_justificative_1} dossier.reload @@ -264,7 +272,7 @@ shared_examples 'description_controller_spec' do it 'ClamavService safe_file? is call' do expect(ClamavService).to receive(:safe_file?).twice - post :create, params: {dossier_id: dossier_id, + post :update, params: {dossier_id: dossier_id, 'piece_justificative_'+all_pj_type[0].to_s => piece_justificative_0, 'piece_justificative_'+all_pj_type[1].to_s => piece_justificative_1} end @@ -281,6 +289,16 @@ shared_examples 'description_controller_spec' do it { expect(subject.user).to eq user } end end + + context 'La procédure est archivée' do + let(:archived) { true } + + before do + post :update, params: { dossier_id: dossier.id } + end + + it { expect(response.status).to eq(403) } + end end describe 'POST #pieces_justificatives', vcr: {cassette_name: 'controllers_users_description_controller_pieces_justificatives'} do diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index ace2b9f1b..fe5965eaf 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -5,8 +5,9 @@ require 'controllers/users/description_controller_shared_example' describe Users::DescriptionController, type: :controller, vcr: {cassette_name: 'controllers_users_description_controller'} do let(:owner_user) { create(:user) } let(:invite_by_user) { create :user, email: 'invite@plop.com' } + let(:archived) { false } - let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true) } + let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true, archived: archived) } let(:dossier) { create(:dossier, procedure: procedure, user: owner_user, state: 'initiated') } let(:dossier_id) { dossier.id } @@ -42,4 +43,23 @@ describe Users::DescriptionController, type: :controller, vcr: {cassette_name: ' it_should_behave_like "description_controller_spec" end + # + # context 'POST #update' do + # let(:user) { owner_user } + # + # subject { post :update, params: { dossier_id: dossier.id } } + # + # before do + # subject + # end + # + # it { expect(response.status).to eq(302) } + # + # context 'when procedure is archived' do + # let(:archived) { true } + # + # it { expect(response.status).to eq(403) } + # end + # + # end end From 88654535ea99d7cd8a4815a33771274867dda3c9 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 6 Mar 2017 14:35:45 +0100 Subject: [PATCH 2/3] Remove comment --- .../users/description_controller_spec.rb | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index fe5965eaf..9511f3d4a 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -43,23 +43,4 @@ describe Users::DescriptionController, type: :controller, vcr: {cassette_name: ' it_should_behave_like "description_controller_spec" end - # - # context 'POST #update' do - # let(:user) { owner_user } - # - # subject { post :update, params: { dossier_id: dossier.id } } - # - # before do - # subject - # end - # - # it { expect(response.status).to eq(302) } - # - # context 'when procedure is archived' do - # let(:archived) { true } - # - # it { expect(response.status).to eq(403) } - # end - # - # end end From 7e4f473027c47718afb86fe729803cea0bc5f36f Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 6 Mar 2017 15:03:51 +0100 Subject: [PATCH 3/3] When procedure is archived user can update dossier if not in brouillon --- .../users/description_controller.rb | 4 ++-- app/models/dossier.rb | 4 ++++ app/views/users/description/_show.html.haml | 11 +++++----- .../description_controller_shared_example.rb | 21 +++++++++++++++---- .../users/description_controller_spec.rb | 3 ++- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index fb4bedda7..fa2d26f8d 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -17,7 +17,7 @@ class Users::DescriptionController < UsersController acc end - if @procedure.archived? + unless @dossier.can_be_initiated? flash[:alert] = t('errors.messages.procedure_archived') end @@ -30,7 +30,7 @@ class Users::DescriptionController < UsersController @dossier = current_user_dossier @procedure = @dossier.procedure - return head :forbidden if @procedure.archived? + return head :forbidden unless @dossier.can_be_initiated? @champs = @dossier.ordered_champs diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 5877e87fd..8031da67b 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -304,4 +304,8 @@ class Dossier < ActiveRecord::Base def invite_by_user? email (invites_user.pluck :email).include? email end + + def can_be_initiated? + !(procedure.archived && draft?) + end end diff --git a/app/views/users/description/_show.html.haml b/app/views/users/description/_show.html.haml index 13bb982e9..14b17b160 100644 --- a/app/views/users/description/_show.html.haml +++ b/app/views/users/description/_show.html.haml @@ -46,12 +46,11 @@ %div{style: 'text-align:right'} %h6 Tous les champs portant un * sont obligatoires. - - if @procedure.archived? + - if !@dossier.can_be_initiated? .alert.alert-danger = t('errors.messages.procedure_archived') + - elsif !@dossier.draft? + = render partial: '/layouts/modifications_terminees' - else - - if !@dossier.draft? - = render partial: '/layouts/modifications_terminees' - - else - = submit_tag 'Soumettre mon dossier', id: 'suivant', name: 'submit[nouveaux]', class: 'btn btn btn-success', style: 'float:right', disabled: @procedure.archived?, data: { disable_with: 'Soumettre votre dossier', submit: true} - = submit_tag 'Enregistrer un brouillon', id: 'brouillon', name: 'submit[brouillon]', class: 'btn btn-xs btn-default', style: 'float:right; margin-right: 10px; margin-top: 6px', disabled: @procedure.archived?, data: {disable_with: 'Enregistrer un brouillon', submit: true} + = submit_tag 'Soumettre mon dossier', id: 'suivant', name: 'submit[nouveaux]', class: 'btn btn btn-success', style: 'float:right', disabled: @procedure.archived?, data: { disable_with: 'Soumettre votre dossier', submit: true} + = submit_tag 'Enregistrer un brouillon', id: 'brouillon', name: 'submit[brouillon]', class: 'btn btn-xs btn-default', style: 'float:right; margin-right: 10px; margin-top: 6px', disabled: @procedure.archived?, data: {disable_with: 'Enregistrer un brouillon', submit: true} diff --git a/spec/controllers/users/description_controller_shared_example.rb b/spec/controllers/users/description_controller_shared_example.rb index 89ebfa094..f74eb29b1 100644 --- a/spec/controllers/users/description_controller_shared_example.rb +++ b/spec/controllers/users/description_controller_shared_example.rb @@ -24,12 +24,19 @@ shared_examples 'description_controller_spec' do expect(response).to have_http_status(:success) end - context 'but procedure is archived' do - let(:archived) { true } + context 'procedure is archived' do render_views + let(:archived) { true } it { expect(response).to have_http_status(:success) } - it { expect(response.body).to have_content(I18n.t('errors.messages.procedure_archived')) } + it { expect(response.body).to_not have_content(I18n.t('errors.messages.procedure_archived')) } + + context 'dossier is a draft' do + let(:state) { 'draft' } + + it { expect(response).to have_http_status(:success) } + it { expect(response.body).to have_content(I18n.t('errors.messages.procedure_archived')) } + end end end @@ -297,7 +304,13 @@ shared_examples 'description_controller_spec' do post :update, params: { dossier_id: dossier.id } end - it { expect(response.status).to eq(403) } + it { expect(response.status).to eq(302) } + + context 'Le dossier est en brouillon' do + let(:state) { 'draft' } + + it { expect(response.status).to eq(403) } + end end end diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index 9511f3d4a..0cd1c0414 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -6,9 +6,10 @@ describe Users::DescriptionController, type: :controller, vcr: {cassette_name: ' let(:owner_user) { create(:user) } let(:invite_by_user) { create :user, email: 'invite@plop.com' } let(:archived) { false } + let(:state) { 'initiated' } let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true, archived: archived) } - let(:dossier) { create(:dossier, procedure: procedure, user: owner_user, state: 'initiated') } + let(:dossier) { create(:dossier, procedure: procedure, user: owner_user, state: state) } let(:dossier_id) { dossier.id } let(:bad_dossier_id) { Dossier.count + 10000 }