2021-10-01 12:10:10 +02:00
|
|
|
describe Users::TransfersController, type: :controller do
|
2023-02-02 15:01:17 +01:00
|
|
|
let(:sender_user) { create(:user) }
|
|
|
|
let(:recipient_user) { create(:user) }
|
|
|
|
let(:dossier) { create(:dossier, user: sender_user) }
|
2021-10-01 12:10:10 +02:00
|
|
|
|
|
|
|
describe 'DELETE destroy' do
|
2023-03-01 17:19:06 +01:00
|
|
|
context "as transfer receiver" do
|
|
|
|
let(:dossier_transfert) { DossierTransfer.initiate(recipient_user.email, [dossier]) }
|
2023-02-02 15:01:17 +01:00
|
|
|
|
2023-03-01 17:19:06 +01:00
|
|
|
subject { delete :destroy, params: { id: dossier_transfert.id } }
|
2021-10-01 12:10:10 +02:00
|
|
|
|
2023-03-01 17:19:06 +01:00
|
|
|
before do
|
|
|
|
sign_in(recipient_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { subject }.not_to raise_error }
|
|
|
|
|
|
|
|
it "deletes dossier transfert" do
|
|
|
|
subject
|
|
|
|
expect { dossier_transfert.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
2023-03-09 11:39:45 +01:00
|
|
|
expect { (flash.notice).to eq('La demande de transfert a été supprimée avec succès') }
|
|
|
|
expect { (subject).to redirect_to dossiers_path }
|
2023-03-01 17:19:06 +01:00
|
|
|
end
|
2021-10-01 12:10:10 +02:00
|
|
|
end
|
|
|
|
|
2023-03-01 17:19:06 +01:00
|
|
|
context "as transfer sender" do
|
|
|
|
let(:dossier_transfert) { DossierTransfer.initiate(recipient_user.email, [dossier]) }
|
|
|
|
|
|
|
|
subject { delete :destroy, params: { id: dossier_transfert.id } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(sender_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { subject }.not_to raise_error }
|
2023-02-02 15:01:17 +01:00
|
|
|
|
2023-03-01 17:19:06 +01:00
|
|
|
it "deletes dossier transfert" do
|
|
|
|
subject
|
|
|
|
expect { dossier_transfert.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
2023-02-02 15:01:17 +01:00
|
|
|
end
|
2023-03-09 11:39:45 +01:00
|
|
|
|
|
|
|
context "as transfer unauthorized" do
|
|
|
|
let(:dossier_transfert) { DossierTransfer.initiate(recipient_user.email, [dossier]) }
|
|
|
|
let(:random_user) { create(:user) }
|
|
|
|
|
|
|
|
subject { delete :destroy, params: { id: dossier_transfert.id } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(random_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { subject }.not_to raise_error }
|
|
|
|
|
|
|
|
it "does not delete dossier transfert" do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect { dossier_transfert.reload.to eq(dossier_transfert) }
|
|
|
|
expect { (flash.alert).to eq("Vous n'avez pas l'autorisation pour supprimer cette demande de transfert") }
|
|
|
|
expect { (subject).to redirect_to dossiers_path }
|
|
|
|
end
|
|
|
|
end
|
2021-10-01 12:10:10 +02:00
|
|
|
end
|
2022-08-02 15:20:33 +02:00
|
|
|
|
|
|
|
describe "POST create" do
|
|
|
|
subject { post :create, params: { dossier_transfer: { email: email, dossier: dossier.id } } }
|
|
|
|
|
2023-02-02 15:01:17 +01:00
|
|
|
before do
|
|
|
|
sign_in(sender_user)
|
|
|
|
subject
|
|
|
|
end
|
2022-08-02 15:20:33 +02:00
|
|
|
|
|
|
|
context "with valid email" do
|
|
|
|
let(:email) { "test@rspec.net" }
|
|
|
|
|
|
|
|
it { expect(DossierTransfer.last.email).to eq(email) }
|
|
|
|
it { expect(DossierTransfer.last.dossiers).to eq([dossier]) }
|
|
|
|
end
|
|
|
|
|
2023-01-31 14:36:51 +01:00
|
|
|
context 'with upper case email' do
|
|
|
|
let(:email) { "Test@rspec.net" }
|
|
|
|
it { expect(DossierTransfer.last.email).to eq(email.strip.downcase) }
|
|
|
|
end
|
|
|
|
|
2022-08-02 15:20:33 +02:00
|
|
|
shared_examples 'email error' do
|
|
|
|
it { expect { subject }.not_to change { DossierTransfer.count } }
|
|
|
|
it { expect(flash.alert).to match([/invalide/]) }
|
|
|
|
it { is_expected.to redirect_to transferer_dossier_path(dossier.id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when email is empty" do
|
|
|
|
let(:email) { "" }
|
|
|
|
it_behaves_like 'email error'
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when email is invalid" do
|
|
|
|
let(:email) { "not-an-email" }
|
|
|
|
it_behaves_like 'email error'
|
|
|
|
end
|
|
|
|
end
|
2021-10-01 12:10:10 +02:00
|
|
|
end
|