sidecar_token_auth 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +13 -0
  3. data/README.md +12 -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 +162 -0
  8. data/app/controllers/devise_token_auth/confirmations_controller.rb +90 -0
  9. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +287 -0
  10. data/app/controllers/devise_token_auth/passwords_controller.rb +206 -0
  11. data/app/controllers/devise_token_auth/registrations_controller.rb +283 -0
  12. data/app/controllers/devise_token_auth/sessions_controller.rb +245 -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 +16 -0
  16. data/app/models/devise_token_auth/concerns/confirmable_support.rb +28 -0
  17. data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
  18. data/app/models/devise_token_auth/concerns/tokens_serialization.rb +19 -0
  19. data/app/models/devise_token_auth/concerns/user.rb +257 -0
  20. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +31 -0
  21. data/app/validators/devise_token_auth_email_validator.rb +23 -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_token_auth/omniauth_external_window.html.erb +38 -0
  26. data/config/locales/da-DK.yml +52 -0
  27. data/config/locales/de.yml +51 -0
  28. data/config/locales/en.yml +60 -0
  29. data/config/locales/es.yml +51 -0
  30. data/config/locales/fr.yml +51 -0
  31. data/config/locales/he.yml +52 -0
  32. data/config/locales/it.yml +48 -0
  33. data/config/locales/ja.yml +48 -0
  34. data/config/locales/ko.yml +51 -0
  35. data/config/locales/nl.yml +32 -0
  36. data/config/locales/pl.yml +51 -0
  37. data/config/locales/pt-BR.yml +48 -0
  38. data/config/locales/pt.yml +51 -0
  39. data/config/locales/ro.yml +48 -0
  40. data/config/locales/ru.yml +52 -0
  41. data/config/locales/sq.yml +48 -0
  42. data/config/locales/sv.yml +52 -0
  43. data/config/locales/uk.yml +61 -0
  44. data/config/locales/vi.yml +52 -0
  45. data/config/locales/zh-CN.yml +48 -0
  46. data/config/locales/zh-HK.yml +50 -0
  47. data/config/locales/zh-TW.yml +50 -0
  48. data/lib/devise_token_auth.rb +14 -0
  49. data/lib/devise_token_auth/blacklist.rb +2 -0
  50. data/lib/devise_token_auth/controllers/helpers.rb +157 -0
  51. data/lib/devise_token_auth/controllers/url_helpers.rb +10 -0
  52. data/lib/devise_token_auth/engine.rb +96 -0
  53. data/lib/devise_token_auth/errors.rb +8 -0
  54. data/lib/devise_token_auth/rails/routes.rb +116 -0
  55. data/lib/devise_token_auth/token_factory.rb +126 -0
  56. data/lib/devise_token_auth/url.rb +44 -0
  57. data/lib/devise_token_auth/version.rb +5 -0
  58. data/lib/generators/devise_token_auth/USAGE +31 -0
  59. data/lib/generators/devise_token_auth/install_generator.rb +91 -0
  60. data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
  61. data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
  62. data/lib/generators/devise_token_auth/install_views_generator.rb +18 -0
  63. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +60 -0
  64. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +49 -0
  65. data/lib/generators/devise_token_auth/templates/user.rb.erb +9 -0
  66. data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +56 -0
  67. data/lib/tasks/devise_token_auth_tasks.rake +6 -0
  68. data/test/controllers/custom/custom_confirmations_controller_test.rb +25 -0
  69. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +33 -0
  70. data/test/controllers/custom/custom_passwords_controller_test.rb +79 -0
  71. data/test/controllers/custom/custom_registrations_controller_test.rb +63 -0
  72. data/test/controllers/custom/custom_sessions_controller_test.rb +39 -0
  73. data/test/controllers/custom/custom_token_validations_controller_test.rb +42 -0
  74. data/test/controllers/demo_group_controller_test.rb +151 -0
  75. data/test/controllers/demo_mang_controller_test.rb +284 -0
  76. data/test/controllers/demo_user_controller_test.rb +629 -0
  77. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +191 -0
  78. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +441 -0
  79. data/test/controllers/devise_token_auth/passwords_controller_test.rb +780 -0
  80. data/test/controllers/devise_token_auth/registrations_controller_test.rb +907 -0
  81. data/test/controllers/devise_token_auth/sessions_controller_test.rb +503 -0
  82. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +102 -0
  83. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +196 -0
  84. data/test/controllers/overrides/confirmations_controller_test.rb +47 -0
  85. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +53 -0
  86. data/test/controllers/overrides/passwords_controller_test.rb +64 -0
  87. data/test/controllers/overrides/registrations_controller_test.rb +46 -0
  88. data/test/controllers/overrides/sessions_controller_test.rb +35 -0
  89. data/test/controllers/overrides/token_validations_controller_test.rb +43 -0
  90. data/test/dummy/README.rdoc +28 -0
  91. data/test/dummy/app/active_record/confirmable_user.rb +11 -0
  92. data/test/dummy/app/active_record/lockable_user.rb +7 -0
  93. data/test/dummy/app/active_record/mang.rb +5 -0
  94. data/test/dummy/app/active_record/only_email_user.rb +7 -0
  95. data/test/dummy/app/active_record/scoped_user.rb +9 -0
  96. data/test/dummy/app/active_record/unconfirmable_user.rb +9 -0
  97. data/test/dummy/app/active_record/unregisterable_user.rb +9 -0
  98. data/test/dummy/app/active_record/user.rb +6 -0
  99. data/test/dummy/app/controllers/application_controller.rb +18 -0
  100. data/test/dummy/app/controllers/auth_origin_controller.rb +7 -0
  101. data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
  102. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +13 -0
  103. data/test/dummy/app/controllers/custom/passwords_controller.rb +39 -0
  104. data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
  105. data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
  106. data/test/dummy/app/controllers/custom/token_validations_controller.rb +19 -0
  107. data/test/dummy/app/controllers/demo_group_controller.rb +15 -0
  108. data/test/dummy/app/controllers/demo_mang_controller.rb +14 -0
  109. data/test/dummy/app/controllers/demo_user_controller.rb +27 -0
  110. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +28 -0
  111. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +16 -0
  112. data/test/dummy/app/controllers/overrides/passwords_controller.rb +35 -0
  113. data/test/dummy/app/controllers/overrides/registrations_controller.rb +29 -0
  114. data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
  115. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
  116. data/test/dummy/app/helpers/application_helper.rb +1058 -0
  117. data/test/dummy/app/models/concerns/favorite_color.rb +19 -0
  118. data/test/dummy/app/mongoid/confirmable_user.rb +52 -0
  119. data/test/dummy/app/mongoid/lockable_user.rb +38 -0
  120. data/test/dummy/app/mongoid/mang.rb +46 -0
  121. data/test/dummy/app/mongoid/only_email_user.rb +33 -0
  122. data/test/dummy/app/mongoid/scoped_user.rb +50 -0
  123. data/test/dummy/app/mongoid/unconfirmable_user.rb +44 -0
  124. data/test/dummy/app/mongoid/unregisterable_user.rb +47 -0
  125. data/test/dummy/app/mongoid/user.rb +49 -0
  126. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  127. data/test/dummy/config.ru +18 -0
  128. data/test/dummy/config/application.rb +48 -0
  129. data/test/dummy/config/application.yml.bk +0 -0
  130. data/test/dummy/config/boot.rb +11 -0
  131. data/test/dummy/config/environment.rb +7 -0
  132. data/test/dummy/config/environments/development.rb +46 -0
  133. data/test/dummy/config/environments/production.rb +84 -0
  134. data/test/dummy/config/environments/test.rb +50 -0
  135. data/test/dummy/config/initializers/assets.rb +10 -0
  136. data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
  137. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  138. data/test/dummy/config/initializers/devise.rb +290 -0
  139. data/test/dummy/config/initializers/devise_token_auth.rb +55 -0
  140. data/test/dummy/config/initializers/figaro.rb +3 -0
  141. data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  142. data/test/dummy/config/initializers/inflections.rb +18 -0
  143. data/test/dummy/config/initializers/mime_types.rb +6 -0
  144. data/test/dummy/config/initializers/omniauth.rb +11 -0
  145. data/test/dummy/config/initializers/session_store.rb +5 -0
  146. data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
  147. data/test/dummy/config/routes.rb +57 -0
  148. data/test/dummy/config/spring.rb +3 -0
  149. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +58 -0
  150. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +57 -0
  151. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +8 -0
  152. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +7 -0
  153. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +55 -0
  154. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +56 -0
  155. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +56 -0
  156. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +56 -0
  157. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +56 -0
  158. data/test/dummy/db/migrate/20190924101113_devise_token_auth_create_confirmable_users.rb +49 -0
  159. data/test/dummy/db/schema.rb +198 -0
  160. data/test/dummy/lib/migration_database_helper.rb +43 -0
  161. data/test/factories/users.rb +41 -0
  162. data/test/lib/devise_token_auth/blacklist_test.rb +11 -0
  163. data/test/lib/devise_token_auth/token_factory_test.rb +191 -0
  164. data/test/lib/devise_token_auth/url_test.rb +26 -0
  165. data/test/lib/generators/devise_token_auth/install_generator_test.rb +217 -0
  166. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +222 -0
  167. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +25 -0
  168. data/test/models/concerns/mongoid_support_test.rb +31 -0
  169. data/test/models/concerns/tokens_serialization_test.rb +70 -0
  170. data/test/models/confirmable_user_test.rb +35 -0
  171. data/test/models/only_email_user_test.rb +29 -0
  172. data/test/models/user_test.rb +108 -0
  173. data/test/support/controllers/routes.rb +43 -0
  174. data/test/test_helper.rb +103 -0
  175. metadata +481 -0
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class DeviseTokenAuth::BlacklistTest < ActiveSupport::TestCase
6
+ describe Devise::Models::Authenticatable::BLACKLIST_FOR_SERIALIZATION do
7
+ test 'should include :tokens' do
8
+ assert Devise::Models::Authenticatable::BLACKLIST_FOR_SERIALIZATION.include?(:tokens)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,191 @@
1
+ require 'test_helper'
2
+
3
+ class DeviseTokenAuth::TokenFactoryTest < ActiveSupport::TestCase
4
+ describe 'TokenFactory module' do
5
+ let(:tf) { DeviseTokenAuth::TokenFactory }
6
+ let(:token_regexp) { /^[-_A-Za-z0-9]{22}$/ }
7
+
8
+ it 'should be defined' do
9
+ assert_equal(tf.present?, true)
10
+ assert_kind_of(Module, tf)
11
+ end
12
+
13
+ describe 'interface' do
14
+ let(:token_hash_cost_regexp) { /\$[\w]+\$([\d]+)\$/ }
15
+ let(:lifespan) { 10 }
16
+ let(:cost) { DeviseTokenAuth.token_cost }
17
+
18
+ it '::secure_string' do
19
+ assert_respond_to(tf, :secure_string)
20
+
21
+ secure_string = tf.secure_string
22
+ assert_equal(secure_string.size, 22)
23
+ assert_match(token_regexp, secure_string)
24
+
25
+ SecureRandom.stub(:urlsafe_base64, secure_string) do
26
+ assert_equal(tf.secure_string, secure_string)
27
+ end
28
+ end
29
+
30
+ it '::client' do
31
+ assert_respond_to(tf, :client)
32
+
33
+ client = tf.client
34
+ assert_equal(client.size, 22)
35
+ assert_match(token_regexp, client)
36
+
37
+ secure_string = tf.secure_string
38
+ tf.stub(:secure_string, secure_string) do
39
+ assert_equal(tf.client, secure_string)
40
+ end
41
+ end
42
+
43
+ it '::token' do
44
+ assert_respond_to(tf, :token)
45
+
46
+ token = tf.token
47
+ assert_kind_of(String, token)
48
+ assert_equal(token.size, 22)
49
+ assert_match(token_regexp, token)
50
+
51
+ secure_string = tf.secure_string
52
+ tf.stub(:secure_string, secure_string) do
53
+ assert_equal(tf.token, secure_string)
54
+ end
55
+ end
56
+
57
+ it '::token_hash(args)' do
58
+ assert_respond_to(tf, :token_hash)
59
+
60
+ token_hash = tf.token_hash(tf.token)
61
+ assert_equal(token_hash.size, 60)
62
+ assert_kind_of(String, token_hash)
63
+
64
+ token_cost = token_hash_cost_regexp.match(token_hash)[1].to_i
65
+ assert_equal(token_cost, cost)
66
+
67
+ cost = DeviseTokenAuth.token_cost == 4 ? 10 : 4
68
+ token_hash = tf.token_hash(tf.token, cost)
69
+ token_cost = token_hash_cost_regexp.match(token_hash)[1].to_i
70
+ assert_equal(token_cost, cost)
71
+
72
+ cost = nil
73
+ token_hash = tf.token_hash(tf.token, cost)
74
+ token_cost = token_hash_cost_regexp.match(token_hash)[1].to_i
75
+ assert_equal(token_cost, DeviseTokenAuth.token_cost)
76
+ end
77
+
78
+ it '::expiry' do
79
+ assert_respond_to(tf, :expiry)
80
+
81
+ assert_kind_of(Integer, tf.expiry)
82
+ assert tf.expiry > Time.now.to_i
83
+ end
84
+
85
+ it '::expiry(args)' do
86
+ time = Time.now
87
+ Time.stub(:now, time) do
88
+ assert_equal(tf.expiry(lifespan), (time + lifespan).to_i)
89
+
90
+ lifespan = nil
91
+ assert_equal(tf.expiry(lifespan), (time + DeviseTokenAuth.token_lifespan).to_i)
92
+ end
93
+ end
94
+
95
+ it '::create' do
96
+ assert_respond_to(tf, :create)
97
+
98
+ token = tf.create
99
+ assert token
100
+ token.members.each { |m| refute_nil token[m] }
101
+ end
102
+
103
+ it '::create(args)' do
104
+ client = tf.client
105
+ token = tf.create(client: client)
106
+ assert_equal(token.client, client)
107
+
108
+ time = Time.now
109
+ Time.stub(:now, time) do
110
+ token = tf.create(lifespan: lifespan)
111
+ assert_equal(token.expiry, (time + lifespan).to_i)
112
+ end
113
+
114
+ token = tf.create(cost: cost)
115
+ token_cost = token_hash_cost_regexp.match(token.token_hash)[1].to_i
116
+ assert_equal(token_cost, cost)
117
+ end
118
+
119
+ it '::new' do
120
+ assert_respond_to(tf, :new)
121
+
122
+ token = tf.new
123
+ token.each { |v| assert_nil v }
124
+ end
125
+
126
+ it '::valid_token_hash?' do
127
+ assert_respond_to(tf, :valid_token_hash?)
128
+
129
+ refute tf.valid_token_hash?('koskoskos')
130
+ assert tf.valid_token_hash?(tf.create.token_hash)
131
+ end
132
+
133
+ it '::token_hash_is_token?' do
134
+ assert_respond_to(tf, :token_hash_is_token?)
135
+
136
+ token = tf.create
137
+ refute tf.token_hash_is_token?(token.token_hash, 'koskoskos')
138
+ refute tf.token_hash_is_token?('koskoskos', token.token)
139
+ assert tf.token_hash_is_token?(token.token_hash, token.token)
140
+ end
141
+ end
142
+
143
+ describe 'token object implements' do
144
+ let(:object) { tf.create }
145
+
146
+ it '#client' do
147
+ assert_respond_to(object, :client)
148
+
149
+ assert_kind_of(String, object.client)
150
+ assert_equal(object.client.size, 22)
151
+ assert_match(token_regexp, object.client)
152
+ end
153
+
154
+ it '#token' do
155
+ assert_respond_to(object, :token)
156
+
157
+ assert_kind_of(String, object.token)
158
+ assert_equal(object.token.size, 22)
159
+ assert_match(token_regexp, object.token)
160
+ end
161
+
162
+ it '#token_hash' do
163
+ assert_respond_to(object, :token_hash)
164
+
165
+ assert_kind_of(String, object.token_hash)
166
+ assert_equal(object.token_hash.size, 60)
167
+ end
168
+
169
+ it '#expiry' do
170
+ assert_respond_to(object, :expiry)
171
+ assert_kind_of(Integer, object.expiry)
172
+ end
173
+
174
+ it '#clear!' do
175
+ assert_respond_to(object, :clear!)
176
+
177
+ assert object.clear!
178
+ object.each { |v| assert_nil v }
179
+ end
180
+
181
+ it '#present?' do
182
+ assert_respond_to(object, :present?)
183
+
184
+ assert object.present?
185
+
186
+ object.token = nil
187
+ refute object.present?
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class DeviseTokenAuth::UrlTest < ActiveSupport::TestCase
6
+ describe 'DeviseTokenAuth::Url#generate' do
7
+ test 'URI fragment should appear at the end of URL with repeat of query params' do
8
+ params = { client_id: 123 }
9
+ url = 'http://example.com#fragment'
10
+ assert_equal DeviseTokenAuth::Url.send(:generate, url, params), 'http://example.com?client_id=123#fragment?client_id=123'
11
+ end
12
+
13
+ describe 'with existing query params' do
14
+ test 'should preserve existing query params' do
15
+ url = 'http://example.com?a=1'
16
+ assert_equal DeviseTokenAuth::Url.send(:generate, url), 'http://example.com?a=1'
17
+ end
18
+
19
+ test 'should marge existing query params with new ones' do
20
+ params = { client_id: 123 }
21
+ url = 'http://example.com?a=1'
22
+ assert_equal DeviseTokenAuth::Url.send(:generate, url, params), 'http://example.com?a=1&client_id=123'
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,217 @@
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
+ describe 'default values, clean install' do
15
+ setup :prepare_destination
16
+
17
+ before do
18
+ run_generator
19
+ end
20
+
21
+ test 'user model is created, concern is included' do
22
+ assert_file 'app/models/user.rb' do |model|
23
+ assert_match(/include DeviseTokenAuth::Concerns::User/, model)
24
+ end
25
+ end
26
+
27
+ test 'initializer is created' do
28
+ assert_file 'config/initializers/devise_token_auth.rb'
29
+ end
30
+
31
+ test 'subsequent runs raise no errors' do
32
+ run_generator
33
+ end
34
+
35
+ if DEVISE_TOKEN_AUTH_ORM == :active_record
36
+ test 'migration is created' do
37
+ assert_migration 'db/migrate/devise_token_auth_create_users.rb'
38
+ end
39
+
40
+ test 'migration file contains rails version' do
41
+ if Rails::VERSION::MAJOR >= 5
42
+ assert_migration 'db/migrate/devise_token_auth_create_users.rb', /#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}/
43
+ else
44
+ assert_migration 'db/migrate/devise_token_auth_create_users.rb'
45
+ end
46
+ end
47
+
48
+ test 'add primary key type with rails 5 when specified in rails generator' do
49
+ run_generator %w[--primary_key_type=uuid --force]
50
+ if Rails::VERSION::MAJOR >= 5
51
+ assert_migration 'db/migrate/devise_token_auth_create_users.rb', /create_table\(:users, id: :uuid\) do/
52
+ else
53
+ assert_migration 'db/migrate/devise_token_auth_create_users.rb', /create_table\(:users\) do/
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ describe 'existing user model' do
60
+ setup :prepare_destination
61
+
62
+ before do
63
+ @dir = File.join(destination_root, 'app', 'models')
64
+
65
+ @fname = File.join(@dir, 'user.rb')
66
+
67
+ # make dir if not exists
68
+ FileUtils.mkdir_p(@dir)
69
+
70
+ case DEVISE_TOKEN_AUTH_ORM
71
+ when :active_record
72
+ # account for rails version 5
73
+ active_record_needle = (Rails::VERSION::MAJOR >= 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
74
+
75
+ @f = File.open(@fname, 'w') do |f|
76
+ f.write <<-RUBY
77
+ class User < #{active_record_needle}
78
+
79
+ def whatever
80
+ puts 'whatever'
81
+ end
82
+ end
83
+ RUBY
84
+ end
85
+ when :mongoid
86
+ @f = File.open(@fname, 'w') do |f|
87
+ f.write <<-'RUBY'
88
+ class User
89
+
90
+ def whatever
91
+ puts 'whatever'
92
+ end
93
+ end
94
+ RUBY
95
+ end
96
+ end
97
+
98
+ run_generator
99
+ end
100
+
101
+ test 'user concern is injected into existing model' do
102
+ assert_file 'app/models/user.rb' do |model|
103
+ assert_match(/include DeviseTokenAuth::Concerns::User/, model)
104
+ end
105
+ end
106
+
107
+ test 'subsequent runs do not modify file' do
108
+ run_generator
109
+ assert_file 'app/models/user.rb' do |model|
110
+ matches = model.scan(/include DeviseTokenAuth::Concerns::User/m).size
111
+ assert_equal 1, matches
112
+ end
113
+ end
114
+ end
115
+
116
+ describe 'routes' do
117
+ setup :prepare_destination
118
+
119
+ before do
120
+ @dir = File.join(destination_root, 'config')
121
+
122
+ @fname = File.join(@dir, 'routes.rb')
123
+
124
+ # make dir if not exists
125
+ FileUtils.mkdir_p(@dir)
126
+
127
+ @f = File.open(@fname, 'w') do |f|
128
+ f.write <<-RUBY
129
+ Rails.application.routes.draw do
130
+ patch '/chong', to: 'bong#index'
131
+ end
132
+ RUBY
133
+ end
134
+
135
+ run_generator
136
+ end
137
+
138
+ test 'route method is appended to routes file' do
139
+ assert_file 'config/routes.rb' do |routes|
140
+ assert_match(/mount_devise_token_auth_for 'User', at: 'auth'/, routes)
141
+ end
142
+ end
143
+
144
+ test 'subsequent runs do not modify file' do
145
+ run_generator
146
+ assert_file 'config/routes.rb' do |routes|
147
+ matches = routes.scan(/mount_devise_token_auth_for 'User', at: 'auth'/m).size
148
+ assert_equal 1, matches
149
+ end
150
+ end
151
+
152
+ describe 'subsequent models' do
153
+ before do
154
+ run_generator %w[Mang mangs]
155
+ end
156
+
157
+ test 'route method is appended to routes file' do
158
+ assert_file 'config/routes.rb' do |routes|
159
+ assert_match(/mount_devise_token_auth_for 'Mang', at: 'mangs'/, routes)
160
+ end
161
+ end
162
+
163
+ test 'devise_for block is appended to routes file' do
164
+ assert_file 'config/routes.rb' do |routes|
165
+ assert_match(/as :mang do/, routes)
166
+ assert_match(/# Define routes for Mang within this block./, routes)
167
+ end
168
+ end
169
+
170
+ if DEVISE_TOKEN_AUTH_ORM == :active_record
171
+ test 'migration is created' do
172
+ assert_migration 'db/migrate/devise_token_auth_create_mangs.rb'
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ describe 'application controller' do
179
+ setup :prepare_destination
180
+
181
+ before do
182
+ @dir = File.join(destination_root, 'app', 'controllers')
183
+
184
+ @fname = File.join(@dir, 'application_controller.rb')
185
+
186
+ # make dir if not exists
187
+ FileUtils.mkdir_p(@dir)
188
+
189
+ @f = File.open(@fname, 'w') do |f|
190
+ f.write <<-RUBY
191
+ class ApplicationController < ActionController::Base
192
+ def whatever
193
+ 'whatever'
194
+ end
195
+ end
196
+ RUBY
197
+ end
198
+
199
+ run_generator
200
+ end
201
+
202
+ test 'controller concern is appended to application controller' do
203
+ assert_file 'app/controllers/application_controller.rb' do |controller|
204
+ assert_match(/include DeviseTokenAuth::Concerns::SetUserByToken/, controller)
205
+ end
206
+ end
207
+
208
+ test 'subsequent runs do not modify file' do
209
+ run_generator
210
+ assert_file 'app/controllers/application_controller.rb' do |controller|
211
+ matches = controller.scan(/include DeviseTokenAuth::Concerns::SetUserByToken/m).size
212
+ assert_equal 1, matches
213
+ end
214
+ end
215
+ end
216
+ end
217
+ end
@@ -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