authlogic-nicho 6.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/authlogic/acts_as_authentic/base.rb +116 -0
- data/lib/authlogic/acts_as_authentic/email.rb +30 -0
- data/lib/authlogic/acts_as_authentic/logged_in_status.rb +85 -0
- data/lib/authlogic/acts_as_authentic/login.rb +63 -0
- data/lib/authlogic/acts_as_authentic/magic_columns.rb +38 -0
- data/lib/authlogic/acts_as_authentic/password.rb +357 -0
- data/lib/authlogic/acts_as_authentic/perishable_token.rb +122 -0
- data/lib/authlogic/acts_as_authentic/persistence_token.rb +70 -0
- data/lib/authlogic/acts_as_authentic/queries/case_sensitivity.rb +53 -0
- data/lib/authlogic/acts_as_authentic/queries/find_with_case.rb +83 -0
- data/lib/authlogic/acts_as_authentic/session_maintenance.rb +186 -0
- data/lib/authlogic/acts_as_authentic/single_access_token.rb +83 -0
- data/lib/authlogic/config.rb +43 -0
- data/lib/authlogic/controller_adapters/abstract_adapter.rb +119 -0
- data/lib/authlogic/controller_adapters/rack_adapter.rb +72 -0
- data/lib/authlogic/controller_adapters/rails_adapter.rb +47 -0
- data/lib/authlogic/controller_adapters/sinatra_adapter.rb +67 -0
- data/lib/authlogic/cookie_credentials.rb +63 -0
- data/lib/authlogic/crypto_providers/bcrypt.rb +113 -0
- data/lib/authlogic/crypto_providers/md5/v2.rb +35 -0
- data/lib/authlogic/crypto_providers/md5.rb +36 -0
- data/lib/authlogic/crypto_providers/scrypt.rb +92 -0
- data/lib/authlogic/crypto_providers/sha1/v2.rb +41 -0
- data/lib/authlogic/crypto_providers/sha1.rb +42 -0
- data/lib/authlogic/crypto_providers/sha256/v2.rb +58 -0
- data/lib/authlogic/crypto_providers/sha256.rb +59 -0
- data/lib/authlogic/crypto_providers/sha512/v2.rb +39 -0
- data/lib/authlogic/crypto_providers/sha512.rb +38 -0
- data/lib/authlogic/crypto_providers.rb +87 -0
- data/lib/authlogic/errors.rb +50 -0
- data/lib/authlogic/i18n/translator.rb +18 -0
- data/lib/authlogic/i18n.rb +100 -0
- data/lib/authlogic/random.rb +18 -0
- data/lib/authlogic/session/base.rb +2207 -0
- data/lib/authlogic/session/magic_column/assigns_last_request_at.rb +46 -0
- data/lib/authlogic/test_case/mock_api_controller.rb +52 -0
- data/lib/authlogic/test_case/mock_controller.rb +58 -0
- data/lib/authlogic/test_case/mock_cookie_jar.rb +109 -0
- data/lib/authlogic/test_case/mock_logger.rb +12 -0
- data/lib/authlogic/test_case/mock_request.rb +35 -0
- data/lib/authlogic/test_case/rails_request_adapter.rb +39 -0
- data/lib/authlogic/test_case.rb +215 -0
- data/lib/authlogic/version.rb +22 -0
- data/lib/authlogic.rb +44 -0
- metadata +382 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Authlogic
|
4
|
+
module Session
|
5
|
+
module MagicColumn
|
6
|
+
# Assigns the current time to the `last_request_at` attribute.
|
7
|
+
#
|
8
|
+
# 1. The `last_request_at` column must exist
|
9
|
+
# 2. Assignment can be disabled on a per-controller basis
|
10
|
+
# 3. Assignment will not happen more often than `last_request_at_threshold`
|
11
|
+
# seconds.
|
12
|
+
#
|
13
|
+
# - current_time - a `Time`
|
14
|
+
# - record - eg. a `User`
|
15
|
+
# - controller - an `Authlogic::ControllerAdapters::AbstractAdapter`
|
16
|
+
# - last_request_at_threshold - integer - seconds
|
17
|
+
#
|
18
|
+
# @api private
|
19
|
+
class AssignsLastRequestAt
|
20
|
+
def initialize(current_time, record, controller, last_request_at_threshold)
|
21
|
+
@current_time = current_time
|
22
|
+
@record = record
|
23
|
+
@controller = controller
|
24
|
+
@last_request_at_threshold = last_request_at_threshold
|
25
|
+
end
|
26
|
+
|
27
|
+
def assign
|
28
|
+
return unless assign?
|
29
|
+
@record.last_request_at = @current_time
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
# @api private
|
35
|
+
def assign?
|
36
|
+
@record &&
|
37
|
+
@record.class.column_names.include?("last_request_at") &&
|
38
|
+
@controller.last_request_update_allowed? && (
|
39
|
+
@record.last_request_at.blank? ||
|
40
|
+
@last_request_at_threshold.to_i.seconds.ago >= @record.last_request_at
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Authlogic
|
4
|
+
module TestCase
|
5
|
+
# Basically acts like an API controller but doesn't do anything.
|
6
|
+
# Authlogic can interact with this, do it's thing and then you can look at
|
7
|
+
# the controller object to see if anything changed.
|
8
|
+
class MockAPIController < ControllerAdapters::AbstractAdapter
|
9
|
+
attr_writer :request_content_type
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
end
|
13
|
+
|
14
|
+
# Expected API controller has no cookies method.
|
15
|
+
undef :cookies
|
16
|
+
|
17
|
+
def cookie_domain
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def logger
|
22
|
+
@logger ||= MockLogger.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def params
|
26
|
+
@params ||= {}
|
27
|
+
end
|
28
|
+
|
29
|
+
def request
|
30
|
+
@request ||= MockRequest.new(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def request_content_type
|
34
|
+
@request_content_type ||= "text/html"
|
35
|
+
end
|
36
|
+
|
37
|
+
def session
|
38
|
+
@session ||= {}
|
39
|
+
end
|
40
|
+
|
41
|
+
# If method is defined, it causes below behavior...
|
42
|
+
# controller = Authlogic::ControllerAdapters::RailsAdapter.new(
|
43
|
+
# Authlogic::TestCase::MockAPIController.new
|
44
|
+
# )
|
45
|
+
# controller.responds_to_single_access_allowed? #=> true
|
46
|
+
# controller.single_access_allowed?
|
47
|
+
# #=> NoMethodError: undefined method `single_access_allowed?' for nil:NilClass
|
48
|
+
#
|
49
|
+
undef :single_access_allowed?
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Authlogic
|
4
|
+
module TestCase
|
5
|
+
# Basically acts like a controller but doesn't do anything. Authlogic can interact
|
6
|
+
# with this, do it's thing and then you can look at the controller object to see if
|
7
|
+
# anything changed.
|
8
|
+
class MockController < ControllerAdapters::AbstractAdapter
|
9
|
+
attr_accessor :http_user, :http_password, :realm
|
10
|
+
attr_writer :request_content_type
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
end
|
14
|
+
|
15
|
+
def authenticate_with_http_basic
|
16
|
+
yield http_user, http_password
|
17
|
+
end
|
18
|
+
|
19
|
+
def authenticate_or_request_with_http_basic(realm = "DefaultRealm")
|
20
|
+
self.realm = realm
|
21
|
+
@http_auth_requested = true
|
22
|
+
yield http_user, http_password
|
23
|
+
end
|
24
|
+
|
25
|
+
def cookies
|
26
|
+
@cookies ||= MockCookieJar.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def cookie_domain
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def logger
|
34
|
+
@logger ||= MockLogger.new
|
35
|
+
end
|
36
|
+
|
37
|
+
def params
|
38
|
+
@params ||= {}
|
39
|
+
end
|
40
|
+
|
41
|
+
def request
|
42
|
+
@request ||= MockRequest.new(self)
|
43
|
+
end
|
44
|
+
|
45
|
+
def request_content_type
|
46
|
+
@request_content_type ||= "text/html"
|
47
|
+
end
|
48
|
+
|
49
|
+
def session
|
50
|
+
@session ||= {}
|
51
|
+
end
|
52
|
+
|
53
|
+
def http_auth_requested?
|
54
|
+
@http_auth_requested ||= false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Authlogic
|
4
|
+
module TestCase
|
5
|
+
# A mock of `ActionDispatch::Cookies::CookieJar`.
|
6
|
+
# See action_dispatch/middleware/cookies.rb
|
7
|
+
class MockCookieJar < Hash # :nodoc:
|
8
|
+
attr_accessor :set_cookies
|
9
|
+
|
10
|
+
def [](key)
|
11
|
+
hash = super
|
12
|
+
hash && hash[:value]
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param options - "the cookie's value [usually a string] or a hash of
|
16
|
+
# options as documented above [in action_dispatch/middleware/cookies.rb]"
|
17
|
+
def []=(key, options)
|
18
|
+
opt = cookie_options_to_hash(options)
|
19
|
+
(@set_cookies ||= {})[key.to_s] = opt
|
20
|
+
super(key, opt)
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete(key, _options = {})
|
24
|
+
super(key)
|
25
|
+
end
|
26
|
+
|
27
|
+
def signed
|
28
|
+
@signed ||= MockSignedCookieJar.new(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
def encrypted
|
32
|
+
@encrypted ||= MockEncryptedCookieJar.new(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
# @api private
|
38
|
+
def cookie_options_to_hash(options)
|
39
|
+
if options.is_a?(Hash)
|
40
|
+
options
|
41
|
+
else
|
42
|
+
{ value: options }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# A mock of `ActionDispatch::Cookies::SignedKeyRotatingCookieJar`
|
48
|
+
#
|
49
|
+
# > .. a jar that'll automatically generate a signed representation of
|
50
|
+
# > cookie value and verify it when reading from the cookie again.
|
51
|
+
# > actionpack/lib/action_dispatch/middleware/cookies.rb
|
52
|
+
class MockSignedCookieJar < MockCookieJar
|
53
|
+
attr_reader :parent_jar # helper for testing
|
54
|
+
|
55
|
+
def initialize(parent_jar)
|
56
|
+
@parent_jar = parent_jar
|
57
|
+
parent_jar.each { |k, v| self[k] = v }
|
58
|
+
end
|
59
|
+
|
60
|
+
def [](val)
|
61
|
+
signed_message = @parent_jar[val]
|
62
|
+
if signed_message
|
63
|
+
payload, signature = signed_message.split("--")
|
64
|
+
raise "Invalid signature" unless Digest::SHA1.hexdigest(payload) == signature
|
65
|
+
payload
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def []=(key, options)
|
70
|
+
opt = cookie_options_to_hash(options)
|
71
|
+
opt[:value] = "#{opt[:value]}--#{Digest::SHA1.hexdigest opt[:value]}"
|
72
|
+
@parent_jar[key] = opt
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Which ActionDispatch class is this a mock of?
|
77
|
+
# TODO: Document as with other mocks above.
|
78
|
+
class MockEncryptedCookieJar < MockCookieJar
|
79
|
+
attr_reader :parent_jar # helper for testing
|
80
|
+
|
81
|
+
def initialize(parent_jar)
|
82
|
+
@parent_jar = parent_jar
|
83
|
+
parent_jar.each { |k, v| self[k] = v }
|
84
|
+
end
|
85
|
+
|
86
|
+
def [](val)
|
87
|
+
encrypted_message = @parent_jar[val]
|
88
|
+
if encrypted_message
|
89
|
+
self.class.decrypt(encrypted_message)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def []=(key, options)
|
94
|
+
opt = cookie_options_to_hash(options)
|
95
|
+
opt[:value] = self.class.encrypt(opt[:value])
|
96
|
+
@parent_jar[key] = opt
|
97
|
+
end
|
98
|
+
|
99
|
+
# simple caesar cipher for testing
|
100
|
+
def self.encrypt(str)
|
101
|
+
str.unpack("U*").map(&:succ).pack("U*")
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.decrypt(str)
|
105
|
+
str.unpack("U*").map(&:pred).pack("U*")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Authlogic
|
4
|
+
module TestCase
|
5
|
+
class MockRequest # :nodoc:
|
6
|
+
attr_accessor :controller
|
7
|
+
|
8
|
+
def initialize(controller)
|
9
|
+
self.controller = controller
|
10
|
+
end
|
11
|
+
|
12
|
+
def env
|
13
|
+
@env ||= {
|
14
|
+
ControllerAdapters::AbstractAdapter::ENV_SESSION_OPTIONS => {}
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def format
|
19
|
+
controller.request_content_type if controller.respond_to? :request_content_type
|
20
|
+
end
|
21
|
+
|
22
|
+
def ip
|
23
|
+
controller&.respond_to?(:env) &&
|
24
|
+
controller.env.is_a?(Hash) &&
|
25
|
+
controller.env["REMOTE_ADDR"] ||
|
26
|
+
"1.1.1.1"
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def method_missing(*args, &block)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Authlogic
|
4
|
+
module TestCase
|
5
|
+
# Adapts authlogic to work with the @request object when testing. This way Authlogic
|
6
|
+
# can set cookies and what not before a request is made, ultimately letting you log in
|
7
|
+
# users in functional tests.
|
8
|
+
class RailsRequestAdapter < ControllerAdapters::AbstractAdapter
|
9
|
+
def authenticate_with_http_basic(&block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def cookies
|
13
|
+
new_cookies = MockCookieJar.new
|
14
|
+
super.each do |key, value|
|
15
|
+
new_cookies[key] = cookie_value(value)
|
16
|
+
end
|
17
|
+
new_cookies
|
18
|
+
end
|
19
|
+
|
20
|
+
def cookie_domain
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def request
|
25
|
+
@request ||= MockRequest.new(controller)
|
26
|
+
end
|
27
|
+
|
28
|
+
def request_content_type
|
29
|
+
request.format.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def cookie_value(value)
|
35
|
+
value.is_a?(Hash) ? value[:value] : value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,215 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/test_case/rails_request_adapter"
|
4
|
+
require File.dirname(__FILE__) + "/test_case/mock_api_controller"
|
5
|
+
require File.dirname(__FILE__) + "/test_case/mock_cookie_jar"
|
6
|
+
require File.dirname(__FILE__) + "/test_case/mock_controller"
|
7
|
+
require File.dirname(__FILE__) + "/test_case/mock_logger"
|
8
|
+
require File.dirname(__FILE__) + "/test_case/mock_request"
|
9
|
+
|
10
|
+
# :nodoc:
|
11
|
+
module Authlogic
|
12
|
+
# This module is a collection of methods and classes that help you easily test
|
13
|
+
# Authlogic. In fact, I use these same tools to test the internals of
|
14
|
+
# Authlogic.
|
15
|
+
#
|
16
|
+
# === The quick and dirty
|
17
|
+
#
|
18
|
+
# require "authlogic/test_case" # include at the top of test_helper.rb
|
19
|
+
# setup :activate_authlogic # run before tests are executed
|
20
|
+
# UserSession.create(users(:whomever)) # logs a user in
|
21
|
+
#
|
22
|
+
# For a more detailed explanation, see below.
|
23
|
+
#
|
24
|
+
# === Setting up
|
25
|
+
#
|
26
|
+
# Authlogic comes with some simple testing tools. To get these, you need to
|
27
|
+
# first require Authlogic's TestCase. If you are doing this in a rails app,
|
28
|
+
# you would require this file at the top of your test_helper.rb file:
|
29
|
+
#
|
30
|
+
# require "authlogic/test_case"
|
31
|
+
#
|
32
|
+
# If you are using Test::Unit::TestCase, the standard testing library that
|
33
|
+
# comes with ruby, then you can skip this next part. If you are not, you need
|
34
|
+
# to include the Authlogic::TestCase into your testing suite as follows:
|
35
|
+
#
|
36
|
+
# include Authlogic::TestCase
|
37
|
+
#
|
38
|
+
# Now that everything is ready to go, let's move onto actually testing. Here
|
39
|
+
# is the basic idea behind testing:
|
40
|
+
#
|
41
|
+
# Authlogic requires a "connection" to your controller to activate it. In the
|
42
|
+
# same manner that ActiveRecord requires a connection to your database. It
|
43
|
+
# can't do anything until it gets connected. That being said, Authlogic will
|
44
|
+
# raise an Authlogic::Session::Activation::NotActivatedError any time you try
|
45
|
+
# to instantiate an object without a "connection". So before you do anything
|
46
|
+
# with Authlogic, you need to activate / connect Authlogic. Let's walk through
|
47
|
+
# how to do this in tests:
|
48
|
+
#
|
49
|
+
# === Fixtures / Factories
|
50
|
+
#
|
51
|
+
# Creating users via fixtures / factories is easy. Here's an example of a
|
52
|
+
# fixture:
|
53
|
+
#
|
54
|
+
# ben:
|
55
|
+
# email: whatever@whatever.com
|
56
|
+
# password_salt: <%= salt = Authlogic::Random.hex_token %>
|
57
|
+
# crypted_password: <%= Authlogic::CryptoProviders::SCrypt.encrypt("benrocks" + salt) %>
|
58
|
+
# persistence_token: <%= Authlogic::Random.hex_token %>
|
59
|
+
# single_access_token: <%= Authlogic::Random.friendly_token %>
|
60
|
+
# perishable_token: <%= Authlogic::Random.friendly_token %>
|
61
|
+
#
|
62
|
+
# Notice the crypted_password value. Just supplement that with whatever crypto
|
63
|
+
# provider you are using, if you are not using the default.
|
64
|
+
#
|
65
|
+
# === Functional tests
|
66
|
+
#
|
67
|
+
# Activating Authlogic isn't a problem here, because making a request will
|
68
|
+
# activate Authlogic for you. The problem is logging users in so they can
|
69
|
+
# access restricted areas. Solving this is simple, just do this:
|
70
|
+
#
|
71
|
+
# setup :activate_authlogic
|
72
|
+
#
|
73
|
+
# For those of you unfamiliar with TestUnit, the setup method basically just
|
74
|
+
# executes a method before any test is ran. It is essentially "setting up"
|
75
|
+
# your tests.
|
76
|
+
#
|
77
|
+
# Once you have done this, just log users in like usual:
|
78
|
+
#
|
79
|
+
# UserSession.create(users(:whomever))
|
80
|
+
# # access my restricted area here
|
81
|
+
#
|
82
|
+
# Do this before you make your request and it will act as if that user is
|
83
|
+
# logged in.
|
84
|
+
#
|
85
|
+
# === Integration tests
|
86
|
+
#
|
87
|
+
# Again, just like functional tests, you don't have to do anything. As soon as
|
88
|
+
# you make a request, Authlogic will be connected. If you want to activate
|
89
|
+
# Authlogic before making a request follow the same steps described in the
|
90
|
+
# "functional tests" section above. It works in the same manner.
|
91
|
+
#
|
92
|
+
# === Unit tests
|
93
|
+
#
|
94
|
+
# The only time you need to do any trickiness here is if you want to test
|
95
|
+
# Authlogic models. Maybe you added some custom code or methods in your
|
96
|
+
# Authlogic models. Maybe you are writing a plugin or a library that extends
|
97
|
+
# Authlogic.
|
98
|
+
#
|
99
|
+
# That being said, in this environment there is no controller. So you need to
|
100
|
+
# use a "mock" controller. Something that looks like a controller, acts like a
|
101
|
+
# controller, but isn't a "real" controller. You are essentially connecting
|
102
|
+
# Authlogic to your "mock" controller, then you can test off of the mock
|
103
|
+
# controller to make sure everything is functioning properly.
|
104
|
+
#
|
105
|
+
# I use a mock controller to test Authlogic myself. It's part of the Authlogic
|
106
|
+
# library that you can easily use. It's as simple as functional and
|
107
|
+
# integration tests. Just do the following:
|
108
|
+
#
|
109
|
+
# setup :activate_authlogic
|
110
|
+
#
|
111
|
+
# You also get a controller method that you can test off of. For example:
|
112
|
+
#
|
113
|
+
# ben = users(:ben)
|
114
|
+
# assert_nil controller.session["user_credentials"]
|
115
|
+
# assert UserSession.create(ben)
|
116
|
+
# assert_equal controller.session["user_credentials"], ben.persistence_token
|
117
|
+
#
|
118
|
+
# See how I am checking that Authlogic is interacting with the controller
|
119
|
+
# properly? That's the idea here.
|
120
|
+
#
|
121
|
+
# === Testing with Rails 5
|
122
|
+
#
|
123
|
+
# Rails 5 has [deprecated classic controller tests](https://goo.gl/4zmt6y).
|
124
|
+
# Controller tests now inherit from `ActionDispatch::IntegrationTest` making
|
125
|
+
# them plain old integration tests now. You have two options for testing
|
126
|
+
# AuthLogic in Rails 5:
|
127
|
+
#
|
128
|
+
# * Add the `rails-controller-testing` gem to bring back the original
|
129
|
+
# controller testing usage
|
130
|
+
# * Go full steam ahead with integration testing and actually log a user in
|
131
|
+
# by submitting a form in the integration test.
|
132
|
+
#
|
133
|
+
# Naturally DHH recommends the second method and this is
|
134
|
+
# [what he does in his own tests](https://goo.gl/Ar6p0u). This is useful
|
135
|
+
# for testing not only AuthLogic itself (submitting login credentials to a
|
136
|
+
# UserSessionsController, for example) but any controller action that is
|
137
|
+
# behind a login wall. Add a helper method and use that before testing your
|
138
|
+
# actual controller action:
|
139
|
+
#
|
140
|
+
# # test/test_helper.rb
|
141
|
+
# def login(user)
|
142
|
+
# post user_sessions_url, :params => { :email => user.email, :password => 'password' }
|
143
|
+
# end
|
144
|
+
#
|
145
|
+
# # test/controllers/posts_controller_test.rb
|
146
|
+
# test "#create requires a user to be logged in
|
147
|
+
# post posts_url, :params => { :body => 'Lorem ipsum' }
|
148
|
+
#
|
149
|
+
# assert_redirected_to new_user_session_url
|
150
|
+
# end
|
151
|
+
#
|
152
|
+
# test "#create lets a logged in user create a new post" do
|
153
|
+
# login(users(:admin))
|
154
|
+
#
|
155
|
+
# assert_difference 'Posts.count' do
|
156
|
+
# post posts_url, :params => { :body => 'Lorem ipsum' }
|
157
|
+
# end
|
158
|
+
#
|
159
|
+
# assert_redirected_to posts_url
|
160
|
+
# end
|
161
|
+
#
|
162
|
+
# You still have access to the `session` helper in an integration test and so
|
163
|
+
# you can still test to see if a user is logged in. A couple of helper methods
|
164
|
+
# might look like:
|
165
|
+
#
|
166
|
+
# # test/test_helper.rb
|
167
|
+
# def assert_logged_in
|
168
|
+
# assert session[:user_credentials].present?
|
169
|
+
# end
|
170
|
+
#
|
171
|
+
# def assert_not_logged_in
|
172
|
+
# assert session[:user_credentials].blank?
|
173
|
+
# end
|
174
|
+
#
|
175
|
+
# # test/user_sessions_controller_test.rb
|
176
|
+
# test "#create logs in a user" do
|
177
|
+
# login(users(:admin))
|
178
|
+
#
|
179
|
+
# assert_logged_in
|
180
|
+
# end
|
181
|
+
module TestCase
|
182
|
+
def initialize(*args)
|
183
|
+
@request = nil
|
184
|
+
super
|
185
|
+
end
|
186
|
+
|
187
|
+
# Activates authlogic so that you can use it in your tests. You should call
|
188
|
+
# this method in your test's setup. Ex:
|
189
|
+
#
|
190
|
+
# setup :activate_authlogic
|
191
|
+
def activate_authlogic
|
192
|
+
if @request && !@request.respond_to?(:params)
|
193
|
+
class <<@request
|
194
|
+
alias_method :params, :parameters
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
Authlogic::Session::Base.controller = @request &&
|
199
|
+
Authlogic::TestCase::RailsRequestAdapter.new(@request) ||
|
200
|
+
controller
|
201
|
+
end
|
202
|
+
|
203
|
+
# The Authlogic::TestCase::MockController object passed to Authlogic to
|
204
|
+
# activate it. You can access this in your test. See the module description
|
205
|
+
# for an example.
|
206
|
+
def controller
|
207
|
+
@controller ||= Authlogic::TestCase::MockController.new
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# TODO: Why are these lines inside the `Authlogic` module? Should be outside?
|
212
|
+
::Test::Unit::TestCase.send(:include, TestCase) if defined?(::Test::Unit::TestCase)
|
213
|
+
::MiniTest::Unit::TestCase.send(:include, TestCase) if defined?(::MiniTest::Unit::TestCase)
|
214
|
+
::MiniTest::Test.send(:include, TestCase) if defined?(::MiniTest::Test)
|
215
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
|
5
|
+
# :nodoc:
|
6
|
+
module Authlogic
|
7
|
+
# Returns a `::Gem::Version`, the version number of the authlogic gem.
|
8
|
+
#
|
9
|
+
# It is preferable for a library to provide a `gem_version` method, rather
|
10
|
+
# than a `VERSION` string, because `::Gem::Version` is easier to use in a
|
11
|
+
# comparison.
|
12
|
+
#
|
13
|
+
# We cannot return a frozen `Version`, because rubygems will try to modify it.
|
14
|
+
# https://github.com/binarylogic/authlogic/pull/590
|
15
|
+
#
|
16
|
+
# Added in 4.0.0
|
17
|
+
#
|
18
|
+
# @api public
|
19
|
+
def self.gem_version
|
20
|
+
::Gem::Version.new("6.5")
|
21
|
+
end
|
22
|
+
end
|
data/lib/authlogic.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Authlogic uses ActiveSupport's core extensions like `strip_heredoc` and
|
4
|
+
# `squish`. ActiveRecord does not `require` these exensions, so we must.
|
5
|
+
#
|
6
|
+
# It's possible that we could save a few milliseconds by loading only the
|
7
|
+
# specific core extensions we need, but `all.rb` is simpler. We can revisit this
|
8
|
+
# decision if it becomes a problem.
|
9
|
+
require "active_support/all"
|
10
|
+
|
11
|
+
require "active_record"
|
12
|
+
|
13
|
+
path = File.dirname(__FILE__) + "/authlogic/"
|
14
|
+
|
15
|
+
[
|
16
|
+
"errors",
|
17
|
+
"i18n",
|
18
|
+
"random",
|
19
|
+
"config",
|
20
|
+
|
21
|
+
"controller_adapters/abstract_adapter",
|
22
|
+
"cookie_credentials",
|
23
|
+
|
24
|
+
"crypto_providers",
|
25
|
+
|
26
|
+
"acts_as_authentic/email",
|
27
|
+
"acts_as_authentic/logged_in_status",
|
28
|
+
"acts_as_authentic/login",
|
29
|
+
"acts_as_authentic/magic_columns",
|
30
|
+
"acts_as_authentic/password",
|
31
|
+
"acts_as_authentic/perishable_token",
|
32
|
+
"acts_as_authentic/persistence_token",
|
33
|
+
"acts_as_authentic/session_maintenance",
|
34
|
+
"acts_as_authentic/single_access_token",
|
35
|
+
"acts_as_authentic/base",
|
36
|
+
|
37
|
+
"session/magic_column/assigns_last_request_at",
|
38
|
+
"session/base"
|
39
|
+
].each do |library|
|
40
|
+
require path + library
|
41
|
+
end
|
42
|
+
|
43
|
+
require path + "controller_adapters/rails_adapter" if defined?(Rails)
|
44
|
+
require path + "controller_adapters/sinatra_adapter" if defined?(Sinatra)
|