devise_jwt_auth 0.1.2 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/app/controllers/devise_jwt_auth/application_controller.rb +12 -24
- data/app/controllers/devise_jwt_auth/concerns/resource_finder.rb +2 -6
- data/app/controllers/devise_jwt_auth/concerns/{set_user_by_jwt_token.rb → set_user_by_token.rb} +23 -19
- data/app/controllers/devise_jwt_auth/confirmations_controller.rb +10 -19
- data/app/controllers/devise_jwt_auth/omniauth_callbacks_controller.rb +32 -33
- data/app/controllers/devise_jwt_auth/passwords_controller.rb +34 -36
- data/app/controllers/devise_jwt_auth/refresh_token_controller.rb +4 -1
- data/app/controllers/devise_jwt_auth/registrations_controller.rb +40 -21
- data/app/controllers/devise_jwt_auth/sessions_controller.rb +21 -21
- data/app/controllers/devise_jwt_auth/unlocks_controller.rb +5 -4
- data/app/models/devise_jwt_auth/concerns/active_record_support.rb +3 -0
- data/app/models/devise_jwt_auth/concerns/confirmable_support.rb +7 -4
- data/app/models/devise_jwt_auth/concerns/mongoid_support.rb +3 -0
- data/app/models/devise_jwt_auth/concerns/tokens_serialization.rb +4 -1
- data/app/models/devise_jwt_auth/concerns/user.rb +18 -9
- data/app/models/devise_jwt_auth/concerns/user_omniauth_callbacks.rb +11 -3
- data/app/validators/devise_jwt_auth_email_validator.rb +4 -3
- data/app/views/devise/mailer/reset_password_instructions.html.erb +1 -1
- data/lib/devise_jwt_auth/blacklist.rb +2 -0
- data/lib/devise_jwt_auth/controllers/url_helpers.rb +1 -2
- data/lib/devise_jwt_auth/engine.rb +4 -4
- data/lib/devise_jwt_auth/rails/routes.rb +35 -24
- data/lib/devise_jwt_auth/token_factory.rb +3 -2
- data/lib/devise_jwt_auth/url.rb +2 -4
- data/lib/devise_jwt_auth/version.rb +1 -1
- data/lib/generators/devise_jwt_auth/install_generator.rb +7 -6
- data/lib/generators/devise_jwt_auth/install_generator_helpers.rb +27 -6
- data/lib/generators/devise_jwt_auth/install_mongoid_generator.rb +3 -2
- data/lib/generators/devise_jwt_auth/templates/devise_jwt_auth.rb +19 -20
- data/lib/generators/devise_jwt_auth/templates/devise_jwt_auth_create_users.rb.erb +15 -11
- data/lib/generators/devise_jwt_auth/templates/user.rb.erb +2 -2
- data/test/controllers/custom/custom_confirmations_controller_test.rb +2 -2
- data/test/controllers/custom/custom_passwords_controller_test.rb +6 -6
- data/test/controllers/custom/custom_refresh_token_controller_test.rb +2 -3
- data/test/controllers/custom/custom_registrations_controller_test.rb +2 -2
- data/test/controllers/demo_mang_controller_test.rb +206 -210
- data/test/controllers/demo_user_controller_test.rb +358 -374
- data/test/controllers/devise_jwt_auth/confirmations_controller_test.rb +5 -5
- data/test/controllers/devise_jwt_auth/omniauth_callbacks_controller_test.rb +6 -7
- data/test/controllers/devise_jwt_auth/passwords_controller_test.rb +29 -30
- data/test/controllers/devise_jwt_auth/refresh_token_controller_test.rb +8 -12
- data/test/controllers/devise_jwt_auth/registrations_controller_test.rb +23 -25
- data/test/controllers/devise_jwt_auth/sessions_controller_test.rb +32 -34
- data/test/controllers/devise_jwt_auth/unlocks_controller_test.rb +2 -2
- data/test/controllers/overrides/confirmations_controller_test.rb +1 -1
- data/test/controllers/overrides/passwords_controller_test.rb +1 -1
- data/test/controllers/overrides/refresh_token_controller_test.rb +1 -2
- data/test/controllers/overrides/registrations_controller_test.rb +1 -1
- data/test/dummy/app/controllers/application_controller.rb +1 -1
- data/test/dummy/app/controllers/custom/refresh_token_controller.rb +2 -1
- data/test/dummy/app/controllers/custom/registrations_controller.rb +1 -1
- data/test/dummy/app/controllers/overrides/confirmations_controller.rb +3 -16
- data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +4 -4
- data/test/dummy/app/controllers/overrides/passwords_controller.rb +4 -16
- data/test/dummy/app/controllers/overrides/refresh_token_controller.rb +1 -1
- data/test/dummy/app/controllers/overrides/registrations_controller.rb +2 -2
- data/test/dummy/app/controllers/overrides/sessions_controller.rb +2 -2
- data/test/dummy/app/models/concerns/favorite_color.rb +11 -9
- data/test/dummy/config.ru +2 -2
- data/test/dummy/config/application.rb +1 -0
- data/test/dummy/config/boot.rb +1 -1
- data/test/dummy/config/environments/test.rb +11 -7
- data/test/dummy/config/initializers/figaro.rb +1 -1
- data/test/dummy/config/initializers/omniauth.rb +2 -2
- data/test/dummy/config/routes.rb +8 -8
- data/test/dummy/db/migrate/20141222035835_devise_jwt_auth_create_only_email_users.rb +9 -9
- data/test/dummy/db/migrate/20190924101113_devise_jwt_auth_create_confirmable_users.rb +6 -5
- data/test/dummy/db/schema.rb +170 -170
- data/test/dummy/tmp/generators/app/models/mang.rb +9 -0
- data/test/dummy/tmp/generators/app/models/{azpire/v1/human_resource/user.rb → user.rb} +3 -3
- data/test/dummy/tmp/generators/config/initializers/devise_jwt_auth.rb +19 -20
- data/test/dummy/tmp/generators/config/routes.rb +9 -0
- data/test/dummy/tmp/generators/db/migrate/{20200209222205_devise_jwt_auth_create_azpire_v1_human_resource_users.rb → 20201208044024_devise_jwt_auth_create_mangs.rb} +20 -17
- data/test/dummy/tmp/generators/db/migrate/20201208044024_devise_jwt_auth_create_users.rb +54 -0
- data/test/factories/users.rb +5 -3
- data/test/lib/devise_jwt_auth/token_factory_test.rb +6 -6
- data/test/lib/generators/devise_jwt_auth/install_generator_test.rb +3 -20
- data/test/lib/generators/devise_jwt_auth/install_generator_with_namespace_test.rb +4 -21
- data/test/models/concerns/tokens_serialization_test.rb +68 -68
- data/test/models/user_test.rb +35 -37
- data/test/support/controllers/routes.rb +7 -5
- data/test/test_helper.rb +1 -1
- metadata +56 -66
- data/test/dummy/tmp/generators/app/controllers/application_controller.rb +0 -6
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Mang < ActiveRecord::Base
|
4
|
+
# Include default devise modules. Others available are:
|
5
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
6
|
+
devise :database_authenticatable, :registerable,
|
7
|
+
:recoverable, :rememberable, :validatable
|
8
|
+
include DeviseJwtAuth::Concerns::User
|
9
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
class
|
3
|
+
class User < ActiveRecord::Base
|
4
4
|
# Include default devise modules. Others available are:
|
5
|
-
# :confirmable, :lockable, :timeoutable and :omniauthable
|
5
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
6
6
|
devise :database_authenticatable, :registerable,
|
7
|
-
:recoverable, :rememberable, :
|
7
|
+
:recoverable, :rememberable, :validatable
|
8
8
|
include DeviseJwtAuth::Concerns::User
|
9
9
|
end
|
@@ -5,38 +5,38 @@ DeviseJwtAuth.setup do |config|
|
|
5
5
|
# user. To receive new access tokens, you should either reauthenticate or
|
6
6
|
# use the HTTP only refresh cookie that is sent during the authentication
|
7
7
|
# process and make refresh token requests.
|
8
|
-
#
|
9
|
-
|
8
|
+
# config.send_new_access_token_on_each_request = false
|
9
|
+
|
10
10
|
# By default, refresh token HTTP Only cookies last for 2 weeks. These tokens
|
11
11
|
# are used for requesting shorter-lived acccess tokens.
|
12
|
-
#
|
12
|
+
# config.refresh_token_lifespan = 2.weeks
|
13
13
|
|
14
14
|
# By default, access tokens last for 15 minutes. These tokens are used to
|
15
15
|
# access protected resources. When these tokens expire, you need to
|
16
16
|
# reauthenticate the user or use a refresh token cookie to get a new access
|
17
17
|
# token.
|
18
|
-
#
|
18
|
+
# config.access_token_lifespan = 15.minutes
|
19
19
|
|
20
20
|
# This is the name of the HTTP Only cookie that will be sent to the client
|
21
21
|
# for the purpose of requesting new access tokens.
|
22
|
-
#
|
22
|
+
# config.refresh_token_name = 'refresh-token'
|
23
23
|
|
24
24
|
# This is the name of the token that will be sent in the JSON responses used
|
25
25
|
# for accessing protected resources. NEVER store this token in a cookie or
|
26
26
|
# any form of local storage on the client. Save it in memory as a javascript
|
27
27
|
# variable or in some kind of context manager like Redux. Send it in your
|
28
28
|
# request headers when you want to be authenticated.
|
29
|
-
#
|
29
|
+
# config.access_token_name = 'access-token'
|
30
30
|
|
31
31
|
# This is the refresh token encryption key. You should set this in an
|
32
|
-
# environment variable or secret key base that isn't
|
33
|
-
# Also, its a good idea to NOT use the same key for access tokens.
|
34
|
-
self.refresh_token_encryption_key = 'your-refresh-token-secret-key-here'
|
35
|
-
|
36
|
-
# This is the refresh token encryption key. You should set this in an
|
37
|
-
# environment variable or secret key base that isn't store in a repository.
|
32
|
+
# environment variable or secret key base that isn't stored in a repository.
|
38
33
|
# Also, its a good idea to NOT use the same key for access tokens.
|
39
|
-
|
34
|
+
config.refresh_token_encryption_key = 'your-refresh-token-secret-key-here'
|
35
|
+
|
36
|
+
# This is the access token encryption key. You should set this in an
|
37
|
+
# environment variable or secret key base that isn't stored in a repository.
|
38
|
+
# Also, its a good idea to NOT use the same key for refresh tokens.
|
39
|
+
config.access_token_encryption_key = 'your-access-token-secret-key-here'
|
40
40
|
|
41
41
|
# This route will be the prefix for all oauth2 redirect callbacks. For
|
42
42
|
# example, using the default '/omniauth', the github oauth2 provider will
|
@@ -64,11 +64,10 @@ DeviseJwtAuth.setup do |config|
|
|
64
64
|
# config.send_confirmation_email = true
|
65
65
|
|
66
66
|
# TODO: Document these settings
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
|
67
|
+
# config.default_confirm_success_url = nil
|
68
|
+
# config.default_password_reset_url = nil
|
69
|
+
# config.redirect_whitelist = nil
|
70
|
+
# config.update_token_version_after_password_reset = true
|
71
|
+
# config.bypass_sign_in = true
|
72
|
+
# config.require_client_password_reset_token = false
|
74
73
|
end
|
@@ -1,13 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
class
|
3
|
+
class DeviseJwtAuthCreateMangs < ActiveRecord::Migration[6.0]
|
4
4
|
def change
|
5
|
-
|
6
|
-
create_table(:azpire_v1_human_resource_users) do |t|
|
5
|
+
create_table(:mangs) do |t|
|
7
6
|
## Required
|
8
7
|
t.string :provider, null: false, default: 'email'
|
9
8
|
t.string :uid, null: false, default: ''
|
10
9
|
|
10
|
+
## User Info
|
11
|
+
t.string :name
|
12
|
+
t.string :nickname
|
13
|
+
t.string :image
|
14
|
+
t.string :email
|
15
|
+
|
11
16
|
## Database authenticatable
|
12
17
|
t.string :encrypted_password, null: false, default: ''
|
13
18
|
|
@@ -19,6 +24,13 @@ class DeviseJwtAuthCreateAzpireV1HumanResourceUsers < ActiveRecord::Migration[6.
|
|
19
24
|
## Rememberable
|
20
25
|
t.datetime :remember_created_at
|
21
26
|
|
27
|
+
## Trackable
|
28
|
+
# t.integer :sign_in_count, default: 0, null: false
|
29
|
+
# t.datetime :current_sign_in_at
|
30
|
+
# t.datetime :last_sign_in_at
|
31
|
+
# t.string :current_sign_in_ip
|
32
|
+
# t.string :last_sign_in_ip
|
33
|
+
|
22
34
|
## Confirmable
|
23
35
|
t.string :confirmation_token
|
24
36
|
t.datetime :confirmed_at
|
@@ -30,22 +42,13 @@ class DeviseJwtAuthCreateAzpireV1HumanResourceUsers < ActiveRecord::Migration[6.
|
|
30
42
|
# t.string :unlock_token # Only if unlock strategy is :email or :both
|
31
43
|
# t.datetime :locked_at
|
32
44
|
|
33
|
-
## User Info
|
34
|
-
t.string :name
|
35
|
-
t.string :nickname
|
36
|
-
t.string :image
|
37
|
-
t.string :email
|
38
|
-
|
39
|
-
## Tokens
|
40
|
-
t.text :tokens
|
41
|
-
|
42
45
|
t.timestamps
|
43
46
|
end
|
44
47
|
|
45
|
-
add_index :
|
46
|
-
add_index :
|
47
|
-
add_index :
|
48
|
-
add_index :
|
49
|
-
# add_index :
|
48
|
+
add_index :mangs, :email, unique: true
|
49
|
+
add_index :mangs, [:uid, :provider], unique: true
|
50
|
+
add_index :mangs, :reset_password_token, unique: true
|
51
|
+
add_index :mangs, :confirmation_token, unique: true
|
52
|
+
# add_index :mangs, :unlock_token, unique: true
|
50
53
|
end
|
51
54
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class DeviseJwtAuthCreateUsers < ActiveRecord::Migration[6.0]
|
4
|
+
def change
|
5
|
+
create_table(:users) do |t|
|
6
|
+
## Required
|
7
|
+
t.string :provider, null: false, default: 'email'
|
8
|
+
t.string :uid, null: false, default: ''
|
9
|
+
|
10
|
+
## User Info
|
11
|
+
t.string :name
|
12
|
+
t.string :nickname
|
13
|
+
t.string :image
|
14
|
+
t.string :email
|
15
|
+
|
16
|
+
## Database authenticatable
|
17
|
+
t.string :encrypted_password, null: false, default: ''
|
18
|
+
|
19
|
+
## Recoverable
|
20
|
+
t.string :reset_password_token
|
21
|
+
t.datetime :reset_password_sent_at
|
22
|
+
t.boolean :allow_password_change, default: false
|
23
|
+
|
24
|
+
## Rememberable
|
25
|
+
t.datetime :remember_created_at
|
26
|
+
|
27
|
+
## Trackable
|
28
|
+
# t.integer :sign_in_count, default: 0, null: false
|
29
|
+
# t.datetime :current_sign_in_at
|
30
|
+
# t.datetime :last_sign_in_at
|
31
|
+
# t.string :current_sign_in_ip
|
32
|
+
# t.string :last_sign_in_ip
|
33
|
+
|
34
|
+
## Confirmable
|
35
|
+
t.string :confirmation_token
|
36
|
+
t.datetime :confirmed_at
|
37
|
+
t.datetime :confirmation_sent_at
|
38
|
+
t.string :unconfirmed_email # Only if using reconfirmable
|
39
|
+
|
40
|
+
## Lockable
|
41
|
+
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
|
42
|
+
# t.string :unlock_token # Only if unlock strategy is :email or :both
|
43
|
+
# t.datetime :locked_at
|
44
|
+
|
45
|
+
t.timestamps
|
46
|
+
end
|
47
|
+
|
48
|
+
add_index :users, :email, unique: true
|
49
|
+
add_index :users, [:uid, :provider], unique: true
|
50
|
+
add_index :users, :reset_password_token, unique: true
|
51
|
+
add_index :users, :confirmation_token, unique: true
|
52
|
+
# add_index :users, :unlock_token, unique: true
|
53
|
+
end
|
54
|
+
end
|
data/test/factories/users.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
FactoryBot.define do
|
2
4
|
factory :user do
|
3
5
|
email { Faker::Internet.unique.safe_email }
|
@@ -13,13 +15,13 @@ FactoryBot.define do
|
|
13
15
|
end
|
14
16
|
|
15
17
|
trait :confirmed do
|
16
|
-
after(:create
|
18
|
+
after(:create, &:confirm)
|
17
19
|
end
|
18
20
|
|
19
21
|
# confirmation period is expired
|
20
22
|
trait :unconfirmed do
|
21
23
|
after(:create) do |user, evaluator|
|
22
|
-
user.update_attribute(:confirmation_sent_at, evaluator.allow_unconfirmed_period - 1.day
|
24
|
+
user.update_attribute(:confirmation_sent_at, evaluator.allow_unconfirmed_period - 1.day)
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
@@ -29,7 +31,7 @@ FactoryBot.define do
|
|
29
31
|
end
|
30
32
|
|
31
33
|
trait :locked do
|
32
|
-
after(:create
|
34
|
+
after(:create, &:lock_access!)
|
33
35
|
end
|
34
36
|
|
35
37
|
factory :lockable_user, class: 'LockableUser'
|
@@ -5,15 +5,15 @@ require 'test_helper'
|
|
5
5
|
class DeviseJwtAuth::TokenFactoryTest < ActiveSupport::TestCase
|
6
6
|
describe 'TokenFactory module' do
|
7
7
|
let(:tf) { DeviseJwtAuth::TokenFactory }
|
8
|
-
let(:token_regexp) {
|
9
|
-
|
8
|
+
let(:token_regexp) { %r{^[A-Za-z0-9\-_=]+\.[A-Za-z0-9\-_=]+\.?[A-Za-z0-9\-_.+/=]*$} }
|
9
|
+
|
10
10
|
it 'should be defined' do
|
11
11
|
assert_equal(tf.present?, true)
|
12
12
|
assert_kind_of(Module, tf)
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
describe 'interface' do
|
16
|
-
let(:payload) { { foo: 'test'} }
|
16
|
+
let(:payload) { { foo: 'test' } }
|
17
17
|
let(:future_exp) { (Time.zone.now + 1.hour).to_i }
|
18
18
|
let(:past_exp) { (Time.zone.now - 1.hour).to_i }
|
19
19
|
|
@@ -50,7 +50,7 @@ class DeviseJwtAuth::TokenFactoryTest < ActiveSupport::TestCase
|
|
50
50
|
result = tf.decode_refresh_token(token)
|
51
51
|
assert_nil result['exp']
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
it 'invalidates bogus token' do
|
55
55
|
result = tf.decode_refresh_token('bogus token')
|
56
56
|
assert_empty result
|
@@ -91,7 +91,7 @@ class DeviseJwtAuth::TokenFactoryTest < ActiveSupport::TestCase
|
|
91
91
|
result = tf.decode_access_token(token)
|
92
92
|
assert_nil result['exp']
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
it 'invalidates bogus token' do
|
96
96
|
result = tf.decode_access_token('bogus token')
|
97
97
|
assert_empty result
|
@@ -44,17 +44,6 @@ module DeviseJwtAuth
|
|
44
44
|
assert_migration 'db/migrate/devise_jwt_auth_create_users.rb'
|
45
45
|
end
|
46
46
|
end
|
47
|
-
|
48
|
-
=begin
|
49
|
-
test 'add primary key type with rails 5 when specified in rails generator' do
|
50
|
-
run_generator %w[--primary_key_type=uuid --force]
|
51
|
-
if Rails::VERSION::MAJOR >= 5
|
52
|
-
assert_migration 'db/migrate/devise_jwt_auth_create_users.rb', /create_table\(:users, id: :uuid\) do/
|
53
|
-
else
|
54
|
-
assert_migration 'db/migrate/devise_jwt_auth_create_users.rb', /create_table\(:users\) do/
|
55
|
-
end
|
56
|
-
end
|
57
|
-
=end
|
58
47
|
end
|
59
48
|
end
|
60
49
|
|
@@ -72,15 +61,12 @@ module DeviseJwtAuth
|
|
72
61
|
case DEVISE_JWT_AUTH_ORM
|
73
62
|
when :active_record
|
74
63
|
# account for rails version 5
|
75
|
-
active_record_needle =
|
64
|
+
active_record_needle = Rails::VERSION::MAJOR == 5 ? 'ApplicationRecord' : 'ActiveRecord::Base'
|
76
65
|
|
77
66
|
@f = File.open(@fname, 'w') do |f|
|
78
67
|
f.write <<-RUBY
|
79
68
|
class User < #{active_record_needle}
|
80
|
-
|
81
|
-
def whatever
|
82
|
-
puts 'whatever'
|
83
|
-
end
|
69
|
+
def whatever; puts 'whatever'; end
|
84
70
|
end
|
85
71
|
RUBY
|
86
72
|
end
|
@@ -88,10 +74,7 @@ module DeviseJwtAuth
|
|
88
74
|
@f = File.open(@fname, 'w') do |f|
|
89
75
|
f.write <<-'RUBY'
|
90
76
|
class User
|
91
|
-
|
92
|
-
def whatever
|
93
|
-
puts 'whatever'
|
94
|
-
end
|
77
|
+
def whatever; puts 'whatever'; end
|
95
78
|
end
|
96
79
|
RUBY
|
97
80
|
end
|
@@ -14,7 +14,7 @@ module DeviseJwtAuth
|
|
14
14
|
# The namespaced user model for testing
|
15
15
|
let(:user_class) { 'Azpire::V1::HumanResource::User' }
|
16
16
|
let(:namespace_path) { user_class.underscore }
|
17
|
-
let(:table_name) { user_class.pluralize.underscore.gsub('/','_') }
|
17
|
+
let(:table_name) { user_class.pluralize.underscore.gsub('/', '_') }
|
18
18
|
|
19
19
|
describe 'user model with namespace, clean install' do
|
20
20
|
setup :prepare_destination
|
@@ -49,17 +49,6 @@ module DeviseJwtAuth
|
|
49
49
|
assert_migration "db/migrate/devise_jwt_auth_create_#{table_name}.rb"
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
53
|
-
=begin
|
54
|
-
test 'add primary key type with rails 5 when specified in rails generator' do
|
55
|
-
run_generator %W[#{user_class} auth --primary_key_type=uuid --force]
|
56
|
-
if Rails::VERSION::MAJOR >= 5
|
57
|
-
assert_migration "db/migrate/devise_jwt_auth_create_#{table_name}.rb", /create_table\(:#{table_name}, id: :uuid\) do/
|
58
|
-
else
|
59
|
-
assert_migration "db/migrate/devise_jwt_auth_create_#{table_name}.rb", /create_table\(:#{table_name}\) do/
|
60
|
-
end
|
61
|
-
end
|
62
|
-
=end
|
63
52
|
end
|
64
53
|
end
|
65
54
|
|
@@ -77,15 +66,12 @@ module DeviseJwtAuth
|
|
77
66
|
case DEVISE_JWT_AUTH_ORM
|
78
67
|
when :active_record
|
79
68
|
# account for rails version 5
|
80
|
-
active_record_needle =
|
69
|
+
active_record_needle = Rails::VERSION::MAJOR == 5 ? 'ApplicationRecord' : 'ActiveRecord::Base'
|
81
70
|
|
82
71
|
@f = File.open(@fname, 'w') do |f|
|
83
72
|
f.write <<-RUBY
|
84
73
|
class User < #{active_record_needle}
|
85
|
-
|
86
|
-
def whatever
|
87
|
-
puts 'whatever'
|
88
|
-
end
|
74
|
+
def whatever; puts 'whatever'; end
|
89
75
|
end
|
90
76
|
RUBY
|
91
77
|
end
|
@@ -93,10 +79,7 @@ module DeviseJwtAuth
|
|
93
79
|
@f = File.open(@fname, 'w') do |f|
|
94
80
|
f.write <<-'RUBY'
|
95
81
|
class User
|
96
|
-
|
97
|
-
def whatever
|
98
|
-
puts 'whatever'
|
99
|
-
end
|
82
|
+
def whatever; puts 'whatever'; end
|
100
83
|
end
|
101
84
|
RUBY
|
102
85
|
end
|
@@ -1,72 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
5
|
if DEVISE_JWT_AUTH_ORM == :active_record
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
71
|
-
=end
|
6
|
+
# describe 'DeviseJwtAuth::Concerns::TokensSerialization' do
|
7
|
+
# let(:ts) { DeviseJwtAuth::Concerns::TokensSerialization }
|
8
|
+
# let(:user) { FactoryBot.create(:user) }
|
9
|
+
# let(:tokens) do
|
10
|
+
# # Create all possible token's attributes combinations
|
11
|
+
# user.create_token
|
12
|
+
# 2.times { user.create_new_auth_token(user.tokens.first[0]) }
|
13
|
+
# user.create_new_auth_token
|
14
|
+
# user.create_token
|
15
|
+
#
|
16
|
+
# user.tokens
|
17
|
+
# end
|
18
|
+
# let(:json) { JSON.generate(tokens) }
|
19
|
+
#
|
20
|
+
# it 'is defined' do
|
21
|
+
# assert_equal(ts.present?, true)
|
22
|
+
# assert_kind_of(Module, ts)
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# describe '.load(json)' do
|
26
|
+
# let(:default) { {} }
|
27
|
+
#
|
28
|
+
# it 'is defined' do
|
29
|
+
# assert_respond_to(ts, :load)
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# it 'handles nil' do
|
33
|
+
# assert_equal(ts.load(nil), default)
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# it 'handles string' do
|
37
|
+
# assert_equal(ts.load(json), JSON.parse(json))
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# it 'returns object of undesirable class' do
|
41
|
+
# assert_equal(ts.load([]), [])
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# describe '.dump(object)' do
|
46
|
+
# let(:default) { 'null' }
|
47
|
+
#
|
48
|
+
# it 'is defined' do
|
49
|
+
# assert_respond_to(ts, :dump)
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# it 'handles nil' do
|
53
|
+
# assert_equal(ts.dump(nil), default)
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# it 'handles empty hash' do
|
57
|
+
# assert_equal(ts.dump({}), '{}')
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# it 'deserialize tokens' do
|
61
|
+
# assert_equal(ts.dump(tokens), json)
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# it 'removes nil values' do
|
65
|
+
# new_tokens = tokens.dup
|
66
|
+
# new_tokens[new_tokens.first[0]][:kos] = nil
|
67
|
+
#
|
68
|
+
# assert_equal(ts.dump(tokens), ts.dump(new_tokens))
|
69
|
+
# end
|
70
|
+
# end
|
71
|
+
# end
|
72
72
|
end
|