deviseOne 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (246) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.travis.yml +38 -0
  4. data/.yardopts +9 -0
  5. data/CHANGELOG.md +1117 -0
  6. data/CONTRIBUTING.md +14 -0
  7. data/Gemfile +29 -0
  8. data/Gemfile.lock +199 -0
  9. data/MIT-LICENSE +20 -0
  10. data/README.md +529 -0
  11. data/Rakefile +35 -0
  12. data/app/controllers/devise/confirmations_controller.rb +47 -0
  13. data/app/controllers/devise/omniauth_callbacks_controller.rb +30 -0
  14. data/app/controllers/devise/passwords_controller.rb +71 -0
  15. data/app/controllers/devise/registrations_controller.rb +143 -0
  16. data/app/controllers/devise/sessions_controller.rb +166 -0
  17. data/app/controllers/devise/unlocks_controller.rb +46 -0
  18. data/app/controllers/devise_controller.rb +193 -0
  19. data/app/helpers/devise_helper.rb +25 -0
  20. data/app/mailers/devise/mailer.rb +20 -0
  21. data/app/views/devise/confirmations/new.html.erb +16 -0
  22. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  23. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  24. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  25. data/app/views/devise/passwords/edit.html.erb +25 -0
  26. data/app/views/devise/passwords/new.html.erb +16 -0
  27. data/app/views/devise/registrations/edit.html.erb +39 -0
  28. data/app/views/devise/registrations/new.html.erb +29 -0
  29. data/app/views/devise/sessions/new.html.erb +27 -0
  30. data/app/views/devise/shared/_links.html.erb +21 -0
  31. data/app/views/devise/unlocks/new.html.erb +16 -0
  32. data/config/locales/en.yml +70 -0
  33. data/devise.gemspec +33 -0
  34. data/devise.png +0 -0
  35. data/gemfiles/Gemfile.rails-3.2-stable +29 -0
  36. data/gemfiles/Gemfile.rails-3.2-stable.lock +169 -0
  37. data/gemfiles/Gemfile.rails-4.0-stable +29 -0
  38. data/gemfiles/Gemfile.rails-4.0-stable.lock +165 -0
  39. data/gemfiles/Gemfile.rails-4.1-stable +29 -0
  40. data/gemfiles/Gemfile.rails-4.1-stable.lock +170 -0
  41. data/lib/devise.rb +499 -0
  42. data/lib/devise/controllers/helpers.rb +284 -0
  43. data/lib/devise/controllers/rememberable.rb +47 -0
  44. data/lib/devise/controllers/scoped_views.rb +17 -0
  45. data/lib/devise/controllers/sign_in_out.rb +102 -0
  46. data/lib/devise/controllers/store_location.rb +58 -0
  47. data/lib/devise/controllers/url_helpers.rb +69 -0
  48. data/lib/devise/delegator.rb +16 -0
  49. data/lib/devise/failure_app.rb +212 -0
  50. data/lib/devise/hooks/activatable.rb +10 -0
  51. data/lib/devise/hooks/csrf_cleaner.rb +7 -0
  52. data/lib/devise/hooks/forgetable.rb +9 -0
  53. data/lib/devise/hooks/lockable.rb +7 -0
  54. data/lib/devise/hooks/proxy.rb +21 -0
  55. data/lib/devise/hooks/rememberable.rb +7 -0
  56. data/lib/devise/hooks/timeoutable.rb +35 -0
  57. data/lib/devise/hooks/trackable.rb +9 -0
  58. data/lib/devise/mailers/helpers.rb +90 -0
  59. data/lib/devise/mapping.rb +175 -0
  60. data/lib/devise/models.rb +119 -0
  61. data/lib/devise/models/authenticatable.rb +290 -0
  62. data/lib/devise/models/confirmable.rb +305 -0
  63. data/lib/devise/models/database_authenticatable.rb +164 -0
  64. data/lib/devise/models/lockable.rb +196 -0
  65. data/lib/devise/models/omniauthable.rb +27 -0
  66. data/lib/devise/models/recoverable.rb +157 -0
  67. data/lib/devise/models/registerable.rb +25 -0
  68. data/lib/devise/models/rememberable.rb +142 -0
  69. data/lib/devise/models/timeoutable.rb +49 -0
  70. data/lib/devise/models/trackable.rb +38 -0
  71. data/lib/devise/models/validatable.rb +66 -0
  72. data/lib/devise/modules.rb +28 -0
  73. data/lib/devise/omniauth.rb +28 -0
  74. data/lib/devise/omniauth/config.rb +45 -0
  75. data/lib/devise/omniauth/url_helpers.rb +18 -0
  76. data/lib/devise/orm/active_record.rb +3 -0
  77. data/lib/devise/orm/mongoid.rb +3 -0
  78. data/lib/devise/parameter_filter.rb +40 -0
  79. data/lib/devise/parameter_sanitizer.rb +99 -0
  80. data/lib/devise/rails.rb +56 -0
  81. data/lib/devise/rails/routes.rb +495 -0
  82. data/lib/devise/rails/warden_compat.rb +22 -0
  83. data/lib/devise/strategies/authenticatable.rb +173 -0
  84. data/lib/devise/strategies/base.rb +20 -0
  85. data/lib/devise/strategies/database_authenticatable.rb +24 -0
  86. data/lib/devise/strategies/rememberable.rb +59 -0
  87. data/lib/devise/test_helpers.rb +132 -0
  88. data/lib/devise/time_inflector.rb +14 -0
  89. data/lib/devise/token_generator.rb +70 -0
  90. data/lib/devise/version.rb +3 -0
  91. data/lib/generators/active_record/devise_generator.rb +91 -0
  92. data/lib/generators/active_record/templates/migration.rb +18 -0
  93. data/lib/generators/active_record/templates/migration_existing.rb +25 -0
  94. data/lib/generators/devise/controllers_generator.rb +44 -0
  95. data/lib/generators/devise/devise_generator.rb +26 -0
  96. data/lib/generators/devise/install_generator.rb +29 -0
  97. data/lib/generators/devise/orm_helpers.rb +51 -0
  98. data/lib/generators/devise/views_generator.rb +135 -0
  99. data/lib/generators/mongoid/devise_generator.rb +55 -0
  100. data/lib/generators/templates/README +35 -0
  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 +263 -0
  109. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  110. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  111. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  112. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +16 -0
  113. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  114. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  115. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +27 -0
  116. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  117. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  118. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +16 -0
  119. data/script/cached-bundle +49 -0
  120. data/script/s3-put +71 -0
  121. data/test/controllers/custom_registrations_controller_test.rb +35 -0
  122. data/test/controllers/custom_strategy_test.rb +62 -0
  123. data/test/controllers/helpers_test.rb +316 -0
  124. data/test/controllers/internal_helpers_test.rb +129 -0
  125. data/test/controllers/load_hooks_controller_test.rb +19 -0
  126. data/test/controllers/passwords_controller_test.rb +31 -0
  127. data/test/controllers/sessions_controller_test.rb +102 -0
  128. data/test/controllers/url_helpers_test.rb +65 -0
  129. data/test/delegator_test.rb +19 -0
  130. data/test/devise_test.rb +107 -0
  131. data/test/failure_app_test.rb +275 -0
  132. data/test/generators/active_record_generator_test.rb +109 -0
  133. data/test/generators/controllers_generator_test.rb +48 -0
  134. data/test/generators/devise_generator_test.rb +39 -0
  135. data/test/generators/install_generator_test.rb +13 -0
  136. data/test/generators/mongoid_generator_test.rb +23 -0
  137. data/test/generators/views_generator_test.rb +96 -0
  138. data/test/helpers/devise_helper_test.rb +49 -0
  139. data/test/integration/authenticatable_test.rb +731 -0
  140. data/test/integration/confirmable_test.rb +324 -0
  141. data/test/integration/database_authenticatable_test.rb +94 -0
  142. data/test/integration/http_authenticatable_test.rb +105 -0
  143. data/test/integration/lockable_test.rb +239 -0
  144. data/test/integration/omniauthable_test.rb +133 -0
  145. data/test/integration/recoverable_test.rb +334 -0
  146. data/test/integration/registerable_test.rb +361 -0
  147. data/test/integration/rememberable_test.rb +176 -0
  148. data/test/integration/timeoutable_test.rb +189 -0
  149. data/test/integration/trackable_test.rb +92 -0
  150. data/test/mailers/confirmation_instructions_test.rb +115 -0
  151. data/test/mailers/reset_password_instructions_test.rb +96 -0
  152. data/test/mailers/unlock_instructions_test.rb +91 -0
  153. data/test/mapping_test.rb +128 -0
  154. data/test/models/authenticatable_test.rb +23 -0
  155. data/test/models/confirmable_test.rb +461 -0
  156. data/test/models/database_authenticatable_test.rb +249 -0
  157. data/test/models/lockable_test.rb +328 -0
  158. data/test/models/omniauthable_test.rb +7 -0
  159. data/test/models/recoverable_test.rb +205 -0
  160. data/test/models/registerable_test.rb +7 -0
  161. data/test/models/rememberable_test.rb +198 -0
  162. data/test/models/serializable_test.rb +49 -0
  163. data/test/models/timeoutable_test.rb +51 -0
  164. data/test/models/trackable_test.rb +41 -0
  165. data/test/models/validatable_test.rb +127 -0
  166. data/test/models_test.rb +144 -0
  167. data/test/omniauth/config_test.rb +57 -0
  168. data/test/omniauth/url_helpers_test.rb +54 -0
  169. data/test/orm/active_record.rb +10 -0
  170. data/test/orm/mongoid.rb +13 -0
  171. data/test/parameter_sanitizer_test.rb +81 -0
  172. data/test/rails_app/Rakefile +6 -0
  173. data/test/rails_app/app/active_record/admin.rb +6 -0
  174. data/test/rails_app/app/active_record/shim.rb +2 -0
  175. data/test/rails_app/app/active_record/user.rb +6 -0
  176. data/test/rails_app/app/active_record/user_on_engine.rb +7 -0
  177. data/test/rails_app/app/active_record/user_on_main_app.rb +7 -0
  178. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  179. data/test/rails_app/app/controllers/admins_controller.rb +11 -0
  180. data/test/rails_app/app/controllers/application_controller.rb +12 -0
  181. data/test/rails_app/app/controllers/application_with_fake_engine.rb +30 -0
  182. data/test/rails_app/app/controllers/custom/registrations_controller.rb +21 -0
  183. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  184. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  185. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  186. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  187. data/test/rails_app/app/controllers/users_controller.rb +31 -0
  188. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  189. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +3 -0
  190. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  191. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +4 -0
  192. data/test/rails_app/app/mongoid/admin.rb +29 -0
  193. data/test/rails_app/app/mongoid/shim.rb +23 -0
  194. data/test/rails_app/app/mongoid/user.rb +39 -0
  195. data/test/rails_app/app/mongoid/user_on_engine.rb +39 -0
  196. data/test/rails_app/app/mongoid/user_on_main_app.rb +39 -0
  197. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  198. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  199. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  200. data/test/rails_app/app/views/home/index.html.erb +1 -0
  201. data/test/rails_app/app/views/home/join.html.erb +1 -0
  202. data/test/rails_app/app/views/home/private.html.erb +1 -0
  203. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  204. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  205. data/test/rails_app/app/views/users/edit_form.html.erb +1 -0
  206. data/test/rails_app/app/views/users/index.html.erb +1 -0
  207. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  208. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  209. data/test/rails_app/bin/bundle +3 -0
  210. data/test/rails_app/bin/rails +4 -0
  211. data/test/rails_app/bin/rake +4 -0
  212. data/test/rails_app/config.ru +4 -0
  213. data/test/rails_app/config/application.rb +40 -0
  214. data/test/rails_app/config/boot.rb +14 -0
  215. data/test/rails_app/config/database.yml +18 -0
  216. data/test/rails_app/config/environment.rb +5 -0
  217. data/test/rails_app/config/environments/development.rb +30 -0
  218. data/test/rails_app/config/environments/production.rb +80 -0
  219. data/test/rails_app/config/environments/test.rb +36 -0
  220. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  221. data/test/rails_app/config/initializers/devise.rb +180 -0
  222. data/test/rails_app/config/initializers/inflections.rb +2 -0
  223. data/test/rails_app/config/initializers/secret_token.rb +8 -0
  224. data/test/rails_app/config/initializers/session_store.rb +1 -0
  225. data/test/rails_app/config/routes.rb +122 -0
  226. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +71 -0
  227. data/test/rails_app/db/schema.rb +55 -0
  228. data/test/rails_app/lib/shared_admin.rb +17 -0
  229. data/test/rails_app/lib/shared_user.rb +29 -0
  230. data/test/rails_app/lib/shared_user_without_omniauth.rb +13 -0
  231. data/test/rails_app/public/404.html +26 -0
  232. data/test/rails_app/public/422.html +26 -0
  233. data/test/rails_app/public/500.html +26 -0
  234. data/test/rails_app/public/favicon.ico +0 -0
  235. data/test/routes_test.rb +264 -0
  236. data/test/support/action_controller/record_identifier.rb +10 -0
  237. data/test/support/assertions.rb +39 -0
  238. data/test/support/helpers.rb +73 -0
  239. data/test/support/integration.rb +92 -0
  240. data/test/support/locale/en.yml +8 -0
  241. data/test/support/mongoid.yml +6 -0
  242. data/test/support/webrat/integrations/rails.rb +24 -0
  243. data/test/test_helper.rb +34 -0
  244. data/test/test_helpers_test.rb +163 -0
  245. data/test/test_models.rb +33 -0
  246. metadata +531 -0
@@ -0,0 +1,91 @@
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 sender from custom mailer defaults with proc' do
53
+ Devise.mailer = 'Users::FromProcMailer'
54
+ assert_equal ['custom@example.com'], mail.from
55
+ end
56
+
57
+ test 'custom mailer renders parent mailer template' do
58
+ Devise.mailer = 'Users::Mailer'
59
+ assert_present mail.body.encoded
60
+ end
61
+
62
+ test 'setup reply to as copy from sender' do
63
+ assert_equal ['test@example.com'], mail.reply_to
64
+ end
65
+
66
+ test 'setup subject from I18n' do
67
+ store_translations :en, devise: { mailer: { unlock_instructions: { subject: 'Yo unlock instructions' } } } do
68
+ assert_equal 'Yo unlock instructions', mail.subject
69
+ end
70
+ end
71
+
72
+ test 'subject namespaced by model' do
73
+ store_translations :en, devise: { mailer: { unlock_instructions: { user_subject: 'User Unlock Instructions' } } } do
74
+ assert_equal 'User Unlock Instructions', mail.subject
75
+ end
76
+ end
77
+
78
+ test 'body should have user info' do
79
+ assert_match user.email, mail.body.encoded
80
+ end
81
+
82
+ test 'body should have link to unlock the account' do
83
+ host, port = ActionMailer::Base.default_url_options.values_at :host, :port
84
+
85
+ if mail.body.encoded =~ %r{<a href=\"http://#{host}:#{port}/users/unlock\?unlock_token=([^"]+)">}
86
+ assert_equal Devise.token_generator.digest(user.class, :unlock_token, $1), user.unlock_token
87
+ else
88
+ flunk "expected unlock url regex to match"
89
+ end
90
+ end
91
+ 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, :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], 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")
66
+ assert_equal :user, Devise::Mapping.find_scope!(User.new)
67
+ end
68
+
69
+ test 'find scope works with single table inheritance' do
70
+ assert_equal :user, Devise::Mapping.find_scope!(Class.new(User))
71
+ assert_equal :user, Devise::Mapping.find_scope!(Class.new(User).new)
72
+ end
73
+
74
+ test 'find scope raises an error if cannot be found' do
75
+ assert_raise RuntimeError do
76
+ Devise::Mapping.find_scope!(String)
77
+ end
78
+ end
79
+
80
+ test 'return default path names' do
81
+ mapping = Devise.mappings[:user]
82
+ assert_equal 'sign_in', mapping.path_names[:sign_in]
83
+ assert_equal 'sign_out', mapping.path_names[:sign_out]
84
+ assert_equal 'password', mapping.path_names[:password]
85
+ assert_equal 'confirmation', mapping.path_names[:confirmation]
86
+ assert_equal 'sign_up', mapping.path_names[:sign_up]
87
+ assert_equal 'unlock', mapping.path_names[:unlock]
88
+ end
89
+
90
+ test 'allow custom path names to be given' do
91
+ mapping = Devise.mappings[:manager]
92
+ assert_equal 'login', mapping.path_names[:sign_in]
93
+ assert_equal 'logout', mapping.path_names[:sign_out]
94
+ assert_equal 'secret', mapping.path_names[:password]
95
+ assert_equal 'verification', mapping.path_names[:confirmation]
96
+ assert_equal 'register', mapping.path_names[:sign_up]
97
+ assert_equal 'unblock', mapping.path_names[:unlock]
98
+ end
99
+
100
+ test 'magic predicates' do
101
+ mapping = Devise.mappings[:user]
102
+ assert mapping.authenticatable?
103
+ assert mapping.confirmable?
104
+ assert mapping.recoverable?
105
+ assert mapping.rememberable?
106
+ assert mapping.registerable?
107
+
108
+ mapping = Devise.mappings[:admin]
109
+ assert mapping.authenticatable?
110
+ assert mapping.recoverable?
111
+ assert mapping.lockable?
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,23 @@
1
+ require 'test_helper'
2
+
3
+ class AuthenticatableTest < ActiveSupport::TestCase
4
+ test 'required_fields should be an empty array' do
5
+ assert_equal Devise::Models::Validatable.required_fields(User), []
6
+ end
7
+
8
+ test 'find_first_by_auth_conditions allows custom filtering parameters' do
9
+ user = User.create!(email: "example@example.com", password: "1234567")
10
+ assert_equal User.find_first_by_auth_conditions({ email: "example@example.com" }), user
11
+ assert_nil User.find_first_by_auth_conditions({ email: "example@example.com" }, id: user.id.to_s.next)
12
+ end
13
+
14
+ if defined?(ActionController::Parameters)
15
+ test 'does not passes an ActionController::Parameters to find_first_by_auth_conditions through find_or_initialize_with_errors' do
16
+ user = create_user(email: 'example@example.com')
17
+ attributes = ActionController::Parameters.new(email: 'example@example.com')
18
+
19
+ User.expects(:find_first_by_auth_conditions).with('email' => 'example@example.com').returns(user)
20
+ User.find_or_initialize_with_errors([:email], attributes)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,461 @@
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 based on the raw token' do
55
+ user = create_user
56
+ raw = user.raw_confirmation_token
57
+ confirmed_user = User.confirm_by_token(raw)
58
+ assert_equal confirmed_user, user
59
+ assert user.reload.confirmed?
60
+ end
61
+
62
+ test 'should return a new record with errors when a invalid token is given' do
63
+ confirmed_user = User.confirm_by_token('invalid_confirmation_token')
64
+ assert_not confirmed_user.persisted?
65
+ assert_equal "is invalid", confirmed_user.errors[:confirmation_token].join
66
+ end
67
+
68
+ test 'should return a new record with errors when a blank token is given' do
69
+ confirmed_user = User.confirm_by_token('')
70
+ assert_not confirmed_user.persisted?
71
+ assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join
72
+ end
73
+
74
+ test 'should generate errors for a user email if user is already confirmed' do
75
+ user = create_user
76
+ user.confirmed_at = Time.now
77
+ user.save
78
+ confirmed_user = User.confirm_by_token(user.raw_confirmation_token)
79
+ assert confirmed_user.confirmed?
80
+ assert_equal "was already confirmed, please try signing in", confirmed_user.errors[:email].join
81
+ end
82
+
83
+ test 'should send confirmation instructions by email' do
84
+ assert_email_sent "mynewuser@example.com" do
85
+ create_user email: "mynewuser@example.com"
86
+ end
87
+ end
88
+
89
+ test 'should not send confirmation when trying to save an invalid user' do
90
+ assert_email_not_sent do
91
+ user = new_user
92
+ user.stubs(:valid?).returns(false)
93
+ user.save
94
+ end
95
+ end
96
+
97
+ test 'should not generate a new token neither send e-mail if skip_confirmation! is invoked' do
98
+ user = new_user
99
+ user.skip_confirmation!
100
+
101
+ assert_email_not_sent do
102
+ user.save!
103
+ assert_nil user.confirmation_token
104
+ assert_not_nil user.confirmed_at
105
+ end
106
+ end
107
+
108
+ test 'should skip confirmation e-mail without confirming if skip_confirmation_notification! is invoked' do
109
+ user = new_user
110
+ user.skip_confirmation_notification!
111
+
112
+ assert_email_not_sent do
113
+ user.save!
114
+ assert !user.confirmed?
115
+ end
116
+ end
117
+
118
+ test 'should not send confirmation when no email is provided' do
119
+ assert_email_not_sent do
120
+ user = new_user
121
+ user.email = ''
122
+ user.save(validate: false)
123
+ end
124
+ end
125
+
126
+ test 'should find a user to send confirmation instructions' do
127
+ user = create_user
128
+ confirmation_user = User.send_confirmation_instructions(email: user.email)
129
+ assert_equal confirmation_user, user
130
+ end
131
+
132
+ test 'should return a new user if no email was found' do
133
+ confirmation_user = User.send_confirmation_instructions(email: "invalid@example.com")
134
+ assert_not confirmation_user.persisted?
135
+ end
136
+
137
+ test 'should add error to new user email if no email was found' do
138
+ confirmation_user = User.send_confirmation_instructions(email: "invalid@example.com")
139
+ assert confirmation_user.errors[:email]
140
+ assert_equal "not found", confirmation_user.errors[:email].join
141
+ end
142
+
143
+ test 'should send email instructions for the user confirm its email' do
144
+ user = create_user
145
+ assert_email_sent user.email do
146
+ User.send_confirmation_instructions(email: user.email)
147
+ end
148
+ end
149
+
150
+ test 'should always have confirmation token when email is sent' do
151
+ user = new_user
152
+ user.instance_eval { def confirmation_required?; false end }
153
+ user.save
154
+ user.send_confirmation_instructions
155
+ assert_not_nil user.reload.confirmation_token
156
+ end
157
+
158
+ test 'should not resend email instructions if the user change their email' do
159
+ user = create_user
160
+ user.email = 'new_test@example.com'
161
+ assert_email_not_sent do
162
+ user.save!
163
+ end
164
+ end
165
+
166
+ test 'should not reset confirmation status or token when updating email' do
167
+ user = create_user
168
+ user.confirm!
169
+ user.email = 'new_test@example.com'
170
+ user.save!
171
+
172
+ user.reload
173
+ assert user.confirmed?
174
+ assert_nil user.confirmation_token
175
+ end
176
+
177
+ test 'should not be able to send instructions if the user is already confirmed' do
178
+ user = create_user
179
+ user.confirm!
180
+ assert_not user.resend_confirmation_instructions
181
+ assert user.confirmed?
182
+ assert_equal 'was already confirmed, please try signing in', user.errors[:email].join
183
+ end
184
+
185
+ test 'confirm time should fallback to devise confirm in default configuration' do
186
+ swap Devise, allow_unconfirmed_access_for: 1.day do
187
+ user = new_user
188
+ user.confirmation_sent_at = 2.days.ago
189
+ assert_not user.active_for_authentication?
190
+
191
+ Devise.allow_unconfirmed_access_for = 3.days
192
+ assert user.active_for_authentication?
193
+ end
194
+ end
195
+
196
+ test 'should be active when confirmation sent at is not overpast' do
197
+ swap Devise, allow_unconfirmed_access_for: 5.days do
198
+ Devise.allow_unconfirmed_access_for = 5.days
199
+ user = create_user
200
+
201
+ user.confirmation_sent_at = 4.days.ago
202
+ assert user.active_for_authentication?
203
+
204
+ user.confirmation_sent_at = 5.days.ago
205
+ assert_not user.active_for_authentication?
206
+ end
207
+ end
208
+
209
+ test 'should be active when already confirmed' do
210
+ user = create_user
211
+ assert_not user.confirmed?
212
+ assert_not user.active_for_authentication?
213
+
214
+ user.confirm!
215
+ assert user.confirmed?
216
+ assert user.active_for_authentication?
217
+ end
218
+
219
+ test 'should not be active when confirm in is zero' do
220
+ Devise.allow_unconfirmed_access_for = 0.days
221
+ user = create_user
222
+ user.confirmation_sent_at = Time.zone.today
223
+ assert_not user.active_for_authentication?
224
+ end
225
+
226
+ test 'should be active when we set allow_unconfirmed_access_for to nil' do
227
+ swap Devise, allow_unconfirmed_access_for: nil do
228
+ user = create_user
229
+ user.confirmation_sent_at = Time.zone.today
230
+ assert user.active_for_authentication?
231
+ end
232
+ end
233
+
234
+ test 'should not be active without confirmation' do
235
+ user = create_user
236
+ user.confirmation_sent_at = nil
237
+ user.save
238
+ assert_not user.reload.active_for_authentication?
239
+ end
240
+
241
+ test 'should be active without confirmation when confirmation is not required' do
242
+ user = create_user
243
+ user.instance_eval { def confirmation_required?; false end }
244
+ user.confirmation_sent_at = nil
245
+ user.save
246
+ assert user.reload.active_for_authentication?
247
+ end
248
+
249
+ test 'should find a user to send email instructions for the user confirm its email by authentication_keys' do
250
+ swap Devise, authentication_keys: [:username, :email] do
251
+ user = create_user
252
+ confirm_user = User.send_confirmation_instructions(email: user.email, username: user.username)
253
+ assert_equal confirm_user, user
254
+ end
255
+ end
256
+
257
+ test 'should require all confirmation_keys' do
258
+ swap Devise, confirmation_keys: [:username, :email] do
259
+ user = create_user
260
+ confirm_user = User.send_confirmation_instructions(email: user.email)
261
+ assert_not confirm_user.persisted?
262
+ assert_equal "can't be blank", confirm_user.errors[:username].join
263
+ end
264
+ end
265
+
266
+ def confirm_user_by_token_with_confirmation_sent_at(confirmation_sent_at)
267
+ user = create_user
268
+ user.update_attribute(:confirmation_sent_at, confirmation_sent_at)
269
+ confirmed_user = User.confirm_by_token(user.raw_confirmation_token)
270
+ assert_equal confirmed_user, user
271
+ user.reload.confirmed?
272
+ end
273
+
274
+ test 'should accept confirmation email token even after 5 years when no expiration is set' do
275
+ assert confirm_user_by_token_with_confirmation_sent_at(5.years.ago)
276
+ end
277
+
278
+ test 'should accept confirmation email token after 2 days when expiration is set to 3 days' do
279
+ swap Devise, confirm_within: 3.days do
280
+ assert confirm_user_by_token_with_confirmation_sent_at(2.days.ago)
281
+ end
282
+ end
283
+
284
+ test 'should not accept confirmation email token after 4 days when expiration is set to 3 days' do
285
+ swap Devise, confirm_within: 3.days do
286
+ assert_not confirm_user_by_token_with_confirmation_sent_at(4.days.ago)
287
+ end
288
+ end
289
+
290
+ test 'always generate a new token on resend' do
291
+ user = create_user
292
+ old = user.confirmation_token
293
+ user = User.find(user.id)
294
+ user.resend_confirmation_instructions
295
+ assert_not_equal user.confirmation_token, old
296
+ end
297
+
298
+ test 'should call after_confirmation if confirmed' do
299
+ user = create_user
300
+ user.define_singleton_method :after_confirmation do
301
+ self.username = self.username.to_s + 'updated'
302
+ end
303
+ old = user.username
304
+ assert user.confirm!
305
+ assert_not_equal user.username, old
306
+ end
307
+
308
+ test 'should not call after_confirmation if not confirmed' do
309
+ user = create_user
310
+ assert user.confirm!
311
+ user.define_singleton_method :after_confirmation do
312
+ self.username = self.username.to_s + 'updated'
313
+ end
314
+ old = user.username
315
+ assert_not user.confirm!
316
+ assert_equal user.username, old
317
+ end
318
+
319
+ test 'should always perform validations upon confirm when ensure valid true' do
320
+ admin = create_admin
321
+ admin.stubs(:valid?).returns(false)
322
+ assert_not admin.confirm!(ensure_valid: true)
323
+ end
324
+ end
325
+
326
+ class ReconfirmableTest < ActiveSupport::TestCase
327
+ test 'should not worry about validations on confirm even with reconfirmable' do
328
+ admin = create_admin
329
+ admin.reset_password_token = "a"
330
+ assert admin.confirm!
331
+ end
332
+
333
+ test 'should generate confirmation token after changing email' do
334
+ admin = create_admin
335
+ assert admin.confirm!
336
+ assert_nil admin.confirmation_token
337
+ assert admin.update_attributes(email: 'new_test@example.com')
338
+ assert_not_nil admin.confirmation_token
339
+ end
340
+
341
+ test 'should not generate confirmation token if skipping reconfirmation after changing email' do
342
+ admin = create_admin
343
+ assert admin.confirm!
344
+ admin.skip_reconfirmation!
345
+ assert admin.update_attributes(email: 'new_test@example.com')
346
+ assert_nil admin.confirmation_token
347
+ end
348
+
349
+ test 'should skip sending reconfirmation email when email is changed and skip_confirmation_notification! is invoked' do
350
+ admin = create_admin
351
+ admin.skip_confirmation_notification!
352
+
353
+ assert_email_not_sent do
354
+ admin.update_attributes(email: 'new_test@example.com')
355
+ end
356
+ end
357
+
358
+ test 'should regenerate confirmation token after changing email' do
359
+ admin = create_admin
360
+ assert admin.confirm!
361
+ assert admin.update_attributes(email: 'old_test@example.com')
362
+ token = admin.confirmation_token
363
+ assert admin.update_attributes(email: 'new_test@example.com')
364
+ assert_not_equal token, admin.confirmation_token
365
+ end
366
+
367
+ test 'should send confirmation instructions by email after changing email' do
368
+ admin = create_admin
369
+ assert admin.confirm!
370
+ assert_email_sent "new_test@example.com" do
371
+ assert admin.update_attributes(email: 'new_test@example.com')
372
+ end
373
+ assert_match "new_test@example.com", ActionMailer::Base.deliveries.last.body.encoded
374
+ end
375
+
376
+ test 'should not send confirmation by email after changing password' do
377
+ admin = create_admin
378
+ assert admin.confirm!
379
+ assert_email_not_sent do
380
+ assert admin.update_attributes(password: 'newpass', password_confirmation: 'newpass')
381
+ end
382
+ end
383
+
384
+ test 'should not send confirmation by email after changing to a blank email' do
385
+ admin = create_admin
386
+ assert admin.confirm!
387
+ assert_email_not_sent do
388
+ admin.email = ''
389
+ admin.save(validate: false)
390
+ end
391
+ end
392
+
393
+ test 'should stay confirmed when email is changed' do
394
+ admin = create_admin
395
+ assert admin.confirm!
396
+ assert admin.update_attributes(email: 'new_test@example.com')
397
+ assert admin.confirmed?
398
+ end
399
+
400
+ test 'should update email only when it is confirmed' do
401
+ admin = create_admin
402
+ assert admin.confirm!
403
+ assert admin.update_attributes(email: 'new_test@example.com')
404
+ assert_not_equal 'new_test@example.com', admin.email
405
+ assert admin.confirm!
406
+ assert_equal 'new_test@example.com', admin.email
407
+ end
408
+
409
+ test 'should not allow admin to get past confirmation email by resubmitting their new address' do
410
+ admin = create_admin
411
+ assert admin.confirm!
412
+ assert admin.update_attributes(email: 'new_test@example.com')
413
+ assert_not_equal 'new_test@example.com', admin.email
414
+ assert admin.update_attributes(email: 'new_test@example.com')
415
+ assert_not_equal 'new_test@example.com', admin.email
416
+ end
417
+
418
+ test 'should find a admin by send confirmation instructions with unconfirmed_email' do
419
+ admin = create_admin
420
+ assert admin.confirm!
421
+ assert admin.update_attributes(email: 'new_test@example.com')
422
+ confirmation_admin = Admin.send_confirmation_instructions(email: admin.unconfirmed_email)
423
+ assert_equal confirmation_admin, admin
424
+ end
425
+
426
+ test 'should return a new admin if no email or unconfirmed_email was found' do
427
+ confirmation_admin = Admin.send_confirmation_instructions(email: "invalid@email.com")
428
+ assert_not confirmation_admin.persisted?
429
+ end
430
+
431
+ test 'should add error to new admin email if no email or unconfirmed_email was found' do
432
+ confirmation_admin = Admin.send_confirmation_instructions(email: "invalid@email.com")
433
+ assert confirmation_admin.errors[:email]
434
+ assert_equal "not found", confirmation_admin.errors[:email].join
435
+ end
436
+
437
+ test 'should find admin with email in unconfirmed_emails' do
438
+ admin = create_admin
439
+ admin.unconfirmed_email = "new_test@email.com"
440
+ assert admin.save
441
+ admin = Admin.find_by_unconfirmed_email_with_errors(email: "new_test@email.com")
442
+ assert admin.persisted?
443
+ end
444
+
445
+ test 'required_fields should contain the fields that Devise uses' do
446
+ assert_same_content Devise::Models::Confirmable.required_fields(User), [
447
+ :confirmation_sent_at,
448
+ :confirmation_token,
449
+ :confirmed_at
450
+ ]
451
+ end
452
+
453
+ test 'required_fields should also contain unconfirmable when reconfirmable_email is true' do
454
+ assert_same_content Devise::Models::Confirmable.required_fields(Admin), [
455
+ :confirmation_sent_at,
456
+ :confirmation_token,
457
+ :confirmed_at,
458
+ :unconfirmed_email
459
+ ]
460
+ end
461
+ end