devise_token_auth 1.0.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise_token_auth might be problematic. Click here for more details.

Files changed (83) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +4 -2
  3. data/app/controllers/devise_token_auth/application_controller.rb +0 -1
  4. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +11 -12
  5. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +39 -55
  6. data/app/controllers/devise_token_auth/confirmations_controller.rb +62 -20
  7. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +51 -26
  8. data/app/controllers/devise_token_auth/passwords_controller.rb +19 -23
  9. data/app/controllers/devise_token_auth/registrations_controller.rb +32 -40
  10. data/app/controllers/devise_token_auth/sessions_controller.rb +5 -5
  11. data/app/controllers/devise_token_auth/unlocks_controller.rb +4 -4
  12. data/app/models/devise_token_auth/concerns/active_record_support.rb +16 -0
  13. data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
  14. data/app/models/devise_token_auth/concerns/tokens_serialization.rb +19 -0
  15. data/app/models/devise_token_auth/concerns/user.rb +44 -67
  16. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +2 -2
  17. data/app/validators/{email_validator.rb → devise_token_auth_email_validator.rb} +1 -1
  18. data/config/locales/en.yml +5 -0
  19. data/config/locales/he.yml +50 -0
  20. data/config/locales/ja.yml +1 -1
  21. data/lib/devise_token_auth/blacklist.rb +2 -0
  22. data/lib/devise_token_auth/engine.rb +2 -0
  23. data/lib/devise_token_auth/rails/routes.rb +1 -1
  24. data/lib/devise_token_auth/token_factory.rb +126 -0
  25. data/lib/devise_token_auth/version.rb +1 -1
  26. data/lib/devise_token_auth.rb +6 -3
  27. data/lib/generators/devise_token_auth/install_generator.rb +3 -87
  28. data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
  29. data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
  30. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +5 -0
  31. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +0 -7
  32. data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +56 -0
  33. data/test/controllers/custom/custom_confirmations_controller_test.rb +1 -1
  34. data/test/controllers/demo_user_controller_test.rb +2 -2
  35. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +79 -19
  36. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +2 -0
  37. data/test/controllers/devise_token_auth/passwords_controller_test.rb +115 -94
  38. data/test/controllers/devise_token_auth/registrations_controller_test.rb +31 -4
  39. data/test/controllers/devise_token_auth/sessions_controller_test.rb +0 -38
  40. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +2 -1
  41. data/test/dummy/app/{models → active_record}/scoped_user.rb +2 -2
  42. data/test/dummy/app/{models → active_record}/unconfirmable_user.rb +1 -2
  43. data/test/dummy/app/{models → active_record}/unregisterable_user.rb +3 -3
  44. data/test/dummy/app/active_record/user.rb +6 -0
  45. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +3 -3
  46. data/test/dummy/app/controllers/overrides/passwords_controller.rb +3 -3
  47. data/test/dummy/app/controllers/overrides/registrations_controller.rb +1 -1
  48. data/test/dummy/app/controllers/overrides/sessions_controller.rb +2 -2
  49. data/test/dummy/app/models/{user.rb → concerns/favorite_color.rb} +7 -8
  50. data/test/dummy/app/mongoid/lockable_user.rb +38 -0
  51. data/test/dummy/app/mongoid/mang.rb +46 -0
  52. data/test/dummy/app/mongoid/only_email_user.rb +33 -0
  53. data/test/dummy/app/mongoid/scoped_user.rb +50 -0
  54. data/test/dummy/app/mongoid/unconfirmable_user.rb +44 -0
  55. data/test/dummy/app/mongoid/unregisterable_user.rb +47 -0
  56. data/test/dummy/app/mongoid/user.rb +49 -0
  57. data/test/dummy/config/application.rb +23 -1
  58. data/test/dummy/config/boot.rb +4 -0
  59. data/test/dummy/config/initializers/devise.rb +285 -0
  60. data/test/dummy/config/initializers/devise_token_auth.rb +35 -4
  61. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +0 -7
  62. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +0 -7
  63. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +0 -7
  64. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +0 -7
  65. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +0 -7
  66. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +0 -7
  67. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +0 -7
  68. data/test/dummy/db/schema.rb +1 -28
  69. data/test/factories/users.rb +1 -1
  70. data/test/lib/devise_token_auth/blacklist_test.rb +11 -0
  71. data/test/lib/devise_token_auth/token_factory_test.rb +191 -0
  72. data/test/lib/generators/devise_token_auth/install_generator_test.rb +51 -31
  73. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +51 -31
  74. data/test/models/concerns/mongoid_support_test.rb +31 -0
  75. data/test/models/concerns/tokens_serialization_test.rb +70 -0
  76. data/test/models/only_email_user_test.rb +0 -8
  77. data/test/models/user_test.rb +1 -33
  78. data/test/test_helper.rb +12 -2
  79. metadata +105 -25
  80. data/config/initializers/devise.rb +0 -198
  81. /data/test/dummy/app/{models → active_record}/lockable_user.rb +0 -0
  82. /data/test/dummy/app/{models → active_record}/mang.rb +0 -0
  83. /data/test/dummy/app/{models → active_record}/only_email_user.rb +0 -0
@@ -20,13 +20,6 @@ class DeviseTokenAuthCreateUnconfirmableUsers < ActiveRecord::Migration[4.2]
20
20
  ## Rememberable
21
21
  t.datetime :remember_created_at
22
22
 
23
- ## Trackable
24
- t.integer :sign_in_count, default: 0, null: false
25
- t.datetime :current_sign_in_at
26
- t.datetime :last_sign_in_at
27
- t.string :current_sign_in_ip
28
- t.string :last_sign_in_ip
29
-
30
23
  ## Confirmable
31
24
  # t.string :confirmation_token
32
25
  # t.datetime :confirmed_at
@@ -20,13 +20,6 @@ class DeviseTokenAuthCreateScopedUsers < ActiveRecord::Migration[4.2]
20
20
  ## Rememberable
21
21
  t.datetime :remember_created_at
22
22
 
23
- ## Trackable
24
- t.integer :sign_in_count, default: 0, null: false
25
- t.datetime :current_sign_in_at
26
- t.datetime :last_sign_in_at
27
- t.string :current_sign_in_ip
28
- t.string :last_sign_in_ip
29
-
30
23
  ## Confirmable
31
24
  t.string :confirmation_token
32
25
  t.datetime :confirmed_at
@@ -20,13 +20,6 @@ class DeviseTokenAuthCreateLockableUsers < ActiveRecord::Migration[4.2]
20
20
  ## Rememberable
21
21
  # t.datetime :remember_created_at
22
22
 
23
- ## Trackable
24
- # t.integer :sign_in_count, :default => 0, :null => false
25
- # t.datetime :current_sign_in_at
26
- # t.datetime :last_sign_in_at
27
- # t.string :current_sign_in_ip
28
- # t.string :last_sign_in_ip
29
-
30
23
  ## Confirmable
31
24
  # t.string :confirmation_token
32
25
  # t.datetime :confirmed_at
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  # This file is auto-generated from the current state of the database. Instead
4
2
  # of editing this file, please use the migrations feature of Active Record to
5
3
  # incrementally modify your database, and then regenerate this schema definition.
@@ -12,7 +10,7 @@
12
10
  #
13
11
  # It's strongly recommended that you check this file into your version control system.
14
12
 
15
- ActiveRecord::Schema.define(version: 20160629184441) do
13
+ ActiveRecord::Schema.define(version: 2016_06_29_184441) do
16
14
 
17
15
  create_table "lockable_users", force: :cascade do |t|
18
16
  t.string "provider", null: false
@@ -41,11 +39,6 @@ ActiveRecord::Schema.define(version: 20160629184441) do
41
39
  t.string "reset_password_redirect_url"
42
40
  t.boolean "allow_password_change", default: false
43
41
  t.datetime "remember_created_at"
44
- t.integer "sign_in_count", default: 0, null: false
45
- t.datetime "current_sign_in_at"
46
- t.datetime "last_sign_in_at"
47
- t.string "current_sign_in_ip"
48
- t.string "last_sign_in_ip"
49
42
  t.string "confirmation_token"
50
43
  t.datetime "confirmed_at"
51
44
  t.datetime "confirmation_sent_at"
@@ -88,11 +81,6 @@ ActiveRecord::Schema.define(version: 20160629184441) do
88
81
  t.datetime "reset_password_sent_at"
89
82
  t.boolean "allow_password_change", default: false
90
83
  t.datetime "remember_created_at"
91
- t.integer "sign_in_count", default: 0, null: false
92
- t.datetime "current_sign_in_at"
93
- t.datetime "last_sign_in_at"
94
- t.string "current_sign_in_ip"
95
- t.string "last_sign_in_ip"
96
84
  t.string "confirmation_token"
97
85
  t.datetime "confirmed_at"
98
86
  t.datetime "confirmation_sent_at"
@@ -117,11 +105,6 @@ ActiveRecord::Schema.define(version: 20160629184441) do
117
105
  t.datetime "reset_password_sent_at"
118
106
  t.boolean "allow_password_change", default: false
119
107
  t.datetime "remember_created_at"
120
- t.integer "sign_in_count", default: 0, null: false
121
- t.datetime "current_sign_in_at"
122
- t.datetime "last_sign_in_at"
123
- t.string "current_sign_in_ip"
124
- t.string "last_sign_in_ip"
125
108
  t.string "name"
126
109
  t.string "nickname"
127
110
  t.string "image"
@@ -142,11 +125,6 @@ ActiveRecord::Schema.define(version: 20160629184441) do
142
125
  t.datetime "reset_password_sent_at"
143
126
  t.boolean "allow_password_change", default: false
144
127
  t.datetime "remember_created_at"
145
- t.integer "sign_in_count", default: 0, null: false
146
- t.datetime "current_sign_in_at"
147
- t.datetime "last_sign_in_at"
148
- t.string "current_sign_in_ip"
149
- t.string "last_sign_in_ip"
150
128
  t.string "confirmation_token"
151
129
  t.datetime "confirmed_at"
152
130
  t.datetime "confirmation_sent_at"
@@ -171,11 +149,6 @@ ActiveRecord::Schema.define(version: 20160629184441) do
171
149
  t.string "reset_password_redirect_url"
172
150
  t.boolean "allow_password_change", default: false
173
151
  t.datetime "remember_created_at"
174
- t.integer "sign_in_count", default: 0, null: false
175
- t.datetime "current_sign_in_at"
176
- t.datetime "last_sign_in_at"
177
- t.string "current_sign_in_ip"
178
- t.string "last_sign_in_ip"
179
152
  t.string "confirmation_token"
180
153
  t.datetime "confirmed_at"
181
154
  t.datetime "confirmation_sent_at"
@@ -24,7 +24,7 @@ FactoryBot.define do
24
24
  end
25
25
 
26
26
  trait :facebook do
27
- uid { Faker::Number.number(10) }
27
+ uid { Faker::Number.number }
28
28
  provider { 'facebook' }
29
29
  end
30
30
 
@@ -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
@@ -2,11 +2,13 @@
2
2
 
3
3
  require 'test_helper'
4
4
  require 'fileutils'
5
- require 'generators/devise_token_auth/install_generator'
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
6
7
 
7
8
  module DeviseTokenAuth
8
9
  class InstallGeneratorTest < Rails::Generators::TestCase
9
- tests InstallGenerator
10
+ tests InstallGenerator if DEVISE_TOKEN_AUTH_ORM == :active_record
11
+ tests InstallMongoidGenerator if DEVISE_TOKEN_AUTH_ORM == :mongoid
10
12
  destination Rails.root.join('tmp/generators')
11
13
 
12
14
  describe 'default values, clean install' do
@@ -26,28 +28,30 @@ module DeviseTokenAuth
26
28
  assert_file 'config/initializers/devise_token_auth.rb'
27
29
  end
28
30
 
29
- test 'migration is created' do
30
- assert_migration 'db/migrate/devise_token_auth_create_users.rb'
31
+ test 'subsequent runs raise no errors' do
32
+ run_generator
31
33
  end
32
34
 
33
- test 'migration file contains rails version' do
34
- if Rails::VERSION::MAJOR >= 5
35
- assert_migration 'db/migrate/devise_token_auth_create_users.rb', /#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}/
36
- else
35
+ if DEVISE_TOKEN_AUTH_ORM == :active_record
36
+ test 'migration is created' do
37
37
  assert_migration 'db/migrate/devise_token_auth_create_users.rb'
38
38
  end
39
- end
40
39
 
41
- test 'subsequent runs raise no errors' do
42
- run_generator
43
- end
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
44
47
 
45
- test 'add primary key type with rails 5 when specified in rails generator' do
46
- run_generator %w[--primary_key_type=uuid --force]
47
- if Rails::VERSION::MAJOR >= 5
48
- assert_migration 'db/migrate/devise_token_auth_create_users.rb', /create_table\(:users, id: :uuid\) do/
49
- else
50
- assert_migration 'db/migrate/devise_token_auth_create_users.rb', /create_table\(:users\) do/
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
51
55
  end
52
56
  end
53
57
  end
@@ -63,18 +67,32 @@ module DeviseTokenAuth
63
67
  # make dir if not exists
64
68
  FileUtils.mkdir_p(@dir)
65
69
 
66
- # account for rails version 5
67
- active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
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'
68
74
 
69
- @f = File.open(@fname, 'w') do |f|
70
- f.write <<-RUBY
71
- class User < #{active_record_needle}
75
+ @f = File.open(@fname, 'w') do |f|
76
+ f.write <<-RUBY
77
+ class User < #{active_record_needle}
72
78
 
73
- def whatever
74
- puts 'whatever'
79
+ def whatever
80
+ puts 'whatever'
81
+ end
75
82
  end
76
- end
77
- RUBY
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
78
96
  end
79
97
 
80
98
  run_generator
@@ -136,10 +154,6 @@ module DeviseTokenAuth
136
154
  run_generator %w[Mang mangs]
137
155
  end
138
156
 
139
- test 'migration is created' do
140
- assert_migration 'db/migrate/devise_token_auth_create_mangs.rb'
141
- end
142
-
143
157
  test 'route method is appended to routes file' do
144
158
  assert_file 'config/routes.rb' do |routes|
145
159
  assert_match(/mount_devise_token_auth_for 'Mang', at: 'mangs'/, routes)
@@ -152,6 +166,12 @@ module DeviseTokenAuth
152
166
  assert_match(/# Define routes for Mang within this block./, routes)
153
167
  end
154
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
155
175
  end
156
176
  end
157
177
 
@@ -2,11 +2,13 @@
2
2
 
3
3
  require 'test_helper'
4
4
  require 'fileutils'
5
- require 'generators/devise_token_auth/install_generator'
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
6
7
 
7
8
  module DeviseTokenAuth
8
9
  class InstallGeneratorTest < Rails::Generators::TestCase
9
- tests InstallGenerator
10
+ tests InstallGenerator if DEVISE_TOKEN_AUTH_ORM == :active_record
11
+ tests InstallMongoidGenerator if DEVISE_TOKEN_AUTH_ORM == :mongoid
10
12
  destination Rails.root.join('tmp/generators')
11
13
 
12
14
  # The namespaced user model for testing
@@ -31,28 +33,30 @@ module DeviseTokenAuth
31
33
  assert_file 'config/initializers/devise_token_auth.rb'
32
34
  end
33
35
 
34
- test 'migration is created for user model with namespace' do
35
- assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb"
36
+ test 'subsequent runs raise no errors' do
37
+ run_generator %W[#{user_class} auth]
36
38
  end
37
39
 
38
- test 'migration file for user model with namespace contains rails version' do
39
- if Rails::VERSION::MAJOR >= 5
40
- assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}/
41
- else
40
+ if DEVISE_TOKEN_AUTH_ORM == :active_record
41
+ test 'migration is created for user model with namespace' do
42
42
  assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb"
43
43
  end
44
- end
45
44
 
46
- test 'subsequent runs raise no errors' do
47
- run_generator %W[#{user_class} auth]
48
- end
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
49
52
 
50
- test 'add primary key type with rails 5 when specified in rails generator' do
51
- run_generator %W[#{user_class} auth --primary_key_type=uuid --force]
52
- if Rails::VERSION::MAJOR >= 5
53
- assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /create_table\(:#{table_name}, id: :uuid\) do/
54
- else
55
- assert_migration "db/migrate/devise_token_auth_create_#{table_name}.rb", /create_table\(:#{table_name}\) do/
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
56
60
  end
57
61
  end
58
62
  end
@@ -68,18 +72,32 @@ module DeviseTokenAuth
68
72
  # make dir if not exists
69
73
  FileUtils.mkdir_p(@dir)
70
74
 
71
- # account for rails version 5
72
- active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
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'
73
79
 
74
- @f = File.open(@fname, 'w') do |f|
75
- f.write <<-RUBY
76
- class User < #{active_record_needle}
80
+ @f = File.open(@fname, 'w') do |f|
81
+ f.write <<-RUBY
82
+ class User < #{active_record_needle}
77
83
 
78
- def whatever
79
- puts 'whatever'
84
+ def whatever
85
+ puts 'whatever'
86
+ end
80
87
  end
81
- end
82
- RUBY
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
83
101
  end
84
102
 
85
103
  run_generator
@@ -141,10 +159,6 @@ module DeviseTokenAuth
141
159
  run_generator %w[Mang mangs]
142
160
  end
143
161
 
144
- test 'migration is created' do
145
- assert_migration 'db/migrate/devise_token_auth_create_mangs.rb'
146
- end
147
-
148
162
  test 'route method is appended to routes file' do
149
163
  assert_file 'config/routes.rb' do |routes|
150
164
  assert_match(/mount_devise_token_auth_for 'Mang', at: 'mangs'/, routes)
@@ -157,6 +171,12 @@ module DeviseTokenAuth
157
171
  assert_match(/# Define routes for Mang within this block./, routes)
158
172
  end
159
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
160
180
  end
161
181
  end
162
182
 
@@ -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