Browse Source

Case logs belong to organisations

pull/114/head
baarkerlounger 4 years ago
parent
commit
93ab6bb7f4
  1. 119
      app/admin/case_logs.rb
  2. 17
      app/controllers/case_logs_controller.rb
  3. 14
      app/models/bulk_upload.rb
  4. 7
      app/models/case_log.rb
  5. 2
      app/models/organisation.rb
  6. 1
      app/models/user.rb
  7. 1
      app/views/case_logs/index.html.erb
  8. 15
      db/migrate/20211126142105_user_belongs_to_organisation.rb
  9. 23
      db/migrate/20211126142105_user_case_logs_belong_to_organisation.rb
  10. 4
      db/schema.rb
  11. 13
      spec/controllers/admin/case_logs_controller_spec.rb
  12. 13
      spec/controllers/case_logs_controller_spec.rb
  13. 2
      spec/factories/case_log.rb
  14. 6
      spec/features/form/form_navigation_spec.rb
  15. 319
      spec/models/case_log_spec.rb
  16. 4
      spec/requests/bulk_upload_controller_spec.rb
  17. 15
      spec/requests/case_log_controller_spec.rb

119
app/admin/case_logs.rb

@ -2,7 +2,122 @@ ActiveAdmin.register CaseLog do
# See permitted parameters documentation: # See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
permit_params do permit_params do
permitted = %i[status tenant_code age1 sex1 tenant_ethnic_group tenant_nationality previous_housing_situation armedforces ecstat1 other_hhmemb relat2 age2 sex2 ecstat2 relat3 age3 sex3 ecstat3 relat4 age4 sex4 ecstat4 relat5 age5 sex5 ecstat5 relat6 age6 sex6 ecstat6 relat7 age7 person_7_gender ecstat7 relat8 age8 sex8 ecstat8 homelessness reason benefit_cap_spare_room_subsidy armed_forces_active armed_forces_injured medical_conditions pregnancy accessibility_requirements condition_effects tenancy_code tenancy_start_date starter_tenancy fixed_term_tenancy tenancy_type letting_type letting_provider la previous_postcode property_relet property_vacancy_reason property_reference property_unit_type property_building_type property_number_of_bedrooms property_void_date majorrepairs mrcdate property_wheelchair_accessible net_income net_income_frequency net_income_uc_proportion hb rent_frequency basic_rent service_charge personal_service_charge support_charge total_charge tshortfall time_lived_in_la time_on_la_waiting_list prevloc property_postcode reasonable_preference reasonable_preference_reason cbl_letting chr_letting cap_letting hbrentshortfall other_reason accessibility_requirements_fully_wheelchair_accessible_housing accessibility_requirements_wheelchair_access_to_essential_rooms accessibility_requirements_level_access_housing accessibility_requirements_other_disability_requirements accessibility_requirements_no_disability_requirements accessibility_requirements_do_not_know accessibility_requirements_prefer_not_to_say condition_effects_vision condition_effects_hearing condition_effects_mobility condition_effects_dexterity condition_effects_stamina condition_effects_learning condition_effects_memory condition_effects_mental_health condition_effects_social_or_behavioral condition_effects_other condition_effects_prefer_not_to_say reasonable_preference_reason_homeless reasonable_preference_reason_unsatisfactory_housing reasonable_preference_reason_medical_grounds reasonable_preference_reason_avoid_hardship reasonable_preference_reason_do_not_know other_tenancy_type override_net_income_validation net_income_known] permitted = %i[status
tenant_code
age1
sex1
tenant_ethnic_group
tenant_nationality
previous_housing_situation
armedforces
ecstat1
other_hhmemb
relat2
age2
sex2
ecstat2
relat3
age3
sex3
ecstat3
relat4
age4
sex4
ecstat4
relat5
age5
sex5
ecstat5
relat6
age6
sex6
ecstat6
relat7
age7
person_7_gender
ecstat7
relat8
age8
sex8
ecstat8
homelessness
reason
benefit_cap_spare_room_subsidy
armed_forces_active
armed_forces_injured
medical_conditions
pregnancy
accessibility_requirements
condition_effects
tenancy_code
tenancy_start_date
starter_tenancy
fixed_term_tenancy
tenancy_type
letting_type
letting_provider
la
previous_postcode
property_relet
property_vacancy_reason
property_reference
property_unit_type
property_building_type
property_number_of_bedrooms
property_void_date
majorrepairs
mrcdate
property_wheelchair_accessible
net_income
net_income_frequency
net_income_uc_proportion
hb
rent_frequency
basic_rent
service_charge
personal_service_charge
support_charge
total_charge
tshortfall
time_lived_in_la
time_on_la_waiting_list
prevloc
property_postcode
reasonable_preference
reasonable_preference_reason
cbl_letting
chr_letting
cap_letting
hbrentshortfall
other_reason
accessibility_requirements_fully_wheelchair_accessible_housing
accessibility_requirements_wheelchair_access_to_essential_rooms
accessibility_requirements_level_access_housing
accessibility_requirements_other_disability_requirements
accessibility_requirements_no_disability_requirements
accessibility_requirements_do_not_know
accessibility_requirements_prefer_not_to_say
condition_effects_vision
condition_effects_hearing
condition_effects_mobility
condition_effects_dexterity
condition_effects_stamina
condition_effects_learning
condition_effects_memory
condition_effects_mental_health
condition_effects_social_or_behavioral
condition_effects_other
condition_effects_prefer_not_to_say
reasonable_preference_reason_homeless
reasonable_preference_reason_unsatisfactory_housing
reasonable_preference_reason_medical_grounds
reasonable_preference_reason_avoid_hardship
reasonable_preference_reason_do_not_know
other_tenancy_type
override_net_income_validation
net_income_known
owning_organisation_id
managing_organisation_id]
permitted permitted
end end
@ -14,6 +129,8 @@ ActiveAdmin.register CaseLog do
column :status column :status
column :tenant_code column :tenant_code
column :property_postcode column :property_postcode
column :owning_organisation
column :managing_organisation
actions actions
end end
end end

17
app/controllers/case_logs_controller.rb

@ -9,7 +9,7 @@ class CaseLogsController < ApplicationController
end end
def create def create
case_log = CaseLog.create(api_case_log_params) case_log = CaseLog.create(case_log_params)
respond_to do |format| respond_to do |format|
format.html { redirect_to case_log } format.html { redirect_to case_log }
format.json do format.json do
@ -138,6 +138,21 @@ private
http_basic_authenticate_or_request_with name: ENV["API_USER"], password: ENV["API_KEY"] http_basic_authenticate_or_request_with name: ENV["API_USER"], password: ENV["API_KEY"]
end end
def case_log_params
if current_user
org_params.merge(api_case_log_params)
else
api_case_log_params
end
end
def org_params
{
"owning_organisation_id" => current_user.organisation.id,
"managing_organisation_id" => current_user.organisation.id,
}
end
def api_case_log_params def api_case_log_params
return {} unless params[:case_log] return {} unless params[:case_log]

14
app/models/bulk_upload.rb

@ -26,8 +26,14 @@ class BulkUpload
else else
data_range = FIRST_DATA_ROW..last_row data_range = FIRST_DATA_ROW..last_row
data_range.map do |row_num| data_range.map do |row_num|
case_log = CaseLog.create! row = sheet.row(row_num)
map_row(sheet.row(row_num)).each do |attr_key, attr_val| owning_organisation = Organisation.find(row[111])
managing_organisation = Organisation.find(row[113])
case_log = CaseLog.create!(
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
map_row(row).each do |attr_key, attr_val|
update = case_log.update(attr_key => attr_val) update = case_log.update(attr_key => attr_val)
unless update unless update
# TODO: determine what to do when a bulk upload contains field values that don't pass validations # TODO: determine what to do when a bulk upload contains field values that don't pass validations
@ -167,9 +173,9 @@ class BulkUpload
# postcode: row[108], # postcode: row[108],
# postcod2: row[109], # postcod2: row[109],
# row[110] removed # row[110] removed
property_owner_organisation: row[111], # row[111] is owning organisation used above
# username: row[112], # username: row[112],
property_manager_organisation: row[113], # row[113] is managing organisation used above
leftreg: row[114], leftreg: row[114],
# uprn: row[115], # uprn: row[115],
incfreq: row[116], incfreq: row[116],

7
app/models/case_log.rb

@ -44,6 +44,9 @@ class CaseLog < ApplicationRecord
validates_with CaseLogValidator validates_with CaseLogValidator
before_save :update_status! before_save :update_status!
belongs_to :owning_organisation, class_name: "Organisation"
belongs_to :managing_organisation, class_name: "Organisation"
attr_accessor :page_id attr_accessor :page_id
enum status: { "not_started" => 0, "in_progress" => 1, "completed" => 2 } enum status: { "not_started" => 0, "in_progress" => 1, "completed" => 2 }
@ -224,7 +227,9 @@ private
end end
def all_fields_nil? def all_fields_nil?
mandatory_fields.all? { |_key, val| val.nil? } init_fields = %w[owning_organisation_id managing_organisation_id]
fields = mandatory_fields.except(*init_fields)
fields.all? { |_key, val| val.nil? }
end end
def mandatory_fields def mandatory_fields

2
app/models/organisation.rb

@ -1,3 +1,5 @@
class Organisation < ApplicationRecord class Organisation < ApplicationRecord
has_many :users has_many :users
has_many :case_logs, as: :owning_organisation
has_many :case_logs, as: :managing_organisation
end end

1
app/models/user.rb

@ -4,4 +4,5 @@ class User < ApplicationRecord
devise :database_authenticatable, :recoverable, :rememberable, :validatable devise :database_authenticatable, :recoverable, :rememberable, :validatable
belongs_to :organisation belongs_to :organisation
has_many :case_logs, through: :organisation
end end

1
app/views/case_logs/index.html.erb

@ -3,7 +3,6 @@
<h1 class="govuk-heading-l">Your logs</h1> <h1 class="govuk-heading-l">Your logs</h1>
</div> </div>
<div class="govuk-grid-column-two-thirds-from-desktop"> <div class="govuk-grid-column-two-thirds-from-desktop">
<%= link_to "Create new log", case_logs_path, method: :post, class: "govuk-button" %> <%= link_to "Create new log", case_logs_path, method: :post, class: "govuk-button" %>
<% if @in_progress_case_logs.present? %> <% if @in_progress_case_logs.present? %>

15
db/migrate/20211126142105_user_belongs_to_organisation.rb

@ -1,15 +0,0 @@
class UserBelongsToOrganisation < ActiveRecord::Migration[6.1]
def up
change_table :users, bulk: true do |t|
t.remove :organisation
t.belongs_to :organisation
end
end
def down
change_table :users, bulk: true do |t|
t.remove :organisation_id
t.column :organisation, :string
end
end
end

23
db/migrate/20211126142105_user_case_logs_belong_to_organisation.rb

@ -0,0 +1,23 @@
class UserCaseLogsBelongToOrganisation < ActiveRecord::Migration[6.1]
def up
change_table :users, bulk: true do |t|
t.remove :organisation
t.belongs_to :organisation
end
change_table :case_logs, bulk: true do |t|
t.belongs_to :owning_organisation, class_name: "Organisation"
t.belongs_to :managing_organisation, class_name: "Organisation"
end
end
def down
change_table :users, bulk: true do |t|
t.remove :organisation_id
t.column :organisation, :string
end
change_table :case_logs, bulk: true do |t|
t.remove :owning_organisation_id
t.remove :managing_organisation_id
end
end
end

4
db/schema.rb

@ -166,7 +166,11 @@ ActiveRecord::Schema.define(version: 2021_11_26_142105) do
t.string "why_dont_you_know_la" t.string "why_dont_you_know_la"
t.integer "unitletas" t.integer "unitletas"
t.integer "builtype" t.integer "builtype"
t.bigint "owning_organisation_id"
t.bigint "managing_organisation_id"
t.index ["discarded_at"], name: "index_case_logs_on_discarded_at" t.index ["discarded_at"], name: "index_case_logs_on_discarded_at"
t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id"
end end
create_table "organisations", force: :cascade do |t| create_table "organisations", force: :cascade do |t|

13
spec/controllers/admin/case_logs_controller_spec.rb

@ -18,12 +18,23 @@ describe Admin::CaseLogsController, type: :controller do
expect(page).to have_content(resource_title) expect(page).to have_content(resource_title)
expect(page).to have_table("index_table_case_logs") expect(page).to have_table("index_table_case_logs")
expect(page).to have_link(case_log.id.to_s) expect(page).to have_link(case_log.id.to_s)
expect(page).to have_link(case_log.owning_organisation.name.to_s)
end end
end end
describe "Create case logs" do describe "Create case logs" do
let(:owning_organisation) { FactoryBot.create(:organisation) }
let(:managing_organisation) { owning_organisation }
let(:params) do
{
"case_log": {
"owning_organisation_id": owning_organisation.id,
"managing_organisation_id": managing_organisation.id,
},
}
end
it "creates a new case log" do it "creates a new case log" do
expect { post :create, session: valid_session }.to change(CaseLog, :count).by(1) expect { post :create, session: valid_session, params: params }.to change(CaseLog, :count).by(1)
end end
end end
end end

13
spec/controllers/case_logs_controller_spec.rb

@ -14,14 +14,23 @@ RSpec.describe CaseLogsController, type: :controller do
end end
describe "Post #create" do describe "Post #create" do
let(:owning_organisation) { FactoryBot.create(:organisation) }
let(:managing_organisation) { owning_organisation }
let(:params) do
{
"owning_organisation_id": owning_organisation.id,
"managing_organisation_id": managing_organisation.id,
}
end
it "creates a new case log record" do it "creates a new case log record" do
expect { expect {
post :create, params: {}, session: valid_session post :create, params: params, session: valid_session
}.to change(CaseLog, :count).by(1) }.to change(CaseLog, :count).by(1)
end end
it "redirects to that case log" do it "redirects to that case log" do
post :create, params: {}, session: valid_session post :create, params: params, session: valid_session
expect(response.status).to eq(302) expect(response.status).to eq(302)
end end
end end

2
spec/factories/case_log.rb

@ -1,5 +1,7 @@
FactoryBot.define do FactoryBot.define do
factory :case_log do factory :case_log do
owning_organisation { FactoryBot.create(:organisation) }
managing_organisation { FactoryBot.create(:organisation) }
trait :in_progress do trait :in_progress do
status { 1 } status { 1 }
tenant_code { "TH356" } tenant_code { "TH356" }

6
spec/features/form/form_navigation_spec.rb

@ -1,6 +1,7 @@
require "rails_helper" require "rails_helper"
RSpec.describe "Form Navigation" do RSpec.describe "Form Navigation" do
let(:user) { FactoryBot.create(:user) }
let(:case_log) { FactoryBot.create(:case_log, :in_progress) } let(:case_log) { FactoryBot.create(:case_log, :in_progress) }
let(:id) { case_log.id } let(:id) { case_log.id }
let(:question_answers) do let(:question_answers) do
@ -13,7 +14,10 @@ RSpec.describe "Form Navigation" do
end end
before do before do
allow_any_instance_of(CaseLogsController).to receive(:authenticate_user!).and_return(true) visit("/case_logs")
fill_in("user_email", with: user.email)
fill_in("user_password", with: user.password)
click_button("Sign in")
end end
describe "Create new log" do describe "Create new log" do

319
spec/models/case_log_spec.rb

@ -1,32 +1,89 @@
require "rails_helper" require "rails_helper"
RSpec.describe Form, type: :model do RSpec.describe Form, type: :model do
let(:owning_organisation) { FactoryBot.create(:organisation) }
let(:managing_organisation) { owning_organisation }
describe "#new" do describe "#new" do
it "validates age is a number" do it "validates age is a number" do
expect { CaseLog.create!(age1: "random") }.to raise_error(ActiveRecord::RecordInvalid) expect {
expect { CaseLog.create!(age3: "random") }.to raise_error(ActiveRecord::RecordInvalid) CaseLog.create!(
age1: "random",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(
age3: "random",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validates age is under 120" do it "validates age is under 120" do
expect { CaseLog.create!(age1: 121) }.to raise_error(ActiveRecord::RecordInvalid) expect {
expect { CaseLog.create!(age3: 121) }.to raise_error(ActiveRecord::RecordInvalid) CaseLog.create!(
age1: 121,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(
age3: 121,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validates age is over 0" do it "validates age is over 0" do
expect { CaseLog.create!(age1: 0) }.to raise_error(ActiveRecord::RecordInvalid) expect {
expect { CaseLog.create!(age3: 0) }.to raise_error(ActiveRecord::RecordInvalid) CaseLog.create!(
age1: 0,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(
age3: 0,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validates number of relets is a number" do it "validates number of relets is a number" do
expect { CaseLog.create!(offered: "random") }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
offered: "random",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validates number of relets is under 20" do it "validates number of relets is under 20" do
expect { CaseLog.create!(offered: 21) }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
offered: 21,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validates number of relets is over 0" do it "validates number of relets is over 0" do
expect { CaseLog.create!(offered: 0) }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
offered: 0,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
context "reasonable preference is yes" do context "reasonable preference is yes" do
@ -37,7 +94,9 @@ RSpec.describe Form, type: :model do
rp_insan_unsat: nil, rp_insan_unsat: nil,
rp_medwel: nil, rp_medwel: nil,
rp_hardship: nil, rp_hardship: nil,
rp_dontknow: nil) rp_dontknow: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
@ -46,6 +105,8 @@ RSpec.describe Form, type: :model do
CaseLog.create!( CaseLog.create!(
homeless: "No", homeless: "No",
reasonpref: "Yes", reasonpref: "Yes",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
) )
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
@ -54,7 +115,12 @@ RSpec.describe Form, type: :model do
context "reasonable preference is no" do context "reasonable preference is no" do
it "validates no reason is needed" do it "validates no reason is needed" do
expect { expect {
CaseLog.create!(reasonpref: "No", rp_homeless: "No") CaseLog.create!(
reasonpref: "No",
rp_homeless: "No",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.not_to raise_error }.not_to raise_error
end end
@ -63,6 +129,8 @@ RSpec.describe Form, type: :model do
CaseLog.create!( CaseLog.create!(
reasonpref: "No", reasonpref: "No",
rp_medwel: "Yes", rp_medwel: "Yes",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
) )
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
@ -72,7 +140,9 @@ RSpec.describe Form, type: :model do
it "Reason for leaving must be don't know if reason for leaving settled home (Q9a) is don't know." do it "Reason for leaving must be don't know if reason for leaving settled home (Q9a) is don't know." do
expect { expect {
CaseLog.create!(reason: "Do not know", CaseLog.create!(reason: "Do not know",
underoccupation_benefitcap: "Yes - benefit cap") underoccupation_benefitcap: "Yes - benefit cap",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
@ -80,14 +150,18 @@ RSpec.describe Form, type: :model do
it "must be provided if main reason for leaving last settled home was given as other" do it "must be provided if main reason for leaving last settled home was given as other" do
expect { expect {
CaseLog.create!(reason: "Other", CaseLog.create!(reason: "Other",
other_reason_for_leaving_last_settled_home: nil) other_reason_for_leaving_last_settled_home: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "must not be provided if the main reason for leaving settled home is not other" do it "must not be provided if the main reason for leaving settled home is not other" do
expect { expect {
CaseLog.create!(reason: "Repossession", CaseLog.create!(reason: "Repossession",
other_reason_for_leaving_last_settled_home: "the other reason provided") other_reason_for_leaving_last_settled_home: "the other reason provided",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
@ -96,14 +170,18 @@ RSpec.describe Form, type: :model do
it "must be answered if tenant was a regular or reserve in armed forces" do it "must be answered if tenant was a regular or reserve in armed forces" do
expect { expect {
CaseLog.create!(armedforces: "A current or former regular in the UK Armed Forces (exc. National Service)", CaseLog.create!(armedforces: "A current or former regular in the UK Armed Forces (exc. National Service)",
reservist: nil) reservist: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "must be answered if tenant was not a regular or reserve in armed forces" do it "must be answered if tenant was not a regular or reserve in armed forces" do
expect { expect {
CaseLog.create!(armedforces: "No", CaseLog.create!(armedforces: "No",
reservist: "Yes") reservist: "Yes",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
@ -113,7 +191,9 @@ RSpec.describe Form, type: :model do
expect { expect {
CaseLog.create!(preg_occ: "Yes", CaseLog.create!(preg_occ: "Yes",
sex1: "Male", sex1: "Male",
age1: 20) age1: 20,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
@ -121,7 +201,9 @@ RSpec.describe Form, type: :model do
expect { expect {
CaseLog.create!(preg_occ: "Yes", CaseLog.create!(preg_occ: "Yes",
sex1: "Female", sex1: "Female",
age1: 51) age1: 51,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
@ -129,7 +211,9 @@ RSpec.describe Form, type: :model do
expect { expect {
CaseLog.create!(preg_occ: "Prefer not to say", CaseLog.create!(preg_occ: "Prefer not to say",
sex1: "Male", sex1: "Male",
age1: 20) age1: 20,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
@ -137,7 +221,9 @@ RSpec.describe Form, type: :model do
expect { expect {
CaseLog.create!(preg_occ: "Yes", CaseLog.create!(preg_occ: "Yes",
sex1: "Female", sex1: "Female",
age1: 20) age1: 20,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.not_to raise_error }.not_to raise_error
end end
@ -146,7 +232,9 @@ RSpec.describe Form, type: :model do
CaseLog.create!(preg_occ: "Yes", CaseLog.create!(preg_occ: "Yes",
sex1: "Male", age1: 99, sex1: "Male", age1: 99,
sex2: "Female", sex2: "Female",
age2: 20) age2: 20,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.not_to raise_error }.not_to raise_error
end end
end end
@ -155,7 +243,9 @@ RSpec.describe Form, type: :model do
it "you must have more than zero bedrooms" do it "you must have more than zero bedrooms" do
expect { expect {
CaseLog.create!(unittype_gn: "Shared house", CaseLog.create!(unittype_gn: "Shared house",
beds: 0) beds: 0,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
@ -163,7 +253,9 @@ RSpec.describe Form, type: :model do
expect { expect {
CaseLog.create!(unittype_gn: "Shared bungalow", CaseLog.create!(unittype_gn: "Shared bungalow",
beds: 8, beds: 8,
other_hhmemb: 1) other_hhmemb: 1,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
@ -171,21 +263,27 @@ RSpec.describe Form, type: :model do
expect { expect {
CaseLog.create!(unittype_gn: "Shared bungalow", CaseLog.create!(unittype_gn: "Shared bungalow",
beds: 4, beds: 4,
other_hhmemb: 0) other_hhmemb: 0,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "A bedsit must only have one room" do it "A bedsit must only have one room" do
expect { expect {
CaseLog.create!(unittype_gn: "Bed-sit", CaseLog.create!(unittype_gn: "Bed-sit",
beds: 2) beds: 2,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "A bedsit must only have one room" do it "A bedsit must only have one room" do
expect { expect {
CaseLog.create!(unittype_gn: "Bed-sit", CaseLog.create!(unittype_gn: "Bed-sit",
beds: 0) beds: 0,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
@ -194,14 +292,18 @@ RSpec.describe Form, type: :model do
it "must be anwered if answered yes to outstanding rent or charges" do it "must be anwered if answered yes to outstanding rent or charges" do
expect { expect {
CaseLog.create!(hbrentshortfall: "Yes", CaseLog.create!(hbrentshortfall: "Yes",
tshortfall: nil) tshortfall: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "must be not be anwered if answered no to outstanding rent or charges" do it "must be not be anwered if answered no to outstanding rent or charges" do
expect { expect {
CaseLog.create!(hbrentshortfall: "No", CaseLog.create!(hbrentshortfall: "No",
tshortfall: 99) tshortfall: 99,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
@ -209,19 +311,35 @@ RSpec.describe Form, type: :model do
context "tenant’s income is from Universal Credit, state pensions or benefits" do context "tenant’s income is from Universal Credit, state pensions or benefits" do
it "Cannot be All if person 1 works full time" do it "Cannot be All if person 1 works full time" do
expect { expect {
CaseLog.create!(benefits: "All", ecstat1: "Full-time - 30 hours or more") CaseLog.create!(
benefits: "All",
ecstat1: "Full-time - 30 hours or more",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "Cannot be All if person 1 works part time" do it "Cannot be All if person 1 works part time" do
expect { expect {
CaseLog.create!(benefits: "All", ecstat1: "Part-time - Less than 30 hours") CaseLog.create!(
benefits: "All",
ecstat1: "Part-time - Less than 30 hours",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "Cannot be 1 All if any of persons 2-4 are person 1's partner and work part or full time" do it "Cannot be 1 All if any of persons 2-4 are person 1's partner and work part or full time" do
expect { expect {
CaseLog.create!(benefits: "All", relat2: "Partner", ecstat2: "Part-time - Less than 30 hours") CaseLog.create!(
benefits: "All",
relat2: "Partner",
ecstat2: "Part-time - Less than 30 hours",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
@ -230,46 +348,62 @@ RSpec.describe Form, type: :model do
it "Must not be completed if Type of main tenancy is not responded with either Secure or Assured shorthold " do it "Must not be completed if Type of main tenancy is not responded with either Secure or Assured shorthold " do
expect { expect {
CaseLog.create!(tenancy: "Other", CaseLog.create!(tenancy: "Other",
tenancylength: 10) tenancylength: 10,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "Must be completed and between 2 and 99 if type of tenancy is Assured shorthold" do it "Must be completed and between 2 and 99 if type of tenancy is Assured shorthold" do
expect { expect {
CaseLog.create!(tenancy: "Assured Shorthold", CaseLog.create!(tenancy: "Assured Shorthold",
tenancylength: 1) tenancylength: 1,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
expect { expect {
CaseLog.create!(tenancy: "Assured Shorthold", CaseLog.create!(tenancy: "Assured Shorthold",
tenancylength: nil) tenancylength: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
expect { expect {
CaseLog.create!(tenancy: "Assured Shorthold", CaseLog.create!(tenancy: "Assured Shorthold",
tenancylength: 2) tenancylength: 2,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.not_to raise_error }.not_to raise_error
end end
it "Must be empty or between 2 and 99 if type of tenancy is Secure" do it "Must be empty or between 2 and 99 if type of tenancy is Secure" do
expect { expect {
CaseLog.create!(tenancy: "Secure (including flexible)", CaseLog.create!(tenancy: "Secure (including flexible)",
tenancylength: 1) tenancylength: 1,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
expect { expect {
CaseLog.create!(tenancy: "Secure (including flexible)", CaseLog.create!(tenancy: "Secure (including flexible)",
tenancylength: 100) tenancylength: 100,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
expect { expect {
CaseLog.create!(tenancy: "Secure (including flexible)", CaseLog.create!(tenancy: "Secure (including flexible)",
tenancylength: nil) tenancylength: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.not_to raise_error }.not_to raise_error
expect { expect {
CaseLog.create!(tenancy: "Secure (including flexible)", CaseLog.create!(tenancy: "Secure (including flexible)",
tenancylength: 2) tenancylength: 2,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.not_to raise_error }.not_to raise_error
end end
end end
@ -278,14 +412,18 @@ RSpec.describe Form, type: :model do
it "must be answered if ever served in the forces as a regular" do it "must be answered if ever served in the forces as a regular" do
expect { expect {
CaseLog.create!(armedforces: "A current or former regular in the UK Armed Forces (exc. National Service)", CaseLog.create!(armedforces: "A current or former regular in the UK Armed Forces (exc. National Service)",
leftreg: nil) leftreg: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "must not be answered if not ever served as a regular" do it "must not be answered if not ever served as a regular" do
expect { expect {
CaseLog.create!(armedforces: "No", CaseLog.create!(armedforces: "No",
leftreg: "Yes") leftreg: "Yes",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
@ -294,42 +432,103 @@ RSpec.describe Form, type: :model do
expect do expect do
CaseLog.create!(armedforces: "A current or former regular in the UK Armed Forces (exc. National Service)", CaseLog.create!(armedforces: "A current or former regular in the UK Armed Forces (exc. National Service)",
leftreg: "Yes", leftreg: "Yes",
reservist: "Yes") reservist: "Yes",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
end end
end end
end end
context "household_member_validations" do context "household_member_validations" do
it "validate that persons aged under 16 must have relationship Child" do it "validate that persons aged under 16 must have relationship Child" do
expect { CaseLog.create!(age2: 14, relat2: "Partner") }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
age2: 14,
relat2: "Partner",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validate that persons aged over 70 must be retired" do it "validate that persons aged over 70 must be retired" do
expect { CaseLog.create!(age2: 71, ecstat2: "Full-time - 30 hours or more") }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
age2: 71,
ecstat2: "Full-time - 30 hours or more",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validate that a male, retired persons must be over 65" do it "validate that a male, retired persons must be over 65" do
expect { CaseLog.create!(age2: 64, sex2: "Male", ecstat2: "Retired") }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
age2: 64,
sex2: "Male",
ecstat2: "Retired",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validate that a female, retired persons must be over 60" do it "validate that a female, retired persons must be over 60" do
expect { CaseLog.create!(age2: 59, sex2: "Female", ecstat2: "Retired") }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
age2: 59,
sex2: "Female",
ecstat2: "Retired",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validate that persons aged under 16 must be a child (economically speaking)" do it "validate that persons aged under 16 must be a child (economically speaking)" do
expect { CaseLog.create!(age2: 15, ecstat2: "Full-time - 30 hours or more") }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
age2: 15,
ecstat2: "Full-time - 30 hours or more",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validate that persons aged between 16 and 19 that are a child must be a full time student or economic status refused" do it "validate that persons aged between 16 and 19 that are a child must be a full time student or economic status refused" do
expect { CaseLog.create!(age2: 17, relat2: "Child - includes young adult and grown-up", ecstat2: "Full-time - 30 hours or more") }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
age2: 17,
relat2: "Child - includes young adult and grown-up",
ecstat2: "Full-time - 30 hours or more",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validate that persons aged under 16 must be a child relationship" do it "validate that persons aged under 16 must be a child relationship" do
expect { CaseLog.create!(age2: 15, relat2: "Partner") }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
age2: 15,
relat2: "Partner",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validate that no more than 1 partner relationship exists" do it "validate that no more than 1 partner relationship exists" do
expect { CaseLog.create!(relat2: "Partner", relat3: "Partner") }.to raise_error(ActiveRecord::RecordInvalid) expect {
CaseLog.create!(
relat2: "Partner",
relat3: "Partner",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
@ -337,24 +536,32 @@ RSpec.describe Form, type: :model do
it "must be provided if tenancy type was given as other" do it "must be provided if tenancy type was given as other" do
expect { expect {
CaseLog.create!(tenancy: "Other", CaseLog.create!(tenancy: "Other",
tenancyother: nil) tenancyother: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
expect { expect {
CaseLog.create!(tenancy: "Other", CaseLog.create!(tenancy: "Other",
tenancyother: "type") tenancyother: "type",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.not_to raise_error }.not_to raise_error
end end
it "must not be provided if tenancy type is not other" do it "must not be provided if tenancy type is not other" do
expect { expect {
CaseLog.create!(tenancy: "Secure (including flexible)", CaseLog.create!(tenancy: "Secure (including flexible)",
tenancyother: "the other reason provided") tenancyother: "the other reason provided",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
expect { expect {
CaseLog.create!(tenancy: "Secure (including flexible)", CaseLog.create!(tenancy: "Secure (including flexible)",
tenancyother: nil) tenancyother: nil,
owning_organisation: owning_organisation,
managing_organisation: managing_organisation)
}.not_to raise_error }.not_to raise_error
end end
end end
@ -366,6 +573,8 @@ RSpec.describe Form, type: :model do
ecstat1: "Full-time - 30 hours or more", ecstat1: "Full-time - 30 hours or more",
earnings: 5000, earnings: 5000,
incfreq: "Weekly", incfreq: "Weekly",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
) )
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
@ -376,6 +585,8 @@ RSpec.describe Form, type: :model do
ecstat1: "Full-time - 30 hours or more", ecstat1: "Full-time - 30 hours or more",
earnings: 1, earnings: 1,
incfreq: "Weekly", incfreq: "Weekly",
owning_organisation: owning_organisation,
managing_organisation: managing_organisation,
) )
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end

4
spec/requests/bulk_upload_controller_spec.rb

@ -2,6 +2,10 @@ require "rails_helper"
RSpec.describe BulkUploadController, type: :request do RSpec.describe BulkUploadController, type: :request do
let(:url) { "/case_logs/bulk_upload" } let(:url) { "/case_logs/bulk_upload" }
let(:organisation) { FactoryBot.create(:organisation) }
before do
allow(Organisation).to receive(:find).with(107_242).and_return(organisation)
end
describe "GET #show" do describe "GET #show" do
before do before do

15
spec/requests/case_log_controller_spec.rb

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe CaseLogsController, type: :request do RSpec.describe CaseLogsController, type: :request do
let(:owning_organisation) { FactoryBot.create(:organisation) }
let(:managing_organisation) { owning_organisation }
let(:api_username) { "test_user" } let(:api_username) { "test_user" }
let(:api_password) { "test_password" } let(:api_password) { "test_password" }
let(:basic_credentials) do let(:basic_credentials) do
@ -32,6 +34,8 @@ RSpec.describe CaseLogsController, type: :request do
let(:params) do let(:params) do
{ {
"owning_organisation_id": owning_organisation.id,
"managing_organisation_id": managing_organisation.id,
"tenant_code": tenant_code, "tenant_code": tenant_code,
"age1": age1, "age1": age1,
"property_postcode": property_postcode, "property_postcode": property_postcode,
@ -78,8 +82,17 @@ RSpec.describe CaseLogsController, type: :request do
end end
context "complete case log submission" do context "complete case log submission" do
let(:org_params) do
{
"case_log" => {
"owning_organisation_id" => owning_organisation.id,
"managing_organisation_id" => managing_organisation.id,
},
}
end
let(:case_log_params) { JSON.parse(File.open("spec/fixtures/complete_case_log.json").read) }
let(:params) do let(:params) do
JSON.parse(File.open("spec/fixtures/complete_case_log.json").read) case_log_params.merge(org_params) { |_k, a_val, b_val| a_val.merge(b_val) }
end end
it "marks the record as completed" do it "marks the record as completed" do

Loading…
Cancel
Save