summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-04-04 17:55:20 -0600
committerHombreLaser <sebastian-440@live.com>2023-04-04 17:55:20 -0600
commitdb9b4b712271a67e60e6c0bf58c9ce510f177682 (patch)
tree791da60f29a1ac3b73efff46493d96c0116318e0
parent3afd35371caa0677fd2f66491bd337d1673013dc (diff)
Mejora presentaciĆ³n de mensajes de error
-rw-r--r--app/controllers/api/addresses_controller.rb4
-rw-r--r--app/controllers/api/cards_controller.rb4
-rw-r--r--app/controllers/api/companies_controller.rb4
-rw-r--r--app/controllers/api/products_controller.rb4
-rw-r--r--app/controllers/api/sessions_controller.rb2
-rw-r--r--app/controllers/api/user_accounts_controller.rb4
-rw-r--r--app/services/addresses/address_services_base.rb4
-rw-r--r--spec/models/product_review_spec.rb12
-rw-r--r--spec/requests/addresses_controller/create_spec.rb3
-rw-r--r--spec/requests/addresses_controller/update_spec.rb4
-rw-r--r--spec/requests/cards_controller/create_spec.rb10
-rw-r--r--spec/requests/cards_controller/update_spec.rb10
-rw-r--r--spec/requests/companies_controller/create_spec.rb2
-rw-r--r--spec/requests/companies_controller/update_spec.rb2
-rw-r--r--spec/requests/products_controller/create_spec.rb4
-rw-r--r--spec/requests/products_controller/update_spec.rb4
-rw-r--r--spec/requests/user_accounts_controller/create_spec.rb2
-rw-r--r--spec/requests/user_accounts_controller/update_spec.rb2
18 files changed, 48 insertions, 33 deletions
diff --git a/app/controllers/api/addresses_controller.rb b/app/controllers/api/addresses_controller.rb
index 386e613..51b6b99 100644
--- a/app/controllers/api/addresses_controller.rb
+++ b/app/controllers/api/addresses_controller.rb
@@ -14,7 +14,7 @@ module Api
begin
unless @service.call
- render json: { error_messages: @service.address.errors.full_messages }, status: :unprocessable_entity
+ render json: { errors: @service.error_messages }, status: :unprocessable_entity
return
end
rescue ActiveRecord::RecordNotUnique
@@ -34,7 +34,7 @@ module Api
when :not_found
render status: :not_found
when :unprocessable_entity
- render json: { error_messages: @service.address.errors.full_messages }, status: :unprocessable_entity
+ render json: { errors: @service.error_messages }, status: :unprocessable_entity
else
@address = @service.address
render json: serialized_object.serializable_hash, status: :ok
diff --git a/app/controllers/api/cards_controller.rb b/app/controllers/api/cards_controller.rb
index 41d6138..daf5f3a 100644
--- a/app/controllers/api/cards_controller.rb
+++ b/app/controllers/api/cards_controller.rb
@@ -16,7 +16,7 @@ module Api
if @card.save
render json: serialized_object.serializable_hash, status: :ok
else
- render json: { error_messages: @card.errors.full_messages }, status: :unprocessable_entity
+ render json: { errors: @card.errors.as_json }, status: :unprocessable_entity
end
end
@@ -28,7 +28,7 @@ module Api
if @card.update(permitted_params)
render json: serialized_object.serializable_hash, status: :ok
else
- render json: { error_messages: @card.errors.full_messages }, status: :unprocessable_entity
+ render json: { errors: @card.errors.as_json }, status: :unprocessable_entity
end
end
diff --git a/app/controllers/api/companies_controller.rb b/app/controllers/api/companies_controller.rb
index 19e3493..4c19716 100644
--- a/app/controllers/api/companies_controller.rb
+++ b/app/controllers/api/companies_controller.rb
@@ -22,7 +22,7 @@ module Api
render json: serialized_object.serializable_hash, status: :ok and return if @company.save
- render json: { error_messages: @company.errors.full_messages }, status: :unprocessable_entity
+ render json: { errors: @company.errors.as_json }, status: :unprocessable_entity
end
def update
@@ -33,7 +33,7 @@ module Api
if @company.update(permitted_params)
render json: serialized_object.serializable_hash, status: :ok
else
- render json: { error_messages: @company.errors.full_messages }, status: :unprocessable_entity
+ render json: { errors: @company.errors.as_json }, status: :unprocessable_entity
end
end
diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb
index 5ebd6b6..2fa435d 100644
--- a/app/controllers/api/products_controller.rb
+++ b/app/controllers/api/products_controller.rb
@@ -21,7 +21,7 @@ module Api
if @product.save
render json: serialized_object(@product.public_id).serializable_hash, status: :ok
else
- render json: { error_messages: @product.errors.full_messages }, status: :unprocessable_entity
+ render json: { errors: @product.errors.as_json }, status: :unprocessable_entity
end
end
@@ -33,7 +33,7 @@ module Api
if @product.update(object_params)
render json: serialized_object(@product.public_id).serializable_hash, status: :ok
else
- render json: { error_messages: @product.errors.full_messages }, status: :unprocessable_entity
+ render json: { errors: @product.errors.as_json }, status: :unprocessable_entity
end
end
diff --git a/app/controllers/api/sessions_controller.rb b/app/controllers/api/sessions_controller.rb
index 3d9f315..a81af71 100644
--- a/app/controllers/api/sessions_controller.rb
+++ b/app/controllers/api/sessions_controller.rb
@@ -9,7 +9,7 @@ module Api
@user_account = UserAccount.find_by(email: permitted_params[:email])
unless @user_account&.authenticate(permitted_params[:password])
- render json: { error_message: 'Credenciales incorrectas' }, status: :unauthorized and return
+ render json: { errors: { auth: 'Credenciales incorrectas' } }, status: :unauthorized and return
end
render json: generate_token, status: :ok
diff --git a/app/controllers/api/user_accounts_controller.rb b/app/controllers/api/user_accounts_controller.rb
index ad276b5..6404dee 100644
--- a/app/controllers/api/user_accounts_controller.rb
+++ b/app/controllers/api/user_accounts_controller.rb
@@ -14,14 +14,14 @@ module Api
render json: generate_token, status: :ok and return if @user_account.save
- render json: @user_account.errors.full_messages, status: :unprocessable_entity
+ render json: { errors: @user_account.errors.as_json }, status: :unprocessable_entity
end
def update
if current_user_account.update(permitted_params)
render json: serialized_object.serializable_hash, status: :ok
else
- render json: current_user_account.errors.full_messages, status: :unprocessable_entity
+ render json: { errors: current_user_account.errors.as_json }, status: :unprocessable_entity
end
end
diff --git a/app/services/addresses/address_services_base.rb b/app/services/addresses/address_services_base.rb
index 06ffde7..9b7f9f3 100644
--- a/app/services/addresses/address_services_base.rb
+++ b/app/services/addresses/address_services_base.rb
@@ -8,6 +8,10 @@ module Addresses
@params = params
end
+ def error_messages
+ @address.errors.as_json
+ end
+
private
def already_existing_address
diff --git a/spec/models/product_review_spec.rb b/spec/models/product_review_spec.rb
index f4e4999..c8f7b69 100644
--- a/spec/models/product_review_spec.rb
+++ b/spec/models/product_review_spec.rb
@@ -3,6 +3,9 @@ require 'rails_helper'
RSpec.describe ProductReview, type: :model do
let(:review) { build(:product_review) }
+ it { should belong_to(:user_account) }
+ it { should belong_to(:product) }
+
it { should validate_presence_of(:review) }
it { should validate_presence_of(:rating) }
@@ -14,4 +17,13 @@ RSpec.describe ProductReview, type: :model do
expect(review.save).to be_falsey
end
end
+
+ describe '#sole_user_review' do
+ it "doesn't allow a user to post more than one review for any given product" do
+ review.save
+ other_review = build(:product_review, user_account: review.user_account, product: review.product)
+ expect(other_review.save).to be_falsey
+ expect(other_review.errors[:review]).to_not be_nil
+ end
+ end
end
diff --git a/spec/requests/addresses_controller/create_spec.rb b/spec/requests/addresses_controller/create_spec.rb
index befb80b..5987e0f 100644
--- a/spec/requests/addresses_controller/create_spec.rb
+++ b/spec/requests/addresses_controller/create_spec.rb
@@ -15,8 +15,7 @@ RSpec.describe 'POST /account/addresses', type: :request do
it_behaves_like 'a POST request' do
let(:route) { '/api/account/addresses' }
let(:expected_error_messages) do
- ["Number can't be blank", "Street can't be blank", "Zip code can't be blank",
- 'Country is invalid', "City can't be blank"]
+ ["can't be blank", "can't be blank", "can't be blank", 'is invalid', "can't be blank"]
end
let(:desired_error_status) { 422 }
let(:expected_text) { [address.number.to_s, address.zip_code.to_s, address.country, address.street, address.city] }
diff --git a/spec/requests/addresses_controller/update_spec.rb b/spec/requests/addresses_controller/update_spec.rb
index 0c3662e..a5b993a 100644
--- a/spec/requests/addresses_controller/update_spec.rb
+++ b/spec/requests/addresses_controller/update_spec.rb
@@ -21,8 +21,8 @@ RSpec.describe 'PUT /api/account/addresses', type: :request do
let(:route) { "/api/account/addresses/#{address.id}" }
let(:wrong_route) { "/api/account/addresses/#{SecureRandom.hex(8)}" }
let(:expected_error_messages) do
- ["Number can't be blank", "Street can't be blank", "Zip code can't be blank",
- 'Country is invalid', "City can't be blank"]
+ ["can't be blank", "can't be blank", "can't be blank",
+ 'is invalid', "can't be blank"]
end
let(:expected_text) do
[new_address.number.to_s, new_address.zip_code.to_s, new_address.country, new_address.street,
diff --git a/spec/requests/cards_controller/create_spec.rb b/spec/requests/cards_controller/create_spec.rb
index 465288b..af04a23 100644
--- a/spec/requests/cards_controller/create_spec.rb
+++ b/spec/requests/cards_controller/create_spec.rb
@@ -15,11 +15,11 @@ RSpec.describe 'POST /account/cards', type: :request do
it_behaves_like 'a POST request' do
let(:route) { '/api/account/cards' }
let(:expected_error_messages) do
- ['Number is the wrong length (should be 16 characters)',
- 'Expiration year must be greater than 1970',
- 'Expiration month is not included in the list',
- 'Expiration day is not included in the list',
- "Security code can't be blank"]
+ ['is the wrong length (should be 16 characters)',
+ 'must be greater than 1970',
+ 'is not included in the list',
+ 'is not included in the list',
+ "can't be blank"]
end
let(:desired_error_status) { 422 }
let(:expected_text) do
diff --git a/spec/requests/cards_controller/update_spec.rb b/spec/requests/cards_controller/update_spec.rb
index 77b4c4f..453befc 100644
--- a/spec/requests/cards_controller/update_spec.rb
+++ b/spec/requests/cards_controller/update_spec.rb
@@ -17,11 +17,11 @@ RSpec.describe 'PUT /api/account/addresses', type: :request do
let(:route) { "/api/account/cards/#{card.id}" }
let(:wrong_route) { "/api/account/cards/#{SecureRandom.hex(8)}" }
let(:expected_error_messages) do
- ['Number is the wrong length (should be 16 characters)',
- 'Expiration year must be greater than 1970',
- 'Expiration month is not included in the list',
- 'Expiration day is not included in the list',
- "Security code can't be blank"]
+ ['is the wrong length (should be 16 characters)',
+ 'must be greater than 1970',
+ 'is not included in the list',
+ 'is not included in the list',
+ "can't be blank"]
end
let(:expected_text) do
[new_card.number, new_card.expiration_year, new_card.expiration_month, new_card.expiration_day,
diff --git a/spec/requests/companies_controller/create_spec.rb b/spec/requests/companies_controller/create_spec.rb
index 219868b..74a448a 100644
--- a/spec/requests/companies_controller/create_spec.rb
+++ b/spec/requests/companies_controller/create_spec.rb
@@ -12,7 +12,7 @@ RSpec.describe 'POST /api/companies', type: :request do
let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } }
let(:route) { '/api/companies' }
let(:expected_error_messages) do
- ["Name can't be blank", "Short name can't be blank", "Country can't be blank"]
+ ["can't be blank", "can't be blank", "can't be blank"]
end
let(:desired_error_status) { 422 }
let(:expected_text) { [company.name, company.short_name, company.country, 'logo', 'http'] }
diff --git a/spec/requests/companies_controller/update_spec.rb b/spec/requests/companies_controller/update_spec.rb
index fd23365..de8cb2a 100644
--- a/spec/requests/companies_controller/update_spec.rb
+++ b/spec/requests/companies_controller/update_spec.rb
@@ -14,7 +14,7 @@ RSpec.describe 'PUT /api/companies/company_id', type: :request do
let(:route) { "/api/companies/#{company.short_name}" }
let(:wrong_route) { "/api/companies/#{SecureRandom.hex(8)}" }
let(:expected_error_messages) do
- ["Name can't be blank", "Short name can't be blank", "Country can't be blank"]
+ ["can't be blank", "can't be blank", "can't be blank"]
end
let(:expected_text) { [new_company.name, new_company.short_name, new_company.country, 'logo', 'http'] }
let(:params) do
diff --git a/spec/requests/products_controller/create_spec.rb b/spec/requests/products_controller/create_spec.rb
index 400fd18..4d6a9bc 100644
--- a/spec/requests/products_controller/create_spec.rb
+++ b/spec/requests/products_controller/create_spec.rb
@@ -17,8 +17,8 @@ RSpec.describe 'POST /api/products', type: :request do
let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } }
let(:route) { '/api/products' }
let(:expected_error_messages) do
- ["Name can't be blank", 'Unitary price must be greater than 0', 'Bulk price must be greater than 0',
- 'Available quantity must be greater than 0', 'Company must exist']
+ ["can't be blank", 'must be greater than 0', 'must be greater than 0',
+ 'must be greater than 0', 'must exist']
end
let(:desired_error_status) { 422 }
let(:expected_text) do
diff --git a/spec/requests/products_controller/update_spec.rb b/spec/requests/products_controller/update_spec.rb
index f916d6a..c84c5bd 100644
--- a/spec/requests/products_controller/update_spec.rb
+++ b/spec/requests/products_controller/update_spec.rb
@@ -19,8 +19,8 @@ RSpec.describe 'PUT /api/products/product_id', type: :request do
let(:route) { "/api/products/#{product.public_id}" }
let(:wrong_route) { "/api/products/#{SecureRandom.hex(8)}" }
let(:expected_error_messages) do
- ["Name can't be blank", 'Unitary price must be greater than 0', 'Bulk price must be greater than 0',
- 'Available quantity must be greater than 0', 'Company must exist']
+ ["can't be blank", 'must be greater than 0', 'must be greater than 0',
+ 'must be greater than 0', 'must exist']
end
let(:desired_error_status) { 422 }
let(:expected_text) do
diff --git a/spec/requests/user_accounts_controller/create_spec.rb b/spec/requests/user_accounts_controller/create_spec.rb
index a45e02b..7b70268 100644
--- a/spec/requests/user_accounts_controller/create_spec.rb
+++ b/spec/requests/user_accounts_controller/create_spec.rb
@@ -7,7 +7,7 @@ RSpec.describe 'POST /api/user_accounts', type: :request do
let(:headers) { {} }
let(:route) { '/api/user_accounts' }
let(:expected_error_messages) do
- ["Password can't be blank", 'Email is invalid', "First name can't be blank", "Last name can't be blank"]
+ ["can't be blank", 'is invalid', "can't be blank", "can't be blank"]
end
let(:desired_error_status) { 422 }
let(:expected_text) { %w[token refresh] }
diff --git a/spec/requests/user_accounts_controller/update_spec.rb b/spec/requests/user_accounts_controller/update_spec.rb
index fcfb28e..e26f819 100644
--- a/spec/requests/user_accounts_controller/update_spec.rb
+++ b/spec/requests/user_accounts_controller/update_spec.rb
@@ -17,7 +17,7 @@ RSpec.describe 'PUT /api/account', type: :request do
let(:desired_error_status) { 422 }
let(:wrong_params) { { email: 'notanemail', password: '', first_name: '', last_name: '' } }
let(:expected_error_messages) do
- ['Email is invalid', "First name can't be blank", "Last name can't be blank"]
+ ['is invalid', "can't be blank", "can't be blank"]
end
end
end