2010-03-26 11:27:19 +01:00
|
|
|
require 'test_helper'
|
2009-11-16 14:58:14 -02:00
|
|
|
|
|
|
|
class TestHelpersTest < ActionController::TestCase
|
|
|
|
tests UsersController
|
|
|
|
include Devise::TestHelpers
|
|
|
|
|
2011-02-18 15:03:55 +08:00
|
|
|
class CustomFailureApp < Devise::FailureApp
|
|
|
|
def redirect
|
|
|
|
self.status = 306
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-16 14:58:14 -02:00
|
|
|
test "redirects if attempting to access a page unauthenticated" do
|
2010-02-16 17:00:36 +01:00
|
|
|
get :index
|
2010-04-03 11:43:31 +02:00
|
|
|
assert_redirected_to new_user_session_path
|
2010-06-23 11:10:23 +02:00
|
|
|
assert_equal "You need to sign in or sign up before continuing.", flash[:alert]
|
2009-11-16 14:58:14 -02:00
|
|
|
end
|
|
|
|
|
2010-06-29 11:52:02 +02:00
|
|
|
test "redirects if attempting to access a page with an unconfirmed account" do
|
2009-11-16 14:58:14 -02:00
|
|
|
swap Devise, :confirm_within => 0 do
|
2010-06-29 11:52:02 +02:00
|
|
|
user = create_user
|
|
|
|
assert !user.active?
|
|
|
|
|
|
|
|
sign_in user
|
2010-02-16 17:00:36 +01:00
|
|
|
get :index
|
2010-04-03 11:43:31 +02:00
|
|
|
assert_redirected_to new_user_session_path
|
2009-11-16 14:58:14 -02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-29 11:52:02 +02:00
|
|
|
test "returns nil if accessing current_user with an unconfirmed account" do
|
|
|
|
swap Devise, :confirm_within => 0 do
|
|
|
|
user = create_user
|
|
|
|
assert !user.active?
|
|
|
|
|
|
|
|
sign_in user
|
2010-06-29 21:41:34 +02:00
|
|
|
get :accept, :id => user
|
2010-06-29 11:52:02 +02:00
|
|
|
assert_nil assigns(:current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-16 14:58:14 -02:00
|
|
|
test "does not redirect with valid user" do
|
|
|
|
user = create_user
|
|
|
|
user.confirm!
|
|
|
|
|
|
|
|
sign_in user
|
2010-02-16 17:00:36 +01:00
|
|
|
get :index
|
2009-11-16 14:58:14 -02:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test "redirects if valid user signed out" do
|
|
|
|
user = create_user
|
|
|
|
user.confirm!
|
|
|
|
|
|
|
|
sign_in user
|
2010-02-16 17:00:36 +01:00
|
|
|
get :index
|
2009-11-16 14:58:14 -02:00
|
|
|
|
|
|
|
sign_out user
|
2010-02-16 17:00:36 +01:00
|
|
|
get :index
|
2010-04-03 11:43:31 +02:00
|
|
|
assert_redirected_to new_user_session_path
|
2009-11-16 14:58:14 -02:00
|
|
|
end
|
2011-02-18 15:03:55 +08:00
|
|
|
|
|
|
|
test "respects custom failure app" do
|
|
|
|
begin
|
|
|
|
Devise.warden_config.failure_app = CustomFailureApp
|
|
|
|
get :index
|
|
|
|
assert_response 306
|
|
|
|
ensure
|
|
|
|
Devise.warden_config.failure_app = Devise::FailureApp
|
|
|
|
end
|
|
|
|
end
|
2009-11-16 14:58:14 -02:00
|
|
|
|
2010-09-21 10:50:01 +02:00
|
|
|
test "defined Warden after_authentication callback should not be called when sign_in is called" do
|
|
|
|
begin
|
|
|
|
Warden::Manager.after_authentication do |user, auth, opts|
|
|
|
|
flunk "callback was called while it should not"
|
|
|
|
end
|
2010-08-02 17:26:42 +03:00
|
|
|
|
2010-09-21 10:50:01 +02:00
|
|
|
user = create_user
|
|
|
|
user.confirm!
|
|
|
|
sign_in user
|
|
|
|
ensure
|
|
|
|
Warden::Manager._after_set_user.pop
|
2010-08-02 17:26:42 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-21 10:50:01 +02:00
|
|
|
test "defined Warden before_logout callback should not be called when sign_out is called" do
|
|
|
|
begin
|
|
|
|
Warden::Manager.before_logout do |user, auth, opts|
|
|
|
|
flunk "callback was called while it should not"
|
|
|
|
end
|
|
|
|
user = create_user
|
|
|
|
user.confirm!
|
2010-08-02 17:26:42 +03:00
|
|
|
|
2010-09-21 10:50:01 +02:00
|
|
|
sign_in user
|
|
|
|
sign_out user
|
|
|
|
ensure
|
|
|
|
Warden::Manager._before_logout.pop
|
2010-08-02 17:26:42 +03:00
|
|
|
end
|
|
|
|
end
|
2010-12-21 07:26:23 +08:00
|
|
|
|
2010-12-25 11:41:14 +01:00
|
|
|
test "before_failure call should work" do
|
|
|
|
begin
|
|
|
|
executed = false
|
|
|
|
Warden::Manager.before_failure do |env,opts|
|
|
|
|
executed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
user = create_user
|
|
|
|
sign_in user
|
|
|
|
|
|
|
|
get :index
|
|
|
|
assert executed
|
|
|
|
ensure
|
|
|
|
Warden::Manager._before_failure.pop
|
|
|
|
end
|
2010-12-21 07:26:23 +08:00
|
|
|
end
|
2010-08-02 17:26:42 +03:00
|
|
|
|
2009-12-14 22:48:15 +01:00
|
|
|
test "allows to sign in with different users" do
|
2009-12-21 21:10:23 +01:00
|
|
|
first_user = create_user
|
2009-12-14 22:48:15 +01:00
|
|
|
first_user.confirm!
|
|
|
|
|
|
|
|
sign_in first_user
|
2010-02-16 17:00:36 +01:00
|
|
|
get :index
|
|
|
|
assert_match /User ##{first_user.id}/, @response.body
|
2009-12-14 22:48:15 +01:00
|
|
|
sign_out first_user
|
|
|
|
|
2009-12-21 21:10:23 +01:00
|
|
|
second_user = create_user
|
2009-12-14 22:48:15 +01:00
|
|
|
second_user.confirm!
|
|
|
|
|
|
|
|
sign_in second_user
|
2010-02-16 17:00:36 +01:00
|
|
|
get :index
|
|
|
|
assert_match /User ##{second_user.id}/, @response.body
|
2009-12-14 22:48:15 +01:00
|
|
|
end
|
2009-11-16 14:58:14 -02:00
|
|
|
end
|