devise_jwt_auth 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +13 -0
- data/README.md +99 -0
- data/Rakefile +42 -0
- data/app/controllers/devise_jwt_auth/application_controller.rb +80 -0
- data/app/controllers/devise_jwt_auth/concerns/resource_finder.rb +44 -0
- data/app/controllers/devise_jwt_auth/concerns/set_user_by_jwt_token.rb +111 -0
- data/app/controllers/devise_jwt_auth/confirmations_controller.rb +88 -0
- data/app/controllers/devise_jwt_auth/omniauth_callbacks_controller.rb +291 -0
- data/app/controllers/devise_jwt_auth/passwords_controller.rb +217 -0
- data/app/controllers/devise_jwt_auth/refresh_token_controller.rb +41 -0
- data/app/controllers/devise_jwt_auth/registrations_controller.rb +203 -0
- data/app/controllers/devise_jwt_auth/sessions_controller.rb +131 -0
- data/app/controllers/devise_jwt_auth/unlocks_controller.rb +99 -0
- data/app/models/devise_jwt_auth/concerns/active_record_support.rb +16 -0
- data/app/models/devise_jwt_auth/concerns/confirmable_support.rb +27 -0
- data/app/models/devise_jwt_auth/concerns/mongoid_support.rb +19 -0
- data/app/models/devise_jwt_auth/concerns/tokens_serialization.rb +19 -0
- data/app/models/devise_jwt_auth/concerns/user.rb +117 -0
- data/app/models/devise_jwt_auth/concerns/user_omniauth_callbacks.rb +28 -0
- data/app/validators/devise_jwt_auth_email_validator.rb +23 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise_jwt_auth/omniauth_external_window.html.erb +38 -0
- data/config/locales/da-DK.yml +52 -0
- data/config/locales/de.yml +51 -0
- data/config/locales/en.yml +57 -0
- data/config/locales/es.yml +51 -0
- data/config/locales/fr.yml +51 -0
- data/config/locales/he.yml +52 -0
- data/config/locales/it.yml +48 -0
- data/config/locales/ja.yml +48 -0
- data/config/locales/ko.yml +51 -0
- data/config/locales/nl.yml +32 -0
- data/config/locales/pl.yml +50 -0
- data/config/locales/pt-BR.yml +48 -0
- data/config/locales/pt.yml +50 -0
- data/config/locales/ro.yml +48 -0
- data/config/locales/ru.yml +52 -0
- data/config/locales/sq.yml +48 -0
- data/config/locales/sv.yml +52 -0
- data/config/locales/uk.yml +61 -0
- data/config/locales/vi.yml +52 -0
- data/config/locales/zh-CN.yml +48 -0
- data/config/locales/zh-HK.yml +50 -0
- data/config/locales/zh-TW.yml +50 -0
- data/lib/devise_jwt_auth.rb +14 -0
- data/lib/devise_jwt_auth/blacklist.rb +2 -0
- data/lib/devise_jwt_auth/controllers/helpers.rb +161 -0
- data/lib/devise_jwt_auth/controllers/url_helpers.rb +10 -0
- data/lib/devise_jwt_auth/engine.rb +96 -0
- data/lib/devise_jwt_auth/errors.rb +8 -0
- data/lib/devise_jwt_auth/rails/routes.rb +118 -0
- data/lib/devise_jwt_auth/token_factory.rb +51 -0
- data/lib/devise_jwt_auth/url.rb +44 -0
- data/lib/devise_jwt_auth/version.rb +5 -0
- data/lib/generators/devise_jwt_auth/USAGE +31 -0
- data/lib/generators/devise_jwt_auth/install_generator.rb +91 -0
- data/lib/generators/devise_jwt_auth/install_generator_helpers.rb +98 -0
- data/lib/generators/devise_jwt_auth/install_mongoid_generator.rb +46 -0
- data/lib/generators/devise_jwt_auth/install_views_generator.rb +18 -0
- data/lib/generators/devise_jwt_auth/templates/devise_jwt_auth.rb +74 -0
- data/lib/generators/devise_jwt_auth/templates/devise_jwt_auth_create_users.rb.erb +51 -0
- data/lib/generators/devise_jwt_auth/templates/user.rb.erb +9 -0
- data/lib/generators/devise_jwt_auth/templates/user_mongoid.rb.erb +56 -0
- data/lib/tasks/devise_token_auth_tasks.rake +6 -0
- data/test/controllers/custom/custom_confirmations_controller_test.rb +25 -0
- data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +33 -0
- data/test/controllers/custom/custom_passwords_controller_test.rb +79 -0
- data/test/controllers/custom/custom_refresh_token_controller_test.rb +36 -0
- data/test/controllers/custom/custom_registrations_controller_test.rb +59 -0
- data/test/controllers/custom/custom_sessions_controller_test.rb +39 -0
- data/test/controllers/demo_group_controller_test.rb +150 -0
- data/test/controllers/demo_mang_controller_test.rb +286 -0
- data/test/controllers/demo_user_controller_test.rb +650 -0
- data/test/controllers/devise_jwt_auth/confirmations_controller_test.rb +194 -0
- data/test/controllers/devise_jwt_auth/omniauth_callbacks_controller_test.rb +462 -0
- data/test/controllers/devise_jwt_auth/passwords_controller_test.rb +881 -0
- data/test/controllers/devise_jwt_auth/refresh_token_controller_test.rb +84 -0
- data/test/controllers/devise_jwt_auth/registrations_controller_test.rb +944 -0
- data/test/controllers/devise_jwt_auth/sessions_controller_test.rb +510 -0
- data/test/controllers/devise_jwt_auth/unlocks_controller_test.rb +197 -0
- data/test/controllers/overrides/confirmations_controller_test.rb +47 -0
- data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +53 -0
- data/test/controllers/overrides/passwords_controller_test.rb +65 -0
- data/test/controllers/overrides/refresh_token_controller_test.rb +37 -0
- data/test/controllers/overrides/registrations_controller_test.rb +47 -0
- data/test/controllers/overrides/sessions_controller_test.rb +35 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/app/active_record/confirmable_user.rb +11 -0
- data/test/dummy/app/active_record/lockable_user.rb +7 -0
- data/test/dummy/app/active_record/mang.rb +5 -0
- data/test/dummy/app/active_record/only_email_user.rb +7 -0
- data/test/dummy/app/active_record/scoped_user.rb +9 -0
- data/test/dummy/app/active_record/unconfirmable_user.rb +9 -0
- data/test/dummy/app/active_record/unregisterable_user.rb +9 -0
- data/test/dummy/app/active_record/user.rb +6 -0
- data/test/dummy/app/controllers/application_controller.rb +18 -0
- data/test/dummy/app/controllers/auth_origin_controller.rb +7 -0
- data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
- data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +13 -0
- data/test/dummy/app/controllers/custom/passwords_controller.rb +39 -0
- data/test/dummy/app/controllers/custom/refresh_token_controller.rb +20 -0
- data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
- data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
- data/test/dummy/app/controllers/demo_group_controller.rb +15 -0
- data/test/dummy/app/controllers/demo_mang_controller.rb +14 -0
- data/test/dummy/app/controllers/demo_user_controller.rb +27 -0
- data/test/dummy/app/controllers/overrides/confirmations_controller.rb +37 -0
- data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +16 -0
- data/test/dummy/app/controllers/overrides/passwords_controller.rb +45 -0
- data/test/dummy/app/controllers/overrides/refresh_token_controller.rb +22 -0
- data/test/dummy/app/controllers/overrides/registrations_controller.rb +29 -0
- data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
- data/test/dummy/app/helpers/application_helper.rb +1058 -0
- data/test/dummy/app/models/concerns/favorite_color.rb +19 -0
- data/test/dummy/app/mongoid/confirmable_user.rb +52 -0
- data/test/dummy/app/mongoid/lockable_user.rb +38 -0
- data/test/dummy/app/mongoid/mang.rb +46 -0
- data/test/dummy/app/mongoid/only_email_user.rb +33 -0
- data/test/dummy/app/mongoid/scoped_user.rb +50 -0
- data/test/dummy/app/mongoid/unconfirmable_user.rb +44 -0
- data/test/dummy/app/mongoid/unregisterable_user.rb +47 -0
- data/test/dummy/app/mongoid/user.rb +49 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config.ru +18 -0
- data/test/dummy/config/application.rb +48 -0
- data/test/dummy/config/application.yml.bk +0 -0
- data/test/dummy/config/boot.rb +11 -0
- data/test/dummy/config/environment.rb +7 -0
- data/test/dummy/config/environments/development.rb +46 -0
- data/test/dummy/config/environments/production.rb +84 -0
- data/test/dummy/config/environments/test.rb +50 -0
- data/test/dummy/config/initializers/assets.rb +10 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/test/dummy/config/initializers/devise.rb +290 -0
- data/test/dummy/config/initializers/devise_jwt_auth.rb +55 -0
- data/test/dummy/config/initializers/figaro.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
- data/test/dummy/config/initializers/inflections.rb +18 -0
- data/test/dummy/config/initializers/mime_types.rb +6 -0
- data/test/dummy/config/initializers/omniauth.rb +11 -0
- data/test/dummy/config/initializers/session_store.rb +5 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
- data/test/dummy/config/routes.rb +57 -0
- data/test/dummy/config/spring.rb +3 -0
- data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +58 -0
- data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +57 -0
- data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +8 -0
- data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +7 -0
- data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +55 -0
- data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +56 -0
- data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +56 -0
- data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +56 -0
- data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +56 -0
- data/test/dummy/db/migrate/20190924101113_devise_token_auth_create_confirmable_users.rb +49 -0
- data/test/dummy/db/schema.rb +198 -0
- data/test/dummy/lib/migration_database_helper.rb +43 -0
- data/test/dummy/tmp/generators/app/models/user.rb +9 -0
- data/test/dummy/tmp/generators/config/initializers/devise_jwt_auth.rb +74 -0
- data/test/dummy/tmp/generators/config/routes.rb +4 -0
- data/test/dummy/tmp/generators/db/migrate/20200206224309_devise_jwt_auth_create_users.rb +51 -0
- data/test/factories/users.rb +41 -0
- data/test/lib/devise_jwt_auth/blacklist_test.rb +11 -0
- data/test/lib/devise_jwt_auth/token_factory_test.rb +115 -0
- data/test/lib/devise_jwt_auth/url_test.rb +26 -0
- data/test/lib/generators/devise_jwt_auth/install_generator_test.rb +219 -0
- data/test/lib/generators/devise_jwt_auth/install_generator_with_namespace_test.rb +224 -0
- data/test/lib/generators/devise_jwt_auth/install_views_generator_test.rb +25 -0
- data/test/models/concerns/mongoid_support_test.rb +31 -0
- data/test/models/concerns/tokens_serialization_test.rb +72 -0
- data/test/models/confirmable_user_test.rb +35 -0
- data/test/models/only_email_user_test.rb +29 -0
- data/test/models/user_test.rb +110 -0
- data/test/support/controllers/routes.rb +43 -0
- data/test/test_helper.rb +91 -0
- metadata +503 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
require 'fileutils'
|
|
5
|
+
require 'generators/devise_jwt_auth/install_generator' if DEVISE_JWT_AUTH_ORM == :active_record
|
|
6
|
+
require 'generators/devise_jwt_auth/install_mongoid_generator' if DEVISE_JWT_AUTH_ORM == :mongoid
|
|
7
|
+
|
|
8
|
+
module DeviseJwtAuth
|
|
9
|
+
class InstallGeneratorTest < Rails::Generators::TestCase
|
|
10
|
+
tests InstallGenerator if DEVISE_JWT_AUTH_ORM == :active_record
|
|
11
|
+
tests InstallMongoidGenerator if DEVISE_JWT_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 DeviseJwtAuth::Concerns::User/, model)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
test 'initializer is created' do
|
|
33
|
+
assert_file 'config/initializers/devise_jwt_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_JWT_AUTH_ORM == :active_record
|
|
41
|
+
test 'migration is created for user model with namespace' do
|
|
42
|
+
assert_migration "db/migrate/devise_jwt_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_jwt_auth_create_#{table_name}.rb", /#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}/
|
|
48
|
+
else
|
|
49
|
+
assert_migration "db/migrate/devise_jwt_auth_create_#{table_name}.rb"
|
|
50
|
+
end
|
|
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
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
describe 'existing user model' do
|
|
67
|
+
setup :prepare_destination
|
|
68
|
+
|
|
69
|
+
before do
|
|
70
|
+
@dir = File.join(destination_root, 'app', 'models')
|
|
71
|
+
|
|
72
|
+
@fname = File.join(@dir, 'user.rb')
|
|
73
|
+
|
|
74
|
+
# make dir if not exists
|
|
75
|
+
FileUtils.mkdir_p(@dir)
|
|
76
|
+
|
|
77
|
+
case DEVISE_JWT_AUTH_ORM
|
|
78
|
+
when :active_record
|
|
79
|
+
# account for rails version 5
|
|
80
|
+
active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
|
|
81
|
+
|
|
82
|
+
@f = File.open(@fname, 'w') do |f|
|
|
83
|
+
f.write <<-RUBY
|
|
84
|
+
class User < #{active_record_needle}
|
|
85
|
+
|
|
86
|
+
def whatever
|
|
87
|
+
puts 'whatever'
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
RUBY
|
|
91
|
+
end
|
|
92
|
+
when :mongoid
|
|
93
|
+
@f = File.open(@fname, 'w') do |f|
|
|
94
|
+
f.write <<-'RUBY'
|
|
95
|
+
class User
|
|
96
|
+
|
|
97
|
+
def whatever
|
|
98
|
+
puts 'whatever'
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
RUBY
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
run_generator
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
test 'user concern is injected into existing model' do
|
|
109
|
+
assert_file 'app/models/user.rb' do |model|
|
|
110
|
+
assert_match(/include DeviseJwtAuth::Concerns::User/, model)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
test 'subsequent runs do not modify file' do
|
|
115
|
+
run_generator
|
|
116
|
+
assert_file 'app/models/user.rb' do |model|
|
|
117
|
+
matches = model.scan(/include DeviseJwtAuth::Concerns::User/m).size
|
|
118
|
+
assert_equal 1, matches
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
describe 'routes' do
|
|
124
|
+
setup :prepare_destination
|
|
125
|
+
|
|
126
|
+
before do
|
|
127
|
+
@dir = File.join(destination_root, 'config')
|
|
128
|
+
|
|
129
|
+
@fname = File.join(@dir, 'routes.rb')
|
|
130
|
+
|
|
131
|
+
# make dir if not exists
|
|
132
|
+
FileUtils.mkdir_p(@dir)
|
|
133
|
+
|
|
134
|
+
@f = File.open(@fname, 'w') do |f|
|
|
135
|
+
f.write <<-RUBY
|
|
136
|
+
Rails.application.routes.draw do
|
|
137
|
+
patch '/chong', to: 'bong#index'
|
|
138
|
+
end
|
|
139
|
+
RUBY
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
run_generator %W[#{user_class} auth]
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
test 'route method for user model with namespace is appended to routes file' do
|
|
146
|
+
assert_file 'config/routes.rb' do |routes|
|
|
147
|
+
assert_match(/mount_devise_jwt_auth_for '#{user_class}', at: 'auth'/, routes)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
test 'subsequent runs do not modify file' do
|
|
152
|
+
run_generator %W[#{user_class} auth]
|
|
153
|
+
assert_file 'config/routes.rb' do |routes|
|
|
154
|
+
matches = routes.scan(/mount_devise_jwt_auth_for '#{user_class}', at: 'auth'/m).size
|
|
155
|
+
assert_equal 1, matches
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
describe 'subsequent models' do
|
|
160
|
+
before do
|
|
161
|
+
run_generator %w[Mang mangs]
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
test 'route method is appended to routes file' do
|
|
165
|
+
assert_file 'config/routes.rb' do |routes|
|
|
166
|
+
assert_match(/mount_devise_jwt_auth_for 'Mang', at: 'mangs'/, routes)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
test 'devise_for block is appended to routes file' do
|
|
171
|
+
assert_file 'config/routes.rb' do |routes|
|
|
172
|
+
assert_match(/as :mang do/, routes)
|
|
173
|
+
assert_match(/# Define routes for Mang within this block./, routes)
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
if DEVISE_JWT_AUTH_ORM == :active_record
|
|
178
|
+
test 'migration is created' do
|
|
179
|
+
assert_migration 'db/migrate/devise_jwt_auth_create_mangs.rb'
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
describe 'application controller' do
|
|
186
|
+
setup :prepare_destination
|
|
187
|
+
|
|
188
|
+
before do
|
|
189
|
+
@dir = File.join(destination_root, 'app', 'controllers')
|
|
190
|
+
|
|
191
|
+
@fname = File.join(@dir, 'application_controller.rb')
|
|
192
|
+
|
|
193
|
+
# make dir if not exists
|
|
194
|
+
FileUtils.mkdir_p(@dir)
|
|
195
|
+
|
|
196
|
+
@f = File.open(@fname, 'w') do |f|
|
|
197
|
+
f.write <<-RUBY
|
|
198
|
+
class ApplicationController < ActionController::Base
|
|
199
|
+
def whatever
|
|
200
|
+
'whatever'
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
RUBY
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
run_generator %W[#{user_class} auth]
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
test 'controller concern is appended to application controller' do
|
|
210
|
+
assert_file 'app/controllers/application_controller.rb' do |controller|
|
|
211
|
+
assert_match(/include DeviseJwtAuth::Concerns::SetUserByToken/, controller)
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
test 'subsequent runs do not modify file' do
|
|
216
|
+
run_generator %W[#{user_class} auth]
|
|
217
|
+
assert_file 'app/controllers/application_controller.rb' do |controller|
|
|
218
|
+
matches = controller.scan(/include DeviseJwtAuth::Concerns::SetUserByToken/m).size
|
|
219
|
+
assert_equal 1, matches
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
require 'fileutils'
|
|
5
|
+
require 'generators/devise_jwt_auth/install_views_generator'
|
|
6
|
+
|
|
7
|
+
module DeviseJwtAuth
|
|
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_JWT_AUTH_ORM == :mongoid
|
|
6
|
+
class DeviseJwtAuth::Concerns::MongoidSupportTest < ActiveSupport::TestCase
|
|
7
|
+
describe DeviseJwtAuth::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,72 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
if DEVISE_JWT_AUTH_ORM == :active_record
|
|
4
|
+
=begin
|
|
5
|
+
describe 'DeviseJwtAuth::Concerns::TokensSerialization' do
|
|
6
|
+
let(:ts) { DeviseJwtAuth::Concerns::TokensSerialization }
|
|
7
|
+
let(:user) { FactoryBot.create(:user) }
|
|
8
|
+
let(:tokens) do
|
|
9
|
+
# Сreate all possible token's attributes combinations
|
|
10
|
+
user.create_token
|
|
11
|
+
2.times { user.create_new_auth_token(user.tokens.first[0]) }
|
|
12
|
+
user.create_new_auth_token
|
|
13
|
+
user.create_token
|
|
14
|
+
|
|
15
|
+
user.tokens
|
|
16
|
+
end
|
|
17
|
+
let(:json) { JSON.generate(tokens) }
|
|
18
|
+
|
|
19
|
+
it 'is defined' do
|
|
20
|
+
assert_equal(ts.present?, true)
|
|
21
|
+
assert_kind_of(Module, ts)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe '.load(json)' do
|
|
25
|
+
let(:default) { {} }
|
|
26
|
+
|
|
27
|
+
it 'is defined' do
|
|
28
|
+
assert_respond_to(ts, :load)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'handles nil' do
|
|
32
|
+
assert_equal(ts.load(nil), default)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it 'handles string' do
|
|
36
|
+
assert_equal(ts.load(json), JSON.parse(json))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it 'returns object of undesirable class' do
|
|
40
|
+
assert_equal(ts.load([]), [])
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
describe '.dump(object)' do
|
|
45
|
+
let(:default) { 'null' }
|
|
46
|
+
|
|
47
|
+
it 'is defined' do
|
|
48
|
+
assert_respond_to(ts, :dump)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'handles nil' do
|
|
52
|
+
assert_equal(ts.dump(nil), default)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'handles empty hash' do
|
|
56
|
+
assert_equal(ts.dump({}), '{}')
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it 'deserialize tokens' do
|
|
60
|
+
assert_equal(ts.dump(tokens), json)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'removes nil values' do
|
|
64
|
+
new_tokens = tokens.dup
|
|
65
|
+
new_tokens[new_tokens.first[0]][:kos] = nil
|
|
66
|
+
|
|
67
|
+
assert_equal(ts.dump(tokens), ts.dump(new_tokens))
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
=end
|
|
72
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
class ConfirmableUserTest < ActiveSupport::TestCase
|
|
6
|
+
describe ConfirmableUser do
|
|
7
|
+
describe 'creation' do
|
|
8
|
+
test 'email should be saved' do
|
|
9
|
+
@resource = create(:confirmable_user)
|
|
10
|
+
assert @resource.email.present?
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe 'updating email' do
|
|
15
|
+
test 'new email should be saved to unconfirmed_email' do
|
|
16
|
+
@resource = create(:confirmable_user, email: 'old_address@example.com')
|
|
17
|
+
@resource.update(email: 'new_address@example.com')
|
|
18
|
+
assert @resource.unconfirmed_email == 'new_address@example.com'
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'old email should be kept in email' do
|
|
22
|
+
@resource = create(:confirmable_user, email: 'old_address@example.com')
|
|
23
|
+
@resource.update(email: 'new_address@example.com')
|
|
24
|
+
assert @resource.email == 'old_address@example.com'
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test 'confirmation_token should be changed' do
|
|
28
|
+
@resource = create(:confirmable_user, email: 'old_address@example.com')
|
|
29
|
+
old_token = @resource.confirmation_token
|
|
30
|
+
@resource.update(email: 'new_address@example.com')
|
|
31
|
+
assert @resource.confirmation_token != old_token
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
class OnlyEmailUserTest < ActiveSupport::TestCase
|
|
6
|
+
describe OnlyEmailUser do
|
|
7
|
+
test 'confirmable is disabled' do
|
|
8
|
+
refute OnlyEmailUser.method_defined?(:confirmation_token)
|
|
9
|
+
refute OnlyEmailUser.method_defined?(:confirmed_at)
|
|
10
|
+
refute OnlyEmailUser.method_defined?(:confirmation_sent_at)
|
|
11
|
+
refute OnlyEmailUser.method_defined?(:unconfirmed_email)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test 'lockable is disabled' do
|
|
15
|
+
refute OnlyEmailUser.method_defined?(:failed_attempts)
|
|
16
|
+
refute OnlyEmailUser.method_defined?(:unlock_token)
|
|
17
|
+
refute OnlyEmailUser.method_defined?(:locked_at)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
test 'recoverable is disabled' do
|
|
21
|
+
refute OnlyEmailUser.method_defined?(:reset_password_token)
|
|
22
|
+
refute OnlyEmailUser.method_defined?(:reset_password_sent_at)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
test 'rememberable is disabled' do
|
|
26
|
+
refute OnlyEmailUser.method_defined?(:remember_created_at)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
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
|
+
=begin
|
|
62
|
+
describe 'token expiry' do
|
|
63
|
+
before do
|
|
64
|
+
@resource = create(:user, :confirmed)
|
|
65
|
+
# TODO: expire this token?
|
|
66
|
+
@auth_headers = @resource.create_named_token_pair
|
|
67
|
+
|
|
68
|
+
# @token = @auth_headers['access-token']
|
|
69
|
+
# @client_id = @auth_headers['client']
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
test 'should properly indicate whether token is current' do
|
|
73
|
+
assert @resource.token_is_current?(@token, @client_id)
|
|
74
|
+
# we want to update the expiry without forcing a cleanup (see below)
|
|
75
|
+
@resource.tokens[@client_id]['expiry'] = Time.zone.now.to_i - 10.seconds
|
|
76
|
+
refute @resource.token_is_current?(@token, @client_id)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
describe 'expired tokens are destroyed on save' do
|
|
81
|
+
before do
|
|
82
|
+
@resource = create(:user, :confirmed)
|
|
83
|
+
|
|
84
|
+
@old_auth_headers = @resource.create_new_auth_token
|
|
85
|
+
@new_auth_headers = @resource.create_new_auth_token
|
|
86
|
+
expire_token(@resource, @old_auth_headers['client'])
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
test 'expired token was removed' do
|
|
90
|
+
refute @resource.tokens[@old_auth_headers[:client]]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
test 'current token was not removed' do
|
|
94
|
+
assert @resource.tokens[@new_auth_headers['client']]
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
=end
|
|
98
|
+
|
|
99
|
+
describe 'nil tokens are handled properly' do
|
|
100
|
+
before do
|
|
101
|
+
@resource = create(:user, :confirmed)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
test 'tokens can be set to nil' do
|
|
105
|
+
@resource.tokens = nil
|
|
106
|
+
assert @resource.save
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|