Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class OtpVerificationController < ApplicationController

before_action :check_sp_required_mfa_bypass
before_action :confirm_multiple_factors_enabled
before_action :redirect_if_blank_phone, only: [:show]
before_action :confirm_voice_capability, only: [:show]

def show
Expand Down Expand Up @@ -35,6 +36,13 @@ def create

private

def redirect_if_blank_phone
return if phone.present?

flash[:error] = t('errors.messages.phone_required')
redirect_to new_user_session_path
end

def confirm_multiple_factors_enabled
return if UserSessionContext.confirmation_context?(context) || phone_enabled?

Expand Down
8 changes: 8 additions & 0 deletions app/controllers/users/two_factor_authentication_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class TwoFactorAuthenticationController < ApplicationController

before_action :check_remember_device_preference
before_action :redirect_to_vendor_outage_if_phone_only, only: [:show]
before_action :redirect_if_blank_phone, only: [:send_code]

def show
service_provider_mfa_requirement_redirect || non_phone_redirect || phone_redirect ||
Expand Down Expand Up @@ -127,6 +128,13 @@ def redirect_to_otp_verification_with_error
)
end

def redirect_if_blank_phone
return if phone_to_deliver_to.present?

flash[:error] = t('errors.messages.phone_required')
redirect_to login_two_factor_options_path
end

def redirect_to_vendor_outage_if_phone_only
return unless VendorStatus.new.all_phone_vendor_outage? &&
phone_enabled? &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
expect(response).to redirect_to login_two_factor_webauthn_url(platform: true)
end

it 'sets phone_id in session when selecting a phone option' do
post :create, params: { two_factor_options_form: { selection: 'sms_0' } }

expect(controller.user_session[:phone_id]).to eq('0')
end

it 'rerenders the page with errors on failure' do
post :create, params: { two_factor_options_form: { selection: 'foo' } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
expect(assigns(:code_value)).to be_nil
end
end

context 'when the user has an invalid phone number in the session' do
it 'redirects to homepage' do
controller.user_session[:phone_id] = 0

get :show, params: { otp_delivery_preference: 'sms' }
expect(response).to redirect_to new_user_session_path
end
end
end

it 'tracks the page visit and context' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,23 @@ def index
otp_make_default_number: nil },
}
end

context 'when selecting specific phone configuration' do
before do
user = create(:user, :signed_up)
sign_in_before_2fa(user)
end
end

it 'redirects to two factor options path with invalid id' do
controller.user_session[:phone_id] = 0

get :send_code, params: {
otp_delivery_selection_form: { otp_delivery_preference: 'voice' },
}

expect(response).to redirect_to(login_two_factor_options_path)
end
end

context 'phone is not confirmed' do
Expand Down