Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test failed and timed out jobs in shared examples #2097

Merged
merged 5 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions spec/features/idv/failed_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
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

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: {}))

visit verify_session_path
fill_out_idv_form_ok
click_idv_continue
context 'profile job' do
let(:idv_job_class) { Idv::ProfileJob }
it_behaves_like 'failed idv job', :profile
end

expect(page).to have_current_path(verify_session_result_path)
expect(page).to have_content t('idv.modal.sessions.jobfail')
end
context 'phone job' do
let(:idv_job_class) { Idv::PhoneJob }
it_behaves_like 'failed idv job', :phone
end
end
102 changes: 102 additions & 0 deletions spec/support/idv_examples/failed_idv_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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_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 == :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_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_locale_key}.heading"))
expect(page).to have_css(
'.modal-warning',
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 == :profile
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 == :profile
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_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_locale_key}.heading"))
expect(page).to have_css(
'.modal-warning',
text: ActionController::Base.helpers.strip_tags(t("idv.modal.#{step_locale_key}.timeout"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding a helper method for strip_tags like you did in the other PR, so that it can be used in various tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, didn't remember that it was also here. Will do once I merge that one.

)
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
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