|
1399 | 1399 | describe '#mhv_user_account' do
|
1400 | 1400 | let(:user) { build(:user, :loa3) }
|
1401 | 1401 | let(:icn) { user.icn }
|
| 1402 | + let(:expected_cache_key) { "mhv_account_creation_#{icn}" } |
1402 | 1403 |
|
1403 | 1404 | let!(:user_verification) do
|
1404 | 1405 | create(:idme_user_verification, idme_uuid: user.idme_uuid, user_credential_email:, user_account:)
|
|
1423 | 1424 | before do
|
1424 | 1425 | allow(Rails.logger).to receive(:info)
|
1425 | 1426 | allow(MHV::AccountCreation::Service).to receive(:new).and_return(mhv_client)
|
1426 |
| - allow(mhv_client).to receive(:create_account).and_return(mhv_response) |
| 1427 | + allow(Rails.cache).to receive(:read).with(expected_cache_key).and_return(mhv_response) |
1427 | 1428 | end
|
1428 | 1429 |
|
1429 |
| - context 'when the user has all required attributes' do |
1430 |
| - it 'returns a MHVUserAccount with the expected attributes' do |
1431 |
| - mhv_user_account = user.mhv_user_account |
| 1430 | + context 'when the mhv response is cached' do |
| 1431 | + context 'when the user has all required attributes' do |
| 1432 | + it 'returns a MHVUserAccount with the expected attributes' do |
| 1433 | + mhv_user_account = user.mhv_user_account |
1432 | 1434 |
|
1433 |
| - expect(mhv_user_account).to be_a(MHVUserAccount) |
1434 |
| - expect(mhv_user_account.attributes).to eq(mhv_response.with_indifferent_access) |
| 1435 | + expect(mhv_user_account).to be_a(MHVUserAccount) |
| 1436 | + expect(mhv_user_account.attributes).to eq(mhv_response.with_indifferent_access) |
| 1437 | + end |
1435 | 1438 | end
|
1436 |
| - end |
1437 | 1439 |
|
1438 |
| - context 'when there is an error creating the account' do |
1439 |
| - shared_examples 'mhv_user_account error' do |
1440 |
| - let(:expected_log_message) { '[User] mhv_user_account error' } |
1441 |
| - let(:expected_log_payload) { { error_message: /#{expected_error_message}/, icn: user.icn } } |
| 1440 | + context 'when there is an error creating the account' do |
| 1441 | + shared_examples 'mhv_user_account error' do |
| 1442 | + let(:expected_log_message) { '[User] mhv_user_account error' } |
| 1443 | + let(:expected_log_payload) { { error_message: /#{expected_error_message}/, icn: user.icn } } |
1442 | 1444 |
|
1443 |
| - it 'logs and returns nil' do |
1444 |
| - expect(user.mhv_user_account).to be_nil |
1445 |
| - expect(Rails.logger).to have_received(:info).with(expected_log_message, expected_log_payload) |
| 1445 | + it 'logs and returns nil' do |
| 1446 | + expect(user.mhv_user_account).to be_nil |
| 1447 | + expect(Rails.logger).to have_received(:info).with(expected_log_message, expected_log_payload) |
| 1448 | + end |
1446 | 1449 | end
|
1447 |
| - end |
1448 | 1450 |
|
1449 |
| - context 'when the user does not have a terms_of_use_agreement' do |
1450 |
| - let(:terms_of_use_agreement) { nil } |
1451 |
| - let(:expected_error_message) { 'Current terms of use agreement must be present' } |
| 1451 | + context 'when the user does not have a terms_of_use_agreement' do |
| 1452 | + let(:terms_of_use_agreement) { nil } |
| 1453 | + let(:expected_error_message) { 'Current terms of use agreement must be present' } |
1452 | 1454 |
|
1453 |
| - it_behaves_like 'mhv_user_account error' |
1454 |
| - end |
| 1455 | + it_behaves_like 'mhv_user_account error' |
| 1456 | + end |
1455 | 1457 |
|
1456 |
| - context 'when the user has not accepted the terms of use' do |
1457 |
| - let(:terms_of_use_response) { 'declined' } |
1458 |
| - let(:expected_error_message) { "Current terms of use agreement must be 'accepted'" } |
| 1458 | + context 'when the user has not accepted the terms of use' do |
| 1459 | + let(:terms_of_use_response) { 'declined' } |
| 1460 | + let(:expected_error_message) { "Current terms of use agreement must be 'accepted'" } |
1459 | 1461 |
|
1460 |
| - it_behaves_like 'mhv_user_account error' |
1461 |
| - end |
| 1462 | + it_behaves_like 'mhv_user_account error' |
| 1463 | + end |
| 1464 | + |
| 1465 | + context 'when the user does not have a user_credential_email' do |
| 1466 | + let(:user_credential_email) { nil } |
| 1467 | + let(:expected_error_message) { 'Email must be present' } |
| 1468 | + |
| 1469 | + it_behaves_like 'mhv_user_account error' |
| 1470 | + end |
1462 | 1471 |
|
1463 |
| - context 'when the user does not have a user_credential_email' do |
1464 |
| - let(:user_credential_email) { nil } |
1465 |
| - let(:expected_error_message) { 'Email must be present' } |
| 1472 | + context 'when the user does not have an icn' do |
| 1473 | + let(:icn) { nil } |
| 1474 | + let(:expected_error_message) { 'ICN must be present' } |
1466 | 1475 |
|
1467 |
| - it_behaves_like 'mhv_user_account error' |
| 1476 | + it_behaves_like 'mhv_user_account error' |
| 1477 | + end |
1468 | 1478 | end
|
| 1479 | + end |
1469 | 1480 |
|
1470 |
| - context 'when the user does not have an icn' do |
1471 |
| - let(:icn) { nil } |
1472 |
| - let(:expected_error_message) { 'ICN must be present' } |
| 1481 | + context 'when the mhv response is not cached' do |
| 1482 | + let(:mhv_response) { nil } |
1473 | 1483 |
|
1474 |
| - it_behaves_like 'mhv_user_account error' |
| 1484 | + it 'returns nil' do |
| 1485 | + expect(user.mhv_user_account).to be_nil |
1475 | 1486 | end
|
1476 | 1487 | end
|
1477 | 1488 | end
|
|
0 commit comments