cloudfoundry-devise 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. data/.gitignore +12 -0
  2. data/.travis.yml +13 -0
  3. data/CHANGELOG.rdoc +755 -0
  4. data/Gemfile +35 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +366 -0
  7. data/Rakefile +34 -0
  8. data/app/controllers/devise/confirmations_controller.rb +46 -0
  9. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  10. data/app/controllers/devise/passwords_controller.rb +50 -0
  11. data/app/controllers/devise/registrations_controller.rb +114 -0
  12. data/app/controllers/devise/sessions_controller.rb +49 -0
  13. data/app/controllers/devise/unlocks_controller.rb +34 -0
  14. data/app/helpers/devise_helper.rb +25 -0
  15. data/app/mailers/devise/mailer.rb +15 -0
  16. data/app/views/devise/confirmations/new.html.erb +12 -0
  17. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  18. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  19. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  20. data/app/views/devise/passwords/edit.html.erb +16 -0
  21. data/app/views/devise/passwords/new.html.erb +12 -0
  22. data/app/views/devise/registrations/edit.html.erb +25 -0
  23. data/app/views/devise/registrations/new.html.erb +18 -0
  24. data/app/views/devise/sessions/new.html.erb +17 -0
  25. data/app/views/devise/shared/_links.erb +25 -0
  26. data/app/views/devise/unlocks/new.html.erb +12 -0
  27. data/cloudfoundry-devise.gemspec +25 -0
  28. data/config/locales/en.yml +59 -0
  29. data/lib/devise.rb +453 -0
  30. data/lib/devise/controllers/helpers.rb +260 -0
  31. data/lib/devise/controllers/internal_helpers.rb +161 -0
  32. data/lib/devise/controllers/rememberable.rb +52 -0
  33. data/lib/devise/controllers/scoped_views.rb +33 -0
  34. data/lib/devise/controllers/shared_helpers.rb +26 -0
  35. data/lib/devise/controllers/url_helpers.rb +53 -0
  36. data/lib/devise/delegator.rb +16 -0
  37. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  38. data/lib/devise/encryptors/base.rb +20 -0
  39. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  40. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  41. data/lib/devise/encryptors/sha1.rb +25 -0
  42. data/lib/devise/encryptors/sha512.rb +25 -0
  43. data/lib/devise/failure_app.rb +149 -0
  44. data/lib/devise/hooks/activatable.rb +11 -0
  45. data/lib/devise/hooks/forgetable.rb +9 -0
  46. data/lib/devise/hooks/rememberable.rb +6 -0
  47. data/lib/devise/hooks/timeoutable.rb +24 -0
  48. data/lib/devise/hooks/trackable.rb +9 -0
  49. data/lib/devise/mailers/helpers.rb +86 -0
  50. data/lib/devise/mapping.rb +175 -0
  51. data/lib/devise/models.rb +91 -0
  52. data/lib/devise/models/authenticatable.rb +181 -0
  53. data/lib/devise/models/confirmable.rb +220 -0
  54. data/lib/devise/models/database_authenticatable.rb +122 -0
  55. data/lib/devise/models/encryptable.rb +72 -0
  56. data/lib/devise/models/lockable.rb +169 -0
  57. data/lib/devise/models/omniauthable.rb +23 -0
  58. data/lib/devise/models/recoverable.rb +136 -0
  59. data/lib/devise/models/registerable.rb +21 -0
  60. data/lib/devise/models/rememberable.rb +114 -0
  61. data/lib/devise/models/serializable.rb +43 -0
  62. data/lib/devise/models/timeoutable.rb +45 -0
  63. data/lib/devise/models/token_authenticatable.rb +72 -0
  64. data/lib/devise/models/trackable.rb +30 -0
  65. data/lib/devise/models/validatable.rb +62 -0
  66. data/lib/devise/modules.rb +30 -0
  67. data/lib/devise/omniauth.rb +28 -0
  68. data/lib/devise/omniauth/config.rb +45 -0
  69. data/lib/devise/omniauth/url_helpers.rb +33 -0
  70. data/lib/devise/orm/active_record.rb +44 -0
  71. data/lib/devise/orm/mongoid.rb +31 -0
  72. data/lib/devise/param_filter.rb +41 -0
  73. data/lib/devise/path_checker.rb +18 -0
  74. data/lib/devise/rails.rb +73 -0
  75. data/lib/devise/rails/routes.rb +385 -0
  76. data/lib/devise/rails/warden_compat.rb +120 -0
  77. data/lib/devise/schema.rb +109 -0
  78. data/lib/devise/strategies/authenticatable.rb +155 -0
  79. data/lib/devise/strategies/base.rb +15 -0
  80. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  81. data/lib/devise/strategies/rememberable.rb +53 -0
  82. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  83. data/lib/devise/test_helpers.rb +90 -0
  84. data/lib/devise/version.rb +3 -0
  85. data/lib/generators/active_record/devise_generator.rb +71 -0
  86. data/lib/generators/active_record/templates/migration.rb +29 -0
  87. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  88. data/lib/generators/devise/devise_generator.rb +22 -0
  89. data/lib/generators/devise/install_generator.rb +24 -0
  90. data/lib/generators/devise/orm_helpers.rb +31 -0
  91. data/lib/generators/devise/views_generator.rb +98 -0
  92. data/lib/generators/mongoid/devise_generator.rb +60 -0
  93. data/lib/generators/templates/README +32 -0
  94. data/lib/generators/templates/devise.rb +215 -0
  95. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  96. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  97. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  98. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  99. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  100. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  101. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  102. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  103. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  104. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  105. data/test/controllers/helpers_test.rb +254 -0
  106. data/test/controllers/internal_helpers_test.rb +96 -0
  107. data/test/controllers/sessions_controller_test.rb +16 -0
  108. data/test/controllers/url_helpers_test.rb +59 -0
  109. data/test/delegator_test.rb +19 -0
  110. data/test/devise_test.rb +72 -0
  111. data/test/encryptors_test.rb +30 -0
  112. data/test/failure_app_test.rb +207 -0
  113. data/test/generators/active_record_generator_test.rb +47 -0
  114. data/test/generators/devise_generator_test.rb +39 -0
  115. data/test/generators/install_generator_test.rb +13 -0
  116. data/test/generators/mongoid_generator_test.rb +23 -0
  117. data/test/generators/views_generator_test.rb +52 -0
  118. data/test/helpers/devise_helper_test.rb +51 -0
  119. data/test/indifferent_hash.rb +33 -0
  120. data/test/integration/authenticatable_test.rb +590 -0
  121. data/test/integration/confirmable_test.rb +262 -0
  122. data/test/integration/database_authenticatable_test.rb +82 -0
  123. data/test/integration/http_authenticatable_test.rb +82 -0
  124. data/test/integration/lockable_test.rb +212 -0
  125. data/test/integration/omniauthable_test.rb +133 -0
  126. data/test/integration/recoverable_test.rb +287 -0
  127. data/test/integration/registerable_test.rb +335 -0
  128. data/test/integration/rememberable_test.rb +158 -0
  129. data/test/integration/timeoutable_test.rb +98 -0
  130. data/test/integration/token_authenticatable_test.rb +148 -0
  131. data/test/integration/trackable_test.rb +92 -0
  132. data/test/mailers/confirmation_instructions_test.rb +95 -0
  133. data/test/mailers/reset_password_instructions_test.rb +83 -0
  134. data/test/mailers/unlock_instructions_test.rb +77 -0
  135. data/test/mapping_test.rb +128 -0
  136. data/test/models/confirmable_test.rb +334 -0
  137. data/test/models/database_authenticatable_test.rb +167 -0
  138. data/test/models/encryptable_test.rb +67 -0
  139. data/test/models/lockable_test.rb +225 -0
  140. data/test/models/recoverable_test.rb +198 -0
  141. data/test/models/rememberable_test.rb +168 -0
  142. data/test/models/serializable_test.rb +38 -0
  143. data/test/models/timeoutable_test.rb +42 -0
  144. data/test/models/token_authenticatable_test.rb +49 -0
  145. data/test/models/trackable_test.rb +5 -0
  146. data/test/models/validatable_test.rb +113 -0
  147. data/test/models_test.rb +109 -0
  148. data/test/omniauth/config_test.rb +57 -0
  149. data/test/omniauth/url_helpers_test.rb +58 -0
  150. data/test/orm/active_record.rb +9 -0
  151. data/test/orm/mongoid.rb +14 -0
  152. data/test/rails_app/Rakefile +10 -0
  153. data/test/rails_app/app/active_record/admin.rb +6 -0
  154. data/test/rails_app/app/active_record/shim.rb +2 -0
  155. data/test/rails_app/app/active_record/user.rb +6 -0
  156. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  157. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  158. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  159. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  160. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  161. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  162. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  163. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  164. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  165. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  166. data/test/rails_app/app/mongoid/admin.rb +24 -0
  167. data/test/rails_app/app/mongoid/shim.rb +24 -0
  168. data/test/rails_app/app/mongoid/user.rb +45 -0
  169. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  170. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  171. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  172. data/test/rails_app/app/views/home/index.html.erb +1 -0
  173. data/test/rails_app/app/views/home/join.html.erb +1 -0
  174. data/test/rails_app/app/views/home/private.html.erb +1 -0
  175. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  176. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  177. data/test/rails_app/app/views/users/index.html.erb +1 -0
  178. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  179. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  180. data/test/rails_app/config.ru +4 -0
  181. data/test/rails_app/config/application.rb +41 -0
  182. data/test/rails_app/config/boot.rb +8 -0
  183. data/test/rails_app/config/database.yml +18 -0
  184. data/test/rails_app/config/environment.rb +5 -0
  185. data/test/rails_app/config/environments/development.rb +18 -0
  186. data/test/rails_app/config/environments/production.rb +33 -0
  187. data/test/rails_app/config/environments/test.rb +33 -0
  188. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  189. data/test/rails_app/config/initializers/devise.rb +197 -0
  190. data/test/rails_app/config/initializers/inflections.rb +2 -0
  191. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  192. data/test/rails_app/config/routes.rb +87 -0
  193. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +71 -0
  194. data/test/rails_app/db/schema.rb +52 -0
  195. data/test/rails_app/lib/shared_admin.rb +10 -0
  196. data/test/rails_app/lib/shared_user.rb +26 -0
  197. data/test/rails_app/public/404.html +26 -0
  198. data/test/rails_app/public/422.html +26 -0
  199. data/test/rails_app/public/500.html +26 -0
  200. data/test/rails_app/public/favicon.ico +0 -0
  201. data/test/rails_app/script/rails +10 -0
  202. data/test/routes_test.rb +240 -0
  203. data/test/support/assertions.rb +27 -0
  204. data/test/support/helpers.rb +109 -0
  205. data/test/support/integration.rb +88 -0
  206. data/test/support/locale/en.yml +4 -0
  207. data/test/support/webrat/integrations/rails.rb +24 -0
  208. data/test/test_helper.rb +27 -0
  209. data/test/test_helpers_test.rb +134 -0
  210. metadata +295 -0
@@ -0,0 +1,95 @@
1
+ require 'test_helper'
2
+
3
+ class ConfirmationInstructionsTest < ActionMailer::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ Devise.mailer = 'Devise::Mailer'
8
+ Devise.mailer_sender = 'test@example.com'
9
+ end
10
+
11
+ def teardown
12
+ Devise.mailer = 'Devise::Mailer'
13
+ Devise.mailer_sender = 'please-change-me@config-initializers-devise.com'
14
+ end
15
+
16
+ def user
17
+ @user ||= create_user
18
+ end
19
+
20
+ def mail
21
+ @mail ||= begin
22
+ user
23
+ ActionMailer::Base.deliveries.first
24
+ end
25
+ end
26
+
27
+ test 'email sent after creating the user' do
28
+ assert_not_nil mail
29
+ end
30
+
31
+ test 'content type should be set to html' do
32
+ assert mail.content_type.include?('text/html')
33
+ end
34
+
35
+ test 'send confirmation instructions to the user email' do
36
+ mail
37
+ assert_equal [user.email], mail.to
38
+ end
39
+
40
+ test 'setup sender from configuration' do
41
+ assert_equal ['test@example.com'], mail.from
42
+ end
43
+
44
+ test 'setup sender from custom mailer defaults' do
45
+ Devise.mailer = 'Users::Mailer'
46
+ assert_equal ['custom@example.com'], mail.from
47
+ end
48
+
49
+ test 'setup reply to as copy from sender' do
50
+ assert_equal ['test@example.com'], mail.reply_to
51
+ end
52
+
53
+ test 'setup subject from I18n' do
54
+ store_translations :en, :devise => { :mailer => { :confirmation_instructions => { :subject => 'Account Confirmation' } } } do
55
+ assert_equal 'Account Confirmation', mail.subject
56
+ end
57
+ end
58
+
59
+ test 'subject namespaced by model' do
60
+ store_translations :en, :devise => { :mailer => { :confirmation_instructions => { :user_subject => 'User Account Confirmation' } } } do
61
+ assert_equal 'User Account Confirmation', mail.subject
62
+ end
63
+ end
64
+
65
+ test 'body should have user info' do
66
+ assert_match /#{user.email}/, mail.body.encoded
67
+ end
68
+
69
+ test 'body should have link to confirm the account' do
70
+ host = ActionMailer::Base.default_url_options[:host]
71
+ confirmation_url_regexp = %r{<a href=\"http://#{host}/users/confirmation\?confirmation_token=#{user.confirmation_token}">}
72
+ assert_match confirmation_url_regexp, mail.body.encoded
73
+ end
74
+
75
+ test 'renders a scoped if scoped_views is set to true' do
76
+ swap Devise, :scoped_views => true do
77
+ assert_equal user.email, mail.body.decoded
78
+ end
79
+ end
80
+
81
+ test 'renders a scoped if scoped_views is set in the mailer class' do
82
+ begin
83
+ Devise::Mailer.scoped_views = true
84
+ assert_equal user.email, mail.body.decoded
85
+ ensure
86
+ Devise::Mailer.send :remove_instance_variable, :@scoped_views
87
+ end
88
+ end
89
+
90
+ test 'mailer sender accepts a proc' do
91
+ swap Devise, :mailer_sender => proc { "another@example.com" } do
92
+ assert_equal ['another@example.com'], mail.from
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,83 @@
1
+ require 'test_helper'
2
+
3
+ class ResetPasswordInstructionsTest < ActionMailer::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ Devise.mailer = 'Devise::Mailer'
8
+ Devise.mailer_sender = 'test@example.com'
9
+ end
10
+
11
+ def teardown
12
+ Devise.mailer = 'Devise::Mailer'
13
+ Devise.mailer_sender = 'please-change-me@config-initializers-devise.com'
14
+ end
15
+
16
+ def user
17
+ @user ||= begin
18
+ user = create_user
19
+ user.send_reset_password_instructions
20
+ user
21
+ end
22
+ end
23
+
24
+ def mail
25
+ @mail ||= begin
26
+ user
27
+ ActionMailer::Base.deliveries.last
28
+ end
29
+ end
30
+
31
+ test 'email sent after reseting the user password' do
32
+ assert_not_nil mail
33
+ end
34
+
35
+ test 'content type should be set to html' do
36
+ assert mail.content_type.include?('text/html')
37
+ end
38
+
39
+ test 'send confirmation instructions to the user email' do
40
+ assert_equal [user.email], mail.to
41
+ end
42
+
43
+ test 'setup sender from configuration' do
44
+ assert_equal ['test@example.com'], mail.from
45
+ end
46
+
47
+ test 'setup sender from custom mailer defaults' do
48
+ Devise.mailer = 'Users::Mailer'
49
+ assert_equal ['custom@example.com'], mail.from
50
+ end
51
+
52
+ test 'setup reply to as copy from sender' do
53
+ assert_equal ['test@example.com'], mail.reply_to
54
+ end
55
+
56
+ test 'setup subject from I18n' do
57
+ store_translations :en, :devise => { :mailer => { :reset_password_instructions => { :subject => 'Reset instructions' } } } do
58
+ assert_equal 'Reset instructions', mail.subject
59
+ end
60
+ end
61
+
62
+ test 'subject namespaced by model' do
63
+ store_translations :en, :devise => { :mailer => { :reset_password_instructions => { :user_subject => 'User Reset Instructions' } } } do
64
+ assert_equal 'User Reset Instructions', mail.subject
65
+ end
66
+ end
67
+
68
+ test 'body should have user info' do
69
+ assert_match(/#{user.email}/, mail.body.encoded)
70
+ end
71
+
72
+ test 'body should have link to confirm the account' do
73
+ host = ActionMailer::Base.default_url_options[:host]
74
+ reset_url_regexp = %r{<a href=\"http://#{host}/users/password/edit\?reset_password_token=#{user.reset_password_token}">}
75
+ assert_match reset_url_regexp, mail.body.encoded
76
+ end
77
+
78
+ test 'mailer sender accepts a proc' do
79
+ swap Devise, :mailer_sender => proc { "another@example.com" } do
80
+ assert_equal ['another@example.com'], mail.from
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ class UnlockInstructionsTest < ActionMailer::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ Devise.mailer = 'Devise::Mailer'
8
+ Devise.mailer_sender = 'test@example.com'
9
+ end
10
+
11
+ def teardown
12
+ Devise.mailer = 'Devise::Mailer'
13
+ Devise.mailer_sender = 'please-change-me@config-initializers-devise.com'
14
+ end
15
+
16
+ def user
17
+ @user ||= begin
18
+ user = create_user
19
+ user.lock_access!
20
+ user
21
+ end
22
+ end
23
+
24
+ def mail
25
+ @mail ||= begin
26
+ user
27
+ ActionMailer::Base.deliveries.last
28
+ end
29
+ end
30
+
31
+ test 'email sent after locking the user' do
32
+ assert_not_nil mail
33
+ end
34
+
35
+ test 'content type should be set to html' do
36
+ assert mail.content_type.include?('text/html')
37
+ end
38
+
39
+ test 'send unlock instructions to the user email' do
40
+ assert_equal [user.email], mail.to
41
+ end
42
+
43
+ test 'setup sender from configuration' do
44
+ assert_equal ['test@example.com'], mail.from
45
+ end
46
+
47
+ test 'setup sender from custom mailer defaults' do
48
+ Devise.mailer = 'Users::Mailer'
49
+ assert_equal ['custom@example.com'], mail.from
50
+ end
51
+
52
+ test 'setup reply to as copy from sender' do
53
+ assert_equal ['test@example.com'], mail.reply_to
54
+ end
55
+
56
+ test 'setup subject from I18n' do
57
+ store_translations :en, :devise => { :mailer => { :unlock_instructions => { :subject => 'Yo unlock instructions' } } } do
58
+ assert_equal 'Yo unlock instructions', mail.subject
59
+ end
60
+ end
61
+
62
+ test 'subject namespaced by model' do
63
+ store_translations :en, :devise => { :mailer => { :unlock_instructions => { :user_subject => 'User Unlock Instructions' } } } do
64
+ assert_equal 'User Unlock Instructions', mail.subject
65
+ end
66
+ end
67
+
68
+ test 'body should have user info' do
69
+ assert_match(/#{user.email}/, mail.body.encoded)
70
+ end
71
+
72
+ test 'body should have link to unlock the account' do
73
+ host = ActionMailer::Base.default_url_options[:host]
74
+ unlock_url_regexp = %r{<a href=\"http://#{host}/users/unlock\?unlock_token=#{user.unlock_token}">}
75
+ assert_match unlock_url_regexp, mail.body.encoded
76
+ end
77
+ end
@@ -0,0 +1,128 @@
1
+ require 'test_helper'
2
+
3
+ class FakeRequest < Struct.new(:path_info, :params)
4
+ end
5
+
6
+ class MappingTest < ActiveSupport::TestCase
7
+ def fake_request(path, params={})
8
+ FakeRequest.new(path, params)
9
+ end
10
+
11
+ test 'store options' do
12
+ mapping = Devise.mappings[:user]
13
+ assert_equal User, mapping.to
14
+ assert_equal User.devise_modules, mapping.modules
15
+ assert_equal "users", mapping.scoped_path
16
+ assert_equal :user, mapping.singular
17
+ assert_equal "users", mapping.path
18
+ assert_equal "/users", mapping.fullpath
19
+ end
20
+
21
+ test 'store options with namespace' do
22
+ mapping = Devise.mappings[:publisher_account]
23
+ assert_equal Admin, mapping.to
24
+ assert_equal "publisher/accounts", mapping.scoped_path
25
+ assert_equal :publisher_account, mapping.singular
26
+ assert_equal "accounts", mapping.path
27
+ assert_equal "/publisher/accounts", mapping.fullpath
28
+ end
29
+
30
+ test 'allows path to be given' do
31
+ assert_equal "admin_area", Devise.mappings[:admin].path
32
+ end
33
+
34
+ test 'allows to skip all routes' do
35
+ assert_equal [], Devise.mappings[:skip_admin].used_routes
36
+ end
37
+
38
+ test 'sign_out_via defaults to :get' do
39
+ assert_equal :get, Devise.mappings[:user].sign_out_via
40
+ end
41
+
42
+ test 'allows custom sign_out_via to be given' do
43
+ assert_equal :delete, Devise.mappings[:sign_out_via_delete].sign_out_via
44
+ assert_equal :post, Devise.mappings[:sign_out_via_post].sign_out_via
45
+ assert_equal [:delete, :post], Devise.mappings[:sign_out_via_delete_or_post].sign_out_via
46
+ end
47
+
48
+ test 'allows custom singular to be given' do
49
+ assert_equal "accounts", Devise.mappings[:manager].path
50
+ end
51
+
52
+ test 'has strategies depending on the model declaration' do
53
+ assert_equal [:rememberable, :token_authenticatable, :database_authenticatable], Devise.mappings[:user].strategies
54
+ assert_equal [:database_authenticatable], Devise.mappings[:admin].strategies
55
+ end
56
+
57
+ test 'has no input strategies depending on the model declaration' do
58
+ assert_equal [:rememberable, :token_authenticatable], Devise.mappings[:user].no_input_strategies
59
+ assert_equal [], Devise.mappings[:admin].no_input_strategies
60
+ end
61
+
62
+ test 'find scope for a given object' do
63
+ assert_equal :user, Devise::Mapping.find_scope!(User)
64
+ assert_equal :user, Devise::Mapping.find_scope!(:user)
65
+ assert_equal :user, Devise::Mapping.find_scope!(User.new)
66
+ end
67
+
68
+ test 'find scope works with single table inheritance' do
69
+ assert_equal :user, Devise::Mapping.find_scope!(Class.new(User))
70
+ assert_equal :user, Devise::Mapping.find_scope!(Class.new(User).new)
71
+ end
72
+
73
+ test 'find scope raises an error if cannot be found' do
74
+ assert_raise RuntimeError do
75
+ Devise::Mapping.find_scope!(String)
76
+ end
77
+ end
78
+
79
+ test 'return default path names' do
80
+ mapping = Devise.mappings[:user]
81
+ assert_equal 'sign_in', mapping.path_names[:sign_in]
82
+ assert_equal 'sign_out', mapping.path_names[:sign_out]
83
+ assert_equal 'password', mapping.path_names[:password]
84
+ assert_equal 'confirmation', mapping.path_names[:confirmation]
85
+ assert_equal 'sign_up', mapping.path_names[:sign_up]
86
+ assert_equal 'unlock', mapping.path_names[:unlock]
87
+ end
88
+
89
+ test 'allow custom path names to be given' do
90
+ mapping = Devise.mappings[:manager]
91
+ assert_equal 'login', mapping.path_names[:sign_in]
92
+ assert_equal 'logout', mapping.path_names[:sign_out]
93
+ assert_equal 'secret', mapping.path_names[:password]
94
+ assert_equal 'verification', mapping.path_names[:confirmation]
95
+ assert_equal 'register', mapping.path_names[:sign_up]
96
+ assert_equal 'unblock', mapping.path_names[:unlock]
97
+ end
98
+
99
+ test 'magic predicates' do
100
+ mapping = Devise.mappings[:user]
101
+ assert mapping.authenticatable?
102
+ assert mapping.confirmable?
103
+ assert mapping.recoverable?
104
+ assert mapping.rememberable?
105
+ assert mapping.registerable?
106
+
107
+ mapping = Devise.mappings[:admin]
108
+ assert mapping.authenticatable?
109
+ assert mapping.recoverable?
110
+ assert mapping.lockable?
111
+ assert_not mapping.confirmable?
112
+ assert_not mapping.omniauthable?
113
+ end
114
+
115
+ test 'find mapping by path' do
116
+ assert_raise RuntimeError do
117
+ Devise::Mapping.find_by_path!('/accounts/facebook/callback')
118
+ end
119
+
120
+ assert_nothing_raised do
121
+ Devise::Mapping.find_by_path!('/:locale/accounts/login')
122
+ end
123
+
124
+ assert_nothing_raised do
125
+ Devise::Mapping.find_by_path!('/accounts/facebook/callback', :path)
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,334 @@
1
+ require 'test_helper'
2
+
3
+ class ConfirmableTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ setup_mailer
7
+ end
8
+
9
+ test 'should generate confirmation token after creating a record' do
10
+ assert_nil new_user.confirmation_token
11
+ assert_not_nil create_user.confirmation_token
12
+ end
13
+
14
+ test 'should never generate the same confirmation token for different users' do
15
+ confirmation_tokens = []
16
+ 3.times do
17
+ token = create_user.confirmation_token
18
+ assert !confirmation_tokens.include?(token)
19
+ confirmation_tokens << token
20
+ end
21
+ end
22
+
23
+ test 'should confirm a user by updating confirmed at' do
24
+ user = create_user
25
+ assert_nil user.confirmed_at
26
+ assert user.confirm!
27
+ assert_not_nil user.confirmed_at
28
+ end
29
+
30
+ test 'should clear confirmation token while confirming a user' do
31
+ user = create_user
32
+ assert_present user.confirmation_token
33
+ user.confirm!
34
+ assert_nil user.confirmation_token
35
+ end
36
+
37
+ test 'should verify whether a user is confirmed or not' do
38
+ assert_not new_user.confirmed?
39
+ user = create_user
40
+ assert_not user.confirmed?
41
+ user.confirm!
42
+ assert user.confirmed?
43
+ end
44
+
45
+ test 'should not confirm a user already confirmed' do
46
+ user = create_user
47
+ assert user.confirm!
48
+ assert_blank user.errors[:email]
49
+
50
+ assert_not user.confirm!
51
+ assert_equal "was already confirmed, please try signing in", user.errors[:email].join
52
+ end
53
+
54
+ test 'should find and confirm a user automatically' do
55
+ user = create_user
56
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
57
+ assert_equal confirmed_user, user
58
+ assert user.reload.confirmed?
59
+ end
60
+
61
+ test 'should return a new record with errors when a invalid token is given' do
62
+ confirmed_user = User.confirm_by_token('invalid_confirmation_token')
63
+ assert_not confirmed_user.persisted?
64
+ assert_equal "is invalid", confirmed_user.errors[:confirmation_token].join
65
+ end
66
+
67
+ test 'should return a new record with errors when a blank token is given' do
68
+ confirmed_user = User.confirm_by_token('')
69
+ assert_not confirmed_user.persisted?
70
+ assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
71
+ end
72
+
73
+ test 'should generate errors for a user email if user is already confirmed' do
74
+ user = create_user
75
+ user.confirmed_at = Time.now
76
+ user.save
77
+ confirmed_user = User.confirm_by_token(user.confirmation_token)
78
+ assert confirmed_user.confirmed?
79
+ assert_equal "was already confirmed, please try signing in", confirmed_user.errors[:email].join
80
+ end
81
+
82
+ test 'should send confirmation instructions by email' do
83
+ assert_email_sent "mynewuser@example.com" do
84
+ create_user :email => "mynewuser@example.com"
85
+ end
86
+ end
87
+
88
+ test 'should not send confirmation when trying to save an invalid user' do
89
+ assert_email_not_sent do
90
+ user = new_user
91
+ user.stubs(:valid?).returns(false)
92
+ user.save
93
+ end
94
+ end
95
+
96
+ test 'should not generate a new token neither send e-mail if skip_confirmation! is invoked' do
97
+ user = new_user
98
+ user.skip_confirmation!
99
+
100
+ assert_email_not_sent do
101
+ user.save!
102
+ assert_nil user.confirmation_token
103
+ assert_not_nil user.confirmed_at
104
+ end
105
+ end
106
+
107
+ test 'should find a user to send confirmation instructions' do
108
+ user = create_user
109
+ confirmation_user = User.send_confirmation_instructions(:email => user.email)
110
+ assert_equal confirmation_user, user
111
+ end
112
+
113
+ test 'should return a new user if no email was found' do
114
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@example.com")
115
+ assert_not confirmation_user.persisted?
116
+ end
117
+
118
+ test 'should add error to new user email if no email was found' do
119
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@example.com")
120
+ assert confirmation_user.errors[:email]
121
+ assert_equal "not found", confirmation_user.errors[:email].join
122
+ end
123
+
124
+ test 'should send email instructions for the user confirm its email' do
125
+ user = create_user
126
+ assert_email_sent user.email do
127
+ User.send_confirmation_instructions(:email => user.email)
128
+ end
129
+ end
130
+
131
+ test 'should always have confirmation token when email is sent' do
132
+ user = new_user
133
+ user.instance_eval { def confirmation_required?; false end }
134
+ user.save
135
+ user.send_confirmation_instructions
136
+ assert_not_nil user.reload.confirmation_token
137
+ end
138
+
139
+ test 'should not resend email instructions if the user change his email' do
140
+ user = create_user
141
+ user.email = 'new_test@example.com'
142
+ assert_email_not_sent do
143
+ user.save!
144
+ end
145
+ end
146
+
147
+ test 'should not reset confirmation status or token when updating email' do
148
+ user = create_user
149
+ user.confirm!
150
+ user.email = 'new_test@example.com'
151
+ user.save!
152
+
153
+ user.reload
154
+ assert user.confirmed?
155
+ assert_nil user.confirmation_token
156
+ end
157
+
158
+ test 'should not be able to send instructions if the user is already confirmed' do
159
+ user = create_user
160
+ user.confirm!
161
+ assert_not user.resend_confirmation_token
162
+ assert user.confirmed?
163
+ assert_equal 'was already confirmed, please try signing in', user.errors[:email].join
164
+ end
165
+
166
+ test 'confirm time should fallback to devise confirm in default configuration' do
167
+ swap Devise, :confirm_within => 1.day do
168
+ user = new_user
169
+ user.confirmation_sent_at = 2.days.ago
170
+ assert_not user.active_for_authentication?
171
+
172
+ Devise.confirm_within = 3.days
173
+ assert user.active_for_authentication?
174
+ end
175
+ end
176
+
177
+ test 'should be active when confirmation sent at is not overpast' do
178
+ swap Devise, :confirm_within => 5.days do
179
+ Devise.confirm_within = 5.days
180
+ user = create_user
181
+
182
+ user.confirmation_sent_at = 4.days.ago
183
+ assert user.active_for_authentication?
184
+
185
+ user.confirmation_sent_at = 5.days.ago
186
+ assert_not user.active_for_authentication?
187
+ end
188
+ end
189
+
190
+ test 'should be active when already confirmed' do
191
+ user = create_user
192
+ assert_not user.confirmed?
193
+ assert_not user.active_for_authentication?
194
+
195
+ user.confirm!
196
+ assert user.confirmed?
197
+ assert user.active_for_authentication?
198
+ end
199
+
200
+ test 'should not be active when confirm in is zero' do
201
+ Devise.confirm_within = 0.days
202
+ user = create_user
203
+ user.confirmation_sent_at = Date.today
204
+ assert_not user.active_for_authentication?
205
+ end
206
+
207
+ test 'should not be active without confirmation' do
208
+ user = create_user
209
+ user.confirmation_sent_at = nil
210
+ user.save
211
+ assert_not user.reload.active_for_authentication?
212
+ end
213
+
214
+ test 'should be active without confirmation when confirmation is not required' do
215
+ user = create_user
216
+ user.instance_eval { def confirmation_required?; false end }
217
+ user.confirmation_sent_at = nil
218
+ user.save
219
+ assert user.reload.active_for_authentication?
220
+ end
221
+
222
+ test 'should find a user to send email instructions for the user confirm its email by authentication_keys' do
223
+ swap Devise, :authentication_keys => [:username, :email] do
224
+ user = create_user
225
+ confirm_user = User.send_confirmation_instructions(:email => user.email, :username => user.username)
226
+ assert_equal confirm_user, user
227
+ end
228
+ end
229
+
230
+ test 'should require all confirmation_keys' do
231
+ swap Devise, :confirmation_keys => [:username, :email] do
232
+ user = create_user
233
+ confirm_user = User.send_confirmation_instructions(:email => user.email)
234
+ assert_not confirm_user.persisted?
235
+ assert_equal "can't be blank", confirm_user.errors[:username].join
236
+ end
237
+ end
238
+ end
239
+
240
+ class ReconfirmableTest < ConfirmableTest
241
+ def setup
242
+ add_unconfirmed_email_column
243
+ Devise.reconfirmable = true
244
+ end
245
+
246
+ def teardown
247
+ remove_unconfirmed_email_column
248
+ Devise.reconfirmable = false
249
+ end
250
+
251
+ def test_should_not_resend_email_instructions_if_the_user_change_his_email
252
+ #behaves differently
253
+ end
254
+
255
+ def test_should_not_reset_confirmation_status_or_token_when_updating_email
256
+ #behaves differently
257
+ end
258
+
259
+ test 'should generate confirmation token after changing email' do
260
+ user = create_user
261
+ assert user.confirm!
262
+ assert_nil user.confirmation_token
263
+ assert user.update_attributes(:email => 'new_test@example.com')
264
+ assert_not_nil user.confirmation_token
265
+ end
266
+
267
+ test 'should send confirmation instructions by email after changing email' do
268
+ user = create_user
269
+ assert user.confirm!
270
+ assert_email_sent "new_test@example.com" do
271
+ assert user.update_attributes(:email => 'new_test@example.com')
272
+ end
273
+ end
274
+
275
+ test 'should not send confirmation by email after changing password' do
276
+ user = create_user
277
+ assert user.confirm!
278
+ assert_email_not_sent do
279
+ assert user.update_attributes(:password => 'newpass', :password_confirmation => 'newpass')
280
+ end
281
+ end
282
+
283
+ test 'should stay confirmed when email is changed' do
284
+ user = create_user
285
+ assert user.confirm!
286
+ assert user.update_attributes(:email => 'new_test@example.com')
287
+ assert user.confirmed?
288
+ end
289
+
290
+ test 'should update email only when it is confirmed' do
291
+ user = create_user
292
+ assert user.confirm!
293
+ assert user.update_attributes(:email => 'new_test@example.com')
294
+ assert_not_equal 'new_test@example.com', user.email
295
+ assert user.confirm!
296
+ assert_equal 'new_test@example.com', user.email
297
+ end
298
+
299
+ test 'should not allow user to get past confirmation email by resubmitting their new address' do
300
+ user = create_user
301
+ assert user.confirm!
302
+ assert user.update_attributes(:email => 'new_test@example.com')
303
+ assert_not_equal 'new_test@example.com', user.email
304
+ assert user.update_attributes(:email => 'new_test@example.com')
305
+ assert_not_equal 'new_test@example.com', user.email
306
+ end
307
+
308
+ test 'should find a user by send confirmation instructions with unconfirmed_email' do
309
+ user = create_user
310
+ assert user.confirm!
311
+ assert user.update_attributes(:email => 'new_test@example.com')
312
+ confirmation_user = User.send_confirmation_instructions(:email => user.unconfirmed_email)
313
+ assert_equal confirmation_user, user
314
+ end
315
+
316
+ test 'should return a new user if no email or unconfirmed_email was found' do
317
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
318
+ assert_not confirmation_user.persisted?
319
+ end
320
+
321
+ test 'should add error to new user email if no email or unconfirmed_email was found' do
322
+ confirmation_user = User.send_confirmation_instructions(:email => "invalid@email.com")
323
+ assert confirmation_user.errors[:email]
324
+ assert_equal "not found", confirmation_user.errors[:email].join
325
+ end
326
+
327
+ test 'should find user with email in unconfirmed_emails' do
328
+ user = create_user
329
+ user.unconfirmed_email = "new_test@email.com"
330
+ assert user.save
331
+ user = User.find_by_unconfirmed_email_with_errors(:email => "new_test@email.com")
332
+ assert user.persisted?
333
+ end
334
+ end