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
52 changes: 25 additions & 27 deletions spec/controllers/account_reset/cancel_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,49 @@
it 'logs a good token to the analytics' do
token = create_account_reset_request_for(user)
session[:cancel_token] = token

stub_analytics
analytics_hash = {

post :create

expect(@analytics).to have_logged_event(
'Account Reset: cancel',
success: true,
errors: {},
user_id: user.uuid,
message_id: 'fake-message-id',
request_id: 'fake-message-request-id',
}

post :create

expect(@analytics).to have_logged_event('Account Reset: cancel', analytics_hash)
)
end

it 'logs a bad token to the analytics' do
stub_analytics
analytics_hash = {
session[:cancel_token] = 'FOO'

post :create

expect(@analytics).to have_logged_event(
'Account Reset: cancel',
success: false,
errors: { token: [t('errors.account_reset.cancel_token_invalid', app_name: APP_NAME)] },
error_details: {
token: { cancel_token_invalid: true },
},
user_id: 'anonymous-uuid',
}

session[:cancel_token] = 'FOO'

post :create

expect(@analytics).to have_logged_event('Account Reset: cancel', analytics_hash)
)
end

it 'logs a missing token to the analytics' do
stub_analytics
analytics_hash = {

post :create

expect(@analytics).to have_logged_event(
'Account Reset: cancel',
success: false,
errors: { token: [t('errors.account_reset.cancel_token_missing', app_name: APP_NAME)] },
error_details: { token: { blank: true } },
user_id: 'anonymous-uuid',
}

post :create

expect(@analytics).to have_logged_event('Account Reset: cancel', analytics_hash)
)
end

it 'redirects to the root without a flash message when the token is missing or invalid' do
Expand Down Expand Up @@ -87,18 +85,18 @@
describe '#show' do
it 'redirects to root if the token does not match one in the DB' do
stub_analytics
properties = {

get :show, params: { token: 'FOO' }

expect(@analytics).to have_logged_event(
'Account Reset: cancel token validation',
user_id: 'anonymous-uuid',
success: false,
errors: { token: [t('errors.account_reset.cancel_token_invalid', app_name: APP_NAME)] },
error_details: {
token: { cancel_token_invalid: true },
},
}

get :show, params: { token: 'FOO' }

expect(@analytics).to have_logged_event('Account Reset: cancel token validation', properties)
)
expect(response).to redirect_to(root_url)
expect(flash[:error]).to eq t('errors.account_reset.cancel_token_invalid', app_name: APP_NAME)
end
Expand Down
79 changes: 37 additions & 42 deletions spec/controllers/account_reset/delete_account_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
create_list(:webauthn_configuration, 2, user: user)
create_account_reset_request_for(user)
grant_request(user)

session[:granted_token] = AccountResetRequest.first.granted_token
properties = {

delete :delete

expect(@analytics).to have_logged_event(
'Account Reset: delete',
user_id: user.uuid,
success: true,
errors: {},
Expand All @@ -29,47 +32,42 @@
},
account_age_in_days: 0,
account_confirmed_at: user.confirmed_at,
}

delete :delete

expect(@analytics).to have_logged_event('Account Reset: delete', properties)
)
expect(response).to redirect_to account_reset_confirm_delete_account_url
end

it 'redirects to root if the token does not match one in the DB' do
session[:granted_token] = 'foo'
properties = {

delete :delete

expect(@analytics).to have_logged_event(
'Account Reset: delete',
user_id: 'anonymous-uuid',
success: false,
errors: invalid_token_error,
error_details: { token: { granted_token_invalid: true } },
mfa_method_counts: {},
account_age_in_days: 0,
account_confirmed_at: kind_of(Time),
}

delete :delete

expect(@analytics).to have_logged_event('Account Reset: delete', properties)
)
expect(response).to redirect_to(root_url)
expect(flash[:error]).to eq(invalid_token_message)
end

it 'displays a flash and redirects to root if the token is missing' do
properties = {
delete :delete

expect(@analytics).to have_logged_event(
'Account Reset: delete',
user_id: 'anonymous-uuid',
success: false,
errors: { token: [t('errors.account_reset.granted_token_missing', app_name: APP_NAME)] },
error_details: { token: { blank: true } },
mfa_method_counts: {},
account_age_in_days: 0,
account_confirmed_at: kind_of(Time),
}

delete :delete

expect(@analytics).to have_logged_event('Account Reset: delete', properties)
)
expect(response).to redirect_to(root_url)
expect(flash[:error]).to eq t(
'errors.account_reset.granted_token_missing',
Expand All @@ -82,22 +80,21 @@
create_account_reset_request_for(user)
grant_request(user)

properties = {
travel_to(Time.zone.now + 2.days) do
session[:granted_token] = AccountResetRequest.first.granted_token
delete :delete
end

expect(@analytics).to have_logged_event(
'Account Reset: delete',
user_id: user.uuid,
success: false,
errors: { token: [t('errors.account_reset.granted_token_expired', app_name: APP_NAME)] },
error_details: { token: { granted_token_expired: true } },
mfa_method_counts: {},
account_age_in_days: 2,
account_confirmed_at: kind_of(Time),
}

travel_to(Time.zone.now + 2.days) do
session[:granted_token] = AccountResetRequest.first.granted_token
delete :delete
end

expect(@analytics).to have_logged_event('Account Reset: delete', properties)
)
expect(response).to redirect_to(root_url)
expect(flash[:error]).to eq(
t('errors.account_reset.granted_token_expired', app_name: APP_NAME),
Expand All @@ -107,16 +104,15 @@

describe '#show' do
it 'redirects to root if the token does not match one in the DB' do
properties = {
get :show, params: { token: 'FOO' }

expect(@analytics).to have_logged_event(
'Account Reset: granted token validation',
user_id: 'anonymous-uuid',
success: false,
errors: invalid_token_error,
error_details: { token: { granted_token_invalid: true } },
}

get :show, params: { token: 'FOO' }

expect(@analytics).to have_logged_event('Account Reset: granted token validation', properties)
)
expect(response).to redirect_to(root_url)
expect(flash[:error]).to eq(invalid_token_message)
end
Expand All @@ -126,18 +122,17 @@
create_account_reset_request_for(user)
grant_request(user)

properties = {
user_id: user.uuid,
success: false,
errors: { token: [t('errors.account_reset.granted_token_expired', app_name: APP_NAME)] },
error_details: { token: { granted_token_expired: true } },
}

travel_to(Time.zone.now + 2.days) do
get :show, params: { token: AccountResetRequest.first.granted_token }
end

expect(@analytics).to have_logged_event('Account Reset: granted token validation', properties)
expect(@analytics).to have_logged_event(
'Account Reset: granted token validation',
user_id: user.uuid,
success: false,
errors: { token: [t('errors.account_reset.granted_token_expired', app_name: APP_NAME)] },
error_details: { token: { granted_token_expired: true } },
)
expect(response).to redirect_to(root_url)
expect(flash[:error]).to eq(
t('errors.account_reset.granted_token_expired', app_name: APP_NAME),
Expand Down
39 changes: 18 additions & 21 deletions spec/controllers/account_reset/request_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,30 @@
describe '#create' do
it 'logs totp user in the analytics' do
stub_sign_in_before_2fa(user)

stub_analytics
attributes = {

post :create

expect(@analytics).to have_logged_event(
'Account Reset: request',
success: true,
sms_phone: false,
totp: true,
piv_cac: false,
email_addresses: 1,
errors: {},
}

post :create

expect(@analytics).to have_logged_event('Account Reset: request', attributes)
)
end

it 'logs sms user in the analytics' do
user = create(:user, :fully_registered)
stub_sign_in_before_2fa(user)

stub_analytics
attributes = {

post :create

expect(@analytics).to have_logged_event(
'Account Reset: request',
success: true,
sms_phone: true,
totp: false,
Expand All @@ -126,30 +128,25 @@
request_id: 'fake-message-request-id',
message_id: 'fake-message-id',
errors: {},
}

post :create

expect(@analytics).to have_logged_event('Account Reset: request', attributes)
)
end

it 'logs PIV/CAC user in the analytics' do
user = create(:user, :with_piv_or_cac, :with_backup_code)
stub_sign_in_before_2fa(user)

stub_analytics
attributes = {

post :create

expect(@analytics).to have_logged_event(
'Account Reset: request',
success: true,
sms_phone: false,
totp: false,
piv_cac: true,
email_addresses: 1,
errors: {},
}

post :create

expect(@analytics).to have_logged_event('Account Reset: request', attributes)
)
end

it 'redirects to root if user not signed in' do
Expand Down
10 changes: 5 additions & 5 deletions spec/controllers/accounts/personal_keys_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
it 'tracks CSRF errors' do
stub_sign_in
stub_analytics
analytics_hash = {
controller: 'accounts/personal_keys#create',
user_signed_in: true,
}
allow(controller).to receive(:create).and_raise(ActionController::InvalidAuthenticityToken)

post :create

expect(@analytics).to have_logged_event('Invalid Authenticity Token', analytics_hash)
expect(@analytics).to have_logged_event(
'Invalid Authenticity Token',
controller: 'accounts/personal_keys#create',
user_signed_in: true,
)
expect(response).to redirect_to new_user_session_url
expect(flash[:error]).to eq t('errors.general')
end
Expand Down
17 changes: 11 additions & 6 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ def index
it 'tracks the InvalidAuthenticityToken event and does not sign the user out' do
sign_in_as_user
expect(subject.current_user).to be_present

stub_analytics
event_properties = { controller: 'anonymous#index', user_signed_in: true }

get :index

expect(@analytics).to have_logged_event('Invalid Authenticity Token', event_properties)
expect(@analytics).to have_logged_event(
'Invalid Authenticity Token',
controller: 'anonymous#index',
user_signed_in: true,
)
expect(flash[:error]).to eq t('errors.general')
expect(response).to redirect_to(root_url)
expect(subject.current_user).to be_present
Expand Down Expand Up @@ -145,13 +147,16 @@ def index
request.env['HTTP_REFERER'] = referer
sign_in_as_user
expect(subject.current_user).to be_present

stub_analytics
event_properties = { controller: 'anonymous#index', user_signed_in: true, referer: referer }

get :index

expect(@analytics).to have_logged_event('Unsafe Redirect', event_properties)
expect(@analytics).to have_logged_event(
'Unsafe Redirect',
controller: 'anonymous#index',
user_signed_in: true,
referer:,
)
expect(flash[:error]).to eq t('errors.general')
expect(response).to redirect_to(root_url)
expect(subject.current_user).to be_present
Expand Down
Loading