demarches-normaliennes/spec/controllers/admin/procedures_controller_spec.rb

849 lines
28 KiB
Ruby
Raw Normal View History

2015-10-26 15:54:20 +01:00
require 'spec_helper'
2017-07-20 14:51:57 +02:00
require 'uri'
2015-10-26 15:54:20 +01:00
describe Admin::ProceduresController, type: :controller do
let(:admin) { create(:administrateur) }
let(:bad_procedure_id) { 100000 }
let(:path) { 'ma-jolie-demarche' }
2018-09-05 14:48:42 +02:00
let(:libelle) { 'Démarche de test' }
2015-10-26 15:54:20 +01:00
let(:description) { 'Description de test' }
let(:organisation) { 'Organisation de test' }
let(:direction) { 'Direction de test' }
2018-04-24 16:02:36 +02:00
let(:cadre_juridique) { 'cadre juridique' }
let(:duree_conservation_dossiers_dans_ds) { 3 }
let(:duree_conservation_dossiers_hors_ds) { 6 }
2019-07-17 15:34:10 +02:00
let(:monavis_embed) { nil }
let(:lien_site_web) { 'http://mon-site.gouv.fr' }
2015-10-26 15:54:20 +01:00
let(:procedure_params) {
{
path: path,
2018-01-15 19:02:12 +01:00
libelle: libelle,
description: description,
organisation: organisation,
direction: direction,
2018-05-02 15:41:43 +02:00
cadre_juridique: cadre_juridique,
duree_conservation_dossiers_dans_ds: duree_conservation_dossiers_dans_ds,
duree_conservation_dossiers_hors_ds: duree_conservation_dossiers_hors_ds,
monavis_embed: monavis_embed,
lien_site_web: lien_site_web
2015-10-26 15:54:20 +01:00
}
}
before do
2019-08-09 10:46:39 +02:00
sign_in(admin.user)
2015-10-26 15:54:20 +01:00
end
describe 'GET #index' do
subject { get :index }
it { expect(response.status).to eq(200) }
end
describe 'GET #index with sorting and pagination' do
before do
create(:procedure, administrateur: admin)
admin.reload
end
subject {
get :index, params: {
'procedures_smart_listing[page]': 1,
'procedures_smart_listing[per_page]': 10,
'procedures_smart_listing[sort][id]': 'asc'
}
}
it { expect(subject.status).to eq(200) }
end
describe 'GET #archived' do
subject { get :archived }
it { expect(response.status).to eq(200) }
end
describe 'GET #archived with sorting and pagination' do
subject {
get :archived, params: {
'procedures_smart_listing[page]': 1,
'procedures_smart_listing[per_page]': 10,
'procedures_smart_listing[sort][libelle]': 'asc'
}
}
it { expect(subject.status).to eq(200) }
end
describe 'GET #published' do
subject { get :published }
it { expect(response.status).to eq(200) }
end
describe 'GET #draft with sorting and pagination' do
subject {
get :draft, params: {
'procedures_smart_listing[page]': 1,
'procedures_smart_listing[per_page]': 10,
'procedures_smart_listing[sort][published_at]': 'asc'
}
}
it { expect(subject.status).to eq(200) }
end
describe 'DELETE #destroy' do
2019-01-23 16:13:14 +01:00
let(:procedure_draft) { create :procedure_with_dossiers, administrateur: admin, published_at: nil, archived_at: nil }
let(:procedure_published) { create :procedure_with_dossiers, administrateur: admin, aasm_state: :publiee, published_at: Time.zone.now, archived_at: nil }
let(:procedure_archived) { create :procedure_with_dossiers, administrateur: admin, aasm_state: :archivee, published_at: nil, archived_at: Time.zone.now }
subject { delete :destroy, params: { id: procedure.id } }
2019-01-23 16:11:16 +01:00
context 'when the procedure is a draft' do
let!(:procedure) { procedure_draft }
2019-01-23 16:11:16 +01:00
it 'destroys the procedure' do
expect { subject }.to change { Procedure.count }.by(-1)
end
2019-01-23 16:13:14 +01:00
it 'deletes associated dossiers' do
subject
expect(Dossier.find_by(procedure_id: procedure.id)).to be_blank
end
2019-01-23 16:11:16 +01:00
it 'redirects to the procedure drafts page' do
subject
2019-01-23 16:11:16 +01:00
expect(response).to redirect_to admin_procedures_draft_path
expect(flash[:notice]).to be_present
end
end
context 'when procedure is published' do
2019-01-23 16:13:14 +01:00
let!(:procedure) { procedure_published }
2019-01-23 16:13:14 +01:00
it { expect { subject }.not_to change { Procedure.count } }
it { expect { subject }.not_to change { Dossier.count } }
it { expect(subject.status).to eq 401 }
end
context 'when procedure is archived' do
2019-01-23 16:13:14 +01:00
let!(:procedure) { procedure_archived }
2019-01-23 16:13:14 +01:00
it { expect { subject }.not_to change { Procedure.count } }
it { expect { subject }.not_to change { Dossier.count } }
it { expect(subject.status).to eq 401 }
end
context "when administrateur does not own the procedure" do
let(:procedure_not_owned) { create :procedure, administrateur: create(:administrateur), published_at: nil, archived_at: nil }
subject { delete :destroy, params: { id: procedure_not_owned.id } }
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
describe 'GET #edit' do
let(:published_at) { nil }
let(:procedure) { create(:procedure, administrateur: admin, published_at: published_at) }
2015-11-04 17:27:01 +01:00
let(:procedure_id) { procedure.id }
2015-10-26 15:54:20 +01:00
subject { get :edit, params: { id: procedure_id } }
2015-10-26 15:54:20 +01:00
context 'when user is not connected' do
before do
2019-08-09 10:46:39 +02:00
sign_out(admin.user)
2015-10-26 15:54:20 +01:00
end
2017-02-15 17:09:25 +01:00
it { is_expected.to redirect_to new_user_session_path }
2015-10-26 15:54:20 +01:00
end
context 'when user is connected' do
context 'when procedure exist' do
2015-10-26 18:08:41 +01:00
let(:procedure_id) { procedure.id }
2017-02-15 17:09:25 +01:00
it { is_expected.to have_http_status(:success) }
2015-10-26 15:54:20 +01:00
end
context 'when procedure is published' do
2018-10-25 15:11:12 +02:00
let(:published_at) { Time.zone.now }
2017-02-15 17:09:25 +01:00
it { is_expected.to have_http_status(:success) }
end
2015-10-26 15:54:20 +01:00
context "when procedure doesn't exist" do
2015-10-26 18:08:41 +01:00
let(:procedure_id) { bad_procedure_id }
2015-10-26 15:54:20 +01:00
2017-02-15 17:09:25 +01:00
it { is_expected.to have_http_status(404) }
2015-10-26 15:54:20 +01:00
end
end
end
describe 'POST #create' do
2015-11-04 17:27:01 +01:00
context 'when all attributs are filled' do
2015-10-26 15:54:20 +01:00
describe 'new procedure in database' do
subject { post :create, params: { procedure: procedure_params } }
2015-10-26 15:54:20 +01:00
it { expect { subject }.to change { Procedure.count }.by(1) }
end
context 'when procedure is correctly save' do
before do
post :create, params: { procedure: procedure_params }
2015-10-26 15:54:20 +01:00
end
describe 'procedure attributs in database' do
subject { Procedure.last }
it { expect(subject.libelle).to eq(libelle) }
it { expect(subject.description).to eq(description) }
it { expect(subject.organisation).to eq(organisation) }
it { expect(subject.direction).to eq(direction) }
it { expect(subject.administrateurs).to eq([admin]) }
it { expect(subject.duree_conservation_dossiers_dans_ds).to eq(duree_conservation_dossiers_dans_ds) }
it { expect(subject.duree_conservation_dossiers_hors_ds).to eq(duree_conservation_dossiers_hors_ds) }
2015-10-26 15:54:20 +01:00
end
2018-11-14 16:28:02 +01:00
it { is_expected.to redirect_to(champs_procedure_path(Procedure.last)) }
2015-10-26 15:54:20 +01:00
it { expect(flash[:notice]).to be_present }
end
context 'when procedure is correctly saved' do
let(:instructeur) { admin.instructeur }
before do
post :create, params: { procedure: procedure_params }
end
describe "admin can also instruct the procedure as a instructeur" do
subject { Procedure.last }
it { expect(subject.defaut_groupe_instructeur.instructeurs).to include(instructeur) }
end
end
2015-10-26 15:54:20 +01:00
end
context 'when many attributs are not valid' do
let(:libelle) { '' }
let(:description) { '' }
describe 'no new procedure in database' do
subject { post :create, params: { procedure: procedure_params } }
2015-10-26 15:54:20 +01:00
it { expect { subject }.to change { Procedure.count }.by(0) }
describe 'no new module api carto in database' do
it { expect { subject }.to change { ModuleAPICarto.count }.by(0) }
end
2015-10-26 15:54:20 +01:00
end
describe 'flash message is present' do
before do
post :create, params: { procedure: procedure_params }
2015-10-26 15:54:20 +01:00
end
it { expect(flash[:alert]).to be_present }
end
end
end
describe 'PUT #update' do
let!(:procedure) { create(:procedure, :with_type_de_champ, administrateur: admin) }
2015-10-26 15:54:20 +01:00
context 'when administrateur is not connected' do
before do
2019-08-09 10:46:39 +02:00
sign_out(admin.user)
2015-10-26 15:54:20 +01:00
end
subject { put :update, params: { id: procedure.id } }
2015-10-26 15:54:20 +01:00
2017-02-15 17:09:25 +01:00
it { is_expected.to redirect_to new_user_session_path }
2015-10-26 15:54:20 +01:00
end
context 'when administrateur is connected' do
def update_procedure
put :update, params: { id: procedure.id, procedure: procedure_params }
2015-10-26 15:54:20 +01:00
procedure.reload
end
2017-02-15 17:09:25 +01:00
context 'when all attributs are present' do
2015-10-26 15:54:20 +01:00
let(:libelle) { 'Blable' }
let(:description) { 'blabla' }
let(:organisation) { 'plop' }
let(:direction) { 'plap' }
let(:duree_conservation_dossiers_dans_ds) { 7 }
let(:duree_conservation_dossiers_hors_ds) { 5 }
2015-10-26 15:54:20 +01:00
before { update_procedure }
2015-10-26 15:54:20 +01:00
describe 'procedure attributs in database' do
subject { procedure }
it { expect(subject.libelle).to eq(libelle) }
it { expect(subject.description).to eq(description) }
it { expect(subject.organisation).to eq(organisation) }
it { expect(subject.direction).to eq(direction) }
it { expect(subject.duree_conservation_dossiers_dans_ds).to eq(duree_conservation_dossiers_dans_ds) }
it { expect(subject.duree_conservation_dossiers_hors_ds).to eq(duree_conservation_dossiers_hors_ds) }
end
2017-02-15 17:09:25 +01:00
it { is_expected.to redirect_to(edit_admin_procedure_path id: procedure.id) }
2015-10-26 15:54:20 +01:00
it { expect(flash[:notice]).to be_present }
end
context 'when many attributs are not valid' do
before { update_procedure }
2015-10-26 15:54:20 +01:00
let(:libelle) { '' }
let(:description) { '' }
describe 'flash message is present' do
it { expect(flash[:alert]).to be_present }
end
end
2017-02-15 17:09:25 +01:00
context 'when procedure is brouillon' do
let(:procedure) { create(:procedure_with_dossiers, :with_path, :with_type_de_champ, administrateur: admin) }
let!(:dossiers_count) { procedure.dossiers.count }
describe 'dossiers are dropped' do
subject { update_procedure }
it {
expect(dossiers_count).to eq(1)
expect(subject.dossiers.count).to eq(0)
}
end
end
2017-02-15 17:09:25 +01:00
context 'when procedure is published' do
let(:procedure) { create(:procedure, :with_type_de_champ, :published, administrateur: admin) }
2017-02-15 17:09:25 +01:00
subject { update_procedure }
2017-02-15 17:09:25 +01:00
describe 'only some properties can be updated' do
2017-02-15 17:09:25 +01:00
it { expect(subject.libelle).to eq procedure_params[:libelle] }
it { expect(subject.description).to eq procedure_params[:description] }
it { expect(subject.organisation).to eq procedure_params[:organisation] }
it { expect(subject.direction).to eq procedure_params[:direction] }
it { expect(subject.for_individual).not_to eq procedure_params[:for_individual] }
end
end
2015-10-26 15:54:20 +01:00
end
end
2015-11-26 18:41:41 +01:00
describe 'PUT #publish' do
2019-07-23 12:09:48 +02:00
let(:procedure) { create(:procedure, administrateur: admin, lien_site_web: lien_site_web) }
let(:procedure2) { create(:procedure, :published, administrateur: admin, lien_site_web: lien_site_web) }
let(:procedure3) { create(:procedure, :published, lien_site_web: lien_site_web) }
let(:lien_site_web) { 'http://some.administration/' }
context 'when admin is the owner of the procedure' do
before do
put :publish, params: { procedure_id: procedure.id, path: path, lien_site_web: lien_site_web }
procedure.reload
procedure2.reload
end
context 'procedure path does not exist' do
2018-10-30 12:00:58 +01:00
let(:path) { 'new_path' }
2019-07-23 12:09:48 +02:00
let(:lien_site_web) { 'http://mon-site.gouv.fr' }
it 'publish the given procedure' do
2017-07-13 15:14:28 +02:00
expect(procedure.publiee?).to be_truthy
2018-10-30 12:00:58 +01:00
expect(procedure.path).to eq(path)
expect(procedure.lien_site_web).to eq(lien_site_web)
2018-05-17 15:43:33 +02:00
expect(response.status).to eq 302
2018-09-05 14:48:42 +02:00
expect(flash[:notice]).to have_content 'Démarche publiée'
end
end
context 'procedure path exists and is owned by current administrator' do
2018-10-30 12:00:58 +01:00
let(:path) { procedure2.path }
2019-07-23 12:09:48 +02:00
let(:lien_site_web) { 'http://mon-site.gouv.fr' }
it 'publish the given procedure' do
2017-07-13 15:14:28 +02:00
expect(procedure.publiee?).to be_truthy
2018-10-30 12:00:58 +01:00
expect(procedure.path).to eq(path)
expect(procedure.lien_site_web).to eq(lien_site_web)
2018-05-17 15:43:33 +02:00
expect(response.status).to eq 302
2018-09-05 14:48:42 +02:00
expect(flash[:notice]).to have_content 'Démarche publiée'
end
it 'archive previous procedure' do
expect(procedure2.archivee?).to be_truthy
end
end
context 'procedure path exists and is not owned by current administrator' do
2018-10-30 12:00:58 +01:00
let(:path) { procedure3.path }
2019-07-23 12:09:48 +02:00
let(:lien_site_web) { 'http://mon-site.gouv.fr' }
it 'does not publish the given procedure' do
2017-07-13 15:14:28 +02:00
expect(procedure.publiee?).to be_falsey
2019-03-06 14:42:27 +01:00
expect(procedure.path).not_to match(path)
2019-07-23 12:09:48 +02:00
expect(procedure.lien_site_web).to match(lien_site_web)
2016-06-30 12:33:43 +02:00
expect(response.status).to eq 200
end
it 'previous procedure remains published' do
2017-07-13 15:14:28 +02:00
expect(procedure2.publiee?).to be_truthy
expect(procedure2.archivee?).to be_falsey
expect(procedure2.path).to match(/fake_path/)
end
end
context 'procedure path is invalid' do
2018-10-30 12:00:58 +01:00
let(:path) { 'Invalid Procedure Path' }
2019-07-23 12:09:48 +02:00
let(:lien_site_web) { 'http://mon-site.gouv.fr' }
it 'does not publish the given procedure' do
2017-07-13 15:14:28 +02:00
expect(procedure.publiee?).to be_falsey
2019-03-06 14:42:27 +01:00
expect(procedure.path).not_to match(path)
2019-07-23 12:09:48 +02:00
expect(procedure.lien_site_web).to match(lien_site_web)
expect(response).to redirect_to :admin_procedures
2018-09-05 14:48:42 +02:00
expect(flash[:alert]).to have_content 'Lien de la démarche invalide'
end
end
end
context 'when admin is not the owner of the procedure' do
let(:admin_2) { create(:administrateur) }
before do
2019-08-09 10:46:39 +02:00
sign_out(admin.user)
sign_in(admin_2.user)
2018-10-30 12:00:58 +01:00
put :publish, params: { procedure_id: procedure.id, path: 'fake_path' }
procedure.reload
end
it 'fails' do
expect(response).to redirect_to :admin_procedures
2018-09-05 14:48:42 +02:00
expect(flash[:alert]).to have_content 'Démarche inexistante'
end
end
2019-07-23 12:09:48 +02:00
context 'when the admin does not provide a lien_site_web' do
before do
put :publish, params: { procedure_id: procedure.id, path: path, lien_site_web: lien_site_web }
procedure.reload
end
context 'procedure path is valid but lien_site_web is missing' do
let(:path) { 'new_path2' }
let(:lien_site_web) { nil }
it 'does not publish the given procedure' do
expect(procedure.publiee?).to be_falsey
end
end
end
end
2015-11-26 18:41:41 +01:00
describe 'PUT #archive' do
let(:procedure) { create(:procedure, :published, administrateur: admin, lien_site_web: lien_site_web) }
2015-11-26 18:41:41 +01:00
context 'when admin is the owner of the procedure' do
before do
put :archive, params: { procedure_id: procedure.id }
2015-11-26 18:41:41 +01:00
procedure.reload
end
context 'when owner want archive procedure' do
it { expect(procedure.archivee?).to be_truthy }
it { expect(response).to redirect_to :admin_procedures }
2018-09-05 14:48:42 +02:00
it { expect(flash[:notice]).to have_content 'Démarche archivée' }
end
2016-06-30 10:24:01 +02:00
context 'when owner want to re-enable procedure' do
before do
put :publish, params: { procedure_id: procedure.id, path: 'fake_path', lien_site_web: lien_site_web }
procedure.reload
end
2018-05-17 15:43:33 +02:00
it { expect(procedure.publiee?).to be_truthy }
it { expect(response.status).to eq 302 }
2018-09-05 14:48:42 +02:00
it { expect(flash[:notice]).to have_content 'Démarche publiée' }
end
2015-11-26 18:41:41 +01:00
end
context 'when admin is not the owner of the procedure' do
let(:admin_2) { create(:administrateur) }
before do
2019-08-09 10:46:39 +02:00
sign_out(admin.user)
sign_in(admin_2.user)
2015-11-26 18:41:41 +01:00
put :archive, params: { procedure_id: procedure.id }
2015-11-26 18:41:41 +01:00
procedure.reload
end
it { expect(response).to redirect_to :admin_procedures }
2018-09-05 14:48:42 +02:00
it { expect(flash[:alert]).to have_content 'Démarche inexistante' }
2015-11-26 18:41:41 +01:00
end
end
2016-06-15 11:34:05 +02:00
describe 'PUT #clone' do
2018-05-31 11:00:22 +02:00
let!(:procedure) { create(:procedure, :with_notice, :with_deliberation, administrateur: admin) }
let(:params) { { procedure_id: procedure.id } }
subject { put :clone, params: params }
2016-06-15 11:34:05 +02:00
2018-04-26 14:36:27 +02:00
before do
response = Typhoeus::Response.new(code: 200, body: 'Hello world')
Typhoeus.stub(/active_storage\/disk/).and_return(response)
end
2016-06-20 17:37:04 +02:00
it { expect { subject }.to change(Procedure, :count).by(1) }
2016-06-15 11:34:05 +02:00
context 'when admin is the owner of the procedure' do
before { subject }
2016-06-15 11:34:05 +02:00
it 'creates a new procedure and redirect to it' do
expect(response).to redirect_to edit_admin_procedure_path(id: Procedure.last.id)
2018-04-26 14:36:27 +02:00
expect(Procedure.last.cloned_from_library).to be_falsey
expect(Procedure.last.notice.attached?).to be_truthy
2018-05-31 11:00:22 +02:00
expect(Procedure.last.deliberation.attached?).to be_truthy
2018-09-05 14:48:42 +02:00
expect(flash[:notice]).to have_content 'Démarche clonée'
2016-06-15 11:34:05 +02:00
end
context 'when the procedure is cloned from the library' do
let(:params) { { procedure_id: procedure.id, from_new_from_existing: true } }
it { expect(Procedure.last.cloned_from_library).to be(true) }
end
2016-06-15 11:34:05 +02:00
end
context 'when admin is not the owner of the procedure' do
let(:admin_2) { create(:administrateur) }
before do
2019-08-09 10:46:39 +02:00
sign_out(admin.user)
sign_in(admin_2.user)
2016-06-15 11:34:05 +02:00
subject
end
it 'creates a new procedure and redirect to it' do
expect(response).to redirect_to edit_admin_procedure_path(id: Procedure.last.id)
2018-09-05 14:48:42 +02:00
expect(flash[:notice]).to have_content 'Démarche clonée'
end
2016-06-15 11:34:05 +02:00
end
end
describe 'GET #new_from_existing' do
before do
stub_const("Admin::ProceduresController::SIGNIFICANT_DOSSIERS_THRESHOLD", 2)
end
subject { get :new_from_existing }
let(:grouped_procedures) { subject; assigns(:grouped_procedures) }
let(:response_procedures) { grouped_procedures.map { |_o, procedures| procedures }.flatten }
describe 'selecting' do
let!(:large_draft_procedure) { create(:procedure_with_dossiers, dossiers_count: 2) }
let!(:large_published_procedure) { create(:procedure_with_dossiers, :published, dossiers_count: 2) }
let!(:large_archived_procedure) { create(:procedure_with_dossiers, :archived, dossiers_count: 2) }
let!(:small_archived_procedure) { create(:procedure_with_dossiers, :archived, dossiers_count: 1) }
it 'displays published and archived procedures' do
expect(response_procedures).to include(large_published_procedure)
expect(response_procedures).to include(large_archived_procedure)
end
it 'doesnt display procedures without a significant number of dossiers' do
expect(response_procedures).not_to include(small_archived_procedure)
end
it 'doesnt display draft procedures' do
expect(response_procedures).not_to include(large_draft_procedure)
end
end
describe 'grouping' do
let(:service_1) { create(:service, nom: 'DDT des Vosges') }
let(:service_2) { create(:service, nom: 'DDT du Loiret') }
let!(:procedure_with_service_1) { create(:procedure_with_dossiers, :published, organisation: nil, service: service_1, dossiers_count: 2) }
let!(:procedure_with_service_2) { create(:procedure_with_dossiers, :published, organisation: nil, service: service_2, dossiers_count: 2) }
let!(:procedure_without_service) { create(:procedure_with_dossiers, :published, organisation: 'DDT du Loiret', dossiers_count: 2) }
it 'groups procedures with services as well as procedures with organisations' do
expect(grouped_procedures.length).to eq 2
expect(grouped_procedures.find { |o, _p| o == 'DDT des Vosges' }.last).to contain_exactly(procedure_with_service_1)
expect(grouped_procedures.find { |o, _p| o == 'DDT du Loiret' }.last).to contain_exactly(procedure_with_service_2, procedure_without_service)
end
end
end
describe 'GET #path_list' do
let!(:procedure) { create(:procedure, :published, administrateur: admin) }
let(:admin2) { create(:administrateur) }
let!(:procedure2) { create(:procedure, :published, administrateur: admin2) }
2016-11-07 17:08:33 +01:00
let!(:procedure3) { create(:procedure, :published, administrateur: admin2) }
subject { get :path_list }
2016-11-07 17:08:33 +01:00
let(:body) { JSON.parse(response.body) }
2016-11-07 17:08:33 +01:00
describe 'when no params' do
before do
subject
end
2016-11-07 17:08:33 +01:00
it { expect(response.status).to eq(200) }
it { expect(body.size).to eq(3) }
it { expect(body.first['label']).to eq(procedure.path) }
it { expect(body.first['mine']).to be_truthy }
it { expect(body.second['label']).to eq(procedure2.path) }
it { expect(body.second['mine']).to be_falsy }
end
context 'filtered' do
2016-11-07 17:08:33 +01:00
before do
subject
end
2017-07-20 14:51:57 +02:00
subject { get :path_list, params: { request: URI.encode(procedure2.path) } }
it { expect(response.status).to eq(200) }
it { expect(body.size).to eq(1) }
it { expect(body.first['label']).to eq(procedure2.path) }
it { expect(body.first['mine']).to be_falsy }
end
2016-11-07 17:08:33 +01:00
context 'when procedure is archived' do
2018-08-14 15:17:22 +02:00
let!(:procedure3) { create(:procedure, :archived, administrateur: admin2) }
2016-11-07 17:08:33 +01:00
before do
subject
end
it 'do not return on the json' do
expect(body.size).to eq(2)
end
end
end
describe 'POST #transfer' do
let!(:procedure) { create :procedure, :with_service, administrateur: admin }
subject { post :transfer, params: { email_admin: email_admin, procedure_id: procedure.id } }
context 'when admin is unknow' do
let(:email_admin) { 'plop' }
it { expect(subject.status).to eq 404 }
end
context 'when admin is known' do
let!(:new_admin) { create :administrateur, email: 'new_admin@admin.com' }
context "and its email address is correct" do
let(:email_admin) { 'new_admin@admin.com' }
it { expect(subject.status).to eq 200 }
it { expect { subject }.to change(new_admin.procedures, :count).by(1) }
it "should create a new service" do
subject
expect(new_admin.procedures.last.service_id).not_to eq(procedure.service_id)
end
end
context 'when admin is know but its email was not downcased' do
let(:email_admin) { "NEW_admin@adMIN.com" }
it { expect(subject.status).to eq 200 }
it { expect { subject }.to change(Procedure, :count).by(1) }
end
describe "correctly assigns the new admin" do
let(:email_admin) { 'new_admin@admin.com' }
before do
subject
end
it { expect(Procedure.last.administrateurs).to eq [new_admin] }
end
end
end
describe "DELETE #delete_deliberation" do
context "with a demarche the admin owns" do
let(:procedure) { create(:procedure, :with_deliberation, administrateur: admin) }
before do
delete :delete_deliberation, params: { id: procedure.id }
procedure.reload
end
it { expect(procedure.deliberation.attached?).to eq(false) }
it { expect(response).to redirect_to(edit_admin_procedure_path(procedure)) }
end
context "with a demarche the admin does not own" do
let(:procedure) { create(:procedure, :with_deliberation) }
before do
delete :delete_deliberation, params: { id: procedure.id }
procedure.reload
end
it { expect(response.status).to eq(404) }
end
end
describe "DELETE #delete_notice" do
context "with a demarche the admin owns" do
let(:procedure) { create(:procedure, :with_notice, administrateur: admin) }
before do
delete :delete_notice, params: { id: procedure.id }
procedure.reload
end
it { expect(procedure.notice.attached?).to eq(false) }
it { expect(response).to redirect_to(edit_admin_procedure_path(procedure)) }
end
context "with a demarche the admin does not own" do
let(:procedure) { create(:procedure, :with_notice) }
before do
delete :delete_notice, params: { id: procedure.id }
procedure.reload
end
it { expect(response.status).to eq(404) }
end
end
2018-09-04 14:53:18 +02:00
describe "GET #check_availability" do
render_views
let(:procedure) { create(:procedure, :with_path, administrateur: admin) }
let(:params) {
{
procedure: {
path: path,
id: procedure.id
}
}
}
let(:path) { generate(:published_path) }
before do
get :check_availability, params: params, format: 'js'
end
context 'self path' do
let(:path) { procedure.path }
it { expect(response.body).to include("innerHTML = ''") }
end
context 'available path' do
it { expect(response.body).to include("innerHTML = ''") }
end
context 'my path (brouillon)' do
2018-09-04 14:53:18 +02:00
let(:procedure_owned) { create(:procedure, :with_path, administrateur: admin) }
let(:path) { procedure_owned.path }
it {
2019-01-16 17:23:33 +01:00
expect(response.body).to include('Une démarche en test existe déjà avec ce lien.')
}
end
context 'my path' do
let(:procedure_owned) { create(:procedure, :published, administrateur: admin) }
let(:path) { procedure_owned.path }
2018-09-04 14:53:18 +02:00
it {
2018-09-18 11:01:20 +02:00
expect(response.body).to include('Ce lien est déjà utilisé par une de vos démarche.')
expect(response.body).to include('Si vous voulez lutiliser, lancienne démarche sera archivée')
2018-09-04 14:53:18 +02:00
}
end
context 'unavailable path' do
let(:procedure_not_owned) { create(:procedure, :with_path, administrateur: create(:administrateur)) }
let(:path) { procedure_not_owned.path }
it {
2018-09-18 11:01:20 +02:00
expect(response.body).to include('Ce lien est déjà utilisé par une démarche.')
2018-09-04 14:53:18 +02:00
expect(response.body).to include('Vous ne pouvez pas lutiliser car il appartient à un autre administrateur.')
}
end
end
describe 'PATCH #monavis' do
let!(:procedure) { create(:procedure, administrateur: admin) }
let(:procedure_params) {
{
2019-07-17 15:34:10 +02:00
monavis_embed: monavis_embed
}
}
context 'when administrateur is not connected' do
before do
2019-08-09 10:46:39 +02:00
sign_out(admin.user)
end
subject { patch :update_monavis, params: { procedure_id: procedure.id } }
it { is_expected.to redirect_to new_user_session_path }
end
context 'when administrateur is connected' do
def update_monavis
patch :update_monavis, params: { procedure_id: procedure.id, procedure: procedure_params }
procedure.reload
end
2019-07-17 15:34:10 +02:00
let(:monavis_embed) {
<<-MSG
<a href="https://monavis.numerique.gouv.fr/Demarches/123?&view-mode=formulaire-avis&nd_mode=en-ligne-enti%C3%A8rement&nd_source=button&key=cd4a872d475e4045666057f">
<img src="https://monavis.numerique.gouv.fr/monavis-static/bouton-blanc.png" alt="Je donne mon avis" title="Je donne mon avis sur cette démarche" />
</a>
MSG
}
context 'when all attributes are present' do
render_views
before { update_monavis }
2019-07-17 15:34:10 +02:00
context 'when the embed code is valid' do
describe 'the monavis field is updated' do
subject { procedure }
it { expect(subject.monavis_embed).to eq(monavis_embed) }
end
2019-07-17 15:34:10 +02:00
it { expect(flash[:notice]).to be_present }
it { expect(response.body).to include "MonAvis" }
end
2019-07-17 15:34:10 +02:00
context 'when the embed code is not valid' do
let(:monavis_embed) { 'invalid embed code' }
describe 'the monavis field is not updated' do
subject { procedure }
it { expect(subject.monavis_embed).to eq(nil) }
end
it { expect(flash[:alert]).to be_present }
it { expect(response.body).to include "MonAvis" }
end
end
context 'when procedure is published' do
let(:procedure) { create(:procedure, :published, administrateur: admin) }
subject { update_monavis }
2019-07-17 15:34:10 +02:00
describe 'the monavis field is not updated' do
it { expect(subject.monavis_embed).to eq monavis_embed }
end
end
end
end
2015-10-26 15:54:20 +01:00
end