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
2 changes: 2 additions & 0 deletions app/controllers/users/phones_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class PhonesController < ApplicationController
def add
user_session[:phone_id] = nil
@new_phone_form = NewPhoneForm.new(user: current_user, analytics: analytics)
analytics.add_phone_setup_visit
end

def create
@new_phone_form = NewPhoneForm.new(user: current_user, analytics: analytics)
result = @new_phone_form.submit(user_params)
analytics.multi_factor_auth_phone_setup(**result.to_h)
if result.success?
confirm_phone
elsif recoverable_recaptcha_error?(result)
Expand Down
7 changes: 7 additions & 0 deletions app/services/analytics_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2817,6 +2817,13 @@ def telephony_otp_sent(
)
end

# Tracks When users visit the add phone page
def add_phone_setup_visit
track_event(
'Phone Setup Visited',
)
end

# @param [Boolean] success
# @param [Hash] errors
# @param [String] sign_up_mfa_priority_bucket
Expand Down
133 changes: 133 additions & 0 deletions spec/controllers/users/phones_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
expect(response).to render_template(:add)
expect(response.request.flash[:alert]).to be_nil
end

it 'tracks analytics' do
expect(@analytics).to receive(:track_event).
with('Phone Setup Visited')
get :add
end
end

context 'user exceeds phone number limit' do
Expand Down Expand Up @@ -104,5 +110,132 @@
expect(response).to render_template('users/phone_setup/spam_protection')
end
end

context 'invalid number' do
it 'tracks an event when the number is invalid' do
sign_in(user)

stub_analytics
result = {
success: false,
errors: {
phone: [
t('errors.messages.improbable_phone'),
t(
'two_factor_authentication.otp_delivery_preference.voice_unsupported',
location: '',
),
],
},
error_details: {
phone: [
:improbable_phone,
t(
'two_factor_authentication.otp_delivery_preference.voice_unsupported',
location: '',
),
],
},
otp_delivery_preference: 'sms',
area_code: nil,
carrier: 'Test Mobile Carrier',
country_code: nil,
phone_type: :mobile,
types: [],
pii_like_keypaths: [[:errors, :phone], [:error_details, :phone]],
}

expect(@analytics).to receive(:track_event).
with('Multi-Factor Authentication: phone setup', result)

post :create, params: {
new_phone_form: {
phone: '703-555-010',
international_code: 'US',
},
}

expect(response).to render_template(:add)
end
end

context 'with SMS' do
it 'prompts to confirm the number' do
sign_in(user)

stub_analytics

result = {
success: true,
errors: {},
otp_delivery_preference: 'sms',
area_code: '703',
carrier: 'Test Mobile Carrier',
country_code: 'US',
phone_type: :mobile,
types: [:fixed_or_mobile],
pii_like_keypaths: [[:errors, :phone], [:error_details, :phone]],
}

expect(@analytics).to receive(:track_event).
with('Multi-Factor Authentication: phone setup', result)

post(
:create,
params: {
new_phone_form: { phone: '703-555-0100',
international_code: 'US' },
},
)

expect(response).to redirect_to(
otp_send_path(
otp_delivery_selection_form: { otp_delivery_preference: 'sms',
otp_make_default_number: false },
),
)

expect(subject.user_session[:context]).to eq 'confirmation'
end
end

context 'without selection' do
it 'prompts to confirm via SMS by default' do
sign_in(user)

stub_analytics
result = {
success: true,
errors: {},
otp_delivery_preference: 'sms',
area_code: '703',
carrier: 'Test Mobile Carrier',
country_code: 'US',
phone_type: :mobile,
types: [:fixed_or_mobile],
pii_like_keypaths: [[:errors, :phone], [:error_details, :phone]],
}

expect(@analytics).to receive(:track_event).
with('Multi-Factor Authentication: phone setup', result)

patch(
:create,
params: {
new_phone_form: { phone: '703-555-0100',
international_code: 'US' },
},
)

expect(response).to redirect_to(
otp_send_path(
otp_delivery_selection_form: { otp_delivery_preference: 'sms',
otp_make_default_number: false },
),
)

expect(subject.user_session[:context]).to eq 'confirmation'
end
end
end
end