Skip to main content
Skip table of contents

Match Response Schema Implementation

Introduction

Creating a service backend endpoint for http://va.gov is a common task for developers. Due to the complexity of integrating with legacy VA REST or SOAP applications we've developed a pattern for making these connections. This document presents example specifications for the test suite, focusing on matching response and request schemas.

Contact Information Service Spec

CODE
# spec/lib/va_profile/contact_information/service_spec.rb  
...
  context 'when successful' do
    it 'creates an old_email record' do
      VCR.use_cassette('va_profile/contact_information/put_email_success', VCR::MATCH_EVERYTHING) do
        VCR.use_cassette('va_profile/contact_information/person_full', VCR::MATCH_EVERYTHING) do
          allow(VAProfile::Configuration::SETTINGS.contact_information).to receive(:cache_enabled).and_return(true)
          old_email = user.vet360_contact_info.email.email_address
          expect_any_instance_of(VAProfile::Models::Transaction).to receive(:received?).and_return(true)
  
          response = subject.put_email(email)
          expect(OldEmail.find(response.transaction.id).email).to eq(old_email)
        end
      end
    end
  
    it 'returns a status of 200' do
      VCR.use_cassette('va_profile/contact_information/put_email_success', VCR::MATCH_EVERYTHING) do
        response = subject.put_email(email)
        expect(response.transaction.id).to eq('7d1667a5-df5f-4559-be35-b36042c61187')
        expect(response).to be_ok
      end
    end
...

Redis Contact Information Redis Request Spec

CODE
# vets-api/spec/models/va_profile_redis/contact_information_spec.rb

describe 'contact information attributes' do
  context 'with a successful response' do
    before do
      allow(VAProfile::Models::Person).to receive(:build_from).and_return(person)
      allow_any_instance_of(
        VAProfile::ContactInformation::Service
      ).to receive(:get_person).and_return(person_response)
    end

    describe '#email' do
      it 'returns the users email address object', :aggregate_failures do
        expect(contact_info.email).to eq person.emails.first
        expect(contact_info.email.class).to eq VAProfile::Models::Email
      end
    end
    ...
    ...
  end
end

Build Email with assigned attributes (build_from)

CODE
 email = VAProfile::Models::Email.build_from(data)
 
 
  def self.build_from(body)
    VAProfile::Models::Email.new(
      created_at: body['create_date'],
      email_address: body['email_address_text'],
      effective_end_date: body['effective_end_date'],
      effective_start_date: body['effective_start_date'],
      id: body['email_id'],
      source_date: body['source_date'],
      transaction_id: body['tx_audit_id'],
      updated_at: body['update_date'],
      vet360_id: body['vet360_id']
    )
  end

Email is updated after build

CODE
  # @param email [VAProfile::Models::Email] the email to update
  # @return [VAProfile::ContactInformation::EmailTransactionResponse] response wrapper around a transaction object
  def put_email(email)
    old_email =
      begin
        @user.va_profile_email
      rescue
        nil
      end

    response = post_or_put_data(:put, email, 'emails', EmailTransactionResponse)

    transaction = response.transaction
    OldEmail.create(transaction_id: transaction.id, email: old_email) if transaction.received? && old_email.present?

    response
  end

Email Transaction Response from Email Build (post_or_put_data)

CODE
    class EmailTransactionResponse < TransactionResponse
      attribute :response_body, String

      def self.from(*args)
        return_val = super

        return_val.response_body = @response_body

        return_val
      end

      def new_email
        tx_output = response_body['tx_output'][0]
        return if tx_output['effective_end_date'].present?

        tx_output['email_address_text']
      end
    end

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.