2016-02-08 18:16:18 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe InvitesController, type: :controller do
|
2017-09-21 17:12:21 +02:00
|
|
|
let(:dossier) { create(:dossier, :initiated) }
|
2016-02-08 18:16:18 +01:00
|
|
|
let(:email) { 'plop@octo.com' }
|
|
|
|
|
|
|
|
describe '#POST create' do
|
|
|
|
let(:invite) { Invite.last }
|
|
|
|
|
|
|
|
before do
|
2017-07-10 14:44:26 +02:00
|
|
|
sign_in signed_in_profile
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
|
2016-11-15 04:24:09 +01:00
|
|
|
subject { post :create, params: {dossier_id: dossier.id, email: email} }
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
context "when gestionnaire is signed_in" do
|
|
|
|
let(:signed_in_profile) { create(:gestionnaire) }
|
2016-09-13 15:54:26 +02:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
shared_examples_for "he can not create invitation" do
|
|
|
|
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
|
|
|
|
it { expect { subject rescue nil }.to change(InviteGestionnaire, :count).by(0) }
|
2016-09-13 15:54:26 +02:00
|
|
|
end
|
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
context 'when gestionnaire has no access to dossier' do
|
|
|
|
it_behaves_like "he can not create invitation"
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
context 'when gestionnaire is invited for avis on dossier' do
|
|
|
|
before { Avis.create(gestionnaire: signed_in_profile, claimant: create(:gestionnaire), dossier: dossier) }
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
it_behaves_like "he can not create invitation"
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
context 'when gestionnaire has access to dossier' do
|
|
|
|
before do
|
|
|
|
signed_in_profile.procedures << dossier.procedure
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { subject }.to change(InviteGestionnaire, :count).by(1) }
|
|
|
|
|
|
|
|
context 'when is a user who is loged' do
|
|
|
|
before do
|
|
|
|
sign_in create(:user)
|
|
|
|
end
|
2016-02-16 16:53:46 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
it { expect { subject }.to change(InviteGestionnaire, :count).by(1) }
|
2016-02-16 16:53:46 +01:00
|
|
|
end
|
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
context 'when email is assign to an user' do
|
|
|
|
let! (:user) { create(:user, email: email) }
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
describe 'Invite information' do
|
|
|
|
let(:email) { 'PLIP@octo.com' }
|
|
|
|
let(:invite) { Invite.last }
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
it 'email is on lower case' do
|
|
|
|
expect(invite.email).to eq 'plip@octo.com'
|
|
|
|
end
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
it { expect(invite.user).to eq user }
|
|
|
|
it { expect(flash[:notice]).to be_present }
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
context 'when email is not assign to an user' do
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
it { expect(invite.user).to be_nil }
|
|
|
|
it { expect(flash[:notice]).to be_present }
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
describe 'not an email' do
|
|
|
|
context 'when email is not valid' do
|
|
|
|
let(:email) { 'plip.com' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { subject }.not_to change(Invite, :count) }
|
|
|
|
it { expect(flash[:alert]).to be_present }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email is already used' do
|
|
|
|
let!(:invite) { create(:invite, dossier: dossier) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { subject }.not_to change(Invite, :count) }
|
|
|
|
it { expect(flash[:alert]).to be_present }
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
describe 'send invitation email' do
|
|
|
|
context 'when user does not exist' do
|
|
|
|
it 'send email' do
|
|
|
|
expect(InviteMailer).to receive(:invite_guest).and_return(InviteMailer)
|
|
|
|
expect(InviteMailer).to receive(:deliver_now!)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user exist' do
|
|
|
|
before do
|
|
|
|
create :user, email: email
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'send email' do
|
|
|
|
expect(InviteMailer).to receive(:invite_user).and_return(InviteMailer)
|
|
|
|
expect(InviteMailer).to receive(:deliver_now!)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
context "when user is signed_in" do
|
|
|
|
let(:signed_in_profile) { create(:user) }
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
shared_examples_for "he can not create a invite" do
|
|
|
|
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
|
|
|
|
it { expect { subject rescue nil }.to change(InviteUser, :count).by(0) }
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
context 'when user has no access to dossier' do
|
|
|
|
it_behaves_like "he can not create a invite"
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
context 'when user is invited on dossier' do
|
|
|
|
before { Invite.create(user: signed_in_profile, email: signed_in_profile.email, dossier: dossier) }
|
2016-02-09 10:51:15 +01:00
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
it_behaves_like "he can not create a invite"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has access to dossier' do
|
|
|
|
before do
|
|
|
|
dossier.update_attributes(user: signed_in_profile)
|
2016-02-09 10:51:15 +01:00
|
|
|
end
|
2017-07-10 14:44:26 +02:00
|
|
|
|
|
|
|
it { expect { subject }.to change(InviteUser, :count).by(1) }
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|