From 2a928455f9dcefa465b80b79768ba1d1a423e6e9 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 3 Dec 2012 10:52:01 +0100 Subject: enable users to cancel their account --- users/test/functional/users_controller_test.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'users/test/functional') diff --git a/users/test/functional/users_controller_test.rb b/users/test/functional/users_controller_test.rb index f008cda..44b6768 100644 --- a/users/test/functional/users_controller_test.rb +++ b/users/test/functional/users_controller_test.rb @@ -63,10 +63,19 @@ class UsersControllerTest < ActionController::TestCase login :is_admin? => true user = stub_record User user.expects(:destroy) - User.expects(:find_by_param).with(user.id.to_s).returns(user) + User.expects(:find_by_param).with(user.id).returns(user) delete :destroy, :id => user.id assert_response :redirect - # assert_redirected_to users_path + assert_redirected_to users_path + end + + test "user can cancel account" do + login + @current_user.expects(:destroy) + User.expects(:find_by_param).with(@current_user.id).returns(@current_user) + delete :destroy, :id => @current_user.id + assert_response :redirect + assert_redirected_to login_path end test "non-admin can't destroy user" do -- cgit v1.2.3 From 1b411de39f38eb0925cf255e941545933f227759 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 3 Dec 2012 14:02:16 +0100 Subject: refactored tests with new find_record helper find_record User will return a stubbed user record and make sure User.find_by_id(user.id) returns the same so it can be used in controllers. --- users/test/functional/users_controller_test.rb | 45 ++++++++++++++++++-------- 1 file changed, 31 insertions(+), 14 deletions(-) (limited to 'users/test/functional') diff --git a/users/test/functional/users_controller_test.rb b/users/test/functional/users_controller_test.rb index 44b6768..939d105 100644 --- a/users/test/functional/users_controller_test.rb +++ b/users/test/functional/users_controller_test.rb @@ -5,6 +5,7 @@ class UsersControllerTest < ActionController::TestCase test "should get new" do get :new + assert_equal User, assigns(:user).class assert_response :success end @@ -12,7 +13,9 @@ class UsersControllerTest < ActionController::TestCase test "should create new user" do user = stub_record User User.expects(:create).with(user.params).returns(user) + post :create, :user => user.params, :format => :json + assert_nil session[:user_id] assert_json_response user assert_response :success @@ -24,70 +27,81 @@ class UsersControllerTest < ActionController::TestCase params.stringify_keys! assert !user.valid? User.expects(:create).with(params).returns(user) + post :create, :user => params, :format => :json + assert_json_error user.errors.messages assert_response 422 end test "should get edit view" do - user = stub_record User - User.expects(:find_by_param).with(user.id.to_s).returns(user) + user = find_record User + login user get :edit, :id => user.id + assert_equal user, assigns[:user] end test "should process updated params" do - user = stub_record User + user = find_record User user.expects(:update_attributes).with(user.params).returns(true) - User.expects(:find_by_param).with(user.id.to_s).returns(user) + login user put :update, :user => user.params, :id => user.id, :format => :json + assert_equal user, assigns[:user] assert_response 204 assert_equal " ", @response.body end - test "admin can edit user" do - user = stub_record User + test "admin can update user" do + user = find_record User user.expects(:update_attributes).with(user.params).returns(true) - User.expects(:find_by_param).with(user.id.to_s).returns(user) + login :is_admin? => true put :update, :user => user.params, :id => user.id, :format => :json + assert_equal user, assigns[:user] assert_response 204 assert_equal " ", @response.body end test "admin can destroy user" do - login :is_admin? => true - user = stub_record User + user = find_record User user.expects(:destroy) - User.expects(:find_by_param).with(user.id).returns(user) + + login :is_admin? => true delete :destroy, :id => user.id + assert_response :redirect assert_redirected_to users_path end test "user can cancel account" do - login - @current_user.expects(:destroy) - User.expects(:find_by_param).with(@current_user.id).returns(@current_user) + user = find_record User + user.expects(:destroy) + + login user delete :destroy, :id => @current_user.id + assert_response :redirect assert_redirected_to login_path end test "non-admin can't destroy user" do - login user = stub_record User + + login delete :destroy, :id => user.id + assert_access_denied end test "admin can list users" do login :is_admin? => true get :index + assert_response :success assert assigns(:users) end @@ -95,12 +109,14 @@ class UsersControllerTest < ActionController::TestCase test "non-admin can't list users" do login get :index + assert_access_denied end test "admin can autocomplete users" do login :is_admin? => true get :index, :format => :json + assert_response :success assert assigns(:users) end @@ -108,6 +124,7 @@ class UsersControllerTest < ActionController::TestCase test "admin can search users" do login :is_admin? => true get :index, :query => "a" + assert_response :success assert assigns(:users) end -- cgit v1.2.3