2016-02-08 18:16:18 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe InvitesController, type: :controller do
|
2017-12-14 15:51:45 +01:00
|
|
|
let(:dossier) { create(:dossier, :en_construction) }
|
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
|
|
|
|
|
2018-08-01 17:23:25 +02:00
|
|
|
subject { post :create, params: { dossier_id: dossier.id, invite_email: email } }
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
context "when instructeur is signed_in" do
|
2019-08-07 11:15:16 +02:00
|
|
|
let(:signed_in_profile) { create(:instructeur).user }
|
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
|
2018-03-22 09:15:32 +01:00
|
|
|
it { expect { subject rescue nil }.to change(Invite, :count).by(0) }
|
2016-09-13 15:54:26 +02:00
|
|
|
end
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
context 'when instructeur has no access to dossier' do
|
2017-07-10 14:44:26 +02:00
|
|
|
it_behaves_like "he can not create invitation"
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
context 'when instructeur is invited for avis on dossier' do
|
2019-08-07 11:15:16 +02:00
|
|
|
before { Avis.create(instructeur: signed_in_profile.instructeur, claimant: create(:instructeur), 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
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
context 'when instructeur has access to dossier' do
|
2017-07-10 14:44:26 +02:00
|
|
|
before do
|
2019-08-07 11:15:16 +02:00
|
|
|
signed_in_profile.instructeur.procedures << dossier.procedure
|
2017-07-10 14:44:26 +02:00
|
|
|
end
|
|
|
|
|
2018-03-22 09:15:32 +01:00
|
|
|
it_behaves_like "he can not create invitation"
|
2017-07-10 14:44:26 +02:00
|
|
|
|
|
|
|
context 'when is a user who is loged' do
|
2018-03-22 09:15:32 +01:00
|
|
|
let(:user) { create(:user) }
|
2017-07-10 14:44:26 +02:00
|
|
|
before do
|
2018-03-22 09:15:32 +01:00
|
|
|
dossier.update(user: user)
|
|
|
|
sign_in(user)
|
2017-07-10 14:44:26 +02:00
|
|
|
end
|
2016-02-16 16:53:46 +01:00
|
|
|
|
2018-10-10 09:23:08 +02:00
|
|
|
it { expect { subject }.to change(Invite, :count).by(1) }
|
2018-03-22 09:15:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is signed_in" do
|
|
|
|
let(:signed_in_profile) { create(:user) }
|
|
|
|
|
|
|
|
shared_examples_for "he can not create a invite" do
|
|
|
|
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
|
2018-10-10 09:23:08 +02:00
|
|
|
it { expect { subject rescue nil }.to change(Invite, :count).by(0) }
|
2018-03-22 09:15:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has no access to dossier' do
|
|
|
|
it_behaves_like "he can not create a invite"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is invited on dossier' do
|
|
|
|
before { Invite.create(user: signed_in_profile, email: signed_in_profile.email, dossier: dossier) }
|
|
|
|
|
|
|
|
it_behaves_like "he can not create a invite"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has access to dossier' do
|
|
|
|
before do
|
2018-09-06 09:09:23 +02:00
|
|
|
request.env["HTTP_REFERER"] = "/dossiers/#{dossier.id}/brouillon"
|
2018-03-22 09:15:32 +01:00
|
|
|
dossier.update(user: signed_in_profile)
|
2016-02-16 16:53:46 +01:00
|
|
|
end
|
|
|
|
|
2018-10-10 09:23:08 +02:00
|
|
|
it { expect { subject }.to change(Invite, :count).by(1) }
|
2018-03-22 09:15:32 +01:00
|
|
|
|
2018-08-01 17:27:50 +02:00
|
|
|
it "redirects to the previous URL" do
|
2018-09-06 09:09:23 +02:00
|
|
|
expect(subject).to redirect_to("/dossiers/#{dossier.id}/brouillon")
|
2018-08-01 17:27:50 +02:00
|
|
|
end
|
|
|
|
|
2017-07-10 14:44:26 +02:00
|
|
|
context 'when email is assign to an user' do
|
2018-03-22 09:15:32 +01:00
|
|
|
let! (:user_invite) { 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
|
|
|
|
2018-03-22 09:15:32 +01:00
|
|
|
it { expect(invite.user).to eq user_invite }
|
2017-07-10 14:44:26 +02:00
|
|
|
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)
|
2018-05-25 18:05:28 +02:00
|
|
|
expect(InviteMailer).to receive(:deliver_later)
|
2017-07-10 14:44:26 +02:00
|
|
|
|
|
|
|
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)
|
2018-05-25 18:05:28 +02:00
|
|
|
expect(InviteMailer).to receive(:deliver_later)
|
2017-07-10 14:44:26 +02:00
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-11-13 16:18:52 +01:00
|
|
|
|
|
|
|
describe '#GET show' do
|
|
|
|
let(:user) { create :user }
|
|
|
|
|
|
|
|
context "when invite without email exists" do
|
|
|
|
let(:invite) { create(:invite, dossier: dossier) }
|
|
|
|
|
|
|
|
subject { get :show, params: { id: invite.id, email: email } }
|
|
|
|
|
|
|
|
context 'when email is not set' do
|
|
|
|
let(:email) { nil }
|
|
|
|
|
|
|
|
context 'and user is not connected' do
|
2019-01-14 16:16:23 +01:00
|
|
|
it 'redirects to the sign-in page' do
|
|
|
|
expect(subject).to redirect_to new_user_session_path
|
|
|
|
expect(controller.stored_location_for(:user)).to be_present
|
|
|
|
end
|
2018-11-13 16:18:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'and user is connected' do
|
|
|
|
let(:invite) { create :invite, dossier: dossier, user: user }
|
|
|
|
|
|
|
|
before { sign_in user }
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to(dossier_path(dossier)) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email is blank' do
|
|
|
|
let(:email) { '' }
|
|
|
|
|
2019-01-14 16:16:23 +01:00
|
|
|
it 'redirects to the sign-in page' do
|
|
|
|
expect(subject).to redirect_to new_user_session_path
|
|
|
|
expect(controller.stored_location_for(:user)).to be_present
|
|
|
|
end
|
2018-11-13 16:18:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email is not blank' do
|
|
|
|
context 'when email is affected at an user' do
|
|
|
|
let(:email) { user.email }
|
|
|
|
|
2019-01-14 16:16:23 +01:00
|
|
|
it 'redirects to the sign-in page' do
|
|
|
|
expect(subject).to redirect_to new_user_session_path
|
|
|
|
expect(controller.stored_location_for(:user)).to be_present
|
|
|
|
end
|
2018-11-13 16:18:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email is not affected at an user' do
|
|
|
|
let(:email) { 'new_user@octo.com' }
|
|
|
|
|
2019-01-14 16:16:23 +01:00
|
|
|
it 'redirects to the sign-up page' do
|
|
|
|
expect(subject).to redirect_to new_user_registration_path(user: { email: email })
|
|
|
|
expect(controller.stored_location_for(:user)).to be_present
|
|
|
|
end
|
2018-11-13 16:18:52 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when invite with email exists" do
|
|
|
|
let(:invite) { create :invite, email: email, dossier: dossier }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in user
|
|
|
|
end
|
|
|
|
|
|
|
|
subject! { get :show, params: { id: invite.id } }
|
|
|
|
|
2019-01-14 16:16:23 +01:00
|
|
|
it 'clears the stored return location' do
|
|
|
|
expect(controller.stored_location_for(:user)).to be nil
|
|
|
|
end
|
|
|
|
|
2018-11-13 16:18:52 +01:00
|
|
|
context 'when invitation ID is attached at the user email account' do
|
|
|
|
let(:email) { user.email }
|
|
|
|
|
|
|
|
context 'and dossier is a brouillon' do
|
|
|
|
let(:dossier) { create :dossier, state: Dossier.states.fetch(:brouillon) }
|
|
|
|
it { is_expected.to redirect_to brouillon_dossier_path(dossier) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and dossier is not a brouillon' do
|
|
|
|
let(:dossier) { create :dossier, :en_construction }
|
|
|
|
it { is_expected.to redirect_to(dossier_path(dossier)) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when invitation ID is not attached at the user email account' do
|
|
|
|
let(:email) { 'fake@email.com' }
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to dossiers_path }
|
|
|
|
it { expect(flash[:alert]).to be_present }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|