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. 242
      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] 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 required_params[:owning_organisation_id] = current_user.organisation_id
end end
required_params required_params

2
app/models/scheme.rb

@ -252,7 +252,7 @@ class Scheme < ApplicationRecord
end end
def validate_owning_organisation 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")) errors.add(:owning_organisation_id, :does_not_own_stock, message: I18n.t("validations.scheme.owning_organisation.does_not_own_stock"))
end end
end end

6
app/views/schemes/details.html.erb

@ -25,10 +25,6 @@
label: { text: "This scheme contains confidential information" } %> label: { text: "This scheme contains confidential information" } %>
<% end %> <% 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) } %> <% 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, <%= 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) } %> <% 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 %> <% 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 %> <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %>
<% else %> <% else %>
<%= f.govuk_collection_select :owning_organisation_id, <%= 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) } %> <% 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 %> <% 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 %> <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %>
<% else %> <% else %>
<%= f.govuk_collection_select :owning_organisation_id, <%= 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) } %> <% 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 %> <% 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 %> <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %>
<% else %> <% else %>
<%= f.govuk_collection_select :owning_organisation_id, <%= 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 context "when the managing organisation is the owning organisation" do
it "doesn't show the organisation providing support" 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 expect(attributes).to be_nil
end end
end end

242
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 context "when signed in as a data coordinator" do
let(:user) { create(:user, :data_coordinator) } 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 before do
sign_in user sign_in user
end end
it "creates a new scheme for user organisation with valid params and renders correct page" do context "when making a scheme in the user's organisation" do
expect { post "/schemes", params: }.to change(Scheme, :count).by(1) let!(:params) do
follow_redirect! { scheme: { service_name: " testy ",
expect(response).to have_http_status(:ok) sensitive: "1",
expect(page).to have_content("What client group is this scheme intended for?") scheme_type: "Foyer",
end 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 it "creates a new scheme for user organisation with valid params and renders correct page" do
post "/schemes", params: params 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) it "creates a new scheme for user organisation with valid params" do
expect(Scheme.last.service_name).to eq("testy") post "/schemes", params: params
expect(Scheme.last.scheme_type).to eq("Foyer")
expect(Scheme.last.sensitive).to eq("Yes") expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id)
expect(Scheme.last.registered_under_care_act).to eq("No") expect(Scheme.last.service_name).to eq("testy")
expect(Scheme.last.id).not_to eq(nil) expect(Scheme.last.scheme_type).to eq("Foyer")
expect(Scheme.last.has_other_client_group).to eq(nil) expect(Scheme.last.sensitive).to eq("Yes")
expect(Scheme.last.primary_client_group).to eq(nil) expect(Scheme.last.registered_under_care_act).to eq("No")
expect(Scheme.last.secondary_client_group).to eq(nil) expect(Scheme.last.id).not_to eq(nil)
expect(Scheme.last.support_type).to eq(nil) expect(Scheme.last.has_other_client_group).to eq(nil)
expect(Scheme.last.intended_stay).to eq(nil) expect(Scheme.last.primary_client_group).to eq(nil)
expect(Scheme.last.id_to_display).to match(/S*/) 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 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 let(:params) do
{ scheme: { service_name: "testy", { scheme: { service_name: " testy ",
sensitive: "1", sensitive: "1",
scheme_type: "Foyer", scheme_type: "Foyer",
registered_under_care_act: "No", 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 end
it "creates a new scheme for user organisation with valid params and renders correct page" do 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) expect { post "/schemes", params: }.to change(Scheme, :count).by(1)
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) 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 end
it "creates a new scheme for user organisation with valid params" do it "creates a new scheme for user organisation with valid params" do
post "/schemes", params: params 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.service_name).to eq("testy")
expect(Scheme.last.scheme_type).to eq("Foyer") expect(Scheme.last.scheme_type).to eq("Foyer")
expect(Scheme.last.sensitive).to eq("Yes") 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.intended_stay).to eq(nil)
expect(Scheme.last.id_to_display).to match(/S*/) expect(Scheme.last.id_to_display).to match(/S*/)
end end
end
context "when required params are missing" do context "when support services provider is selected" do
let(:params) do let(:params) do
{ scheme: { service_name: "", { scheme: { service_name: "testy",
scheme_type: "", sensitive: "1",
registered_under_care_act: "", scheme_type: "Foyer",
arrangement_type: "" } } 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 end
it "renders the same page with error message" do context "when required params are missing" do
post "/schemes", params: params let(:params) do
expect(response).to have_http_status(:unprocessable_entity) { scheme: { service_name: "",
expect(page).to have_content("Create a new supported housing scheme") scheme_type: "",
expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.scheme_type.invalid")) registered_under_care_act: "",
expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.registered_under_care_act.invalid")) arrangement_type: "",
expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.arrangement_type.invalid")) owning_organisation_id: ""} }
expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.service_name.invalid")) 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
end
context "when the organisation id param is included" do context "when the organisation id param is included" do
let(:organisation) { create(:organisation) } let(:organisation) { create(:organisation) }
let(:params) { { scheme: { owning_organisation: organisation } } } let(:params) { { scheme: { owning_organisation: organisation } } }
it "sets the owning organisation correctly" do it "sets the owning organisation correctly" do
post "/schemes", params: params 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)
end
end end
end end
end end

Loading…
Cancel
Save