diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d5d351038..03b175e17 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/verification/letter_controller.rb b/app/controllers/verification/letter_controller.rb index 5f0513556..8b8a19f55 100644 --- a/app/controllers/verification/letter_controller.rb +++ b/app/controllers/verification/letter_controller.rb @@ -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 \ No newline at end of file + +end diff --git a/app/models/verification/letter.rb b/app/models/verification/letter.rb index 2124e1664..897960a72 100644 --- a/app/models/verification/letter.rb +++ b/app/models/verification/letter.rb @@ -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? diff --git a/app/views/verification/letter/edit.html.erb b/app/views/verification/letter/edit.html.erb index 3f2084d26..bf847da5a 100644 --- a/app/views/verification/letter/edit.html.erb +++ b/app/views/verification/letter/edit.html.erb @@ -11,7 +11,6 @@
<%= 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") %> @@ -20,4 +19,4 @@ <% end %>
- \ No newline at end of file + diff --git a/spec/factories.rb b/spec/factories.rb index 83bc79af1..a3784678e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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 diff --git a/spec/features/verification/letter_spec.rb b/spec/features/verification/letter_spec.rb index 656f6708e..00baef12d 100644 --- a/spec/features/verification/letter_spec.rb +++ b/spec/features/verification/letter_spec.rb @@ -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 diff --git a/spec/models/letter_spec.rb b/spec/models/letter_spec.rb index 3b774b618..68d6afe5b 100644 --- a/spec/models/letter_spec.rb +++ b/spec/models/letter_spec.rb @@ -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")