2019-05-29 12:05:28 +02:00
|
|
|
|
describe AttachmentsController, type: :controller do
|
|
|
|
|
let(:user) { create(:user) }
|
2022-10-29 16:08:59 +02:00
|
|
|
|
let(:attachment) { champ.piece_justificative_file.attachments.first }
|
2024-07-01 15:31:32 +02:00
|
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :piece_justificative }]) }
|
|
|
|
|
let(:dossier) { create(:dossier, :with_populated_champs, user:, procedure:) }
|
|
|
|
|
let(:champ) { dossier.champs.first }
|
2020-04-08 17:53:04 +02:00
|
|
|
|
let(:signed_id) { attachment.blob.signed_id }
|
|
|
|
|
|
|
|
|
|
describe '#show' do
|
|
|
|
|
render_views
|
|
|
|
|
|
2022-05-06 12:25:58 +02:00
|
|
|
|
let(:format) { :turbo_stream }
|
2020-04-08 17:53:04 +02:00
|
|
|
|
|
|
|
|
|
subject do
|
2020-04-08 18:07:28 +02:00
|
|
|
|
request.headers['HTTP_REFERER'] = dossier_url(dossier)
|
2022-05-06 12:25:58 +02:00
|
|
|
|
get :show, params: { id: attachment.id, signed_id: signed_id }, format: format
|
2020-04-08 17:53:04 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when authenticated' do
|
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
2022-05-06 12:25:58 +02:00
|
|
|
|
context 'when requesting turbo_stream' do
|
|
|
|
|
let(:format) { :turbo_stream }
|
2020-04-08 17:53:04 +02:00
|
|
|
|
|
|
|
|
|
it { is_expected.to have_http_status(200) }
|
|
|
|
|
|
2022-05-06 12:25:58 +02:00
|
|
|
|
it 'renders turbo_stream that replaces the attachment HTML' do
|
2020-04-08 17:53:04 +02:00
|
|
|
|
subject
|
2022-05-06 12:25:58 +02:00
|
|
|
|
expect(response.body).to include(ActionView::RecordIdentifier.dom_id(attachment, :show))
|
2020-04-08 17:53:04 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the user opens the delete link in a new tab' do
|
|
|
|
|
let(:format) { :html }
|
|
|
|
|
|
|
|
|
|
it { is_expected.to have_http_status(302) }
|
|
|
|
|
it { is_expected.to redirect_to(dossier_path(dossier)) }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when not authenticated' do
|
2023-05-02 12:27:36 +02:00
|
|
|
|
it { is_expected.to redirect_to(new_user_session_path) }
|
2020-04-08 17:53:04 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-05-29 12:05:28 +02:00
|
|
|
|
|
|
|
|
|
describe '#destroy' do
|
|
|
|
|
render_views
|
|
|
|
|
|
2022-10-29 16:08:59 +02:00
|
|
|
|
let(:attachment) { champ.piece_justificative_file.attachments.first }
|
2019-05-29 12:05:28 +02:00
|
|
|
|
let(:signed_id) { attachment.blob.signed_id }
|
|
|
|
|
|
|
|
|
|
subject do
|
2022-05-06 12:25:58 +02:00
|
|
|
|
delete :destroy, params: { id: attachment.id, signed_id: signed_id }, format: :turbo_stream
|
2019-05-29 12:05:28 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when authenticated" do
|
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
|
|
context 'and dossier is owned by user' do
|
|
|
|
|
it { is_expected.to have_http_status(200) }
|
|
|
|
|
|
2020-04-08 17:53:04 +02:00
|
|
|
|
it 'removes the attachment' do
|
2019-05-29 12:05:28 +02:00
|
|
|
|
subject
|
|
|
|
|
expect(champ.reload.piece_justificative_file.attached?).to be(false)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'and signed_id is invalid' do
|
|
|
|
|
let(:signed_id) { 'yolo' }
|
|
|
|
|
|
|
|
|
|
it { is_expected.to have_http_status(404) }
|
|
|
|
|
|
2020-04-08 17:53:04 +02:00
|
|
|
|
it 'doesn’t remove the attachment' do
|
2019-05-29 12:05:28 +02:00
|
|
|
|
subject
|
|
|
|
|
expect(champ.reload.piece_justificative_file.attached?).to be(true)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when not authenticated' do
|
2023-05-02 12:27:36 +02:00
|
|
|
|
it { is_expected.to redirect_to(new_user_session_path) }
|
2019-05-29 12:05:28 +02:00
|
|
|
|
|
2020-04-08 17:53:04 +02:00
|
|
|
|
it 'doesn’t remove the attachment' do
|
2019-05-29 12:05:28 +02:00
|
|
|
|
subject
|
|
|
|
|
expect(champ.reload.piece_justificative_file.attached?).to be(true)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|