vendredi 1 janvier 2016

Expected response to be a

Hi am having this failure

19:44:39 - INFO - Running: test/integration/password_resets_test.rb
Started with run options --seed 54531

DEPRECATION WARNING: You attempted to assign a value which is not explicitly `true` or `false` to a boolean column. Currently this value casts to `false`. This will change to match Ruby's semantics, and will cast to `true` in Rails 5. If you would like to maintain the current behavior, you should explicitly handle the values you would like cast to `false`. (called from create_reset_digest at /home/ubuntu/workspace/sample_app/app/models/user.rb:52)
 FAIL["test_reset_passwords", PasswordResetsTest, 2.098880915902555]
 test_reset_passwords#PasswordResetsTest (2.10s)
        Expected response to be a <redirect>, but was <200>
        test/integration/password_resets_test.rb:31:in `block in <class:PasswordResetsTest>'

And i can't figure out what the problem is, i would appreciate some help!

user.rb

class User < ActiveRecord::Base
    attr_accessor :remember_token, :activation_token, :reset_token
    before_save   :downcase_email
    before_create :create_activation_digest
    validates :name,  presence:true,  length: {maximum: 50 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\-.]+\.[a-z]+\z/i
    validates :email, presence:true,  length: {maximum: 255 },
                      format: { with:  VALID_EMAIL_REGEX },
                      uniqueness: { case_sensitive: false}
    has_secure_password
    validates :password,  length: {minimum: 6 }, allow_blank: true
    def User.digest(string)
        cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                      BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)                                                 
    end
    #devuelve un token cualquiera
    def User.new_token
        SecureRandom.urlsafe_base64
    end
    #recuerda un usuario 
    def remember
        self.remember_token = User.new_token
        update_attribute(:remember_digest, User.digest(remember_token))
    end
    def forget
        update_attribute(:remember_digest, nil)
    end

    def authenticated?(attribute, token)
        digest = send("#{attribute}_digest") 
        return false if digest.nil?
        BCrypt::Password.new(digest).is_password?(token)
    end
     #activa una cuenta
     def activate
        update_attribute(:activated,    true)
        update_attribute(:activated_at, Time.zone.now) 
     end

    def send_activation_email
        UserMailer.account_activation(self).deliver_now
    end

    def send_password_reset_email
        UserMailer.password_reset(self).deliver_now
    end


    def create_reset_digest
      self.reset_token = User.new_token
      update_attribute(:reset_digest,  User.digest(reset_token))
      update_attribute(:reset_sent_at, Time.zone.now)
    end
    #devuelve true si el password expiro hace mas de 2 horas
    def password_reset_expired?
        reset_sent_at < 2.hours.ago
    end
    private 

    #convierte el mail a minuscula 

    def downcase_email
        self.email = email.downcase 
    end

    def create_activation_digest
       #crea el token y digest 
       self.activation_token  = User.new_token
       self.activation_digest = User.digest(activation_token) 
    end
end     

and this is the password resets controller

class PasswordResetsController < ApplicationController

  before_action :get_user,         only: [:edit, :update]
  before_action :valid_user,       only: [:edit, :update]
  before_action :check_expiration, only: [:edit, :update]

  def new
  end

  def create
    @user = User.find_by(email: params[:password_reset][:email].downcase) 
   if @user 
     @user.create_reset_digest
     @user.send_password_reset_email
     flash[:info] = "Email enviado con intrucciones para resetear el password"
   redirect_to root_url 
   else
     flash.now[:danger] = "Email no encontrado"  
     render 'new'
   end
  end
  def edit
  end

  def update
    if password_blank?
      flash.now[:danger]= "No puedes dejar en blanco las contraseñas"
      render 'edit'
    elsif @user.update_attributes(user_params)
      log_in @user
      flash[:success] = "La contraseña ha sido cambiada"
      redirect_to @user
    else
      render 'edit'
    end
  end

  private

  def user_params
    params.require(:user).permit(:password, :password_confirmation)
  end

  def get_user
      @user = User.find_by(email: params[:email])
  end

  def valid_user
      unless (@user && @user.activated? &&
              @user.authenticated?(:reset, params[:id]))
        redirect_to root_url
    end 
  end
    # Checks expiration of reset token.
    def check_expiration
      if @user.password_reset_expired?
        flash[:danger] = "Password reset has expired."
        redirect_to new_password_reset_url
      end 
   end

   def password_blank?
     (params[:user][:password]).blank?
   end
end

so basically it should redirect but it is not doing that, so i don't what is happening, thanks in advance

password_reset_test

require 'test_helper'

class PasswordResetsTest < ActionDispatch::IntegrationTest

  def setup
  ActionMailer::Base.deliveries.clear
  @user = users(:michael)
  end

  test "reset passwords" do
  get new_password_reset_path
  assert_template 'password_resets/new'
  #mail invalido
  post password_resets_path, password_reset: { email: '' }
  assert_not flash.empty?
  assert_template 'password_resets/new'
  #mail valido
  post password_resets_path, password_reset: { email: @user.email }
  assert_not_equal @user.reset_digest, @user.reload.reset_digest
  assert_equal 1, ActionMailer::Base.deliveries.size
  assert_not flash.empty?
  assert_redirected_to root_url
  #Formulario de resteo de contraseña
  user = assigns(:user)
  #email equivocado
  get edit_password_reset_path(user.reset_token, email: "")
  assert_redirected_to root_url
  #USUARIO INACTIVO
  user.toggle!(:activated)
  get edit_password_reset_path(user.reset_token, email: user.email) 
  assert_redirected_to root_url
  user.toggle!(:activated)
  #mail correcto , mal token
  get edit_password_reset_path('wrong token', email: user.email)
  assert_redirected_to root_url
  #mail correcto , token correcto
  get edit_password_reset_path(user.reset_token, email: user.email) 
  assert_template 'password_resets/edit'
  assert_select "input[name=email][type=hidden][value=?]", user.email

  patch password_reset_path(user.reset_token),
        email: user.email,
        user: { password:              "foobaz",
                password_confirmation: "barquux" }
  assert_select 'div#error_explanation'  
     # Blank password
  patch password_reset_path(user.reset_token),
        email: user.email,
        user: { password:              "  ",
                password_confirmation: "foobar" }
  assert_not flash.empty?
  assert_template 'password_resets/edit'

  # Valid password & confirmation
  patch password_reset_path(user.reset_token),
        email: user.email,
        user: { password:              "foobaz",
                password_confirmation: "foobaz" }
  assert is_logged_in?
  assert_not flash.empty?
  assert_redirected_to user
  end
end

Aucun commentaire:

Enregistrer un commentaire