summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/api/carts_controller.rb2
-rw-r--r--app/models/cart.rb2
-rw-r--r--spec/factories/cart.rb7
-rw-r--r--spec/factories/product_cart.rb9
-rw-r--r--spec/models/product_cart_spec.rb13
-rw-r--r--spec/requests/carts_controller/destroy_spec.rb21
6 files changed, 50 insertions, 4 deletions
diff --git a/app/controllers/api/carts_controller.rb b/app/controllers/api/carts_controller.rb
index 0e14c17..d7c1529 100644
--- a/app/controllers/api/carts_controller.rb
+++ b/app/controllers/api/carts_controller.rb
@@ -20,7 +20,7 @@ module Api
end
def destroy
- if @cart.delete_product(product.id)
+ if @cart.delete_product(params[:id])
render status: :no_content
else
render status: :not_found
diff --git a/app/models/cart.rb b/app/models/cart.rb
index b08fc13..87e91d3 100644
--- a/app/models/cart.rb
+++ b/app/models/cart.rb
@@ -7,7 +7,7 @@ class Cart < ApplicationRecord
has_many :products, through: :product_carts
def delete_product(product_id)
- relation = product_carts.find_by(product_id:, cart_id:)
+ relation = ProductCart.find_by(product_id:, cart_id: id)
return false if relation.nil?
diff --git a/spec/factories/cart.rb b/spec/factories/cart.rb
new file mode 100644
index 0000000..e184181
--- /dev/null
+++ b/spec/factories/cart.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+FactoryBot.define do
+ factory :cart, class: 'Cart' do
+ user_account { create(:user_account) }
+ end
+end
diff --git a/spec/factories/product_cart.rb b/spec/factories/product_cart.rb
new file mode 100644
index 0000000..713fd0a
--- /dev/null
+++ b/spec/factories/product_cart.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+FactoryBot.define do
+ factory :product_cart, class: 'ProductCart' do
+ cart { create(:cart) }
+ product { create(:product) }
+ quantity { rand(1..1000) }
+ end
+end
diff --git a/spec/models/product_cart_spec.rb b/spec/models/product_cart_spec.rb
index 5a300b8..00a8f6c 100644
--- a/spec/models/product_cart_spec.rb
+++ b/spec/models/product_cart_spec.rb
@@ -1,6 +1,15 @@
+# frozen_string_literal: true
+
require 'rails_helper'
RSpec.describe ProductCart, type: :model do
- it { should belong_to(:product) }
- it { should belong_to(:cart) }
+ let(:product_cart) { create(:product_cart) }
+
+ it 'should belong to product' do
+ expect(product_cart).to respond_to(:product)
+ end
+
+ it 'should belong to cart' do
+ expect(product_cart).to respond_to(:cart)
+ end
end
diff --git a/spec/requests/carts_controller/destroy_spec.rb b/spec/requests/carts_controller/destroy_spec.rb
new file mode 100644
index 0000000..ce083ef
--- /dev/null
+++ b/spec/requests/carts_controller/destroy_spec.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe 'DELETE /api/account/cart/product_id', type: :request do
+ let(:user) { create(:user_account, role: 'regular') }
+ let(:product) { create(:product) }
+ let(:token) { jwt(user) }
+ let(:headers) { { 'Authorization' => "Bearer #{token['token']}" } }
+
+ before(:each) do
+ user.cart = Cart.create
+ user.cart.add_product(product.id, 1)
+ end
+
+ it_behaves_like 'a DELETE request' do
+ let(:product_not_in_cart) { create(:product) }
+ let(:resource) { "/api/account/cart/#{product.id}" }
+ let(:nonexistent_resource) { "/api/account/cart/#{product_not_in_cart.id}" }
+ end
+end