Browse Source

feat: update tests

pull/1958/head
natdeanlewissoftwire 3 years ago
parent
commit
ff36957970
  1. 2
      app/controllers/schemes_controller.rb
  2. 2
      app/models/scheme.rb
  3. 6
      app/views/schemes/details.html.erb
  4. 2
      app/views/schemes/edit_name.html.erb
  5. 2
      app/views/schemes/new.html.erb
  6. 2
      spec/helpers/schemes_helper_spec.rb
  7. 144
      spec/requests/schemes_controller_spec.rb

2
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

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

6
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,

2
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,

2
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,

2
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

144
spec/requests/schemes_controller_spec.rb

@ -724,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!
@ -766,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
@ -813,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 } } }
@ -824,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) }

Loading…
Cancel
Save