Skip to content

Commit 7ecdf0e

Browse files
authored
Add paranoid mode (#1378)
* Add swap test helper * Add paranoid mode to passwords controller * Add paranoid mode to unlocks controller * Add paranoid mode to confirmations controller * Extract method for i18n messages with paranoid
1 parent 51ba215 commit 7ecdf0e

File tree

9 files changed

+243
-52
lines changed

9 files changed

+243
-52
lines changed

app/controllers/devise_token_auth/application_controller.rb

+8
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,13 @@ def render_error(status, message, data = nil)
7575
response = response.merge(data) if data
7676
render json: response, status: status
7777
end
78+
79+
def success_message(name, email)
80+
if Devise.paranoid
81+
I18n.t("devise_token_auth.#{name}.sended_paranoid")
82+
else
83+
I18n.t("devise_token_auth.#{name}.sended", email: email)
84+
end
85+
end
7886
end
7987
end

app/controllers/devise_token_auth/confirmations_controller.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,17 @@ def render_create_error_missing_email
5555

5656
def render_create_success
5757
render json: {
58-
success: true,
59-
message: I18n.t('devise_token_auth.confirmations.sended', email: @email)
60-
}
58+
success: true,
59+
message: success_message('confirmations', @email)
60+
}
6161
end
6262

6363
def render_not_found_error
64-
render_error(404, I18n.t('devise_token_auth.confirmations.user_not_found', email: @email))
64+
if Devise.paranoid
65+
render_error(404, I18n.t('devise_token_auth.confirmations.sended_paranoid'))
66+
else
67+
render_error(404, I18n.t('devise_token_auth.confirmations.user_not_found', email: @email))
68+
end
6569
end
6670

6771
private

app/controllers/devise_token_auth/passwords_controller.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def render_error_not_allowed_redirect_url
128128
def render_create_success
129129
render json: {
130130
success: true,
131-
message: I18n.t('devise_token_auth.passwords.sended', email: @email)
131+
message: success_message('passwords', @email)
132132
}
133133
end
134134

@@ -181,7 +181,11 @@ def password_resource_params
181181
end
182182

183183
def render_not_found_error
184-
render_error(404, I18n.t('devise_token_auth.passwords.user_not_found', email: @email))
184+
if Devise.paranoid
185+
render_error(404, I18n.t('devise_token_auth.passwords.sended_paranoid'))
186+
else
187+
render_error(404, I18n.t('devise_token_auth.passwords.user_not_found', email: @email))
188+
end
185189
end
186190

187191
def validate_redirect_url_param

app/controllers/devise_token_auth/unlocks_controller.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def render_create_error_missing_email
6363
def render_create_success
6464
render json: {
6565
success: true,
66-
message: I18n.t('devise_token_auth.unlocks.sended', email: @email)
66+
message: success_message('unlocks', @email)
6767
}
6868
end
6969

@@ -79,7 +79,11 @@ def render_show_error
7979
end
8080

8181
def render_not_found_error
82-
render_error(404, I18n.t('devise_token_auth.unlocks.user_not_found', email: @email))
82+
if Devise.paranoid
83+
render_error(404, I18n.t('devise_token_auth.unlocks.sended_paranoid'))
84+
else
85+
render_error(404, I18n.t('devise_token_auth.unlocks.user_not_found', email: @email))
86+
end
8387
end
8488

8589
def resource_params

config/locales/en.yml

+3
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@ en:
2121
missing_redirect_url: "Missing redirect URL."
2222
not_allowed_redirect_url: "Redirect to '%{redirect_url}' not allowed."
2323
sended: "An email has been sent to '%{email}' containing instructions for resetting your password."
24+
sended_paranoid: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes."
2425
user_not_found: "Unable to find user with email '%{email}'."
2526
password_not_required: "This account does not require a password. Sign in using your '%{provider}' account instead."
2627
missing_passwords: "You must fill out the fields labeled 'Password' and 'Password confirmation'."
2728
successfully_updated: "Your password has been successfully updated."
2829
unlocks:
2930
missing_email: "You must provide an email address."
3031
sended: "An email has been sent to '%{email}' containing instructions for unlocking your account."
32+
sended_paranoid: "If your account exists, you will receive an email with instructions for how to unlock it in a few minutes."
3133
user_not_found: "Unable to find user with email '%{email}'."
3234
confirmations:
3335
sended: "An email has been sent to '%{email}' containing instructions for confirming your account."
36+
sended_paranoid: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes."
3437
user_not_found: "Unable to find user with email '%{email}'."
3538
missing_email: "You must provide an email address."
3639

test/controllers/devise_token_auth/confirmations_controller_test.rb

+91-19
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,102 @@ def token_and_client_config_from(body)
9292
end
9393

9494
describe 'resend confirmation' do
95-
before do
96-
post :create,
97-
params: { email: @new_user.email,
98-
redirect_url: @redirect_url },
99-
xhr: true
100-
@resource = assigns(:resource)
101-
102-
@mail = ActionMailer::Base.deliveries.last
103-
@token, @client_config = token_and_client_config_from(@mail.body)
104-
end
105-
106-
test 'user should not be confirmed' do
107-
assert_nil @resource.confirmed_at
95+
describe 'without paranoid mode' do
96+
97+
describe 'on success' do
98+
before do
99+
post :create,
100+
params: { email: @new_user.email,
101+
redirect_url: @redirect_url },
102+
xhr: true
103+
@resource = assigns(:resource)
104+
@data = JSON.parse(response.body)
105+
@mail = ActionMailer::Base.deliveries.last
106+
@token, @client_config = token_and_client_config_from(@mail.body)
107+
end
108+
109+
test 'user should not be confirmed' do
110+
assert_nil @resource.confirmed_at
111+
end
112+
113+
test 'should generate raw token' do
114+
assert @token
115+
assert_equal @new_user.confirmation_token, @token
116+
end
117+
118+
test 'user should receive confirmation email' do
119+
assert_equal @resource.email, @mail['to'].to_s
120+
end
121+
122+
test 'response should contain message' do
123+
assert_equal @data['message'], I18n.t('devise_token_auth.confirmations.sended', email: @resource.email)
124+
end
125+
end
126+
127+
describe 'on failure' do
128+
before do
129+
post :create,
130+
params: { email: '[email protected]',
131+
redirect_url: @redirect_url },
132+
xhr: true
133+
@data = JSON.parse(response.body)
134+
end
135+
136+
test 'response should contain errors' do
137+
assert_equal @data['errors'], [I18n.t('devise_token_auth.confirmations.user_not_found', email: '[email protected]')]
138+
end
139+
end
108140
end
141+
end
109142

110-
test 'should generate raw token' do
111-
assert @token
112-
assert_equal @new_user.confirmation_token, @token
143+
describe 'with paranoid mode' do
144+
describe 'on success' do
145+
before do
146+
swap Devise, paranoid: true do
147+
post :create,
148+
params: { email: @new_user.email,
149+
redirect_url: @redirect_url },
150+
xhr: true
151+
@resource = assigns(:resource)
152+
@data = JSON.parse(response.body)
153+
@mail = ActionMailer::Base.deliveries.last
154+
@token, @client_config = token_and_client_config_from(@mail.body)
155+
end
156+
end
157+
158+
test 'user should not be confirmed' do
159+
assert_nil @resource.confirmed_at
160+
end
161+
162+
test 'should generate raw token' do
163+
assert @token
164+
assert_equal @new_user.confirmation_token, @token
165+
end
166+
167+
test 'user should receive confirmation email' do
168+
assert_equal @resource.email, @mail['to'].to_s
169+
end
170+
171+
test 'response should contain message' do
172+
assert_equal @data['message'], I18n.t('devise_token_auth.confirmations.sended_paranoid', email: @resource.email)
173+
end
113174
end
114175

115-
test 'user should receive confirmation email' do
116-
assert_equal @resource.email, @mail['to'].to_s
176+
describe 'on failure' do
177+
before do
178+
swap Devise, paranoid: true do
179+
post :create,
180+
params: { email: '[email protected]',
181+
redirect_url: @redirect_url },
182+
xhr: true
183+
@data = JSON.parse(response.body)
184+
end
185+
end
186+
187+
test 'response should contain errors' do
188+
assert_equal @data['errors'], [I18n.t('devise_token_auth.confirmations.sended_paranoid')]
189+
end
117190
end
118-
119191
end
120192
end
121193

test/controllers/devise_token_auth/passwords_controller_test.rb

+73-21
Original file line numberDiff line numberDiff line change
@@ -85,37 +85,89 @@ class DeviseTokenAuth::PasswordsControllerTest < ActionController::TestCase
8585
end
8686

8787
describe 'request password reset' do
88-
describe 'unknown user should return 404' do
89-
before do
90-
post :create,
91-
params: { email: '[email protected]',
92-
redirect_url: @redirect_url }
93-
@data = JSON.parse(response.body)
94-
end
88+
describe 'unknown user' do
89+
describe 'without paranoid mode' do
90+
before do
91+
post :create,
92+
params: { email: '[email protected]',
93+
redirect_url: @redirect_url }
94+
@data = JSON.parse(response.body)
95+
end
9596

96-
test 'unknown user should return 404' do
97-
assert_equal 404, response.status
97+
test 'unknown user should return 404' do
98+
assert_equal 404, response.status
99+
end
100+
101+
test 'errors should be returned' do
102+
assert @data['errors']
103+
assert_equal @data['errors'],
104+
[I18n.t('devise_token_auth.passwords.user_not_found',
105+
email: '[email protected]')]
106+
end
98107
end
99108

100-
test 'errors should be returned' do
101-
assert @data['errors']
102-
assert_equal @data['errors'],
103-
[I18n.t('devise_token_auth.passwords.user_not_found',
104-
email: '[email protected]')]
109+
describe 'with paranoid mode' do
110+
before do
111+
swap Devise, paranoid: true do
112+
post :create,
113+
params: { email: '[email protected]',
114+
redirect_url: @redirect_url }
115+
@data = JSON.parse(response.body)
116+
end
117+
end
118+
119+
test 'unknown user should return 404' do
120+
assert_equal 404, response.status
121+
end
122+
123+
test 'errors should be returned' do
124+
assert @data['errors']
125+
assert_equal @data['errors'],
126+
[I18n.t('devise_token_auth.passwords.sended_paranoid')]
127+
end
105128
end
106129
end
107130

108131
describe 'successfully requested password reset' do
109-
before do
110-
post :create,
111-
params: { email: @resource.email,
112-
redirect_url: @redirect_url }
132+
describe 'without paranoid mode' do
133+
before do
134+
post :create,
135+
params: { email: @resource.email,
136+
redirect_url: @redirect_url }
113137

114-
@data = JSON.parse(response.body)
138+
@data = JSON.parse(response.body)
139+
end
140+
141+
test 'response should not contain extra data' do
142+
assert_nil @data['data']
143+
end
144+
145+
test 'response should contains message' do
146+
assert_equal \
147+
@data['message'],
148+
I18n.t('devise_token_auth.passwords.sended', email: @resource.email)
149+
end
115150
end
116151

117-
test 'response should not contain extra data' do
118-
assert_nil @data['data']
152+
describe 'with paranoid mode' do
153+
before do
154+
swap Devise, paranoid: true do
155+
post :create,
156+
params: { email: @resource.email,
157+
redirect_url: @redirect_url }
158+
@data = JSON.parse(response.body)
159+
end
160+
end
161+
162+
test 'response should return success status' do
163+
assert_equal 200, response.status
164+
end
165+
166+
test 'response should contain message' do
167+
assert_equal \
168+
@data['message'],
169+
I18n.t('devise_token_auth.passwords.sended_paranoid')
170+
end
119171
end
120172
end
121173

test/controllers/devise_token_auth/unlocks_controller_test.rb

+21-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class DeviseTokenAuth::UnlocksControllerTest < ActionController::TestCase
5757
end
5858

5959
describe 'request unlock' do
60-
describe 'unknown user should return 404' do
60+
describe 'without paranoid mode' do
6161
before do
6262
post :create, params: { email: '[email protected]' }
6363
@data = JSON.parse(response.body)
@@ -68,9 +68,26 @@ class DeviseTokenAuth::UnlocksControllerTest < ActionController::TestCase
6868

6969
test 'errors should be returned' do
7070
assert @data['errors']
71-
assert_equal @data['errors'],
72-
[I18n.t('devise_token_auth.passwords.user_not_found',
73-
email: '[email protected]')]
71+
assert_equal @data['errors'], [I18n.t('devise_token_auth.unlocks.user_not_found',
72+
email: '[email protected]')]
73+
end
74+
end
75+
76+
describe 'with paranoid mode' do
77+
before do
78+
swap Devise, paranoid: true do
79+
post :create, params: { email: '[email protected]' }
80+
@data = JSON.parse(response.body)
81+
end
82+
end
83+
84+
test 'unknown user should return 404' do
85+
assert_equal 404, response.status
86+
end
87+
88+
test 'errors should be returned' do
89+
assert @data['errors']
90+
assert_equal @data['errors'], [I18n.t('devise_token_auth.unlocks.sended_paranoid')]
7491
end
7592
end
7693

0 commit comments

Comments
 (0)