From fd9061abdfc486619fec031782180b956aaac05b Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 10 Nov 2015 11:58:56 +0100 Subject: [PATCH] - Administrateur can't show only his procedures - Gestionnaire can't show only files of his Administrateur's procedures --- .../admin/procedures_controller.rb | 2 +- app/controllers/backoffice_controller.rb | 11 +++--- app/models/dossier.rb | 12 +++---- spec/features/backoffice/connection_spec.rb | 4 ++- .../backoffice/navigate_to_dossier_spec.rb | 10 +++--- spec/models/dossier_spec.rb | 35 ++++++++++++------- spec/views/backoffice/index.html.haml_spec.rb | 13 ++++--- 7 files changed, 54 insertions(+), 33 deletions(-) diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 655c2ddc6..e41c20a4b 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -2,7 +2,7 @@ class Admin::ProceduresController < ApplicationController before_action :authenticate_administrateur! def index - @procedures = Procedure.all + @procedures = current_administrateur.procedures end def show diff --git a/app/controllers/backoffice_controller.rb b/app/controllers/backoffice_controller.rb index 0b2b305c1..5096033d4 100644 --- a/app/controllers/backoffice_controller.rb +++ b/app/controllers/backoffice_controller.rb @@ -1,9 +1,12 @@ class BackofficeController < ApplicationController def index - redirect_to(controller: '/gestionnaires/sessions', action: :new) unless gestionnaire_signed_in? - @dossiers_a_traiter = Dossier.a_traiter.decorate - @dossiers_en_attente = Dossier.en_attente.decorate - @dossiers_termine = Dossier.termine.decorate + if !gestionnaire_signed_in? + redirect_to(controller: '/gestionnaires/sessions', action: :new) + else + @dossiers_a_traiter = Dossier.a_traiter(current_gestionnaire).decorate + @dossiers_en_attente = Dossier.en_attente(current_gestionnaire).decorate + @dossiers_termine = Dossier.termine(current_gestionnaire).decorate + end end end \ No newline at end of file diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 586ea71c3..fa68aa36a 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -116,16 +116,16 @@ class Dossier < ActiveRecord::Base state end - def self.a_traiter - Dossier.where("state='initiated' OR state='updated' OR state='submitted'").order('updated_at ASC') + def self.a_traiter current_gestionnaire + Dossier.joins(:procedure).where("(state='initiated' OR state='updated' OR state='submitted') AND dossiers.procedure_id = procedures.id AND procedures.administrateur_id = #{current_gestionnaire.administrateur_id}").order('updated_at ASC') end - def self.en_attente - Dossier.where("state='replied' OR state='validated'").order('updated_at ASC') + def self.en_attente current_gestionnaire + Dossier.joins(:procedure).where("(state='replied' OR state='validated') AND dossiers.procedure_id = procedures.id AND procedures.administrateur_id = #{current_gestionnaire.administrateur_id}").order('updated_at ASC') end - def self.termine - Dossier.where("state='closed'").order('updated_at ASC') + def self.termine current_gestionnaire + Dossier.joins(:procedure).where("state='closed' AND dossiers.procedure_id = procedures.id AND procedures.administrateur_id = #{current_gestionnaire.administrateur_id}").order('updated_at ASC') end private diff --git a/spec/features/backoffice/connection_spec.rb b/spec/features/backoffice/connection_spec.rb index 50430db45..03fd060e9 100644 --- a/spec/features/backoffice/connection_spec.rb +++ b/spec/features/backoffice/connection_spec.rb @@ -20,7 +20,9 @@ feature 'when gestionnaire come to /backoffice and is not authenticated' do end end context 'when user enter good credentials' do - let(:gestionnaire) { create(:gestionnaire) } + let(:administrateur) { create(:administrateur) } + let(:gestionnaire) { create(:gestionnaire, administrateur: administrateur) } + before do page.find_by_id(:gestionnaire_email).set gestionnaire.email page.find_by_id(:gestionnaire_password).set gestionnaire.password diff --git a/spec/features/backoffice/navigate_to_dossier_spec.rb b/spec/features/backoffice/navigate_to_dossier_spec.rb index 0b76c7740..542eb5637 100644 --- a/spec/features/backoffice/navigate_to_dossier_spec.rb +++ b/spec/features/backoffice/navigate_to_dossier_spec.rb @@ -1,16 +1,18 @@ require 'spec_helper' feature 'on backoffice page' do - let(:procedure) { create(:procedure) } + let(:administrateur) { create(:administrateur) } + let(:gestionnaire) { create(:gestionnaire, administrateur: administrateur) } + let(:procedure) { create(:procedure, administrateur: administrateur) } + let!(:dossier) { create(:dossier, :with_user, :with_entreprise, procedure: procedure, state: 'replied') } before do visit backoffice_path end context 'when gestionnaire is logged in' do - let(:gestionnaire) { create(:gestionnaire) } before do - page.find_by_id(:gestionnaire_email).set gestionnaire.email - page.find_by_id(:gestionnaire_password).set gestionnaire.password + page.find_by_id(:gestionnaire_email).set gestionnaire.email + page.find_by_id(:gestionnaire_password).set gestionnaire.password page.click_on 'Se connecter' end context 'when he click on first dossier' do diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 7c98f6f3f..cf5fd0d19 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -381,29 +381,38 @@ describe Dossier do end context 'gestionnaire backoffice methods' do - let!(:dossier1) { create(:dossier, :with_user, :with_procedure, state: 'draft')} - let!(:dossier2) { create(:dossier, :with_user, :with_procedure, state: 'initiated')} - let!(:dossier3) { create(:dossier, :with_user, :with_procedure, state: 'initiated')} - let!(:dossier4) { create(:dossier, :with_user, :with_procedure, state: 'replied')} - let!(:dossier5) { create(:dossier, :with_user, :with_procedure, state: 'updated')} - let!(:dossier6) { create(:dossier, :with_user, :with_procedure, state: 'validated')} - let!(:dossier7) { create(:dossier, :with_user, :with_procedure, state: 'submitted')} - let!(:dossier8) { create(:dossier, :with_user, :with_procedure, state: 'closed')} + let(:admin) { create(:administrateur) } + let(:admin_2) { create(:administrateur) } + + let(:gestionnaire) { create(:gestionnaire, administrateur: admin) } + let(:procedure_admin) { create(:procedure, administrateur: admin) } + let(:procedure_admin_2) { create(:procedure, administrateur: admin_2) } + + let!(:dossier1) { create(:dossier, :with_user, procedure: procedure_admin, state: 'draft')} + let!(:dossier2) { create(:dossier, :with_user, procedure: procedure_admin, state: 'initiated')} #a_traiter + let!(:dossier3) { create(:dossier, :with_user, procedure: procedure_admin, state: 'initiated')} #a_traiter + let!(:dossier4) { create(:dossier, :with_user, procedure: procedure_admin, state: 'replied')} #en_attente + let!(:dossier5) { create(:dossier, :with_user, procedure: procedure_admin, state: 'updated')} #a_traiter + let!(:dossier6) { create(:dossier, :with_user, procedure: procedure_admin_2, state: 'validated')} #en_attente + let!(:dossier7) { create(:dossier, :with_user, procedure: procedure_admin_2, state: 'submitted')} #a_traiter + let!(:dossier8) { create(:dossier, :with_user, procedure: procedure_admin_2, state: 'closed')} #termine + let!(:dossier9) { create(:dossier, :with_user, procedure: procedure_admin, state: 'closed')} #termine + describe '#a_traiter' do - subject { described_class.a_traiter } + subject { described_class.a_traiter gestionnaire } - it { expect(subject.size).to eq(4) } + it { expect(subject.size).to eq(3) } end describe '#en_attente' do - subject { described_class.en_attente } + subject { described_class.en_attente gestionnaire } - it { expect(subject.size).to eq(2) } + it { expect(subject.size).to eq(1) } end describe '#termine' do - subject { described_class.termine } + subject { described_class.termine gestionnaire } it { expect(subject.size).to eq(1) } end diff --git a/spec/views/backoffice/index.html.haml_spec.rb b/spec/views/backoffice/index.html.haml_spec.rb index 784a42038..01512ee4a 100644 --- a/spec/views/backoffice/index.html.haml_spec.rb +++ b/spec/views/backoffice/index.html.haml_spec.rb @@ -1,16 +1,21 @@ require 'spec_helper' describe 'backoffice/index.html.haml', type: :view do - let!(:procedure) { create(:procedure) } + let(:administrateur) { create(:administrateur) } + let(:gestionnaire) { create(:gestionnaire, administrateur: administrateur) } + + let!(:procedure) { create(:procedure, administrateur: administrateur) } let!(:decorate_dossier) { create(:dossier, :with_user, procedure: procedure).decorate } + before do - assign(:dossiers_a_traiter, Dossier.a_traiter.decorate) - assign(:dossiers_en_attente, Dossier.en_attente.decorate) - assign(:dossiers_termine, Dossier.termine.decorate) + assign(:dossiers_a_traiter, Dossier.a_traiter(gestionnaire).decorate) + assign(:dossiers_en_attente, Dossier.en_attente(gestionnaire).decorate) + assign(:dossiers_termine, Dossier.termine(gestionnaire).decorate) decorate_dossier.initiated! render end + subject { rendered } it { is_expected.to have_css('#backoffice') } it { is_expected.to have_content(procedure.libelle) }