namxam-devise 1.1.0.win

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. data/CHANGELOG.rdoc +455 -0
  2. data/Gemfile +23 -0
  3. data/Gemfile.lock +118 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +311 -0
  6. data/Rakefile +55 -0
  7. data/TODO +3 -0
  8. data/app/controllers/devise/confirmations_controller.rb +33 -0
  9. data/app/controllers/devise/passwords_controller.rb +41 -0
  10. data/app/controllers/devise/registrations_controller.rb +57 -0
  11. data/app/controllers/devise/sessions_controller.rb +23 -0
  12. data/app/controllers/devise/unlocks_controller.rb +34 -0
  13. data/app/helpers/devise_helper.rb +17 -0
  14. data/app/mailers/devise/mailer.rb +71 -0
  15. data/app/views/devise/confirmations/new.html.erb +12 -0
  16. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  17. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  18. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  19. data/app/views/devise/passwords/edit.html.erb +16 -0
  20. data/app/views/devise/passwords/new.html.erb +12 -0
  21. data/app/views/devise/registrations/edit.html.erb +25 -0
  22. data/app/views/devise/registrations/new.html.erb +18 -0
  23. data/app/views/devise/sessions/new.html.erb +17 -0
  24. data/app/views/devise/shared/_links.erb +19 -0
  25. data/app/views/devise/unlocks/new.html.erb +12 -0
  26. data/config/locales/en.yml +39 -0
  27. data/lib/devise.rb +290 -0
  28. data/lib/devise/controllers/helpers.rb +231 -0
  29. data/lib/devise/controllers/internal_helpers.rb +98 -0
  30. data/lib/devise/controllers/scoped_views.rb +35 -0
  31. data/lib/devise/controllers/url_helpers.rb +41 -0
  32. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  33. data/lib/devise/encryptors/base.rb +20 -0
  34. data/lib/devise/encryptors/bcrypt.rb +19 -0
  35. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  36. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  37. data/lib/devise/encryptors/sha1.rb +25 -0
  38. data/lib/devise/encryptors/sha512.rb +25 -0
  39. data/lib/devise/failure_app.rb +107 -0
  40. data/lib/devise/hooks/activatable.rb +11 -0
  41. data/lib/devise/hooks/forgetable.rb +11 -0
  42. data/lib/devise/hooks/rememberable.rb +35 -0
  43. data/lib/devise/hooks/timeoutable.rb +22 -0
  44. data/lib/devise/hooks/trackable.rb +9 -0
  45. data/lib/devise/mapping.rb +103 -0
  46. data/lib/devise/models.rb +80 -0
  47. data/lib/devise/models/authenticatable.rb +126 -0
  48. data/lib/devise/models/confirmable.rb +164 -0
  49. data/lib/devise/models/database_authenticatable.rb +110 -0
  50. data/lib/devise/models/lockable.rb +165 -0
  51. data/lib/devise/models/recoverable.rb +81 -0
  52. data/lib/devise/models/registerable.rb +8 -0
  53. data/lib/devise/models/rememberable.rb +104 -0
  54. data/lib/devise/models/timeoutable.rb +26 -0
  55. data/lib/devise/models/token_authenticatable.rb +60 -0
  56. data/lib/devise/models/trackable.rb +30 -0
  57. data/lib/devise/models/validatable.rb +53 -0
  58. data/lib/devise/modules.rb +23 -0
  59. data/lib/devise/orm/active_record.rb +36 -0
  60. data/lib/devise/orm/mongoid.rb +29 -0
  61. data/lib/devise/path_checker.rb +18 -0
  62. data/lib/devise/rails.rb +69 -0
  63. data/lib/devise/rails/routes.rb +248 -0
  64. data/lib/devise/rails/warden_compat.rb +39 -0
  65. data/lib/devise/schema.rb +97 -0
  66. data/lib/devise/strategies/authenticatable.rb +111 -0
  67. data/lib/devise/strategies/base.rb +33 -0
  68. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  69. data/lib/devise/strategies/rememberable.rb +43 -0
  70. data/lib/devise/strategies/token_authenticatable.rb +49 -0
  71. data/lib/devise/test_helpers.rb +90 -0
  72. data/lib/devise/version.rb +3 -0
  73. data/lib/generators/active_record/devise_generator.rb +28 -0
  74. data/lib/generators/active_record/templates/migration.rb +29 -0
  75. data/lib/generators/devise/devise_generator.rb +17 -0
  76. data/lib/generators/devise/install_generator.rb +24 -0
  77. data/lib/generators/devise/orm_helpers.rb +23 -0
  78. data/lib/generators/devise/templates/README +25 -0
  79. data/lib/generators/devise/templates/devise.rb +139 -0
  80. data/lib/generators/devise/views_generator.rb +63 -0
  81. data/lib/generators/devise_install_generator.rb +4 -0
  82. data/lib/generators/devise_views_generator.rb +4 -0
  83. data/lib/generators/mongoid/devise_generator.rb +17 -0
  84. data/test/controllers/helpers_test.rb +213 -0
  85. data/test/controllers/internal_helpers_test.rb +51 -0
  86. data/test/controllers/url_helpers_test.rb +58 -0
  87. data/test/devise_test.rb +65 -0
  88. data/test/encryptors_test.rb +30 -0
  89. data/test/failure_app_test.rb +123 -0
  90. data/test/integration/authenticatable_test.rb +344 -0
  91. data/test/integration/confirmable_test.rb +104 -0
  92. data/test/integration/database_authenticatable_test.rb +38 -0
  93. data/test/integration/http_authenticatable_test.rb +49 -0
  94. data/test/integration/lockable_test.rb +109 -0
  95. data/test/integration/recoverable_test.rb +141 -0
  96. data/test/integration/registerable_test.rb +153 -0
  97. data/test/integration/rememberable_test.rb +91 -0
  98. data/test/integration/timeoutable_test.rb +80 -0
  99. data/test/integration/token_authenticatable_test.rb +88 -0
  100. data/test/integration/trackable_test.rb +64 -0
  101. data/test/mailers/confirmation_instructions_test.rb +80 -0
  102. data/test/mailers/reset_password_instructions_test.rb +68 -0
  103. data/test/mailers/unlock_instructions_test.rb +62 -0
  104. data/test/mapping_test.rb +85 -0
  105. data/test/models/confirmable_test.rb +221 -0
  106. data/test/models/database_authenticatable_test.rb +148 -0
  107. data/test/models/lockable_test.rb +188 -0
  108. data/test/models/recoverable_test.rb +138 -0
  109. data/test/models/rememberable_test.rb +176 -0
  110. data/test/models/timeoutable_test.rb +28 -0
  111. data/test/models/token_authenticatable_test.rb +37 -0
  112. data/test/models/trackable_test.rb +5 -0
  113. data/test/models/validatable_test.rb +99 -0
  114. data/test/models_test.rb +77 -0
  115. data/test/orm/active_record.rb +9 -0
  116. data/test/orm/mongoid.rb +10 -0
  117. data/test/rails_app/app/active_record/admin.rb +3 -0
  118. data/test/rails_app/app/active_record/shim.rb +2 -0
  119. data/test/rails_app/app/active_record/user.rb +7 -0
  120. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  121. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  122. data/test/rails_app/app/controllers/home_controller.rb +7 -0
  123. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  124. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  125. data/test/rails_app/app/controllers/sessions_controller.rb +6 -0
  126. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  127. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  128. data/test/rails_app/app/mongoid/admin.rb +6 -0
  129. data/test/rails_app/app/mongoid/shim.rb +16 -0
  130. data/test/rails_app/app/mongoid/user.rb +10 -0
  131. data/test/rails_app/config/application.rb +35 -0
  132. data/test/rails_app/config/boot.rb +13 -0
  133. data/test/rails_app/config/environment.rb +5 -0
  134. data/test/rails_app/config/environments/development.rb +19 -0
  135. data/test/rails_app/config/environments/production.rb +33 -0
  136. data/test/rails_app/config/environments/test.rb +33 -0
  137. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  138. data/test/rails_app/config/initializers/devise.rb +136 -0
  139. data/test/rails_app/config/initializers/inflections.rb +2 -0
  140. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  141. data/test/rails_app/config/routes.rb +47 -0
  142. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +27 -0
  143. data/test/rails_app/db/schema.rb +86 -0
  144. data/test/routes_test.rb +146 -0
  145. data/test/support/assertions.rb +24 -0
  146. data/test/support/helpers.rb +54 -0
  147. data/test/support/integration.rb +88 -0
  148. data/test/support/test_silencer.rb +5 -0
  149. data/test/support/webrat/integrations/rails.rb +32 -0
  150. data/test/test_helper.rb +21 -0
  151. data/test/test_helpers_test.rb +72 -0
  152. metadata +230 -0
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+
3
+ class MyController < ApplicationController
4
+ include Devise::Controllers::InternalHelpers
5
+ end
6
+
7
+ class HelpersTest < ActionController::TestCase
8
+ tests MyController
9
+
10
+ def setup
11
+ @mock_warden = OpenStruct.new
12
+ @controller.request.env['warden'] = @mock_warden
13
+ @controller.request.env['devise.mapping'] = Devise.mappings[:user]
14
+ end
15
+
16
+ test 'get resource name from env' do
17
+ assert_equal :user, @controller.resource_name
18
+ end
19
+
20
+ test 'get resource class from env' do
21
+ assert_equal User, @controller.resource_class
22
+ end
23
+
24
+ test 'get resource instance variable from env' do
25
+ @controller.instance_variable_set(:@user, admin = Admin.new)
26
+ assert_equal admin, @controller.resource
27
+ end
28
+
29
+ test 'set resource instance variable from env' do
30
+ admin = @controller.send(:resource_class).new
31
+ @controller.send(:resource=, admin)
32
+
33
+ assert_equal admin, @controller.send(:resource)
34
+ assert_equal admin, @controller.instance_variable_get(:@user)
35
+ end
36
+
37
+ test 'resources methods are not controller actions' do
38
+ assert @controller.class.action_methods.empty?
39
+ end
40
+
41
+ test 'require no authentication tests current mapping' do
42
+ @controller.expects(:resource_name).returns(:user).twice
43
+ @mock_warden.expects(:authenticated?).with(:user).returns(true)
44
+ @controller.expects(:redirect_to).with(root_path)
45
+ @controller.send :require_no_authentication
46
+ end
47
+
48
+ test 'is a devise controller' do
49
+ assert @controller.devise_controller?
50
+ end
51
+ end
@@ -0,0 +1,58 @@
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 AR 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
+ end
58
+ end
@@ -0,0 +1,65 @@
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_equal Devise::FailureApp, 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
+
62
+ assert_nothing_raised(Exception) { Devise.add_module(:authenticatable_again, :model => 'devise/model/authenticatable') }
63
+ assert defined?(Devise::Models::AuthenticatableAgain)
64
+ end
65
+ 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,123 @@
1
+ require 'test_helper'
2
+ require 'ostruct'
3
+
4
+ class FailureTest < ActiveSupport::TestCase
5
+ def self.context(name, &block)
6
+ instance_eval(&block)
7
+ end
8
+
9
+ def call_failure(env_params={})
10
+ env = {
11
+ 'REQUEST_URI' => 'http://test.host/',
12
+ 'HTTP_HOST' => 'test.host',
13
+ 'REQUEST_METHOD' => 'GET',
14
+ 'warden.options' => { :scope => :user },
15
+ 'rack.session' => {},
16
+ 'action_dispatch.request.formats' => Array(env_params.delete('formats') || :html),
17
+ 'rack.input' => "",
18
+ 'warden' => OpenStruct.new(:message => nil)
19
+ }.merge!(env_params)
20
+
21
+ @response = Devise::FailureApp.call(env).to_a
22
+ @request = ActionDispatch::Request.new(env)
23
+ end
24
+
25
+ context 'When redirecting' do
26
+ test 'return 302 status' do
27
+ call_failure
28
+ assert_equal 302, @response.first
29
+ end
30
+
31
+ test 'return to the default redirect location' do
32
+ call_failure
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 'uses the proxy failure message as symbol' do
38
+ call_failure('warden' => OpenStruct.new(:message => :test))
39
+ assert_equal 'test', @request.flash[:alert]
40
+ assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
41
+ end
42
+
43
+ test 'uses the proxy failure message as string' do
44
+ call_failure('warden' => OpenStruct.new(:message => 'Hello world'))
45
+ assert_equal 'Hello world', @request.flash[:alert]
46
+ assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
47
+ end
48
+
49
+ test 'set content type to default text/html' do
50
+ call_failure
51
+ assert_equal 'text/html; charset=utf-8', @response.second['Content-Type']
52
+ end
53
+
54
+ test 'setup a default message' do
55
+ call_failure
56
+ assert_match /You are being/, @response.last.body
57
+ assert_match /redirected/, @response.last.body
58
+ assert_match /users\/sign_in/, @response.last.body
59
+ end
60
+
61
+ test 'works for any navigational format' do
62
+ swap Devise, :navigational_formats => [:xml] do
63
+ call_failure('formats' => :xml)
64
+ assert_equal 302, @response.first
65
+ end
66
+ end
67
+ end
68
+
69
+ context 'For HTTP request' do
70
+ test 'return 401 status' do
71
+ call_failure('formats' => :xml)
72
+ assert_equal 401, @response.first
73
+ end
74
+
75
+ test 'return WWW-authenticate headers' do
76
+ call_failure('formats' => :xml)
77
+ assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"]
78
+ end
79
+
80
+ test 'dont return WWW-authenticate on ajax call if http_authenticatable_on_xhr false' do
81
+ swap Devise, :http_authenticatable_on_xhr => false do
82
+ call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
83
+ assert_equal 302, @response.first
84
+ assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
85
+ assert_nil @response.second['WWW-Authenticate']
86
+ end
87
+ end
88
+
89
+ test 'return WWW-authenticate on ajax call if http_authenticatable_on_xhr true' do
90
+ swap Devise, :http_authenticatable_on_xhr => true do
91
+ call_failure('formats' => :html, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
92
+ assert_equal 401, @response.first
93
+ assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"]
94
+ end
95
+ end
96
+
97
+ test 'uses the proxy failure message as response body' do
98
+ call_failure('formats' => :xml, 'warden' => OpenStruct.new(:message => :invalid))
99
+ assert_match '<error>Invalid email or password.</error>', @response.third.body
100
+ end
101
+
102
+ test 'works for any non navigational format' do
103
+ swap Devise, :navigational_formats => [] do
104
+ call_failure('formats' => :html)
105
+ assert_equal 401, @response.first
106
+ end
107
+ end
108
+ end
109
+
110
+ context 'With recall' do
111
+ test 'calls the original controller' do
112
+ env = {
113
+ "action_dispatch.request.parameters" => { :controller => "devise/sessions" },
114
+ "warden.options" => { :recall => "new", :attempted_path => "/users/sign_in" },
115
+ "devise.mapping" => Devise.mappings[:user],
116
+ "warden" => stub_everything
117
+ }
118
+ call_failure(env)
119
+ assert @response.third.body.include?('<h2>Sign in</h2>')
120
+ assert @response.third.body.include?('Invalid email or password.')
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,344 @@
1
+ require 'test_helper'
2
+
3
+ class AuthenticationSanityTest < ActionController::IntegrationTest
4
+
5
+ def setup
6
+ Devise.sign_out_all_scopes = false
7
+ end
8
+
9
+ def teardown
10
+ Devise.sign_out_all_scopes = false
11
+ end
12
+
13
+ test 'home should be accessible without sign in' do
14
+ visit '/'
15
+ assert_response :success
16
+ assert_template 'home/index'
17
+ end
18
+
19
+ test 'sign in as user should not authenticate admin scope' do
20
+ sign_in_as_user
21
+
22
+ assert warden.authenticated?(:user)
23
+ assert_not warden.authenticated?(:admin)
24
+ end
25
+
26
+ test 'sign in as admin should not authenticate user scope' do
27
+ sign_in_as_admin
28
+
29
+ assert warden.authenticated?(:admin)
30
+ assert_not warden.authenticated?(:user)
31
+ end
32
+
33
+ test 'sign in as both user and admin at same time' do
34
+ sign_in_as_user
35
+ sign_in_as_admin
36
+
37
+ assert warden.authenticated?(:user)
38
+ assert warden.authenticated?(:admin)
39
+ end
40
+
41
+ test 'sign out as user should not touch admin authentication if sign_out_all_scopes is false' do
42
+ sign_in_as_user
43
+ sign_in_as_admin
44
+
45
+ get destroy_user_session_path
46
+ assert_not warden.authenticated?(:user)
47
+ assert warden.authenticated?(:admin)
48
+ end
49
+
50
+ test 'sign out as admin should not touch user authentication if sign_out_all_scopes is false' do
51
+ sign_in_as_user
52
+ sign_in_as_admin
53
+
54
+ get destroy_admin_session_path
55
+ assert_not warden.authenticated?(:admin)
56
+ assert warden.authenticated?(:user)
57
+ end
58
+
59
+ test 'sign out as user should also sign out admin if sign_out_all_scopes is true' do
60
+ Devise.sign_out_all_scopes = true
61
+ sign_in_as_user
62
+ sign_in_as_admin
63
+
64
+ get destroy_user_session_path
65
+ assert_not warden.authenticated?(:user)
66
+ assert_not warden.authenticated?(:admin)
67
+ end
68
+
69
+ test 'sign out as admin should also sign out user if sign_out_all_scopes is true' do
70
+ Devise.sign_out_all_scopes = true
71
+ sign_in_as_user
72
+ sign_in_as_admin
73
+
74
+ get destroy_admin_session_path
75
+ assert_not warden.authenticated?(:admin)
76
+ assert_not warden.authenticated?(:user)
77
+ end
78
+
79
+ test 'not signed in as admin should not be able to access admins actions' do
80
+ get admins_path
81
+
82
+ assert_redirected_to new_admin_session_path
83
+ assert_not warden.authenticated?(:admin)
84
+ end
85
+
86
+ test 'not signed in as admin should not be able to access private route restricted to admins' do
87
+ get private_path
88
+
89
+ assert_redirected_to new_admin_session_path
90
+ assert_not warden.authenticated?(:admin)
91
+ end
92
+
93
+ test 'signed in as user should not be able to access private route restricted to admins' do
94
+ sign_in_as_user
95
+ assert warden.authenticated?(:user)
96
+ assert_not warden.authenticated?(:admin)
97
+
98
+ get private_path
99
+ assert_redirected_to new_admin_session_path
100
+ end
101
+
102
+ test 'signed in as admin should be able to access private route restricted to admins' do
103
+ sign_in_as_admin
104
+ assert warden.authenticated?(:admin)
105
+ assert_not warden.authenticated?(:user)
106
+
107
+ get private_path
108
+
109
+ assert_response :success
110
+ assert_template 'home/private'
111
+ assert_contain 'Private!'
112
+ end
113
+
114
+ test 'signed in as user should not be able to access admins actions' do
115
+ sign_in_as_user
116
+ assert warden.authenticated?(:user)
117
+ assert_not warden.authenticated?(:admin)
118
+
119
+ get admins_path
120
+ assert_redirected_to new_admin_session_path
121
+ end
122
+
123
+ test 'signed in as admin should be able to access admin actions' do
124
+ sign_in_as_admin
125
+ assert warden.authenticated?(:admin)
126
+ assert_not warden.authenticated?(:user)
127
+
128
+ get admins_path
129
+
130
+ assert_response :success
131
+ assert_template 'admins/index'
132
+ assert_contain 'Welcome Admin'
133
+ end
134
+
135
+ test 'authenticated admin should not be able to sign as admin again' do
136
+ sign_in_as_admin
137
+ get new_admin_session_path
138
+
139
+ assert_response :redirect
140
+ assert_redirected_to admin_root_path
141
+ assert warden.authenticated?(:admin)
142
+ end
143
+
144
+ test 'authenticated admin should be able to sign out' do
145
+ sign_in_as_admin
146
+ assert warden.authenticated?(:admin)
147
+
148
+ get destroy_admin_session_path
149
+ assert_response :redirect
150
+ assert_redirected_to root_path
151
+
152
+ get root_path
153
+ assert_contain 'Signed out successfully'
154
+ assert_not warden.authenticated?(:admin)
155
+ end
156
+
157
+ test 'unauthenticated admin does not set message on sign out' do
158
+ get destroy_admin_session_path
159
+ assert_response :redirect
160
+ assert_redirected_to root_path
161
+
162
+ get root_path
163
+ assert_not_contain 'Signed out successfully'
164
+ end
165
+ end
166
+
167
+ class AuthenticationRedirectTest < ActionController::IntegrationTest
168
+ test 'redirect from warden shows sign in or sign up message' do
169
+ get admins_path
170
+
171
+ warden_path = new_admin_session_path
172
+ assert_redirected_to warden_path
173
+
174
+ get warden_path
175
+ assert_contain 'You need to sign in or sign up before continuing.'
176
+ end
177
+
178
+ test 'redirect to default url if no other was configured' do
179
+ sign_in_as_user
180
+ assert_template 'home/index'
181
+ assert_nil session[:"user_return_to"]
182
+ end
183
+
184
+ test 'redirect to requested url after sign in' do
185
+ get users_path
186
+ assert_redirected_to new_user_session_path
187
+ assert_equal users_path, session[:"user_return_to"]
188
+
189
+ follow_redirect!
190
+ sign_in_as_user :visit => false
191
+
192
+ assert_current_url '/users'
193
+ assert_nil session[:"user_return_to"]
194
+ end
195
+
196
+ test 'redirect to last requested url overwriting the stored return_to option' do
197
+ get expire_user_path(create_user)
198
+ assert_redirected_to new_user_session_path
199
+ assert_equal expire_user_path(create_user), session[:"user_return_to"]
200
+
201
+ get users_path
202
+ assert_redirected_to new_user_session_path
203
+ assert_equal users_path, session[:"user_return_to"]
204
+
205
+ follow_redirect!
206
+ sign_in_as_user :visit => false
207
+
208
+ assert_current_url '/users'
209
+ assert_nil session[:"user_return_to"]
210
+ end
211
+
212
+ test 'xml http requests does not store urls for redirect' do
213
+ get users_path, {}, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'
214
+ assert_equal 401, response.status
215
+ assert_nil session[:"user_return_to"]
216
+ end
217
+
218
+ test 'redirect to configured home path for a given scope after sign in' do
219
+ sign_in_as_admin
220
+ assert_equal "/admin_area/home", @request.path
221
+ end
222
+ end
223
+
224
+ class AuthenticationSessionTest < ActionController::IntegrationTest
225
+ test 'destroyed account is signed out' do
226
+ sign_in_as_user
227
+ get '/users'
228
+
229
+ User.destroy_all
230
+ get '/users'
231
+ assert_redirected_to new_user_session_path
232
+ end
233
+
234
+ test 'allows session to be set for a given scope' do
235
+ sign_in_as_user
236
+ get '/users'
237
+ assert_equal "Cart", @controller.user_session[:cart]
238
+ end
239
+ end
240
+
241
+ class AuthenticationWithScopesTest < ActionController::IntegrationTest
242
+ test 'renders the scoped view if turned on and view is available' do
243
+ swap Devise, :scoped_views => true do
244
+ assert_raise Webrat::NotFoundError do
245
+ sign_in_as_user
246
+ end
247
+ assert_match /Special user view/, response.body
248
+ end
249
+ end
250
+
251
+ test 'renders the scoped view if turned on in an specific controller' do
252
+ begin
253
+ Devise::SessionsController.scoped_views = true
254
+ assert_raise Webrat::NotFoundError do
255
+ sign_in_as_user
256
+ end
257
+
258
+ assert_match /Special user view/, response.body
259
+ assert !Devise::PasswordsController.scoped_views?
260
+ ensure
261
+ Devise::SessionsController.send :remove_instance_variable, :@scoped_views
262
+ end
263
+ end
264
+
265
+ test 'does not render the scoped view if turned off' do
266
+ swap Devise, :scoped_views => false do
267
+ assert_nothing_raised do
268
+ sign_in_as_user
269
+ end
270
+ end
271
+ end
272
+
273
+ test 'does not render the scoped view if not available' do
274
+ swap Devise, :scoped_views => true do
275
+ assert_nothing_raised do
276
+ sign_in_as_admin
277
+ end
278
+ end
279
+ end
280
+
281
+ test 'uses the mapping from router' do
282
+ sign_in_as_user :visit => "/as/sign_in"
283
+ assert warden.authenticated?(:user)
284
+ assert_not warden.authenticated?(:admin)
285
+ end
286
+
287
+ test 'uses the mapping from nested devise_for call' do
288
+ sign_in_as_user :visit => "/devise_for/sign_in"
289
+ assert warden.authenticated?(:user)
290
+ assert_not warden.authenticated?(:admin)
291
+ end
292
+ end
293
+
294
+ class AuthenticationOthersTest < ActionController::IntegrationTest
295
+ test 'uses the custom controller with the custom controller view' do
296
+ get '/admin_area/sign_in'
297
+ assert_contain 'Sign in'
298
+ assert_contain 'Welcome to "sessions" controller!'
299
+ assert_contain 'Welcome to "sessions/new" view!'
300
+ end
301
+
302
+ test 'render 404 on roles without routes' do
303
+ get '/admin_area/password/new'
304
+ assert_equal 404, response.status
305
+ end
306
+
307
+ test 'render 404 on roles without mapping' do
308
+ assert_raise AbstractController::ActionNotFound do
309
+ get '/sign_in'
310
+ end
311
+ end
312
+
313
+ test 'sign in with script name' do
314
+ assert_nothing_raised do
315
+ get new_user_session_path, {}, "SCRIPT_NAME" => "/omg"
316
+ fill_in "email", "user@test.com"
317
+ end
318
+ end
319
+
320
+ test 'registration in xml format' do
321
+ assert_nothing_raised do
322
+ post user_registration_path(:format => 'xml', :user => {:email => "test@example.com", :password => "invalid"} )
323
+ end
324
+ end
325
+
326
+ test 'does not explode when invalid user class is stored in session' do
327
+ klass = User
328
+ paths = ActiveSupport::Dependencies.autoload_paths.dup
329
+
330
+ begin
331
+ sign_in_as_user
332
+ assert warden.authenticated?(:user)
333
+
334
+ Object.send :remove_const, :User
335
+ ActiveSupport::Dependencies.autoload_paths.clear
336
+
337
+ visit "/users"
338
+ assert_not warden.authenticated?(:user)
339
+ ensure
340
+ Object.const_set(:User, klass)
341
+ ActiveSupport::Dependencies.autoload_paths.replace(paths)
342
+ end
343
+ end
344
+ end