2015-11-20 13:54:08 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-11-15 05:19:30 +01:00
|
|
|
describe Admin::PiecesJustificativesController, type: :controller do
|
2015-11-20 13:54:08 +01:00
|
|
|
let(:admin) { create(:administrateur) }
|
2017-07-11 14:21:10 +02:00
|
|
|
let(:published_at) { nil }
|
|
|
|
let(:procedure) { create(:procedure, administrateur: admin, published_at: published_at) }
|
2015-11-20 13:54:08 +01:00
|
|
|
before do
|
|
|
|
sign_in admin
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #show' do
|
|
|
|
let(:procedure_id) { procedure.id }
|
2015-12-21 12:02:53 +01:00
|
|
|
|
2016-11-15 05:19:30 +01:00
|
|
|
subject { get :show, params: {procedure_id: procedure_id} }
|
2015-12-21 12:02:53 +01:00
|
|
|
|
2015-11-20 13:54:08 +01:00
|
|
|
context 'when procedure is not found' do
|
|
|
|
let(:procedure_id) { 9_999_999 }
|
|
|
|
it { expect(subject.status).to eq(404) }
|
|
|
|
end
|
2015-12-21 12:02:53 +01:00
|
|
|
|
2016-06-09 17:49:38 +02:00
|
|
|
context 'when procedure is published' do
|
2017-07-11 14:21:10 +02:00
|
|
|
let(:published_at) { Time.now }
|
2015-12-24 15:57:03 +01:00
|
|
|
it { is_expected.to redirect_to admin_procedure_path id: procedure_id }
|
2015-12-21 12:02:53 +01:00
|
|
|
end
|
|
|
|
|
2015-11-20 13:54:08 +01:00
|
|
|
context 'when procedure does not belong to admin' do
|
|
|
|
let(:admin_2) { create(:administrateur) }
|
|
|
|
let(:procedure) { create(:procedure, administrateur: admin_2) }
|
|
|
|
it { expect(subject.status).to eq(404) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-20 15:12:47 +01:00
|
|
|
describe 'PUT #update' do
|
2015-11-20 13:54:08 +01:00
|
|
|
let(:procedure_id) { procedure.id }
|
|
|
|
let(:libelle) { 'RIB' }
|
|
|
|
let(:description) { "relevé d'identité bancaire" }
|
|
|
|
let(:update_params) do
|
|
|
|
{
|
2018-01-15 19:02:12 +01:00
|
|
|
types_de_piece_justificative_attributes:
|
|
|
|
{
|
|
|
|
'0' =>
|
2016-11-15 05:19:30 +01:00
|
|
|
{
|
2018-01-15 19:02:12 +01:00
|
|
|
libelle: libelle,
|
|
|
|
description: description
|
2016-11-15 05:19:30 +01:00
|
|
|
}
|
2018-01-15 19:02:12 +01:00
|
|
|
}
|
2015-11-20 13:54:08 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-11-15 05:19:30 +01:00
|
|
|
let(:request) { put :update, params: {procedure_id: procedure_id, format: :js, procedure: update_params} }
|
2015-11-20 13:54:08 +01:00
|
|
|
subject { request }
|
|
|
|
|
|
|
|
it { is_expected.to render_template('show') }
|
2016-11-15 05:19:30 +01:00
|
|
|
it { expect { subject }.to change(TypeDePieceJustificative, :count).by(1) }
|
2015-11-20 13:54:08 +01:00
|
|
|
it 'adds type de pj to procedure' do
|
|
|
|
request
|
|
|
|
procedure.reload
|
|
|
|
pj = procedure.types_de_piece_justificative.first
|
|
|
|
expect(pj.libelle).to eq(libelle)
|
|
|
|
expect(pj.description).to eq(description)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when procedure is not found' do
|
|
|
|
let(:procedure_id) { 9_999_999 }
|
|
|
|
it { expect(subject.status).to eq(404) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when libelle is blank' do
|
|
|
|
let(:libelle) { '' }
|
2016-11-15 05:19:30 +01:00
|
|
|
it { expect { subject }.not_to change(TypeDePieceJustificative, :count) }
|
2015-11-20 13:54:08 +01:00
|
|
|
end
|
|
|
|
end
|
2015-11-20 15:12:47 +01:00
|
|
|
|
|
|
|
describe 'DELETE #destroy' do
|
|
|
|
let!(:pj) { create(:type_de_piece_justificative, procedure: procedure) }
|
|
|
|
let(:procedure_id) { procedure.id }
|
|
|
|
let(:pj_id) { pj.id }
|
2016-11-15 05:19:30 +01:00
|
|
|
let(:request) { delete :destroy, params: {procedure_id: procedure_id, id: pj_id} }
|
2015-11-20 15:12:47 +01:00
|
|
|
subject { request }
|
|
|
|
context 'when procedure is not found' do
|
|
|
|
let(:procedure_id) { 9_999_999 }
|
|
|
|
it { expect(subject.status).to eq(404) }
|
|
|
|
end
|
|
|
|
context 'when pj id does not exist' do
|
|
|
|
let(:pj_id) { 9_999_999 }
|
|
|
|
it { expect(subject.status).to eq(404) }
|
|
|
|
end
|
|
|
|
context 'when pj id exist but is not linked to procedure' do
|
|
|
|
let(:procedure_1) { create(:procedure, administrateur: admin) }
|
|
|
|
let!(:pj_1) { create(:type_de_piece_justificative, procedure: procedure_1) }
|
|
|
|
let(:pj_id) { pj_1 }
|
|
|
|
it { expect(subject.status).to eq(404) }
|
|
|
|
end
|
|
|
|
context 'when pj is found' do
|
|
|
|
it { expect(subject.status).to eq(200) }
|
2016-11-15 05:19:30 +01:00
|
|
|
it { expect { subject }.to change(TypeDePieceJustificative, :count).by(-1) }
|
2015-11-20 15:12:47 +01:00
|
|
|
end
|
|
|
|
end
|
2016-06-08 16:45:18 +02:00
|
|
|
|
|
|
|
describe 'POST #move_up' do
|
2016-11-15 05:19:30 +01:00
|
|
|
subject { post :move_up, params: {procedure_id: procedure.id, index: index, format: :js} }
|
2016-06-08 16:45:18 +02:00
|
|
|
|
|
|
|
context 'when procedure have no type de champ' do
|
|
|
|
let(:index) { 0 }
|
|
|
|
it { expect(subject.status).to eq(400) }
|
|
|
|
end
|
|
|
|
context 'when procedure have only one type de champ' do
|
|
|
|
let(:index) { 1 }
|
|
|
|
let!(:type_de_piece_justificative) { create(:type_de_piece_justificative, procedure: procedure) }
|
|
|
|
it { expect(subject.status).to eq(400) }
|
|
|
|
end
|
|
|
|
context 'when procedure have tow type de champs' do
|
|
|
|
context 'when index == 0' do
|
|
|
|
let(:index) { 0 }
|
|
|
|
let!(:type_de_piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure) }
|
|
|
|
let!(:type_de_piece_justificative_2) { create(:type_de_piece_justificative, procedure: procedure) }
|
|
|
|
it { expect(subject.status).to eq(400) }
|
|
|
|
end
|
|
|
|
context 'when index > 0' do
|
|
|
|
let(:index) { 1 }
|
|
|
|
let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) }
|
|
|
|
let!(:type_de_piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) }
|
|
|
|
|
|
|
|
it { expect(subject.status).to eq(200) }
|
|
|
|
it { expect(subject).to render_template('show') }
|
|
|
|
it 'changes order places' do
|
2016-11-15 05:19:30 +01:00
|
|
|
post :move_up, params: {procedure_id: procedure.id, index: index, format: :js}
|
2016-06-08 16:45:18 +02:00
|
|
|
type_de_piece_justificative_0.reload
|
|
|
|
type_de_piece_justificative_1.reload
|
|
|
|
expect(type_de_piece_justificative_0.order_place).to eq(1)
|
|
|
|
expect(type_de_piece_justificative_1.order_place).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST #move_down' do
|
2016-11-15 05:19:30 +01:00
|
|
|
let(:request) { post :move_down, params: {procedure_id: procedure.id, index: index, format: :js} }
|
2016-06-08 16:45:18 +02:00
|
|
|
let(:index) { 0 }
|
|
|
|
|
|
|
|
subject { request }
|
|
|
|
|
|
|
|
context 'when procedure have no type de champ' do
|
|
|
|
it { expect(subject.status).to eq(400) }
|
|
|
|
end
|
|
|
|
context 'when procedure have only one type de champ' do
|
|
|
|
let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure) }
|
|
|
|
it { expect(subject.status).to eq(400) }
|
|
|
|
end
|
|
|
|
context 'when procedure have 2 type de champ' do
|
|
|
|
let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) }
|
|
|
|
let!(:type_de_piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) }
|
|
|
|
context 'when index represent last type_de_piece_justificative' do
|
|
|
|
let(:index) { 1 }
|
|
|
|
it { expect(subject.status).to eq(400) }
|
|
|
|
end
|
|
|
|
context 'when index does not represent last type_de_piece_justificative' do
|
|
|
|
let(:index) { 0 }
|
|
|
|
it { expect(subject.status).to eq(200) }
|
|
|
|
it { expect(subject).to render_template('show') }
|
|
|
|
it 'changes order place' do
|
|
|
|
request
|
|
|
|
type_de_piece_justificative_0.reload
|
|
|
|
type_de_piece_justificative_1.reload
|
|
|
|
expect(type_de_piece_justificative_0.order_place).to eq(1)
|
|
|
|
expect(type_de_piece_justificative_1.order_place).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|