demarches-normaliennes/spec/controllers/administrateurs/services_controller_spec.rb

299 lines
9.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
describe Administrateurs::ServicesController, type: :controller do
let(:admin) { administrateurs(:default_admin) }
let(:procedure) { create(:procedure, administrateur: admin) }
2018-04-19 10:44:14 +02:00
describe '#new' do
let(:admin) { administrateurs(:default_admin) }
let(:procedure) { create(:procedure, administrateur: admin) }
before do
sign_in(admin.user)
end
subject { get :new, params: { procedure_id: procedure.id } }
context 'when admin has a SIRET from AgentConnect' do
let(:siret) { "20004021000060" }
before do
agi = build(:agent_connect_information, siret:)
admin.instructeur.agent_connect_information << agi
end
it 'prefills the SIRET and fetches service information' do
VCR.use_cassette("annuaire_service_public_success_#{siret}") do
subject
expect(assigns[:service].siret).to eq(siret)
expect(assigns[:service].nom).to eq("Communauté de communes - Lacs et Gorges du Verdon")
expect(assigns[:service].adresse).to eq("242 avenue Albert-1er 83630 Aups")
expect(assigns[:prefilled]).to eq(:success)
end
end
end
context 'when admin has no SIRET from AgentConnect' do
it 'does not prefill the SIRET' do
subject
expect(assigns[:service].siret).to be_nil
expect(assigns[:prefilled]).to be_nil
end
end
end
describe '#prefill' do
2018-04-19 10:44:14 +02:00
before do
2019-08-09 10:46:39 +02:00
sign_in(admin.user)
end
subject { get :prefill, params:, xhr: true }
context 'when prefilling from a SIRET' do
let(:params) do
{
procedure_id: procedure.id,
siret: "20004021000060"
}
end
it "prefill from annuaire public" do
VCR.use_cassette('annuaire_service_public_success_20004021000060') do
subject
expect(response.body).to include('turbo-stream')
expect(assigns[:service].nom).to eq("Communauté de communes - Lacs et Gorges du Verdon")
expect(assigns[:service].adresse).to eq("242 avenue Albert-1er 83630 Aups")
end
end
end
context 'when attempting to prefilling from invalid SIRET' do
let(:params) do
{
procedure_id: procedure.id,
siret: "20004021000000"
}
end
it "render an error" do
subject
expect(response.body).to include('turbo-stream')
expect(assigns[:service].nom).to be_nil
expect(assigns[:service].errors.key?(:siret)).to be_present
end
end
context 'when attempting to prefilling from not service public SIRET' do
let(:params) do
{
procedure_id: procedure.id,
siret: "41816609600051"
}
end
it "render partial information" do
VCR.use_cassette('annuaire_service_public_success_41816609600051') do
subject
expect(response.body).to include('turbo-stream')
expect(assigns[:service].nom).to eq("OCTO-TECHNOLOGY")
expect(assigns[:service].horaires).to be_nil
expect(assigns[:service].errors.key?(:siret)).not_to be_present
end
end
2018-04-19 10:44:14 +02:00
end
end
describe '#create' do
before do
sign_in(admin.user)
end
subject { post :create, params: }
2018-04-19 10:44:14 +02:00
context 'when submitting a new service' do
2018-04-19 15:59:11 +02:00
let(:params) do
{
service: {
nom: 'super service',
organisme: 'organisme',
2019-02-18 16:18:09 +01:00
type_organisme: 'association',
email: 'email@toto.com',
telephone: '1234',
horaires: 'horaires',
adresse: 'adresse',
2024-09-02 17:13:43 +02:00
siret: "35600011719156"
2018-04-19 15:59:11 +02:00
},
procedure_id: procedure.id
2018-04-19 15:59:11 +02:00
}
end
2018-04-19 10:44:14 +02:00
2022-10-25 14:36:58 +02:00
it do
subject
2022-10-25 14:36:58 +02:00
expect(flash.alert).to be_nil
expect(flash.notice).to eq('super service créé')
expect(Service.last.nom).to eq('super service')
expect(Service.last.organisme).to eq('organisme')
expect(Service.last.type_organisme).to eq(Service.type_organismes.fetch(:association))
expect(Service.last.email).to eq('email@toto.com')
expect(Service.last.telephone).to eq('1234')
expect(Service.last.horaires).to eq('horaires')
expect(Service.last.adresse).to eq('adresse')
2024-09-02 17:13:43 +02:00
expect(Service.last.siret).to eq('35600011719156')
2022-10-25 14:36:58 +02:00
expect(APIEntreprise::ServiceJob).to have_been_enqueued.with(Service.last.id)
expect(response).to redirect_to(admin_services_path(procedure_id: procedure.id))
end
2018-04-19 10:44:14 +02:00
end
context 'when submitting an invalid service' do
let(:params) { { service: { nom: 'super service' }, procedure_id: procedure.id } }
2018-04-19 10:44:14 +02:00
it do
subject
expect(flash.alert).not_to be_nil
expect(response).to render_template(:new)
expect(assigns(:service).nom).to eq('super service')
end
2018-04-19 10:44:14 +02:00
end
end
2018-04-19 18:12:48 +02:00
describe '#update' do
let!(:service) { create(:service, administrateur: admin) }
let(:service_params) { { nom: 'nom', type_organisme: Service.type_organismes.fetch(:association), siret: "13002526500013" } }
2018-04-19 18:12:48 +02:00
before do
2019-08-09 10:46:39 +02:00
sign_in(admin.user)
2018-04-19 15:59:11 +02:00
params = {
id: service.id,
service: service_params,
procedure_id: procedure.id
2018-04-19 15:59:11 +02:00
}
patch :update, params: params
2018-04-19 18:12:48 +02:00
end
context 'when updating a service' do
it { expect(flash.alert).to be_nil }
it { expect(flash.notice).to eq('nom modifié') }
it { expect(Service.last.nom).to eq('nom') }
2019-02-18 16:18:09 +01:00
it { expect(Service.last.type_organisme).to eq(Service.type_organismes.fetch(:association)) }
it { expect(response).to redirect_to(admin_services_path(procedure_id: procedure.id)) }
it { expect(APIEntreprise::ServiceJob).to have_been_enqueued.with(service.id) }
2018-04-19 18:12:48 +02:00
end
context 'when updating a service with invalid data' do
2019-02-18 16:18:09 +01:00
let(:service_params) { { nom: '', type_organisme: Service.type_organismes.fetch(:association) } }
2018-04-19 18:12:48 +02:00
it { expect(flash.alert).not_to be_nil }
it { expect(response).to render_template(:edit) }
end
end
2018-04-20 12:03:02 +02:00
describe '#add_to_procedure' do
let!(:procedure) { create(:procedure, administrateur: admin) }
let!(:service) { create(:service, administrateur: admin) }
def post_add_to_procedure
2019-08-09 10:46:39 +02:00
sign_in(admin.user)
2018-04-20 12:03:02 +02:00
params = {
procedure: {
id: procedure.id,
service_id: service.id
}
}
patch :add_to_procedure, params: params
procedure.reload
end
context 'when adding a service to a procedure' do
before { post_add_to_procedure }
it { expect(flash.alert).to be_nil }
it { expect(flash.notice).to eq("service affecté : #{service.nom}") }
it { expect(procedure.service_id).to eq(service.id) }
it { expect(response).to redirect_to(admin_procedure_path(procedure.id)) }
end
context 'when stealing a service to add it to a procedure' do
let!(:service) { create(:service) }
it { expect { post_add_to_procedure }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
2018-04-23 15:32:27 +02:00
describe '#destroy' do
let!(:service) { create(:service, administrateur: admin) }
context 'when a service has no related procedure' do
before do
2019-08-09 10:46:39 +02:00
sign_in(admin.user)
delete :destroy, params: { id: service.id, procedure_id: procedure.id }
2018-04-23 15:32:27 +02:00
end
it { expect { service.reload }.to raise_error(ActiveRecord::RecordNotFound) }
2018-04-23 15:32:27 +02:00
it { expect(flash.alert).to be_nil }
it { expect(flash.notice).to eq("#{service.nom} est supprimé") }
it { expect(response).to redirect_to(admin_services_path(procedure_id: procedure.id)) }
2018-04-23 15:32:27 +02:00
end
context 'when a service still has some related procedures' do
let!(:procedure) { create(:procedure, service: service) }
before do
2019-08-09 10:46:39 +02:00
sign_in(admin.user)
delete :destroy, params: { id: service.id, procedure_id: procedure.id }
2018-04-23 15:32:27 +02:00
end
it { expect(service.reload).not_to be_nil }
it { expect(flash.alert).to eq("la démarche #{procedure.libelle} utilise encore le service #{service.nom}. Veuillez l'affecter à un autre service avant de pouvoir le supprimer") }
2018-04-23 15:32:27 +02:00
it { expect(flash.notice).to be_nil }
it { expect(response).to redirect_to(admin_services_path(procedure_id: procedure.id)) }
2018-04-23 15:32:27 +02:00
end
context "when a service has some related discarded procedures" do
let!(:procedure) { create(:procedure, :discarded, service: service) }
before do
sign_in(admin.user)
delete :destroy, params: { id: service.id, procedure_id: procedure.id }
end
it { expect { service.reload }.to raise_error(ActiveRecord::RecordNotFound) }
it { expect(flash.alert).to be_nil }
it { expect(flash.notice).to eq("#{service.nom} est supprimé") }
it { expect(procedure.reload.service_id).to be_nil }
end
2018-04-23 15:32:27 +02:00
end
2023-07-24 11:05:02 +02:00
describe "#index" do
let(:admin) { administrateurs(:default_admin) }
2023-07-24 11:05:02 +02:00
before do
sign_in(admin.user)
end
context 'when admin has service without siret' do
let(:service) { create(:service) }
let(:procedure) { create(:procedure, :published, service: service, administrateur: admin) }
it 'display alert when admin has service without siret' do
service.siret = nil
service.save(validate: false)
get :index, params: { procedure_id: procedure.id }
2023-07-25 13:06:24 +02:00
expect(flash.alert.first).to eq "Vous navez pas renseigné le siret du service pour certaines de vos démarches. Merci de les modifier."
2023-07-24 11:05:02 +02:00
expect(flash.alert.last).to include(service.nom)
end
end
context 'when admin has procedure without service' do
let(:procedure) { create(:procedure, :published, service: nil, administrateur: admin) }
it 'display alert' do
get :index, params: { procedure_id: procedure.id }
expect(procedure.service).to be nil
2023-07-25 13:06:24 +02:00
expect(flash.alert.first).to eq "Certaines de vos démarches nont pas de service associé."
expect(flash.alert.last).to include "démarche #{procedure.id}"
end
end
2023-07-24 11:05:02 +02:00
end
2018-04-19 10:44:14 +02:00
end