gpt4 book ai didi

ruby-on-rails - rails 功能测试中的身份验证问题

转载 作者:太空宇宙 更新时间:2023-11-03 18:30:29 25 4
gpt4 key购买 nike

我正在测试这个 Controller :

class SessionsController < ApplicationController
def new
end

def create
@current_user = User.find_by_login_and_password(
params[:login], params[:password]
)

if @current_user
session[:user_id] = @current_user.id
if session[:return_to]
redirect_to session[:return_to]
session[:return_to] = nil
else
redirect_to stories_path
end
else
render :action => 'new'
end
end

def destroy
session[:user_id] = @current_user = nil
end

end

使用这个固定装置 (users.yml):

patrick:
login: patrick
password: sekrit
name: Patrick Lenz
email: patrick@limited-overload.de

john:
login: john
password: gh752px
name: John Doe
email: john@doe.com

和这个功能测试:

require 'test_helper'

class SessionsControllerTest < ActionController::TestCase
def test_should_show_login_form
get :new
assert_response :success
assert_template 'new'
assert_select 'form p', 4
end

def test_should_perform_user_login
post :create, :login =>'patrick', :password => 'sekrit'
assert_redirected_to stories_path
assert_equal users(:patrick).id, session[:user_id]
assert_equal users(:patrick), assigns(:current_user)
end
end

但是,我得到了这个失败:

test_should_perform_user_login(SessionsControllerTest) [/home/ygamretuta/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-2.3.9/lib/action_controller/test_case.rb:119]:
Expected response to be a <:redirect>, but was <200>.
Expected block to return true value

登录逻辑正常,但我的测试失败,我到 stories_path 的路径:

story_votes GET    /stories/:story_id/votes(.:format)          {:controller=>"votes", :action=>"index"}
POST /stories/:story_id/votes(.:format) {:controller=>"votes", :action=>"create"}
new_story_vote GET /stories/:story_id/votes/new(.:format) {:controller=>"votes", :action=>"new"}
edit_story_vote GET /stories/:story_id/votes/:id/edit(.:format) {:controller=>"votes", :action=>"edit"}
story_vote GET /stories/:story_id/votes/:id(.:format) {:controller=>"votes", :action=>"show"}
PUT /stories/:story_id/votes/:id(.:format) {:controller=>"votes", :action=>"update"}
DELETE /stories/:story_id/votes/:id(.:format) {:controller=>"votes", :action=>"destroy"}
stories GET /stories(.:format) {:controller=>"stories", :action=>"index"}
POST /stories(.:format) {:controller=>"stories", :action=>"create"}
new_story GET /stories/new(.:format) {:controller=>"stories", :action=>"new"}
edit_story GET /stories/:id/edit(.:format) {:controller=>"stories", :action=>"edit"}
story GET /stories/:id(.:format) {:controller=>"stories", :action=>"show"}
PUT /stories/:id(.:format) {:controller=>"stories", :action=>"update"}

我可能遗漏了什么?

编辑:这是我的用户表的结构(它只对书籍示例使用纯文本密码)

CREATE TABLE "users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"login" varchar(255),
"password" varchar(255),
"name" varchar(255),
"email" varchar(255),
"created_at" datetime,
"updated_at" datetime
)

最佳答案

Ygam,我 99% 确定问题是密码不匹配。您使用的是加盐/加密密码吗?如果是这种情况,请检查您的灯具是否具有纯文本密码。

我非常有信心是这种情况,因为您正在发帖并获得 200,而 create 中返回 200 的唯一路径是用户/密码配对验证失败。

如果你使用加盐密码,你可能需要在你的设备中做这样的事情

<% SALT = "NaCl" unless defined?(SALT) %>
one:
name: dave
hashed_password: <%= User.encrypt_password('secret', SALT) %>
salt: <%= SALT %>

two:
name: MyString
hashed_password: MyString
salt: MyString

关于ruby-on-rails - rails 功能测试中的身份验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4997966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com