From 9cddf371c961042b1a6c02877a188b22d63efe0f Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Fri, 28 Jul 2023 17:43:16 +0100 Subject: [PATCH] feat: update tests --- .../form/sales/pages/organisation_spec.rb | 126 ++++++++++++++++-- .../questions/owning_organisation_id_spec.rb | 89 ++++++++++++- 2 files changed, 203 insertions(+), 12 deletions(-) diff --git a/spec/models/form/sales/pages/organisation_spec.rb b/spec/models/form/sales/pages/organisation_spec.rb index 62156031f..73f5ab2c7 100644 --- a/spec/models/form/sales/pages/organisation_spec.rb +++ b/spec/models/form/sales/pages/organisation_spec.rb @@ -7,7 +7,6 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do let(:page_definition) { nil } let(:subsection) { instance_double(Form::Subsection) } let(:form) { instance_double(Form) } - let(:lettings_log) { instance_double(LettingsLog) } it "has correct subsection" do expect(page.subsection).to eq(subsection) @@ -33,19 +32,126 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do expect(page.depends_on).to be nil end - context "when the current user is a support user" do - let(:support_user) { FactoryBot.build(:user, :support) } + describe "#routed_to?" do + let(:log) { create(:lettings_log, owning_organisation_id: nil) } - it "is shown" do - expect(page.routed_to?(lettings_log, support_user)).to be true + context "when user nil" do + it "is not shown" do + expect(page.routed_to?(log, nil)).to eq(false) + end + + it "does not update owning_organisation_id" do + expect { page.routed_to?(log, nil) }.not_to change(log.reload, :owning_organisation).from(nil) + end end - end - context "when the current user is not a support user" do - let(:user) { FactoryBot.build(:user) } + context "when support" do + let(:user) { create(:user, :support) } + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) + end + + it "does not update owning_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil) + end + end + + context "when not support" do + context "when does not hold own stock" do + let(:user) do + create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: false)) + end + + context "with 0 stock_owners" do + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + + it "does not update owning_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation) + end + end + + context "with 1 stock_owners" do + let(:stock_owner) { create(:organisation) } + + before do + create( + :organisation_relationship, + child_organisation: user.organisation, + parent_organisation: stock_owner, + ) + end + + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + + it "updates owning_organisation_id" do + expect { page.routed_to?(log, user) }.to change(log.reload, :owning_organisation).from(nil).to(stock_owner) + end + end + + context "with >1 stock_owners" do + let(:stock_owner1) { create(:organisation) } + let(:stock_owner2) { create(:organisation) } + + before do + create( + :organisation_relationship, + child_organisation: user.organisation, + parent_organisation: stock_owner1, + ) + create( + :organisation_relationship, + child_organisation: user.organisation, + parent_organisation: stock_owner2, + ) + end + + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(true) + end + + it "updates owning_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation) + end + end + end + + context "when holds own stock" do + let(:user) do + create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true)) + end + + context "with 0 stock_owners" do + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + + it "updates owning_organisation_id to user organisation" do + expect { + page.routed_to?(log, user) + }.to change(log.reload, :owning_organisation).from(nil).to(user.organisation) + end + end + + context "with >0 stock_owners" do + before do + create(:organisation_relationship, child_organisation: user.organisation) + create(:organisation_relationship, child_organisation: user.organisation) + end + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) + end - it "is not shown" do - expect(page.routed_to?(lettings_log, user)).to be false + it "does not update owning_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil) + end + end + end end end end diff --git a/spec/models/form/sales/questions/owning_organisation_id_spec.rb b/spec/models/form/sales/questions/owning_organisation_id_spec.rb index f6dc02c27..b005838ba 100644 --- a/spec/models/form/sales/questions/owning_organisation_id_spec.rb +++ b/spec/models/form/sales/questions/owning_organisation_id_spec.rb @@ -3,6 +3,7 @@ require "rails_helper" RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do subject(:question) { described_class.new(question_id, question_definition, page) } + let(:user) { FactoryBot.create(:user, :data_coordinator)} let(:question_id) { nil } let(:question_definition) { nil } let(:page) { instance_double(Form::Page) } @@ -43,8 +44,92 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do expect(question.hint_text).to be_nil end - it "has the correct answer options" do - expect(question.answer_options).to eq(expected_answer_options) + describe "answer options" do + let(:options) { { "" => "Select an option" } } + + context "when current_user nil" do + it "shows default options" do + expect(question.answer_options).to eq(options) + end + end + + context "when user is not support" do + let(:user_org) { create(:organisation, name: "User org") } + let(:user) { create(:user, :data_coordinator, organisation: user_org) } + + let(:owning_org_1) { create(:organisation, name: "Owning org 1") } + let(:owning_org_2) { create(:organisation, name: "Owning org 2") } + let!(:org_rel) do + create(:organisation_relationship, child_organisation: user.organisation, parent_organisation: owning_org_2) + end + let(:log) { create(:lettings_log, owning_organisation: owning_org_1) } + + context "when user's org owns stock" do + let(:options) do + { + "" => "Select an option", + owning_org_1.id => "Owning org 1", + user.organisation.id => "User org (Your organisation)", + owning_org_2.id => "Owning org 2", + } + end + + it "shows current stock owner at top, followed by user's org (with hint), followed by the stock owners of the user's org" do + user.organisation.update!(holds_own_stock: true) + expect(question.displayed_answer_options(log, user)).to eq(options) + end + + context "when the owning-managing organisation relationship is deleted" do + let(:options) do + { + "" => "Select an option", + user.organisation.id => "User org (Your organisation)", + owning_org_2.id => "Owning org 2", + } + end + + it "doesn't remove the housing provider from the list of allowed housing providers" do + log.update!(owning_organisation: owning_org_2) + expect(question.displayed_answer_options(log, user)).to eq(options) + org_rel.destroy! + expect(question.displayed_answer_options(log, user)).to eq(options) + end + end + end + + context "when user's org doesn't own stock" do + let(:options) do + { + "" => "Select an option", + owning_org_1.id => "Owning org 1", + owning_org_2.id => "Owning org 2", + } + end + + it "shows current stock owner at top, followed by the stock owners of the user's org" do + user.organisation.update!(holds_own_stock: false) + expect(question.displayed_answer_options(log, user)).to eq(options) + end + end + end + context "when user is support" do + let(:user) { create(:user, :support) } + + let(:log) { create(:lettings_log) } + + let(:non_stock_organisation) { create(:organisation, holds_own_stock: false) } + let(:expected_opts) do + Organisation.where(holds_own_stock: true).each_with_object(options) do |organisation, hsh| + hsh[organisation.id] = organisation.name + hsh + end + end + + it "shows orgs where organisation holds own stock" do + expect(question.displayed_answer_options(log, user)).to eq(expected_opts) + expect(question.displayed_answer_options(log, user)).not_to include(non_stock_organisation.id) + end + end end it "is marked as derived" do