From d55c789c737866b75335e5071095348c6742b128 Mon Sep 17 00:00:00 2001 From: Jack S Date: Mon, 21 Nov 2022 11:24:17 +0000 Subject: [PATCH] Managing Organisation page should behave the same if user is support --- .../lettings/pages/managing_organisation.rb | 1 - .../pages/managing_organisation_spec.rb | 67 +++++++++++++++++-- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/app/models/form/lettings/pages/managing_organisation.rb b/app/models/form/lettings/pages/managing_organisation.rb index 67e8ae586..f1d1197f7 100644 --- a/app/models/form/lettings/pages/managing_organisation.rb +++ b/app/models/form/lettings/pages/managing_organisation.rb @@ -15,7 +15,6 @@ class Form::Lettings::Pages::ManagingOrganisation < ::Form::Page def routed_to?(log, current_user) return false unless current_user - return true if current_user.support? return true unless current_user.organisation.holds_own_stock? managing_agents = current_user.organisation.managing_agents diff --git a/spec/models/form/lettings/pages/managing_organisation_spec.rb b/spec/models/form/lettings/pages/managing_organisation_spec.rb index fcb93d565..e4f27516f 100644 --- a/spec/models/form/lettings/pages/managing_organisation_spec.rb +++ b/spec/models/form/lettings/pages/managing_organisation_spec.rb @@ -47,15 +47,70 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do end context "when support" do - let(:organisation) { create(:organisation) } - let(:user) { create(:user, :support, organisation:) } + context "when does not hold own stock" do + let(:user) do + create(:user, :support, organisation: create(:organisation, holds_own_stock: false)) + end + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) + end - it "is shown" do - expect(page.routed_to?(log, user)).to eq(true) + it "does not update managing_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :managing_organisation) + end end - it "does not update managing_organisation_id" do - expect { page.routed_to?(log, user) }.not_to change(log.reload, :managing_organisation) + context "when holds own stock" do + let(:user) do + create(:user, :support, organisation: create(:organisation, holds_own_stock: true)) + end + + context "with 0 managing_agents" do + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + + it "does not update managing_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :managing_organisation) + end + end + + context "with >1 managing_agents" do + before do + create(:organisation_relationship, :managing, parent_organisation: user.organisation) + create(:organisation_relationship, :managing, parent_organisation: user.organisation) + end + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) + end + + it "does not update managing_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :managing_organisation) + end + end + + context "with 1 managing_agents" do + let(:managing_agent) { create(:organisation) } + + before do + create( + :organisation_relationship, + :managing, + child_organisation: managing_agent, + parent_organisation: user.organisation, + ) + end + + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + + it "updates managing_organisation_id" do + expect { page.routed_to?(log, user) }.to change(log.reload, :managing_organisation).to(managing_agent) + end + end end end