devise_token_auth_multi_email 0.9.4 → 0.9.5
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 +4 -4
- data/README.md +3 -0
- data/Rakefile +4 -8
- data/app/controllers/devise_token_auth/concerns/resource_finder.rb +2 -9
- data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +35 -5
- data/app/controllers/devise_token_auth/registrations_controller.rb +24 -18
- data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +6 -3
- data/lib/devise_token_auth/engine.rb +40 -1
- data/lib/devise_token_auth/version.rb +1 -1
- data/test/controllers/devise_token_auth/confirmations_controller_test.rb +4 -4
- data/test/controllers/devise_token_auth/multi_email_coexistence_test.rb +130 -0
- data/test/controllers/devise_token_auth/multi_email_confirmations_controller_test.rb +210 -0
- data/test/controllers/devise_token_auth/multi_email_passwords_controller_test.rb +247 -0
- data/test/controllers/devise_token_auth/multi_email_registrations_controller_test.rb +137 -0
- data/test/controllers/devise_token_auth/multi_email_sessions_controller_test.rb +191 -0
- data/test/controllers/devise_token_auth/multi_email_token_validations_controller_test.rb +140 -0
- data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +5 -4
- data/test/controllers/devise_token_auth/standard_user_registrations_controller_test.rb +165 -0
- data/test/coverage/assets/0.13.2/colorbox/loading.gif +0 -0
- data/test/coverage/assets/0.13.2/loading.gif +0 -0
- data/test/dummy/app/active_record/multi_email_user.rb +45 -0
- data/test/dummy/app/active_record/multi_email_user_email.rb +21 -0
- data/test/dummy/config/application.rb +6 -1
- data/test/dummy/config/initializers/omniauth.rb +15 -1
- data/test/dummy/config/routes.rb +8 -0
- data/test/dummy/db/migrate/20260401000001_devise_token_auth_create_multi_email_users.rb +49 -0
- data/test/dummy/db/migrate/20260401000002_devise_token_auth_create_multi_email_user_emails.rb +29 -0
- data/test/dummy/db/schema.rb +81 -41
- data/test/dummy/db/test.sqlite3-shm +0 -0
- data/test/dummy/tmp/generators/app/controllers/application_controller.rb +6 -0
- data/test/dummy/tmp/generators/app/models/{user.rb → azpire/v1/human_resource/user.rb} +1 -1
- data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +11 -5
- data/test/dummy/tmp/generators/db/migrate/{20210305040222_devise_token_auth_create_users.rb → 20260408021432_devise_token_auth_create_azpire_v1_human_resource_users.rb} +7 -7
- data/test/factories/users.rb +1 -0
- data/test/lib/devise_token_auth/controllers/helpers_test.rb +402 -0
- data/test/lib/devise_token_auth/token_factory_test.rb +18 -18
- data/test/lib/generators/devise_token_auth/install_generator_test.rb +60 -0
- data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +1 -1
- data/test/lib/generators/devise_token_auth/install_mongoid_generator_test.rb +218 -0
- data/test/models/multi_email_user_email_test.rb +95 -0
- data/test/models/multi_email_user_test.rb +225 -0
- data/test/test_helper.rb +21 -11
- data/test/validators/devise_token_auth_email_validator_test.rb +114 -0
- metadata +58 -27
- data/test/dummy/tmp/generators/app/models/mang.rb +0 -9
- data/test/dummy/tmp/generators/config/routes.rb +0 -9
- data/test/dummy/tmp/generators/db/migrate/20210305040222_devise_token_auth_create_mangs.rb +0 -49
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
# Tests that verify the PasswordsController works correctly with a MultiEmailUser —
|
|
6
|
+
# a model that uses :multi_email_authenticatable from the devise-multi_email gem.
|
|
7
|
+
#
|
|
8
|
+
# Password reset for multi-email users works via the uid column (synced with the
|
|
9
|
+
# primary email by the sync_uid before_save callback) so the standard password
|
|
10
|
+
# reset flow applies. The reset token is sent to the user's primary email address
|
|
11
|
+
# via the emails association.
|
|
12
|
+
#
|
|
13
|
+
# These tests are ActiveRecord-only — the MultiEmailUser model and its route
|
|
14
|
+
# are not available in Mongoid runs.
|
|
15
|
+
return unless DEVISE_TOKEN_AUTH_ORM == :active_record
|
|
16
|
+
|
|
17
|
+
class MultiEmailPasswordsControllerTest < ActionDispatch::IntegrationTest
|
|
18
|
+
RESET_PASSWORD = 'NewPassword123!'
|
|
19
|
+
|
|
20
|
+
describe 'MultiEmailUser passwords' do
|
|
21
|
+
def registration_params(email: nil)
|
|
22
|
+
{
|
|
23
|
+
email: email || Faker::Internet.unique.email,
|
|
24
|
+
password: 'secret123',
|
|
25
|
+
password_confirmation: 'secret123',
|
|
26
|
+
confirm_success_url: Faker::Internet.url
|
|
27
|
+
}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Create a confirmed MultiEmailUser through the endpoint, confirm it, and
|
|
31
|
+
# return the user record and email address used.
|
|
32
|
+
def create_confirmed_user(email: nil)
|
|
33
|
+
email ||= Faker::Internet.unique.email
|
|
34
|
+
post '/multi_email_auth', params: registration_params(email: email)
|
|
35
|
+
assert_equal 200, response.status, "Setup registration failed: #{response.body}"
|
|
36
|
+
user = assigns(:resource)
|
|
37
|
+
user.confirm
|
|
38
|
+
[user, email]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
before do
|
|
42
|
+
@redirect_url = 'http://ng-token-auth.dev'
|
|
43
|
+
@user, @email = create_confirmed_user
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# -----------------------------------------------------------------------
|
|
47
|
+
# Create — missing email
|
|
48
|
+
# -----------------------------------------------------------------------
|
|
49
|
+
describe 'missing email param' do
|
|
50
|
+
before do
|
|
51
|
+
post '/multi_email_auth/password', params: { redirect_url: @redirect_url }
|
|
52
|
+
@data = JSON.parse(response.body)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
test 'request fails' do
|
|
56
|
+
assert_equal 401, response.status
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
test 'missing email error is returned' do
|
|
60
|
+
assert @data['errors']
|
|
61
|
+
assert_equal [I18n.t('devise_token_auth.passwords.missing_email')],
|
|
62
|
+
@data['errors']
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# -----------------------------------------------------------------------
|
|
67
|
+
# Create — missing redirect_url
|
|
68
|
+
# -----------------------------------------------------------------------
|
|
69
|
+
describe 'missing redirect_url param' do
|
|
70
|
+
before do
|
|
71
|
+
post '/multi_email_auth/password', params: { email: @email }
|
|
72
|
+
@data = JSON.parse(response.body)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
test 'request fails' do
|
|
76
|
+
assert_equal 401, response.status
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
test 'missing redirect_url error is returned' do
|
|
80
|
+
assert @data['errors']
|
|
81
|
+
assert_equal [I18n.t('devise_token_auth.passwords.missing_redirect_url')],
|
|
82
|
+
@data['errors']
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# -----------------------------------------------------------------------
|
|
87
|
+
# Create — unknown email (without paranoid mode)
|
|
88
|
+
# -----------------------------------------------------------------------
|
|
89
|
+
describe 'unknown email without paranoid mode' do
|
|
90
|
+
before do
|
|
91
|
+
post '/multi_email_auth/password',
|
|
92
|
+
params: { email: 'unknown@example.com', redirect_url: @redirect_url }
|
|
93
|
+
@data = JSON.parse(response.body)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
test 'request fails with 404' do
|
|
97
|
+
assert_equal 404, response.status
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
test 'user not found error is returned' do
|
|
101
|
+
assert @data['errors']
|
|
102
|
+
assert_equal [I18n.t('devise_token_auth.passwords.user_not_found',
|
|
103
|
+
email: 'unknown@example.com')],
|
|
104
|
+
@data['errors']
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# -----------------------------------------------------------------------
|
|
109
|
+
# Create — unknown email (with paranoid mode)
|
|
110
|
+
# -----------------------------------------------------------------------
|
|
111
|
+
describe 'unknown email with paranoid mode' do
|
|
112
|
+
before do
|
|
113
|
+
swap Devise, paranoid: true do
|
|
114
|
+
post '/multi_email_auth/password',
|
|
115
|
+
params: { email: 'unknown@example.com', redirect_url: @redirect_url }
|
|
116
|
+
@data = JSON.parse(response.body)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
test 'request returns 200 to hide existence' do
|
|
121
|
+
assert_equal 200, response.status
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
test 'paranoid success message is returned' do
|
|
125
|
+
assert_equal I18n.t('devise_token_auth.passwords.sended_paranoid'),
|
|
126
|
+
@data['message']
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# -----------------------------------------------------------------------
|
|
131
|
+
# Create — successful password reset request
|
|
132
|
+
# -----------------------------------------------------------------------
|
|
133
|
+
describe 'successful password reset request' do
|
|
134
|
+
before do
|
|
135
|
+
@mail_count = ActionMailer::Base.deliveries.count
|
|
136
|
+
|
|
137
|
+
post '/multi_email_auth/password',
|
|
138
|
+
params: { email: @email, redirect_url: @redirect_url }
|
|
139
|
+
@data = JSON.parse(response.body)
|
|
140
|
+
@mail = ActionMailer::Base.deliveries.last
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
test 'request is successful' do
|
|
144
|
+
assert_equal 200, response.status
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
test 'success message is returned' do
|
|
148
|
+
assert_equal I18n.t('devise_token_auth.passwords.sended', email: @email),
|
|
149
|
+
@data['message']
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
test 'response does not include extra data' do
|
|
153
|
+
assert_nil @data['data']
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
test 'a password reset email is sent' do
|
|
157
|
+
assert_equal @mail_count + 1, ActionMailer::Base.deliveries.count
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
test 'email is addressed to the user' do
|
|
161
|
+
assert_equal @email, @mail['to'].to_s
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
test 'email body includes a reset password token' do
|
|
165
|
+
assert @mail.body.match(/reset_password_token=/)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
test 'email body includes the redirect URL' do
|
|
169
|
+
assert @mail.body.match(/redirect_url=/)
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# -----------------------------------------------------------------------
|
|
174
|
+
# Create — successful request with paranoid mode
|
|
175
|
+
# -----------------------------------------------------------------------
|
|
176
|
+
describe 'successful password reset request with paranoid mode' do
|
|
177
|
+
before do
|
|
178
|
+
swap Devise, paranoid: true do
|
|
179
|
+
post '/multi_email_auth/password',
|
|
180
|
+
params: { email: @email, redirect_url: @redirect_url }
|
|
181
|
+
@data = JSON.parse(response.body)
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
test 'request returns 200' do
|
|
186
|
+
assert_equal 200, response.status
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
test 'paranoid success message is returned' do
|
|
190
|
+
assert_equal I18n.t('devise_token_auth.passwords.sended_paranoid'),
|
|
191
|
+
@data['message']
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# -----------------------------------------------------------------------
|
|
196
|
+
# Update — successful password change via auth token
|
|
197
|
+
# -----------------------------------------------------------------------
|
|
198
|
+
describe 'password update via auth headers' do
|
|
199
|
+
before do
|
|
200
|
+
# Request the reset link so the user gets a reset token
|
|
201
|
+
post '/multi_email_auth/password',
|
|
202
|
+
params: { email: @email, redirect_url: @redirect_url }
|
|
203
|
+
assert_equal 200, response.status, "Reset request failed: #{response.body}"
|
|
204
|
+
|
|
205
|
+
@reset_mail = ActionMailer::Base.deliveries.last
|
|
206
|
+
@reset_token = @reset_mail.body.match(/reset_password_token=(.*)\"/)[1]
|
|
207
|
+
@redirect_url_encoded = CGI.unescape(
|
|
208
|
+
@reset_mail.body.match(/redirect_url=([^&]*)&/)[1]
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
# Follow the edit link to obtain auth headers
|
|
212
|
+
get '/multi_email_auth/password/edit',
|
|
213
|
+
params: { reset_password_token: @reset_token, redirect_url: @redirect_url }
|
|
214
|
+
|
|
215
|
+
raw_qs = response.location.split('?')[1]
|
|
216
|
+
qs = Rack::Utils.parse_nested_query(raw_qs)
|
|
217
|
+
|
|
218
|
+
@auth_headers = {
|
|
219
|
+
'access-token' => qs['access-token'] || qs['token'],
|
|
220
|
+
'client' => qs['client'] || qs['client_id'],
|
|
221
|
+
'uid' => qs['uid'],
|
|
222
|
+
'token-type' => 'Bearer'
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
# Update to a new password
|
|
226
|
+
put '/multi_email_auth/password',
|
|
227
|
+
params: { password: RESET_PASSWORD, password_confirmation: RESET_PASSWORD },
|
|
228
|
+
headers: @auth_headers
|
|
229
|
+
@data = JSON.parse(response.body)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
test 'password update is successful' do
|
|
233
|
+
assert_equal 200, response.status
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
test 'success flag is true' do
|
|
237
|
+
assert @data['success']
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
test 'user can sign in with new password' do
|
|
241
|
+
post '/multi_email_auth/sign_in',
|
|
242
|
+
params: { email: @email, password: RESET_PASSWORD }
|
|
243
|
+
assert_equal 200, response.status
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
# Tests that verify devise_token_auth works correctly with a **multi-email**
|
|
6
|
+
# model — one that uses :multi_email_authenticatable from the devise-multi_email gem.
|
|
7
|
+
#
|
|
8
|
+
# With multi_email active on a model:
|
|
9
|
+
# • Email uniqueness is managed through the emails association/table, NOT by
|
|
10
|
+
# a column-level validation on the model itself.
|
|
11
|
+
# • After registration, an email record exists in multi_email_user_emails.
|
|
12
|
+
# • The user's primary email is accessible via the emails association.
|
|
13
|
+
# • Duplicate email registration is still rejected (enforced by the emails
|
|
14
|
+
# table unique index and Devise::MultiEmail's own validation).
|
|
15
|
+
#
|
|
16
|
+
# These tests are ActiveRecord-only — the MultiEmailUser model and its
|
|
17
|
+
# devise-multi_email setup are not available in Mongoid runs.
|
|
18
|
+
return unless DEVISE_TOKEN_AUTH_ORM == :active_record
|
|
19
|
+
|
|
20
|
+
class MultiEmailRegistrationsControllerTest < ActionDispatch::IntegrationTest
|
|
21
|
+
describe 'MultiEmailUser (with :multi_email_authenticatable)' do
|
|
22
|
+
def registration_params(email: nil)
|
|
23
|
+
{
|
|
24
|
+
email: email || Faker::Internet.unique.email,
|
|
25
|
+
password: 'secret123',
|
|
26
|
+
password_confirmation: 'secret123',
|
|
27
|
+
confirm_success_url: Faker::Internet.url
|
|
28
|
+
}
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# -----------------------------------------------------------------------
|
|
32
|
+
# Model configuration sanity checks
|
|
33
|
+
# -----------------------------------------------------------------------
|
|
34
|
+
describe 'model configuration' do
|
|
35
|
+
test 'MultiEmailUser has multi_email_association class method' do
|
|
36
|
+
# Added by Devise::MultiEmail::ParentModelExtensions via :multi_email_authenticatable
|
|
37
|
+
assert MultiEmailUser.respond_to?(:multi_email_association)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
test 'MultiEmailUser has the emails association' do
|
|
41
|
+
assert MultiEmailUser.reflect_on_association(:emails)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
test 'MultiEmailUser has find_by_email class method' do
|
|
45
|
+
# Added by Devise::Models::MultiEmailAuthenticatable::ClassMethods
|
|
46
|
+
assert MultiEmailUser.respond_to?(:find_by_email)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
test 'MultiEmailUser does NOT carry the concern uniqueness validator' do
|
|
50
|
+
# email uniqueness is handled by the emails table, not the user model
|
|
51
|
+
refute MultiEmailUser.validators_on(:email).any? { |v|
|
|
52
|
+
v.is_a?(ActiveRecord::Validations::UniquenessValidator)
|
|
53
|
+
}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
test 'MultiEmailUserEmail has email uniqueness validator from EmailValidatable' do
|
|
57
|
+
# Devise::Models::EmailValidatable is included into MultiEmailUserEmail
|
|
58
|
+
# automatically by MultiEmailUser's ParentModelExtensions when the
|
|
59
|
+
# :multi_email_validatable module is set up.
|
|
60
|
+
#
|
|
61
|
+
# Ensure MultiEmailUser is loaded to trigger the association setup:
|
|
62
|
+
MultiEmailUser
|
|
63
|
+
|
|
64
|
+
assert MultiEmailUserEmail.validators_on(:email).any? { |v|
|
|
65
|
+
v.is_a?(ActiveRecord::Validations::UniquenessValidator)
|
|
66
|
+
}, 'Expected UniquenessValidator on MultiEmailUserEmail#email (from EmailValidatable)'
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# -----------------------------------------------------------------------
|
|
71
|
+
# Successful registration
|
|
72
|
+
# -----------------------------------------------------------------------
|
|
73
|
+
describe 'successful registration' do
|
|
74
|
+
before do
|
|
75
|
+
@email = Faker::Internet.unique.email
|
|
76
|
+
post '/multi_email_auth', params: registration_params(email: @email)
|
|
77
|
+
@resource = assigns(:resource)
|
|
78
|
+
@data = JSON.parse(response.body)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
test 'request is successful' do
|
|
82
|
+
assert_equal 200, response.status
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
test 'resource is a MultiEmailUser' do
|
|
86
|
+
assert_equal MultiEmailUser, @resource.class
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
test 'user is persisted' do
|
|
90
|
+
assert @resource.id
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
test 'response includes email' do
|
|
94
|
+
assert @data['data']['email']
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
test 'an email record is created in the emails association' do
|
|
98
|
+
assert_equal 1, @resource.emails.count
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
test 'primary email record is marked correctly' do
|
|
102
|
+
email_record = @resource.emails.first
|
|
103
|
+
assert email_record.primary?
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
test 'the email record stores the registered email' do
|
|
107
|
+
assert_equal @email, @resource.emails.first.email
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# -----------------------------------------------------------------------
|
|
112
|
+
# Duplicate email registration is rejected
|
|
113
|
+
# -----------------------------------------------------------------------
|
|
114
|
+
describe 'duplicate email registration' do
|
|
115
|
+
before do
|
|
116
|
+
@email = Faker::Internet.unique.email
|
|
117
|
+
|
|
118
|
+
# Register the first user via the endpoint (not factory), since
|
|
119
|
+
# the gem handles email association creation on save internally.
|
|
120
|
+
post '/multi_email_auth', params: registration_params(email: @email)
|
|
121
|
+
assert_equal 200, response.status, "Setup registration failed: #{response.body}"
|
|
122
|
+
|
|
123
|
+
# Attempt a duplicate registration
|
|
124
|
+
post '/multi_email_auth', params: registration_params(email: @email)
|
|
125
|
+
@data = JSON.parse(response.body)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
test 'request is rejected' do
|
|
129
|
+
assert_equal 422, response.status
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
test 'errors are returned' do
|
|
133
|
+
assert_not_empty @data['errors']
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
# Tests that verify the SessionsController works correctly with a MultiEmailUser —
|
|
6
|
+
# a model that uses :multi_email_authenticatable from the devise-multi_email gem.
|
|
7
|
+
#
|
|
8
|
+
# Authentication for multi-email users goes through MultiEmailUserEmail records
|
|
9
|
+
# rather than a direct email column. The ResourceFinder concern detects the
|
|
10
|
+
# multi_email_association class method and delegates to find_by_email so that
|
|
11
|
+
# sign-in lookups correctly traverse the emails association.
|
|
12
|
+
#
|
|
13
|
+
# These tests are ActiveRecord-only — the MultiEmailUser model and its route
|
|
14
|
+
# are not available in Mongoid runs.
|
|
15
|
+
return unless DEVISE_TOKEN_AUTH_ORM == :active_record
|
|
16
|
+
|
|
17
|
+
class MultiEmailSessionsControllerTest < ActionDispatch::IntegrationTest
|
|
18
|
+
SIGN_IN_PASSWORD = 'secret123'
|
|
19
|
+
|
|
20
|
+
describe 'MultiEmailUser sessions' do
|
|
21
|
+
def registration_params(email: nil)
|
|
22
|
+
{
|
|
23
|
+
email: email || Faker::Internet.unique.email,
|
|
24
|
+
password: SIGN_IN_PASSWORD,
|
|
25
|
+
password_confirmation: SIGN_IN_PASSWORD,
|
|
26
|
+
confirm_success_url: Faker::Internet.url
|
|
27
|
+
}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Create a confirmed MultiEmailUser by registering through the endpoint and
|
|
31
|
+
# then calling confirm on the resource so the account is immediately active.
|
|
32
|
+
def create_confirmed_user(email: nil)
|
|
33
|
+
email ||= Faker::Internet.unique.email
|
|
34
|
+
post '/multi_email_auth', params: registration_params(email: email)
|
|
35
|
+
assert_equal 200, response.status, "Setup registration failed: #{response.body}"
|
|
36
|
+
user = assigns(:resource)
|
|
37
|
+
user.confirm
|
|
38
|
+
[user, email]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# -----------------------------------------------------------------------
|
|
42
|
+
# Sign in — confirmed user
|
|
43
|
+
# -----------------------------------------------------------------------
|
|
44
|
+
describe 'sign in with confirmed user' do
|
|
45
|
+
before do
|
|
46
|
+
@user, @email = create_confirmed_user
|
|
47
|
+
post '/multi_email_auth/sign_in',
|
|
48
|
+
params: { email: @email, password: SIGN_IN_PASSWORD }
|
|
49
|
+
@data = JSON.parse(response.body)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
test 'request is successful' do
|
|
53
|
+
assert_equal 200, response.status
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
test 'response includes user data' do
|
|
57
|
+
assert @data['data']
|
|
58
|
+
assert_equal @email, @data['data']['email']
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
test 'response includes access-token header' do
|
|
62
|
+
assert response.headers['access-token']
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
test 'response includes client header' do
|
|
66
|
+
assert response.headers['client']
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
test 'response includes uid header' do
|
|
70
|
+
assert response.headers['uid']
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# -----------------------------------------------------------------------
|
|
75
|
+
# Sign in — wrong password
|
|
76
|
+
# -----------------------------------------------------------------------
|
|
77
|
+
describe 'sign in with wrong password' do
|
|
78
|
+
before do
|
|
79
|
+
@user, @email = create_confirmed_user
|
|
80
|
+
post '/multi_email_auth/sign_in',
|
|
81
|
+
params: { email: @email, password: 'definitely-wrong' }
|
|
82
|
+
@data = JSON.parse(response.body)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
test 'request fails' do
|
|
86
|
+
assert_equal 401, response.status
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
test 'response contains errors' do
|
|
90
|
+
assert @data['errors']
|
|
91
|
+
assert_equal [I18n.t('devise_token_auth.sessions.bad_credentials')],
|
|
92
|
+
@data['errors']
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# -----------------------------------------------------------------------
|
|
97
|
+
# Sign in — unconfirmed user
|
|
98
|
+
# -----------------------------------------------------------------------
|
|
99
|
+
describe 'sign in with unconfirmed user' do
|
|
100
|
+
before do
|
|
101
|
+
@email = Faker::Internet.unique.email
|
|
102
|
+
post '/multi_email_auth', params: registration_params(email: @email)
|
|
103
|
+
assert_equal 200, response.status, "Setup registration failed: #{response.body}"
|
|
104
|
+
# Do NOT confirm the user.
|
|
105
|
+
post '/multi_email_auth/sign_in',
|
|
106
|
+
params: { email: @email, password: SIGN_IN_PASSWORD }
|
|
107
|
+
@data = JSON.parse(response.body)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
test 'request fails' do
|
|
111
|
+
assert_equal 401, response.status
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
test 'response contains errors' do
|
|
115
|
+
assert @data['errors']
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# -----------------------------------------------------------------------
|
|
120
|
+
# Sign in — non-existent email
|
|
121
|
+
# -----------------------------------------------------------------------
|
|
122
|
+
describe 'sign in with non-existent email' do
|
|
123
|
+
before do
|
|
124
|
+
post '/multi_email_auth/sign_in',
|
|
125
|
+
params: { email: Faker::Internet.unique.email, password: SIGN_IN_PASSWORD }
|
|
126
|
+
@data = JSON.parse(response.body)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
test 'request fails' do
|
|
130
|
+
assert_equal 401, response.status
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
test 'response contains errors' do
|
|
134
|
+
assert @data['errors']
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# -----------------------------------------------------------------------
|
|
139
|
+
# Sign out — authenticated user
|
|
140
|
+
# -----------------------------------------------------------------------
|
|
141
|
+
describe 'sign out authenticated user' do
|
|
142
|
+
before do
|
|
143
|
+
@user, @email = create_confirmed_user
|
|
144
|
+
|
|
145
|
+
post '/multi_email_auth/sign_in',
|
|
146
|
+
params: { email: @email, password: SIGN_IN_PASSWORD }
|
|
147
|
+
assert_equal 200, response.status, "Sign-in failed: #{response.body}"
|
|
148
|
+
|
|
149
|
+
@auth_headers = {
|
|
150
|
+
'access-token' => response.headers['access-token'],
|
|
151
|
+
'client' => response.headers['client'],
|
|
152
|
+
'uid' => response.headers['uid'],
|
|
153
|
+
'token-type' => response.headers['token-type']
|
|
154
|
+
}
|
|
155
|
+
@client_id = @auth_headers['client']
|
|
156
|
+
|
|
157
|
+
delete '/multi_email_auth/sign_out', headers: @auth_headers
|
|
158
|
+
@data = JSON.parse(response.body)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
test 'sign out is successful' do
|
|
162
|
+
assert_equal 200, response.status
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
test 'token is invalidated' do
|
|
166
|
+
@user.reload
|
|
167
|
+
assert_nil @user.tokens[@client_id]
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# -----------------------------------------------------------------------
|
|
172
|
+
# Sign out — unauthenticated user
|
|
173
|
+
# -----------------------------------------------------------------------
|
|
174
|
+
describe 'sign out without authentication' do
|
|
175
|
+
before do
|
|
176
|
+
delete '/multi_email_auth/sign_out'
|
|
177
|
+
@data = JSON.parse(response.body)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
test 'request fails with 404' do
|
|
181
|
+
assert_equal 404, response.status
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
test 'response contains errors' do
|
|
185
|
+
assert @data['errors']
|
|
186
|
+
assert_equal [I18n.t('devise_token_auth.sessions.user_not_found')],
|
|
187
|
+
@data['errors']
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|