mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Add Devise::Test::IntegrationHelpers
.
This commit is contained in:
parent
3f3ec236bb
commit
7b000390a0
3 changed files with 94 additions and 1 deletions
|
@ -41,9 +41,9 @@ module Devise
|
|||
|
||||
module Test
|
||||
autoload :ControllerHelpers, 'devise/test/controller_helpers'
|
||||
autoload :IntegrationHelpers, 'devise/test/integration_helpers'
|
||||
end
|
||||
|
||||
|
||||
# Constants which holds devise configuration for extensions. Those should
|
||||
# not be modified by the "end user" (this is why they are constants).
|
||||
ALL = []
|
||||
|
|
61
lib/devise/test/integration_helpers.rb
Normal file
61
lib/devise/test/integration_helpers.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
module Devise
|
||||
# Devise::Test::IntegrationHelpers is a helper module for facilitating
|
||||
# authentication on Rails integration tests to bypass the required steps for
|
||||
# signin in or signin out a record.
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# class PostsTest < ActionDispatch::IntegrationTest
|
||||
# include Devise::Test::IntegrationHelpers
|
||||
#
|
||||
# test 'authenticated users can see posts' do
|
||||
# sign_in users(:bob)
|
||||
#
|
||||
# get '/posts'
|
||||
# assert_response :success
|
||||
# end
|
||||
# end
|
||||
module Test
|
||||
module IntegrationHelpers
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
include Warden::Test::Helpers
|
||||
|
||||
setup :setup_integration_for_devise
|
||||
teardown :teardown_integration_for_devise
|
||||
end
|
||||
end
|
||||
|
||||
# Signs in a specific resource, mimicking a successfull sign in
|
||||
# operation through +Devise::SessionsController#create+.
|
||||
#
|
||||
# * +resource+ - The resource that should be authenticated
|
||||
# * +scope+ - An optional +Symbol+ with the scope where the resource
|
||||
# should be signed in with.
|
||||
def sign_in(resource, scope: nil)
|
||||
scope ||= Devise::Mapping.find_scope!(resource)
|
||||
|
||||
login_as(resource, scope: scope)
|
||||
end
|
||||
|
||||
# Signs out a specific scope from the session.
|
||||
#
|
||||
# * +resource_or_scope+ - The resource or scope that should be signed out.
|
||||
def sign_out(resource_or_scope)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
|
||||
logout scope
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def setup_integration_for_devise
|
||||
Warden.test_mode!
|
||||
end
|
||||
|
||||
def teardown_integration_for_devise
|
||||
Warden.test_reset!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
test/test/integration_helpers_test.rb
Normal file
32
test/test/integration_helpers_test.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require 'test_helper'
|
||||
|
||||
class TestIntegrationsHelpersTest < Devise::IntegrationTest
|
||||
include Devise::Test::IntegrationHelpers
|
||||
|
||||
test '#sign_in signs in the resource directly' do
|
||||
sign_in(create_user)
|
||||
|
||||
visit '/'
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
test '#sign_outs signs out in the resource directly' do
|
||||
user = create_user
|
||||
sign_in user
|
||||
sign_out user
|
||||
|
||||
visit '/'
|
||||
refute warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
test '#sign_out does not signs out other scopes' do
|
||||
sign_in(create_user)
|
||||
sign_in(create_admin)
|
||||
sign_out :user
|
||||
|
||||
visit '/'
|
||||
|
||||
refute warden.authenticated?(:user)
|
||||
assert warden.authenticated?(:admin)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue