From ff369579701f393fa63ff4e90ea66ea5430bc0c5 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Fri, 6 Oct 2023 12:24:08 +0100 Subject: [PATCH] feat: update tests --- app/controllers/schemes_controller.rb | 2 +- app/models/scheme.rb | 2 +- app/views/schemes/details.html.erb | 6 +- app/views/schemes/edit_name.html.erb | 2 +- app/views/schemes/new.html.erb | 2 +- spec/helpers/schemes_helper_spec.rb | 2 +- spec/requests/schemes_controller_spec.rb | 242 ++++++++++++++++++----- 7 files changed, 194 insertions(+), 64 deletions(-) diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 53055cf82..908cc65f8 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -273,7 +273,7 @@ private required_params[:sensitive] = required_params[:sensitive].to_i if required_params[:sensitive] - if current_user.data_coordinator? && current_user.organisation.stock_owners.nil? + if current_user.data_coordinator? && current_user.organisation.stock_owners.blank? required_params[:owning_organisation_id] = current_user.organisation_id end required_params diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 55d15dfb2..19a6491fd 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -252,7 +252,7 @@ class Scheme < ApplicationRecord end def validate_owning_organisation - unless owning_organisation.holds_own_stock? + unless owning_organisation&.holds_own_stock? errors.add(:owning_organisation_id, :does_not_own_stock, message: I18n.t("validations.scheme.owning_organisation.does_not_own_stock")) end end diff --git a/app/views/schemes/details.html.erb b/app/views/schemes/details.html.erb index 9b8e84e66..962d72548 100644 --- a/app/views/schemes/details.html.erb +++ b/app/views/schemes/details.html.erb @@ -25,10 +25,6 @@ label: { text: "This scheme contains confidential information" } %> <% end %> - <% if current_user.data_coordinator? %> - <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> - <% end %> - <% scheme_types_selection = Scheme.scheme_types.keys.excluding("Missing").map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> <%= f.govuk_collection_radio_buttons :scheme_type, @@ -53,7 +49,7 @@ <% stock_owners = current_user.organisation.stock_owners.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> <% organisations = current_user.support? ? all_orgs : user_org + stock_owners %> - <% if current_user.data_coordinator? && current_user.organisation.stock_owners.nil? %> + <% if current_user.data_coordinator? && current_user.organisation.stock_owners.count.zero? %> <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> <% else %> <%= f.govuk_collection_select :owning_organisation_id, diff --git a/app/views/schemes/edit_name.html.erb b/app/views/schemes/edit_name.html.erb index b5f18f422..5453deda2 100644 --- a/app/views/schemes/edit_name.html.erb +++ b/app/views/schemes/edit_name.html.erb @@ -30,7 +30,7 @@ <% stock_owners = current_user.organisation.stock_owners.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> <% organisations = current_user.support? ? all_orgs : user_org + stock_owners %> - <% if current_user.data_coordinator? && current_user.organisation.stock_owners.nil? %> + <% if current_user.data_coordinator? && current_user.organisation.stock_owners.count.zero? %> <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> <% else %> <%= f.govuk_collection_select :owning_organisation_id, diff --git a/app/views/schemes/new.html.erb b/app/views/schemes/new.html.erb index 2dadbe852..6f347c359 100644 --- a/app/views/schemes/new.html.erb +++ b/app/views/schemes/new.html.erb @@ -50,7 +50,7 @@ <% stock_owners = current_user.organisation.stock_owners.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> <% answer_options = current_user.support? ? null_option + all_orgs : null_option + user_org + stock_owners %> - <% if current_user.data_coordinator? && current_user.organisation.stock_owners.nil? %> + <% if current_user.data_coordinator? && current_user.organisation.stock_owners.count.zero? %> <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> <% else %> <%= f.govuk_collection_select :owning_organisation_id, diff --git a/spec/helpers/schemes_helper_spec.rb b/spec/helpers/schemes_helper_spec.rb index cf1d8af4d..9b5f72a58 100644 --- a/spec/helpers/schemes_helper_spec.rb +++ b/spec/helpers/schemes_helper_spec.rb @@ -199,7 +199,7 @@ RSpec.describe SchemesHelper do context "when the managing organisation is the owning organisation" do it "doesn't show the organisation providing support" do - attributes = display_scheme_attributes(scheme_where_managing_organisation_is_owning_organisation, support_user).find { |x| x[:name] == "Organisation providing support" } + attributes = display_scheme_attributes(scheme_where_managing_organisation_is_owning_organisation).find { |x| x[:name] == "Organisation providing support" } expect(attributes).to be_nil end end diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index fd740d31f..de46815f2 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -724,62 +724,159 @@ RSpec.describe SchemesController, type: :request do context "when signed in as a data coordinator" do let(:user) { create(:user, :data_coordinator) } - let(:params) do - { scheme: { service_name: " testy ", - sensitive: "1", - scheme_type: "Foyer", - registered_under_care_act: "No", - arrangement_type: "D" } } - end before do sign_in user end - it "creates a new scheme for user organisation with valid params and renders correct page" do - expect { post "/schemes", params: }.to change(Scheme, :count).by(1) - follow_redirect! - expect(response).to have_http_status(:ok) - expect(page).to have_content("What client group is this scheme intended for?") - end + context "when making a scheme in the user's organisation" do + let!(:params) do + { scheme: { service_name: " testy ", + sensitive: "1", + scheme_type: "Foyer", + registered_under_care_act: "No", + owning_organisation_id: user.organisation.id, + arrangement_type: "D" } } + end - it "creates a new scheme for user organisation with valid params" do - post "/schemes", params: params + it "creates a new scheme for user organisation with valid params and renders correct page" do + expect { post "/schemes", params: }.to change(Scheme, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("What client group is this scheme intended for?") + end - expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) - expect(Scheme.last.service_name).to eq("testy") - expect(Scheme.last.scheme_type).to eq("Foyer") - expect(Scheme.last.sensitive).to eq("Yes") - expect(Scheme.last.registered_under_care_act).to eq("No") - expect(Scheme.last.id).not_to eq(nil) - expect(Scheme.last.has_other_client_group).to eq(nil) - expect(Scheme.last.primary_client_group).to eq(nil) - expect(Scheme.last.secondary_client_group).to eq(nil) - expect(Scheme.last.support_type).to eq(nil) - expect(Scheme.last.intended_stay).to eq(nil) - expect(Scheme.last.id_to_display).to match(/S*/) + it "creates a new scheme for user organisation with valid params" do + post "/schemes", params: params + + expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + expect(Scheme.last.service_name).to eq("testy") + expect(Scheme.last.scheme_type).to eq("Foyer") + expect(Scheme.last.sensitive).to eq("Yes") + expect(Scheme.last.registered_under_care_act).to eq("No") + expect(Scheme.last.id).not_to eq(nil) + expect(Scheme.last.has_other_client_group).to eq(nil) + expect(Scheme.last.primary_client_group).to eq(nil) + expect(Scheme.last.secondary_client_group).to eq(nil) + expect(Scheme.last.support_type).to eq(nil) + expect(Scheme.last.intended_stay).to eq(nil) + expect(Scheme.last.id_to_display).to match(/S*/) + end + + context "when support services provider is selected" do + let(:params) do + { scheme: { service_name: "testy", + sensitive: "1", + scheme_type: "Foyer", + registered_under_care_act: "No", + owning_organisation_id: user.organisation.id, + arrangement_type: "R" } } + end + + it "creates a new scheme for user organisation with valid params and renders correct page" do + expect { post "/schemes", params: }.to change(Scheme, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content(" What client group is this scheme intended for?") + end + + it "creates a new scheme for user organisation with valid params" do + post "/schemes", params: params + + expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + expect(Scheme.last.service_name).to eq("testy") + expect(Scheme.last.scheme_type).to eq("Foyer") + expect(Scheme.last.sensitive).to eq("Yes") + expect(Scheme.last.registered_under_care_act).to eq("No") + expect(Scheme.last.id).not_to eq(nil) + expect(Scheme.last.has_other_client_group).to eq(nil) + expect(Scheme.last.primary_client_group).to eq(nil) + expect(Scheme.last.secondary_client_group).to eq(nil) + expect(Scheme.last.support_type).to eq(nil) + expect(Scheme.last.intended_stay).to eq(nil) + expect(Scheme.last.id_to_display).to match(/S*/) + end + end + + context "when required params are missing" do + let(:params) do + { scheme: { service_name: "", + scheme_type: "", + registered_under_care_act: "", + arrangement_type: "" } } + end + + it "renders the same page with error message" do + post "/schemes", params: params + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content("Create a new supported housing scheme") + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.scheme_type.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.registered_under_care_act.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.arrangement_type.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.service_name.invalid")) + end + end + + context "when there are no stock owners" do + let(:params) do + { scheme: { service_name: " testy ", + sensitive: "1", + scheme_type: "Foyer", + registered_under_care_act: "No", + arrangement_type: "D" } } + end + + before do + user.organisation.stock_owners.destroy_all + end + + it "infers the user's organisation" do + post "/schemes", params: params + expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + end + end + + context "when the organisation id param is included" do + let(:organisation) { create(:organisation) } + let(:params) { { scheme: { owning_organisation: organisation } } } + + it "sets the owning organisation correctly" do + post "/schemes", params: params + expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + end + end end - context "when support services provider is selected" do + context "when making a scheme in a parent organisation of the user's organisation" do + let(:parent_organisation) { create(:organisation) } + let!(:parent_schemes) { create_list(:scheme, 5, owning_organisation: parent_organisation) } let(:params) do - { scheme: { service_name: "testy", + { scheme: { service_name: " testy ", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", - arrangement_type: "R" } } + owning_organisation_id: user.organisation.stock_owners.first.id, + arrangement_type: "D" } } + end + + before do + create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation) + parent_schemes.each do |scheme| + create(:location, scheme:) + end end it "creates a new scheme for user organisation with valid params and renders correct page" do expect { post "/schemes", params: }.to change(Scheme, :count).by(1) follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content(" What client group is this scheme intended for?") + expect(page).to have_content("What client group is this scheme intended for?") end it "creates a new scheme for user organisation with valid params" do post "/schemes", params: params - expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + expect(Scheme.last.owning_organisation_id).to eq(user.organisation.stock_owners.first.id) expect(Scheme.last.service_name).to eq("testy") expect(Scheme.last.scheme_type).to eq("Foyer") expect(Scheme.last.sensitive).to eq("Yes") @@ -792,34 +889,71 @@ RSpec.describe SchemesController, type: :request do expect(Scheme.last.intended_stay).to eq(nil) expect(Scheme.last.id_to_display).to match(/S*/) end - end - context "when required params are missing" do - let(:params) do - { scheme: { service_name: "", - scheme_type: "", - registered_under_care_act: "", - arrangement_type: "" } } + context "when support services provider is selected" do + let(:params) do + { scheme: { service_name: "testy", + sensitive: "1", + scheme_type: "Foyer", + registered_under_care_act: "No", + owning_organisation_id: user.organisation.stock_owners.first.id, + arrangement_type: "R" } } + end + + it "creates a new scheme for user organisation with valid params and renders correct page" do + expect { post "/schemes", params: }.to change(Scheme, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content(" What client group is this scheme intended for?") + end + + it "creates a new scheme for user organisation with valid params" do + post "/schemes", params: params + + expect(Scheme.last.owning_organisation_id).to eq(user.organisation.stock_owners.first.id) + expect(Scheme.last.service_name).to eq("testy") + expect(Scheme.last.scheme_type).to eq("Foyer") + expect(Scheme.last.sensitive).to eq("Yes") + expect(Scheme.last.registered_under_care_act).to eq("No") + expect(Scheme.last.id).not_to eq(nil) + expect(Scheme.last.has_other_client_group).to eq(nil) + expect(Scheme.last.primary_client_group).to eq(nil) + expect(Scheme.last.secondary_client_group).to eq(nil) + expect(Scheme.last.support_type).to eq(nil) + expect(Scheme.last.intended_stay).to eq(nil) + expect(Scheme.last.id_to_display).to match(/S*/) + end end - it "renders the same page with error message" do - post "/schemes", params: params - expect(response).to have_http_status(:unprocessable_entity) - expect(page).to have_content("Create a new supported housing scheme") - expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.scheme_type.invalid")) - expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.registered_under_care_act.invalid")) - expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.arrangement_type.invalid")) - expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.service_name.invalid")) + context "when required params are missing" do + let(:params) do + { scheme: { service_name: "", + scheme_type: "", + registered_under_care_act: "", + arrangement_type: "", + owning_organisation_id: ""} } + end + + it "renders the same page with error message" do + post "/schemes", params: params + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content("Create a new supported housing scheme") + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.scheme_type.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.registered_under_care_act.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.arrangement_type.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.owning_organisation_id.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.service_name.invalid")) + end end - end - context "when the organisation id param is included" do - let(:organisation) { create(:organisation) } - let(:params) { { scheme: { owning_organisation: organisation } } } + context "when the organisation id param is included" do + let(:organisation) { create(:organisation) } + let(:params) { { scheme: { owning_organisation: organisation } } } - it "sets the owning organisation correctly" do - post "/schemes", params: params - expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + it "sets the owning organisation correctly" do + post "/schemes", params: params + expect(Scheme.last.owning_organisation_id).to eq(user.organisation.stock_owners.first.id) + end end end end