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,51 @@
1
+ require 'test_helper'
2
+
3
+ class TimeoutableTest < ActiveSupport::TestCase
4
+
5
+ test 'should be expired' do
6
+ assert new_user.timedout?(31.minutes.ago)
7
+ end
8
+
9
+ test 'should not be expired' do
10
+ assert_not new_user.timedout?(29.minutes.ago)
11
+ end
12
+
13
+ test 'should not be expired when params is nil' do
14
+ assert_not new_user.timedout?(nil)
15
+ end
16
+
17
+ test 'should use timeout_in method' do
18
+ user = new_user
19
+ user.instance_eval { def timeout_in; 10.minutes end }
20
+
21
+ assert user.timedout?(12.minutes.ago)
22
+ assert_not user.timedout?(8.minutes.ago)
23
+ end
24
+
25
+ test 'should not be expired when timeout_in method returns nil' do
26
+ user = new_user
27
+ user.instance_eval { def timeout_in; nil end }
28
+ assert_not user.timedout?(10.hours.ago)
29
+ end
30
+
31
+ test 'fallback to Devise config option' do
32
+ swap Devise, timeout_in: 1.minute do
33
+ user = new_user
34
+ assert user.timedout?(2.minutes.ago)
35
+ assert_not user.timedout?(30.seconds.ago)
36
+
37
+ Devise.timeout_in = 5.minutes
38
+ assert_not user.timedout?(2.minutes.ago)
39
+ assert user.timedout?(6.minutes.ago)
40
+ end
41
+ end
42
+
43
+ test 'required_fields should contain the fields that Devise uses' do
44
+ assert_same_content Devise::Models::Timeoutable.required_fields(User), []
45
+ end
46
+
47
+ test 'should not raise error if remember_created_at is not empty and rememberable is disabled' do
48
+ user = create_admin(remember_created_at: Time.current)
49
+ assert user.timedout?(31.minutes.ago)
50
+ end
51
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ class TrackableTest < ActiveSupport::TestCase
4
+ test 'required_fields should contain the fields that Devise uses' do
5
+ assert_same_content Devise::Models::Trackable.required_fields(User), [
6
+ :current_sign_in_at,
7
+ :current_sign_in_ip,
8
+ :last_sign_in_at,
9
+ :last_sign_in_ip,
10
+ :sign_in_count
11
+ ]
12
+ end
13
+
14
+ test 'update_tracked_fields should only set attributes but not save the record' do
15
+ user = create_user
16
+ request = mock
17
+ request.stubs(:remote_ip).returns("127.0.0.1")
18
+
19
+ assert_nil user.current_sign_in_ip
20
+ assert_nil user.last_sign_in_ip
21
+ assert_nil user.current_sign_in_at
22
+ assert_nil user.last_sign_in_at
23
+ assert_equal 0, user.sign_in_count
24
+
25
+ user.update_tracked_fields(request)
26
+
27
+ assert_equal "127.0.0.1", user.current_sign_in_ip
28
+ assert_equal "127.0.0.1", user.last_sign_in_ip
29
+ assert_not_nil user.current_sign_in_at
30
+ assert_not_nil user.last_sign_in_at
31
+ assert_equal 1, user.sign_in_count
32
+
33
+ user.reload
34
+
35
+ assert_nil user.current_sign_in_ip
36
+ assert_nil user.last_sign_in_ip
37
+ assert_nil user.current_sign_in_at
38
+ assert_nil user.last_sign_in_at
39
+ assert_equal 0, user.sign_in_count
40
+ end
41
+ end
@@ -0,0 +1,127 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+
4
+ class ValidatableTest < ActiveSupport::TestCase
5
+ test 'should require email to be set' do
6
+ user = new_user(email: nil)
7
+ assert user.invalid?
8
+ assert user.errors[:email]
9
+ assert_equal 'can\'t be blank', user.errors[:email].join
10
+ end
11
+
12
+ test 'should require uniqueness of email if email has changed, allowing blank' do
13
+ existing_user = create_user
14
+
15
+ user = new_user(email: '')
16
+ assert user.invalid?
17
+ assert_no_match(/taken/, user.errors[:email].join)
18
+
19
+ user.email = existing_user.email
20
+ assert user.invalid?
21
+ assert_match(/taken/, user.errors[:email].join)
22
+
23
+ user.save(validate: false)
24
+ assert user.valid?
25
+ end
26
+
27
+ test 'should require correct email format if email has changed, allowing blank' do
28
+ user = new_user(email: '')
29
+ assert user.invalid?
30
+ assert_not_equal 'is invalid', user.errors[:email].join
31
+
32
+ %w{invalid_email_format 123 $$$ () ☃ bla@bla.}.each do |email|
33
+ user.email = email
34
+ assert user.invalid?, 'should be invalid with email ' << email
35
+ assert_equal 'is invalid', user.errors[:email].join
36
+ end
37
+
38
+ user.save(validate: false)
39
+ assert user.valid?
40
+ end
41
+
42
+ test 'should accept valid emails' do
43
+ %w(a.b.c@example.com test_mail@gmail.com any@any.net email@test.br 123@mail.test 1☃3@mail.test).each do |email|
44
+ user = new_user(email: email)
45
+ assert user.valid?, 'should be valid with email ' << email
46
+ assert_blank user.errors[:email]
47
+ end
48
+ end
49
+
50
+ test 'should require password to be set when creating a new record' do
51
+ user = new_user(password: '', password_confirmation: '')
52
+ assert user.invalid?
53
+ assert_equal 'can\'t be blank', user.errors[:password].join
54
+ end
55
+
56
+ test 'should require confirmation to be set when creating a new record' do
57
+ user = new_user(password: 'new_password', password_confirmation: 'blabla')
58
+ assert user.invalid?
59
+
60
+ if Devise.rails4?
61
+ assert_equal 'doesn\'t match Password', user.errors[:password_confirmation].join
62
+ else
63
+ assert_equal 'doesn\'t match confirmation', user.errors[:password].join
64
+ end
65
+ end
66
+
67
+ test 'should require password when updating/resetting password' do
68
+ user = create_user
69
+
70
+ user.password = ''
71
+ user.password_confirmation = ''
72
+
73
+ assert user.invalid?
74
+ assert_equal 'can\'t be blank', user.errors[:password].join
75
+ end
76
+
77
+ test 'should require confirmation when updating/resetting password' do
78
+ user = create_user
79
+ user.password_confirmation = 'another_password'
80
+ assert user.invalid?
81
+
82
+ if Devise.rails4?
83
+ assert_equal 'doesn\'t match Password', user.errors[:password_confirmation].join
84
+ else
85
+ assert_equal 'doesn\'t match confirmation', user.errors[:password].join
86
+ end
87
+ end
88
+
89
+ test 'should require a password with minimum of 7 characters' do
90
+ user = new_user(password: '12345', password_confirmation: '12345')
91
+ assert user.invalid?
92
+ assert_equal 'is too short (minimum is 7 characters)', user.errors[:password].join
93
+ end
94
+
95
+ test 'should require a password with maximum of 72 characters long' do
96
+ user = new_user(password: 'x'*73, password_confirmation: 'x'*73)
97
+ assert user.invalid?
98
+ assert_equal 'is too long (maximum is 72 characters)', user.errors[:password].join
99
+ end
100
+
101
+ test 'should not require password length when it\'s not changed' do
102
+ user = create_user.reload
103
+ user.password = user.password_confirmation = nil
104
+ assert user.valid?
105
+
106
+ user.password_confirmation = 'confirmation'
107
+ assert user.invalid?
108
+ assert_not (user.errors[:password].join =~ /is too long/)
109
+ end
110
+
111
+ test 'should complain about length even if password is not required' do
112
+ user = new_user(password: 'x'*73, password_confirmation: 'x'*73)
113
+ user.stubs(:password_required?).returns(false)
114
+ assert user.invalid?
115
+ assert_equal 'is too long (maximum is 72 characters)', user.errors[:password].join
116
+ end
117
+
118
+ test 'should not be included in objects with invalid API' do
119
+ assert_raise RuntimeError do
120
+ Class.new.send :include, Devise::Models::Validatable
121
+ end
122
+ end
123
+
124
+ test 'required_fields should be an empty array' do
125
+ assert_equal Devise::Models::Validatable.required_fields(User), []
126
+ end
127
+ end
@@ -0,0 +1,144 @@
1
+ require 'test_helper'
2
+ require 'test_models'
3
+
4
+ class ActiveRecordTest < ActiveSupport::TestCase
5
+ def include_module?(klass, mod)
6
+ klass.devise_modules.include?(mod) &&
7
+ klass.included_modules.include?(Devise::Models::const_get(mod.to_s.classify))
8
+ end
9
+
10
+ def assert_include_modules(klass, *modules)
11
+ modules.each do |mod|
12
+ assert include_module?(klass, mod)
13
+ end
14
+
15
+ (Devise::ALL - modules).each do |mod|
16
+ assert_not include_module?(klass, mod)
17
+ end
18
+ end
19
+
20
+ test 'can cherry pick modules' do
21
+ assert_include_modules Admin, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :confirmable
22
+ end
23
+
24
+ test 'validations options are not applied too late' do
25
+ validators = WithValidation.validators_on :password
26
+ length = validators.find { |v| v.kind == :length }
27
+ assert_equal 2, length.options[:minimum]
28
+ assert_equal 6, length.options[:maximum]
29
+ end
30
+
31
+ test 'validations are applied just once' do
32
+ validators = Several.validators_on :password
33
+ assert_equal 1, validators.select{ |v| v.kind == :length }.length
34
+ end
35
+
36
+ test 'chosen modules are inheritable' do
37
+ assert_include_modules Inheritable, :database_authenticatable, :registerable, :timeoutable, :recoverable, :lockable, :confirmable
38
+ end
39
+
40
+ test 'order of module inclusion' do
41
+ correct_module_order = [:database_authenticatable, :recoverable, :registerable, :confirmable, :lockable, :timeoutable]
42
+ incorrect_module_order = [:database_authenticatable, :timeoutable, :registerable, :recoverable, :lockable, :confirmable]
43
+
44
+ assert_include_modules Admin, *incorrect_module_order
45
+
46
+ # get module constants from symbol list
47
+ module_constants = correct_module_order.collect { |mod| Devise::Models::const_get(mod.to_s.classify) }
48
+
49
+ # confirm that they adhere to the order in ALL
50
+ # get included modules, filter out the noise, and reverse the order
51
+ assert_equal module_constants, (Admin.included_modules & module_constants).reverse
52
+ end
53
+
54
+ test 'raise error on invalid module' do
55
+ assert_raise NameError do
56
+ # Mix valid an invalid modules.
57
+ Configurable.class_eval { devise :database_authenticatable, :doesnotexit }
58
+ end
59
+ end
60
+
61
+ test 'set a default value for stretches' do
62
+ assert_equal 15, Configurable.stretches
63
+ end
64
+
65
+ test 'set a default value for pepper' do
66
+ assert_equal 'abcdef', Configurable.pepper
67
+ end
68
+
69
+ test 'set a default value for allow_unconfirmed_access_for' do
70
+ assert_equal 5.days, Configurable.allow_unconfirmed_access_for
71
+ end
72
+
73
+ test 'set a default value for remember_for' do
74
+ assert_equal 7.days, Configurable.remember_for
75
+ end
76
+
77
+ test 'set a default value for timeout_in' do
78
+ assert_equal 15.minutes, Configurable.timeout_in
79
+ end
80
+
81
+ test 'set a default value for unlock_in' do
82
+ assert_equal 10.days, Configurable.unlock_in
83
+ end
84
+
85
+ test 'set null fields on migrations' do
86
+ # Ignore email sending since no email exists.
87
+ klass = Class.new(Admin) do
88
+ def send_devise_notification(*); end
89
+ end
90
+
91
+ klass.create!
92
+ end
93
+ end
94
+
95
+ class CheckFieldsTest < ActiveSupport::TestCase
96
+ test 'checks if the class respond_to the required fields' do
97
+ Player = Class.new do
98
+ extend Devise::Models
99
+
100
+ def self.before_validation(instance)
101
+ end
102
+
103
+ devise :database_authenticatable
104
+
105
+ attr_accessor :encrypted_password, :email
106
+ end
107
+
108
+ assert_nothing_raised Devise::Models::MissingAttribute do
109
+ Devise::Models.check_fields!(Player)
110
+ end
111
+ end
112
+
113
+ test 'raises Devise::Models::MissingAtrribute and shows the missing attribute if the class doesn\'t respond_to one of the attributes' do
114
+ Clown = Class.new do
115
+ extend Devise::Models
116
+
117
+ def self.before_validation(instance)
118
+ end
119
+
120
+ devise :database_authenticatable
121
+
122
+ attr_accessor :encrypted_password
123
+ end
124
+
125
+ assert_raise_with_message Devise::Models::MissingAttribute, "The following attribute(s) is (are) missing on your model: email" do
126
+ Devise::Models.check_fields!(Clown)
127
+ end
128
+ end
129
+
130
+ test 'raises Devise::Models::MissingAtrribute with all the missing attributes if there is more than one' do
131
+ Magician = Class.new do
132
+ extend Devise::Models
133
+
134
+ def self.before_validation(instance)
135
+ end
136
+
137
+ devise :database_authenticatable
138
+ end
139
+
140
+ assert_raise_with_message Devise::Models::MissingAttribute, "The following attribute(s) is (are) missing on your model: encrypted_password, email" do
141
+ Devise::Models.check_fields!(Magician)
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ class OmniAuthConfigTest < ActiveSupport::TestCase
4
+ class MyStrategy
5
+ include OmniAuth::Strategy
6
+ end
7
+
8
+ test 'strategy_name returns provider if no options given' do
9
+ config = Devise::OmniAuth::Config.new :facebook, [{}]
10
+ assert_equal :facebook, config.strategy_name
11
+ end
12
+
13
+ test 'strategy_name returns provider if no name option are given' do
14
+ config = Devise::OmniAuth::Config.new :facebook, [{ other: :option }]
15
+ assert_equal :facebook, config.strategy_name
16
+ end
17
+
18
+ test 'returns name option when have a name' do
19
+ config = Devise::OmniAuth::Config.new :facebook, [{ name: :github }]
20
+ assert_equal :github, config.strategy_name
21
+ end
22
+
23
+ test "finds contrib strategies" do
24
+ config = Devise::OmniAuth::Config.new :facebook, [{}]
25
+ assert_equal OmniAuth::Strategies::Facebook, config.strategy_class
26
+ end
27
+
28
+ test "finds the strategy in OmniAuth's list by name" do
29
+ NamedTestStrategy = Class.new
30
+ NamedTestStrategy.send :include, OmniAuth::Strategy
31
+ NamedTestStrategy.option :name, :the_one
32
+
33
+ config = Devise::OmniAuth::Config.new :the_one, [{}]
34
+ assert_equal NamedTestStrategy, config.strategy_class
35
+ end
36
+
37
+ test "finds the strategy in OmniAuth's list by class name" do
38
+ UnNamedTestStrategy = Class.new
39
+ UnNamedTestStrategy.send :include, OmniAuth::Strategy
40
+
41
+ config = Devise::OmniAuth::Config.new :un_named_test_strategy, [{}]
42
+ assert_equal UnNamedTestStrategy, config.strategy_class
43
+ end
44
+
45
+ test 'raises an error if strategy cannot be found' do
46
+ config = Devise::OmniAuth::Config.new :my_other_strategy, [{}]
47
+ assert_raise Devise::OmniAuth::StrategyNotFound do
48
+ config.strategy_class
49
+ end
50
+ end
51
+
52
+ test 'allows the user to define a custom require path' do
53
+ config = Devise::OmniAuth::Config.new :my_strategy, [{strategy_class: MyStrategy}]
54
+ config_class = config.strategy_class
55
+ assert_equal MyStrategy, config_class
56
+ end
57
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class OmniAuthRoutesTest < ActionController::TestCase
4
+ ExpectedUrlGeneratiorError = Devise.rails4? ?
5
+ ActionController::UrlGenerationError : ActionController::RoutingError
6
+
7
+ tests ApplicationController
8
+
9
+ def assert_path(action, provider, with_param=true)
10
+ # Resource param
11
+ assert_equal @controller.send(action, :user, provider),
12
+ @controller.send("user_#{action}", provider)
13
+
14
+ # With an object
15
+ assert_equal @controller.send(action, User.new, provider),
16
+ @controller.send("user_#{action}", provider)
17
+
18
+ if with_param
19
+ # Default url params
20
+ assert_equal @controller.send(action, :user, provider, param: 123),
21
+ @controller.send("user_#{action}", provider, param: 123)
22
+ end
23
+ end
24
+
25
+ test 'should alias omniauth_callback to mapped user auth_callback' do
26
+ assert_path :omniauth_callback_path, :facebook
27
+ end
28
+
29
+ test 'should alias omniauth_authorize to mapped user auth_authorize' do
30
+ assert_path :omniauth_authorize_path, :facebook, false
31
+ end
32
+
33
+ test 'should generate authorization path' do
34
+ assert_match "/users/auth/facebook", @controller.omniauth_authorize_path(:user, :facebook)
35
+
36
+ assert_raise ExpectedUrlGeneratiorError do
37
+ @controller.omniauth_authorize_path(:user, :github)
38
+ end
39
+ end
40
+
41
+ test 'should generate authorization path for named open_id omniauth' do
42
+ assert_match "/users/auth/google", @controller.omniauth_authorize_path(:user, :google)
43
+ end
44
+
45
+ test 'should generate authorization path with params' do
46
+ assert_match "/users/auth/openid?openid_url=http%3A%2F%2Fyahoo.com",
47
+ @controller.omniauth_authorize_path(:user, :openid, openid_url: "http://yahoo.com")
48
+ end
49
+
50
+ test 'should not add a "?" if no param was sent' do
51
+ assert_equal "/users/auth/openid",
52
+ @controller.omniauth_authorize_path(:user, :openid)
53
+ end
54
+ end