2016-02-05 17:40:58 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Admin::GestionnairesController, type: :controller do
|
|
|
|
let(:admin) { create(:administrateur) }
|
|
|
|
before do
|
|
|
|
sign_in admin
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #index' do
|
|
|
|
subject { get :index }
|
|
|
|
it { expect(subject.status).to eq(200) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST #create' do
|
|
|
|
let(:email) { 'test@plop.com' }
|
2016-02-09 11:00:13 +01:00
|
|
|
subject { post :create, gestionnaire: { email: email } }
|
|
|
|
|
|
|
|
context 'When email is valid' do
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
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-02-09 11:00:13 +01:00
|
|
|
describe 'Gestionnaire attributs in database' do
|
|
|
|
let(:gestionnaire) { Gestionnaire.last }
|
|
|
|
it { expect(gestionnaire.email).to eq(email) }
|
|
|
|
it { expect(gestionnaire.administrateur_id).to eq(admin.id) }
|
|
|
|
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-02-05 17:40:58 +01:00
|
|
|
let(:email) { 'piou' }
|
|
|
|
it { expect(response.status).to eq(302) }
|
|
|
|
it { expect{ response }.not_to change(Gestionnaire, :count) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email is empty' do
|
2016-02-09 11:00:13 +01:00
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
2016-02-05 17:40:58 +01: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)
|
|
|
|
expect(GestionnaireMailer).not_to receive(:deliver_now!)
|
|
|
|
end
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context ' when email already exists' do
|
|
|
|
let(:email) { 'test@plop.com' }
|
|
|
|
before do
|
2016-02-09 11:00:13 +01:00
|
|
|
subject
|
2016-02-05 17:40:58 +01:00
|
|
|
post :create, gestionnaire: { email: email }
|
|
|
|
end
|
|
|
|
it { expect(response.status).to eq(302) }
|
|
|
|
it { expect{ response }.not_to change(Gestionnaire, :count) }
|
|
|
|
end
|
|
|
|
|
2016-02-09 11:00:13 +01:00
|
|
|
context 'Email notification' do
|
|
|
|
|
|
|
|
it 'Notification email is sent when email is valid' do
|
|
|
|
expect(GestionnaireMailer).to receive(:new_gestionnaire).and_return(GestionnaireMailer)
|
|
|
|
expect(GestionnaireMailer).to receive(:deliver_now!)
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'is not sent when email is not valid' do
|
|
|
|
let(:email) { 'testplop.com' }
|
|
|
|
it {
|
|
|
|
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
|
|
|
|
expect(GestionnaireMailer).not_to receive(:deliver_now!)
|
|
|
|
subject
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not sent when email already exists' do
|
|
|
|
subject
|
|
|
|
expect(GestionnaireMailer).not_to receive(:new_gestionnaire)
|
|
|
|
expect(GestionnaireMailer).not_to receive(:deliver_now!)
|
|
|
|
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-02-19 14:11:03 +01:00
|
|
|
let!(:gestionnaire) { create :gestionnaire, email: email }
|
|
|
|
subject { delete :destroy, 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
|
|
|
|
end
|
|
|
|
it { expect(response.status).to eq(302) }
|
|
|
|
it { expect(response).to redirect_to admin_gestionnaires_path }
|
|
|
|
it { expect{Gestionnaire.find(gestionnaire.id)}.to raise_error ActiveRecord::RecordNotFound}
|
|
|
|
end
|
2016-02-09 12:23:49 +01:00
|
|
|
|
2016-02-19 14:11:03 +01:00
|
|
|
it { expect{subject}.to change(Gestionnaire, :count).by(-1) }
|
2016-02-09 12:23:49 +01:00
|
|
|
end
|
|
|
|
|
2016-02-05 17:40:58 +01:00
|
|
|
end
|