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

262 lines
7.7 KiB
Ruby
Raw Normal View History

2015-10-26 15:54:20 +01:00
require 'spec_helper'
describe Admin::ProceduresController, type: :controller do
let(:admin) { create(:administrateur) }
let(:bad_procedure_id) { 100000 }
let(:libelle) { 'Procédure de test' }
let(:description) { 'Description de test' }
let(:organisation) { 'Organisation de test' }
let(:direction) { 'Direction de test' }
let(:lien_demarche) { 'http://localhost.com' }
let(:use_api_carto) { '0' }
let(:quartiers_prioritaires) { '0' }
let(:cadastre) { '0' }
2015-10-26 15:54:20 +01:00
let(:procedure_params) {
{
libelle: libelle,
description: description,
organisation: organisation,
direction: direction,
lien_demarche: lien_demarche,
module_api_carto_attributes: {
use_api_carto: use_api_carto,
quartiers_prioritaires: quartiers_prioritaires,
cadastre: cadastre
}
2015-10-26 15:54:20 +01:00
}
}
2015-10-28 18:09:47 +01:00
2015-10-26 15:54:20 +01:00
before do
sign_in admin
end
describe 'GET #index' do
subject { get :index }
it { expect(response.status).to eq(200) }
end
describe 'GET #archived' do
subject { get :archived }
it { expect(response.status).to eq(200) }
end
describe 'GET #edit' do
let(:procedure) { create(:procedure, administrateur: admin) }
2015-11-04 17:27:01 +01:00
let(:procedure_id) { procedure.id }
2015-10-26 15:54:20 +01:00
subject { get :edit, id: procedure_id }
2015-10-26 15:54:20 +01:00
context 'when user is not connected' do
before do
sign_out admin
end
it { expect(subject).to redirect_to new_administrateur_session_path }
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 }
2015-10-26 15:54:20 +01:00
it { expect(subject).to have_http_status(:success) }
end
context 'when procedure have at least a file' do
2016-01-18 17:18:04 +01:00
let!(:dossier) { create(:dossier, procedure: procedure, state: :initiated) }
it { is_expected.to redirect_to admin_procedure_path id: procedure_id }
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
it { expect(subject).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, procedure: procedure_params }
it { expect { subject }.to change { Procedure.count }.by(1) }
end
context 'when procedure is correctly save' do
before do
post :create, procedure: procedure_params
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.lien_demarche).to eq(lien_demarche) }
2015-11-16 18:25:31 +01:00
it { expect(subject.administrateur_id).to eq(admin.id) }
2015-10-26 15:54:20 +01:00
end
describe 'procedure module api carto attributs in database' do
let(:procedure) { Procedure.last }
let(:use_api_carto) { '1' }
let(:quartiers_prioritaires) { '1' }
subject { ModuleAPICarto.last }
it { expect(subject.procedure).to eq(procedure) }
it { expect(subject.use_api_carto).to be_truthy }
it { expect(subject.quartiers_prioritaires).to be_truthy }
end
it { expect(subject).to redirect_to(admin_procedure_types_de_champ_path(procedure_id: Procedure.last.id)) }
2015-10-26 15:54:20 +01:00
it { expect(flash[:notice]).to be_present }
end
end
context 'when many attributs are not valid' do
let(:libelle) { '' }
let(:description) { '' }
describe 'no new procedure in database' do
subject { post :create, procedure: procedure_params }
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, procedure: procedure_params
end
it { expect(flash[:alert]).to be_present }
end
end
end
describe 'PUT #update' do
let!(:procedure) { create(:procedure, :with_type_de_champ, :with_two_type_de_piece_justificative, administrateur: admin) }
2015-10-26 15:54:20 +01:00
context 'when administrateur is not connected' do
before do
sign_out admin
end
subject { put :update, id: procedure.id }
it { expect(subject).to redirect_to new_administrateur_session_path }
end
context 'when administrateur is connected' do
before do
2015-11-09 17:15:51 +01:00
put :update, id: procedure.id, procedure: procedure_params
2015-10-26 15:54:20 +01:00
procedure.reload
end
context 'when all attributs are informated' do
let(:libelle) { 'Blable' }
let(:description) { 'blabla' }
let(:organisation) { 'plop' }
let(:direction) { 'plap' }
let(:lien_demarche) { 'http://plip.com' }
let(:use_api_carto) { '1' }
let(:cadastre) { '1' }
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.lien_demarche).to eq(lien_demarche) }
end
describe 'procedure module api carto attributs in database' do
subject { procedure.module_api_carto }
it { expect(subject.use_api_carto).to be_truthy }
it { expect(subject.quartiers_prioritaires).to be_falsey }
it { expect(subject.cadastre).to be_truthy }
2015-10-26 15:54:20 +01:00
end
it { expect(subject).to redirect_to(admin_procedures_path) }
it { expect(flash[:notice]).to be_present }
end
context 'when many attributs are not valid' do
let(:libelle) { '' }
let(:description) { '' }
describe 'flash message is present' do
it { expect(flash[:alert]).to be_present }
end
describe 'procedure module api carto attributs in database' do
subject { procedure.module_api_carto }
it { expect(subject.use_api_carto).to be_falsey }
it { expect(subject.quartiers_prioritaires).to be_falsey }
it { expect(subject.cadastre).to be_falsey }
end
2015-10-26 15:54:20 +01:00
end
end
end
2015-11-26 18:41:41 +01:00
describe 'PUT #archive' do
let(:procedure) { create(:procedure, administrateur: admin) }
context 'when admin is the owner of the procedure' do
before do
put :archive, procedure_id: procedure.id, archive: archive
2015-11-26 18:41:41 +01:00
procedure.reload
end
context 'when owner want archive procedure' do
let(:archive) { true }
it { expect(procedure.archived).to be_truthy }
it { expect(response).to redirect_to :admin_procedures }
it { expect(flash[:notice]).to have_content 'Procédure éditée' }
end
context 'when owner want reactive procedure' do
let(:archive) { false }
it { expect(procedure.archived).to be_falsey }
it { expect(response).to redirect_to :admin_procedures }
it { expect(flash[:notice]).to have_content 'Procédure édité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
sign_out admin
sign_in admin_2
put :archive, procedure_id: procedure.id
procedure.reload
end
it { expect(response).to redirect_to :admin_procedures }
it { expect(flash[:alert]).to have_content 'Procédure inéxistante' }
end
end
2015-10-26 15:54:20 +01:00
end