[Git][noosfero/noosfero][master] 2 commits: api: return model errors when fail to register

Victor Costa gitlab at mg.gitlab.com
Tue May 23 13:59:15 BRT 2017


Victor Costa pushed to branch master at Noosfero / noosfero


Commits:
e66bdbdc by Victor Costa at 2017-05-23T12:58:56-03:00
api: return model errors when fail to register

- - - - -
10501240 by Victor Costa at 2017-05-23T16:59:01+00:00
Merge branch 'fix-register-api' into 'master'

api: return model errors when fail to register

See merge request !1216
- - - - -


2 changed files:

- app/api/v1/session.rb
- test/api/session_test.rb


Changes:

=====================================
app/api/v1/session.rb
=====================================
--- a/app/api/v1/session.rb
+++ b/app/api/v1/session.rb
@@ -61,8 +61,7 @@ module Api
           user.generate_private_token! if user.activated?
           present user, :with => Entities::UserLogin, :current_person => user.person
         rescue ActiveRecord::RecordInvalid
-          message = user.errors.as_json.merge((user.person.present? ? user.person.errors : {}).as_json).to_json
-          render_api_error!(message, Api::Status::BAD_REQUEST)
+          render_model_errors!(user.errors)
         end
       end
 


=====================================
test/api/session_test.rb
=====================================
--- a/test/api/session_test.rb
+++ b/test/api/session_test.rb
@@ -61,40 +61,36 @@ class SessionTest < ActiveSupport::TestCase
   should 'not register a user with invalid login' do
     params = {:login => "c", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi at email.com" }
     post "/api/v1/register?#{params.to_query}"
-    assert_equal 400, last_response.status
     json = JSON.parse(last_response.body)
-    msg = json['message'].split(':')
-    key = msg[0][2, 5]
-    val = msg[1][2, 38]
-    assert_equal "login", key
-    assert_equal "is too short (minimum is 2 characters)", val
+    assert_equal 422, last_response.status
+    assert_equal 'too_short', json['errors']['login'].first['error']
   end
 
-  should 'not register a user with invalid login pt' do
+  should 'return translated message when fail to register a user with invalid login' do
     I18n.locale = "pt-BR"
     params = {:lang => "pt-BR", :login => "c", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi at email.com" }
     post "/api/v1/register?#{params.to_query}"
-    assert_equal 400, last_response.status
     json = JSON.parse(last_response.body)
-    msg = json['message'].split(':')
-    key = msg[0][2, 5]
-    val = msg[1][2, 35]
-    assert_equal "login", key
-    assert val.include? "muito curto"
+    assert_equal 422, last_response.status
+    assert_match /muito curto/, json['errors']['login'].first['full_message']
   end
 
   should 'not register a user without email' do
     params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => nil }
     post "/api/v1/register?#{params.to_query}"
-    assert_equal 400, last_response.status
+    json = JSON.parse(last_response.body)
+    assert_equal 422, last_response.status
+    assert_equal 'blank', json['errors']['email'].first['error']
   end
 
   should 'not register a duplicated user' do
     params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi at email.com" }
     post "/api/v1/register?#{params.to_query}"
     post "/api/v1/register?#{params.to_query}"
-    assert_equal 400, last_response.status
     json = JSON.parse(last_response.body)
+    assert_equal 422, last_response.status
+    assert_equal 'taken', json['errors']['login'].first['error']
+    assert_equal 'taken', json['errors']['email'].first['error']
   end
 
   # TODO: Add another test cases to check register situations



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/720672396aae9de83ff68ccf7a0e159f6d21a93c...1050124086548da04794bfbe6acc79cd3b398d44

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/720672396aae9de83ff68ccf7a0e159f6d21a93c...1050124086548da04794bfbe6acc79cd3b398d44
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170523/d3c4a1d6/attachment-0001.html>


More information about the Noosfero-dev mailing list