diff --git a/app/sidekiq/terms_of_use/sign_up_service_updater_job.rb b/app/sidekiq/terms_of_use/sign_up_service_updater_job.rb index e7621ae601a..0dd8770d114 100644 --- a/app/sidekiq/terms_of_use/sign_up_service_updater_job.rb +++ b/app/sidekiq/terms_of_use/sign_up_service_updater_job.rb @@ -33,7 +33,7 @@ def perform(user_account_uuid, version) @user_account_uuid = user_account_uuid @version = version - return unless sec_id? + return if missing_sec_id? || agreement_unchanged? log_updated_icn terms_of_use_agreement.accepted? ? accept : decline @@ -48,23 +48,42 @@ def log_updated_icn end end + def map_client + @map_client ||= MAP::SignUp::Service.new + end + + def map_status + @map_status ||= map_client.status(icn: mpi_profile.icn) + end + + def agreement_unchanged? + if terms_of_use_agreement.declined? != map_status[:opt_out] || + terms_of_use_agreement.accepted? != map_status[:agreement_signed] + return false + end + + Rails.logger.info("#{LOG_TITLE} Not updating Sign Up Service due to unchanged agreement", + { icn: user_account.icn }) + true + end + def accept - MAP::SignUp::Service.new.agreements_accept(icn: mpi_profile.icn, signature_name:, version:) + map_client.agreements_accept(icn: mpi_profile.icn, signature_name:, version:) end def decline - MAP::SignUp::Service.new.agreements_decline(icn: mpi_profile.icn) + map_client.agreements_decline(icn: mpi_profile.icn) end - def sec_id? + def missing_sec_id? if mpi_profile.sec_id.present? validate_multiple_sec_ids - return true + return false end Rails.logger.info("#{LOG_TITLE} Sign Up Service not updated due to user missing sec_id", { icn: user_account.icn }) - false + true end def validate_multiple_sec_ids diff --git a/spec/sidekiq/terms_of_use/sign_up_service_updater_job_spec.rb b/spec/sidekiq/terms_of_use/sign_up_service_updater_job_spec.rb index 08f1e09c048..81c70cfd12e 100644 --- a/spec/sidekiq/terms_of_use/sign_up_service_updater_job_spec.rb +++ b/spec/sidekiq/terms_of_use/sign_up_service_updater_job_spec.rb @@ -80,106 +80,130 @@ end context 'when sec_id is present' do - context 'sec_id validation' do + let(:status) { { opt_out: false, agreement_signed: false } } + + before do + allow(service_instance).to receive(:agreements_accept) + allow(service_instance).to receive(:status).and_return(status) + allow(Rails.logger).to receive(:info) + end + + shared_examples 'logs an unchanged agreement' do + let(:expected_log) do + '[TermsOfUse][SignUpServiceUpdaterJob] Not updating Sign Up Service due to unchanged agreement' + end + before do - allow(service_instance).to receive(:agreements_accept) allow(Rails.logger).to receive(:info) end - context 'when a single sec_id value is detected' do - it 'does not log a warning message' do - job.perform(user_account_uuid, version) + it 'logs that the agreement is not changed' do + job.perform(user_account_uuid, version) - expect(Rails.logger).not_to have_received(:info) - end + expect(Rails.logger).to have_received(:info).with(expected_log, icn:) end + end - context 'when multiple sec_id values are detected' do - let(:sec_ids) { [sec_id, 'other-sec-id'] } - let(:expected_log) { '[TermsOfUse][SignUpServiceUpdaterJob] Multiple sec_id values detected' } + shared_examples 'logs an icn mismatch warning' do + let(:expected_log) do + '[TermsOfUse][SignUpServiceUpdaterJob] Detected changed ICN for user' + end + let(:mpi_profile) { build(:mpi_profile, icn: mpi_icn, sec_id:, given_names:, family_name:) } + let(:mpi_icn) { 'some-mpi-icn' } - it 'logs a warning message' do - job.perform(user_account_uuid, version) + before do + allow(Rails.logger).to receive(:info) + end - expect(Rails.logger).to have_received(:info).with(expected_log, icn:) - end + it 'logs a detected changed ICN message' do + job.perform(user_account_uuid, version) - it 'updates the terms of use agreement in sign up service' do - job.perform(user_account_uuid, version) + expect(MAP::SignUp::Service).to have_received(:new) + expect(Rails.logger).to have_received(:info).with(expected_log, { icn:, mpi_icn: }) + end + end - expect(MAP::SignUp::Service).to have_received(:new) - expect(service_instance).to have_received(:agreements_accept).with(icn: user_account.icn, - signature_name: common_name, - version:) - end + context 'when multiple sec_id values are detected' do + let(:sec_ids) { [sec_id, 'other-sec-id'] } + + let(:expected_log) { '[TermsOfUse][SignUpServiceUpdaterJob] Multiple sec_id values detected' } + + it 'logs a warning message' do + job.perform(user_account_uuid, version) + + expect(Rails.logger).to have_received(:info).with(expected_log, icn:) end end - context 'when the terms of use agreement is accepted' do + context 'and terms of use agreement is accepted' do + let(:response) { 'accepted' } + before do + allow(service_instance).to receive(:status).and_return(status) allow(service_instance).to receive(:agreements_accept) end - context 'and user account icn does not equal the mpi profile icn' do - let(:expected_log) do - '[TermsOfUse][SignUpServiceUpdaterJob] Detected changed ICN for user' - end - let(:mpi_profile) { build(:mpi_profile, icn: mpi_icn, sec_id:, given_names:, family_name:) } - let(:mpi_icn) { 'some-mpi-icn' } + context 'when sign up service status agreement response is true' do + let(:status) { { opt_out: false, agreement_signed: true } } - before do - allow(Rails.logger).to receive(:info) - end + it_behaves_like 'logs an unchanged agreement' - it 'logs a detected changed ICN message' do + it 'does not update terms of use agreement in sign up service' do job.perform(user_account_uuid, version) - expect(MAP::SignUp::Service).to have_received(:new) - expect(Rails.logger).to have_received(:info).with(expected_log, { icn:, mpi_icn: }) + expect(service_instance).not_to have_received(:agreements_accept) end end - it 'updates the terms of use agreement in sign up service' do - job.perform(user_account_uuid, version) + context 'when sign up service status agreement response is false' do + let(:status) { { opt_out: false, agreement_signed: false } } - expect(MAP::SignUp::Service).to have_received(:new) - expect(service_instance).to have_received(:agreements_accept).with(icn: mpi_profile.icn, - signature_name: common_name, - version:) + it 'updates the terms of use agreement in sign up service' do + job.perform(user_account_uuid, version) + + expect(service_instance).to have_received(:agreements_accept).with(icn: user_account.icn, + signature_name: common_name, + version:) + end + + context 'and user account icn does not equal the mpi profile icn' do + it_behaves_like 'logs an icn mismatch warning' + end end end - context 'when the terms of use agreement is declined' do + context 'and terms of use agreement is declined' do let(:response) { 'declined' } before do + allow(service_instance).to receive(:status).and_return(status) allow(service_instance).to receive(:agreements_decline) end - context 'and user account icn does not equal the mpi profile icn' do - let(:expected_log) do - '[TermsOfUse][SignUpServiceUpdaterJob] Detected changed ICN for user' - end - let(:mpi_profile) { build(:mpi_profile, icn: mpi_icn, sec_id:, given_names:, family_name:) } - let(:mpi_icn) { 'some-mpi-icn' } + context 'when sign up service status opt out response is true' do + let(:status) { { opt_out: true, agreement_signed: false } } - before do - allow(Rails.logger).to receive(:info) - end + it_behaves_like 'logs an unchanged agreement' - it 'logs a detected changed ICN message' do + it 'does not update terms of use agreement in sign up service' do job.perform(user_account_uuid, version) - expect(MAP::SignUp::Service).to have_received(:new) - expect(Rails.logger).to have_received(:info).with(expected_log, { icn:, mpi_icn: }) + expect(service_instance).not_to have_received(:agreements_decline) end end - it 'updates the terms of use agreement in sign up service' do - job.perform(user_account_uuid, version) + context 'when sign up service status opt out response is false' do + let(:status) { { opt_out: false, agreement_signed: false } } - expect(MAP::SignUp::Service).to have_received(:new) - expect(service_instance).to have_received(:agreements_decline).with(icn: mpi_profile.icn) + it 'updates the terms of use agreement in sign up service' do + job.perform(user_account_uuid, version) + + expect(service_instance).to have_received(:agreements_decline).with(icn: user_account.icn) + end + + context 'and user account icn does not equal the mpi profile icn' do + it_behaves_like 'logs an icn mismatch warning' + end end end end