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

169 lines
4.9 KiB
Ruby
Raw Normal View History

2024-10-16 14:31:08 +02:00
# frozen_string_literal: true
2024-10-29 15:39:58 +01:00
describe Administrateurs::LabelsController, type: :controller do
2024-10-16 14:31:08 +02:00
let(:admin) { administrateurs(:default_admin) }
let(:procedure) { create(:procedure, administrateur: admin) }
let(:admin_2) { create(:administrateur) }
let(:procedure_2) { create(:procedure, administrateur: admin_2) }
describe '#index' do
render_views
2024-10-29 15:39:58 +01:00
let!(:label_1) { create(:label, procedure:) }
let!(:label_2) { create(:label, procedure:) }
let!(:label_3) { create(:label, procedure:) }
2024-10-16 14:31:08 +02:00
before do
sign_in(admin.user)
end
subject { get :index, params: { procedure_id: procedure.id } }
it 'displays all procedure labels' do
subject
expect(response.body).to have_link("Nouveau label")
expect(response.body).to have_link("Modifier", count: 3)
expect(response.body).to have_link("Supprimer", count: 3)
end
end
describe '#create' do
before do
sign_in(admin.user)
end
subject { post :create, params: params }
context 'when submitting a new label' do
let(:params) do
{
2024-10-29 15:39:58 +01:00
label: {
2024-10-16 14:31:08 +02:00
name: 'Nouveau label',
color: 'green-bourgeon'
},
procedure_id: procedure.id
}
end
2024-10-29 15:39:58 +01:00
it { expect { subject }.to change { Label.count } .by(1) }
2024-10-16 14:31:08 +02:00
it 'creates a new label' do
subject
expect(flash.alert).to be_nil
expect(flash.notice).to eq('Le label a bien été créé')
2024-10-29 15:39:58 +01:00
expect(Label.last.name).to eq('Nouveau label')
expect(Label.last.color).to eq('green_bourgeon')
expect(procedure.labels.last).to eq(Label.last)
2024-10-16 14:31:08 +02:00
end
end
context 'when submitting an invalid label' do
2024-10-29 15:39:58 +01:00
let(:params) { { label: { name: 'Nouveau label' }, procedure_id: procedure.id } }
2024-10-16 14:31:08 +02:00
2024-10-29 15:39:58 +01:00
it { expect { subject }.not_to change { Label.count } }
2024-10-16 14:31:08 +02:00
it 'does not create a new label' do
subject
expect(flash.alert).to eq(["Le champ « Couleur » doit être rempli"])
expect(response).to render_template(:new)
expect(assigns(:label).name).to eq('Nouveau label')
end
end
context 'when submitting a label for a not own procedure' do
let(:params) do
{
2024-10-29 15:39:58 +01:00
label: {
2024-10-16 14:31:08 +02:00
name: 'Nouveau label',
color: 'green-bourgeon'
},
procedure_id: procedure_2.id
}
end
2024-10-29 15:39:58 +01:00
it { expect { subject }.not_to change { Label.count } }
2024-10-16 14:31:08 +02:00
it 'does not create a new label' do
subject
expect(flash.alert).to eq("Démarche inexistante")
expect(response.status).to eq(404)
end
end
end
describe '#update' do
2024-10-29 15:39:58 +01:00
let!(:label) { create(:label, procedure:) }
2024-10-16 14:31:08 +02:00
let(:label_params) { { name: 'Nouveau nom' } }
2024-10-29 15:39:58 +01:00
let(:params) { { id: label.id, label: label_params, procedure_id: procedure.id } }
2024-10-16 14:31:08 +02:00
before do
sign_in(admin.user)
end
subject { patch :update, params: }
context 'when updating a label' do
it 'updates correctly' do
subject
expect(flash.alert).to be_nil
expect(flash.notice).to eq('Le label a bien été modifié')
expect(label.reload.name).to eq('Nouveau nom')
expect(label.reload.color).to eq('green_bourgeon')
expect(label.reload.updated_at).not_to eq(label.reload.created_at)
2024-10-29 15:39:58 +01:00
expect(response).to redirect_to(admin_procedure_labels_path(procedure_id: procedure.id))
2024-10-16 14:31:08 +02:00
end
end
context 'when updating a service with invalid data' do
let(:label_params) { { name: '' } }
it 'does not update' do
subject
expect(flash.alert).not_to be_nil
expect(response).to render_template(:edit)
expect(label.reload.updated_at).to eq(label.reload.created_at)
end
end
context 'when updating a label for a not own procedure' do
2024-10-29 15:39:58 +01:00
let(:params) { { id: label.id, label: label_params, procedure_id: procedure_2.id } }
2024-10-16 14:31:08 +02:00
it 'does not update' do
subject
expect(label.reload.updated_at).to eq(label.reload.created_at)
end
end
end
describe '#destroy' do
2024-10-29 15:39:58 +01:00
let(:label) { create(:label, procedure:) }
2024-10-16 14:31:08 +02:00
before do
sign_in(admin.user)
end
subject { delete :destroy, params: }
context "when deleting a label" do
let(:params) { { id: label.id, procedure_id: procedure.id } }
it "delete the label" do
subject
expect { label.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(flash.notice).to eq('Le label a bien été supprimé')
2024-10-29 15:39:58 +01:00
expect(response).to redirect_to(admin_procedure_labels_path(procedure_id: procedure.id))
2024-10-16 14:31:08 +02:00
end
end
context 'when deleting a label for a not own procedure' do
let(:params) { { id: label.id, procedure_id: procedure_2.id } }
it 'does not delete' do
subject
expect(flash.alert).to eq("Démarche inexistante")
expect(response.status).to eq(404)
expect { label.reload }.not_to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end