demarches-normaliennes/spec/controllers/admin/instructeurs_controller_spec.rb

181 lines
5.2 KiB
Ruby
Raw Normal View History

2016-02-05 17:40:58 +01:00
require 'spec_helper'
describe Admin::InstructeursController, type: :controller do
2016-02-05 17:40:58 +01:00
let(:admin) { create(:administrateur) }
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
subject { get :index }
it { expect(subject.status).to eq(200) }
2016-02-05 17:40:58 +01:00
end
describe 'GET #index with sorting and pagination' do
subject {
get :index, params: {
'instructeurs_smart_listing[page]': 1,
'instructeurs_smart_listing[per_page]': 10,
'instructeurs_smart_listing[sort][email]': 'asc'
}
}
it { expect(subject.status).to eq(200) }
end
2016-02-05 17:40:58 +01:00
describe 'POST #create' do
let(:email) { 'test@plop.com' }
let(:procedure_id) { nil }
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
let(:instructeur) { Instructeur.last }
2016-02-09 11:00:13 +01:00
it { expect(response.status).to eq(302) }
it { expect(response).to redirect_to admin_instructeurs_path }
2016-02-05 17:40:58 +01: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_assigns_path(procedure_id: procedure_id) }
end
describe 'Instructeur attributs in database' do
it { expect(instructeur.email).to eq(email) }
end
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
let(:email) { 'piou' }
it { expect(response.status).to eq(302) }
it { expect { response }.not_to change(Instructeur, :count) }
it { expect(flash[:alert]).to be_present }
describe 'Email Notification' do
it {
expect(InstructeurMailer).not_to receive(:new_instructeur)
expect(InstructeurMailer).not_to receive(:deliver_later)
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
let(:email) { '' }
it { expect(response.status).to eq(302) }
it { expect { response }.not_to change(Instructeur, :count) }
2016-02-09 11:00:13 +01:00
it 'Notification email is not send' do
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
context 'when email is already assign at the admin' do
2016-02-05 17:40:58 +01:00
before do
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-02-05 17:40:58 +01:00
it { expect(response.status).to eq(302) }
it { expect { response }.not_to change(Instructeur, :count) }
it { expect(flash[:alert]).to be_present }
describe 'Email notification' do
it 'is not sent when email already exists' do
expect(InstructeurMailer).not_to receive(:new_instructeur)
expect(InstructeurMailer).not_to receive(:deliver_later)
subject
end
end
2016-02-05 17:40:58 +01:00
end
context 'when an other admin will add the same email' do
let(:instructeur) { Instructeur.find_by(email: email) }
before do
create :instructeur, email: email, administrateurs: [admin]
2019-08-09 10:46:39 +02:00
sign_out(admin.user)
sign_in(admin_2.user)
subject
end
it { expect(response.status).to eq(302) }
it { expect { response }.not_to change(Instructeur, :count) }
it { expect(flash[:notice]).to be_present }
2016-02-09 11:00:13 +01:00
it { expect(admin_2.instructeurs).to include instructeur }
it { expect(instructeur.administrateurs.size).to eq 2 }
end
context 'when an other admin will add the same email with some uppercase in it' do
let(:email) { 'Test@Plop.com' }
let(:instructeur) { Instructeur.find_by(email: email.downcase) }
before do
create :instructeur, email: email, administrateurs: [admin]
2019-08-09 10:46:39 +02:00
sign_out(admin.user)
sign_in(admin_2.user)
subject
end
it { expect(admin_2.instructeurs).to include instructeur }
end
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' }
let!(:admin) { create :administrateur }
let!(:instructeur) { create :instructeur, email: email, administrateurs: [admin] }
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
admin.reload
instructeur.reload
2016-02-19 14:11:03 +01:00
end
2016-02-19 14:11:03 +01:00
it { expect(response.status).to eq(302) }
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
it { expect { subject }.not_to change(Instructeur, :count) }
2016-02-09 12:23:49 +01:00
end
end