2011-04-15 04:38:16 -04:00
|
|
|
require 'test_helper'
|
2009-11-16 11:58:14 -05:00
|
|
|
|
|
|
|
class TestHelpersTest < ActionController::TestCase
|
|
|
|
tests UsersController
|
|
|
|
include Devise::TestHelpers
|
|
|
|
|
|
|
|
test "redirects if attempting to access a page unauthenticated" do
|
2010-02-16 11:00:36 -05:00
|
|
|
get :index
|
2010-04-03 05:43:31 -04:00
|
|
|
assert_redirected_to new_user_session_path
|
2010-06-23 05:10:23 -04:00
|
|
|
assert_equal "You need to sign in or sign up before continuing.", flash[:alert]
|
2009-11-16 11:58:14 -05:00
|
|
|
end
|
|
|
|
|
2010-06-29 05:52:02 -04:00
|
|
|
test "redirects if attempting to access a page with an unconfirmed account" do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, allow_unconfirmed_access_for: 0.days do
|
2010-06-29 05:52:02 -04:00
|
|
|
user = create_user
|
2011-03-25 10:39:08 -04:00
|
|
|
assert !user.active_for_authentication?
|
2010-06-29 05:52:02 -04:00
|
|
|
|
|
|
|
sign_in user
|
2010-02-16 11:00:36 -05:00
|
|
|
get :index
|
2010-04-03 05:43:31 -04:00
|
|
|
assert_redirected_to new_user_session_path
|
2009-11-16 11:58:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-29 05:52:02 -04:00
|
|
|
test "returns nil if accessing current_user with an unconfirmed account" do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, allow_unconfirmed_access_for: 0.days do
|
2010-06-29 05:52:02 -04:00
|
|
|
user = create_user
|
2011-03-25 10:39:08 -04:00
|
|
|
assert !user.active_for_authentication?
|
2010-06-29 05:52:02 -04:00
|
|
|
|
|
|
|
sign_in user
|
2014-02-25 11:42:55 -05:00
|
|
|
get :accept, id: user
|
2010-06-29 05:52:02 -04:00
|
|
|
assert_nil assigns(:current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-16 11:58:14 -05:00
|
|
|
test "does not redirect with valid user" do
|
|
|
|
user = create_user
|
|
|
|
user.confirm!
|
|
|
|
|
|
|
|
sign_in user
|
2010-02-16 11:00:36 -05:00
|
|
|
get :index
|
2009-11-16 11:58:14 -05:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
2012-06-28 06:05:20 -04:00
|
|
|
test "does not redirect with valid user after failed first attempt" do
|
|
|
|
get :index
|
|
|
|
assert_response :redirect
|
|
|
|
|
|
|
|
user = create_user
|
|
|
|
user.confirm!
|
|
|
|
|
|
|
|
sign_in user
|
|
|
|
get :index
|
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
2009-11-16 11:58:14 -05:00
|
|
|
test "redirects if valid user signed out" do
|
|
|
|
user = create_user
|
|
|
|
user.confirm!
|
|
|
|
|
|
|
|
sign_in user
|
2010-02-16 11:00:36 -05:00
|
|
|
get :index
|
2009-11-16 11:58:14 -05:00
|
|
|
|
|
|
|
sign_out user
|
2010-02-16 11:00:36 -05:00
|
|
|
get :index
|
2010-04-03 05:43:31 -04:00
|
|
|
assert_redirected_to new_user_session_path
|
2009-11-16 11:58:14 -05:00
|
|
|
end
|
2012-09-27 20:44:31 -04:00
|
|
|
|
2011-02-18 02:03:55 -05:00
|
|
|
test "respects custom failure app" do
|
2014-07-10 07:48:37 -04:00
|
|
|
custom_failure_app = Class.new(Devise::FailureApp) do
|
|
|
|
def redirect
|
|
|
|
self.status = 306
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
swap Devise.warden_config, failure_app: custom_failure_app do
|
2011-02-18 02:03:55 -05:00
|
|
|
get :index
|
|
|
|
assert_response 306
|
|
|
|
end
|
|
|
|
end
|
2009-11-16 11:58:14 -05:00
|
|
|
|
2014-07-10 07:48:37 -04:00
|
|
|
test "passes given headers from the failure app to the response" do
|
|
|
|
custom_failure_app = Class.new(Devise::FailureApp) do
|
|
|
|
def respond
|
|
|
|
self.status = 401
|
|
|
|
self.response.headers["CUSTOMHEADER"] = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
swap Devise.warden_config, failure_app: custom_failure_app do
|
|
|
|
sign_in create_user
|
|
|
|
get :index
|
|
|
|
assert_equal 1, @response.headers["CUSTOMHEADER"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-27 20:44:31 -04:00
|
|
|
test "returns the body of a failure app" do
|
|
|
|
get :index
|
|
|
|
assert_equal response.body, "<html><body>You are being <a href=\"http://test.host/users/sign_in\">redirected</a>.</body></html>"
|
|
|
|
end
|
|
|
|
|
2010-09-21 04:50:01 -04: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 10:26:42 -04:00
|
|
|
|
2010-09-21 04:50:01 -04:00
|
|
|
user = create_user
|
|
|
|
user.confirm!
|
|
|
|
sign_in user
|
|
|
|
ensure
|
|
|
|
Warden::Manager._after_set_user.pop
|
2010-08-02 10:26:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-21 04:50:01 -04: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 10:26:42 -04:00
|
|
|
|
2010-09-21 04:50:01 -04:00
|
|
|
sign_in user
|
|
|
|
sign_out user
|
|
|
|
ensure
|
|
|
|
Warden::Manager._before_logout.pop
|
2010-08-02 10:26:42 -04:00
|
|
|
end
|
|
|
|
end
|
2010-12-20 18:26:23 -05:00
|
|
|
|
2010-12-25 05:41:14 -05: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-20 18:26:23 -05:00
|
|
|
end
|
2010-08-02 10:26:42 -04:00
|
|
|
|
2009-12-14 16:48:15 -05:00
|
|
|
test "allows to sign in with different users" do
|
2009-12-21 15:10:23 -05:00
|
|
|
first_user = create_user
|
2009-12-14 16:48:15 -05:00
|
|
|
first_user.confirm!
|
|
|
|
|
|
|
|
sign_in first_user
|
2010-02-16 11:00:36 -05:00
|
|
|
get :index
|
|
|
|
assert_match /User ##{first_user.id}/, @response.body
|
2009-12-14 16:48:15 -05:00
|
|
|
sign_out first_user
|
|
|
|
|
2009-12-21 15:10:23 -05:00
|
|
|
second_user = create_user
|
2009-12-14 16:48:15 -05:00
|
|
|
second_user.confirm!
|
|
|
|
|
|
|
|
sign_in second_user
|
2010-02-16 11:00:36 -05:00
|
|
|
get :index
|
|
|
|
assert_match /User ##{second_user.id}/, @response.body
|
2009-12-14 16:48:15 -05:00
|
|
|
end
|
Allow warden proxy to change with request in tests
The warden method in the Devise::TestHelpers module adds a Warden proxy
object to the request environment hash under the 'warden' key. Including
this module in your test case registers that method as a callback, which
runs before every test:
https://github.com/plataformatec/devise/blob/v3.4.1/lib/devise/test_helpers.rb#L12
The request object itself is created in a callback added by Rails:
https://github.com/rails/rails/blob/v4.2.0/actionpack/lib/action_controller/test_case.rb#L687
So before each test runs, the Rails callback creates the request object,
and then the Devise callback adds a Warden proxy object to it.
I was using the rspec-retry gem (https://github.com/y310/rspec-retry),
and noticed that my controller specs would always fail whenever they
were retried with this error:
NoMethodError: undefined method `authenticate!' for nil:NilClass
When rspec-retry re-runs a failed test, it runs the setup callbacks
again. The Rails callback creates a new request object, but because of
the memoization that was here before, the Devise callback wouldn't add a
Warden proxy to it, which was causing the error.
With this change, the Warden setup code will still only run once as long
as the request object stays the same, but if it changes a new Warden
proxy will be added to the new request object.
2015-01-04 19:34:54 -05:00
|
|
|
|
|
|
|
test "creates a new warden proxy if the request object has changed" do
|
|
|
|
old_warden_proxy = warden
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
new_warden_proxy = warden
|
|
|
|
|
|
|
|
assert_not_equal old_warden_proxy, new_warden_proxy
|
|
|
|
end
|
|
|
|
|
|
|
|
test "doesn't create a new warden proxy if the request object hasn't changed" do
|
|
|
|
old_warden_proxy = warden
|
|
|
|
new_warden_proxy = warden
|
|
|
|
|
|
|
|
assert_equal old_warden_proxy, new_warden_proxy
|
|
|
|
end
|
2009-11-16 11:58:14 -05:00
|
|
|
end
|