Browse Source

Merge branch 'main' into CLDC-477/reason-for-leaving-because-of-the-benefit-cap

pull/59/head
Matthew Phelan 3 years ago
parent
commit
b430ad0cb5
  1. 10
      Gemfile.lock
  2. 6
      app/controllers/case_logs_controller.rb
  3. 20
      app/models/case_log.rb
  4. 21
      docs/api/DLUHC-CORE-Data.v1.json
  5. 2
      spec/helpers/check_answers_helper_spec.rb
  6. 18
      spec/models/case_log_spec.rb
  7. 4
      spec/requests/case_log_controller_spec.rb

10
Gemfile.lock

@ -1,6 +1,6 @@
GIT GIT
remote: https://github.com/rspec/rspec-core.git remote: https://github.com/rspec/rspec-core.git
revision: 053fcfeb6b0b6627edf7261737553a6f7df8cc14 revision: d57c371ee92b16211b80ac7b0b025968438f5297
branch: main branch: main
specs: specs:
rspec-core (3.11.0.pre) rspec-core (3.11.0.pre)
@ -198,7 +198,7 @@ GEM
public_suffix (4.0.6) public_suffix (4.0.6)
puma (5.5.2) puma (5.5.2)
nio4r (~> 2.0) nio4r (~> 2.0)
racc (1.5.2) racc (1.6.0)
rack (2.2.3) rack (2.2.3)
rack-mini-profiler (2.3.3) rack-mini-profiler (2.3.3)
rack (>= 1.2.0) rack (>= 1.2.0)
@ -279,7 +279,7 @@ GEM
sass (~> 3.5, >= 3.5.5) sass (~> 3.5, >= 3.5.5)
scss_lint-govuk (0.2.0) scss_lint-govuk (0.2.0)
scss_lint scss_lint
selenium-webdriver (4.0.0) selenium-webdriver (4.0.3)
childprocess (>= 0.5, < 5.0) childprocess (>= 0.5, < 5.0)
rexml (~> 3.2, >= 3.2.5) rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2) rubyzip (>= 1.2.2)
@ -300,7 +300,7 @@ GEM
stimulus-rails (0.7.1) stimulus-rails (0.7.1)
rails (>= 6.0.0) rails (>= 6.0.0)
thor (1.1.0) thor (1.1.0)
turbo-rails (7.1.0) turbo-rails (7.1.1)
rails (>= 6.0.0) rails (>= 6.0.0)
tzinfo (2.0.4) tzinfo (2.0.4)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
@ -323,7 +323,7 @@ GEM
websocket-extensions (0.1.5) websocket-extensions (0.1.5)
xpath (3.2.0) xpath (3.2.0)
nokogiri (~> 1.8) nokogiri (~> 1.8)
zeitwerk (2.4.2) zeitwerk (2.5.1)
PLATFORMS PLATFORMS
x86_64-darwin-19 x86_64-darwin-19

6
app/controllers/case_logs_controller.rb

@ -15,7 +15,7 @@ class CaseLogsController < ApplicationController
if case_log.persisted? if case_log.persisted?
render json: case_log, status: :created render json: case_log, status: :created
else else
render json: { errors: case_log.errors.full_messages }, status: :unprocessable_entity render json: { errors: case_log.errors.messages }, status: :unprocessable_entity
end end
end end
end end
@ -26,7 +26,7 @@ class CaseLogsController < ApplicationController
if case_log.update(api_case_log_params) if case_log.update(api_case_log_params)
render json: case_log, status: :ok render json: case_log, status: :ok
else else
render json: { errors: case_log.errors.full_messages }, status: :unprocessable_entity render json: { errors: case_log.errors.messages }, status: :unprocessable_entity
end end
else else
render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found
@ -74,7 +74,7 @@ class CaseLogsController < ApplicationController
if case_log.discard if case_log.discard
head :no_content head :no_content
else else
render json: { errors: case_log.errors.full_messages }, status: :unprocessable_entity render json: { errors: case_log.errors.messages }, status: :unprocessable_entity
end end
else else
render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found render json: { error: "Case Log #{params[:id]} not found" }, status: :not_found

20
app/models/case_log.rb

@ -5,26 +5,26 @@ class CaseLogValidator < ActiveModel::Validator
def validate_tenant_age(record) def validate_tenant_age(record)
if record.tenant_age && !/^[1-9][0-9]?$|^120$/.match?(record.tenant_age.to_s) if record.tenant_age && !/^[1-9][0-9]?$|^120$/.match?(record.tenant_age.to_s)
record.errors.add :tenant_age, "must be between 0 and 120" record.errors.add :tenant_age, "Tenant age must be between 0 and 120"
end end
end end
def validate_property_number_of_times_relet(record) def validate_property_number_of_times_relet(record)
if record.property_number_of_times_relet && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.property_number_of_times_relet.to_s) if record.property_number_of_times_relet && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.property_number_of_times_relet.to_s)
record.errors.add :property_number_of_times_relet, "must be between 0 and 20" record.errors.add :property_number_of_times_relet, "Must be between 0 and 20"
end end
end end
def validate_reasonable_preference(record) def validate_reasonable_preference(record)
if record.homelessness == "No" && record.reasonable_preference == "Yes" if record.homelessness == "No" && record.reasonable_preference == "Yes"
record.errors.add :reasonable_preference, "can not be Yes if Not Homeless immediately prior to this letting has been selected" record.errors.add :reasonable_preference, "Can not be Yes if Not Homeless immediately prior to this letting has been selected"
elsif record.reasonable_preference == "Yes" elsif record.reasonable_preference == "Yes"
if !record.reasonable_preference_reason_homeless && !record.reasonable_preference_reason_unsatisfactory_housing && !record.reasonable_preference_reason_medical_grounds && !record.reasonable_preference_reason_avoid_hardship && !record.reasonable_preference_reason_do_not_know if !record.reasonable_preference_reason_homeless && !record.reasonable_preference_reason_unsatisfactory_housing && !record.reasonable_preference_reason_medical_grounds && !record.reasonable_preference_reason_avoid_hardship && !record.reasonable_preference_reason_do_not_know
record.errors.add :reasonable_preference_reason, "- if reasonable preference is Yes, a reason must be given" record.errors.add :reasonable_preference_reason, "If reasonable preference is Yes, a reason must be given"
end end
elsif record.reasonable_preference == "No" elsif record.reasonable_preference == "No"
if record.reasonable_preference_reason_homeless || record.reasonable_preference_reason_unsatisfactory_housing || record.reasonable_preference_reason_medical_grounds || record.reasonable_preference_reason_avoid_hardship || record.reasonable_preference_reason_do_not_know if record.reasonable_preference_reason_homeless || record.reasonable_preference_reason_unsatisfactory_housing || record.reasonable_preference_reason_medical_grounds || record.reasonable_preference_reason_avoid_hardship || record.reasonable_preference_reason_do_not_know
record.errors.add :reasonable_preference_reason, "- if reasonable preference is No, no reasons should be given" record.errors.add :reasonable_preference_reason, "If reasonable preference is No, no reasons should be given"
end end
end end
end end
@ -45,6 +45,16 @@ class CaseLogValidator < ActiveModel::Validator
end end
end end
def validate_armed_forces_injured(record)
if (record.armed_forces == "Yes - a regular" || record.armed_forces == "Yes - a reserve") && record.armed_forces_injured.blank?
record.errors.add :armed_forces_injured, "You must answer the armed forces injury question if the tenant has served in the armed forces"
end
if (record.armed_forces == "No" || record.armed_forces == "Prefer not to say") && record.armed_forces_injured.present?
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
def validate(record) def validate(record)
# If we've come from the form UI we only want to validate the specific fields # 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 # that have just been submitted. If we're submitting a log via API or Bulk Upload

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

@ -108,9 +108,11 @@
"examples": { "examples": {
"Invalid Age": { "Invalid Age": {
"value": { "value": {
"errors": [ "errors": {
"Tenant age must be between 0 and 120" "tenant_age": [
] "Tenant age must be between 0 and 120"
]
}
} }
} }
} }
@ -211,11 +213,16 @@
"properties": {} "properties": {}
}, },
"examples": { "examples": {
"Invalid Age": { "Invalid Age & Reasonable Preference": {
"value": { "value": {
"errors": [ "errors": {
"Tenant age must be between 0 and 120" "reasonable_preference_reason": [
] "If reasonable preference is Yes, a reason must be given"
],
"tenant_age": [
"Tenant age must be between 0 and 120"
]
}
} }
} }
} }

2
spec/helpers/check_answers_helper_spec.rb

@ -11,7 +11,7 @@ RSpec.describe CheckAnswersHelper do
) )
end end
let(:case_log_with_met_radio_condition) do let(:case_log_with_met_radio_condition) do
FactoryBot.create(:case_log, armed_forces: "Yes - a regular") FactoryBot.create(:case_log, armed_forces: "Yes - a regular", armed_forces_injured: "No")
end end
let(:subsection) { "income_and_benefits" } let(:subsection) { "income_and_benefits" }
let(:subsection_with_numeric_conditionals) { "household_characteristics" } let(:subsection_with_numeric_conditionals) { "household_characteristics" }

18
spec/models/case_log_spec.rb

@ -26,7 +26,7 @@ RSpec.describe Form, type: :model do
expect { CaseLog.create!(property_number_of_times_relet: 0) }.to raise_error(ActiveRecord::RecordInvalid) expect { CaseLog.create!(property_number_of_times_relet: 0) }.to raise_error(ActiveRecord::RecordInvalid)
end end
describe "reasonable preference validation" do context "reasonable preference validation" do
it "if given reasonable preference is yes a reason must be selected" do it "if given reasonable preference is yes a reason must be selected" do
expect { expect {
CaseLog.create!(reasonable_preference: "Yes", CaseLog.create!(reasonable_preference: "Yes",
@ -80,6 +80,22 @@ RSpec.describe Form, type: :model do
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
context "armed forces injured validation" do
it "must be anwered 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
expect {
CaseLog.create!(armed_forces: "No",
armed_forces_injured: "Yes")
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
end end
describe "status" do describe "status" do

4
spec/requests/case_log_controller_spec.rb

@ -66,7 +66,7 @@ RSpec.describe CaseLogsController, type: :request do
it "validates case log parameters" do it "validates case log parameters" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
expect(json_response["errors"]).to match_array(["Tenant age must be between 0 and 120", "Property number of times relet must be between 0 and 20"]) expect(json_response["errors"]).to match_array([["property_number_of_times_relet", ["Must be between 0 and 20"]], ["tenant_age", ["Tenant age must be between 0 and 120"]]])
end end
end end
@ -157,7 +157,7 @@ RSpec.describe CaseLogsController, type: :request do
it "returns an error message" do it "returns an error message" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response["errors"]).to eq(["Tenant age must be between 0 and 120"]) expect(json_response["errors"]).to eq({ "tenant_age" => ["Tenant age must be between 0 and 120"] })
end end
end end

Loading…
Cancel
Save