2019-07-02 18:15:03 +02:00
|
|
|
|
describe Users::ProfilController, type: :controller do
|
2019-07-09 17:08:27 +02:00
|
|
|
|
include ActiveJob::TestHelper
|
|
|
|
|
|
2019-07-08 10:40:50 +02:00
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
2021-09-20 13:26:57 +02:00
|
|
|
|
describe 'GET #show' do
|
|
|
|
|
render_views
|
|
|
|
|
|
|
|
|
|
before { post :show }
|
|
|
|
|
|
2021-09-20 13:57:55 +02:00
|
|
|
|
context 'when the current user is not an instructeur' do
|
|
|
|
|
it { expect(response.body).to include(I18n.t('users.profil.show.transfer_title')) }
|
|
|
|
|
|
|
|
|
|
context 'when an existing transfer exists' do
|
|
|
|
|
let(:dossiers) { Array.new(3) { create(:dossier, user: user) } }
|
|
|
|
|
let(:next_owner) { 'loulou@lou.com' }
|
|
|
|
|
let!(:transfer) { DossierTransfer.initiate(next_owner, dossiers) }
|
|
|
|
|
|
|
|
|
|
before { post :show }
|
|
|
|
|
|
|
|
|
|
it { expect(response.body).to include(I18n.t('users.profil.show.one_waiting_transfer', count: dossiers.count, email: next_owner)) }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the current user is an instructeur' do
|
|
|
|
|
let(:user) { create(:instructeur).user }
|
|
|
|
|
|
|
|
|
|
it { expect(response.body).not_to include(I18n.t('users.profil.show.transfer_title')) }
|
|
|
|
|
end
|
2021-09-20 13:26:57 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-07-08 10:40:50 +02:00
|
|
|
|
describe 'PATCH #update_email' do
|
2021-12-23 14:54:37 +01:00
|
|
|
|
context 'when email is same as user' do
|
|
|
|
|
it 'fails' do
|
|
|
|
|
patch :update_email, params: { user: { email: user.email } }
|
|
|
|
|
expect(response).to have_http_status(302)
|
2022-12-20 17:51:36 +01:00
|
|
|
|
expect(flash[:alert]).to eq(["Le champ « La nouvelle adresse email » ne peut être identique à l’ancienne. Saisir une autre adresse email"])
|
2021-12-23 14:54:37 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-07-08 10:40:50 +02:00
|
|
|
|
context 'when everything is fine' do
|
2021-10-26 16:18:12 +02:00
|
|
|
|
let(:previous_request) { create(:user) }
|
|
|
|
|
|
2019-07-08 10:40:50 +02:00
|
|
|
|
before do
|
2021-10-26 16:18:12 +02:00
|
|
|
|
user.update(requested_merge_into: previous_request)
|
2019-07-08 10:40:50 +02:00
|
|
|
|
patch :update_email, params: { user: { email: 'loulou@lou.com' } }
|
|
|
|
|
user.reload
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { expect(user.unconfirmed_email).to eq('loulou@lou.com') }
|
2021-10-26 16:18:12 +02:00
|
|
|
|
it { expect(user.requested_merge_into).to be_nil }
|
2019-07-08 10:40:50 +02:00
|
|
|
|
it { expect(response).to redirect_to(profil_path) }
|
|
|
|
|
it { expect(flash.notice).to eq(I18n.t('devise.registrations.update_needs_confirmation')) }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the mail is already taken' do
|
2019-07-09 17:08:27 +02:00
|
|
|
|
let(:existing_user) { create(:user) }
|
2019-07-08 10:40:50 +02:00
|
|
|
|
|
|
|
|
|
before do
|
2021-10-26 16:18:12 +02:00
|
|
|
|
user.update(unconfirmed_email: 'unconfirmed@mail.com')
|
|
|
|
|
|
2021-12-23 14:54:37 +01:00
|
|
|
|
expect(UserMailer).to receive(:ask_for_merge).with(user, existing_user.email).and_return(double(deliver_later: true))
|
2021-10-26 13:36:14 +02:00
|
|
|
|
|
2019-07-09 17:08:27 +02:00
|
|
|
|
perform_enqueued_jobs do
|
|
|
|
|
patch :update_email, params: { user: { email: existing_user.email } }
|
|
|
|
|
end
|
2019-07-08 10:40:50 +02:00
|
|
|
|
user.reload
|
|
|
|
|
end
|
|
|
|
|
|
2021-10-26 13:36:14 +02:00
|
|
|
|
it 'launches the merge process' do
|
|
|
|
|
expect(user.unconfirmed_email).to be_nil
|
|
|
|
|
expect(response).to redirect_to(profil_path)
|
|
|
|
|
expect(flash.notice).to eq(I18n.t('devise.registrations.update_needs_confirmation'))
|
|
|
|
|
end
|
2019-07-08 10:40:50 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the mail is incorrect' do
|
|
|
|
|
before do
|
|
|
|
|
patch :update_email, params: { user: { email: 'incorrect' } }
|
|
|
|
|
user.reload
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { expect(response).to redirect_to(profil_path) }
|
2022-12-20 17:51:36 +01:00
|
|
|
|
it { expect(flash.alert).to eq(["Le champ « Adresse éléctronique » est invalide. Saisir une adresse éléctronique valide, exemple : john.doe@exemple.fr"]) }
|
2019-07-08 10:40:50 +02:00
|
|
|
|
end
|
2019-12-09 17:11:12 +01:00
|
|
|
|
|
|
|
|
|
context 'when the user has an instructeur role' do
|
|
|
|
|
let(:instructeur_email) { 'instructeur_email@a.com' }
|
|
|
|
|
let!(:user) { create(:instructeur, email: instructeur_email).user }
|
|
|
|
|
|
|
|
|
|
before do
|
2021-10-18 12:03:13 +02:00
|
|
|
|
patch :update_email, params: { user: { email: requested_email } }
|
2019-12-09 17:11:12 +01:00
|
|
|
|
user.reload
|
|
|
|
|
end
|
|
|
|
|
|
2021-10-18 12:03:13 +02:00
|
|
|
|
context 'when the requested email is allowed' do
|
|
|
|
|
let(:requested_email) { 'legit@gouv.fr' }
|
|
|
|
|
|
|
|
|
|
it { expect(user.unconfirmed_email).to eq('legit@gouv.fr') }
|
|
|
|
|
it { expect(response).to redirect_to(profil_path) }
|
|
|
|
|
it { expect(flash.notice).to eq(I18n.t('devise.registrations.update_needs_confirmation')) }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the requested email is not allowed' do
|
|
|
|
|
let(:requested_email) { 'weird@gmail.com' }
|
|
|
|
|
|
|
|
|
|
it { expect(response).to redirect_to(profil_path) }
|
|
|
|
|
it { expect(flash.alert).to include('contactez le support') }
|
|
|
|
|
end
|
2019-12-09 17:11:12 +01:00
|
|
|
|
end
|
2019-07-08 10:40:50 +02:00
|
|
|
|
end
|
2021-09-20 13:14:03 +02:00
|
|
|
|
|
|
|
|
|
context 'POST #transfer_all_dossiers' do
|
|
|
|
|
let!(:dossiers) { Array.new(3) { create(:dossier, user: user) } }
|
|
|
|
|
let(:next_owner) { 'loulou@lou.com' }
|
|
|
|
|
let(:created_transfer) { DossierTransfer.first }
|
|
|
|
|
|
2022-08-02 13:01:06 +02:00
|
|
|
|
subject {
|
2021-09-20 13:14:03 +02:00
|
|
|
|
post :transfer_all_dossiers, params: { next_owner: next_owner }
|
2022-08-02 13:01:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
before { subject }
|
2021-09-20 13:14:03 +02:00
|
|
|
|
|
|
|
|
|
it "transfer all dossiers" do
|
|
|
|
|
expect(created_transfer.email).to eq(next_owner)
|
2021-09-28 15:53:18 +02:00
|
|
|
|
expect(created_transfer.dossiers).to match_array(dossiers)
|
2021-09-20 13:14:03 +02:00
|
|
|
|
expect(flash.notice).to eq("Le transfert de 3 dossiers à #{next_owner} est en cours")
|
|
|
|
|
end
|
2022-08-02 13:01:06 +02:00
|
|
|
|
|
|
|
|
|
context "next owner has an empty email" do
|
|
|
|
|
let(:next_owner) { '' }
|
|
|
|
|
|
|
|
|
|
it "should not transfer to an empty email" do
|
|
|
|
|
expect { subject }.not_to change { DossierTransfer.count }
|
2023-06-08 10:03:34 +02:00
|
|
|
|
expect(flash.alert).to eq(["L’adresse email est invalide"])
|
2022-08-02 13:01:06 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2021-09-20 13:14:03 +02:00
|
|
|
|
end
|
2021-10-26 13:22:51 +02:00
|
|
|
|
|
|
|
|
|
context 'POST #accept_merge' do
|
|
|
|
|
let!(:requesting_user) { create(:user, requested_merge_into: user) }
|
|
|
|
|
|
|
|
|
|
subject { post :accept_merge }
|
|
|
|
|
|
|
|
|
|
it 'merges the account' do
|
|
|
|
|
expect_any_instance_of(User).to receive(:merge)
|
|
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
requesting_user.reload
|
|
|
|
|
|
|
|
|
|
expect(requesting_user.requested_merge_into).to be_nil
|
|
|
|
|
expect(flash.notice).to include('Vous avez absorbé')
|
|
|
|
|
expect(response).to redirect_to(profil_path)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'POST #refuse_merge' do
|
|
|
|
|
let!(:requesting_user) { create(:user, requested_merge_into: user) }
|
|
|
|
|
|
|
|
|
|
subject { post :refuse_merge }
|
|
|
|
|
|
|
|
|
|
it 'merges the account' do
|
|
|
|
|
expect_any_instance_of(User).not_to receive(:merge)
|
|
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
requesting_user.reload
|
|
|
|
|
|
|
|
|
|
expect(requesting_user.requested_merge_into).to be_nil
|
|
|
|
|
expect(flash.notice).to include('La fusion a été refusé')
|
|
|
|
|
expect(response).to redirect_to(profil_path)
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-11-07 17:18:42 +01:00
|
|
|
|
|
|
|
|
|
context 'DELETE #destroy_fci' do
|
|
|
|
|
let!(:fci) { create(:france_connect_information, user: user) }
|
|
|
|
|
|
|
|
|
|
subject { delete :destroy_fci, params: { fci_id: fci.id } }
|
|
|
|
|
|
|
|
|
|
it do
|
|
|
|
|
expect(FranceConnectInformation.where(user: user).count).to eq(1)
|
|
|
|
|
subject
|
|
|
|
|
expect(FranceConnectInformation.where(user: user).count).to eq(0)
|
|
|
|
|
expect(response).to redirect_to(profil_path)
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-08-24 14:19:44 +02:00
|
|
|
|
end
|