2016-02-05 17:40:58 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
describe Admin::GestionnairesController, type: :controller do
|
2016-02-05 17:40:58 +01:00
|
|
|
let(:admin) { create(:administrateur) }
|
2016-05-24 16:39:39 +02:00
|
|
|
let(:email_2) { 'plip@octo.com' }
|
|
|
|
let(:admin_2) { create :administrateur, email: email_2 }
|
|
|
|
|
2016-02-05 17:40:58 +01:00
|
|
|
before do
|
|
|
|
sign_in admin
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #index' do
|
2016-05-24 16:39:39 +02:00
|
|
|
subject { get :index }
|
|
|
|
it { expect(subject.status).to eq(200) }
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
2018-01-30 14:43:56 +01:00
|
|
|
describe 'GET #index with sorting and pagination' do
|
|
|
|
subject {
|
2018-01-23 18:16:44 +01:00
|
|
|
get :index, params: {
|
2018-01-30 14:43:56 +01:00
|
|
|
'gestionnaires_smart_listing[page]': 1,
|
|
|
|
'gestionnaires_smart_listing[per_page]': 10,
|
|
|
|
'gestionnaires_smart_listing[sort][email]': 'asc'
|
2018-01-23 18:16:44 +01:00
|
|
|
}
|
2018-01-30 14:43:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
it { expect(subject.status).to eq(200) }
|
|
|
|
end
|
|
|
|
|
2016-02-05 17:40:58 +01:00
|
|
|
describe 'POST #create' do
|
2016-05-24 16:39:39 +02:00
|
|
|
let(:email) { 'test@plop.com' }
|
2016-05-26 16:44:10 +02:00
|
|
|
let(:procedure_id) { nil }
|
2018-01-16 13:34:24 +01:00
|
|
|
subject { post :create, params: { gestionnaire: { email: email }, procedure_id: procedure_id } }
|
2016-02-09 11:00:13 +01:00
|
|
|
|
|
|
|
context 'When email is valid' do
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
2016-05-20 15:39:17 +02:00
|
|
|
|
|
|
|
let(:gestionnaire) { Gestionnaire.last }
|
|
|
|
|
2016-02-09 11:00:13 +01:00
|
|
|
it { expect(response.status).to eq(302) }
|
|
|
|
it { expect(response).to redirect_to admin_gestionnaires_path }
|
2016-02-05 17:40:58 +01:00
|
|
|
|
2016-05-26 16:44:10 +02:00
|
|
|
context 'when procedure_id params is not null' do
|
|
|
|
let(:procedure) { create :procedure }
|
|
|
|
let(:procedure_id) { procedure.id }
|
|
|
|
|
|
|
|
it { expect(response.status).to eq(302) }
|
|
|
|
it { expect(response).to redirect_to admin_procedure_accompagnateurs_path(procedure_id: procedure_id) }
|
|
|
|
end
|
|
|
|
|
2016-02-09 11:00:13 +01:00
|
|
|
describe 'Gestionnaire attributs in database' do
|
|
|
|
it { expect(gestionnaire.email).to eq(email) }
|
2016-05-20 15:39:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'New gestionnaire is assign to the admin' do
|
|
|
|
it { expect(gestionnaire.administrateurs).to include admin }
|
|
|
|
it { expect(admin.gestionnaires).to include gestionnaire }
|
2016-02-09 11:00:13 +01:00
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email is not valid' do
|
2016-02-09 11:00:13 +01:00
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
2016-05-24 16:39:39 +02:00
|
|
|
let(:email) { 'piou' }
|
|
|
|
it { expect(response.status).to eq(302) }
|
|
|
|
it { expect { response }.not_to change(Gestionnaire, :count) }
|
|
|
|
it { expect(flash[:alert]).to be_present }
|
|
|
|
|
|
|
|
describe 'Email Notification' do
|
|
|
|
it {
|
|
|
|
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
|
2018-05-25 18:05:28 +02:00
|
|
|
expect(GestionnaireMailer).not_to receive(:deliver_later)
|
2016-05-24 16:39:39 +02:00
|
|
|
subject
|
|
|
|
}
|
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email is empty' do
|
2016-02-09 11:00:13 +01:00
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
2016-05-24 16:39:39 +02:00
|
|
|
let(:email) { '' }
|
|
|
|
it { expect(response.status).to eq(302) }
|
|
|
|
it { expect { response }.not_to change(Gestionnaire, :count) }
|
|
|
|
|
2016-02-09 11:00:13 +01:00
|
|
|
it 'Notification email is not send' do
|
|
|
|
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
|
2018-05-25 18:05:28 +02:00
|
|
|
expect(GestionnaireMailer).not_to receive(:deliver_later)
|
2016-02-09 11:00:13 +01:00
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
context 'when email is already assign at the admin' do
|
2016-02-05 17:40:58 +01:00
|
|
|
before do
|
2016-05-24 16:39:39 +02:00
|
|
|
create :gestionnaire, email: email, administrateurs: [admin]
|
2016-02-09 11:00:13 +01:00
|
|
|
subject
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
2016-05-24 16:39:39 +02:00
|
|
|
|
2016-02-05 17:40:58 +01:00
|
|
|
it { expect(response.status).to eq(302) }
|
2016-05-24 16:39:39 +02:00
|
|
|
it { expect { response }.not_to change(Gestionnaire, :count) }
|
|
|
|
it { expect(flash[:alert]).to be_present }
|
|
|
|
|
|
|
|
describe 'Email notification' do
|
|
|
|
it 'is not sent when email already exists' do
|
|
|
|
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
|
2018-05-25 18:05:28 +02:00
|
|
|
expect(GestionnaireMailer).not_to receive(:deliver_later)
|
2016-05-24 16:39:39 +02:00
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
context 'when an other admin will add the same email' do
|
2018-03-06 13:44:29 +01:00
|
|
|
let(:gestionnaire) { Gestionnaire.find_by(email: email) }
|
2016-05-24 16:39:39 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
create :gestionnaire, email: email, administrateurs: [admin]
|
|
|
|
|
|
|
|
sign_out admin
|
|
|
|
sign_in admin_2
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(response.status).to eq(302) }
|
|
|
|
it { expect { response }.not_to change(Gestionnaire, :count) }
|
|
|
|
it { expect(flash[:notice]).to be_present }
|
2016-02-09 11:00:13 +01:00
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
it { expect(admin_2.gestionnaires).to include gestionnaire }
|
|
|
|
it { expect(gestionnaire.administrateurs.size).to eq 2 }
|
|
|
|
end
|
|
|
|
|
2017-08-28 15:36:34 +02:00
|
|
|
context 'when an other admin will add the same email with some uppercase in it' do
|
|
|
|
let(:email) { 'Test@Plop.com' }
|
2018-03-06 13:44:29 +01:00
|
|
|
let(:gestionnaire) { Gestionnaire.find_by(email: email.downcase) }
|
2017-08-28 15:36:34 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
create :gestionnaire, email: email, administrateurs: [admin]
|
|
|
|
|
|
|
|
sign_out admin
|
|
|
|
sign_in admin_2
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(admin_2.gestionnaires).to include gestionnaire }
|
|
|
|
end
|
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
context 'Email notification' do
|
|
|
|
it 'Notification email is sent when accompagnateur is create' do
|
2018-04-27 12:29:01 +02:00
|
|
|
expect_any_instance_of(Gestionnaire).to receive(:invite!)
|
2016-02-09 11:00:13 +01:00
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2016-10-18 15:49:46 +02:00
|
|
|
|
|
|
|
context 'unified login' do
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates associated user with same credentials" do
|
|
|
|
gestionnaire = controller.instance_variable_get(:@gestionnaire)
|
|
|
|
user = User.find_by(email: gestionnaire.email)
|
|
|
|
expect(user.valid_password?(gestionnaire.password)).to be(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'invalid email' do
|
|
|
|
let(:email) { 'fail' }
|
|
|
|
|
|
|
|
it "won't create associated user" do
|
|
|
|
expect(User.where(email: email).exists?).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
2016-02-09 12:23:49 +01:00
|
|
|
|
|
|
|
describe 'DELETE #destroy' do
|
|
|
|
let(:email) { 'test@plop.com' }
|
2016-05-24 16:39:39 +02:00
|
|
|
let!(:admin) { create :administrateur }
|
|
|
|
let!(:gestionnaire) { create :gestionnaire, email: email, administrateurs: [admin] }
|
|
|
|
|
2018-01-16 13:34:24 +01:00
|
|
|
subject { delete :destroy, params: { id: gestionnaire.id } }
|
2016-02-09 12:23:49 +01:00
|
|
|
|
2016-02-19 14:11:03 +01:00
|
|
|
context "when gestionaire_id is valid" do
|
|
|
|
before do
|
|
|
|
subject
|
2016-05-24 16:39:39 +02:00
|
|
|
admin.reload
|
|
|
|
gestionnaire.reload
|
2016-02-19 14:11:03 +01:00
|
|
|
end
|
2016-05-24 16:39:39 +02:00
|
|
|
|
2016-02-19 14:11:03 +01:00
|
|
|
it { expect(response.status).to eq(302) }
|
|
|
|
it { expect(response).to redirect_to admin_gestionnaires_path }
|
2016-05-24 16:39:39 +02:00
|
|
|
it { expect(admin.gestionnaires).not_to include gestionnaire }
|
|
|
|
it { expect(gestionnaire.administrateurs).not_to include admin }
|
2016-02-19 14:11:03 +01:00
|
|
|
end
|
2016-02-09 12:23:49 +01:00
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
it { expect { subject }.not_to change(Gestionnaire, :count) }
|
2016-02-09 12:23:49 +01:00
|
|
|
end
|
2016-10-18 15:49:46 +02:00
|
|
|
end
|