-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use mhv_user_account cached response or nil on User
- Loading branch information
1 parent
4893f1c
commit b04b6d6
Showing
7 changed files
with
162 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
describe MHV::AccountCreation::Service do | ||
describe '#create_account' do | ||
subject { described_class.new.create_account(icn:, email:, tou_occurred_at:, break_cache:) } | ||
subject { described_class.new.create_account(icn:, email:, tou_occurred_at:, break_cache:, from_cache_only:) } | ||
|
||
let(:icn) { '10101V964144' } | ||
let(:email) { '[email protected]' } | ||
|
@@ -16,9 +16,20 @@ | |
let(:account_creation_base_url) { 'https://apigw-intb.aws.myhealth.va.gov' } | ||
let(:account_creation_path) { 'v1/usermgmt/account-service/account' } | ||
let(:break_cache) { false } | ||
let(:from_cache_only) { false } | ||
let(:start_time) { Time.zone.now } | ||
let(:end_time) { start_time + 10.seconds } | ||
|
||
let(:user_profile_id) { '12345678' } | ||
let(:premium) { true } | ||
let(:champ_va) { true } | ||
let(:patient) { true } | ||
let(:sm_account_created) { true } | ||
let(:message) { 'Existing MHV Account Found for ICN' } | ||
let(:expected_response_body) do | ||
{ user_profile_id:, premium:, champ_va:, patient:, sm_account_created:, message: } | ||
end | ||
|
||
before do | ||
allow(Rails.logger).to receive(:info) | ||
allow(Rails.logger).to receive(:error) | ||
|
@@ -47,22 +58,53 @@ | |
end | ||
end | ||
|
||
context 'when from_cache_only is true' do | ||
let(:from_cache_only) { true } | ||
let(:expected_cache_key) { "mhv_account_creation_#{icn}" } | ||
|
||
context 'when the account is in the cache' do | ||
before do | ||
allow(Rails.cache).to receive(:read).with(expected_cache_key).and_return(expected_response_body) | ||
end | ||
|
||
it 'returns the cached response' do | ||
expect(subject).to eq(expected_response_body) | ||
end | ||
|
||
it 'does not make a request to the account creation service' do | ||
subject | ||
expect(a_request(:post, "#{account_creation_base_url}/#{account_creation_path}")).not_to have_been_made | ||
expect(Rails.logger).not_to have_received(:info).with("#{log_prefix} create_account request", | ||
anything) | ||
end | ||
end | ||
|
||
context 'when the account is not in the cache' do | ||
before do | ||
allow(Rails.cache).to receive(:read).with(expected_cache_key).and_return(nil) | ||
end | ||
|
||
it 'returns nil' do | ||
expect(subject).to be_nil | ||
end | ||
|
||
it 'does not make a request to the account creation service' do | ||
subject | ||
expect(a_request(:post, "#{account_creation_base_url}/#{account_creation_path}")).not_to have_been_made | ||
expect(Rails.logger).not_to have_received(:info).with("#{log_prefix} create_account request", | ||
anything) | ||
end | ||
end | ||
end | ||
|
||
context 'when the response is successful' do | ||
let(:successful_response_cassette) { 'mhv/account_creation/account_creation_service_200_found' } | ||
let(:expected_log_message) { "#{log_prefix} create_account success" } | ||
let(:expected_log_payload) do | ||
{ icn:, account: expected_response_body, from_cache: expected_from_cache_log, | ||
duration_ms: expected_duration }.compact | ||
end | ||
let(:user_profile_id) { '12345678' } | ||
let(:premium) { true } | ||
let(:champ_va) { true } | ||
let(:patient) { true } | ||
let(:sm_account_created) { true } | ||
let(:message) { 'Existing MHV Account Found for ICN' } | ||
let(:expected_response_body) do | ||
{ user_profile_id:, premium:, champ_va:, patient:, sm_account_created:, message: } | ||
end | ||
|
||
let(:expected_duration) { 10_000.0 } | ||
|
||
shared_examples 'a successful external request' do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
require 'mhv/account_creation/service' | ||
|
||
RSpec.describe MHV::UserAccount::Creator do | ||
subject { described_class.new(user_verification:, break_cache:) } | ||
subject { described_class.new(user_verification:, break_cache:, from_cache_only:) } | ||
|
||
let(:user_account) { create(:user_account, icn:) } | ||
let(:user_verification) { create(:user_verification, user_account:, user_credential_email:) } | ||
|
@@ -14,6 +14,7 @@ | |
let(:email) { '[email protected]' } | ||
let(:tou_occurred_at) { terms_of_use_agreement&.created_at } | ||
let(:break_cache) { false } | ||
let(:from_cache_only) { false } | ||
let(:mhv_client) { instance_double(MHV::AccountCreation::Service) } | ||
let(:mhv_response_body) do | ||
{ | ||
|
@@ -30,7 +31,7 @@ | |
|
||
allow(MHV::AccountCreation::Service).to receive(:new).and_return(mhv_client) | ||
allow(mhv_client).to receive(:create_account) | ||
.with(icn:, email:, tou_occurred_at:, break_cache:) | ||
.with(icn:, email:, tou_occurred_at:, break_cache:, from_cache_only:) | ||
.and_return(mhv_response_body) | ||
end | ||
|
||
|
@@ -80,7 +81,8 @@ | |
context 'when break_cache is false' do | ||
it 'calls MHV::AccountCreation::Service#create_account with break_cache: false' do | ||
subject.perform | ||
expect(mhv_client).to have_received(:create_account).with(icn:, email:, tou_occurred_at:, break_cache: false) | ||
expect(mhv_client).to have_received(:create_account).with(icn:, email:, tou_occurred_at:, break_cache: false, | ||
from_cache_only:) | ||
end | ||
end | ||
|
||
|
@@ -89,7 +91,39 @@ | |
|
||
it 'calls MHV::AccountCreation::Service#create_account with break_cache: true' do | ||
subject.perform | ||
expect(mhv_client).to have_received(:create_account).with(icn:, email:, tou_occurred_at:, break_cache: true) | ||
expect(mhv_client).to have_received(:create_account).with(icn:, email:, tou_occurred_at:, break_cache: true, | ||
from_cache_only:) | ||
end | ||
end | ||
|
||
context 'when from_cache_only is true' do | ||
let(:from_cache_only) { true } | ||
let(:expected_cache_key) { "mhv_account_creation_#{icn}" } | ||
|
||
before do | ||
allow(Rails.cache).to receive(:read).with(expected_cache_key).and_return(mhv_response_body) | ||
end | ||
|
||
it 'calls MHV::AccountCreation::Service#create_account with from_cache_only: true' do | ||
subject.perform | ||
expect(mhv_client).to have_received(:create_account).with(icn:, email:, tou_occurred_at:, break_cache:, | ||
from_cache_only: true) | ||
end | ||
|
||
context 'when the response is cached' do | ||
it 'returns the cached response' do | ||
mhv_user_account = subject.perform | ||
expect(mhv_user_account).to be_a(MHVUserAccount) | ||
expect(mhv_user_account).to be_valid | ||
end | ||
end | ||
|
||
context 'when the response is not cached' do | ||
let(:mhv_response_body) { nil } | ||
|
||
it 'returns nil' do | ||
expect(subject.perform).to be_nil | ||
end | ||
end | ||
end | ||
end | ||
|