From becec7bcd981a7c49b3e9c269b5b5bebed05839b Mon Sep 17 00:00:00 2001 From: Jonathan Hooper Date: Thu, 12 Apr 2018 16:50:55 -0500 Subject: [PATCH 1/3] Test failed and timed out jobs in shared examples **Why**: To make sure that job failed and timeout behavior is consistent across steps, and to provide coverage for timeout behavior which was not covered before. --- spec/features/idv/failed_job_spec.rb | 30 +++---- spec/support/features/idv_step_helper.rb | 54 ++++++++++++ spec/support/idv_examples/failed_idv_job.rb | 97 +++++++++++++++++++++ 3 files changed, 164 insertions(+), 17 deletions(-) create mode 100644 spec/support/features/idv_step_helper.rb create mode 100644 spec/support/idv_examples/failed_idv_job.rb diff --git a/spec/features/idv/failed_job_spec.rb b/spec/features/idv/failed_job_spec.rb index f2d6a76b015..d2cd2bd30a8 100644 --- a/spec/features/idv/failed_job_spec.rb +++ b/spec/features/idv/failed_job_spec.rb @@ -1,25 +1,21 @@ require 'rails_helper' -feature 'IdV session' do - include IdvHelper +feature 'IdV session', :idv_job do + include IdvStepHelper - context 'Idv job raises an error', idv_job: true do - it 'displays a warning that something went wrong' do - sign_in_and_2fa_user + context 'profile job' do + let(:idv_job_class) { Idv::ProfileJob } - step = instance_double( - Idv::ProfileStep, attempts_exceeded?: false, vendor_validator_job_failed?: true - ) - allow(Idv::ProfileStep).to receive(:new).and_return(step) - allow(step).to receive(:submit). - and_return(FormResponse.new(success: false, errors: {}, extra: {})) + alias complete_previous_idv_steps start_idv_at_profile_step - visit verify_session_path - fill_out_idv_form_ok - click_idv_continue + it_behaves_like 'failed idv job', :sessions + end + + context 'phone job' do + let(:idv_job_class) { Idv::PhoneJob } + + alias complete_previous_idv_steps complete_idv_steps_before_phone_step - expect(page).to have_current_path(verify_session_result_path) - expect(page).to have_content t('idv.modal.sessions.jobfail') - end + it_behaves_like 'failed idv job', :phone end end diff --git a/spec/support/features/idv_step_helper.rb b/spec/support/features/idv_step_helper.rb new file mode 100644 index 00000000000..7bb5b797804 --- /dev/null +++ b/spec/support/features/idv_step_helper.rb @@ -0,0 +1,54 @@ +module IdvStepHelper + def self.included(base) + base.class_eval { include IdvHelper } + end + + def start_idv_at_profile_step(user = user_with_2fa) + sign_in_and_2fa_user(user) + visit verify_path unless current_path == verify_path + click_idv_begin + end + + def complete_idv_steps_before_address_step(user = user_with_2fa) + start_idv_at_profile_step(user) + fill_out_idv_form_ok + click_idv_continue + end + + def complete_idv_steps_before_phone_step(user = user_with_2fa) + complete_idv_steps_before_address_step(user) + click_idv_address_choose_phone + end + + def complete_idv_steps_before_usps_step(user = user_with_2fa) + complete_idv_steps_before_address_step(user) + click_idv_address_choose_usps + end + + def complete_idv_steps_before_phone_otp_delivery_selection_step(user = user_with_2fa) + complete_idv_steps_before_phone_step(user) + fill_out_phone_form_ok('2341230638') + click_idv_continue + end + + def complete_idv_steps_before_phone_otp_verification_step(user = user_with_2fa) + complete_idv_steps_before_phone_otp_delivery_selection_step(user) + choose_idv_otp_delivery_method_sms + end + + def complete_idv_steps_before_usps_otp_verification_step + # TODO Figure this one out + end + + def complete_idv_steps_before_review_step(user = user_with_2fa) + complete_idv_steps_before_phone_step(user) + fill_out_phone_form_ok(user.phone) + click_idv_continue + end + + def complete_idv_steps_before_confirmation_step(user = user_with_2fa) + complete_idv_steps_before_review_step(user) + fill_in 'Password', with: password + click_continue + end +end diff --git a/spec/support/idv_examples/failed_idv_job.rb b/spec/support/idv_examples/failed_idv_job.rb new file mode 100644 index 00000000000..cc7dbda9d7e --- /dev/null +++ b/spec/support/idv_examples/failed_idv_job.rb @@ -0,0 +1,97 @@ +shared_examples 'failed idv job' do |step| + before do + visit_idp_from_sp_with_loa3(:oidc) + click_link t('links.sign_in') + complete_previous_idv_steps + end + + context 'the job raises an error' do + before do + stub_idv_job_to_raise_error_in_background(idv_job_class) + + fill_out_idv_form_ok if step == :sessions + fill_out_phone_form_ok if step == :phone + click_idv_continue + end + + context 'without js' do + it 'shows a warning' do + expect(page).to have_content t("idv.modal.#{step}.heading") + expect(page).to have_content t("idv.modal.#{step}.jobfail") + expect(page).to have_current_path(verify_session_result_path) if step == :sessions + expect(page).to have_current_path(verify_phone_result_path) if step == :phone + end + end + + context 'with js', :js do + it 'shows a modal' do + expect(page).to have_css('.modal-warning', text: t("idv.modal.#{step}.heading")) + expect(page).to have_css( + '.modal-warning', + text: ActionController::Base.helpers.strip_tags(t("idv.modal.#{step}.jobfail")) + ) + expect(page).to have_current_path(verify_session_result_path) if step == :sessions + expect(page).to have_current_path(verify_phone_result_path) if step == :phone + end + end + end + + context 'the job times out' do + before do + stub_idv_job_to_timeout_in_background(idv_job_class) + + fill_out_idv_form_ok if step == :sessions + fill_out_phone_form_ok('5202691958') if step == :phone + click_idv_continue + + Timecop.travel (Figaro.env.async_job_refresh_max_wait_seconds.to_i + 1).seconds + + visit current_path + end + + after do + Timecop.return + end + + context 'without js' do + it 'shows a warning' do + expect(page).to have_content t("idv.modal.#{step}.heading") + expect(page).to have_content t("idv.modal.#{step}.timeout") + expect(page).to have_current_path(verify_session_result_path) if step == :sessions + expect(page).to have_current_path(verify_phone_result_path) if step == :phone + end + end + + context 'with js' do + it 'shows a modal' do + expect(page).to have_css('.modal-warning', text: t("idv.modal.#{step}.heading")) + expect(page).to have_css( + '.modal-warning', + text: ActionController::Base.helpers.strip_tags(t("idv.modal.#{step}.timeout")) + ) + expect(page).to have_current_path(verify_session_result_path) if step == :sessions + expect(page).to have_current_path(verify_phone_result_path) if step == :phone + end + end + end + + def stub_idv_job_to_raise_error_in_background(idv_job_class) + allow(idv_job_class).to receive(:new).and_wrap_original do |new, *args| + idv_job = new.call(*args) + allow(idv_job).to receive(:verify_identity_with_vendor). + and_raise('this is a test error') + idv_job + end + allow(idv_job_class).to receive(:perform_now).and_wrap_original do |perform_now, *args| + begin + perform_now.call(*args) + rescue StandardError => err + # Swallow the error so it does not get re-raised by the job + end + end + end + + def stub_idv_job_to_timeout_in_background(idv_job_class) + allow(idv_job_class).to receive(:perform_now) + end +end From 6c913c1f13044803859c38b840898af731f1c20b Mon Sep 17 00:00:00 2001 From: Jonathan Hooper Date: Fri, 13 Apr 2018 13:21:23 -0500 Subject: [PATCH 2/3] use idv step helpers from master --- spec/features/idv/failed_job_spec.rb | 8 +---- spec/support/idv_examples/failed_idv_job.rb | 35 ++++++++++++--------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/spec/features/idv/failed_job_spec.rb b/spec/features/idv/failed_job_spec.rb index d2cd2bd30a8..54bafd1306e 100644 --- a/spec/features/idv/failed_job_spec.rb +++ b/spec/features/idv/failed_job_spec.rb @@ -5,17 +5,11 @@ context 'profile job' do let(:idv_job_class) { Idv::ProfileJob } - - alias complete_previous_idv_steps start_idv_at_profile_step - - it_behaves_like 'failed idv job', :sessions + it_behaves_like 'failed idv job', :profile end context 'phone job' do let(:idv_job_class) { Idv::PhoneJob } - - alias complete_previous_idv_steps complete_idv_steps_before_phone_step - it_behaves_like 'failed idv job', :phone end end diff --git a/spec/support/idv_examples/failed_idv_job.rb b/spec/support/idv_examples/failed_idv_job.rb index cc7dbda9d7e..ad2a9b342b9 100644 --- a/spec/support/idv_examples/failed_idv_job.rb +++ b/spec/support/idv_examples/failed_idv_job.rb @@ -1,36 +1,41 @@ shared_examples 'failed idv job' do |step| + let(:step_locale_key) do + return :sessions if step == :profile + step + end + before do visit_idp_from_sp_with_loa3(:oidc) click_link t('links.sign_in') - complete_previous_idv_steps + complete_idv_steps_before_step(step) end context 'the job raises an error' do before do stub_idv_job_to_raise_error_in_background(idv_job_class) - fill_out_idv_form_ok if step == :sessions + fill_out_idv_form_ok if step == :profile fill_out_phone_form_ok if step == :phone click_idv_continue end context 'without js' do it 'shows a warning' do - expect(page).to have_content t("idv.modal.#{step}.heading") - expect(page).to have_content t("idv.modal.#{step}.jobfail") - expect(page).to have_current_path(verify_session_result_path) if step == :sessions + expect(page).to have_content t("idv.modal.#{step_locale_key}.heading") + expect(page).to have_content t("idv.modal.#{step_locale_key}.jobfail") + expect(page).to have_current_path(verify_session_result_path) if step == :profile expect(page).to have_current_path(verify_phone_result_path) if step == :phone end end context 'with js', :js do it 'shows a modal' do - expect(page).to have_css('.modal-warning', text: t("idv.modal.#{step}.heading")) + expect(page).to have_css('.modal-warning', text: t("idv.modal.#{step_locale_key}.heading")) expect(page).to have_css( '.modal-warning', - text: ActionController::Base.helpers.strip_tags(t("idv.modal.#{step}.jobfail")) + text: ActionController::Base.helpers.strip_tags(t("idv.modal.#{step_locale_key}.jobfail")) ) - expect(page).to have_current_path(verify_session_result_path) if step == :sessions + expect(page).to have_current_path(verify_session_result_path) if step == :profile expect(page).to have_current_path(verify_phone_result_path) if step == :phone end end @@ -40,7 +45,7 @@ before do stub_idv_job_to_timeout_in_background(idv_job_class) - fill_out_idv_form_ok if step == :sessions + fill_out_idv_form_ok if step == :profile fill_out_phone_form_ok('5202691958') if step == :phone click_idv_continue @@ -55,21 +60,21 @@ context 'without js' do it 'shows a warning' do - expect(page).to have_content t("idv.modal.#{step}.heading") - expect(page).to have_content t("idv.modal.#{step}.timeout") - expect(page).to have_current_path(verify_session_result_path) if step == :sessions + expect(page).to have_content t("idv.modal.#{step_locale_key}.heading") + expect(page).to have_content t("idv.modal.#{step_locale_key}.timeout") + expect(page).to have_current_path(verify_session_result_path) if step == :profile expect(page).to have_current_path(verify_phone_result_path) if step == :phone end end context 'with js' do it 'shows a modal' do - expect(page).to have_css('.modal-warning', text: t("idv.modal.#{step}.heading")) + expect(page).to have_css('.modal-warning', text: t("idv.modal.#{step_locale_key}.heading")) expect(page).to have_css( '.modal-warning', - text: ActionController::Base.helpers.strip_tags(t("idv.modal.#{step}.timeout")) + text: ActionController::Base.helpers.strip_tags(t("idv.modal.#{step_locale_key}.timeout")) ) - expect(page).to have_current_path(verify_session_result_path) if step == :sessions + expect(page).to have_current_path(verify_session_result_path) if step == :profile expect(page).to have_current_path(verify_phone_result_path) if step == :phone end end From deac0a4cf81f85ecf1bfee179247a145d4acb6c0 Mon Sep 17 00:00:00 2001 From: Jonathan Hooper Date: Fri, 13 Apr 2018 14:40:20 -0500 Subject: [PATCH 3/3] add strip tags helper --- spec/rails_helper.rb | 1 + spec/support/features/step_tags_helper.rb | 7 +++++++ spec/support/idv_examples/failed_idv_job.rb | 4 ++-- spec/support/idv_examples/max_attempts.rb | 4 ---- 4 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 spec/support/features/step_tags_helper.rb diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 555968d4751..13e7c12f2c6 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -29,6 +29,7 @@ config.include Features::LocalizationHelper, type: :feature config.include Features::MailerHelper, type: :feature config.include Features::SessionHelper, type: :feature + config.include Features::StripTagsHelper, type: :feature config.include AnalyticsHelper config.include AwsKmsClientHelper config.include KeyRotationHelper diff --git a/spec/support/features/step_tags_helper.rb b/spec/support/features/step_tags_helper.rb new file mode 100644 index 00000000000..8131446fd35 --- /dev/null +++ b/spec/support/features/step_tags_helper.rb @@ -0,0 +1,7 @@ +module Features + module StripTagsHelper + def strip_tags(*args) + ActionController::Base.helpers.strip_tags(*args) + end + end +end diff --git a/spec/support/idv_examples/failed_idv_job.rb b/spec/support/idv_examples/failed_idv_job.rb index ad2a9b342b9..98f0ce8f837 100644 --- a/spec/support/idv_examples/failed_idv_job.rb +++ b/spec/support/idv_examples/failed_idv_job.rb @@ -33,7 +33,7 @@ expect(page).to have_css('.modal-warning', text: t("idv.modal.#{step_locale_key}.heading")) expect(page).to have_css( '.modal-warning', - text: ActionController::Base.helpers.strip_tags(t("idv.modal.#{step_locale_key}.jobfail")) + text: strip_tags(t("idv.modal.#{step_locale_key}.jobfail")) ) expect(page).to have_current_path(verify_session_result_path) if step == :profile expect(page).to have_current_path(verify_phone_result_path) if step == :phone @@ -72,7 +72,7 @@ expect(page).to have_css('.modal-warning', text: t("idv.modal.#{step_locale_key}.heading")) expect(page).to have_css( '.modal-warning', - text: ActionController::Base.helpers.strip_tags(t("idv.modal.#{step_locale_key}.timeout")) + text: strip_tags(t("idv.modal.#{step_locale_key}.timeout")) ) expect(page).to have_current_path(verify_session_result_path) if step == :profile expect(page).to have_current_path(verify_phone_result_path) if step == :phone diff --git a/spec/support/idv_examples/max_attempts.rb b/spec/support/idv_examples/max_attempts.rb index 83d7b0a2742..92995bbab8d 100644 --- a/spec/support/idv_examples/max_attempts.rb +++ b/spec/support/idv_examples/max_attempts.rb @@ -104,8 +104,4 @@ def advance_to_phone_step click_idv_begin click_idv_address_choose_phone end - - def strip_tags(*args) - ActionController::Base.helpers.strip_tags(*args) - end end