0

嗨,这个失败

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>'

而且我无法弄清楚问题是什么,我将不胜感激!

用户.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     

这是密码重置控制器

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

所以基本上它应该重定向但它没有这样做,所以我不知道发生了什么,在此先感谢

密码重置测试

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

add_reset_to_users

class AddResetToUsers < ActiveRecord::Migration
  def change
    add_column :users, :reset_digest, :string
    add_column :users, :reset_sent_at, :datetime
  end
end

另一个错误日志

20:54:08 - INFO - Running: test/integration/password_resets_test.rb
Started with run options --seed 32975

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, 1.7121580690145493]
 test_reset_passwords#PasswordResetsTest (1.71s)
        expecting <"password_resets/edit"> but rendering with <[]>
        test/integration/password_resets_test.rb:38:in `block in <class:PasswordResetsTest>'

  1/1: [===================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 1.71599s
1 tests, 11 assertions, 1 failures, 0 errors, 0 skips

架构.rb

# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160101004150) do

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at",                        null: false
    t.datetime "updated_at",                        null: false
    t.string   "password_digest"
    t.string   "remember_digest"
    t.boolean  "admin",             default: false
    t.string   "activation_digest"
    t.boolean  "activated",         default: false
    t.datetime "activated_at"
    t.string   "reset_digest"
    t.datetime "reset_sent_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true

end
4

2 回答 2

1

我昏迷了,activated: true,但真正的文件没有它-.-。这是users.yml

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>

archer:
  name: Sterling Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

lana:
  name: Lana Kane
  email: hands@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

mallory:
  name: Mallory Archer
  email: boss@example.gov
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>

<% 30.times do |n| %>
user_<%= n %>:
  name:  <%= "User #{n}" %>
  email: <%= "user-#{n}@example.com" %>
  password_digest: <%= User.digest('password') %>
  activated: true
  activated_at: <%= Time.zone.now %>
<% end %>
于 2016-01-02T00:33:28.047 回答
0

您在此处的编辑操作中没有任何内容

class PasswordResetsController < ApplicationController
  ...
  def edit
  end
  ...
end

并期望在您的测试中重定向到 root_url

get edit_password_reset_path(user.reset_token, email: "")
assert_redirected_to root_url

我相信这就是你得到当前错误的原因

预期响应为重定向,但为 200

如果您希望它重定向到 root_url 只需添加一个 redirect_to 但我认为您尝试执行的操作可能会丢失更多内容。无论如何,这是 redirect_to 的答案

def edit
  redirect_to root_url
end
于 2016-01-01T20:35:02.770 回答