Module: Sorcery::TestHelpers::Internal::Rails

Includes:
Rails::Controller
Defined in:
lib/sorcery/test_helpers/internal/rails.rb

Constant Summary collapse

SUBMODULES_AUTO_ADDED_CONTROLLER_FILTERS =
%i[
  register_last_activity_time_to_db
  deny_banned_user
  validate_session
].freeze

Instance Method Summary collapse

Methods included from Rails::Controller

#logged_in?, #login_user, #logout_user

Instance Method Details

#clear_user_without_logoutObject

This helper is used to fake multiple users signing in in tests. It does so by clearing @current_user, thus allowing a new user to login, all this without calling the :logout action explicitly. A dirty dirty hack.



62
63
64
# File 'lib/sorcery/test_helpers/internal/rails.rb', line 62

def clear_user_without_logout
  subject.instance_variable_set(:@current_user, nil)
end

#sorcery_controller_external_property_set(provider, property, value) ⇒ Object



54
55
56
# File 'lib/sorcery/test_helpers/internal/rails.rb', line 54

def sorcery_controller_external_property_set(provider, property, value)
  ::Sorcery::Controller::Config.send(provider).send(:"#{property}=", value)
end

#sorcery_controller_property_set(property, value) ⇒ Object



50
51
52
# File 'lib/sorcery/test_helpers/internal/rails.rb', line 50

def sorcery_controller_property_set(property, value)
  ::Sorcery::Controller::Config.send(:"#{property}=", value)
end

#sorcery_reload!(submodules = [], options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sorcery/test_helpers/internal/rails.rb', line 13

def sorcery_reload!(submodules = [], options = {})
  reload_user_class

  # return to no-module configuration
  ::Sorcery::Controller::Config.init!
  ::Sorcery::Controller::Config.reset!

  # remove all plugin before_actions so they won't fail other tests.
  # I don't like this way, but I didn't find another.
  # hopefully it won't break until Rails 4.
  chain = if Gem::Version.new(::Rails::VERSION::STRING) >= Gem::Version.new('4.1.0')
            SorceryController._process_action_callbacks.send :chain
          else
            SorceryController._process_action_callbacks
          end

  chain.delete_if { |c| SUBMODULES_AUTO_ADDED_CONTROLLER_FILTERS.include?(c.filter) }

  # configure
  ::Sorcery::Controller::Config.submodules = submodules
  ::Sorcery::Controller::Config.user_class = nil
  ActionController::Base.send(:include, ::Sorcery::Controller)
  ::Sorcery::Controller::Config.user_class = 'User'

  ::Sorcery::Controller::Config.user_config do |user|
    options.each do |property, value|
      user.send(:"#{property}=", value)
    end
  end
  User.authenticates_with_sorcery!
  return unless defined?(DataMapper) && User.ancestors.include?(DataMapper::Resource)

  DataMapper.auto_migrate!
  User.finalize
  Authentication.finalize
end