2016-02-05 17:40:58 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
describe Admin::InstructeursController, 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
|
2019-08-09 10:46:39 +02:00
|
|
|
sign_in(admin.user)
|
2016-02-05 17:40:58 +01:00
|
|
|
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: {
|
2019-08-06 11:02:54 +02:00
|
|
|
'instructeurs_smart_listing[page]': 1,
|
|
|
|
'instructeurs_smart_listing[per_page]': 10,
|
|
|
|
'instructeurs_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 }
|
2019-08-06 11:02:54 +02:00
|
|
|
subject { post :create, params: { instructeur: { 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
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
let(:instructeur) { Instructeur.last }
|
2016-05-20 15:39:17 +02:00
|
|
|
|
2016-02-09 11:00:13 +01:00
|
|
|
it { expect(response.status).to eq(302) }
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect(response).to redirect_to admin_instructeurs_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) }
|
2019-08-06 11:46:55 +02:00
|
|
|
it { expect(response).to redirect_to admin_procedure_assigns_path(procedure_id: procedure_id) }
|
2016-05-26 16:44:10 +02:00
|
|
|
end
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
describe 'Instructeur attributs in database' do
|
|
|
|
it { expect(instructeur.email).to eq(email) }
|
2016-05-20 15:39:17 +02:00
|
|
|
end
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
describe 'New instructeur is assign to the admin' do
|
|
|
|
it { expect(instructeur.administrateurs).to include admin }
|
|
|
|
it { expect(admin.instructeurs).to include instructeur }
|
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) }
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect { response }.not_to change(Instructeur, :count) }
|
2016-05-24 16:39:39 +02:00
|
|
|
it { expect(flash[:alert]).to be_present }
|
|
|
|
|
|
|
|
describe 'Email Notification' do
|
|
|
|
it {
|
2019-08-06 11:02:54 +02:00
|
|
|
expect(InstructeurMailer).not_to receive(:new_instructeur)
|
|
|
|
expect(InstructeurMailer).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) }
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect { response }.not_to change(Instructeur, :count) }
|
2016-05-24 16:39:39 +02:00
|
|
|
|
2016-02-09 11:00:13 +01:00
|
|
|
it 'Notification email is not send' do
|
2019-08-06 11:02:54 +02:00
|
|
|
expect(InstructeurMailer).not_to receive(:new_instructeur)
|
|
|
|
expect(InstructeurMailer).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
|
2019-08-06 11:02:54 +02:00
|
|
|
create :instructeur, 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) }
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect { response }.not_to change(Instructeur, :count) }
|
2016-05-24 16:39:39 +02:00
|
|
|
it { expect(flash[:alert]).to be_present }
|
|
|
|
|
|
|
|
describe 'Email notification' do
|
|
|
|
it 'is not sent when email already exists' do
|
2019-08-06 11:02:54 +02:00
|
|
|
expect(InstructeurMailer).not_to receive(:new_instructeur)
|
|
|
|
expect(InstructeurMailer).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
|
2019-08-06 11:02:54 +02:00
|
|
|
let(:instructeur) { Instructeur.find_by(email: email) }
|
2016-05-24 16:39:39 +02:00
|
|
|
|
|
|
|
before do
|
2019-08-06 11:02:54 +02:00
|
|
|
create :instructeur, email: email, administrateurs: [admin]
|
2016-05-24 16:39:39 +02:00
|
|
|
|
2019-08-09 10:46:39 +02:00
|
|
|
sign_out(admin.user)
|
|
|
|
sign_in(admin_2.user)
|
2016-05-24 16:39:39 +02:00
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(response.status).to eq(302) }
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect { response }.not_to change(Instructeur, :count) }
|
2016-05-24 16:39:39 +02:00
|
|
|
it { expect(flash[:notice]).to be_present }
|
2016-02-09 11:00:13 +01:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect(admin_2.instructeurs).to include instructeur }
|
|
|
|
it { expect(instructeur.administrateurs.size).to eq 2 }
|
2016-05-24 16:39:39 +02:00
|
|
|
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' }
|
2019-08-06 11:02:54 +02:00
|
|
|
let(:instructeur) { Instructeur.find_by(email: email.downcase) }
|
2017-08-28 15:36:34 +02:00
|
|
|
|
|
|
|
before do
|
2019-08-06 11:02:54 +02:00
|
|
|
create :instructeur, email: email, administrateurs: [admin]
|
2017-08-28 15:36:34 +02:00
|
|
|
|
2019-08-09 10:46:39 +02:00
|
|
|
sign_out(admin.user)
|
|
|
|
sign_in(admin_2.user)
|
2017-08-28 15:36:34 +02:00
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect(admin_2.instructeurs).to include instructeur }
|
2017-08-28 15:36:34 +02:00
|
|
|
end
|
|
|
|
|
2016-05-24 16:39:39 +02:00
|
|
|
context 'Email notification' do
|
2018-08-29 22:11:38 +02:00
|
|
|
it 'Notification email is sent when instructeur is create' do
|
2019-08-07 15:52:38 +02:00
|
|
|
expect_any_instance_of(User).to receive(:invite!)
|
2016-02-09 11:00:13 +01:00
|
|
|
subject
|
|
|
|
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 }
|
2019-08-06 11:02:54 +02:00
|
|
|
let!(:instructeur) { create :instructeur, email: email, administrateurs: [admin] }
|
2016-05-24 16:39:39 +02:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
subject { delete :destroy, params: { id: instructeur.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
|
2019-08-06 11:02:54 +02:00
|
|
|
instructeur.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) }
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect(response).to redirect_to admin_instructeurs_path }
|
|
|
|
it { expect(admin.instructeurs).not_to include instructeur }
|
|
|
|
it { expect(instructeur.administrateurs).not_to include admin }
|
2016-02-19 14:11:03 +01:00
|
|
|
end
|
2016-02-09 12:23:49 +01:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
it { expect { subject }.not_to change(Instructeur, :count) }
|
2016-02-09 12:23:49 +01:00
|
|
|
end
|
2016-10-18 15:49:46 +02:00
|
|
|
end
|