Skip to content

Commit

Permalink
Update form spec expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jan 28, 2025
1 parent d7888ba commit 26e6419
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 133 deletions.
5 changes: 2 additions & 3 deletions app/forms/webauthn_visit_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ def check_params(params)
return unless error

if @platform_authenticator
errors.add error, translate_platform_authenticator_error(error),
type: :"#{translate_platform_authenticator_error(error).split('.').last}"
errors.add error, :invalid, message: translate_platform_authenticator_error(error)
else
errors.add error, translate_error(error), type: :"#{translate_error(error).split('.').last}"
errors.add error, :invalid, message: translate_error(error)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/forms/openid_connect_authorize_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
it 'is unsuccessful and has error messages' do
expect(result.to_h).to eq(
success: false,
errors: { response_type: ['is not included in the list'] },
errors: nil,
error_details: { response_type: { inclusion: true } },
client_id: client_id,
prompt: 'select_account',
Expand Down
4 changes: 2 additions & 2 deletions spec/forms/openid_connect_token_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@

expect(submission.to_h).to include(
success: false,
errors: form.errors.messages,
errors: nil,
error_details: hash_including(*form.errors.attribute_names),
client_id: nil,
user_id: nil,
Expand All @@ -412,7 +412,7 @@

expect(submission.to_h).to include(
success: false,
errors: form.errors.messages,
errors: nil,
error_details: hash_including(:grant_type),
client_id: client_id,
user_id: user.uuid,
Expand Down
12 changes: 5 additions & 7 deletions spec/forms/otp_delivery_selection_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@

context 'when the form is invalid' do
it 'returns false for success? and includes errors' do
errors = {
otp_delivery_preference: ['is not included in the list'],
phone: ['Please fill in this field.'],
}

extra = {
otp_delivery_preference: 'foo',
resend: false,
Expand All @@ -62,8 +57,11 @@

expect(subject.submit(otp_delivery_preference: 'foo').to_h).to include(
success: false,
errors: errors,
error_details: hash_including(*errors.keys),
errors: nil,
error_details: {
otp_delivery_preference: { inclusion: true },
phone: { blank: true },
},
**extra,
)
end
Expand Down
11 changes: 4 additions & 7 deletions spec/forms/password_reset_email_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@
it 'returns hash with properties about the event and the nonexistent user' do
subject = PasswordResetEmailForm.new('invalid')

errors = { email: [t('valid_email.validations.email.invalid')] }

expect(subject.submit.to_h).to include(
success: false,
errors: errors,
error_details: hash_including(*errors.keys),
errors: nil,
error_details: { email: { invalid: true } },
user_id: 'nonexistent-uuid',
confirmed: false,
active_profile: false,
Expand All @@ -53,12 +51,11 @@

it 'returns false and adds errors to the form object when domain is invalid' do
subject = PasswordResetEmailForm.new('test@çà.com')
errors = { email: [t('valid_email.validations.email.invalid')] }

expect(subject.submit.to_h).to include(
success: false,
errors: errors,
error_details: hash_including(*errors.keys),
errors: nil,
error_details: { email: { domain: true } },
user_id: 'nonexistent-uuid',
confirmed: false,
active_profile: false,
Expand Down
5 changes: 2 additions & 3 deletions spec/forms/personal_key_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
context 'when the form is invalid' do
it 'returns FormResponse with success: false' do
user = create(:user, :fully_registered, personal_key: 'code')
errors = { personal_key: ['Incorrect personal key'] }

form = PersonalKeyForm.new(user, 'foo')

expect(form.submit.to_h).to include(
success: false,
errors: errors,
error_details: hash_including(*errors.keys),
errors: nil,
error_details: { personal_key: { personal_key_incorrect: true } },
)
expect(user.encrypted_recovery_code_digest).to_not be_nil
expect(form.personal_key).to be_nil
Expand Down
23 changes: 9 additions & 14 deletions spec/forms/register_user_email_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@
context 'when email is invalid' do
it 'returns false and adds errors to the form object' do
invalid_email = 'invalid_email'
errors = { email: [t('valid_email.validations.email.invalid')] }

extra = {
email_already_exists: false,
Expand All @@ -291,16 +290,14 @@

expect(subject.submit(email: invalid_email, terms_accepted: '1').to_h).to include(
success: false,
errors: errors,
error_details: hash_including(*errors.keys),
errors: nil,
error_details: { email: { invalid: true } },
**extra,
)
expect_delivered_email_count(0)
end

it 'returns false and adds errors to the form object when domain is invalid' do
errors = { email: [t('valid_email.validations.email.invalid')] }

extra = {
email_already_exists: false,
rate_limited: false,
Expand All @@ -310,8 +307,8 @@

expect(subject.submit(email: 'test@çà.com', terms_accepted: '1').to_h).to include(
success: false,
errors: errors,
error_details: hash_including(*errors.keys),
errors: nil,
error_details: { email: { domain: true } },
**extra,
)
expect_delivered_email_count(0)
Expand All @@ -321,7 +318,6 @@
blocked_domain = 'blocked.com'
blocked_email = 'test@' + blocked_domain
email_address = create(:email_address, email: blocked_email)
errors = { email: [t('valid_email.validations.email.invalid')] }
allow(BanDisposableEmailValidator).to receive(:config).and_return([blocked_domain])

extra = {
Expand All @@ -333,8 +329,8 @@

expect(subject.submit(email: blocked_email, terms_accepted: '1').to_h).to include(
success: false,
errors: errors,
error_details: hash_including(*errors.keys),
errors: nil,
error_details: { email: { t('valid_email.validations.email.invalid') => true } },
**extra,
)
expect_delivered_email_count(0)
Expand All @@ -344,13 +340,12 @@
blocked_domain = 'blocked.com'
blocked_email = 'test@sub.' + blocked_domain

errors = { email: [t('valid_email.validations.email.invalid')] }
expect(BanDisposableEmailValidator).to receive(:config).and_return([blocked_domain])

expect(subject.submit(email: blocked_email, terms_accepted: '1').to_h).to include(
success: false,
errors: errors,
error_details: hash_including(*errors.keys),
errors: nil,
error_details: { email: { t('valid_email.validations.email.invalid') => true } },
email_already_exists: false,
rate_limited: false,
user_id: 'anonymous-uuid',
Expand Down Expand Up @@ -389,7 +384,7 @@

expect(result.to_h).to eq(
success: false,
errors: { terms_accepted: [t('errors.registration.terms')] },
errors: nil,
error_details: { terms_accepted: { terms: true } },
email_already_exists: false,
rate_limited: false,
Expand Down
23 changes: 4 additions & 19 deletions spec/forms/reset_password_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
it 'returns a hash with errors' do
expect(result.to_h).to eq(
success: false,
errors: { reset_password_token: ['token_expired'] },
errors: nil,
error_details: { reset_password_token: { token_expired: true } },
user_id: '123',
profile_deactivated: false,
Expand All @@ -46,14 +46,7 @@
it 'returns a hash with errors' do
expect(result.to_h).to eq(
success: false,
errors: {
password:
["Password must be at least #{Devise.password_length.first} characters long"],
password_confirmation: [I18n.t(
'errors.messages.too_short',
count: Devise.password_length.first,
)],
},
errors: nil,
error_details: {
password: { too_short: true },
password_confirmation: { too_short: true },
Expand Down Expand Up @@ -95,15 +88,7 @@
it 'returns a hash with errors' do
expect(result.to_h).to eq(
success: false,
errors: {
password: [
t('errors.attributes.password.too_short.other', count: Devise.password_length.first),
],
password_confirmation: [
t('errors.messages.too_short', count: Devise.password_length.first),
],
reset_password_token: ['token_expired'],
},
errors: nil,
error_details: {
password: { too_short: true },
password_confirmation: { too_short: true },
Expand All @@ -123,7 +108,7 @@
it 'returns a hash with errors' do
expect(result.to_h).to eq(
success: false,
errors: { reset_password_token: ['invalid_token'] },
errors: nil,
error_details: { reset_password_token: { invalid_token: true } },
user_id: nil,
profile_deactivated: false,
Expand Down
4 changes: 2 additions & 2 deletions spec/forms/totp_setup_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@

expect(form.submit.to_h).to include(
success: false,
errors: nil,
error_details: { name: { blank: true } },
errors: { name: [t('errors.messages.blank')] },
)
expect(user.auth_app_configurations.any?).to eq false
end
Expand All @@ -95,8 +95,8 @@

expect(form2.submit.to_h).to include(
success: false,
errors: nil,
error_details: { name: { unique_name: true } },
errors: { name: [t('errors.piv_cac_setup.unique_name')] },
)
end
end
Expand Down
8 changes: 2 additions & 6 deletions spec/forms/two_factor_login_options_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@

context 'when the form is invalid' do
it 'returns false for success? and includes errors' do
errors = {
selection: ['is not included in the list'],
}

extra = {
selection: 'foo',
enabled_mfa_methods_count: 1,
Expand All @@ -40,8 +36,8 @@

expect(subject.submit(selection: 'foo').to_h).to include(
success: false,
errors: errors,
error_details: hash_including(*errors.keys),
errors: nil,
error_details: { selection: { inclusion: true } },
**extra,
)
end
Expand Down
13 changes: 1 addition & 12 deletions spec/forms/update_user_password_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@
let(:password) { 'invalid' }

it 'returns FormResponse with success: false and does not do anything else' do
errors = {
password: [t(
'errors.attributes.password.too_short.other',
count: Devise.password_length.first,
)],
password_confirmation: [I18n.t(
'errors.messages.too_short',
count: Devise.password_length.first,
)],
}

expect(UserProfilesEncryptor).not_to receive(:new)
user.save!

Expand All @@ -43,7 +32,7 @@

expect(result).to include(
success: false,
errors: errors,
errors: nil,
error_details: hash_including(:password, :password_confirmation),
)
end
Expand Down
18 changes: 2 additions & 16 deletions spec/forms/webauthn_setup_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,7 @@

expect(subject.submit(params).to_h).to eq(
success: false,
errors: {
attestation_object: [
I18n.t(
'errors.webauthn_setup.general_error_html',
link_html: I18n.t('errors.webauthn_setup.additional_methods_link'),
),
],
},
errors: nil,
error_details: { attestation_object: { invalid: true } },
**extra_attributes,
)
Expand Down Expand Up @@ -244,14 +237,7 @@

expect(subject.submit(params).to_h).to eq(
success: false,
errors: {
attestation_object: [
I18n.t(
'errors.webauthn_setup.general_error_html',
link_html: I18n.t('errors.webauthn_setup.additional_methods_link'),
),
],
},
errors: nil,
error_details: { attestation_object: { invalid: true } },
**extra_attributes,
)
Expand Down
Loading

0 comments on commit 26e6419

Please sign in to comment.