Skip to content

Commit 16d1993

Browse files
Merge pull request #204 from mchavarriagam/issue/203
Return 422 (was 500) when empty body for sign up and account update. Fixes #203
2 parents f296406 + 180d69e commit 16d1993

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

app/controllers/devise_token_auth/registrations_controller.rb

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module DeviseTokenAuth
22
class RegistrationsController < DeviseTokenAuth::ApplicationController
33
before_filter :set_user_by_token, :only => [:destroy, :update]
4+
before_filter :validate_sign_up_params, :only => :create
5+
before_filter :validate_account_update_params, :only => :update
46
skip_after_filter :update_auth_header, :only => [:create, :destroy]
57

68
def create
@@ -137,5 +139,22 @@ def sign_up_params
137139
def account_update_params
138140
params.permit(devise_parameter_sanitizer.for(:account_update))
139141
end
142+
143+
private
144+
145+
def validate_sign_up_params
146+
validate_post_data sign_up_params, 'Please submit proper sign up data in request body.'
147+
end
148+
149+
def validate_account_update_params
150+
validate_post_data account_update_params, 'Please submit proper account update data in request body.'
151+
end
152+
153+
def validate_post_data which, message
154+
render json: {
155+
status: 'error',
156+
errors: [message]
157+
}, status: :unprocessable_entity if which.empty?
158+
end
140159
end
141160
end

test/controllers/devise_token_auth/registrations_controller_test.rb

+53
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,32 @@
99

1010
class DeviseTokenAuth::RegistrationsControllerTest < ActionDispatch::IntegrationTest
1111
describe DeviseTokenAuth::RegistrationsController do
12+
describe 'Validate non-empty body' do
13+
before do
14+
# need to post empty data
15+
post '/auth', {}
16+
17+
@resource = assigns(:resource)
18+
@data = JSON.parse(response.body)
19+
end
20+
21+
test 'request should fail' do
22+
assert_equal 422, response.status
23+
end
24+
25+
test 'returns error message' do
26+
assert_not_empty @data['errors']
27+
end
28+
29+
test 'return error status' do
30+
assert_equal 'error', @data['status']
31+
end
32+
33+
test 'user should not have been saved' do
34+
assert @resource.nil?
35+
end
36+
end
37+
1238
describe "Successful registration" do
1339
before do
1440
@mails_sent = ActionMailer::Base.deliveries.count
@@ -416,6 +442,33 @@ class DeviseTokenAuth::RegistrationsControllerTest < ActionDispatch::Integration
416442
end
417443
end
418444

445+
describe 'validate non-empty body' do
446+
before do
447+
# get the email so we can check it wasn't updated
448+
@email = @existing_user.email
449+
put '/auth', {}, @auth_headers
450+
451+
@data = JSON.parse(response.body)
452+
@existing_user.reload
453+
end
454+
455+
test 'request should fail' do
456+
assert_equal 422, response.status
457+
end
458+
459+
test 'returns error message' do
460+
assert_not_empty @data['errors']
461+
end
462+
463+
test 'return error status' do
464+
assert_equal 'error', @data['status']
465+
end
466+
467+
test 'user should not have been saved' do
468+
assert_equal @email, @existing_user.email
469+
end
470+
end
471+
419472
describe "error" do
420473
before do
421474
# test invalid update param

0 commit comments

Comments
 (0)