[Git][noosfero/noosfero][master] 3 commits: Fix errors 400 to 422 when the call is correct but the data is rejected by the service

Leandro Nunes gitlab at mg.gitlab.com
Thu Apr 27 09:54:42 BRT 2017


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
c412c510 by Josafá Souza Jr at 2017-04-27T08:42:38-03:00
Fix errors 400 to 422 when the call is correct but the data is rejected by the service

- - - - -
efab24dd by Leandro Nunes dos Santos at 2017-04-27T09:27:46-03:00
fix unit tests

- - - - -
39f72622 by Leandro Nunes at 2017-04-27T12:54:28+00:00
Merge branch 'fix-error-400-to-422' into 'master'

Fix errors 400 to 422 when the call is correct but the data is rejected by the service

See merge request !1187
- - - - -


6 changed files:

- app/api/helpers.rb
- app/api/status.rb
- test/api/articles_test.rb
- test/api/communities_test.rb
- test/api/people_test.rb
- test/api/profiles_test.rb


Changes:

=====================================
app/api/helpers.rb
=====================================
--- a/app/api/helpers.rb
+++ b/app/api/helpers.rb
@@ -404,16 +404,16 @@ module Api
       error!(message_hash, status)
     end
 
-    def render_model_errors!(active_model_errors)
+    def render_model_errors!(active_record_errors)
       message_hash = {}
-      message_hash[:errors_details] = active_model_errors.details if active_model_errors.details
-      message_hash[:errors_messages] = active_model_errors.messages if active_model_errors.messages
-      message_hash[:full_messages] = active_model_errors.full_messages if active_model_errors.full_messages
+      message_hash[:errors_details] = active_record_errors.details if active_record_errors.details
+      message_hash[:errors_messages] = active_record_errors.messages if active_record_errors.messages
+      message_hash[:full_messages] = active_record_errors.full_messages if active_record_errors.full_messages
       full_messages = ''
-      full_messages = active_model_errors.full_messages.join(', ') if active_model_errors.full_messages
+      full_messages = active_record_errors.full_messages.join(', ') if active_record_errors.full_messages
       log_message = "#{status}, model errors: #{full_messages}"
       logger.error log_message unless Rails.env.test?
-      error!(message_hash, Api::Status::BAD_REQUEST)
+      error!(message_hash, Api::Status::UNPROCESSABLE_ENTITY)
     end
 
     protected


=====================================
app/api/status.rb
=====================================
--- a/app/api/status.rb
+++ b/app/api/status.rb
@@ -7,6 +7,7 @@ module Api
     FORBIDDEN = 403
     NOT_FOUND = 404
     METHOD_NOT_ALLOWED = 405
+    UNPROCESSABLE_ENTITY = 422
     OK = 200
     CREATED = 201
 


=====================================
test/api/articles_test.rb
=====================================
--- a/test/api/articles_test.rb
+++ b/test/api/articles_test.rb
@@ -228,14 +228,14 @@ class ArticlesTest < ActiveSupport::TestCase
     post "/api/v1/articles/#{article.id}/vote?#{params.to_query}"
     json = JSON.parse(last_response.body)
     ## The api should not allow to save this vote
-    assert_equal 400, last_response.status
+    assert_equal Api::Status::UNPROCESSABLE_ENTITY, last_response.status
   end
 
   should 'not perform a vote in a archived article' do
     article = fast_create(Article, :profile_id => @person.id, :name => "Some thing", :archived => true)
     @params[:value] = 1
     post "/api/v1/articles/#{article.id}/vote?#{params.to_query}"
-    assert_equal 400, last_response.status
+    assert_equal Api::Status::UNPROCESSABLE_ENTITY, last_response.status
   end
 
   should 'not update hit attribute of a specific child if a article is archived' do


=====================================
test/api/communities_test.rb
=====================================
--- a/test/api/communities_test.rb
+++ b/test/api/communities_test.rb
@@ -71,7 +71,7 @@ class CommunitiesTest < ActiveSupport::TestCase
     login_api
     post "/api/v1/communities?#{params.to_query}"
     json = JSON.parse(last_response.body)
-    assert_equal 400, last_response.status
+    assert_equal 422, last_response.status
   end
 
   should 'get community to logged user' do


=====================================
test/api/people_test.rb
=====================================
--- a/test/api/people_test.rb
+++ b/test/api/people_test.rb
@@ -228,12 +228,12 @@ class PeopleTest < ActiveSupport::TestCase
     assert_equal login, json['identifier']
   end
 
-  should 'return 400 status for invalid person creation' do
+  should "return #{Api::Status::UNPROCESSABLE_ENTITY} status for invalid person creation" do
     login_api
     params[:person] = {:login => 'some'}
     post "/api/v1/people?#{params.to_query}"
     json = JSON.parse(last_response.body)
-    assert_equal 400, last_response.status
+    assert_equal Api::Status::UNPROCESSABLE_ENTITY, last_response.status
   end
 
   should 'display permissions' do


=====================================
test/api/profiles_test.rb
=====================================
--- a/test/api/profiles_test.rb
+++ b/test/api/profiles_test.rb
@@ -341,7 +341,7 @@ class ProfilesTest < ActiveSupport::TestCase
     params[:profile][:identifier] = other_person.identifier
     post "/api/v1/profiles/#{person.id}?#{params.to_query}"
     json = JSON.parse(last_response.body)
-    assert_equal 400, last_response.status
+    assert_equal Api::Status::UNPROCESSABLE_ENTITY, last_response.status
     assert_equal "blank", json['errors_details']['name'].first['error']
     assert_equal "not_available", json['errors_details']['identifier'].first['error']
   end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/9008a9aae6c14c3db59f5d9b2b662e7a315939b1...39f726228cc91af324f33417439b4b85d76f5504

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/9008a9aae6c14c3db59f5d9b2b662e7a315939b1...39f726228cc91af324f33417439b4b85d76f5504
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/20170427/84cc3a28/attachment-0001.html>


More information about the Noosfero-dev mailing list