Browse Source

Merge branch 'main' into CLDC-469-if-served-did-leave-validation

pull/64/head
Milo 3 years ago committed by GitHub
parent
commit
de4d58b711
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      Gemfile
  2. 15
      Gemfile.lock
  3. 31
      app/models/case_log.rb
  4. 2
      config/forms/2021_2022.json
  5. 7
      db/migrate/20211027091521_rename_person1_fields.rb
  6. 4
      db/schema.rb
  7. 19
      docs/api/DLUHC-CORE-Data.v1.json
  8. 4
      spec/fixtures/complete_case_log.json
  9. 73
      spec/models/case_log_spec.rb
  10. 44
      spec/rails_helper.rb

18
Gemfile

@ -29,15 +29,8 @@ gem "discard"
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem "byebug", platforms: %i[mri mingw x64_mingw]
gem "capybara", require: false
gem "dotenv-rails"
gem "factory_bot_rails"
gem "pry-byebug"
gem "selenium-webdriver", require: false
gem "simplecov", require: false
%w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
gem lib, git: "https://github.com/rspec/#{lib}.git", branch: "main", require: false
end
end
group :development do
@ -54,5 +47,16 @@ group :development do
gem "scss_lint-govuk"
end
group :test do
gem "capybara", require: false
gem "factory_bot_rails"
gem "selenium-webdriver", require: false
gem "simplecov", require: false
gem "database_cleaner-active_record", require: false
%w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
gem lib, git: "https://github.com/rspec/#{lib}.git", branch: "main", require: false
end
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[mingw mswin x64_mingw jruby]

15
Gemfile.lock

@ -26,7 +26,7 @@ GIT
GIT
remote: https://github.com/rspec/rspec-rails.git
revision: 3f0e35085f5765decf96fee179ec9a2e132a67c1
revision: cfe4db707cc5a0c9437aa90e3059256f30368da4
branch: main
specs:
rspec-rails (5.1.0.pre)
@ -40,7 +40,7 @@ GIT
GIT
remote: https://github.com/rspec/rspec-support.git
revision: 28526172c42302858c18681d7d7580490d885b4d
revision: 2a17194b9fc868a4b4a41d7168103dca825c3004
branch: main
specs:
rspec-support (3.11.0.pre)
@ -115,8 +115,9 @@ GEM
msgpack (~> 1.0)
builder (3.2.4)
byebug (11.1.3)
capybara (3.35.3)
capybara (3.36.0)
addressable
matrix
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
@ -127,6 +128,10 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.1.9)
crass (1.0.6)
database_cleaner-active_record (2.0.1)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
deep_merge (1.2.1)
diff-lcs (1.4.4)
discard (1.2.0)
@ -145,7 +150,7 @@ GEM
ffi (1.15.4)
globalid (0.5.2)
activesupport (>= 5.0)
govuk-components (2.1.3)
govuk-components (2.1.4)
activemodel (>= 6.0)
railties (>= 6.0)
view_component (~> 2.39.0)
@ -172,6 +177,7 @@ GEM
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mini_mime (1.1.2)
minitest (5.14.4)
@ -334,6 +340,7 @@ DEPENDENCIES
bootsnap (>= 1.4.4)
byebug
capybara
database_cleaner-active_record
discard
dotenv-rails
factory_bot_rails

31
app/models/case_log.rb

@ -54,6 +54,19 @@ class CaseLogValidator < ActiveModel::Validator
record.errors.add :armed_forces_injured, "You must not answer the armed forces injury question if the tenant has not served in the armed forces or prefer not to say was chosen"
end
end
EMPLOYED_STATUSES = ["Full-time - 30 hours or more", "Part-time - Less than 30 hours"].freeze
def validate_net_income_uc_proportion(record)
(1..8).any? do |n|
economic_status = record["person_#{n}_economic_status"]
is_employed = EMPLOYED_STATUSES.include?(economic_status)
relationship = record["person_#{n}_relationship"]
is_partner_or_main = relationship == "Partner" || (relationship.nil? && economic_status.present?)
if is_employed && is_partner_or_main && record.net_income_uc_proportion == "All"
record.errors.add :net_income_uc_proportion, "income is from Universal Credit, state pensions or benefits cannot be All if the tenant or the partner works part or full time"
end
end
end
def validate_armed_forces_active_response(record)
if record.armed_forces == "Yes - a regular" && record.armed_forces_active.blank?
@ -71,6 +84,20 @@ class CaseLogValidator < ActiveModel::Validator
end
end
def validate_fixed_term_tenancy(record)
is_present = record.fixed_term_tenancy.present?
is_in_range = record.fixed_term_tenancy.to_i.between?(2, 99)
is_secure = record.tenancy_type == "Fixed term – Secure"
is_ast = record.tenancy_type == "Fixed term – Assured Shorthold Tenancy (AST)"
conditions = [
{ condition: !(is_secure || is_ast) && is_present, error: "You must only answer the fixed term tenancy length question if the tenancy type is fixed term" },
{ condition: is_ast && !is_in_range, error: "Fixed term – Assured Shorthold Tenancy (AST) should be between 2 and 99 years" },
{ condition: is_secure && (!is_in_range && is_present), error: "Fixed term – Secure should be between 2 and 99 years or not specified" },
]
conditions.each { |condition| condition[:condition] ? (record.errors.add :fixed_term_tenancy, condition[:error]) : nil }
end
def validate(record)
# If we've come from the form UI we only want to validate the specific fields
# that have just been submitted. If we're submitting a log via API or Bulk Upload
@ -169,6 +196,10 @@ private
dynamically_not_required << "net_income_frequency"
end
if tenancy_type == "Fixed term – Secure"
dynamically_not_required << "fixed_term_tenancy"
end
required.delete_if { |key, _value| dynamically_not_required.include?(key) }
end
end

2
config/forms/2021_2022.json

@ -121,7 +121,7 @@
"header": "",
"description": "",
"questions": {
"tenant_economic_status": {
"person_1_economic_status": {
"check_answer_label": "Work",
"header": "Which of these best describes the tenant's working situation?",
"hint_text": "",

7
db/migrate/20211027091521_rename_person1_fields.rb

@ -0,0 +1,7 @@
class RenamePerson1Fields < ActiveRecord::Migration[6.1]
def change
change_table :case_logs, bulk: true do |t|
t.rename :tenant_economic_status, :person_1_economic_status
end
end
end

4
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_10_26_123542) do
ActiveRecord::Schema.define(version: 2021_10_27_091521) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -26,7 +26,7 @@ ActiveRecord::Schema.define(version: 2021_10_26_123542) do
t.string "tenant_nationality"
t.string "previous_housing_situation"
t.string "armed_forces"
t.string "tenant_economic_status"
t.string "person_1_economic_status"
t.integer "household_number_of_other_members"
t.string "person_2_relationship"
t.integer "person_2_age"

19
docs/api/DLUHC-CORE-Data.v1.json

@ -274,7 +274,7 @@
"tenant_nationality": "UK national resident in UK",
"previous_housing_situation": "Private sector tenancy",
"armed_forces": "Yes - a regular",
"tenant_economic_status": "Full-time - 30 hours or more",
"person_1_economic_status": "Full-time - 30 hours or more",
"household_number_of_other_members": 7,
"person_2_relationship": "Partner",
"person_2_age": 32,
@ -459,7 +459,7 @@
"type": "string",
"minLength": 1
},
"tenant_economic_status": {
"person_1_economic_status": {
"type": "string",
"minLength": 1,
"enum": [
@ -873,7 +873,8 @@
},
"fixed_term_tenancy": {
"type": "string",
"minLength": 1
"minLength": 1,
"pattern": "((?!1|0)([0-9][0-9]))+"
},
"tenancy_type": {
"type": "string",
@ -945,7 +946,13 @@
},
"net_income_uc_proportion": {
"type": "string",
"minLength": 1
"minLength": 1,
"enum": [
"All",
"Some",
"None",
"Do not know"
]
},
"housing_benefit": {
"type": "string",
@ -1092,7 +1099,7 @@
"tenant_nationality",
"previous_housing_situation",
"armed_forces",
"tenant_economic_status",
"person_1_economic_status",
"household_number_of_other_members",
"person_2_relationship",
"person_2_age",
@ -1202,4 +1209,4 @@
},
"securitySchemes": {}
}
}
}

4
spec/fixtures/complete_case_log.json vendored

@ -8,7 +8,7 @@
"tenant_nationality": "UK national resident in UK",
"previous_housing_situation": "Private sector tenancy",
"armed_forces": "Yes - a regular",
"tenant_economic_status": "Full-time - 30 hours or more",
"person_1_economic_status": "Full-time - 30 hours or more",
"household_number_of_other_members": 7,
"person_2_relationship": "Partner",
"person_2_age": 32,
@ -51,7 +51,7 @@
"tenancy_code": "BZ757",
"tenancy_start_date": "12/03/2019",
"starter_tenancy": "No",
"fixed_term_tenancy": "No",
"fixed_term_tenancy": "5",
"tenancy_type": "Fixed term – Secure",
"letting_type": "Affordable Rent - General Needs",
"letting_provider": "This landlord",

73
spec/models/case_log_spec.rb

@ -82,21 +82,88 @@ RSpec.describe Form, type: :model do
end
context "armed forces injured validation" do
it "must be anwered 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 {
CaseLog.create!(armed_forces: "Yes - a regular",
armed_forces_injured: nil)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "must be anwered 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 {
CaseLog.create!(armed_forces: "No",
armed_forces_injured: "Yes")
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "tenant’s income is from Universal Credit, state pensions or benefits" do
it "Cannot be All if person 1 works full time" do
expect {
CaseLog.create!(net_income_uc_proportion: "All", person_1_economic_status: "Full-time - 30 hours or more")
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Cannot be All if person 1 works part time" do
expect {
CaseLog.create!(net_income_uc_proportion: "All", person_1_economic_status: "Part-time - Less than 30 hours")
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Cannot be 1 All if any of persons 2-4 are person 1's partner and work part or full time" do
expect {
CaseLog.create!(net_income_uc_proportion: "All", person_2_relationship: "Partner", person_2_economic_status: "Part-time - Less than 30 hours")
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "fixed term tenancy length" do
it "Must not be completed if Type of main tenancy is not responded with either Secure or Assured shorthold " do
expect {
CaseLog.create!(tenancy_type: "Other",
fixed_term_tenancy: 10)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Must be completed and between 2 and 99 if type of tenancy is Assured shorthold" do
expect {
CaseLog.create!(tenancy_type: "Fixed term – Assured Shorthold Tenancy (AST)",
fixed_term_tenancy: 1)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Assured Shorthold Tenancy (AST)",
fixed_term_tenancy: nil)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Assured Shorthold Tenancy (AST)",
fixed_term_tenancy: 2)
}.not_to raise_error
end
it "Must be empty or between 2 and 99 if type of tenancy is Secure" do
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: 1)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: 100)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: nil)
}.not_to raise_error
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: 2)
}.not_to raise_error
end
end
context "armed forces active validation" do
it "must be answered if ever served in the forces as a regular" do
expect {
@ -118,7 +185,7 @@ RSpec.describe Form, type: :model do
CaseLog.create!(armed_forces: "Yes - a regular",
armed_forces_active: "Yes",
armed_forces_injured: "Yes")
}.not_to raise_error
}.not_to raise_error
end
end
end

44
spec/rails_helper.rb

@ -6,6 +6,7 @@ require File.expand_path("../config/environment", __dir__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require "rspec/rails"
require "capybara/rspec"
require 'database_cleaner/active_record'
# Comment to run `js: true specs` with visible browser interaction
Capybara.javascript_driver = :selenium_headless
@ -42,7 +43,48 @@ RSpec.configure do |config|
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# config.use_transactional_fixtures = true
config.before(:suite) do
if config.use_transactional_fixtures?
raise(<<-MSG)
Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
(or set it to false) to prevent uncommitted transactions being used in
JavaScript-dependent specs.
During testing, the app-under-test that the browser driver connects to
uses a different database connection to the database connection used by
the spec. The app's database connection would not be able to access
uncommitted transaction data setup over the spec's database connection.
MSG
end
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, type: :feature) do
# :rack_test driver's Rack app under test shares database connection
# with the specs, so continue to use transaction strategy for speed.
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
unless driver_shares_db_connection_with_specs
# Driver is probably for an external browser with an app
# under test that does *not* share a database connection with the
# specs, so use truncation strategy.
DatabaseCleaner.strategy = :truncation
end
end
config.before(:each) do
DatabaseCleaner.start
end
config.append_after(:each) do
DatabaseCleaner.clean
end
# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false

Loading…
Cancel
Save