Browse Source

CLDC-2723 Allow data coordinators access to stock owners' schemes (#1958)

* feat: allow data coordinators to create, edit and view stock owners' schemes

* feat: only show data coordinators their org and stock owners in scheme owner select, update location policy

* feat: update some tests

* feat: update tests

* feat: use zero? where appropriate and lint

* feat: update tests

* refactor: lint

* feat: use helper for answer options
pull/1979/head
natdeanlewissoftwire 1 year ago committed by GitHub
parent
commit
683dc1ad20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/controllers/schemes_controller.rb
  2. 2
      app/helpers/check_answers_helper.rb
  3. 25
      app/helpers/schemes_helper.rb
  4. 2
      app/models/scheme.rb
  5. 12
      app/policies/location_policy.rb
  6. 14
      app/policies/scheme_policy.rb
  7. 1
      app/views/schemes/check_answers.html.erb
  8. 12
      app/views/schemes/details.html.erb
  9. 8
      app/views/schemes/edit_name.html.erb
  10. 14
      app/views/schemes/new.html.erb
  11. 2
      app/views/schemes/show.html.erb
  12. 2
      spec/features/schemes_spec.rb
  13. 62
      spec/helpers/schemes_helper_spec.rb
  14. 6
      spec/requests/locations_controller_spec.rb
  15. 221
      spec/requests/schemes_controller_spec.rb

6
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?
if current_user.data_coordinator? && current_user.organisation.stock_owners.count.zero?
required_params[:owning_organisation_id] = current_user.organisation_id
end
required_params
@ -291,10 +291,6 @@ private
@scheme
end
def user_allowed_action?
current_user.support? || current_user.organisation == @scheme&.owning_organisation || current_user.organisation.parent_organisations.exists?(@scheme&.owning_organisation_id)
end
def redirect_if_scheme_confirmed
redirect_to @scheme if @scheme.confirmed?
end

2
app/helpers/check_answers_helper.rb

@ -14,7 +14,7 @@ module CheckAnswersHelper
def can_change_scheme_answer?(attribute_name, scheme)
return false unless current_user.support? || current_user.data_coordinator?
editable_attributes = current_user.support? ? ["Name", "Confidential information", "Housing stock owned by"] : ["Name", "Confidential information"]
editable_attributes = ["Name", "Confidential information", "Housing stock owned by"]
!scheme.confirmed? || editable_attributes.include?(attribute_name)
end

25
app/helpers/schemes_helper.rb

@ -1,6 +1,6 @@
module SchemesHelper
def display_scheme_attributes(scheme, user)
base_attributes = [
def display_scheme_attributes(scheme)
[
{ name: "Scheme code", value: scheme.id_to_display },
{ name: "Name", value: scheme.service_name, edit: true },
{ name: "Status", value: status_tag_from_resource(scheme) },
@ -16,16 +16,6 @@ module SchemesHelper
{ name: "Intended length of stay", value: scheme.intended_stay },
{ name: "Availability", value: scheme_availability(scheme) },
]
if user.data_coordinator?
base_attributes.delete_if { |item| item[:name] == "Housing stock owned by" }
end
if scheme.has_other_client_group == "Yes"
base_attributes.append
end
base_attributes
end
def scheme_availability(scheme)
@ -44,6 +34,17 @@ module SchemesHelper
return govuk_button_link_to "Reactivate this scheme", scheme_new_reactivation_path(scheme) if scheme.deactivated?
end
def owning_organisation_options(current_user)
all_orgs = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) }
user_org = [OpenStruct.new(id: current_user.organisation_id, name: current_user.organisation.name)]
stock_owners = current_user.organisation.stock_owners.map { |org| OpenStruct.new(id: org.id, name: org.name) }
current_user.support? ? all_orgs : user_org + stock_owners
end
def null_option
[OpenStruct.new(id: "", name: "Select an option")]
end
private
ActivePeriod = Struct.new(:from, :to)

2
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

12
app/policies/location_policy.rb

@ -16,14 +16,14 @@ class LocationPolicy
if location == Location
user.data_coordinator?
else
user.data_coordinator? && user.organisation == scheme&.owning_organisation
user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner
end
end
def update?
return true if user.support?
user.data_coordinator? && scheme&.owning_organisation == user.organisation
user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner
end
%w[
@ -51,7 +51,7 @@ class LocationPolicy
define_method method_name do
return true if user.support?
user.data_coordinator? && scheme&.owning_organisation == user.organisation
user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner
end
end
@ -62,7 +62,7 @@ class LocationPolicy
define_method method_name do
return true if user.support?
user.organisation.parent_organisations.exists?(scheme&.owning_organisation_id) || scheme&.owning_organisation == user.organisation
scheme_owned_by_user_org_or_stock_owner
end
end
@ -71,4 +71,8 @@ private
def scheme
location.scheme
end
def scheme_owned_by_user_org_or_stock_owner
scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id)
end
end

14
app/policies/scheme_policy.rb

@ -12,7 +12,7 @@ class SchemePolicy
if scheme == Scheme
true
else
user.organisation.parent_organisations.exists?(scheme&.owning_organisation_id) || scheme&.owning_organisation == user.organisation
scheme_owned_by_user_org_or_stock_owner
end
end
@ -27,7 +27,7 @@ class SchemePolicy
def update?
return true if user.support?
user.data_coordinator? && (scheme&.owning_organisation == user.organisation)
user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner
end
%w[
@ -37,7 +37,7 @@ class SchemePolicy
define_method method_name do
return true if user.support?
user.organisation.parent_organisations.exists?(scheme&.owning_organisation_id) || scheme&.owning_organisation == user.organisation
scheme_owned_by_user_org_or_stock_owner
end
end
@ -57,7 +57,13 @@ class SchemePolicy
define_method method_name do
return true if user.support?
user.data_coordinator? && scheme&.owning_organisation == user.organisation
user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner
end
end
private
def scheme_owned_by_user_org_or_stock_owner
scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id)
end
end

1
app/views/schemes/check_answers.html.erb

@ -9,7 +9,6 @@
<h2 class="govuk-visually-hidden">Scheme</h2>
<dl class="govuk-summary-list">
<% @scheme.check_details_attributes.each do |attr| %>
<% next if current_user.data_coordinator? && attr[:name] == ("owned by") %>
<%= render partial: "scheme_summary_list_row", locals: { scheme: @scheme, attribute: attr, change_link: @scheme.confirmed? ? scheme_edit_name_path(@scheme) : scheme_details_path(@scheme, check_answers: true) } %>
<% end %>

12
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,
@ -48,11 +44,11 @@
:description,
legend: { text: "Is this scheme registered under the Care Standards Act 2000?", size: "m" } %>
<% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %>
<% if current_user.support? %>
<% 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,
organisations,
owning_organisation_options(current_user),
:id,
:name,
label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" },

8
app/views/schemes/edit_name.html.erb

@ -25,11 +25,11 @@
label: { text: "This scheme contains confidential information" } %>
<% end %>
<% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %>
<% if current_user.support? %>
<% 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,
organisations,
owning_organisation_options(current_user),
:id,
:name,
label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" },

14
app/views/schemes/new.html.erb

@ -26,14 +26,6 @@
label: { text: "This scheme contains confidential information" } %>
<% end %>
<% null_option = [OpenStruct.new(id: "", name: "Select an option")] %>
<% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %>
<% answer_options = null_option + organisations %>
<% 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,
scheme_types_selection,
@ -52,9 +44,11 @@
:description,
legend: { text: "Is this scheme registered under the Care Standards Act 2000?", size: "m" } %>
<% if current_user.support? %>
<% 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,
answer_options,
null_option + owning_organisation_options(current_user),
:id,
:name,
label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" },

2
app/views/schemes/show.html.erb

@ -16,7 +16,7 @@
<h2 class="govuk-visually-hidden">Scheme</h2>
<%= govuk_summary_list do |summary_list| %>
<% display_scheme_attributes(@scheme, current_user).each do |attr| %>
<% display_scheme_attributes(@scheme).each do |attr| %>
<%= summary_list.row do |row| %>
<% row.key { attr[:name] } %>
<% row.value do %>

2
spec/features/schemes_spec.rb

@ -1069,7 +1069,7 @@ RSpec.describe "Schemes scheme Features" do
it "lets me see amended details on the check answers page" do
expect(page).to have_content "FooBar"
expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers")
expect(page).to have_link("Change", href: /schemes\/#{scheme.id}\/edit-name/, count: 2)
expect(page).to have_link("Change", href: /schemes\/#{scheme.id}\/edit-name/, count: 3)
end
end
end

62
spec/helpers/schemes_helper_spec.rb

@ -111,7 +111,7 @@ RSpec.describe SchemesHelper do
let(:coordinator_user) { FactoryBot.create(:user, :data_coordinator) }
context "when scheme has no locations" do
it "returns correct display attributes for a support user" do
it "returns correct display attributes" do
attributes = [
{ name: "Scheme code", value: "S#{scheme.id}" },
{ name: "Name", value: "Test service_name", edit: true },
@ -128,26 +128,7 @@ RSpec.describe SchemesHelper do
{ name: "Intended length of stay", value: "Permanent" },
{ name: "Availability", value: "Active from 1 April 2021" },
]
expect(display_scheme_attributes(scheme, support_user)).to eq(attributes)
end
it "returns correct display attributes for a coordinator user" do
attributes = [
{ name: "Scheme code", value: "S#{scheme.id}" },
{ name: "Name", value: "Test service_name", edit: true },
{ name: "Status", value: status_tag(:incomplete) },
{ name: "Confidential information", value: "No", edit: true },
{ name: "Type of scheme", value: "Housing for older people" },
{ name: "Registered under Care Standards Act 2000", value: "Yes – registered care home providing personal care" },
{ name: "Support services provided by", value: "A registered charity or voluntary organisation" },
{ name: "Primary client group", value: "Rough sleepers" },
{ name: "Has another client group", value: "Yes" },
{ name: "Secondary client group", value: "Refugees (permanent)" },
{ name: "Level of support given", value: "High level" },
{ name: "Intended length of stay", value: "Permanent" },
{ name: "Availability", value: "Active from 1 April 2021" },
]
expect(display_scheme_attributes(scheme, coordinator_user)).to eq(attributes)
expect(display_scheme_attributes(scheme)).to eq(attributes)
end
end
@ -156,7 +137,7 @@ RSpec.describe SchemesHelper do
FactoryBot.create(:location, scheme:)
end
it "returns correct display attributes for a support user" do
it "returns correct display attributes" do
attributes = [
{ name: "Scheme code", value: "S#{scheme.id}" },
{ name: "Name", value: "Test service_name", edit: true },
@ -173,31 +154,12 @@ RSpec.describe SchemesHelper do
{ name: "Intended length of stay", value: "Permanent" },
{ name: "Availability", value: "Active from 1 April 2021" },
]
expect(display_scheme_attributes(scheme, support_user)).to eq(attributes)
end
it "returns correct display attributes for a coordinator user" do
attributes = [
{ name: "Scheme code", value: "S#{scheme.id}" },
{ name: "Name", value: "Test service_name", edit: true },
{ name: "Status", value: status_tag(:active) },
{ name: "Confidential information", value: "No", edit: true },
{ name: "Type of scheme", value: "Housing for older people" },
{ name: "Registered under Care Standards Act 2000", value: "Yes – registered care home providing personal care" },
{ name: "Support services provided by", value: "A registered charity or voluntary organisation" },
{ name: "Primary client group", value: "Rough sleepers" },
{ name: "Has another client group", value: "Yes" },
{ name: "Secondary client group", value: "Refugees (permanent)" },
{ name: "Level of support given", value: "High level" },
{ name: "Intended length of stay", value: "Permanent" },
{ name: "Availability", value: "Active from 1 April 2021" },
]
expect(display_scheme_attributes(scheme, coordinator_user)).to eq(attributes)
expect(display_scheme_attributes(scheme)).to eq(attributes)
end
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
@ -206,7 +168,7 @@ RSpec.describe SchemesHelper do
context "with no deactivations" do
it "displays current collection start date as availability date if created_at is later than collection start date" do
scheme.update!(created_at: Time.zone.local(2022, 4, 16))
availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value]
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2021")
end
@ -221,7 +183,7 @@ RSpec.describe SchemesHelper do
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value]
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2021 to 9 August 2022\nDeactivated on 10 August 2022\nActive from 1 September 2022 to 14 September 2022\nDeactivated on 15 September 2022\nActive from 28 September 2022")
end
@ -235,7 +197,7 @@ RSpec.describe SchemesHelper do
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value]
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2021 to 9 August 2022\nDeactivated on 10 August 2022\nActive from 1 September 2022 to 14 September 2022\nDeactivated on 15 September 2022")
end
@ -251,7 +213,7 @@ RSpec.describe SchemesHelper do
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value]
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2021 to 14 June 2022\nDeactivated on 15 June 2022\nActive from 18 June 2022 to 23 September 2022\nDeactivated on 24 September 2022\nActive from 28 September 2022")
end
@ -265,7 +227,7 @@ RSpec.describe SchemesHelper do
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value]
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2021 to 14 June 2022\nDeactivated on 15 June 2022\nActive from 28 September 2022")
end
@ -282,7 +244,7 @@ RSpec.describe SchemesHelper do
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value]
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2021 to 14 June 2022\nDeactivated on 15 June 2022\nActive from 28 September 2022 to 23 October 2022\nDeactivated on 24 October 2022\nActive from 28 October 2022")
end
@ -297,7 +259,7 @@ RSpec.describe SchemesHelper do
end
it "displays the timeline of availability" do
availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value]
availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 1 April 2021 to 9 October 2022\nDeactivated on 10 October 2022\nActive from 11 December 2022")
end

6
spec/requests/locations_controller_spec.rb

@ -179,7 +179,7 @@ RSpec.describe LocationsController, type: :request do
get "/schemes/#{scheme.id}/locations"
end
context "when coordinator attempts to see scheme belonging to a different organisation" do
context "when coordinator attempts to see scheme belonging to a different (and not their parent) organisation" do
let(:another_scheme) { create(:scheme) }
before do
@ -302,8 +302,8 @@ RSpec.describe LocationsController, type: :request do
end
end
it "does not allow adding new locations" do
expect(page).not_to have_button("Add a location")
it "does allow adding new locations" do
expect(page).to have_button("Add a location")
end
end
end

221
spec/requests/schemes_controller_spec.rb

@ -457,7 +457,7 @@ RSpec.describe SchemesController, type: :request do
expect(page).to have_content(specific_scheme.intended_stay)
end
context "when coordinator attempts to see scheme belonging to a different organisation" do
context "when coordinator attempts to see scheme belonging to a different (and not their parent) organisation" do
let!(:specific_scheme) { create(:scheme) }
it "returns 401" do
@ -474,7 +474,6 @@ RSpec.describe SchemesController, type: :request do
end
context "when looking at scheme details" do
let(:user) { create(:user, :data_coordinator) }
let!(:scheme) { create(:scheme, owning_organisation: user.organisation) }
let(:add_deactivations) { scheme.scheme_deactivation_periods << scheme_deactivation_period }
@ -535,21 +534,68 @@ RSpec.describe SchemesController, type: :request do
context "when coordinator attempts to see scheme belonging to a parent organisation" do
let(:parent_organisation) { create(:organisation) }
let!(:specific_scheme) { create(:scheme, owning_organisation: parent_organisation) }
let(:add_deactivations) { specific_scheme.scheme_deactivation_periods << scheme_deactivation_period }
before do
create(:location, scheme: specific_scheme)
create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation)
Timecop.freeze(Time.utc(2022, 10, 10))
sign_in user
add_deactivations
specific_scheme.save!
get "/schemes/#{specific_scheme.id}"
end
after do
Timecop.unfreeze
end
context "with active scheme" do
let(:add_deactivations) {}
it "shows the scheme" do
expect(page).to have_content(specific_scheme.id_to_display)
end
it "does not allow editing the scheme" do
expect(page).not_to have_link("Change")
expect(page).not_to have_content("Reactivate this scheme")
expect(page).not_to have_content("Deactivate this scheme")
it "allows editing" do
expect(page).to have_link("Change")
end
it "renders deactivate this scheme" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Deactivate this scheme", href: "/schemes/#{specific_scheme.id}/new-deactivation")
end
end
context "with deactivated scheme" do
let(:scheme_deactivation_period) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), scheme: specific_scheme) }
it "renders reactivate this scheme" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{specific_scheme.id}/new-reactivation")
end
end
context "with scheme that's deactivating soon" do
let(:scheme_deactivation_period) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 12), scheme: specific_scheme) }
it "does not render toggle scheme link" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Reactivate this scheme")
expect(page).not_to have_link("Deactivate this scheme")
end
end
context "with scheme that's deactivating in more than 6 months" do
let(:scheme_deactivation_period) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 5, 12), scheme: specific_scheme) }
it "does not render toggle scheme link" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Reactivate this scheme")
expect(page).to have_link("Deactivate this scheme")
expect(response.body).not_to include("<strong class=\"govuk-tag govuk-tag--yellow\">Deactivating soon</strong>")
expect(response.body).to include("<strong class=\"govuk-tag govuk-tag--green\">Active</strong>")
end
end
end
@ -678,18 +724,21 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a data coordinator" do
let(:user) { create(:user, :data_coordinator) }
let(:params) do
before do
sign_in user
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
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!
@ -720,6 +769,7 @@ RSpec.describe SchemesController, type: :request do
sensitive: "1",
scheme_type: "Foyer",
registered_under_care_act: "No",
owning_organisation_id: user.organisation.id,
arrangement_type: "R" } }
end
@ -767,6 +817,25 @@ RSpec.describe SchemesController, type: :request do
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 } } }
@ -778,6 +847,117 @@ RSpec.describe SchemesController, type: :request do
end
end
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 ",
sensitive: "1",
scheme_type: "Foyer",
registered_under_care_act: "No",
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?")
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
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
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
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.stock_owners.first.id)
end
end
end
end
context "when signed in as a support user" do
let(:organisation) { create(:organisation) }
let(:user) { create(:user, :support) }
@ -1813,7 +1993,7 @@ RSpec.describe SchemesController, type: :request do
expect(response).to have_http_status(:ok)
expect(path).to match("/schemes/#{scheme.id}")
expect(page).to have_content(scheme.service_name)
assert_select "a", text: /Change/, count: 2
assert_select "a", text: /Change/, count: 3
end
end
end
@ -1958,7 +2138,7 @@ RSpec.describe SchemesController, type: :request do
expect(response).to have_http_status(:ok)
expect(path).to match("/schemes/#{scheme.id}")
expect(page).to have_content(scheme.service_name)
assert_select "a", text: /Change/, count: 2
assert_select "a", text: /Change/, count: 3
end
end
end
@ -2016,7 +2196,20 @@ RSpec.describe SchemesController, type: :request do
expect(response).to have_http_status(:ok)
expect(page).to have_content("Scheme details")
expect(page).to have_content("This scheme contains confidential information")
expect(page).not_to have_content("Which organisation owns the housing stock for this scheme?")
end
context "when there are stock owners" do
let(:parent_organisation) { create(:organisation) }
before do
create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation)
get "/schemes/#{scheme.id}/edit-name"
end
it "includes the owning organisation question" do
expect(response).to have_http_status(:ok)
expect(page).to have_content("Which organisation owns the housing stock for this scheme?")
end
end
context "when attempting to access secondary-client-group scheme page for another organisation" do

Loading…
Cancel
Save