我有这样的测试:
class LockableFlowTest < ActionDispatch::IntegrationTest
context 'A user' do
setup do
@organization = Factory(:organization)
@user = Factory(:user, :organization => @organization)
end
should "login after 4 attempts" do
4.times do
post_via_redirect '/users/sign_in', 'user[username]' => @user.username, 'user[password]' => "bad_password"
assert_equal '/users/sign_in', path
assert_equal 'Invalid email or password.', flash[:alert]
end
post_via_redirect '/users/sign_in', 'user[username]' => @user.username, 'user[password]' => "password"
assert_equal "/registrations/#{@user.id}/edit", path
assert_nil flash[:alert]
end
它不起作用,但应用程序没问题。我想测试登录应用程序的尝试。4 次尝试后应该可以登录。
代码控制器:
class SessionsController < Devise::SessionsController
after_filter :log_failed_login, :only => :new
private
def log_failed_login
if request.filtered_parameters["user"]
user = User.find_by_username(request.filtered_parameters["user"]["username"])
if user
if user.first_failed_attempt.nil?
user.first_failed_attempt = Time.now
else
if user.first_failed_attempt + 15.minutes <= Time.now
user.failed_attempts = 1
user.first_failed_attempt = Time.now
end
end
user.save
end
end
end
def failed_login?
(options = env["warden.options"]) && options[:action] == "unauthenticated"
end
end