From 6effa4a88776ef96329400ee913a45f64925d582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Mon, 4 Jan 2016 13:54:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Euser=20controller=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 8196 bytes app/.DS_Store | Bin 0 -> 8196 bytes app/controllers/users_controller.rb | 6 +- test/controllers/users_controller_test.rb | 64 ++++++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 app/.DS_Store create mode 100644 test/controllers/users_controller_test.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..52f3d37e42a0071a7b193b73cc7a2ef7a7db84fb GIT binary patch literal 8196 zcmeI1Piz!b9LIm(-2yYizqhrOWevD#ttF&NK|~t~?6#|c6r|YJwuqG7oe?HXXV#tB z-3FuCo6%rU6Nwr*aM8fg#OPTsTr^Qh)OgY8$(tt=_2BQ#n?g(7s~UMPdGGh$@6XKa zcRrcPn>be%Vus&YcI`9N}JBs&zuXXoSR<8(qnSv_h6 zv;wOYkbCzum8nKGFI>KV_uMcV9{v`obk~}->kQLcpDp%SW3z5G@F(5#&b-(ho()=u zc{x#cny%BDsRmwS!4Ky}rdsx@4X+$IMbDGT;cVnAMsju{4C>KXoH#E;BXpi}IL~c5 zCC=v3Zh53*wa5+p@M)(Wvh6PntLrweZRpKy@89vjo=5iI*tlu4u)3|DnWh`M70;P& zxps7Rt{he!-*){oCtTa{BlkJishgSfMRUq8FF2}dOLnU%J~B?)e9EJNEpzNRlHHQs zW*E;%|5)AOQMs*rw$Bu0V=Ckypbjx$4Voe}r_Q;4Qo7R`GK6hTd*zDbsiW<(b{k^c zoN21xp`3fIeMSaD=4>@sSl}xRm3t)nm?5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llers/users_controller.rb b/app/controllers/users_controller.rb index 3164455..c3ef57b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -7,7 +7,7 @@ class UsersController < ApplicationController end 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 to_login @user redirect_to root_path @@ -18,7 +18,7 @@ class UsersController < ApplicationController end 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 render 'login', layout: 'register' end @@ -28,7 +28,7 @@ class UsersController < ApplicationController if @user && @user.check_password(params[:user][:password]) to_login @user @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 flash[:error] = '用户名或密码错误' render 'login', layout: 'register' diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..7b3bfd4 --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -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