2017-05-02 16:20:16 +02:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Backoffice::AvisController, type: :controller do
|
|
|
|
|
|
|
|
describe '#POST create' do
|
|
|
|
let(:gestionnaire){ create(:gestionnaire) }
|
|
|
|
let!(:dossier){ create(:dossier, state: 'received') }
|
|
|
|
let!(:assign_to){ create(:assign_to, gestionnaire: gestionnaire, procedure: dossier.procedure )}
|
|
|
|
|
|
|
|
subject { post :create, params: { dossier_id: dossier.id, avis: { email: gestionnaire.email, introduction: "Bonjour, regardez ce joli dossier." } } }
|
|
|
|
|
|
|
|
context 'when gestionnaire is not authenticated' do
|
|
|
|
it { is_expected.to redirect_to new_user_session_path }
|
|
|
|
it { expect{ subject }.to_not change(Avis, :count) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire is authenticated' do
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'When gestionnaire is known' do
|
|
|
|
it { is_expected.to redirect_to backoffice_dossier_path(dossier.id) }
|
|
|
|
it { expect{ subject }.to change(Avis, :count).by(1) }
|
|
|
|
it do
|
|
|
|
subject
|
|
|
|
expect(gestionnaire.avis.last).to_not eq(nil)
|
|
|
|
expect(gestionnaire.avis.last.email).to eq(nil)
|
|
|
|
expect(gestionnaire.avis.last.dossier_id).to eq(dossier.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-24 18:45:31 +02:00
|
|
|
describe '#POST update' do
|
|
|
|
let(:gestionnaire){ create(:gestionnaire) }
|
|
|
|
let(:dossier){ create(:dossier, state: 'received') }
|
|
|
|
let(:avis){ create(:avis, dossier: dossier, gestionnaire: gestionnaire )}
|
|
|
|
|
|
|
|
subject { post :update, params: { dossier_id: dossier.id, id: avis.id, avis: { answer: "Ok ce dossier est valide." } } }
|
|
|
|
|
2017-05-22 17:10:17 +02:00
|
|
|
before :each do
|
|
|
|
notification = double('notification', notify: true)
|
|
|
|
allow(NotificationService).to receive(:new).and_return(notification)
|
|
|
|
end
|
|
|
|
|
2017-05-24 18:45:31 +02:00
|
|
|
context 'when gestionnaire is not authenticated' do
|
|
|
|
it { is_expected.to redirect_to new_user_session_path }
|
|
|
|
it { expect(avis.answer).to be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when gestionnaire is authenticated' do
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and is invited on dossier' do
|
|
|
|
it { is_expected.to redirect_to backoffice_dossier_path(dossier.id) }
|
|
|
|
it do
|
|
|
|
subject
|
|
|
|
expect(avis.reload.answer).to eq("Ok ce dossier est valide.")
|
2017-05-22 17:10:17 +02:00
|
|
|
expect(NotificationService).to have_received(:new).at_least(:once)
|
2017-05-24 18:45:31 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'but is not invited on dossier' do
|
|
|
|
let(:gestionnaire2) { create(:gestionnaire) }
|
|
|
|
let(:avis){ create(:avis, dossier: dossier, gestionnaire: gestionnaire2 )}
|
|
|
|
|
|
|
|
it { expect{ subject }.to raise_error(ActiveRecord::RecordNotFound) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-12 10:22:18 +02:00
|
|
|
describe '.sign_up' do
|
|
|
|
let(:invited_email) { 'invited@avis.com' }
|
|
|
|
let(:dossier) { create(:dossier) }
|
2017-05-23 13:28:17 +02:00
|
|
|
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
|
2017-05-12 10:22:18 +02:00
|
|
|
let(:invitations_email) { true }
|
|
|
|
|
2017-05-05 17:38:43 +02:00
|
|
|
context 'when the new gestionnaire has never signed up' do
|
|
|
|
before do
|
|
|
|
expect(Avis).to receive(:avis_exists_and_email_belongs_to_avis?)
|
|
|
|
.with(avis.id.to_s, invited_email)
|
|
|
|
.and_return(invitations_email)
|
|
|
|
get :sign_up, params: { id: avis.id, email: invited_email }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the email belongs to the invitation' do
|
|
|
|
it { expect(subject.status).to eq(200) }
|
|
|
|
it { expect(assigns(:email)).to eq(invited_email) }
|
|
|
|
it { expect(assigns(:dossier)).to eq(dossier) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the email does not belong to the invitation' do
|
|
|
|
let(:invitations_email) { false }
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to root_path }
|
|
|
|
end
|
2017-05-12 10:22:18 +02:00
|
|
|
end
|
|
|
|
|
2017-05-05 17:38:43 +02:00
|
|
|
context 'when the gestionnaire has already signed up and belongs to the invitation' do
|
|
|
|
let(:gestionnaire) { create(:gestionnaire, email: invited_email) }
|
2017-05-23 13:28:17 +02:00
|
|
|
let!(:avis) { create(:avis, dossier: dossier, gestionnaire: gestionnaire) }
|
2017-05-05 17:38:43 +02:00
|
|
|
|
|
|
|
context 'when the gestionnaire is authenticated' do
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
get :sign_up, params: { id: avis.id, email: invited_email }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to backoffice_dossier_url(avis.dossier) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the gestionnaire is not authenticated' do
|
|
|
|
before do
|
|
|
|
get :sign_up, params: { id: avis.id, email: invited_email }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to new_gestionnaire_session_url }
|
|
|
|
end
|
2017-05-12 10:22:18 +02:00
|
|
|
end
|
|
|
|
|
2017-05-05 17:38:43 +02:00
|
|
|
context 'when the gestionnaire has already signed up / is authenticated and does not belong to the invitation' do
|
|
|
|
let(:gestionnaire) { create(:gestionnaire, email: 'other@gmail.com') }
|
2017-05-23 13:28:17 +02:00
|
|
|
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
|
2017-05-05 17:38:43 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in gestionnaire
|
|
|
|
get :sign_up, params: { id: avis.id, email: invited_email }
|
|
|
|
end
|
2017-05-12 10:22:18 +02:00
|
|
|
|
2017-05-05 17:38:43 +02:00
|
|
|
# redirected to dossier but then the gestionnaire gonna be banished !
|
|
|
|
it { is_expected.to redirect_to backoffice_dossier_url(avis.dossier) }
|
2017-05-12 10:22:18 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-12 10:22:53 +02:00
|
|
|
describe '.create_gestionnaire' do
|
|
|
|
let(:invited_email) { 'invited@avis.com' }
|
|
|
|
let(:dossier) { create(:dossier) }
|
2017-05-23 13:28:17 +02:00
|
|
|
let!(:avis) { create(:avis, email: invited_email, dossier: dossier) }
|
2017-05-12 10:22:53 +02:00
|
|
|
let(:avis_id) { avis.id }
|
|
|
|
let(:password) { '12345678' }
|
|
|
|
let(:created_gestionnaire) { Gestionnaire.find_by(email: invited_email) }
|
|
|
|
let(:invitations_email) { true }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Avis).to receive(:link_avis_to_gestionnaire)
|
|
|
|
expect(Avis).to receive(:avis_exists_and_email_belongs_to_avis?)
|
|
|
|
.with(avis_id.to_s, invited_email)
|
|
|
|
.and_return(invitations_email)
|
|
|
|
|
|
|
|
post :create_gestionnaire, params: { id: avis_id,
|
|
|
|
email: invited_email,
|
|
|
|
gestionnaire: {
|
|
|
|
password: password
|
|
|
|
} }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the email does not belong to the invitation' do
|
|
|
|
let(:invitations_email) { false }
|
|
|
|
|
|
|
|
it { is_expected.to redirect_to root_path }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the email belongs to the invitation' do
|
|
|
|
context 'when the gestionnaire creation succeeds' do
|
|
|
|
it { expect(created_gestionnaire).to be_present }
|
|
|
|
it { expect(created_gestionnaire.valid_password?(password)).to be true }
|
|
|
|
|
|
|
|
it { expect(Avis).to have_received(:link_avis_to_gestionnaire) }
|
|
|
|
|
|
|
|
it { expect(subject.current_gestionnaire).to eq(created_gestionnaire) }
|
|
|
|
it { is_expected.to redirect_to backoffice_dossier_path(dossier) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the gestionnaire creation fails' do
|
|
|
|
let(:password) { '' }
|
|
|
|
|
|
|
|
it { expect(created_gestionnaire).to be_nil }
|
|
|
|
it { is_expected.to redirect_to avis_sign_up_path(avis_id, invited_email) }
|
|
|
|
it { expect(flash.alert).to eq('Password : Le mot de passe est vide') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-02 16:20:16 +02:00
|
|
|
end
|