diff --git a/modules/check_in/app/services/check_in/vaos/base_service.rb b/modules/check_in/app/services/check_in/vaos/base_service.rb index 8cf947ab968..8a906d04d98 100644 --- a/modules/check_in/app/services/check_in/vaos/base_service.rb +++ b/modules/check_in/app/services/check_in/vaos/base_service.rb @@ -9,13 +9,25 @@ class BaseService < Common::Client::Base include SentryLogging include Common::Client::Concerns::Monitoring - attr_reader :patient_icn, :token_service + attr_reader :check_in_session, :patient_icn STATSD_KEY_PREFIX = 'api.check_in.vaos' - def initialize(patient_icn:) - @patient_icn = patient_icn - @token_service = CheckIn::Map::TokenService.build({ patient_icn: }) + ## + # Builds a Service instance + # + # @param opts [Hash] options to create the object + # + # @return [Service] an instance of this class + # + def self.build(opts = {}) + new(opts) + end + + def initialize(opts) + @check_in_session = opts[:check_in_session] + @patient_icn = ::V2::Lorota::RedisClient.build.icn(uuid: check_in_session.uuid) + super() end @@ -35,6 +47,10 @@ def headers } end + def token_service + @token_service ||= Map::TokenService.build(patient_icn:) + end + def referrer if Settings.hostname.ends_with?('.gov') "https://#{Settings.hostname}".gsub('vets', 'va') diff --git a/modules/check_in/spec/services/check_in/map/token_service_spec.rb b/modules/check_in/spec/services/check_in/map/token_service_spec.rb index a87429a51f1..a03e1e7b073 100644 --- a/modules/check_in/spec/services/check_in/map/token_service_spec.rb +++ b/modules/check_in/spec/services/check_in/map/token_service_spec.rb @@ -3,14 +3,10 @@ require 'rails_helper' describe CheckIn::Map::TokenService do - subject { described_class } + subject { described_class.build(opts) } let(:patient_icn) { '123' } - let(:opts) do - { - patient_icn: - } - end + let(:opts) { { patient_icn: } } let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) } before do @@ -21,13 +17,13 @@ describe '.build' do it 'returns an instance of Service' do - expect(subject.build(opts)).to be_an_instance_of(described_class) + expect(subject).to be_an_instance_of(described_class) end end describe '#initialize' do it 'has a redis client' do - expect(subject.build(opts).redis_client).to be_a(CheckIn::Map::RedisClient) + expect(subject.redis_client).to be_a(CheckIn::Map::RedisClient) end end @@ -41,7 +37,7 @@ end it 'returns token from redis' do - expect(subject.build(opts).token).to eq(access_token) + expect(subject.token).to eq(access_token) end end @@ -51,8 +47,11 @@ .and_return({ access_token:, expiration: }) end - it 'returns token by calling client' do - expect(subject.build(opts).token).to eq(access_token) + it 'returns token by calling client and saves it in redis' do + redis_client = subject.redis_client + expect(redis_client).to receive(:save_token) + + expect(subject.token).to eq(access_token) end end end diff --git a/modules/check_in/spec/services/check_in/vaos/appointment_service_spec.rb b/modules/check_in/spec/services/check_in/vaos/appointment_service_spec.rb index ca71d3cb05c..37adf918027 100644 --- a/modules/check_in/spec/services/check_in/vaos/appointment_service_spec.rb +++ b/modules/check_in/spec/services/check_in/vaos/appointment_service_spec.rb @@ -3,17 +3,17 @@ require 'rails_helper' describe CheckIn::VAOS::AppointmentService do - subject { described_class.new(patient_icn:) } + subject { described_class } + let(:uuid) { 'd602d9eb-9a31-484f-9637-13ab0b507e0d' } + let(:check_in_session) { CheckIn::V2::Session.build(data: { uuid: }) } let(:patient_icn) { '123' } let(:token) { 'test_token' } let(:request_id) { SecureRandom.uuid } - describe '#initialize' do - it 'returns an instance of service' do - service_obj = subject - expect(service_obj).to be_an_instance_of(CheckIn::VAOS::AppointmentService) - expect(service_obj.token_service).to be_an_instance_of(CheckIn::Map::TokenService) + describe '.build' do + it 'returns an instance of Service' do + expect(subject.build(check_in_session:)).to be_an_instance_of(described_class) end end @@ -42,21 +42,27 @@ let(:faraday_response) { double('Faraday::Response') } let(:faraday_env) { double('Faraday::Env', status: 200, body: appointments_response.to_json) } + before do + allow_any_instance_of(V2::Lorota::RedisClient).to receive(:icn).with(uuid:) + .and_return(patient_icn) + allow_any_instance_of(CheckIn::Map::TokenService).to receive(:token) + .and_return(token) + end + context 'when vaos returns successful response' do before do - allow_any_instance_of(CheckIn::Map::TokenService).to receive(:token) - .and_return(token) - allow_any_instance_of(Faraday::Connection).to receive(:get).with('/vaos/v1/patients/123/appointments', - { start: start_date, end: end_date, - statuses: }) - .and_return(faraday_response) + allow_any_instance_of(Faraday::Connection).to receive(:get) + .with("/vaos/v1/patients/#{patient_icn}/appointments", + { start: start_date, end: end_date, statuses: }) + .and_return(faraday_response) allow(faraday_response).to receive(:env).and_return(faraday_env) end it 'returns appointments' do - response = subject.get_appointments(DateTime.parse(start_date).in_time_zone, - DateTime.parse(end_date).in_time_zone, - statuses) + svc = subject.build(check_in_session:) + response = svc.get_appointments(DateTime.parse(start_date).in_time_zone, + DateTime.parse(end_date).in_time_zone, + statuses) expect(response).to eq(appointments_response) end end @@ -66,8 +72,6 @@ let(:exception) { Common::Exceptions::BackendServiceException.new(nil, {}, resp.status, resp.body) } before do - allow_any_instance_of(CheckIn::Map::TokenService).to receive(:token) - .and_return(token) allow_any_instance_of(Faraday::Connection).to receive(:get).with('/vaos/v1/patients/123/appointments', { start: start_date, end: end_date, statuses: }) @@ -75,10 +79,11 @@ end it 'throws exception' do + svc = subject.build(check_in_session:) expect do - subject.get_appointments(DateTime.parse(start_date).in_time_zone, - DateTime.parse(end_date).in_time_zone, - statuses) + svc.get_appointments(DateTime.parse(start_date).in_time_zone, + DateTime.parse(end_date).in_time_zone, + statuses) end.to(raise_error do |error| expect(error).to be_a(Common::Exceptions::BackendServiceException) end) diff --git a/modules/check_in/spec/services/check_in/vaos/base_service_spec.rb b/modules/check_in/spec/services/check_in/vaos/base_service_spec.rb index f523aab9fc6..e7733b82363 100644 --- a/modules/check_in/spec/services/check_in/vaos/base_service_spec.rb +++ b/modules/check_in/spec/services/check_in/vaos/base_service_spec.rb @@ -3,12 +3,35 @@ require 'rails_helper' describe CheckIn::VAOS::BaseService do - subject { described_class.new(patient_icn:) } + subject { described_class.build(check_in_session:) } + let(:uuid) { 'd602d9eb-9a31-484f-9637-13ab0b507e0d' } + let(:check_in_session) { CheckIn::V2::Session.build(data: { uuid: }) } let(:patient_icn) { '123' } let(:token) { 'test_token' } let(:request_id) { SecureRandom.uuid } + describe '.build' do + it 'returns an instance of Service' do + expect(subject).to be_an_instance_of(described_class) + end + end + + describe '#initialize' do + before do + allow_any_instance_of(V2::Lorota::RedisClient).to receive(:icn).with(uuid:) + .and_return(patient_icn) + end + + it 'has a check_in_session object' do + expect(subject.check_in_session).to be_a(CheckIn::V2::Session) + end + + it 'has a patient icn' do + expect(subject.patient_icn).to eq(patient_icn) + end + end + describe '#config' do it 'returns an instance of Configuration' do expect(subject.config).to be_an_instance_of(CheckIn::VAOS::Configuration) @@ -17,8 +40,9 @@ describe '#headers' do before do - allow_any_instance_of(CheckIn::Map::TokenService).to receive(:token).and_return(token) RequestStore.store['request_id'] = request_id + + allow_any_instance_of(CheckIn::Map::TokenService).to receive(:token).and_return(token) end it 'returns correct headers' do