Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10-10CG] Add ability to not call fully_validate_schema in SavedClaim #19468

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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 @@ -1164,6 +1164,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
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change these to stubs instead of actually enabling/disabling the toggle. Added stub at the top to default to original values to handle any that are not stubbed.

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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were failing because some expectations were validating that some toggle was set and the new Flipper call in saved_claim was expecting it. I added this to default all to call the original so any that don't have explicit expectations won't fail.

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

describe 'POST v0/contact_us/inquiries' do
let(:post_body) do
{
Expand Down
Loading