DossierController: user can destroy pjs

This commit is contained in:
simon lehericey 2018-11-08 10:19:52 +01:00 committed by Mathieu Magnin
parent 4c7c09c270
commit db6c86b242
5 changed files with 60 additions and 5 deletions

View file

@ -4,11 +4,11 @@ module NewUser
SESSION_USER_RETURN_LOCATION = 'user_return_to'
ACTIONS_ALLOWED_TO_ANY_USER = [:index, :recherche, :new]
ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :demande, :messagerie, :brouillon, :update_brouillon, :modifier, :update, :create_commentaire]
ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :demande, :messagerie, :brouillon, :update_brouillon, :modifier, :update, :create_commentaire, :purge_champ_piece_justificative]
before_action :ensure_ownership!, except: ACTIONS_ALLOWED_TO_ANY_USER + ACTIONS_ALLOWED_TO_OWNER_OR_INVITE
before_action :ensure_ownership_or_invitation!, only: ACTIONS_ALLOWED_TO_OWNER_OR_INVITE
before_action :ensure_dossier_can_be_updated, only: [:update_identite, :update_brouillon, :modifier, :update]
before_action :ensure_dossier_can_be_updated, only: [:update_identite, :update_brouillon, :modifier, :update, :purge_champ_piece_justificative]
before_action :forbid_invite_submission!, only: [:update_brouillon]
before_action :forbid_closed_submission!, only: [:update_brouillon]
before_action :show_demarche_en_test_banner
@ -230,6 +230,14 @@ module NewUser
redirect_to url_for dossiers_path
end
def purge_champ_piece_justificative
@champ = dossier.champs.find(params[:champ_id])
@champ.piece_justificative_file.purge
flash.notice = 'La pièce jointe a bien été supprimée.'
end
private
def store_user_location!

View file

@ -0,0 +1,2 @@
<%= render_flash(timeout: 5000, sticky: true) %>
<%= remove_element("#piece_justificative_#{@champ.id}") %>

View file

@ -10,8 +10,11 @@
id: "champs_#{champ.id}",
direct_upload: true
- else
%div{ id: "piece_justificative_#{champ.id}" }
= render partial: "shared/champs/piece_justificative/pj_link", locals: { champ: champ, user_can_upload: true }
%br
= link_to 'supprimer', purge_champ_piece_justificative_dossier_path(champ_id: champ.id), remote: true, method: :delete
%br
Modifier :
= form.file_field :piece_justificative_file,
id: "champs_#{champ.id}",

View file

@ -284,6 +284,7 @@ Rails.application.routes.draw do
post 'commentaire' => 'dossiers#create_commentaire'
post 'ask_deletion'
get 'attestation'
delete 'purge_champ_piece_justificative'
end
collection do

View file

@ -906,4 +906,45 @@ describe NewUser::DossiersController, type: :controller do
end
end
end
describe '#purge_champ_piece_justificative' do
before { sign_in(user) }
subject { delete :purge_champ_piece_justificative, params: { id: champ.dossier.id, champ_id: champ.id }, format: :js }
context 'when dossier is owned by user' do
let(:dossier){ create(:dossier, user: user) }
let(:champ){ create(:champ_piece_justificative, dossier_id: dossier.id) }
it { is_expected.to have_http_status(200) }
it do
subject
expect(champ.reload.piece_justificative_file.attached?).to be(false)
end
context 'but champ is not linked to this dossier' do
let(:champ){ create(:champ_piece_justificative, dossier: create(:dossier)) }
it { is_expected.to redirect_to(root_path) }
it do
subject
expect(champ.reload.piece_justificative_file.attached?).to be(true)
end
end
end
context 'when dossier is not owned by user' do
let(:dossier){ create(:dossier, user: create(:user)) }
let(:champ){ create(:champ_piece_justificative, dossier_id: dossier.id) }
it { is_expected.to redirect_to(root_path) }
it do
subject
expect(champ.reload.piece_justificative_file.attached?).to be(true)
end
end
end
end