cloudfoundry-devise 1.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/.travis.yml +13 -0
- data/CHANGELOG.rdoc +755 -0
- data/Gemfile +35 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +366 -0
- data/Rakefile +34 -0
- data/app/controllers/devise/confirmations_controller.rb +46 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
- data/app/controllers/devise/passwords_controller.rb +50 -0
- data/app/controllers/devise/registrations_controller.rb +114 -0
- data/app/controllers/devise/sessions_controller.rb +49 -0
- data/app/controllers/devise/unlocks_controller.rb +34 -0
- data/app/helpers/devise_helper.rb +25 -0
- data/app/mailers/devise/mailer.rb +15 -0
- data/app/views/devise/confirmations/new.html.erb +12 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise/passwords/edit.html.erb +16 -0
- data/app/views/devise/passwords/new.html.erb +12 -0
- data/app/views/devise/registrations/edit.html.erb +25 -0
- data/app/views/devise/registrations/new.html.erb +18 -0
- data/app/views/devise/sessions/new.html.erb +17 -0
- data/app/views/devise/shared/_links.erb +25 -0
- data/app/views/devise/unlocks/new.html.erb +12 -0
- data/cloudfoundry-devise.gemspec +25 -0
- data/config/locales/en.yml +59 -0
- data/lib/devise.rb +453 -0
- data/lib/devise/controllers/helpers.rb +260 -0
- data/lib/devise/controllers/internal_helpers.rb +161 -0
- data/lib/devise/controllers/rememberable.rb +52 -0
- data/lib/devise/controllers/scoped_views.rb +33 -0
- data/lib/devise/controllers/shared_helpers.rb +26 -0
- data/lib/devise/controllers/url_helpers.rb +53 -0
- data/lib/devise/delegator.rb +16 -0
- data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
- data/lib/devise/encryptors/base.rb +20 -0
- data/lib/devise/encryptors/clearance_sha1.rb +17 -0
- data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
- data/lib/devise/encryptors/sha1.rb +25 -0
- data/lib/devise/encryptors/sha512.rb +25 -0
- data/lib/devise/failure_app.rb +149 -0
- data/lib/devise/hooks/activatable.rb +11 -0
- data/lib/devise/hooks/forgetable.rb +9 -0
- data/lib/devise/hooks/rememberable.rb +6 -0
- data/lib/devise/hooks/timeoutable.rb +24 -0
- data/lib/devise/hooks/trackable.rb +9 -0
- data/lib/devise/mailers/helpers.rb +86 -0
- data/lib/devise/mapping.rb +175 -0
- data/lib/devise/models.rb +91 -0
- data/lib/devise/models/authenticatable.rb +181 -0
- data/lib/devise/models/confirmable.rb +220 -0
- data/lib/devise/models/database_authenticatable.rb +122 -0
- data/lib/devise/models/encryptable.rb +72 -0
- data/lib/devise/models/lockable.rb +169 -0
- data/lib/devise/models/omniauthable.rb +23 -0
- data/lib/devise/models/recoverable.rb +136 -0
- data/lib/devise/models/registerable.rb +21 -0
- data/lib/devise/models/rememberable.rb +114 -0
- data/lib/devise/models/serializable.rb +43 -0
- data/lib/devise/models/timeoutable.rb +45 -0
- data/lib/devise/models/token_authenticatable.rb +72 -0
- data/lib/devise/models/trackable.rb +30 -0
- data/lib/devise/models/validatable.rb +62 -0
- data/lib/devise/modules.rb +30 -0
- data/lib/devise/omniauth.rb +28 -0
- data/lib/devise/omniauth/config.rb +45 -0
- data/lib/devise/omniauth/url_helpers.rb +33 -0
- data/lib/devise/orm/active_record.rb +44 -0
- data/lib/devise/orm/mongoid.rb +31 -0
- data/lib/devise/param_filter.rb +41 -0
- data/lib/devise/path_checker.rb +18 -0
- data/lib/devise/rails.rb +73 -0
- data/lib/devise/rails/routes.rb +385 -0
- data/lib/devise/rails/warden_compat.rb +120 -0
- data/lib/devise/schema.rb +109 -0
- data/lib/devise/strategies/authenticatable.rb +155 -0
- data/lib/devise/strategies/base.rb +15 -0
- data/lib/devise/strategies/database_authenticatable.rb +21 -0
- data/lib/devise/strategies/rememberable.rb +53 -0
- data/lib/devise/strategies/token_authenticatable.rb +57 -0
- data/lib/devise/test_helpers.rb +90 -0
- data/lib/devise/version.rb +3 -0
- data/lib/generators/active_record/devise_generator.rb +71 -0
- data/lib/generators/active_record/templates/migration.rb +29 -0
- data/lib/generators/active_record/templates/migration_existing.rb +26 -0
- data/lib/generators/devise/devise_generator.rb +22 -0
- data/lib/generators/devise/install_generator.rb +24 -0
- data/lib/generators/devise/orm_helpers.rb +31 -0
- data/lib/generators/devise/views_generator.rb +98 -0
- data/lib/generators/mongoid/devise_generator.rb +60 -0
- data/lib/generators/templates/README +32 -0
- data/lib/generators/templates/devise.rb +215 -0
- data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
- data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
- data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
- data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
- data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
- data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
- data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
- data/test/controllers/helpers_test.rb +254 -0
- data/test/controllers/internal_helpers_test.rb +96 -0
- data/test/controllers/sessions_controller_test.rb +16 -0
- data/test/controllers/url_helpers_test.rb +59 -0
- data/test/delegator_test.rb +19 -0
- data/test/devise_test.rb +72 -0
- data/test/encryptors_test.rb +30 -0
- data/test/failure_app_test.rb +207 -0
- data/test/generators/active_record_generator_test.rb +47 -0
- data/test/generators/devise_generator_test.rb +39 -0
- data/test/generators/install_generator_test.rb +13 -0
- data/test/generators/mongoid_generator_test.rb +23 -0
- data/test/generators/views_generator_test.rb +52 -0
- data/test/helpers/devise_helper_test.rb +51 -0
- data/test/indifferent_hash.rb +33 -0
- data/test/integration/authenticatable_test.rb +590 -0
- data/test/integration/confirmable_test.rb +262 -0
- data/test/integration/database_authenticatable_test.rb +82 -0
- data/test/integration/http_authenticatable_test.rb +82 -0
- data/test/integration/lockable_test.rb +212 -0
- data/test/integration/omniauthable_test.rb +133 -0
- data/test/integration/recoverable_test.rb +287 -0
- data/test/integration/registerable_test.rb +335 -0
- data/test/integration/rememberable_test.rb +158 -0
- data/test/integration/timeoutable_test.rb +98 -0
- data/test/integration/token_authenticatable_test.rb +148 -0
- data/test/integration/trackable_test.rb +92 -0
- data/test/mailers/confirmation_instructions_test.rb +95 -0
- data/test/mailers/reset_password_instructions_test.rb +83 -0
- data/test/mailers/unlock_instructions_test.rb +77 -0
- data/test/mapping_test.rb +128 -0
- data/test/models/confirmable_test.rb +334 -0
- data/test/models/database_authenticatable_test.rb +167 -0
- data/test/models/encryptable_test.rb +67 -0
- data/test/models/lockable_test.rb +225 -0
- data/test/models/recoverable_test.rb +198 -0
- data/test/models/rememberable_test.rb +168 -0
- data/test/models/serializable_test.rb +38 -0
- data/test/models/timeoutable_test.rb +42 -0
- data/test/models/token_authenticatable_test.rb +49 -0
- data/test/models/trackable_test.rb +5 -0
- data/test/models/validatable_test.rb +113 -0
- data/test/models_test.rb +109 -0
- data/test/omniauth/config_test.rb +57 -0
- data/test/omniauth/url_helpers_test.rb +58 -0
- data/test/orm/active_record.rb +9 -0
- data/test/orm/mongoid.rb +14 -0
- data/test/rails_app/Rakefile +10 -0
- data/test/rails_app/app/active_record/admin.rb +6 -0
- data/test/rails_app/app/active_record/shim.rb +2 -0
- data/test/rails_app/app/active_record/user.rb +6 -0
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
- data/test/rails_app/app/controllers/admins_controller.rb +6 -0
- data/test/rails_app/app/controllers/application_controller.rb +8 -0
- data/test/rails_app/app/controllers/home_controller.rb +25 -0
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
- data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
- data/test/rails_app/app/controllers/users_controller.rb +23 -0
- data/test/rails_app/app/helpers/application_helper.rb +3 -0
- data/test/rails_app/app/mailers/users/mailer.rb +3 -0
- data/test/rails_app/app/mongoid/admin.rb +24 -0
- data/test/rails_app/app/mongoid/shim.rb +24 -0
- data/test/rails_app/app/mongoid/user.rb +45 -0
- data/test/rails_app/app/views/admins/index.html.erb +1 -0
- data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
- data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/home/index.html.erb +1 -0
- data/test/rails_app/app/views/home/join.html.erb +1 -0
- data/test/rails_app/app/views/home/private.html.erb +1 -0
- data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/layouts/application.html.erb +24 -0
- data/test/rails_app/app/views/users/index.html.erb +1 -0
- data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
- data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/config/application.rb +41 -0
- data/test/rails_app/config/boot.rb +8 -0
- data/test/rails_app/config/database.yml +18 -0
- data/test/rails_app/config/environment.rb +5 -0
- data/test/rails_app/config/environments/development.rb +18 -0
- data/test/rails_app/config/environments/production.rb +33 -0
- data/test/rails_app/config/environments/test.rb +33 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test/rails_app/config/initializers/devise.rb +197 -0
- data/test/rails_app/config/initializers/inflections.rb +2 -0
- data/test/rails_app/config/initializers/secret_token.rb +2 -0
- data/test/rails_app/config/routes.rb +87 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +71 -0
- data/test/rails_app/db/schema.rb +52 -0
- data/test/rails_app/lib/shared_admin.rb +10 -0
- data/test/rails_app/lib/shared_user.rb +26 -0
- data/test/rails_app/public/404.html +26 -0
- data/test/rails_app/public/422.html +26 -0
- data/test/rails_app/public/500.html +26 -0
- data/test/rails_app/public/favicon.ico +0 -0
- data/test/rails_app/script/rails +10 -0
- data/test/routes_test.rb +240 -0
- data/test/support/assertions.rb +27 -0
- data/test/support/helpers.rb +109 -0
- data/test/support/integration.rb +88 -0
- data/test/support/locale/en.yml +4 -0
- data/test/support/webrat/integrations/rails.rb +24 -0
- data/test/test_helper.rb +27 -0
- data/test/test_helpers_test.rb +134 -0
- metadata +295 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SessionsControllerTest < ActionController::TestCase
|
4
|
+
tests Devise::SessionsController
|
5
|
+
include Devise::TestHelpers
|
6
|
+
|
7
|
+
test "#create doesn't raise exception after Warden authentication fails when TestHelpers included" do
|
8
|
+
request.env["devise.mapping"] = Devise.mappings[:user]
|
9
|
+
post :create, :user => {
|
10
|
+
:email => "nosuchuser@example.com",
|
11
|
+
:password => "wevdude"
|
12
|
+
}
|
13
|
+
assert_equal 200, @response.status
|
14
|
+
assert_template "devise/sessions/new"
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RoutesTest < ActionController::TestCase
|
4
|
+
tests ApplicationController
|
5
|
+
|
6
|
+
def assert_path_and_url(name, prepend_path=nil)
|
7
|
+
@request.path = '/users/session'
|
8
|
+
prepend_path = "#{prepend_path}_" if prepend_path
|
9
|
+
|
10
|
+
# Resource param
|
11
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user),
|
12
|
+
send(:"#{prepend_path}user_#{name}_path")
|
13
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user),
|
14
|
+
send(:"#{prepend_path}user_#{name}_url")
|
15
|
+
|
16
|
+
# Default url params
|
17
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user, :param => 123),
|
18
|
+
send(:"#{prepend_path}user_#{name}_path", :param => 123)
|
19
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user, :param => 123),
|
20
|
+
send(:"#{prepend_path}user_#{name}_url", :param => 123)
|
21
|
+
|
22
|
+
@request.path = nil
|
23
|
+
# With an object
|
24
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_path", User.new),
|
25
|
+
send(:"#{prepend_path}user_#{name}_path")
|
26
|
+
assert_equal @controller.send(:"#{prepend_path}#{name}_url", User.new),
|
27
|
+
send(:"#{prepend_path}user_#{name}_url")
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
test 'should alias session to mapped user session' do
|
32
|
+
assert_path_and_url :session
|
33
|
+
assert_path_and_url :session, :new
|
34
|
+
assert_path_and_url :session, :destroy
|
35
|
+
end
|
36
|
+
|
37
|
+
test 'should alias password to mapped user password' do
|
38
|
+
assert_path_and_url :password
|
39
|
+
assert_path_and_url :password, :new
|
40
|
+
assert_path_and_url :password, :edit
|
41
|
+
end
|
42
|
+
|
43
|
+
test 'should alias confirmation to mapped user confirmation' do
|
44
|
+
assert_path_and_url :confirmation
|
45
|
+
assert_path_and_url :confirmation, :new
|
46
|
+
end
|
47
|
+
|
48
|
+
test 'should alias unlock to mapped user unlock' do
|
49
|
+
assert_path_and_url :unlock
|
50
|
+
assert_path_and_url :unlock, :new
|
51
|
+
end
|
52
|
+
|
53
|
+
test 'should alias registration to mapped user registration' do
|
54
|
+
assert_path_and_url :registration
|
55
|
+
assert_path_and_url :registration, :new
|
56
|
+
assert_path_and_url :registration, :edit
|
57
|
+
assert_path_and_url :registration, :cancel
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DelegatorTest < ActiveSupport::TestCase
|
4
|
+
def delegator
|
5
|
+
Devise::Delegator.new
|
6
|
+
end
|
7
|
+
|
8
|
+
test 'failure_app returns default failure app if no warden options in env' do
|
9
|
+
assert_equal Devise::FailureApp, delegator.failure_app({})
|
10
|
+
end
|
11
|
+
|
12
|
+
test 'failure_app returns default failure app if no scope in warden options' do
|
13
|
+
assert_equal Devise::FailureApp, delegator.failure_app({"warden.options" => {}})
|
14
|
+
end
|
15
|
+
|
16
|
+
test 'failure_app returns associated failure app by scope in the given environment' do
|
17
|
+
assert_kind_of Proc, delegator.failure_app({"warden.options" => {:scope => "manager"}})
|
18
|
+
end
|
19
|
+
end
|
data/test/devise_test.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
def self.yield_and_restore
|
5
|
+
@@warden_configured = nil
|
6
|
+
c, b = @@warden_config, @@warden_config_block
|
7
|
+
yield
|
8
|
+
ensure
|
9
|
+
@@warden_config, @@warden_config_block = c, b
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class DeviseTest < ActiveSupport::TestCase
|
14
|
+
test 'model options can be configured through Devise' do
|
15
|
+
swap Devise, :confirm_within => 113, :pepper => "foo" do
|
16
|
+
assert_equal 113, Devise.confirm_within
|
17
|
+
assert_equal "foo", Devise.pepper
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
test 'setup block yields self' do
|
22
|
+
Devise.setup do |config|
|
23
|
+
assert_equal Devise, config
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
test 'stores warden configuration' do
|
28
|
+
assert_kind_of Devise::Delegator, Devise.warden_config.failure_app
|
29
|
+
assert_equal :user, Devise.warden_config.default_scope
|
30
|
+
end
|
31
|
+
|
32
|
+
test 'warden manager user configuration through a block' do
|
33
|
+
Devise.yield_and_restore do
|
34
|
+
@executed = false
|
35
|
+
Devise.warden do |config|
|
36
|
+
@executed = true
|
37
|
+
assert_kind_of Warden::Config, config
|
38
|
+
end
|
39
|
+
|
40
|
+
Devise.configure_warden!
|
41
|
+
assert @executed
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
test 'add new module using the helper method' do
|
46
|
+
assert_nothing_raised(Exception) { Devise.add_module(:coconut) }
|
47
|
+
assert_equal 1, Devise::ALL.select { |v| v == :coconut }.size
|
48
|
+
assert_not Devise::STRATEGIES.include?(:coconut)
|
49
|
+
assert_not defined?(Devise::Models::Coconut)
|
50
|
+
Devise::ALL.delete(:coconut)
|
51
|
+
|
52
|
+
assert_nothing_raised(Exception) { Devise.add_module(:banana, :strategy => :fruits) }
|
53
|
+
assert_equal :fruits, Devise::STRATEGIES[:banana]
|
54
|
+
Devise::ALL.delete(:banana)
|
55
|
+
Devise::STRATEGIES.delete(:banana)
|
56
|
+
|
57
|
+
assert_nothing_raised(Exception) { Devise.add_module(:kivi, :controller => :fruits) }
|
58
|
+
assert_equal :fruits, Devise::CONTROLLERS[:kivi]
|
59
|
+
Devise::ALL.delete(:kivi)
|
60
|
+
Devise::CONTROLLERS.delete(:kivi)
|
61
|
+
end
|
62
|
+
|
63
|
+
test 'should complain when comparing empty or different sized passes' do
|
64
|
+
[nil, ""].each do |empty|
|
65
|
+
assert_not Devise.secure_compare(empty, "something")
|
66
|
+
assert_not Devise.secure_compare("something", empty)
|
67
|
+
assert_not Devise.secure_compare(empty, empty)
|
68
|
+
end
|
69
|
+
assert_not Devise.secure_compare("size_1", "size_four")
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Encryptors < ActiveSupport::TestCase
|
4
|
+
test 'should match a password created by authlogic' do
|
5
|
+
authlogic = "b623c3bc9c775b0eb8edb218a382453396fec4146422853e66ecc4b6bc32d7162ee42074dcb5f180a770dc38b5df15812f09bbf497a4a1b95fe5e7d2b8eb7eb4"
|
6
|
+
encryptor = Devise::Encryptors::AuthlogicSha512.digest('123mudar', 20, 'usZK_z_EAaF61Gwkw-ed', '')
|
7
|
+
assert_equal authlogic, encryptor
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'should match a password created by restful_authentication' do
|
11
|
+
restful_authentication = "93110f71309ce91366375ea44e2a6f5cc73fa8d4"
|
12
|
+
encryptor = Devise::Encryptors::RestfulAuthenticationSha1.digest('123mudar', 10, '48901d2b247a54088acb7f8ea3e695e50fe6791b', 'fee9a51ec0a28d11be380ca6dee6b4b760c1a3bf')
|
13
|
+
assert_equal restful_authentication, encryptor
|
14
|
+
end
|
15
|
+
|
16
|
+
test 'should match a password created by clearance' do
|
17
|
+
clearance = "0f40bbae18ddefd7066276c3ef209d40729b0378"
|
18
|
+
encryptor = Devise::Encryptors::ClearanceSha1.digest('123mudar', nil, '65c58472c207c829f28c68619d3e3aefed18ab3f', nil)
|
19
|
+
assert_equal clearance, encryptor
|
20
|
+
end
|
21
|
+
|
22
|
+
Devise::ENCRYPTORS_LENGTH.each do |key, value|
|
23
|
+
test "should have length #{value} for #{key.inspect}" do
|
24
|
+
swap Devise, :encryptor => key do
|
25
|
+
encryptor = Devise::Encryptors.const_get(key.to_s.classify)
|
26
|
+
assert_equal value, encryptor.digest('a', 4, encryptor.salt(4), nil).size
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
class FailureTest < ActiveSupport::TestCase
|
5
|
+
class RootFailureApp < Devise::FailureApp
|
6
|
+
undef_method :new_user_session_path
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.context(name, &block)
|
10
|
+
instance_eval(&block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def call_failure(env_params={})
|
14
|
+
env = {
|
15
|
+
'REQUEST_URI' => 'http://test.host/',
|
16
|
+
'HTTP_HOST' => 'test.host',
|
17
|
+
'REQUEST_METHOD' => 'GET',
|
18
|
+
'warden.options' => { :scope => :user },
|
19
|
+
'rack.session' => {},
|
20
|
+
'action_dispatch.request.formats' => Array(env_params.delete('formats') || Mime::HTML),
|
21
|
+
'rack.input' => "",
|
22
|
+
'warden' => OpenStruct.new(:message => nil)
|
23
|
+
}.merge!(env_params)
|
24
|
+
|
25
|
+
@response = (env.delete(:app) || Devise::FailureApp).call(env).to_a
|
26
|
+
@request = ActionDispatch::Request.new(env)
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'When redirecting' do
|
30
|
+
test 'return to the default redirect location' do
|
31
|
+
call_failure
|
32
|
+
assert_equal 302, @response.first
|
33
|
+
assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
|
34
|
+
assert_equal 'http://test.host/users/sign_in', @response.second['Location']
|
35
|
+
end
|
36
|
+
|
37
|
+
test 'return to the default redirect location for wildcard requests' do
|
38
|
+
call_failure 'action_dispatch.request.formats' => nil, 'HTTP_ACCEPT' => '*/*'
|
39
|
+
assert_equal 302, @response.first
|
40
|
+
assert_equal 'http://test.host/users/sign_in', @response.second['Location']
|
41
|
+
end
|
42
|
+
|
43
|
+
test 'return to the root path if no session path is available' do
|
44
|
+
call_failure :app => RootFailureApp
|
45
|
+
assert_equal 302, @response.first
|
46
|
+
assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
|
47
|
+
assert_equal 'http://test.host/', @response.second['Location']
|
48
|
+
end
|
49
|
+
|
50
|
+
test 'uses the proxy failure message as symbol' do
|
51
|
+
call_failure('warden' => OpenStruct.new(:message => :test))
|
52
|
+
assert_equal 'test', @request.flash[:alert]
|
53
|
+
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
54
|
+
end
|
55
|
+
|
56
|
+
test 'uses the proxy failure message as string' do
|
57
|
+
call_failure('warden' => OpenStruct.new(:message => 'Hello world'))
|
58
|
+
assert_equal 'Hello world', @request.flash[:alert]
|
59
|
+
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
60
|
+
end
|
61
|
+
|
62
|
+
test 'set content type to default text/html' do
|
63
|
+
call_failure
|
64
|
+
assert_equal 'text/html; charset=utf-8', @response.second['Content-Type']
|
65
|
+
end
|
66
|
+
|
67
|
+
test 'setup a default message' do
|
68
|
+
call_failure
|
69
|
+
assert_match /You are being/, @response.last.body
|
70
|
+
assert_match /redirected/, @response.last.body
|
71
|
+
assert_match /users\/sign_in/, @response.last.body
|
72
|
+
end
|
73
|
+
|
74
|
+
test 'works for any navigational format' do
|
75
|
+
swap Devise, :navigational_formats => [:xml] do
|
76
|
+
call_failure('formats' => :xml)
|
77
|
+
assert_equal 302, @response.first
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
test 'redirects the correct format if it is a non-html format request' do
|
82
|
+
swap Devise, :navigational_formats => [:js] do
|
83
|
+
call_failure('formats' => :js)
|
84
|
+
assert_equal 'http://test.host/users/sign_in.js', @response.second["Location"]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'For HTTP request' do
|
90
|
+
test 'return 401 status' do
|
91
|
+
call_failure('formats' => :xml)
|
92
|
+
assert_equal 401, @response.first
|
93
|
+
end
|
94
|
+
|
95
|
+
test 'return appropriate body for xml' do
|
96
|
+
call_failure('formats' => :xml)
|
97
|
+
result = %(<?xml version="1.0" encoding="UTF-8"?>\n<errors>\n <error>You need to sign in or sign up before continuing.</error>\n</errors>\n)
|
98
|
+
assert_equal result, @response.last.body
|
99
|
+
end
|
100
|
+
|
101
|
+
test 'return appropriate body for json' do
|
102
|
+
call_failure('formats' => :json)
|
103
|
+
result = %({"error":"You need to sign in or sign up before continuing."})
|
104
|
+
assert_equal result, @response.last.body
|
105
|
+
end
|
106
|
+
|
107
|
+
test 'return 401 status for unknown formats' do
|
108
|
+
call_failure 'formats' => []
|
109
|
+
assert_equal 401, @response.first
|
110
|
+
end
|
111
|
+
|
112
|
+
test 'return WWW-authenticate headers if model allows' do
|
113
|
+
call_failure('formats' => :xml)
|
114
|
+
assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"]
|
115
|
+
end
|
116
|
+
|
117
|
+
test 'does not return WWW-authenticate headers if model does not allow' do
|
118
|
+
swap Devise, :http_authenticatable => false do
|
119
|
+
call_failure('formats' => :xml)
|
120
|
+
assert_nil @response.second["WWW-Authenticate"]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
test 'works for any non navigational format' do
|
125
|
+
swap Devise, :navigational_formats => [] do
|
126
|
+
call_failure('formats' => :html)
|
127
|
+
assert_equal 401, @response.first
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
test 'uses the failure message as response body' do
|
132
|
+
call_failure('formats' => :xml, 'warden' => OpenStruct.new(:message => :invalid))
|
133
|
+
assert_match '<error>Invalid email or password.</error>', @response.third.body
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'on ajax call' do
|
137
|
+
context 'when http_authenticatable_on_xhr is false' do
|
138
|
+
test 'dont return 401 with navigational formats' do
|
139
|
+
swap Devise, :http_authenticatable_on_xhr => false do
|
140
|
+
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
141
|
+
assert_equal 302, @response.first
|
142
|
+
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
test 'dont return 401 with non navigational formats' do
|
147
|
+
swap Devise, :http_authenticatable_on_xhr => false do
|
148
|
+
call_failure('formats' => :json, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
149
|
+
assert_equal 302, @response.first
|
150
|
+
assert_equal 'http://test.host/users/sign_in.json', @response.second["Location"]
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'when http_authenticatable_on_xhr is true' do
|
156
|
+
test 'return 401' do
|
157
|
+
swap Devise, :http_authenticatable_on_xhr => true do
|
158
|
+
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
159
|
+
assert_equal 401, @response.first
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
test 'skip WWW-Authenticate header' do
|
164
|
+
swap Devise, :http_authenticatable_on_xhr => true do
|
165
|
+
call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
166
|
+
assert_nil @response.second['WWW-Authenticate']
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context 'With recall' do
|
174
|
+
test 'calls the original controller if invalid email or password' do
|
175
|
+
env = {
|
176
|
+
"warden.options" => { :recall => "devise/sessions#new", :attempted_path => "/users/sign_in" },
|
177
|
+
"devise.mapping" => Devise.mappings[:user],
|
178
|
+
"warden" => stub_everything
|
179
|
+
}
|
180
|
+
call_failure(env)
|
181
|
+
assert @response.third.body.include?('<h2>Sign in</h2>')
|
182
|
+
assert @response.third.body.include?('Invalid email or password.')
|
183
|
+
end
|
184
|
+
|
185
|
+
test 'calls the original controller if not confirmed email' do
|
186
|
+
env = {
|
187
|
+
"warden.options" => { :recall => "devise/sessions#new", :attempted_path => "/users/sign_in", :message => :unconfirmed },
|
188
|
+
"devise.mapping" => Devise.mappings[:user],
|
189
|
+
"warden" => stub_everything
|
190
|
+
}
|
191
|
+
call_failure(env)
|
192
|
+
assert @response.third.body.include?('<h2>Sign in</h2>')
|
193
|
+
assert @response.third.body.include?('You have to confirm your account before continuing.')
|
194
|
+
end
|
195
|
+
|
196
|
+
test 'calls the original controller if inactive account' do
|
197
|
+
env = {
|
198
|
+
"warden.options" => { :recall => "devise/sessions#new", :attempted_path => "/users/sign_in", :message => :inactive },
|
199
|
+
"devise.mapping" => Devise.mappings[:user],
|
200
|
+
"warden" => stub_everything
|
201
|
+
}
|
202
|
+
call_failure(env)
|
203
|
+
assert @response.third.body.include?('<h2>Sign in</h2>')
|
204
|
+
assert @response.third.body.include?('Your account was not activated yet.')
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
if DEVISE_ORM == :active_record
|
4
|
+
require "generators/active_record/devise_generator"
|
5
|
+
|
6
|
+
class ActiveRecordGeneratorTest < Rails::Generators::TestCase
|
7
|
+
tests ActiveRecord::Generators::DeviseGenerator
|
8
|
+
destination File.expand_path("../../tmp", __FILE__)
|
9
|
+
setup :prepare_destination
|
10
|
+
|
11
|
+
test "all files are properly created" do
|
12
|
+
with_rails_version :MAJOR => 3, :MINOR => 0 do
|
13
|
+
run_generator %w(monster)
|
14
|
+
assert_file "app/models/monster.rb", /devise/, /attr_accessible (:[a-z_]+(, )?)+/
|
15
|
+
assert_migration "db/migrate/devise_create_monsters.rb", /def self\.up/
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
test "all files are properly created with rails31 migration syntax" do
|
20
|
+
with_rails_version :MAJOR => 3, :MINOR => 1 do
|
21
|
+
run_generator %w(monster)
|
22
|
+
assert_file "app/models/monster.rb", /devise/, /attr_accessible (:[a-z_]+(, )?)+/
|
23
|
+
assert_migration "db/migrate/devise_create_monsters.rb", /def change/
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
test "update model migration when model exists" do
|
28
|
+
run_generator %w(monster)
|
29
|
+
assert_file "app/models/monster.rb"
|
30
|
+
run_generator %w(monster)
|
31
|
+
assert_migration "db/migrate/add_devise_to_monsters.rb"
|
32
|
+
end
|
33
|
+
|
34
|
+
test "all files are properly deleted" do
|
35
|
+
run_generator %w(monster)
|
36
|
+
run_generator %w(monster)
|
37
|
+
assert_migration "db/migrate/devise_create_monsters.rb"
|
38
|
+
assert_migration "db/migrate/add_devise_to_monsters.rb"
|
39
|
+
run_generator %w(monster), :behavior => :revoke
|
40
|
+
assert_no_migration "db/migrate/add_devise_to_monsters.rb"
|
41
|
+
assert_migration "db/migrate/devise_create_monsters.rb"
|
42
|
+
run_generator %w(monster), :behavior => :revoke
|
43
|
+
assert_no_file "app/models/monster.rb"
|
44
|
+
assert_no_migration "db/migrate/devise_create_monsters.rb"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|