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

Add errors to FormResponse in PersonalKeyForm #1450

Merged
merged 1 commit into from
May 24, 2017
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
4 changes: 3 additions & 1 deletion app/forms/personal_key_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ class PersonalKeyForm
attr_accessor :personal_key
attr_reader :user

validate :check_personal_key

def initialize(user, personal_key = nil)
@user = user
@personal_key = normalize_personal_key(personal_key)
end

def submit
@success = valid_personal_key?
@success = valid?

if success
UpdateUser.new(user: user, attributes: { personal_key: nil }).call
Expand Down
4 changes: 2 additions & 2 deletions app/forms/reactivate_account_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ def validate_password_reset_profile
end

def validate_password
return if password.blank? || valid_password?
return if valid_password?
errors.add :password, :password_incorrect
end

def validate_personal_key
return if personal_key.blank? || (valid_personal_key? && personal_key_decrypts?)
return check_personal_key if personal_key_decrypts?
errors.add :personal_key, :personal_key_incorrect
end

Expand Down
18 changes: 11 additions & 7 deletions app/services/personal_key_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
class PersonalKeyFormatter
def regexp
char_count = RandomPhrase::WORD_LENGTH
word_count = Figaro.env.recovery_code_length.to_i
valid_char = '[a-zA-Z0-9]'
regexp =
"(?:#{valid_char}{#{char_count}}([\s-])?){#{word_count - 1}}#{valid_char}{#{char_count}}"
CHAR_COUNT = RandomPhrase::WORD_LENGTH
WORD_COUNT = Figaro.env.recovery_code_length.to_i
VALID_CHAR = '[a-zA-Z0-9]'.freeze
VALID_WORD = "#{VALID_CHAR}{#{CHAR_COUNT}}".freeze
REGEXP = "(?:#{VALID_WORD}([\s-])?){#{WORD_COUNT - 1}}#{VALID_WORD}".freeze

regexp
def self.regexp
/\A#{REGEXP}\Z/
end

def self.regexp_string
REGEXP
end
end
14 changes: 7 additions & 7 deletions app/validators/personal_key_validator.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module PersonalKeyValidator
extend ActiveSupport::Concern

included do
validate :valid_personal_key?
end

private

def normalize_personal_key(personal_key = nil)
return nil if personal_key.blank?
personal_key_generator.normalize(personal_key)
end

def valid_personal_key?
return false unless personal_key =~ /\A#{PersonalKeyFormatter.new.regexp}\Z/
personal_key_generator.verify(personal_key)
def check_personal_key
return if personal_key_format_matches? && personal_key_generator.verify(personal_key)
errors.add :personal_key, :personal_key_incorrect
end

def personal_key_format_matches?
personal_key =~ PersonalKeyFormatter.regexp
end

def personal_key_generator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
autocomplete='off'
class='col-12 mb4 border-dashed field monospace personal-key caps'
name="personal-key"
pattern="^#{PersonalKeyFormatter.new.regexp}$"
pattern="^#{PersonalKeyFormatter.regexp_string}$"
required=true
spellcheck='false'
type='text')
Expand Down
5 changes: 3 additions & 2 deletions spec/forms/personal_key_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

context 'when the form is invalid' do
it 'returns FormResponse with success: false' do
user = build_stubbed(:user, :signed_up, personal_key: 'code')
user = create(:user, :signed_up, personal_key: 'code')
errors = { personal_key: ['Incorrect personal key'] }

form = PersonalKeyForm.new(user, 'foo')
result = instance_double(FormResponse)
extra = { multi_factor_auth_method: 'personal key' }

expect(FormResponse).to receive(:new).
with(success: false, errors: {}, extra: extra).and_return(result)
with(success: false, errors: errors, extra: extra).and_return(result)
expect(form.submit).to eq result
expect(user.personal_key).to_not be_nil
expect(form.personal_key).to be_nil
Expand Down
2 changes: 1 addition & 1 deletion spec/forms/reactivate_account_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

it 'is invalid' do
expect(subject).to_not be_valid
expect(subject.errors[:personal_key]).to eq [t('errors.messages.blank')]
expect(subject.errors[:personal_key]).to include t('errors.messages.blank')
expect(subject.errors[:password]).to eq [t('errors.messages.blank')]
end
end
Expand Down