devise 2.1.2 → 3.5.10

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise might be problematic. Click here for more details.

Files changed (242) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +39 -10
  3. data/.yardopts +9 -0
  4. data/{CHANGELOG.rdoc → CHANGELOG.md} +445 -112
  5. data/CODE_OF_CONDUCT.md +22 -0
  6. data/CONTRIBUTING.md +16 -0
  7. data/Gemfile +10 -15
  8. data/Gemfile.lock +151 -129
  9. data/MIT-LICENSE +1 -1
  10. data/README.md +256 -96
  11. data/Rakefile +4 -2
  12. data/app/controllers/devise/confirmations_controller.rb +15 -7
  13. data/app/controllers/devise/omniauth_callbacks_controller.rb +6 -2
  14. data/app/controllers/devise/passwords_controller.rb +33 -9
  15. data/app/controllers/devise/registrations_controller.rb +66 -26
  16. data/app/controllers/devise/sessions_controller.rb +52 -21
  17. data/app/controllers/devise/unlocks_controller.rb +11 -6
  18. data/app/controllers/devise_controller.rb +65 -58
  19. data/app/helpers/devise_helper.rb +2 -2
  20. data/app/mailers/devise/mailer.rb +19 -10
  21. data/app/views/devise/confirmations/new.html.erb +8 -4
  22. data/app/views/devise/mailer/confirmation_instructions.html.erb +2 -2
  23. data/app/views/devise/mailer/password_change.html.erb +3 -0
  24. data/app/views/devise/mailer/reset_password_instructions.html.erb +2 -2
  25. data/app/views/devise/mailer/unlock_instructions.html.erb +2 -2
  26. data/app/views/devise/passwords/edit.html.erb +15 -6
  27. data/app/views/devise/passwords/new.html.erb +8 -4
  28. data/app/views/devise/registrations/edit.html.erb +29 -15
  29. data/app/views/devise/registrations/new.html.erb +19 -8
  30. data/app/views/devise/sessions/new.html.erb +17 -8
  31. data/app/views/devise/shared/{_links.erb → _links.html.erb} +4 -4
  32. data/app/views/devise/unlocks/new.html.erb +8 -4
  33. data/config/locales/en.yml +51 -47
  34. data/devise.gemspec +8 -6
  35. data/devise.png +0 -0
  36. data/gemfiles/Gemfile.rails-3.2-stable +29 -0
  37. data/gemfiles/Gemfile.rails-3.2-stable.lock +172 -0
  38. data/gemfiles/Gemfile.rails-4.0-stable +30 -0
  39. data/gemfiles/Gemfile.rails-4.0-stable.lock +166 -0
  40. data/gemfiles/Gemfile.rails-4.1-stable +30 -0
  41. data/gemfiles/Gemfile.rails-4.1-stable.lock +171 -0
  42. data/gemfiles/Gemfile.rails-4.2-stable +30 -0
  43. data/gemfiles/Gemfile.rails-4.2-stable.lock +193 -0
  44. data/lib/devise/controllers/helpers.rb +126 -108
  45. data/lib/devise/controllers/rememberable.rb +19 -17
  46. data/lib/devise/controllers/scoped_views.rb +1 -1
  47. data/lib/devise/controllers/sign_in_out.rb +96 -0
  48. data/lib/devise/controllers/store_location.rb +58 -0
  49. data/lib/devise/controllers/url_helpers.rb +7 -7
  50. data/lib/devise/encryptor.rb +22 -0
  51. data/lib/devise/failure_app.rb +85 -25
  52. data/lib/devise/hooks/activatable.rb +5 -6
  53. data/lib/devise/hooks/csrf_cleaner.rb +7 -0
  54. data/lib/devise/hooks/forgetable.rb +1 -1
  55. data/lib/devise/hooks/lockable.rb +2 -2
  56. data/lib/devise/hooks/proxy.rb +21 -0
  57. data/lib/devise/hooks/rememberable.rb +5 -4
  58. data/lib/devise/hooks/timeoutable.rb +16 -8
  59. data/lib/devise/hooks/trackable.rb +1 -1
  60. data/lib/devise/mailers/helpers.rb +27 -23
  61. data/lib/devise/mapping.rb +11 -7
  62. data/lib/devise/models/authenticatable.rb +82 -66
  63. data/lib/devise/models/confirmable.rb +142 -55
  64. data/lib/devise/models/database_authenticatable.rb +59 -15
  65. data/lib/devise/models/lockable.rb +41 -30
  66. data/lib/devise/models/omniauthable.rb +3 -3
  67. data/lib/devise/models/recoverable.rb +56 -41
  68. data/lib/devise/models/rememberable.rb +65 -27
  69. data/lib/devise/models/timeoutable.rb +2 -8
  70. data/lib/devise/models/trackable.rb +6 -4
  71. data/lib/devise/models/validatable.rb +9 -9
  72. data/lib/devise/models.rb +4 -13
  73. data/lib/devise/modules.rb +10 -11
  74. data/lib/devise/omniauth/url_helpers.rb +2 -2
  75. data/lib/devise/orm/active_record.rb +1 -1
  76. data/lib/devise/orm/mongoid.rb +1 -1
  77. data/lib/devise/{param_filter.rb → parameter_filter.rb} +10 -11
  78. data/lib/devise/parameter_sanitizer.rb +99 -0
  79. data/lib/devise/rails/routes.rb +173 -115
  80. data/lib/devise/rails/warden_compat.rb +10 -31
  81. data/lib/devise/rails.rb +14 -12
  82. data/lib/devise/strategies/authenticatable.rb +26 -26
  83. data/lib/devise/strategies/base.rb +1 -1
  84. data/lib/devise/strategies/database_authenticatable.rb +8 -4
  85. data/lib/devise/strategies/rememberable.rb +15 -5
  86. data/lib/devise/test_helpers.rb +7 -5
  87. data/lib/devise/time_inflector.rb +14 -0
  88. data/lib/devise/token_generator.rb +70 -0
  89. data/lib/devise/version.rb +1 -1
  90. data/lib/devise.rb +110 -52
  91. data/lib/generators/active_record/devise_generator.rb +34 -18
  92. data/lib/generators/active_record/templates/migration.rb +5 -6
  93. data/lib/generators/active_record/templates/migration_existing.rb +5 -6
  94. data/lib/generators/devise/controllers_generator.rb +44 -0
  95. data/lib/generators/devise/devise_generator.rb +5 -3
  96. data/lib/generators/devise/install_generator.rb +5 -0
  97. data/lib/generators/devise/orm_helpers.rb +25 -6
  98. data/lib/generators/devise/views_generator.rb +52 -22
  99. data/lib/generators/mongoid/devise_generator.rb +21 -26
  100. data/lib/generators/templates/README +9 -5
  101. data/lib/generators/templates/controllers/README +14 -0
  102. data/lib/generators/templates/controllers/confirmations_controller.rb +28 -0
  103. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +28 -0
  104. data/lib/generators/templates/controllers/passwords_controller.rb +32 -0
  105. data/lib/generators/templates/controllers/registrations_controller.rb +60 -0
  106. data/lib/generators/templates/controllers/sessions_controller.rb +25 -0
  107. data/lib/generators/templates/controllers/unlocks_controller.rb +28 -0
  108. data/lib/generators/templates/devise.rb +80 -43
  109. data/lib/generators/templates/markerb/confirmation_instructions.markerb +2 -2
  110. data/lib/generators/templates/markerb/password_change.markerb +3 -0
  111. data/lib/generators/templates/markerb/reset_password_instructions.markerb +1 -1
  112. data/lib/generators/templates/markerb/unlock_instructions.markerb +2 -2
  113. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +3 -2
  114. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +4 -4
  115. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +2 -2
  116. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +11 -6
  117. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +4 -4
  118. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +6 -6
  119. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +3 -2
  120. data/script/cached-bundle +49 -0
  121. data/script/s3-put +71 -0
  122. data/test/controllers/custom_registrations_controller_test.rb +40 -0
  123. data/test/controllers/helper_methods_test.rb +21 -0
  124. data/test/controllers/helpers_test.rb +95 -32
  125. data/test/controllers/inherited_controller_i18n_messages_test.rb +51 -0
  126. data/test/controllers/internal_helpers_test.rb +39 -14
  127. data/test/controllers/load_hooks_controller_test.rb +19 -0
  128. data/test/controllers/passwords_controller_test.rb +31 -0
  129. data/test/controllers/sessions_controller_test.rb +66 -6
  130. data/test/controllers/url_helpers_test.rb +10 -4
  131. data/test/delegator_test.rb +1 -1
  132. data/test/devise_test.rb +45 -10
  133. data/test/failure_app_test.rb +121 -27
  134. data/test/generators/active_record_generator_test.rb +48 -8
  135. data/test/generators/controllers_generator_test.rb +48 -0
  136. data/test/generators/devise_generator_test.rb +2 -2
  137. data/test/generators/mongoid_generator_test.rb +3 -3
  138. data/test/generators/views_generator_test.rb +54 -3
  139. data/test/helpers/devise_helper_test.rb +18 -20
  140. data/test/integration/authenticatable_test.rb +161 -65
  141. data/test/integration/confirmable_test.rb +146 -77
  142. data/test/integration/database_authenticatable_test.rb +43 -30
  143. data/test/integration/http_authenticatable_test.rb +30 -22
  144. data/test/integration/lockable_test.rb +64 -49
  145. data/test/integration/omniauthable_test.rb +17 -15
  146. data/test/integration/recoverable_test.rb +111 -70
  147. data/test/integration/registerable_test.rb +114 -79
  148. data/test/integration/rememberable_test.rb +87 -31
  149. data/test/integration/timeoutable_test.rb +77 -33
  150. data/test/integration/trackable_test.rb +5 -5
  151. data/test/mailers/confirmation_instructions_test.rb +28 -8
  152. data/test/mailers/reset_password_instructions_test.rb +21 -8
  153. data/test/mailers/unlock_instructions_test.rb +20 -6
  154. data/test/mapping_test.rb +12 -5
  155. data/test/models/authenticatable_test.rb +17 -1
  156. data/test/models/confirmable_test.rb +216 -62
  157. data/test/models/database_authenticatable_test.rb +129 -49
  158. data/test/models/lockable_test.rb +132 -45
  159. data/test/models/recoverable_test.rb +100 -54
  160. data/test/models/rememberable_test.rb +89 -94
  161. data/test/models/serializable_test.rb +12 -11
  162. data/test/models/timeoutable_test.rb +6 -1
  163. data/test/models/trackable_test.rb +28 -0
  164. data/test/models/validatable_test.rb +31 -21
  165. data/test/models_test.rb +22 -48
  166. data/test/omniauth/config_test.rb +4 -4
  167. data/test/omniauth/url_helpers_test.rb +7 -4
  168. data/test/orm/active_record.rb +1 -0
  169. data/test/orm/mongoid.rb +2 -3
  170. data/test/parameter_sanitizer_test.rb +81 -0
  171. data/test/rails_app/Rakefile +0 -4
  172. data/test/rails_app/app/active_record/shim.rb +1 -1
  173. data/test/rails_app/app/active_record/user_on_engine.rb +7 -0
  174. data/test/rails_app/app/active_record/user_on_main_app.rb +7 -0
  175. data/test/rails_app/app/active_record/user_without_email.rb +8 -0
  176. data/test/rails_app/app/controllers/admins/sessions_controller.rb +1 -1
  177. data/test/rails_app/app/controllers/admins_controller.rb +0 -5
  178. data/test/rails_app/app/controllers/application_controller.rb +6 -2
  179. data/test/rails_app/app/controllers/application_with_fake_engine.rb +30 -0
  180. data/test/rails_app/app/controllers/custom/registrations_controller.rb +31 -0
  181. data/test/rails_app/app/controllers/home_controller.rb +1 -1
  182. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +1 -1
  183. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +1 -1
  184. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +4 -4
  185. data/test/rails_app/app/controllers/users_controller.rb +12 -4
  186. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +3 -0
  187. data/test/rails_app/app/mailers/users/mailer.rb +1 -1
  188. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +4 -0
  189. data/test/rails_app/app/mongoid/admin.rb +12 -10
  190. data/test/rails_app/app/mongoid/shim.rb +4 -5
  191. data/test/rails_app/app/mongoid/user.rb +19 -22
  192. data/test/rails_app/app/mongoid/user_on_engine.rb +39 -0
  193. data/test/rails_app/app/mongoid/user_on_main_app.rb +39 -0
  194. data/test/rails_app/app/mongoid/user_without_email.rb +33 -0
  195. data/test/rails_app/app/views/admins/sessions/new.html.erb +1 -1
  196. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -1
  197. data/test/rails_app/app/views/home/index.html.erb +1 -1
  198. data/test/rails_app/app/views/home/join.html.erb +1 -1
  199. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -1
  200. data/test/rails_app/app/views/layouts/application.html.erb +1 -1
  201. data/test/rails_app/app/views/users/edit_form.html.erb +1 -0
  202. data/test/rails_app/bin/bundle +3 -0
  203. data/test/rails_app/bin/rails +4 -0
  204. data/test/rails_app/bin/rake +4 -0
  205. data/test/rails_app/config/application.rb +4 -5
  206. data/test/rails_app/config/boot.rb +9 -3
  207. data/test/rails_app/config/environment.rb +2 -2
  208. data/test/rails_app/config/environments/development.rb +19 -7
  209. data/test/rails_app/config/environments/production.rb +68 -17
  210. data/test/rails_app/config/environments/test.rb +24 -16
  211. data/test/rails_app/config/initializers/devise.rb +22 -20
  212. data/test/rails_app/config/initializers/secret_token.rb +8 -2
  213. data/test/rails_app/config/initializers/session_store.rb +1 -0
  214. data/test/rails_app/config/routes.rb +71 -46
  215. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +9 -12
  216. data/test/rails_app/db/schema.rb +21 -18
  217. data/test/rails_app/lib/shared_admin.rb +7 -4
  218. data/test/rails_app/lib/shared_user.rb +6 -3
  219. data/test/rails_app/lib/shared_user_without_email.rb +26 -0
  220. data/test/rails_app/lib/shared_user_without_omniauth.rb +13 -0
  221. data/test/rails_test.rb +9 -0
  222. data/test/routes_test.rb +94 -78
  223. data/test/support/action_controller/record_identifier.rb +10 -0
  224. data/test/support/assertions.rb +2 -3
  225. data/test/support/helpers.rb +18 -32
  226. data/test/support/integration.rb +17 -16
  227. data/test/support/locale/en.yml +4 -0
  228. data/test/support/mongoid.yml +6 -0
  229. data/test/test_helper.rb +8 -1
  230. data/test/test_helpers_test.rb +64 -20
  231. data/test/test_models.rb +33 -0
  232. data/test/time_helpers.rb +137 -0
  233. metadata +172 -51
  234. data/app/views/devise/_links.erb +0 -3
  235. data/gemfiles/Gemfile.rails-3.1.x +0 -35
  236. data/gemfiles/Gemfile.rails-3.1.x.lock +0 -167
  237. data/lib/devise/models/token_authenticatable.rb +0 -77
  238. data/lib/devise/strategies/token_authenticatable.rb +0 -56
  239. data/test/indifferent_hash.rb +0 -33
  240. data/test/integration/token_authenticatable_test.rb +0 -161
  241. data/test/models/token_authenticatable_test.rb +0 -55
  242. data/test/rails_app/script/rails +0 -10
@@ -0,0 +1,10 @@
1
+ # Since webrat uses ActionController::RecordIdentifier class that was moved to
2
+ # ActionView namespace in Rails 4.1+
3
+
4
+ unless defined?(ActionController::RecordIdentifier)
5
+ require 'action_view/record_identifier'
6
+
7
+ module ActionController
8
+ RecordIdentifier = ActionView::RecordIdentifier
9
+ end
10
+ end
@@ -9,10 +9,9 @@ class ActiveSupport::TestCase
9
9
  assert assertion.blank?
10
10
  end
11
11
 
12
- def assert_not_blank(assertion)
13
- assert !assertion.blank?
12
+ def assert_present(assertion)
13
+ assert assertion.present?
14
14
  end
15
- alias :assert_present :assert_not_blank
16
15
 
17
16
  def assert_email_sent(address = nil, &block)
18
17
  assert_difference('ActionMailer::Base.deliveries.size', &block)
@@ -8,12 +8,15 @@ class ActiveSupport::TestCase
8
8
  end
9
9
 
10
10
  def store_translations(locale, translations, &block)
11
- begin
12
- I18n.backend.store_translations(locale, translations)
13
- yield
14
- ensure
15
- I18n.reload!
16
- end
11
+ # Calling 'available_locales' before storing the translations to ensure
12
+ # that the I18n backend will be initialized before we store our custom
13
+ # translations, so they will always override the translations for the
14
+ # YML file.
15
+ I18n.available_locales
16
+ I18n.backend.store_translations(locale, translations)
17
+ yield
18
+ ensure
19
+ I18n.reload!
17
20
  end
18
21
 
19
22
  def generate_unique_email
@@ -23,10 +26,10 @@ class ActiveSupport::TestCase
23
26
  end
24
27
 
25
28
  def valid_attributes(attributes={})
26
- { :username => "usertest",
27
- :email => generate_unique_email,
28
- :password => '123456',
29
- :password_confirmation => '123456' }.update(attributes)
29
+ { username: "usertest",
30
+ email: generate_unique_email,
31
+ password: '12345678',
32
+ password_confirmation: '12345678' }.update(attributes)
30
33
  end
31
34
 
32
35
  def new_user(attributes={})
@@ -43,6 +46,10 @@ class ActiveSupport::TestCase
43
46
  Admin.create!(valid_attributes)
44
47
  end
45
48
 
49
+ def create_user_without_email(attributes={})
50
+ UserWithoutEmail.create!(valid_attributes(attributes))
51
+ end
52
+
46
53
  # Execute the block setting the given values and restoring old values after
47
54
  # the block is executed.
48
55
  def swap(object, new_values)
@@ -63,29 +70,8 @@ class ActiveSupport::TestCase
63
70
  def clear_cached_variables(options)
64
71
  if options.key?(:case_insensitive_keys) || options.key?(:strip_whitespace_keys)
65
72
  Devise.mappings.each do |_, mapping|
66
- mapping.to.instance_variable_set(:@devise_param_filter, nil)
73
+ mapping.to.instance_variable_set(:@devise_parameter_filter, nil)
67
74
  end
68
75
  end
69
76
  end
70
-
71
- def swap_module_method_existence(klass, method)
72
- klass.module_eval %Q[
73
- class << self
74
- alias #{method}_referenced #{method}
75
- undef #{method}
76
- end
77
- ]
78
-
79
- begin
80
- yield if block_given?
81
- ensure
82
-
83
- klass.module_eval %Q[
84
- class << self
85
- alias #{method} #{method}_referenced
86
- undef #{method}_referenced
87
- end
88
- ]
89
- end
90
- end
91
77
  end
@@ -8,13 +8,14 @@ class ActionDispatch::IntegrationTest
8
8
  def create_user(options={})
9
9
  @user ||= begin
10
10
  user = User.create!(
11
- :username => 'usertest',
12
- :email => options[:email] || 'user@test.com',
13
- :password => options[:password] || '123456',
14
- :password_confirmation => options[:password] || '123456',
15
- :created_at => Time.now.utc
11
+ username: 'usertest',
12
+ email: options[:email] || 'user@test.com',
13
+ password: options[:password] || '12345678',
14
+ password_confirmation: options[:password] || '12345678',
15
+ created_at: Time.now.utc
16
16
  )
17
- user.confirm! unless options[:confirm] == false
17
+ user.update_attribute(:confirmation_sent_at, options[:confirmation_sent_at]) if options[:confirmation_sent_at]
18
+ user.confirm unless options[:confirm] == false
18
19
  user.lock_access! if options[:locked] == true
19
20
  user
20
21
  end
@@ -23,11 +24,11 @@ class ActionDispatch::IntegrationTest
23
24
  def create_admin(options={})
24
25
  @admin ||= begin
25
26
  admin = Admin.create!(
26
- :email => options[:email] || 'admin@test.com',
27
- :password => '123456', :password_confirmation => '123456',
28
- :active => options[:active]
27
+ email: options[:email] || 'admin@test.com',
28
+ password: '123456', password_confirmation: '123456',
29
+ active: options[:active]
29
30
  )
30
- admin.confirm! unless options[:confirm] == false
31
+ admin.confirm unless options[:confirm] == false
31
32
  admin
32
33
  end
33
34
  end
@@ -35,21 +36,21 @@ class ActionDispatch::IntegrationTest
35
36
  def sign_in_as_user(options={}, &block)
36
37
  user = create_user(options)
37
38
  visit_with_option options[:visit], new_user_session_path
38
- fill_in 'email', :with => options[:email] || 'user@test.com'
39
- fill_in 'password', :with => options[:password] || '123456'
39
+ fill_in 'email', with: options[:email] || 'user@test.com'
40
+ fill_in 'password', with: options[:password] || '12345678'
40
41
  check 'remember me' if options[:remember_me] == true
41
42
  yield if block_given?
42
- click_button 'Sign In'
43
+ click_button 'Log In'
43
44
  user
44
45
  end
45
46
 
46
47
  def sign_in_as_admin(options={}, &block)
47
48
  admin = create_admin(options)
48
49
  visit_with_option options[:visit], new_admin_session_path
49
- fill_in 'email', :with => 'admin@test.com'
50
- fill_in 'password', :with => '123456'
50
+ fill_in 'email', with: 'admin@test.com'
51
+ fill_in 'password', with: '123456'
51
52
  yield if block_given?
52
- click_button 'Sign In'
53
+ click_button 'Log In'
53
54
  admin
54
55
  end
55
56
 
@@ -1,4 +1,8 @@
1
1
  en:
2
+ devise:
3
+ failure:
4
+ user:
5
+ does_not_exist: "User %{name} does not exist"
2
6
  errors:
3
7
  messages:
4
8
  taken: "has already been taken"
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: devise-test-suite
5
+ hosts:
6
+ - localhost:<%= ENV['MONGODB_PORT'] || '27017' %>
data/test/test_helper.rb CHANGED
@@ -10,13 +10,19 @@ require "orm/#{DEVISE_ORM}"
10
10
 
11
11
  I18n.load_path << File.expand_path("../support/locale/en.yml", __FILE__)
12
12
 
13
- require 'mocha'
13
+ require 'mocha/setup'
14
14
  require 'webrat'
15
15
  Webrat.configure do |config|
16
16
  config.mode = :rails
17
17
  config.open_error_files = false
18
18
  end
19
19
 
20
+ if ActiveSupport.respond_to?(:test_order)
21
+ ActiveSupport.test_order = :random
22
+ end
23
+
24
+ OmniAuth.config.logger = Logger.new('/dev/null')
25
+
20
26
  # Add support to load paths so we can overwrite broken webrat setup
21
27
  $:.unshift File.expand_path('../support', __FILE__)
22
28
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -25,3 +31,4 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
25
31
  require "rails/generators/test_case"
26
32
  require "generators/devise/install_generator"
27
33
  require "generators/devise/views_generator"
34
+ require "generators/devise/controllers_generator"
@@ -4,12 +4,6 @@ class TestHelpersTest < ActionController::TestCase
4
4
  tests UsersController
5
5
  include Devise::TestHelpers
6
6
 
7
- class CustomFailureApp < Devise::FailureApp
8
- def redirect
9
- self.status = 306
10
- end
11
- end
12
-
13
7
  test "redirects if attempting to access a page unauthenticated" do
14
8
  get :index
15
9
  assert_redirected_to new_user_session_path
@@ -17,7 +11,7 @@ class TestHelpersTest < ActionController::TestCase
17
11
  end
18
12
 
19
13
  test "redirects if attempting to access a page with an unconfirmed account" do
20
- swap Devise, :allow_unconfirmed_access_for => 0 do
14
+ swap Devise, allow_unconfirmed_access_for: 0.days do
21
15
  user = create_user
22
16
  assert !user.active_for_authentication?
23
17
 
@@ -28,19 +22,31 @@ class TestHelpersTest < ActionController::TestCase
28
22
  end
29
23
 
30
24
  test "returns nil if accessing current_user with an unconfirmed account" do
31
- swap Devise, :allow_unconfirmed_access_for => 0 do
25
+ swap Devise, allow_unconfirmed_access_for: 0.days do
32
26
  user = create_user
33
27
  assert !user.active_for_authentication?
34
28
 
35
29
  sign_in user
36
- get :accept, :id => user
30
+ get :accept, id: user
37
31
  assert_nil assigns(:current_user)
38
32
  end
39
33
  end
40
34
 
41
35
  test "does not redirect with valid user" do
42
36
  user = create_user
43
- user.confirm!
37
+ user.confirm
38
+
39
+ sign_in user
40
+ get :index
41
+ assert_response :success
42
+ end
43
+
44
+ test "does not redirect with valid user after failed first attempt" do
45
+ get :index
46
+ assert_response :redirect
47
+
48
+ user = create_user
49
+ user.confirm
44
50
 
45
51
  sign_in user
46
52
  get :index
@@ -49,7 +55,7 @@ class TestHelpersTest < ActionController::TestCase
49
55
 
50
56
  test "redirects if valid user signed out" do
51
57
  user = create_user
52
- user.confirm!
58
+ user.confirm
53
59
 
54
60
  sign_in user
55
61
  get :index
@@ -58,17 +64,40 @@ class TestHelpersTest < ActionController::TestCase
58
64
  get :index
59
65
  assert_redirected_to new_user_session_path
60
66
  end
61
-
67
+
62
68
  test "respects custom failure app" do
63
- begin
64
- Devise.warden_config.failure_app = CustomFailureApp
69
+ custom_failure_app = Class.new(Devise::FailureApp) do
70
+ def redirect
71
+ self.status = 306
72
+ end
73
+ end
74
+
75
+ swap Devise.warden_config, failure_app: custom_failure_app do
65
76
  get :index
66
77
  assert_response 306
67
- ensure
68
- Devise.warden_config.failure_app = Devise::FailureApp
69
78
  end
70
79
  end
71
80
 
81
+ test "passes given headers from the failure app to the response" do
82
+ custom_failure_app = Class.new(Devise::FailureApp) do
83
+ def respond
84
+ self.status = 401
85
+ self.response.headers["CUSTOMHEADER"] = 1
86
+ end
87
+ end
88
+
89
+ swap Devise.warden_config, failure_app: custom_failure_app do
90
+ sign_in create_user
91
+ get :index
92
+ assert_equal 1, @response.headers["CUSTOMHEADER"]
93
+ end
94
+ end
95
+
96
+ test "returns the body of a failure app" do
97
+ get :index
98
+ assert_equal response.body, "<html><body>You are being <a href=\"http://test.host/users/sign_in\">redirected</a>.</body></html>"
99
+ end
100
+
72
101
  test "defined Warden after_authentication callback should not be called when sign_in is called" do
73
102
  begin
74
103
  Warden::Manager.after_authentication do |user, auth, opts|
@@ -76,7 +105,7 @@ class TestHelpersTest < ActionController::TestCase
76
105
  end
77
106
 
78
107
  user = create_user
79
- user.confirm!
108
+ user.confirm
80
109
  sign_in user
81
110
  ensure
82
111
  Warden::Manager._after_set_user.pop
@@ -89,7 +118,7 @@ class TestHelpersTest < ActionController::TestCase
89
118
  flunk "callback was called while it should not"
90
119
  end
91
120
  user = create_user
92
- user.confirm!
121
+ user.confirm
93
122
 
94
123
  sign_in user
95
124
  sign_out user
@@ -117,7 +146,7 @@ class TestHelpersTest < ActionController::TestCase
117
146
 
118
147
  test "allows to sign in with different users" do
119
148
  first_user = create_user
120
- first_user.confirm!
149
+ first_user.confirm
121
150
 
122
151
  sign_in first_user
123
152
  get :index
@@ -125,10 +154,25 @@ class TestHelpersTest < ActionController::TestCase
125
154
  sign_out first_user
126
155
 
127
156
  second_user = create_user
128
- second_user.confirm!
157
+ second_user.confirm
129
158
 
130
159
  sign_in second_user
131
160
  get :index
132
161
  assert_match /User ##{second_user.id}/, @response.body
133
162
  end
163
+
164
+ test "creates a new warden proxy if the request object has changed" do
165
+ old_warden_proxy = warden
166
+ @request = ActionController::TestRequest.new
167
+ new_warden_proxy = warden
168
+
169
+ assert_not_equal old_warden_proxy, new_warden_proxy
170
+ end
171
+
172
+ test "doesn't create a new warden proxy if the request object hasn't changed" do
173
+ old_warden_proxy = warden
174
+ new_warden_proxy = warden
175
+
176
+ assert_equal old_warden_proxy, new_warden_proxy
177
+ end
134
178
  end
@@ -0,0 +1,33 @@
1
+ class Configurable < User
2
+ devise :database_authenticatable, :confirmable, :rememberable, :timeoutable, :lockable,
3
+ stretches: 15, pepper: 'abcdef', allow_unconfirmed_access_for: 5.days,
4
+ remember_for: 7.days, timeout_in: 15.minutes, unlock_in: 10.days
5
+ end
6
+
7
+ class WithValidation < Admin
8
+ devise :database_authenticatable, :validatable, password_length: 2..6
9
+ end
10
+
11
+ class UserWithValidation < User
12
+ validates_presence_of :username
13
+ end
14
+
15
+ class UserWithCustomEncryption < User
16
+ protected
17
+ def password_digest(password)
18
+ password.reverse
19
+ end
20
+ end
21
+
22
+ class UserWithVirtualAttributes < User
23
+ devise case_insensitive_keys: [:email, :email_confirmation]
24
+ validates :email, presence: true, confirmation: { on: :create }
25
+ end
26
+
27
+ class Several < Admin
28
+ devise :validatable
29
+ devise :lockable
30
+ end
31
+
32
+ class Inheritable < Admin
33
+ end
@@ -0,0 +1,137 @@
1
+ # A copy of Rails time helpers. With this file we can support the `travel_to`
2
+ # helper for Rails versions prior 4.1.
3
+ # File origin: https://github.com/rails/rails/blob/52ce6ece8c8f74064bb64e0a0b1ddd83092718e1/activesupport/lib/active_support/testing/time_helpers.rb
4
+ module ActiveSupport
5
+ module Testing
6
+ class SimpleStubs # :nodoc:
7
+ Stub = Struct.new(:object, :method_name, :original_method)
8
+
9
+ def initialize
10
+ @stubs = {}
11
+ end
12
+
13
+ def stub_object(object, method_name, return_value)
14
+ key = [object.object_id, method_name]
15
+
16
+ if stub = @stubs[key]
17
+ unstub_object(stub)
18
+ end
19
+
20
+ new_name = "__simple_stub__#{method_name}"
21
+
22
+ @stubs[key] = Stub.new(object, method_name, new_name)
23
+
24
+ object.singleton_class.send :alias_method, new_name, method_name
25
+ object.define_singleton_method(method_name) { return_value }
26
+ end
27
+
28
+ def unstub_all!
29
+ @stubs.each_value do |stub|
30
+ unstub_object(stub)
31
+ end
32
+ @stubs = {}
33
+ end
34
+
35
+ private
36
+
37
+ def unstub_object(stub)
38
+ singleton_class = stub.object.singleton_class
39
+ singleton_class.send :undef_method, stub.method_name
40
+ singleton_class.send :alias_method, stub.method_name, stub.original_method
41
+ singleton_class.send :undef_method, stub.original_method
42
+ end
43
+ end
44
+
45
+ # Contains helpers that help you test passage of time.
46
+ module TimeHelpers
47
+ # Changes current time to the time in the future or in the past by a given time difference by
48
+ # stubbing +Time.now+, +Date.today+, and +DateTime.now+.
49
+ #
50
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
51
+ # travel 1.day
52
+ # Time.current # => Sun, 10 Nov 2013 15:34:49 EST -05:00
53
+ # Date.current # => Sun, 10 Nov 2013
54
+ # DateTime.current # => Sun, 10 Nov 2013 15:34:49 -0500
55
+ #
56
+ # This method also accepts a block, which will return the current time back to its original
57
+ # state at the end of the block:
58
+ #
59
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
60
+ # travel 1.day do
61
+ # User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
62
+ # end
63
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
64
+ def travel(duration, &block)
65
+ travel_to Time.now + duration, &block
66
+ end
67
+
68
+ # Changes current time to the given time by stubbing +Time.now+,
69
+ # +Date.today+, and +DateTime.now+ to return the time or date passed into this method.
70
+ #
71
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
72
+ # travel_to Time.new(2004, 11, 24, 01, 04, 44)
73
+ # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
74
+ # Date.current # => Wed, 24 Nov 2004
75
+ # DateTime.current # => Wed, 24 Nov 2004 01:04:44 -0500
76
+ #
77
+ # Dates are taken as their timestamp at the beginning of the day in the
78
+ # application time zone. <tt>Time.current</tt> returns said timestamp,
79
+ # and <tt>Time.now</tt> its equivalent in the system time zone. Similarly,
80
+ # <tt>Date.current</tt> returns a date equal to the argument, and
81
+ # <tt>Date.today</tt> the date according to <tt>Time.now</tt>, which may
82
+ # be different. (Note that you rarely want to deal with <tt>Time.now</tt>,
83
+ # or <tt>Date.today</tt>, in order to honor the application time zone
84
+ # please always use <tt>Time.current</tt> and <tt>Date.current</tt>.)
85
+ #
86
+ # Note that the usec for the time passed will be set to 0 to prevent rounding
87
+ # errors with external services, like MySQL (which will round instead of floor,
88
+ # leading to off-by-one-second errors).
89
+ #
90
+ # This method also accepts a block, which will return the current time back to its original
91
+ # state at the end of the block:
92
+ #
93
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
94
+ # travel_to Time.new(2004, 11, 24, 01, 04, 44) do
95
+ # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
96
+ # end
97
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
98
+ def travel_to(date_or_time)
99
+ if date_or_time.is_a?(Date) && !date_or_time.is_a?(DateTime)
100
+ now = date_or_time.midnight.to_time
101
+ else
102
+ now = date_or_time.to_time.change(usec: 0)
103
+ end
104
+
105
+ simple_stubs.stub_object(Time, :now, now)
106
+ simple_stubs.stub_object(Date, :today, now.to_date)
107
+ simple_stubs.stub_object(DateTime, :now, now.to_datetime)
108
+
109
+ if block_given?
110
+ begin
111
+ yield
112
+ ensure
113
+ travel_back
114
+ end
115
+ end
116
+ end
117
+
118
+ # Returns the current time back to its original state, by removing the stubs added by
119
+ # `travel` and `travel_to`.
120
+ #
121
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
122
+ # travel_to Time.new(2004, 11, 24, 01, 04, 44)
123
+ # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
124
+ # travel_back
125
+ # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
126
+ def travel_back
127
+ simple_stubs.unstub_all!
128
+ end
129
+
130
+ private
131
+
132
+ def simple_stubs
133
+ @simple_stubs ||= SimpleStubs.new
134
+ end
135
+ end
136
+ end
137
+ end