Browse Source

First set of changes

cldc-1228-organisation-fixes-juris
Stéphane Meny 3 years ago committed by JG
parent
commit
9df9efb23d
  1. 2
      app/controllers/schemes_controller.rb
  2. 4
      app/models/organisation.rb
  3. 8
      app/models/scheme.rb
  4. 2
      app/views/schemes/_scheme_list.html.erb
  5. 16
      app/views/schemes/details.html.erb
  6. 11
      app/views/schemes/new.html.erb
  7. 8
      db/migrate/20220705130923_rename_scheme_organisations.rb
  8. 14
      db/schema.rb
  9. 2
      spec/factories/scheme.rb
  10. 4
      spec/features/schemes_spec.rb
  11. 2
      spec/models/form/setup/questions/scheme_id_spec.rb
  12. 2
      spec/models/organisation_spec.rb
  13. 2
      spec/models/scheme_spec.rb
  14. 219
      spec/requests/schemes_controller_spec.rb

2
app/controllers/schemes_controller.rb

@ -132,7 +132,7 @@ private
def authenticate_scope!
head :unauthorized and return unless current_user.data_coordinator? || current_user.support?
if %w[show locations primary_client_group confirm_secondary_client_group secondary_client_group support details check_answers].include?(action_name) && !((current_user.organisation == @scheme.organisation) || current_user.support?)
if %w[show locations primary_client_group confirm_secondary_client_group secondary_client_group support details check_answers].include?(action_name) && !((current_user.organisation == @scheme.owning_organisation) || current_user.support?)
render_not_found and return
end
end

4
app/models/organisation.rb

@ -4,8 +4,8 @@ class Organisation < ApplicationRecord
has_many :managed_case_logs, class_name: "CaseLog", foreign_key: "managing_organisation_id"
has_many :data_protection_confirmations
has_many :organisation_rent_periods
has_many :owned_schemes, class_name: "Scheme", foreign_key: "stock_owning_organisation_id"
has_many :managed_schemes, class_name: "Scheme"
has_many :owned_schemes, class_name: "Scheme", foreign_key: "owning_organisation_id"
has_many :managed_schemes, class_name: "Scheme", foreign_key: "managing_organisation_id"
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param) }

8
app/models/scheme.rb

@ -1,6 +1,6 @@
class Scheme < ApplicationRecord
belongs_to :organisation
belongs_to :stock_owning_organisation, optional: true, class_name: "Organisation"
belongs_to :owning_organisation, class_name: "Organisation"
belongs_to :managing_organisation, optional: true, class_name: "Organisation"
has_many :locations
has_many :case_logs
@ -95,8 +95,8 @@ class Scheme < ApplicationRecord
{ name: "Service code", value: id_to_display },
{ name: "Name", value: service_name },
{ name: "Confidential information", value: sensitive },
{ name: "Housing stock owned by", value: stock_owning_organisation&.name },
{ name: "Managed by", value: organisation.name },
{ name: "Housing stock owned by", value: owning_organisation.name },
{ name: "Managed by", value: managing_organisation&.name },
{ name: "Type of scheme", value: scheme_type },
{ name: "Registered under Care Standards Act 2000", value: registered_under_care_act },
]

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

@ -24,7 +24,7 @@
<%= body.row do |row| %>
<% row.cell(text: scheme.id_to_display) %>
<% row.cell(text: simple_format(scheme_cell(scheme), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %>
<% row.cell(text: scheme.organisation.name) %>
<% row.cell(text: scheme.managing_organisation.name) %>
<% row.cell(text: scheme.created_at.to_formatted_s(:govuk_date)) %>
<% end %>
<% end %>

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

@ -29,25 +29,25 @@
<% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %>
<% stock_org_answer_options = null_option + organisations %>
<%= f.govuk_collection_select :stock_owning_organisation_id,
stock_org_answer_options,
<%= f.govuk_collection_select :managing_organisation_id,
organisations,
:id,
:name,
label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" },
label: { text: "Which organisation manages this scheme?", size: "m" },
options: { required: true },
"data-controller": %w[accessible-autocomplete conditional-filter] %>
<% if current_user.support? %>
<%= f.govuk_collection_select :organisation_id,
organisations,
<%= f.govuk_collection_select :owning_organisation_id,
stock_org_answer_options,
:id,
:name,
label: { text: "Which organisation manages this scheme?", size: "m" },
options: { required: true },
label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" },
"data-controller": %w[accessible-autocomplete conditional-filter] %>
<% end %>
<% if current_user.data_coordinator? %>
<%= f.hidden_field :organisation_id, value: current_user.organisation.id %>
<%= 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) } %>

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

@ -30,24 +30,23 @@
<% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %>
<% answer_options = null_option + organisations %>
<%= f.govuk_collection_select :stock_owning_organisation_id,
<%= f.govuk_collection_select :managing_organisation_id,
answer_options,
:id,
:name,
label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" },
label: { text: "Which organisation manages this scheme?", size: "m" },
"data-controller": %w[accessible-autocomplete conditional-filter] %>
<% if current_user.support? %>
<%= f.govuk_collection_select :organisation_id,
<%= f.govuk_collection_select :owning_organisation_id,
answer_options,
:id,
:name,
label: { text: "Which organisation manages this scheme?", size: "m" },
label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" },
"data-controller": %w[accessible-autocomplete conditional-filter] %>
<% end %>
<% if current_user.data_coordinator? %>
<%= f.hidden_field :organisation_id, value: current_user.organisation.id %>
<%= 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) } %>

8
db/migrate/20220705130923_rename_scheme_organisations.rb

@ -0,0 +1,8 @@
class RenameSchemeOrganisations < ActiveRecord::Migration[7.0]
def change
change_table :schemes, bulk: true do |t|
t.rename :organisation_id, :owning_organisation_id
t.rename :stock_owning_organisation_id, :managing_organisation_id
end
end
end

14
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_07_04_135746) do
ActiveRecord::Schema[7.0].define(version: 2022_07_05_130923) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -300,7 +300,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_04_135746) do
create_table "schemes", force: :cascade do |t|
t.string "service_name"
t.bigint "organisation_id", null: false
t.bigint "owning_organisation_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "primary_client_group"
@ -312,9 +312,9 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_04_135746) do
t.string "intended_stay"
t.datetime "end_date"
t.integer "has_other_client_group"
t.bigint "stock_owning_organisation_id"
t.index ["organisation_id"], name: "index_schemes_on_organisation_id"
t.index ["stock_owning_organisation_id"], name: "index_schemes_on_stock_owning_organisation_id"
t.bigint "managing_organisation_id"
t.index ["managing_organisation_id"], name: "index_schemes_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_schemes_on_owning_organisation_id"
end
create_table "users", force: :cascade do |t|
@ -373,6 +373,6 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_04_135746) do
add_foreign_key "case_logs", "locations"
add_foreign_key "case_logs", "schemes"
add_foreign_key "locations", "schemes"
add_foreign_key "schemes", "organisations"
add_foreign_key "schemes", "organisations", column: "stock_owning_organisation_id"
add_foreign_key "schemes", "organisations", column: "managing_organisation_id"
add_foreign_key "schemes", "organisations", column: "owning_organisation_id"
end

2
spec/factories/scheme.rb

@ -8,7 +8,7 @@ FactoryBot.define do
intended_stay { %w[M P S V X].sample }
primary_client_group { %w[O H M L A G F B D E I S N R Q P X].sample }
secondary_client_group { %w[O H M L A G F B D E I S N R Q P X].sample }
organisation
owning_organisation { FactoryBot.create(:organisation) }
created_at { Time.zone.now }
end
end

4
spec/features/schemes_spec.rb

@ -4,8 +4,8 @@ RSpec.describe "Schemes scheme Features" do
context "when viewing list of schemes" do
context "when I am signed as a coordinator user and there are schemes in the database" do
let!(:user) { FactoryBot.create(:user, :data_coordinator, last_sign_in_at: Time.zone.now) }
let!(:schemes) { FactoryBot.create_list(:scheme, 5, organisation: user.organisation) }
let!(:scheme_to_search) { FactoryBot.create(:scheme, organisation: user.organisation) }
let!(:schemes) { FactoryBot.create_list(:scheme, 5, owning_organisation: user.organisation) }
let!(:scheme_to_search) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
before do
visit("/logs")

2
spec/models/form/setup/questions/scheme_id_spec.rb

@ -47,7 +47,7 @@ RSpec.describe Form::Setup::Questions::SchemeId, type: :model do
let(:case_log) { FactoryBot.create(:case_log, created_by: user) }
before do
FactoryBot.create(:scheme, organisation_id: organisation_2.id)
FactoryBot.create(:scheme, owning_organisation: organisation_2)
end
it "has the correct answer_options based on the schemes the user's organisation owns or manages" do

2
spec/models/organisation_spec.rb

@ -4,7 +4,7 @@ RSpec.describe Organisation, type: :model do
describe "#new" do
let(:user) { FactoryBot.create(:user) }
let!(:organisation) { user.organisation }
let!(:scheme) { FactoryBot.create(:scheme, organisation:, stock_owning_organisation: organisation) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: organisation, managing_organisation: organisation) }
it "has expected fields" do
expect(organisation.attribute_names).to include("name", "phone", "provider_type")

2
spec/models/scheme_spec.rb

@ -5,7 +5,7 @@ RSpec.describe Scheme, type: :model do
let(:scheme) { FactoryBot.create(:scheme) }
it "belongs to an organisation" do
expect(scheme.organisation).to be_a(Organisation)
expect(scheme.owning_organisation).to be_a(Organisation)
end
describe "scopes" do

219
spec/requests/schemes_controller_spec.rb

@ -203,7 +203,7 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a data coordinator user" do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:specific_scheme) { FactoryBot.create(:scheme, organisation: user.organisation) }
let!(:specific_scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
before do
sign_in user
@ -261,6 +261,205 @@ RSpec.describe SchemesController, type: :request do
end
end
describe "#locations" do
let(:specific_scheme) { schemes.first }
context "when not signed in" do
it "redirects to the sign in page" do
get "/schemes/#{specific_scheme.id}/locations"
expect(response).to redirect_to("/account/sign-in")
end
end
context "when signed in as a data provider user" do
let(:user) { FactoryBot.create(:user) }
before do
sign_in user
get "/schemes/#{specific_scheme.id}/locations"
end
it "returns 401 unauthorized" do
request
expect(response).to have_http_status(:unauthorized)
end
end
context "when signed in as a data coordinator user" do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:locations) { FactoryBot.create_list(:location, 3, scheme:) }
before do
sign_in user
get "/schemes/#{scheme.id}/locations"
end
context "when coordinator attempts to see scheme belonging to a different organisation" do
let!(:specific_scheme) { FactoryBot.create(:scheme) }
before do
FactoryBot.create(:location, scheme: specific_scheme)
end
it "returns 404 not found" do
get "/schemes/#{specific_scheme.id}/locations"
expect(response).to have_http_status(:not_found)
end
end
it "shows scheme" do
locations.each do |location|
expect(page).to have_content(location.location_code)
expect(page).to have_content(location.postcode)
expect(page).to have_content(location.county)
expect(page).to have_content(location.type_of_unit)
expect(page).to have_content(location.type_of_building)
expect(page).to have_content(location.wheelchair_adaptation)
expect(page).to have_content(location.name)
end
end
it "has page heading" do
expect(page).to have_content(scheme.service_name)
end
it "has correct title" do
expected_title = CGI.escapeHTML("#{scheme.service_name} - Submit social housing lettings and sales data (CORE) - GOV.UK")
expect(page).to have_title(expected_title)
end
context "when paginating over 20 results" do
let!(:locations) { FactoryBot.create_list(:location, 25, scheme:) }
context "when on the first page" do
before do
get "/schemes/#{scheme.id}/locations"
end
it "shows which schemes are being shown on the current page" do
expect(CGI.unescape_html(response.body)).to match("Showing <b>1</b> to <b>20</b> of <b>#{locations.count}</b> locations")
end
it "has correct page 1 of 2 title" do
expected_title = CGI.escapeHTML("#{scheme.service_name} (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK")
expect(page).to have_title(expected_title)
end
it "has pagination links" do
expect(page).not_to have_content("Previous")
expect(page).not_to have_link("Previous")
expect(page).to have_content("Next")
expect(page).to have_link("Next")
end
end
context "when on the second page" do
before do
get "/schemes/#{scheme.id}/locations?page=2"
end
it "shows which schemes are being shown on the current page" do
expect(CGI.unescape_html(response.body)).to match("Showing <b>21</b> to <b>25</b> of <b>#{locations.count}</b> locations")
end
it "has correct page 2 of 2 title" do
expected_title = CGI.escapeHTML("#{scheme.service_name} (page 2 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK")
expect(page).to have_title(expected_title)
end
it "has pagination links" do
expect(page).to have_content("Previous")
expect(page).to have_link("Previous")
expect(page).not_to have_content("Next")
expect(page).not_to have_link("Next")
end
end
end
end
context "when signed in as a support user" do
let(:user) { FactoryBot.create(:user, :support) }
let!(:scheme) { FactoryBot.create(:scheme) }
let!(:locations) { FactoryBot.create_list(:location, 3, scheme:) }
before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
get "/schemes/#{scheme.id}/locations"
end
it "shows scheme" do
locations.each do |location|
expect(page).to have_content(location.location_code)
expect(page).to have_content(location.postcode)
expect(page).to have_content(location.county)
expect(page).to have_content(location.type_of_unit)
expect(page).to have_content(location.type_of_building)
expect(page).to have_content(location.wheelchair_adaptation)
expect(page).to have_content(location.name)
end
end
it "has page heading" do
expect(page).to have_content(scheme.service_name)
end
it "has correct title" do
expected_title = CGI.escapeHTML("#{scheme.service_name} - Submit social housing lettings and sales data (CORE) - GOV.UK")
expect(page).to have_title(expected_title)
end
context "when paginating over 20 results" do
let!(:locations) { FactoryBot.create_list(:location, 25, scheme:) }
context "when on the first page" do
before do
get "/schemes/#{scheme.id}/locations"
end
it "shows which schemes are being shown on the current page" do
expect(CGI.unescape_html(response.body)).to match("Showing <b>1</b> to <b>20</b> of <b>#{locations.count}</b> locations")
end
it "has correct page 1 of 2 title" do
expected_title = CGI.escapeHTML("#{scheme.service_name} (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK")
expect(page).to have_title(expected_title)
end
it "has pagination links" do
expect(page).not_to have_content("Previous")
expect(page).not_to have_link("Previous")
expect(page).to have_content("Next")
expect(page).to have_link("Next")
end
end
context "when on the second page" do
before do
get "/schemes/#{scheme.id}/locations?page=2"
end
it "shows which schemes are being shown on the current page" do
expect(CGI.unescape_html(response.body)).to match("Showing <b>21</b> to <b>25</b> of <b>#{locations.count}</b> locations")
end
it "has correct page 1 of 2 title" do
expected_title = CGI.escapeHTML("#{scheme.service_name} (page 2 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK")
expect(page).to have_title(expected_title)
end
it "has pagination links" do
expect(page).to have_content("Previous")
expect(page).to have_link("Previous")
expect(page).not_to have_content("Next")
expect(page).not_to have_link("Next")
end
end
end
end
end
describe "#new" do
context "when not signed in" do
it "redirects to the sign in page" do
@ -401,7 +600,7 @@ RSpec.describe SchemesController, type: :request do
end
context "when required organisation id param is missing" do
let(:params) { { "scheme" => { "service_name" => "qweqwer", "sensitive" => "Yes", "organisation_id" => "", "scheme_type" => "Foyer", "registered_under_care_act" => "Yes – part registered as a care home" } } }
let(:params) { { "scheme" => { "service_name" => "qweqwer", "sensitive" => "Yes", "owning_organisation_id" => "", "scheme_type" => "Foyer", "registered_under_care_act" => "Yes – part registered as a care home", "total_units" => "1" } } }
it "displays the new page with an error message" do
post "/schemes", params: params
@ -436,7 +635,7 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let(:scheme_to_update) { FactoryBot.create(:scheme, organisation: user.organisation) }
let(:scheme_to_update) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
before do
sign_in user
@ -619,7 +818,7 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a support" do
let(:user) { FactoryBot.create(:user, :support) }
let(:scheme_to_update) { FactoryBot.create(:scheme, organisation: user.organisation) }
let(:scheme_to_update) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
@ -837,7 +1036,7 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:another_scheme) { FactoryBot.create(:scheme) }
before do
@ -903,7 +1102,7 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:another_scheme) { FactoryBot.create(:scheme) }
before do
@ -969,7 +1168,7 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:another_scheme) { FactoryBot.create(:scheme) }
before do
@ -1035,7 +1234,7 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:another_scheme) { FactoryBot.create(:scheme) }
before do
@ -1101,7 +1300,7 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:another_scheme) { FactoryBot.create(:scheme) }
before do
@ -1167,7 +1366,7 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, organisation: user.organisation) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:another_scheme) { FactoryBot.create(:scheme) }
before do

Loading…
Cancel
Save