improvements in the letter verification error messages
This commit is contained in:
@@ -52,7 +52,7 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
|
||||
def verify_lock
|
||||
if current_user.locked?
|
||||
if current_user.try(:locked?)
|
||||
redirect_to account_path, alert: t('verification.alert.lock')
|
||||
end
|
||||
end
|
||||
@@ -97,12 +97,14 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
|
||||
def verify_resident!
|
||||
unless current_user.residence_verified?
|
||||
if current_user && !current_user.residence_verified?
|
||||
redirect_to new_residence_path, alert: t('verification.residence.alert.unconfirmed_residency')
|
||||
end
|
||||
end
|
||||
|
||||
def verify_verified!
|
||||
redirect_to(account_path, notice: t('verification.redirect_notices.already_verified')) if current_user.level_three_verified?
|
||||
if current_user.try(:level_three_verified?)
|
||||
redirect_to(account_path, notice: t('verification.redirect_notices.already_verified'))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class Verification::LetterController < ApplicationController
|
||||
before_action :authenticate_user!, except: [:edit, :update]
|
||||
before_action :check_credentials, only: :update
|
||||
before_action :login_via_form, only: :update
|
||||
|
||||
before_action :verify_resident!, except: :edit
|
||||
before_action :verify_phone!, except: :edit
|
||||
@@ -32,7 +32,7 @@ class Verification::LetterController < ApplicationController
|
||||
current_user.update(verified_at: Time.now)
|
||||
redirect_to account_path, notice: t('verification.letter.update.flash.success')
|
||||
else
|
||||
Lock.increase_tries(@letter.user)
|
||||
Lock.increase_tries(@letter.user) if @letter.user
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
@@ -44,18 +44,17 @@ class Verification::LetterController < ApplicationController
|
||||
end
|
||||
|
||||
def verify_phone!
|
||||
unless current_user.confirmed_phone?
|
||||
if current_user && !current_user.confirmed_phone?
|
||||
redirect_to verified_user_path, alert: t('verification.letter.alert.unconfirmed_code')
|
||||
end
|
||||
end
|
||||
|
||||
def check_credentials
|
||||
user = User.where(email: letter_params[:email]).first
|
||||
def login_via_form
|
||||
user = User.find_by_email(letter_params[:email])
|
||||
if user && user.valid_password?(letter_params[:password])
|
||||
sign_in(user)
|
||||
else
|
||||
redirect_to edit_letter_path, alert: t('devise.failure.invalid', authentication_keys: 'email')
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
@@ -3,9 +3,12 @@ class Verification::Letter
|
||||
|
||||
attr_accessor :user, :verification_code, :email, :password, :verify
|
||||
|
||||
validates :user, presence: true
|
||||
validates :email, presence: true
|
||||
validates :password, presence: true
|
||||
validates :verification_code, presence: true
|
||||
|
||||
validate :correct_code, if: :verify?
|
||||
validate :validate_existing_user
|
||||
validate :validate_correct_code, if: :verify?
|
||||
|
||||
def save
|
||||
valid? &&
|
||||
@@ -16,9 +19,17 @@ class Verification::Letter
|
||||
user.update(letter_requested_at: Time.now, letter_verification_code: generate_verification_code)
|
||||
end
|
||||
|
||||
def correct_code
|
||||
errors.add(:verification_code, I18n.t('verification.letter.errors.incorrect_code')) unless
|
||||
user.letter_verification_code == verification_code
|
||||
def validate_existing_user
|
||||
unless user
|
||||
errors.add(:email, I18n.t('devise.failure.invalid', authentication_keys: 'email'))
|
||||
end
|
||||
end
|
||||
|
||||
def validate_correct_code
|
||||
return if errors.include?(:verification_code)
|
||||
if user.try(:letter_verification_code) != verification_code
|
||||
errors.add(:verification_code, I18n.t('verification.letter.errors.incorrect_code'))
|
||||
end
|
||||
end
|
||||
|
||||
def verify?
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
<div class="medium-6 small-centered column">
|
||||
<%= form_for @letter, url: letter_path, method: :patch do |f| %>
|
||||
<%= render "/shared/errors", resource: @letter %>
|
||||
<%= f.text_field :email, label: t("pages.verify.email") %>
|
||||
<%= f.password_field :password, label: t("pages.verify.password") %>
|
||||
<%= f.text_field :verification_code, label: t("pages.verify.code") %>
|
||||
|
||||
@@ -79,6 +79,9 @@ FactoryGirl.define do
|
||||
|
||||
factory :verification_letter, class: Verification::Letter do
|
||||
user
|
||||
email 'user@madrid.es'
|
||||
password '1234'
|
||||
verification_code '5555'
|
||||
end
|
||||
|
||||
factory :lock do
|
||||
|
||||
@@ -96,7 +96,7 @@ feature 'Verify Letter' do
|
||||
fill_in "verification_letter_password", with: user.password
|
||||
click_button "Verify my account"
|
||||
|
||||
expect(page).to have_content error_message
|
||||
expect(page).to have_content "can't be blank"
|
||||
end
|
||||
|
||||
scenario '6 tries allowed' do
|
||||
|
||||
@@ -21,14 +21,10 @@ describe 'Verification::Letter' do
|
||||
|
||||
describe "save" do
|
||||
|
||||
before(:each) do
|
||||
letter = Verification::Letter.new(user: user)
|
||||
letter.save
|
||||
user.reload
|
||||
end
|
||||
|
||||
it "should update letter_requested" do
|
||||
expect(user.letter_requested_at).to be
|
||||
letter = build(:verification_letter)
|
||||
letter.save
|
||||
expect(letter.user.letter_requested_at).to be
|
||||
end
|
||||
|
||||
end
|
||||
@@ -39,7 +35,7 @@ describe 'Verification::Letter' do
|
||||
|
||||
it "incorrect code" do
|
||||
letter.user.update(letter_verification_code: "123456")
|
||||
letter.verification_code = nil
|
||||
letter.verification_code = "5555"
|
||||
|
||||
expect(letter.valid?).to eq(false)
|
||||
expect(letter.errors[:verification_code].first).to eq("Incorrect confirmation code")
|
||||
|
||||
Reference in New Issue
Block a user