新增user controller测试

This commit is contained in:
刘惊坤 2016-01-04 13:54:35 +08:00
parent 7d2162ea10
commit 6effa4a887
4 changed files with 67 additions and 3 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

BIN
app/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -7,7 +7,7 @@ class UsersController < ApplicationController
end end
def register_confirm def register_confirm
@user = User.new params.require(:user).permit(:username,:em ail,:password,:password_confirmation) @user = User.new params.require(:user).permit(:username,:email,:password,:password_confirmation)
if @user.save if @user.save
to_login @user to_login @user
redirect_to root_path redirect_to root_path
@ -18,7 +18,7 @@ class UsersController < ApplicationController
end end
def login def login
return redirect_to(login_path(from: referer)) unless pa rams[:from].present? return redirect_to(login_path(from: referer)) unless params[:from].present?
@user = User.new @user = User.new
render 'login', layout: 'register' render 'login', layout: 'register'
end end
@ -28,7 +28,7 @@ class UsersController < ApplicationController
if @user && @user.check_password(params[:user][:password]) if @user && @user.check_password(params[:user][:password])
to_login @user to_login @user
@user.update_attribute :last_login_time, DateTime.now @user.update_attribute :last_login_time, DateTime.now
redirect_to (params[:from].present? ? params[:from] : ro ot_path) redirect_to (params[:from].present? ? params[:from] : root_path)
else else
flash[:error] = '用户名或密码错误' flash[:error] = '用户名或密码错误'
render 'login', layout: 'register' render 'login', layout: 'register'

View File

@ -0,0 +1,64 @@
require 'test_helper'
require 'users_controller'
class UserControllerTest < ActionController::TestCase
setup do
@controller = UsersController.new
@user = User.new
@user.username = 'user1'
@user.email = '123456677@qq.com'
@user.password = '1234567890'
@user.password_confirmation = '1234567890'
@user.admin = 1
@user.save
end
test "register" do
get :register
assert_response :success
assert_template :register
assert_template layout: "layouts/register"
end
test "register_confim_exist_user" do
post :register_confirm, :user => {:username => 'user1', :email => '1234566@qq.com', :password => 'password123', :password_confirmation => 'password123'}
assert_template :register
assert_template layout: "layouts/register"
end
test "register_confim_no_valid" do
post :register_confirm, :user => {:username => 'usertest', :email => '1234566@qq.com'}
assert_response :success
assert_template :register
assert_template layout: "layouts/register"
end
test "register_confim_new_user" do
post :register_confirm, :user => {:username => 'test1', :email => '234566@qq.com', :password => 'password123', :password_confirmation => 'password123'}
assert_not_nil session[:user_id]
assert_redirected_to root_path
end
test "login" do
get :login, :from => "test"
assert_response :success
assert_template :login
end
test "logout" do
get :logout
assert_nil session[:user_id]
end
test "login_confim" do
post :login_confirm, :user => {:username => 'user1', :password => '1234567890'}
assert_redirected_to root_path
end
test "login_confim_wrong" do
post :login_confirm, :user => {:username => 'user1', :password => '123456000000'}
assert_response :success
assert_equal '用户名或密码错误', flash[:error]
end
end