summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-04-06 15:29:08 -0600
committerHombreLaser <sebastian-440@live.com>2023-04-06 15:29:08 -0600
commit4611fdd6da7f9a1cc04144104e5ca26398f1b502 (patch)
tree0e04bb1a480d3484cadb9cb3e9119da27ba2c38d
parent61110336bc5a924346f0654cb5bb812101c3bc7a (diff)
Añade specs de product_cart
-rw-r--r--app/models/product_cart.rb2
-rw-r--r--spec/factories/cart.rb2
-rw-r--r--spec/models/product_cart_spec.rb8
-rw-r--r--spec/requests/carts_controller/create_spec.rb32
4 files changed, 42 insertions, 2 deletions
diff --git a/app/models/product_cart.rb b/app/models/product_cart.rb
index eb9cf9f..09e7a25 100644
--- a/app/models/product_cart.rb
+++ b/app/models/product_cart.rb
@@ -10,7 +10,7 @@ class ProductCart < ApplicationRecord
validate :sole_product_in_cart
def sole_product_in_cart
- return if cart.products.find_by(id: product_id).nil?
+ return if cart.nil? || cart.products.find_by(id: product_id).nil?
errors.add(:product_id, "cart already has product with id #{product_id}")
end
diff --git a/spec/factories/cart.rb b/spec/factories/cart.rb
index e184181..8fd4bc7 100644
--- a/spec/factories/cart.rb
+++ b/spec/factories/cart.rb
@@ -2,6 +2,6 @@
FactoryBot.define do
factory :cart, class: 'Cart' do
- user_account { create(:user_account) }
+ user_account { create(:user_account, role: 'regular') }
end
end
diff --git a/spec/models/product_cart_spec.rb b/spec/models/product_cart_spec.rb
index 00a8f6c..2c4d9d1 100644
--- a/spec/models/product_cart_spec.rb
+++ b/spec/models/product_cart_spec.rb
@@ -12,4 +12,12 @@ RSpec.describe ProductCart, type: :model do
it 'should belong to cart' do
expect(product_cart).to respond_to(:cart)
end
+
+ describe '#sole_product_in_cart' do
+ it 'validates for duplicate products in the cart' do
+ another_product_cart = build(:product_cart, cart: product_cart.cart, product: product_cart.product)
+ expect(another_product_cart.save).to be_falsey
+ expect(another_product_cart.errors[:product_id]).to be_present
+ end
+ end
end
diff --git a/spec/requests/carts_controller/create_spec.rb b/spec/requests/carts_controller/create_spec.rb
new file mode 100644
index 0000000..c0d2e4b
--- /dev/null
+++ b/spec/requests/carts_controller/create_spec.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe 'POST /account/cart', type: :request do
+ let(:cart) { create(:cart) }
+ let(:products) { create_list(:product, 2) }
+ let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } }
+ let(:token) { jwt(cart.user_account) }
+ let(:params) do
+ { product_id: products[0].id, quantity: 1 }
+ end
+
+ before(:each) do
+ cart.user_account.cart = cart
+ cart.user_account.save
+ end
+
+ it_behaves_like 'a POST request' do
+ let(:route) { '/api/account/cart' }
+ let(:expected_error_messages) do
+ ['must exist', 'must be greater than 0']
+ end
+ let(:desired_error_status) { 422 }
+ let(:expected_text) do
+ [products[0].name, 'quantity']
+ end
+ let(:wrong_params) do
+ { product_id: SecureRandom.hex(25), quantity: -1 }
+ end
+ end
+end