devise_token_auth_skycocker_fork 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +13 -0
  3. data/README.md +97 -0
  4. data/Rakefile +42 -0
  5. data/app/controllers/devise_token_auth/application_controller.rb +79 -0
  6. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +44 -0
  7. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +178 -0
  8. data/app/controllers/devise_token_auth/confirmations_controller.rb +39 -0
  9. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +239 -0
  10. data/app/controllers/devise_token_auth/passwords_controller.rb +185 -0
  11. data/app/controllers/devise_token_auth/registrations_controller.rb +198 -0
  12. data/app/controllers/devise_token_auth/sessions_controller.rb +131 -0
  13. data/app/controllers/devise_token_auth/token_validations_controller.rb +31 -0
  14. data/app/controllers/devise_token_auth/unlocks_controller.rb +89 -0
  15. data/app/models/devise_token_auth/concerns/active_record_support.rb +34 -0
  16. data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
  17. data/app/models/devise_token_auth/concerns/user.rb +262 -0
  18. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +28 -0
  19. data/app/validators/devise_token_auth/email_validator.rb +23 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/app/views/devise_token_auth/omniauth_external_window.html.erb +38 -0
  24. data/config/locales/da-DK.yml +50 -0
  25. data/config/locales/de.yml +49 -0
  26. data/config/locales/en.yml +50 -0
  27. data/config/locales/es.yml +49 -0
  28. data/config/locales/fr.yml +49 -0
  29. data/config/locales/it.yml +46 -0
  30. data/config/locales/ja.yml +46 -0
  31. data/config/locales/nl.yml +30 -0
  32. data/config/locales/pl.yml +48 -0
  33. data/config/locales/pt-BR.yml +46 -0
  34. data/config/locales/pt.yml +48 -0
  35. data/config/locales/ro.yml +46 -0
  36. data/config/locales/ru.yml +50 -0
  37. data/config/locales/sq.yml +46 -0
  38. data/config/locales/sv.yml +50 -0
  39. data/config/locales/uk.yml +59 -0
  40. data/config/locales/vi.yml +50 -0
  41. data/config/locales/zh-CN.yml +46 -0
  42. data/config/locales/zh-HK.yml +48 -0
  43. data/config/locales/zh-TW.yml +48 -0
  44. data/lib/devise_token_auth.rb +13 -0
  45. data/lib/devise_token_auth/blacklist.rb +2 -0
  46. data/lib/devise_token_auth/controllers/helpers.rb +161 -0
  47. data/lib/devise_token_auth/controllers/url_helpers.rb +10 -0
  48. data/lib/devise_token_auth/engine.rb +90 -0
  49. data/lib/devise_token_auth/errors.rb +8 -0
  50. data/lib/devise_token_auth/rails/routes.rb +116 -0
  51. data/lib/devise_token_auth/url.rb +41 -0
  52. data/lib/devise_token_auth/version.rb +5 -0
  53. data/lib/generators/devise_token_auth/USAGE +31 -0
  54. data/lib/generators/devise_token_auth/install_generator.rb +91 -0
  55. data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
  56. data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
  57. data/lib/generators/devise_token_auth/install_views_generator.rb +18 -0
  58. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +50 -0
  59. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +56 -0
  60. data/lib/generators/devise_token_auth/templates/user.rb.erb +9 -0
  61. data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +63 -0
  62. data/lib/tasks/devise_token_auth_tasks.rake +6 -0
  63. data/test/controllers/custom/custom_confirmations_controller_test.rb +25 -0
  64. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +33 -0
  65. data/test/controllers/custom/custom_passwords_controller_test.rb +79 -0
  66. data/test/controllers/custom/custom_registrations_controller_test.rb +63 -0
  67. data/test/controllers/custom/custom_sessions_controller_test.rb +39 -0
  68. data/test/controllers/custom/custom_token_validations_controller_test.rb +42 -0
  69. data/test/controllers/demo_group_controller_test.rb +151 -0
  70. data/test/controllers/demo_mang_controller_test.rb +284 -0
  71. data/test/controllers/demo_user_controller_test.rb +629 -0
  72. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +127 -0
  73. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +376 -0
  74. data/test/controllers/devise_token_auth/passwords_controller_test.rb +639 -0
  75. data/test/controllers/devise_token_auth/registrations_controller_test.rb +880 -0
  76. data/test/controllers/devise_token_auth/sessions_controller_test.rb +541 -0
  77. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +102 -0
  78. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +196 -0
  79. data/test/controllers/overrides/confirmations_controller_test.rb +47 -0
  80. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +53 -0
  81. data/test/controllers/overrides/passwords_controller_test.rb +64 -0
  82. data/test/controllers/overrides/registrations_controller_test.rb +46 -0
  83. data/test/controllers/overrides/sessions_controller_test.rb +35 -0
  84. data/test/controllers/overrides/token_validations_controller_test.rb +43 -0
  85. data/test/dummy/README.rdoc +28 -0
  86. data/test/dummy/app/active_record/lockable_user.rb +7 -0
  87. data/test/dummy/app/active_record/mang.rb +5 -0
  88. data/test/dummy/app/active_record/only_email_user.rb +7 -0
  89. data/test/dummy/app/active_record/scoped_user.rb +9 -0
  90. data/test/dummy/app/active_record/unconfirmable_user.rb +9 -0
  91. data/test/dummy/app/active_record/unregisterable_user.rb +9 -0
  92. data/test/dummy/app/active_record/user.rb +6 -0
  93. data/test/dummy/app/controllers/application_controller.rb +18 -0
  94. data/test/dummy/app/controllers/auth_origin_controller.rb +7 -0
  95. data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
  96. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +13 -0
  97. data/test/dummy/app/controllers/custom/passwords_controller.rb +39 -0
  98. data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
  99. data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
  100. data/test/dummy/app/controllers/custom/token_validations_controller.rb +19 -0
  101. data/test/dummy/app/controllers/demo_group_controller.rb +15 -0
  102. data/test/dummy/app/controllers/demo_mang_controller.rb +14 -0
  103. data/test/dummy/app/controllers/demo_user_controller.rb +27 -0
  104. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +28 -0
  105. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +16 -0
  106. data/test/dummy/app/controllers/overrides/passwords_controller.rb +35 -0
  107. data/test/dummy/app/controllers/overrides/registrations_controller.rb +29 -0
  108. data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
  109. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
  110. data/test/dummy/app/helpers/application_helper.rb +1058 -0
  111. data/test/dummy/app/models/concerns/favorite_color.rb +19 -0
  112. data/test/dummy/app/mongoid/lockable_user.rb +38 -0
  113. data/test/dummy/app/mongoid/mang.rb +53 -0
  114. data/test/dummy/app/mongoid/only_email_user.rb +33 -0
  115. data/test/dummy/app/mongoid/scoped_user.rb +57 -0
  116. data/test/dummy/app/mongoid/unconfirmable_user.rb +51 -0
  117. data/test/dummy/app/mongoid/unregisterable_user.rb +54 -0
  118. data/test/dummy/app/mongoid/user.rb +56 -0
  119. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  120. data/test/dummy/config.ru +18 -0
  121. data/test/dummy/config/application.rb +48 -0
  122. data/test/dummy/config/application.yml.bk +0 -0
  123. data/test/dummy/config/boot.rb +11 -0
  124. data/test/dummy/config/environment.rb +7 -0
  125. data/test/dummy/config/environments/development.rb +46 -0
  126. data/test/dummy/config/environments/production.rb +84 -0
  127. data/test/dummy/config/environments/test.rb +50 -0
  128. data/test/dummy/config/initializers/assets.rb +10 -0
  129. data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
  130. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  131. data/test/dummy/config/initializers/devise.rb +17 -0
  132. data/test/dummy/config/initializers/devise_token_auth.rb +24 -0
  133. data/test/dummy/config/initializers/figaro.rb +3 -0
  134. data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  135. data/test/dummy/config/initializers/inflections.rb +18 -0
  136. data/test/dummy/config/initializers/mime_types.rb +6 -0
  137. data/test/dummy/config/initializers/omniauth.rb +10 -0
  138. data/test/dummy/config/initializers/session_store.rb +5 -0
  139. data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
  140. data/test/dummy/config/routes.rb +55 -0
  141. data/test/dummy/config/spring.rb +3 -0
  142. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +65 -0
  143. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +64 -0
  144. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +8 -0
  145. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +7 -0
  146. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +62 -0
  147. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +63 -0
  148. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +63 -0
  149. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +63 -0
  150. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +63 -0
  151. data/test/dummy/db/schema.rb +200 -0
  152. data/test/dummy/lib/migration_database_helper.rb +43 -0
  153. data/test/dummy/tmp/generators/app/models/user.rb +9 -0
  154. data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +50 -0
  155. data/test/dummy/tmp/generators/db/migrate/20181030122248_devise_token_auth_create_users.rb +56 -0
  156. data/test/factories/users.rb +40 -0
  157. data/test/lib/devise_token_auth/blacklist_test.rb +11 -0
  158. data/test/lib/devise_token_auth/url_test.rb +26 -0
  159. data/test/lib/generators/devise_token_auth/install_generator_test.rb +217 -0
  160. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +222 -0
  161. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +25 -0
  162. data/test/models/concerns/mongoid_support_test.rb +31 -0
  163. data/test/models/only_email_user_test.rb +37 -0
  164. data/test/models/user_test.rb +140 -0
  165. data/test/support/controllers/routes.rb +43 -0
  166. data/test/test_helper.rb +103 -0
  167. metadata +443 -0
@@ -0,0 +1,222 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'fileutils'
5
+ require 'generators/devise_token_auth/install_generator' if DEVISE_TOKEN_AUTH_ORM == :active_record
6
+ require 'generators/devise_token_auth/install_mongoid_generator' if DEVISE_TOKEN_AUTH_ORM == :mongoid
7
+
8
+ module DeviseTokenAuth
9
+ class InstallGeneratorTest < Rails::Generators::TestCase
10
+ tests InstallGenerator if DEVISE_TOKEN_AUTH_ORM == :active_record
11
+ tests InstallMongoidGenerator if DEVISE_TOKEN_AUTH_ORM == :mongoid
12
+ destination Rails.root.join('tmp/generators')
13
+
14
+ # The namespaced user model for testing
15
+ let(:user_class) { 'Azpire::V1::HumanResource::User' }
16
+ let(:namespace_path) { user_class.underscore }
17
+ let(:table_name) { user_class.pluralize.underscore.gsub('/','_') }
18
+
19
+ describe 'user model with namespace, clean install' do
20
+ setup :prepare_destination
21
+
22
+ before do
23
+ run_generator %W[#{user_class} auth]
24
+ end
25
+
26
+ test 'user model (with namespace) is created, concern is included' do
27
+ assert_file "app/models/#{namespace_path}.rb" do |model|
28
+ assert_match(/include DeviseTokenAuth::Concerns::User/, model)
29
+ end
30
+ end
31
+
32
+ test 'initializer is created' do
33
+ assert_file 'config/initializers/devise_token_auth.rb'
34
+ end
35
+
36
+ test 'subsequent runs raise no errors' do
37
+ run_generator %W[#{user_class} auth]
38
+ end
39
+
40
+ if DEVISE_TOKEN_AUTH_ORM == :active_record
41
+ test 'migration is created for user model with namespace' do
42
+ assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb"
43
+ end
44
+
45
+ test 'migration file for user model with namespace contains rails version' do
46
+ if Rails::VERSION::MAJOR >= 5
47
+ assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}/
48
+ else
49
+ assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb"
50
+ end
51
+ end
52
+
53
+ test 'add primary key type with rails 5 when specified in rails generator' do
54
+ run_generator %W[#{user_class} auth --primary_key_type=uuid --force]
55
+ if Rails::VERSION::MAJOR >= 5
56
+ assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /create_table\(:#{table_name}, id: :uuid\) do/
57
+ else
58
+ assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /create_table\(:#{table_name}\) do/
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ describe 'existing user model' do
65
+ setup :prepare_destination
66
+
67
+ before do
68
+ @dir = File.join(destination_root, 'app', 'models')
69
+
70
+ @fname = File.join(@dir, 'user.rb')
71
+
72
+ # make dir if not exists
73
+ FileUtils.mkdir_p(@dir)
74
+
75
+ case DEVISE_TOKEN_AUTH_ORM
76
+ when :active_record
77
+ # account for rails version 5
78
+ active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
79
+
80
+ @f = File.open(@fname, 'w') do |f|
81
+ f.write <<-RUBY
82
+ class User < #{active_record_needle}
83
+
84
+ def whatever
85
+ puts 'whatever'
86
+ end
87
+ end
88
+ RUBY
89
+ end
90
+ when :mongoid
91
+ @f = File.open(@fname, 'w') do |f|
92
+ f.write <<-'RUBY'
93
+ class User
94
+
95
+ def whatever
96
+ puts 'whatever'
97
+ end
98
+ end
99
+ RUBY
100
+ end
101
+ end
102
+
103
+ run_generator
104
+ end
105
+
106
+ test 'user concern is injected into existing model' do
107
+ assert_file 'app/models/user.rb' do |model|
108
+ assert_match(/include DeviseTokenAuth::Concerns::User/, model)
109
+ end
110
+ end
111
+
112
+ test 'subsequent runs do not modify file' do
113
+ run_generator
114
+ assert_file 'app/models/user.rb' do |model|
115
+ matches = model.scan(/include DeviseTokenAuth::Concerns::User/m).size
116
+ assert_equal 1, matches
117
+ end
118
+ end
119
+ end
120
+
121
+ describe 'routes' do
122
+ setup :prepare_destination
123
+
124
+ before do
125
+ @dir = File.join(destination_root, 'config')
126
+
127
+ @fname = File.join(@dir, 'routes.rb')
128
+
129
+ # make dir if not exists
130
+ FileUtils.mkdir_p(@dir)
131
+
132
+ @f = File.open(@fname, 'w') do |f|
133
+ f.write <<-RUBY
134
+ Rails.application.routes.draw do
135
+ patch '/chong', to: 'bong#index'
136
+ end
137
+ RUBY
138
+ end
139
+
140
+ run_generator %W[#{user_class} auth]
141
+ end
142
+
143
+ test 'route method for user model with namespace is appended to routes file' do
144
+ assert_file 'config/routes.rb' do |routes|
145
+ assert_match(/mount_devise_token_auth_for '#{user_class}', at: 'auth'/, routes)
146
+ end
147
+ end
148
+
149
+ test 'subsequent runs do not modify file' do
150
+ run_generator %W[#{user_class} auth]
151
+ assert_file 'config/routes.rb' do |routes|
152
+ matches = routes.scan(/mount_devise_token_auth_for '#{user_class}', at: 'auth'/m).size
153
+ assert_equal 1, matches
154
+ end
155
+ end
156
+
157
+ describe 'subsequent models' do
158
+ before do
159
+ run_generator %w[Mang mangs]
160
+ end
161
+
162
+ test 'route method is appended to routes file' do
163
+ assert_file 'config/routes.rb' do |routes|
164
+ assert_match(/mount_devise_token_auth_for 'Mang', at: 'mangs'/, routes)
165
+ end
166
+ end
167
+
168
+ test 'devise_for block is appended to routes file' do
169
+ assert_file 'config/routes.rb' do |routes|
170
+ assert_match(/as :mang do/, routes)
171
+ assert_match(/# Define routes for Mang within this block./, routes)
172
+ end
173
+ end
174
+
175
+ if DEVISE_TOKEN_AUTH_ORM == :active_record
176
+ test 'migration is created' do
177
+ assert_migration 'db/migrate/devise_token_auth_create_mangs.rb'
178
+ end
179
+ end
180
+ end
181
+ end
182
+
183
+ describe 'application controller' do
184
+ setup :prepare_destination
185
+
186
+ before do
187
+ @dir = File.join(destination_root, 'app', 'controllers')
188
+
189
+ @fname = File.join(@dir, 'application_controller.rb')
190
+
191
+ # make dir if not exists
192
+ FileUtils.mkdir_p(@dir)
193
+
194
+ @f = File.open(@fname, 'w') do |f|
195
+ f.write <<-RUBY
196
+ class ApplicationController < ActionController::Base
197
+ def whatever
198
+ 'whatever'
199
+ end
200
+ end
201
+ RUBY
202
+ end
203
+
204
+ run_generator %W[#{user_class} auth]
205
+ end
206
+
207
+ test 'controller concern is appended to application controller' do
208
+ assert_file 'app/controllers/application_controller.rb' do |controller|
209
+ assert_match(/include DeviseTokenAuth::Concerns::SetUserByToken/, controller)
210
+ end
211
+ end
212
+
213
+ test 'subsequent runs do not modify file' do
214
+ run_generator %W[#{user_class} auth]
215
+ assert_file 'app/controllers/application_controller.rb' do |controller|
216
+ matches = controller.scan(/include DeviseTokenAuth::Concerns::SetUserByToken/m).size
217
+ assert_equal 1, matches
218
+ end
219
+ end
220
+ end
221
+ end
222
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'fileutils'
5
+ require 'generators/devise_token_auth/install_views_generator'
6
+
7
+ module DeviseTokenAuth
8
+ class InstallViewsGeneratorTest < Rails::Generators::TestCase
9
+ tests InstallViewsGenerator
10
+ destination Rails.root.join('tmp/generators')
11
+
12
+ describe 'default values, clean install' do
13
+ setup :prepare_destination
14
+
15
+ before do
16
+ run_generator
17
+ end
18
+
19
+ test 'files are copied' do
20
+ assert_file 'app/views/devise/mailer/reset_password_instructions.html.erb'
21
+ assert_file 'app/views/devise/mailer/confirmation_instructions.html.erb'
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ if DEVISE_TOKEN_AUTH_ORM == :mongoid
6
+ class DeviseTokenAuth::Concerns::MongoidSupportTest < ActiveSupport::TestCase
7
+ describe DeviseTokenAuth::Concerns::MongoidSupport do
8
+ before do
9
+ @user = create(:user)
10
+ end
11
+
12
+ describe '#as_json' do
13
+ test 'should be defined' do
14
+ assert @user.methods.include?(:as_json)
15
+ end
16
+
17
+ test 'should except _id attribute' do
18
+ refute @user.as_json.key?('_id')
19
+ end
20
+
21
+ test 'should return with id attribute' do
22
+ assert_equal @user._id.to_s, @user.as_json['id']
23
+ end
24
+
25
+ test 'should accept options' do
26
+ refute @user.as_json(except: [:created_at]).key?('created_at')
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class OnlyEmailUserTest < ActiveSupport::TestCase
6
+ describe OnlyEmailUser do
7
+ test 'trackable is disabled' do
8
+ refute OnlyEmailUser.method_defined?(:sign_in_count)
9
+ refute OnlyEmailUser.method_defined?(:current_sign_in_at)
10
+ refute OnlyEmailUser.method_defined?(:last_sign_in_at)
11
+ refute OnlyEmailUser.method_defined?(:current_sign_in_ip)
12
+ refute OnlyEmailUser.method_defined?(:last_sign_in_ip)
13
+ end
14
+
15
+ test 'confirmable is disabled' do
16
+ refute OnlyEmailUser.method_defined?(:confirmation_token)
17
+ refute OnlyEmailUser.method_defined?(:confirmed_at)
18
+ refute OnlyEmailUser.method_defined?(:confirmation_sent_at)
19
+ refute OnlyEmailUser.method_defined?(:unconfirmed_email)
20
+ end
21
+
22
+ test 'lockable is disabled' do
23
+ refute OnlyEmailUser.method_defined?(:failed_attempts)
24
+ refute OnlyEmailUser.method_defined?(:unlock_token)
25
+ refute OnlyEmailUser.method_defined?(:locked_at)
26
+ end
27
+
28
+ test 'recoverable is disabled' do
29
+ refute OnlyEmailUser.method_defined?(:reset_password_token)
30
+ refute OnlyEmailUser.method_defined?(:reset_password_sent_at)
31
+ end
32
+
33
+ test 'rememberable is disabled' do
34
+ refute OnlyEmailUser.method_defined?(:remember_created_at)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class UserTest < ActiveSupport::TestCase
6
+ describe User do
7
+ describe 'serialization' do
8
+ test 'hash should not include sensitive info' do
9
+ @resource = build(:user)
10
+ refute @resource.as_json[:tokens]
11
+ end
12
+ end
13
+
14
+ describe 'creation' do
15
+ test 'save fails if uid is missing' do
16
+ @resource = User.new
17
+ @resource.uid = nil
18
+ @resource.save
19
+
20
+ assert @resource.errors.messages[:uid]
21
+ end
22
+ end
23
+
24
+ describe 'email registration' do
25
+ test 'model should not save if email is blank' do
26
+ @resource = build(:user, email: nil)
27
+
28
+ refute @resource.save
29
+ assert @resource.errors.messages[:email] == [I18n.t('errors.messages.blank')]
30
+ end
31
+
32
+ test 'model should not save if email is not an email' do
33
+ @resource = build(:user, email: '@example.com')
34
+
35
+ refute @resource.save
36
+ assert @resource.errors.messages[:email] == [I18n.t('errors.messages.not_email')]
37
+ end
38
+ end
39
+
40
+ describe 'email uniqueness' do
41
+ test 'model should not save if email is taken' do
42
+ user_attributes = attributes_for(:user)
43
+ create(:user, user_attributes)
44
+ @resource = build(:user, user_attributes)
45
+
46
+ refute @resource.save
47
+ assert @resource.errors.messages[:email].first.include? 'taken'
48
+ assert @resource.errors.messages[:email].none? { |e| e =~ /translation missing/ }
49
+ end
50
+ end
51
+
52
+ describe 'oauth2 authentication' do
53
+ test 'model should save even if email is blank' do
54
+ @resource = build(:user, :facebook, email: nil)
55
+
56
+ assert @resource.save
57
+ assert @resource.errors.messages[:email].blank?
58
+ end
59
+ end
60
+
61
+ describe 'token expiry' do
62
+ before do
63
+ @resource = create(:user, :confirmed)
64
+
65
+ @auth_headers = @resource.create_new_auth_token
66
+
67
+ @token = @auth_headers['access-token']
68
+ @client_id = @auth_headers['client']
69
+ end
70
+
71
+ test 'should properly indicate whether token is current' do
72
+ assert @resource.token_is_current?(@token, @client_id)
73
+ # we want to update the expiry without forcing a cleanup (see below)
74
+ @resource.tokens[@client_id]['expiry'] = Time.zone.now.to_i - 10.seconds
75
+ refute @resource.token_is_current?(@token, @client_id)
76
+ end
77
+ end
78
+
79
+ describe 'user specific token lifespan' do
80
+ before do
81
+ @resource = create(:user, :confirmed)
82
+
83
+ auth_headers = @resource.create_new_auth_token
84
+ @token_global = auth_headers['access-token']
85
+ @client_id_global = auth_headers['client']
86
+
87
+ def @resource.token_lifespan
88
+ 1.minute
89
+ end
90
+
91
+ auth_headers = @resource.create_new_auth_token
92
+ @token_specific = auth_headers['access-token']
93
+ @client_id_specific = auth_headers['client']
94
+ end
95
+
96
+ test 'works per user' do
97
+ assert @resource.token_is_current?(@token_global, @client_id_global)
98
+
99
+ time = Time.zone.now.to_i
100
+ expiry_global = @resource.tokens[@client_id_global]['expiry'] || @resource.tokens[@client_id_global][:expiry]
101
+
102
+ assert expiry_global > time + DeviseTokenAuth.token_lifespan - 5.seconds
103
+ assert expiry_global < time + DeviseTokenAuth.token_lifespan + 5.seconds
104
+
105
+ expiry_specific = @resource.tokens[@client_id_specific]['expiry'] || @resource.tokens[@client_id_specific][:expiry]
106
+ assert expiry_specific > time + 55.seconds
107
+ assert expiry_specific < time + 65.seconds
108
+ end
109
+ end
110
+
111
+ describe 'expired tokens are destroyed on save' do
112
+ before do
113
+ @resource = create(:user, :confirmed)
114
+
115
+ @old_auth_headers = @resource.create_new_auth_token
116
+ @new_auth_headers = @resource.create_new_auth_token
117
+ expire_token(@resource, @old_auth_headers['client'])
118
+ end
119
+
120
+ test 'expired token was removed' do
121
+ refute @resource.tokens[@old_auth_headers[:client]]
122
+ end
123
+
124
+ test 'current token was not removed' do
125
+ assert @resource.tokens[@new_auth_headers['client']]
126
+ end
127
+ end
128
+
129
+ describe 'nil tokens are handled properly' do
130
+ before do
131
+ @resource = create(:user, :confirmed)
132
+ end
133
+
134
+ test 'tokens can be set to nil' do
135
+ @resource.tokens = nil
136
+ assert @resource.save
137
+ end
138
+ end
139
+ end
140
+ end