Skip to content

Commit

Permalink
[10-10CG] Add ability to not call fully_validate_schema in SavedClaim (
Browse files Browse the repository at this point in the history
…#19468)

* add new flipper toggle

* add logic to skip fully_validate_schema when toggle is turned on

* add flipper toggle expect to spec since it is calling the valid? on a saved_claim

* update specs to handle extra flipper call in stubs by calling original be default

* stub flipper enabled call to call original for some specs

* add codeowner for spec file
  • Loading branch information
coope93 authored Nov 18, 2024
1 parent 77816f2 commit 729b8ac
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 47 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ spec/controllers/v0/benefits_reference_data_controller_spec.rb @department-of-ve
spec/controllers/v0/burial_claims_controller_spec.rb @department-of-veterans-affairs/mbs-core-team @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
spec/controllers/v0/caregivers_assistance_claims_controller_spec.rb @department-of-veterans-affairs/vfs-10-10
spec/controllers/v0/claim_letters_controller_spec.rb @department-of-veterans-affairs/benefits-management-tools-be @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
spec/controllers/v0/contact_us @department-of-veterans-affairs/ask-va-team @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
spec/controllers/v0/debt_letters_controller_spec.rb @department-of-veterans-affairs/vsa-debt-resolution @department-of-veterans-affairs/backend-review-group
spec/controllers/v0/dependents_applications_controller_spec.rb @department-of-veterans-affairs/vfs-authenticated-experience-backend @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
spec/controllers/v0/dependents_verifications_controller_spec.rb @department-of-veterans-affairs/benefits-dependents-management @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
Expand Down
12 changes: 7 additions & 5 deletions app/models/saved_claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ def form_matches_schema
return unless form_is_string

schema = VetsJsonSchema::SCHEMAS[self.class::FORM]
clear_cache = false

schema_errors = validate_schema(schema)
unless Flipper.enabled?(:saved_claim_schema_validation_disable)
schema_errors = validate_schema(schema)

clear_cache = false
unless schema_errors.empty?
Rails.logger.error('SavedClaim schema failed validation! Attempting to clear cache.', { errors: schema_errors })
clear_cache = true
unless schema_errors.empty?
Rails.logger.error('SavedClaim schema failed validation! Attempting to clear cache.', { errors: schema_errors })
clear_cache = true
end
end

validation_errors = validate_form(schema, clear_cache)
Expand Down
3 changes: 3 additions & 0 deletions config/features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,9 @@ features:
actor_type: user
description: When enabled, the rated disabilities application uses Lighthouse instead of EVSS
enable_in_development: true
saved_claim_schema_validation_disable:
actor_type: user
description: Disables validating a saved_claim schema before validating the form data with the schema.
schema_contract_appointments_index:
actor_type: user
description: Enables schema validation for the appointments service index fetch.
Expand Down
4 changes: 4 additions & 0 deletions spec/controllers/v0/contact_us/inquiries_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def send_create
post(:create, params: { ask: { form: } })
end

before do
allow(Flipper).to receive(:enabled?).and_call_original
end

context 'when Flipper :get_help_ask_form is' do
context 'disabled' do
it 'renders :not_implemented' do
Expand Down
8 changes: 6 additions & 2 deletions spec/lib/bgs/form674_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
let(:user_struct) { FactoryBot.build(:user_struct) }
let(:saved_claim) { create(:dependency_claim_no_vet_information) }

before do
allow(Flipper).to receive(:enabled?).and_call_original
end

context 'The flipper is turned on' do
before do
Flipper.enable(:dependents_enqueue_with_user_struct)
allow(Flipper).to receive(:enabled?).with(:dependents_enqueue_with_user_struct).and_return(true)
end

# @TODO: may want to return something else
Expand Down Expand Up @@ -82,7 +86,7 @@

context 'The flipper is turned off' do
before do
Flipper.disable(:dependents_enqueue_with_user_struct)
allow(Flipper).to receive(:enabled?).with(:dependents_enqueue_with_user_struct).and_return(false)
end

# @TODO: may want to return something else
Expand Down
4 changes: 4 additions & 0 deletions spec/lib/bgs/form686c_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
let(:user_struct) { FactoryBot.build(:user_struct) }
let(:saved_claim) { create(:dependency_claim_no_vet_information) }

before do
allow(Flipper).to receive(:enabled?).and_call_original
end

describe '#submit' do
subject { form686c.submit(payload) }

Expand Down
4 changes: 4 additions & 0 deletions spec/models/saved_claim/education_benefits/va10203_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
let(:user) { create(:user) }
let!(:user_verification) { create(:idme_user_verification, idme_uuid: user.idme_uuid) }

before do
allow(Flipper).to receive(:enabled?).and_call_original
end

it_behaves_like 'saved_claim'

validate_inclusion(:form_id, '22-10203')
Expand Down
132 changes: 92 additions & 40 deletions spec/models/saved_claim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,69 +44,121 @@ def attachment_keys
end

context 'validation errors' do
let(:schema_errors) { [{ fragment: 'error' }] }
context 'saved_claim_schema_validation_disable disabled' do
let(:schema_errors) { [{ fragment: 'error' }] }

context 'when fully_validate_schema returns errors' do
before do
allow(JSON::Validator).to receive_messages(fully_validate_schema: schema_errors, fully_validate: [])
allow(Flipper).to receive(:enabled?).with(:saved_claim_schema_validation_disable).and_return(false)
end

it 'logs schema failed error and calls fully_validate' do
expect(Rails.logger).to receive(:error)
.with('SavedClaim schema failed validation! Attempting to clear cache.', { errors: schema_errors })
context 'when fully_validate_schema returns errors' do
before do
allow(JSON::Validator).to receive_messages(fully_validate_schema: schema_errors, fully_validate: [])
end

expect(saved_claim.validate).to eq true
end
end
it 'logs schema failed error and calls fully_validate' do
expect(Rails.logger).to receive(:error)
.with('SavedClaim schema failed validation! Attempting to clear cache.', { errors: schema_errors })

context 'when fully_validate returns errors' do
before do
allow(JSON::Validator).to receive(:fully_validate).and_return(schema_errors)
expect(saved_claim.validate).to eq true
end
end

it 'adds validation errors to the form' do
saved_claim.validate
expect(saved_claim.errors.full_messages).not_to be_empty
context 'when fully_validate returns errors' do
before do
allow(JSON::Validator).to receive(:fully_validate).and_return(schema_errors)
end

it 'adds validation errors to the form' do
saved_claim.validate
expect(saved_claim.errors.full_messages).not_to be_empty
end
end
end

context 'when JSON:Validator.fully_validate_schema throws an exception' do
let(:exception) { StandardError.new('Some exception') }
context 'when JSON:Validator.fully_validate_schema throws an exception' do
let(:exception) { StandardError.new('Some exception') }

before do
allow(JSON::Validator).to receive(:fully_validate_schema).and_raise(exception)
allow(JSON::Validator).to receive(:fully_validate).and_return([])
before do
allow(JSON::Validator).to receive(:fully_validate_schema).and_raise(exception)
allow(JSON::Validator).to receive(:fully_validate).and_return([])
end

it 'logs exception and raises exception' do
expect(Rails.logger).to receive(:error)
.with('Error during schema validation!', { error: exception.message, backtrace: anything, schema: })

expect { saved_claim.validate }.to raise_error(exception.class, exception.message)
end
end

it 'logs exception and raises exception' do
expect(Rails.logger).to receive(:error)
.with('Error during schema validation!', { error: exception.message, backtrace: anything, schema: })
context 'when JSON:Validator.fully_validate throws an exception' do
let(:exception) { StandardError.new('Some exception') }

before do
allow(JSON::Validator).to receive(:fully_validate_schema).and_return([])
allow(JSON::Validator).to receive(:fully_validate).and_raise(exception)
end

it 'logs exception and raises exception' do
expect(Rails.logger).to receive(:error)
.with('Error during form validation!', { error: exception.message, backtrace: anything, schema:,
clear_cache: false })

expect { saved_claim.validate }.to raise_error(exception.class, exception.message)
expect(PersonalInformationLog).to receive(:create).with(
data: { schema: schema,
parsed_form: saved_claim.parsed_form,
params: { errors_as_objects: true, clear_cache: false } },
error_class: 'SavedClaim FormValidationError'
)

expect { saved_claim.validate }.to raise_error(exception.class, exception.message)
end
end
end

context 'when JSON:Validator.fully_validate throws an exception' do
let(:exception) { StandardError.new('Some exception') }
context 'saved_claim_schema_validation_disable enabled' do
let(:schema_errors) { [{ fragment: 'error' }] }

before do
allow(JSON::Validator).to receive(:fully_validate_schema).and_return([])
allow(JSON::Validator).to receive(:fully_validate).and_raise(exception)
allow(Flipper).to receive(:enabled?).with(:saved_claim_schema_validation_disable).and_return(true)
end

it 'logs exception and raises exception' do
expect(Rails.logger).to receive(:error)
.with('Error during form validation!', { error: exception.message, backtrace: anything, schema:,
clear_cache: false })
context 'when fully_validate returns errors' do
before do
allow(JSON::Validator).to receive(:fully_validate).and_return(schema_errors)
end

expect(PersonalInformationLog).to receive(:create).with(
data: { schema: schema,
parsed_form: saved_claim.parsed_form,
params: { errors_as_objects: true, clear_cache: false } },
error_class: 'SavedClaim FormValidationError'
)
it 'adds validation errors to the form' do
expect(JSON::Validator).not_to receive(:fully_validate_schema)

saved_claim.validate
expect(saved_claim.errors.full_messages).not_to be_empty
end
end

context 'when JSON:Validator.fully_validate throws an exception' do
let(:exception) { StandardError.new('Some exception') }

before do
allow(JSON::Validator).to receive(:fully_validate).and_raise(exception)
end

it 'logs exception and raises exception' do
expect(JSON::Validator).not_to receive(:fully_validate_schema)

expect(Rails.logger).to receive(:error)
.with('Error during form validation!', { error: exception.message, backtrace: anything, schema:,
clear_cache: false })

expect(PersonalInformationLog).to receive(:create).with(
data: { schema: schema,
parsed_form: saved_claim.parsed_form,
params: { errors_as_objects: true, clear_cache: false } },
error_class: 'SavedClaim FormValidationError'
)

expect { saved_claim.validate }.to raise_error(exception.class, exception.message)
expect { saved_claim.validate }.to raise_error(exception.class, exception.message)
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/requests/swagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3270,6 +3270,10 @@
end

describe 'contact us' do
before do
allow(Flipper).to receive(:enabled?).and_call_original
end

describe 'POST v0/contact_us/inquiries' do
let(:post_body) do
{
Expand Down
4 changes: 4 additions & 0 deletions spec/sidekiq/education_form/create_daily_spool_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
end
let(:line_break) { EducationForm::CreateDailySpoolFiles::WINDOWS_NOTEPAD_LINEBREAK }

before do
allow(Flipper).to receive(:enabled?).and_call_original
end

after(:all) do
FileUtils.rm_rf('tmp/spool_files')
end
Expand Down
4 changes: 4 additions & 0 deletions spec/sidekiq/education_form/process10203_submissions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
let(:no_edipi_user) { create(:user, participant_id: nil) }
let(:evss_response_with_poa) { OpenStruct.new(body: get_fixture('json/evss_with_poa')) }

before do
allow(Flipper).to receive(:enabled?).and_call_original
end

describe 'scheduling' do
before do
allow(Rails.env).to receive('development?').and_return(true)
Expand Down

0 comments on commit 729b8ac

Please sign in to comment.