Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Restructure / fix Store API controllers

Damian Legawiecce361f26f0c7
diff --git a/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb b/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb
index 5388406c13..20e7d4c0fb 100644
--- a/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb
+++ b/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb
@@ -40,10 +40,6 @@ module Spree
true
end
- def admin_context?
- @current_api_key&.secret? || current_user&.try(:spree_admin?, current_store)
- end
-
private
def extract_api_key
diff --git a/api/app/controllers/concerns/spree/api/v3/jwt_authentication.rb b/api/app/controllers/concerns/spree/api/v3/jwt_authentication.rb
index b2b8523265..20817d3191 100644
--- a/api/app/controllers/concerns/spree/api/v3/jwt_authentication.rb
+++ b/api/app/controllers/concerns/spree/api/v3/jwt_authentication.rb
@@ -26,13 +26,14 @@ module Spree
end
# Required authentication - fails if no valid token
+ # Returns true if authenticated, false otherwise (also renders error and halts)
def require_authentication!
authenticate_user
- unless current_user
- render_error(code: ErrorHandler::ERROR_CODES[:authentication_required], message: 'Authentication required', status: :unauthorized)
- return false
- end
+ return true if current_user
+
+ render_error(code: ErrorHandler::ERROR_CODES[:authentication_required], message: 'Authentication required', status: :unauthorized)
+ false
end
protected
diff --git a/api/app/controllers/concerns/spree/api/v3/order_concern.rb b/api/app/controllers/concerns/spree/api/v3/order_concern.rb
index 9c008a5a43..7d37becef4 100644
--- a/api/app/controllers/concerns/spree/api/v3/order_concern.rb
+++ b/api/app/controllers/concerns/spree/api/v3/order_concern.rb
@@ -5,18 +5,13 @@ module Spree
extend ActiveSupport::Concern
# Allow access to order via order token for guests or authenticated users
+ # Expects @parent to be set to the order
def authorize_order_access!
- authorize!(:update, @order, order_token)
+ authorize!(:update, @parent, order_token)
end
protected
- # Finds order by number from params
- # Uses params[:order_id] for nested resources (line items, payments, etc.)
- def set_order
- @order = current_store.orders.friendly.find(params[:order_id])
- end
-
def order_token
# Check X-Spree-Order-Token header first
header = request.headers['X-Spree-Order-Token']
diff --git a/api/app/controllers/spree/api/v3/resource_controller.rb b/api/app/controllers/spree/api/v3/resource_controller.rb
index 2bf183b2e8..e879b8e392 100644
--- a/api/app/controllers/spree/api/v3/resource_controller.rb
+++ b/api/app/controllers/spree/api/v3/resource_controller.rb
@@ -2,6 +2,7 @@ module Spree
module Api
module V3
class ResourceController < BaseController
+ before_action :set_parent
before_action :set_resource, only: [:show, :update, :destroy]
# GET /api/v3/resource
@@ -27,7 +28,7 @@ module Spree
# POST /api/v3/resource
def create
- @resource = model_class.new(permitted_params)
+ @resource = build_resource
authorize_resource!(@resource, :create)
if @resource.save
@@ -54,17 +55,38 @@ module Spree
protected
+ # Override in subclass to set parent resource (e.g., @wishlist, @order)
+ # This runs before set_resource, allowing scope to use the parent
+ def set_parent
+ # No-op by default, override in nested resource controllers
+ end
+
# Sets the resource for show, update, destroy actions
+ # Always uses scope to respect controller's custom scoping
def set_resource
- @resource = if model_class.respond_to?(:friendly)
- model_class.friendly.find(params[:id])
- else
- scope.find(params[:id])
- end
-
+ @resource = find_resource
authorize_resource!(@resource)
end
+ # Builds a new resource, using parent association when @parent is set
+ def build_resource
+ if @parent.present?
+ @parent.send(parent_association).build(permitted_params)
+ else
+ model_class.new(permitted_params)
+ end
+ end
+
+ # Finds a single resource within scope
+ # Supports FriendlyId and standard ActiveRecord find
+ def find_resource
+ if model_class.try(:friendly)
+ scope.friendly.find(params[:id])
+ else
+ scope.find(params[:id])
+ end
+ end
+
# Authorize resource with CanCanCan
def authorize_resource!(resource = @resource, action = action_name.to_sym)
authorize!(action, resource)
@@ -121,13 +143,24 @@ module Spree
end
# Base scope with store and ability
+ # When @parent is set (nested resources), uses parent association instead
def scope
- base_scope = model_class.for_store(current_store)
- base_scope = base_scope.accessible_by(current_ability, :show)
+ base_scope = if @parent.present?
+ @parent.send(parent_association)
+ else
+ model_class.for_store(current_store)
+ end
+ base_scope = base_scope.accessible_by(current_ability, :show) unless @parent.present?
base_scope = base_scope.includes(scope_includes) if scope_includes.any?
model_class.include?(Spree::TranslatableResource) ? base_scope.i18n : base_scope
end
+ # Override to specify the association name on @parent
+ # Defaults to controller_name (e.g., 'wished_items' for WishlistItemsController)
+ def parent_association
+ controller_name
+ end
+
# Override in subclass to eager load associations that don't work well
# with ar_lazy_preload (e.g., prices, stock_items)
def scope_includes
diff --git a/api/app/controllers/spree/api/v3/store/addresses_controller.rb b/api/app/controllers/spree/api/v3/store/addresses_controller.rb
index 3048a0b5ab..0e7742cd43 100644
--- a/api/app/controllers/spree/api/v3/store/addresses_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/addresses_controller.rb
@@ -2,13 +2,17 @@ module Spree
module Api
module V3
module Store
- class AddressesController < ResourceController
- before_action :require_authentication!
+ class AddressesController < Store::ResourceController
+ prepend_before_action :require_authentication!
protected
- def scope
- current_user.addresses
+ def set_parent
+ @parent = current_user
+ end
+
+ def parent_association
+ :addresses
end
def model_class
@@ -16,7 +20,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_address_serializer
+ Spree.api.address_serializer
end
def permitted_params
diff --git a/api/app/controllers/spree/api/v3/store/auth_controller.rb b/api/app/controllers/spree/api/v3/store/auth_controller.rb
index 5c0b61a4ac..746d3dd812 100644
--- a/api/app/controllers/spree/api/v3/store/auth_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/auth_controller.rb
@@ -4,7 +4,7 @@ module Spree
module Store
class AuthController < Store::BaseController
skip_before_action :authenticate_user, only: [:create, :register, :oauth_callback]
- before_action :require_authentication!, only: [:refresh]
+ prepend_before_action :require_authentication!, only: [:refresh]
# POST /api/v3/store/auth/login
# Supports multiple authentication providers via :provider param
@@ -133,7 +133,7 @@ module Spree
end
def user_serializer
- Spree.api.v3_store_user_serializer
+ Spree.api.user_serializer
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/base_controller.rb b/api/app/controllers/spree/api/v3/store/base_controller.rb
index 183354efb5..fd330eef7f 100644
--- a/api/app/controllers/spree/api/v3/store/base_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/base_controller.rb
@@ -5,13 +5,6 @@ module Spree
class BaseController < Spree::Api::V3::BaseController
# Require publishable API key for all Store API requests
before_action :authenticate_api_key!
-
- protected
-
- # Store API always uses store context (not admin)
- def admin_context?
- false
- end
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/countries_controller.rb b/api/app/controllers/spree/api/v3/store/countries_controller.rb
index ad4f3f4897..70c5af6c1a 100644
--- a/api/app/controllers/spree/api/v3/store/countries_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/countries_controller.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module V3
module Store
- class CountriesController < ResourceController
+ class CountriesController < Store::ResourceController
protected
def scope
@@ -14,7 +14,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_country_serializer
+ Spree.api.country_serializer
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/credit_cards_controller.rb b/api/app/controllers/spree/api/v3/store/credit_cards_controller.rb
index d58818e711..4700243e47 100644
--- a/api/app/controllers/spree/api/v3/store/credit_cards_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/credit_cards_controller.rb
@@ -2,13 +2,17 @@ module Spree
module Api
module V3
module Store
- class CreditCardsController < ResourceController
- before_action :require_authentication!
+ class CreditCardsController < Store::ResourceController
+ prepend_before_action :require_authentication!
protected
- def scope
- current_user.credit_cards
+ def set_parent
+ @parent = current_user
+ end
+
+ def parent_association
+ :credit_cards
end
def model_class
@@ -16,7 +20,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_credit_card_serializer
+ Spree.api.credit_card_serializer
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/customers_controller.rb b/api/app/controllers/spree/api/v3/store/customers_controller.rb
index 49d1c3d946..68f184401c 100644
--- a/api/app/controllers/spree/api/v3/store/customers_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/customers_controller.rb
@@ -2,8 +2,8 @@ module Spree
module Api
module V3
module Store
- class CustomersController < BaseController
- before_action :require_authentication!
+ class CustomersController < Store::BaseController
+ prepend_before_action :require_authentication!
# GET /api/v3/store/customers/me
def show
@@ -22,11 +22,11 @@ module Spree
protected
def serializer_class
- Spree.api.v3_store_user_serializer
+ Spree.api.user_serializer
end
def permitted_params
- params.require(:customer).permit(Spree::PermittedAttributes.user_attributes)
+ params.require(:user).permit(Spree::PermittedAttributes.user_attributes)
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/digitals_controller.rb b/api/app/controllers/spree/api/v3/store/digitals_controller.rb
index 2ceed46e51..bf2d480740 100644
--- a/api/app/controllers/spree/api/v3/store/digitals_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/digitals_controller.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module V3
module Store
- class DigitalsController < ResourceController
+ class DigitalsController < Store::ResourceController
# GET /api/v3/store/digitals/:id?token=...
def show
if @resource.authorize!
@@ -25,7 +25,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_digital_link_serializer
+ Spree.api.digital_link_serializer
end
def authorize_resource!(resource = @resource, action = action_name.to_sym)
diff --git a/api/app/controllers/spree/api/v3/store/line_items_controller.rb b/api/app/controllers/spree/api/v3/store/line_items_controller.rb
index 9504afb7d2..00168391c3 100644
--- a/api/app/controllers/spree/api/v3/store/line_items_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/line_items_controller.rb
@@ -2,16 +2,16 @@ module Spree
module Api
module V3
module Store
- class LineItemsController < ResourceController
+ class LineItemsController < Store::ResourceController
include Spree::Api::V3::OrderConcern
- before_action :set_order
+ skip_before_action :set_resource
before_action :authorize_order_access!
# POST /api/v3/store/orders/:order_id/line_items
def create
result = Spree.cart_add_item_service.call(
- order: @order,
+ order: @parent,
variant: variant,
quantity: permitted_params[:quantity] || 1,
options: permitted_params[:options] || {}
@@ -27,11 +27,11 @@ module Spree
# PATCH /api/v3/store/orders/:order_id/line_items/:id
def update
- @line_item = @order.line_items.find(params[:id])
+ @line_item = scope.find(params[:id])
if permitted_params[:quantity].present?
result = Spree.cart_set_item_quantity_service.call(
- order: @order,
+ order: @parent,
line_item: @line_item,
quantity: permitted_params[:quantity]
)
@@ -48,10 +48,10 @@ module Spree
# DELETE /api/v3/store/orders/:order_id/line_items/:id
def destroy
- @line_item = @order.line_items.find(params[:id])
+ @line_item = scope.find(params[:id])
Spree.cart_remove_line_item_service.call(
- order: @order,
+ order: @parent,
line_item: @line_item
)
@@ -60,12 +60,16 @@ module Spree
protected
- def variant
- current_store.variants.accessible_by(current_ability).find(permitted_params[:variant_id])
+ def set_parent
+ @parent = current_store.orders.friendly.find(params[:order_id])
+ end
+
+ def parent_association
+ :line_items
end
- def scope
- @order.line_items
+ def variant
+ current_store.variants.accessible_by(current_ability).find(permitted_params[:variant_id])
end
def model_class
@@ -73,7 +77,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_line_item_serializer
+ Spree.api.line_item_serializer
end
def permitted_params
diff --git a/api/app/controllers/spree/api/v3/store/order_promotions_controller.rb b/api/app/controllers/spree/api/v3/store/order_promotions_controller.rb
index bb9688e29f..6d3ab5b3a4 100644
--- a/api/app/controllers/spree/api/v3/store/order_promotions_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/order_promotions_controller.rb
@@ -2,15 +2,14 @@ module Spree
module Api
module V3
module Store
- class OrderPromotionsController < ResourceController
+ class OrderPromotionsController < Store::ResourceController
include Spree::Api::V3::OrderConcern
- before_action :set_order
before_action :authorize_order_access!
# POST /api/v3/store/orders/:order_id/order_promotions
def create
- @order.coupon_code = permitted_params[:coupon_code]
+ @parent.coupon_code = permitted_params[:coupon_code]
promotion_handler.apply
@@ -36,24 +35,28 @@ module Spree
protected
+ def set_parent
+ @parent = current_store.orders.friendly.find(params[:order_id])
+ end
+
+ def parent_association
+ :order_promotions
+ end
+
def promotion_handler
- @promotion_handler ||= Spree::PromotionHandler::Coupon.new(@order)
+ @promotion_handler ||= Spree::PromotionHandler::Coupon.new(@parent)
end
def permitted_params
params.require(:order_promotion).permit(:coupon_code)
end
- def scope
- @order.order_promotions
- end
-
def model_class
Spree::OrderPromotion
end
def serializer_class
- Spree.api.v3_store_order_promotion_serializer
+ Spree.api.order_promotion_serializer
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/orders_controller.rb b/api/app/controllers/spree/api/v3/store/orders_controller.rb
index 1819fd61f9..ad55e79156 100644
--- a/api/app/controllers/spree/api/v3/store/orders_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/orders_controller.rb
@@ -2,12 +2,12 @@ module Spree
module Api
module V3
module Store
- class OrdersController < ResourceController
+ class OrdersController < Store::ResourceController
include Spree::Api::V3::OrderConcern
# Override authorization for orders
skip_before_action :set_resource, only: [:index, :create]
- before_action :require_authentication!, only: [:index]
+ prepend_before_action :require_authentication!, only: [:index]
# POST /api/v3/store/orders (public - guest checkout)
def create
@@ -24,17 +24,10 @@ module Spree
# PATCH /api/v3/store/orders/:id
def update
- result = update_service.call(
- order: @order,
- params: params,
- permitted_attributes: permitted_params,
- request_env: request.headers.env
- )
-
- if result.success?
- render json: serialize_resource(@order)
+ if @order.update(permitted_params)
+ render json: serialize_resource(@order.reload)
else
- render_service_error(result.error)
+ render_errors(@order.errors)
end
end
@@ -79,6 +72,18 @@ module Spree
protected
+ # Override scope to avoid accessible_by (Order permissions use blocks)
+ def scope
+ current_store.orders.where(user: current_user)
+ end
+
+ # Override set_resource to use friendly finder and order_token authorization
+ def set_resource
+ @order = current_store.orders.friendly.find(params[:id])
+ @resource = @order
+ authorize_resource!(@order)
+ end
+
# override authorize_resource! to pass the order token
def authorize_resource!(resource = @resource, action = action_name.to_sym)
authorize!(action, resource, order_token)
@@ -89,7 +94,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_order_serializer
+ Spree.api.order_serializer
end
def permitted_params
diff --git a/api/app/controllers/spree/api/v3/store/payment_sources_controller.rb b/api/app/controllers/spree/api/v3/store/payment_sources_controller.rb
index f5a41ed414..0e336a7269 100644
--- a/api/app/controllers/spree/api/v3/store/payment_sources_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/payment_sources_controller.rb
@@ -2,8 +2,8 @@ module Spree
module Api
module V3
module Store
- class PaymentSourcesController < ResourceController
- before_action :require_authentication!
+ class PaymentSourcesController < Store::ResourceController
+ prepend_before_action :require_authentication!
protected
@@ -16,7 +16,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_payment_source_serializer
+ Spree.api.payment_source_serializer
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/products_controller.rb b/api/app/controllers/spree/api/v3/store/products_controller.rb
index c423615637..e5a66ae82e 100644
--- a/api/app/controllers/spree/api/v3/store/products_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/products_controller.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module V3
module Store
- class ProductsController < ResourceController
+ class ProductsController < Store::ResourceController
protected
def model_class
@@ -10,7 +10,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_product_serializer
+ Spree.api.product_serializer
end
def scope
diff --git a/api/app/controllers/spree/api/v3/store/resource_controller.rb b/api/app/controllers/spree/api/v3/store/resource_controller.rb
new file mode 100644
index 0000000000..188238daf9
--- /dev/null
+++ b/api/app/controllers/spree/api/v3/store/resource_controller.rb
@@ -0,0 +1,12 @@
+module Spree
+ module Api
+ module V3
+ module Store
+ class ResourceController < Spree::Api::V3::ResourceController
+ # Require publishable API key for all Store API requests
+ before_action :authenticate_api_key!
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/controllers/spree/api/v3/store/shipments_controller.rb b/api/app/controllers/spree/api/v3/store/shipments_controller.rb
index 4018b12789..182b18c79c 100644
--- a/api/app/controllers/spree/api/v3/store/shipments_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/shipments_controller.rb
@@ -2,16 +2,19 @@ module Spree
module Api
module V3
module Store
- class ShipmentsController < ResourceController
+ class ShipmentsController < Store::ResourceController
include Spree::Api::V3::OrderConcern
- before_action :set_order
before_action :authorize_order_access!
protected
- def scope
- @order.shipments
+ def set_parent
+ @parent = current_store.orders.friendly.find(params[:order_id])
+ end
+
+ def parent_association
+ :shipments
end
def model_class
@@ -19,7 +22,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_shipment_serializer
+ Spree.api.shipment_serializer
end
def permitted_params
diff --git a/api/app/controllers/spree/api/v3/store/stores_controller.rb b/api/app/controllers/spree/api/v3/store/stores_controller.rb
index f6ddc815ed..c91dae964c 100644
--- a/api/app/controllers/spree/api/v3/store/stores_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/stores_controller.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module V3
module Store
- class StoresController < ResourceController
+ class StoresController < Store::ResourceController
# Public endpoint - no authentication required
# GET /api/v3/store/store
@@ -24,7 +24,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_store_serializer
+ Spree.api.store_serializer
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/taxonomies_controller.rb b/api/app/controllers/spree/api/v3/store/taxonomies_controller.rb
index a56ba82931..3b2c914f46 100644
--- a/api/app/controllers/spree/api/v3/store/taxonomies_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/taxonomies_controller.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module V3
module Store
- class TaxonomiesController < ResourceController
+ class TaxonomiesController < Store::ResourceController
protected
def model_class
@@ -10,7 +10,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_taxonomy_serializer
+ Spree.api.taxonomy_serializer
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/taxons_controller.rb b/api/app/controllers/spree/api/v3/store/taxons_controller.rb
index 93378e4e0c..fa9d36f098 100644
--- a/api/app/controllers/spree/api/v3/store/taxons_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/taxons_controller.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module V3
module Store
- class TaxonsController < ResourceController
+ class TaxonsController < Store::ResourceController
protected
def model_class
@@ -10,11 +10,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_taxon_serializer
- end
-
- def scope
- Spree::Taxon.for_store(current_store)
+ Spree.api.taxon_serializer
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/wishlist_items_controller.rb b/api/app/controllers/spree/api/v3/store/wishlist_items_controller.rb
index a7905b2696..c88a69e3e6 100644
--- a/api/app/controllers/spree/api/v3/store/wishlist_items_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/wishlist_items_controller.rb
@@ -2,18 +2,17 @@ module Spree
module Api
module V3
module Store
- class WishlistItemsController < ResourceController
- before_action :require_authentication!
- before_action :set_wishlist
+ class WishlistItemsController < Store::ResourceController
+ prepend_before_action :require_authentication!
protected
- def set_wishlist
- @wishlist = current_user.wishlists.find(params[:wishlist_id])
+ def set_parent
+ @parent = current_user.wishlists.find(params[:wishlist_id])
end
- def scope
- @wishlist.wished_items
+ def parent_association
+ :wished_items
end
def model_class
@@ -21,11 +20,11 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_wished_item_serializer
+ Spree.api.wished_item_serializer
end
def permitted_params
- params.require(:item).permit(Spree::PermittedAttributes.wished_item_attributes)
+ params.require(:wished_item).permit(Spree::PermittedAttributes.wished_item_attributes)
end
end
end
diff --git a/api/app/controllers/spree/api/v3/store/wishlists_controller.rb b/api/app/controllers/spree/api/v3/store/wishlists_controller.rb
index 409239fb8b..7a8cbbb9f1 100644
--- a/api/app/controllers/spree/api/v3/store/wishlists_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/wishlists_controller.rb
@@ -2,8 +2,20 @@ module Spree
module Api
module V3
module Store
- class WishlistsController < ResourceController
- before_action :require_authentication!
+ class WishlistsController < Store::ResourceController
+ prepend_before_action :require_authentication!
+
+ # POST /api/v3/store/wishlists
+ def create
+ @resource = current_user.wishlists.build(permitted_params)
+ @resource.store = current_store
+
+ if @resource.save
+ render json: serialize_resource(@resource), status: :created
+ else
+ render_errors(@resource.errors)
+ end
+ end
protected
@@ -16,7 +28,7 @@ module Spree
end
def serializer_class
- Spree.api.v3_store_wishlist_serializer
+ Spree.api.wishlist_serializer
end
def permitted_params
diff --git a/api/lib/spree/api/testing_support/v3/base.rb b/api/lib/spree/api/testing_support/v3/base.rb
new file mode 100644
index 0000000000..d52674ec16
--- /dev/null
+++ b/api/lib/spree/api/testing_support/v3/base.rb
@@ -0,0 +1,114 @@
+# Helper module for generating JWT tokens in tests
+module Spree
+ module Api
+ module V3
+ module TestingSupport
+ def self.generate_jwt(user, expiration: 24.hours.to_i)
+ user_type = user.is_a?(Spree.admin_user_class) ? 'admin' : 'customer'
+ payload = {
+ user_id: user.id,
+ user_type: user_type,
+ exp: Time.current.to_i + expiration
+ }
+ secret = Rails.application.credentials.jwt_secret_key || ENV['JWT_SECRET_KEY'] || Rails.application.secret_key_base
+ JWT.encode(payload, secret, 'HS256')
+ end
+ end
+ end
+ end
+end
+
+shared_context 'API v3 Store' do
+ let(:store) { @default_store || create(:store, default: true) }
+ let(:api_key) { create(:api_key, :publishable, store: store) }
+ let(:api_key_headers) { { 'X-Spree-Api-Key' => api_key.token } }
+
+ let(:user) { create(:user_with_addresses) }
+ let(:jwt_token) { Spree::Api::V3::TestingSupport.generate_jwt(user) }
+ let(:bearer_headers) { api_key_headers.merge('Authorization' => "Bearer #{jwt_token}") }
+end
+
+shared_context 'API v3 Store authenticated' do
+ include_context 'API v3 Store'
+
+ let(:headers) { bearer_headers }
+end
+
+shared_context 'API v3 Store guest' do
+ include_context 'API v3 Store'
+
+ let(:headers) { api_key_headers }
+end
+
+# Shared examples for common response patterns
+shared_examples 'returns 200 OK' do
+ it 'returns 200 status' do
+ subject
+ expect(response).to have_http_status(:ok)
+ end
+end
+
+shared_examples 'returns 201 Created' do
+ it 'returns 201 status' do
+ subject
+ expect(response).to have_http_status(:created)
+ end
+end
+
+shared_examples 'returns 204 No Content' do
+ it 'returns 204 status' do
+ subject
+ expect(response).to have_http_status(:no_content)
+ end
+end
+
+shared_examples 'returns 401 Unauthorized' do
+ it 'returns 401 status' do
+ subject
+ expect(response).to have_http_status(:unauthorized)
+ end
+end
+
+shared_examples 'returns 403 Forbidden' do
+ it 'returns 403 status' do
+ subject
+ expect(response).to have_http_status(:forbidden)
+ end
+end
+
+shared_examples 'returns 404 Not Found' do
+ it 'returns 404 status' do
+ subject
+ expect(response).to have_http_status(:not_found)
+ end
+end
+
+shared_examples 'returns 422 Unprocessable Entity' do
+ it 'returns 422 status' do
+ subject
+ expect(response).to have_http_status(:unprocessable_entity)
+ end
+end
+
+shared_examples 'requires API key' do
+ context 'without API key' do
+ let(:headers) { {} }
+
+ it 'returns 401 unauthorized' do
+ subject
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response[:error]).to include('API key')
+ end
+ end
+end
+
+shared_examples 'requires authentication' do
+ context 'without JWT token' do
+ let(:headers) { api_key_headers }
+
+ it 'returns 401 unauthorized' do
+ subject
+ expect(response).to have_http_status(:unauthorized)
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/http_caching_spec.rb b/api/spec/controllers/spree/api/v3/http_caching_spec.rb
deleted file mode 100644
index 6534451e7a..0000000000
--- a/api/spec/controllers/spree/api/v3/http_caching_spec.rb
+++ /dev/null
@@ -1,287 +0,0 @@
-require 'spec_helper'
-
-class HttpCachingTestController < ActionController::API
- include Spree::Api::V3::HttpCaching
-
- attr_accessor :current_user, :_current_currency, :_current_locale
-
- def current_currency
- _current_currency || 'USD'
- end
-
- def current_locale
- _current_locale || 'en'
- end
-end
-
-describe Spree::Api::V3::HttpCaching, type: :controller do
- let(:controller) { HttpCachingTestController.new }
- let(:request) { ActionDispatch::Request.new({}) }
- let(:response) { ActionDispatch::Response.new }
-
- before do
- allow(controller).to receive(:request).and_return(request)
- allow(controller).to receive(:response).and_return(response)
- allow(controller).to receive(:params).and_return(ActionController::Parameters.new(params))
- # Stub expires_in to set Cache-Control header directly
- allow(controller).to receive(:expires_in) do |duration, options = {}|
- parts = []
- parts << 'public' if options[:public]
- parts << "max-age=#{duration.to_i}"
- parts << "stale-while-revalidate=#{options[:stale_while_revalidate].to_i}" if options[:stale_while_revalidate]
- response.headers['Cache-Control'] = parts.join(', ')
- end
- end
-
- let(:params) { {} }
-
- describe '#guest_user?' do
- context 'when current_user is nil' do
- before { controller.current_user = nil }
-
- it 'returns true' do
- expect(controller.send(:guest_user?)).to be true
- end
- end
-
- context 'when current_user is present' do
- let(:user) { create(:user) }
-
- before { controller.current_user = user }
-
- it 'returns false' do
- expect(controller.send(:guest_user?)).to be false
- end
- end
- end
-
- describe '#set_vary_headers' do
- context 'for guest users' do
- before { controller.current_user = nil }
-
- it 'sets Vary header with Accept, X-Spree-Currency, X-Spree-Locale' do
- controller.send(:set_vary_headers)
- expect(response.headers['Vary']).to eq('Accept, X-Spree-Currency, X-Spree-Locale')
- end
-
- it 'does not set Cache-Control to private' do
- controller.send(:set_vary_headers)
- cache_control = response.headers['Cache-Control']
- expect(cache_control.nil? || !cache_control.include?('private')).to be true
- end
- end
-
- context 'for authenticated users' do
- let(:user) { create(:user) }
-
- before { controller.current_user = user }
-
- it 'sets Cache-Control to private, no-store' do
- controller.send(:set_vary_headers)
- expect(response.headers['Cache-Control']).to eq('private, no-store')
- end
- end
- end
-
- describe '#cache_collection' do
- let(:store) { @default_store }
- let!(:products) { create_list(:product, 3, stores: [store]) }
- let(:collection) { Spree::Product.where(id: products.map(&:id)) }
-
- context 'for guest users' do
- before do
- controller.current_user = nil
- allow(request).to receive(:fresh?).and_return(false)
- end
-
- it 'returns true to render response' do
- result = controller.send(:cache_collection, collection)
- expect(result).to be true
- end
-
- it 'sets ETag header' do
- controller.send(:cache_collection, collection)
- expect(response.headers['ETag']).to be_present
- expect(response.headers['ETag']).to match(/^"[a-f0-9]{32}"$/)
- end
-
- it 'sets Cache-Control with public, max-age and stale-while-revalidate' do
- controller.send(:cache_collection, collection)
- cache_control = response.headers['Cache-Control']
- expect(cache_control).to include('public')
- expect(cache_control).to include('max-age=300')
- expect(cache_control).to include('stale-while-revalidate=30')
- end
-
- context 'when client has fresh cache' do
- before do
- allow(request).to receive(:fresh?).and_return(true)
- end
-
- it 'returns false (304 Not Modified)' do
- result = controller.send(:cache_collection, collection)
- expect(result).to be false
- end
- end
-
- context 'with custom expires_in' do
- it 'sets custom max-age' do
- controller.send(:cache_collection, collection, expires_in: 10.minutes)
- cache_control = response.headers['Cache-Control']
- expect(cache_control).to include('max-age=600')
- end
- end
-
- context 'with custom stale_while_revalidate' do
- it 'sets custom stale-while-revalidate' do
- controller.send(:cache_collection, collection, stale_while_revalidate: 1.minute)
- cache_control = response.headers['Cache-Control']
- expect(cache_control).to include('stale-while-revalidate=60')
- end
- end
- end
-
- context 'for authenticated users' do
- let(:user) { create(:user) }
-
- before { controller.current_user = user }
-
- it 'returns true without setting cache headers' do
- result = controller.send(:cache_collection, collection)
- expect(result).to be true
- end
-
- it 'does not set ETag header' do
- controller.send(:cache_collection, collection)
- expect(response.headers['ETag']).to be_nil
- end
-
- it 'does not set public Cache-Control' do
- controller.send(:cache_collection, collection)
- cache_control = response.headers['Cache-Control']
- expect(cache_control.nil? || !cache_control.include?('public')).to be true
- end
- end
- end
-
- describe '#cache_resource' do
- let(:store) { @default_store }
- let!(:product) { create(:product, stores: [store]) }
-
- context 'for guest users' do
- before do
- controller.current_user = nil
- allow(controller).to receive(:stale?).and_return(true)
- end
-
- it 'returns true when resource is stale' do
- result = controller.send(:cache_resource, product)
- expect(result).to be true
- end
-
- it 'calls stale? with public: true' do
- expect(controller).to receive(:stale?).with(product, public: true).and_return(true)
- controller.send(:cache_resource, product)
- end
-
- it 'sets Cache-Control with public and max-age' do
- controller.send(:cache_resource, product)
- cache_control = response.headers['Cache-Control']
- expect(cache_control).to include('public')
- expect(cache_control).to include('max-age=300')
- end
-
- context 'when resource is fresh (304 Not Modified)' do
- before do
- allow(controller).to receive(:stale?).and_return(false)
- end
-
- it 'returns false' do
- result = controller.send(:cache_resource, product)
- expect(result).to be false
- end
- end
-
- context 'with custom expires_in' do
- it 'sets custom max-age' do
- controller.send(:cache_resource, product, expires_in: 15.minutes)
- cache_control = response.headers['Cache-Control']
- expect(cache_control).to include('max-age=900')
- end
- end
- end
-
- context 'for authenticated users' do
- let(:user) { create(:user) }
-
- before { controller.current_user = user }
-
- it 'returns true without calling stale?' do
- expect(controller).not_to receive(:stale?)
- result = controller.send(:cache_resource, product)
- expect(result).to be true
- end
- end
- end
-
- describe '#collection_cache_key' do
- let(:store) { @default_store }
- let!(:products) { create_list(:product, 3, stores: [store]) }
- let(:collection) { Spree::Product.where(id: products.map(&:id)) }
-
- it 'includes collection cache_key_with_version' do
- cache_key = controller.send(:collection_cache_key, collection)
- expect(cache_key).to include(collection.cache_key_with_version)
- end
-
- context 'with include param' do
- let(:params) { { include: 'variants,images' } }
-
- it 'includes the include param in cache key' do
- cache_key = controller.send(:collection_cache_key, collection)
- expect(cache_key).to include('variants,images')
- end
- end
-
- context 'with q (ransack) param' do
- let(:params) { { q: { name_cont: 'test' } } }
-
- it 'includes the q param in cache key' do
- cache_key = controller.send(:collection_cache_key, collection)
- expect(cache_key).to include('name_cont')
- end
- end
-
- context 'with pagination params' do
- let(:params) { { page: 2, per_page: 10 } }
-
- it 'includes pagination params in cache key' do
- cache_key = controller.send(:collection_cache_key, collection)
- expect(cache_key).to include('2')
- expect(cache_key).to include('10')
- end
- end
-
- it 'includes current_currency in cache key' do
- cache_key = controller.send(:collection_cache_key, collection)
- expect(cache_key).to include('USD')
- end
-
- it 'includes current_locale in cache key' do
- cache_key = controller.send(:collection_cache_key, collection)
- expect(cache_key).to include('en')
- end
-
- context 'with different currencies' do
- before do
- controller._current_currency = 'EUR'
- end
-
- it 'generates different cache keys for different currencies' do
- cache_key_eur = controller.send(:collection_cache_key, collection)
- expect(cache_key_eur).to include('EUR')
- expect(cache_key_eur).not_to include('USD')
- end
- end
- end
-end
diff --git a/api/spec/controllers/spree/api/v3/resource_controller_spec.rb b/api/spec/controllers/spree/api/v3/resource_controller_spec.rb
deleted file mode 100644
index 6dbe03207e..0000000000
--- a/api/spec/controllers/spree/api/v3/resource_controller_spec.rb
+++ /dev/null
@@ -1,182 +0,0 @@
-require 'spec_helper'
-
-class ApiV3DummyController < Spree::Api::V3::ResourceController
- private
-
- def model_class
- Spree::Product
- end
-
- def serializer_class
- Spree::Api::V3::ProductSerializer
- end
-
- def permitted_params
- {}
- end
-end
-
-describe Spree::Api::V3::ResourceController, type: :controller do
- let(:dummy_controller) { ApiV3DummyController.new }
- let(:store) { @default_store }
- let!(:products) { create_list(:product, 30, stores: [store]) }
-
- before do
- request = double('request').as_null_object
- allow(request).to receive(:base_url).and_return('http://example.com')
- allow(request).to receive(:path).and_return('/api/v3/products')
- allow(request).to receive(:query_string).and_return('')
- allow(request).to receive(:GET).and_return({})
- allow(request).to receive(:POST).and_return({})
- allow(request).to receive(:cookies).and_return({})
-
- allow(dummy_controller).to receive(:current_store).and_return(store)
- allow(dummy_controller).to receive(:current_ability).and_return(Spree::Ability.new(nil))
- allow(dummy_controller).to receive(:params).and_return(ActionController::Parameters.new(params))
- allow(dummy_controller).to receive(:request).and_return(request)
- end
-
- describe '#collection' do
- context 'with default pagination' do
- let(:params) { {} }
-
- it 'paginates with default limit of 25' do
- collection = dummy_controller.send(:collection)
- expect(collection.size).to eq(25)
- end
-
- it 'sets pagy instance variable' do
- dummy_controller.send(:collection)
- pagy = dummy_controller.instance_variable_get(:@pagy)
- expect(pagy).to be_a(Pagy)
- expect(pagy.limit).to eq(25)
- expect(pagy.page).to eq(1)
- expect(pagy.count).to eq(30)
- end
- end
-
- context 'with custom per_page parameter' do
- let(:params) { { per_page: 10 } }
-
- it 'paginates with specified limit' do
- collection = dummy_controller.send(:collection)
- expect(collection.size).to eq(10)
- end
-
- it 'sets correct pagy metadata' do
- dummy_controller.send(:collection)
- pagy = dummy_controller.instance_variable_get(:@pagy)
- expect(pagy.limit).to eq(10)
- expect(pagy.pages).to eq(3)
- end
- end
-
- context 'with custom limit parameter' do
- let(:params) { { limit: 15 } }
-
- it 'paginates with specified limit' do
- collection = dummy_controller.send(:collection)
- expect(collection.size).to eq(15)
- end
- end
-
- context 'with page parameter' do
- let(:params) { { page: 2, per_page: 10 } }
-
- it 'returns correct page' do
- collection = dummy_controller.send(:collection)
- expect(collection.size).to eq(10)
- pagy = dummy_controller.instance_variable_get(:@pagy)
- expect(pagy.page).to eq(2)
- end
- end
-
- context 'with limit exceeding maximum' do
- let(:params) { { per_page: 200 } }
-
- it 'caps at maximum of 100' do
- collection = dummy_controller.send(:collection)
- pagy = dummy_controller.instance_variable_get(:@pagy)
- expect(pagy.limit).to eq(100)
- end
- end
- end
-
- describe '#collection_meta' do
- let(:params) { { page: 1, per_page: 10 } }
-
- before do
- dummy_controller.send(:collection)
- end
-
- it 'returns data_hash with pagination metadata' do
- meta = dummy_controller.send(:collection_meta, nil)
- expect(meta).to be_a(Hash)
- expect(meta[:count]).to eq(30)
- expect(meta[:pages]).to eq(3)
- expect(meta[:page]).to eq(1)
- expect(meta[:limit]).to eq(10)
- expect(meta[:from]).to eq(1)
- expect(meta[:to]).to eq(10)
- expect(meta[:in]).to eq(10)
- end
-
- context 'on last page' do
- let(:params) { { page: 3, per_page: 10 } }
-
- it 'returns correct metadata for last page' do
- dummy_controller.send(:collection)
- meta = dummy_controller.send(:collection_meta, nil)
- expect(meta[:count]).to eq(30)
- expect(meta[:pages]).to eq(3)
- expect(meta[:page]).to eq(3)
- expect(meta[:limit]).to eq(10)
- expect(meta[:from]).to eq(21)
- expect(meta[:to]).to eq(30)
- expect(meta[:in]).to eq(10)
- end
- end
- end
-
- describe '#limit' do
- context 'with per_page parameter' do
- let(:params) { { per_page: 50 } }
-
- it 'returns the per_page value' do
- expect(dummy_controller.send(:limit)).to eq(50)
- end
- end
-
- context 'with limit parameter' do
- let(:params) { { limit: 30 } }
-
- it 'returns the limit value' do
- expect(dummy_controller.send(:limit)).to eq(30)
- end
- end
-
- context 'with both per_page and limit parameters' do
- let(:params) { { per_page: 20, limit: 30 } }
-
- it 'prefers per_page over limit' do
- expect(dummy_controller.send(:limit)).to eq(20)
- end
- end
-
- context 'without parameters' do
- let(:params) { {} }
-
- it 'returns default limit of 25' do
- expect(dummy_controller.send(:limit)).to eq(25)
- end
- end
-
- context 'with limit exceeding maximum' do
- let(:params) { { per_page: 150 } }
-
- it 'caps at 100' do
- expect(dummy_controller.send(:limit)).to eq(100)
- end
- end
- end
-end
diff --git a/api/spec/controllers/spree/api/v3/store/addresses_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/addresses_controller_spec.rb
new file mode 100644
index 0000000000..49151027ef
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/addresses_controller_spec.rb
@@ -0,0 +1,219 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::AddressesController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ let(:country) { create(:country) }
+ let(:state) { create(:state, country: country) }
+ let!(:address) { create(:address, user: user, country: country, state: state) }
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ describe 'GET #index' do
+ it 'returns user addresses' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data']).to be_present
+ end
+
+ it 'returns pagination metadata' do
+ get :index
+
+ expect(json_response['meta']).to include('page', 'count', 'pages')
+ end
+
+ context 'without authentication' do
+ before { request.headers['Authorization'] = nil }
+
+ it 'returns unauthorized' do
+ get :index
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+
+ describe 'GET #show' do
+ it 'returns the address' do
+ get :show, params: { id: address.id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(address.id)
+ expect(json_response['firstname']).to eq(address.firstname)
+ expect(json_response['lastname']).to eq(address.lastname)
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent address' do
+ get :show, params: { id: 0 }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for other users address' do
+ other_user = create(:user)
+ other_address = create(:address, user: other_user)
+
+ get :show, params: { id: other_address.id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+ end
+
+ describe 'POST #create' do
+ let(:valid_params) do
+ {
+ address: {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '123 Main St',
+ city: 'New York',
+ zipcode: '10001',
+ phone: '555-1234',
+ country_id: country.id,
+ state_id: state.id
+ }
+ }
+ end
+
+ it 'creates a new address' do
+ expect {
+ post :create, params: valid_params
+ }.to change(Spree::Address, :count).by(1)
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['firstname']).to eq('John')
+ expect(json_response['lastname']).to eq('Doe')
+ end
+
+ it 'associates address with current user' do
+ post :create, params: valid_params
+
+ expect(Spree::Address.last.user_id).to eq(user.id)
+ end
+
+ context 'validation errors' do
+ it 'returns errors for missing required fields' do
+ post :create, params: { address: { firstname: 'John' } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns errors for blank firstname' do
+ post :create, params: { address: valid_params[:address].merge(firstname: '') }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['details']['firstname']).to be_present
+ end
+
+ it 'returns errors for invalid country' do
+ post :create, params: { address: valid_params[:address].merge(country_id: 0) }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['details']['country']).to be_present
+ end
+ end
+
+ context 'without authentication' do
+ before { request.headers['Authorization'] = nil }
+
+ it 'returns unauthorized' do
+ post :create, params: valid_params
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ end
+ end
+ end
+
+ describe 'PATCH #update' do
+ it 'updates the address' do
+ patch :update, params: { id: address.id, address: { address1: 'Updated Street' } }
+
+ expect(response).to have_http_status(:ok)
+ expect(address.reload.address1).to eq('Updated Street')
+ end
+
+ it 'updates multiple fields' do
+ patch :update, params: {
+ id: address.id,
+ address: { firstname: 'Jane', lastname: 'Smith', city: 'Boston' }
+ }
+
+ expect(response).to have_http_status(:ok)
+ address.reload
+ expect(address.firstname).to eq('Jane')
+ expect(address.lastname).to eq('Smith')
+ expect(address.city).to eq('Boston')
+ end
+
+ context 'validation errors' do
+ it 'returns errors for invalid update' do
+ patch :update, params: { id: address.id, address: { firstname: '' } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['details']['firstname']).to be_present
+ end
+ end
+
+ context 'error handling' do
+ it 'returns not found for other users address' do
+ other_user = create(:user)
+ other_address = create(:address, user: other_user)
+
+ patch :update, params: { id: other_address.id, address: { address1: 'Hack Street' } }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ it 'deletes the address' do
+ expect {
+ delete :destroy, params: { id: address.id }
+ }.to change(Spree::Address, :count).by(-1)
+
+ expect(response).to have_http_status(:no_content)
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent address' do
+ delete :destroy, params: { id: 0 }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for other users address' do
+ other_user = create(:user)
+ other_address = create(:address, user: other_user)
+
+ delete :destroy, params: { id: other_address.id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/auth_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/auth_controller_spec.rb
new file mode 100644
index 0000000000..835388fccb
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/auth_controller_spec.rb
@@ -0,0 +1,178 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::AuthController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ end
+
+ describe 'POST #create (login)' do
+ let!(:existing_user) { create(:user, password: 'password123', password_confirmation: 'password123') }
+
+ it 'authenticates with email and password' do
+ post :create, params: { provider: 'email', email: existing_user.email, password: 'password123' }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['token']).to be_present
+ end
+
+ it 'returns user data on successful login' do
+ post :create, params: { provider: 'email', email: existing_user.email, password: 'password123' }
+
+ expect(json_response['user']).to be_present
+ expect(json_response['user']['email']).to eq(existing_user.email)
+ end
+
+ context 'invalid credentials' do
+ it 'returns unauthorized for wrong password' do
+ post :create, params: { provider: 'email', email: existing_user.email, password: 'wrong' }
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_failed')
+ end
+
+ it 'returns unauthorized for non-existent email' do
+ post :create, params: { provider: 'email', email: '[email protected]', password: 'password123' }
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_failed')
+ end
+
+ it 'returns unauthorized for missing email' do
+ post :create, params: { provider: 'email', password: 'password123' }
+
+ expect(response).to have_http_status(:unauthorized)
+ end
+
+ it 'returns unauthorized for missing password' do
+ post :create, params: { provider: 'email', email: existing_user.email }
+
+ expect(response).to have_http_status(:unauthorized)
+ end
+ end
+
+ context 'without API key' do
+ before { request.headers['X-Spree-Api-Key'] = nil }
+
+ it 'returns unauthorized' do
+ post :create, params: { provider: 'email', email: existing_user.email, password: 'password123' }
+
+ expect(response).to have_http_status(:unauthorized)
+ end
+ end
+ end
+
+ describe 'POST #register' do
+ let(:valid_params) do
+ {
+ user: {
+ email: '[email protected]',
+ password: 'password123',
+ password_confirmation: 'password123'
+ }
+ }
+ end
+
+ it 'creates a new user' do
+ expect {
+ post :register, params: valid_params
+ }.to change(Spree.user_class, :count).by(1)
+
+ expect(response).to have_http_status(:created)
+ end
+
+ it 'returns a token' do
+ post :register, params: valid_params
+
+ expect(json_response['token']).to be_present
+ end
+
+ it 'returns user data' do
+ post :register, params: valid_params
+
+ expect(json_response['user']).to be_present
+ expect(json_response['user']['email']).to eq('[email protected]')
+ end
+
+ context 'validation errors' do
+ it 'returns validation error for blank email' do
+ post :register, params: { user: { email: '', password: 'password123', password_confirmation: 'password123' } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['details']['email']).to be_present
+ end
+
+ it 'returns validation error for duplicate email' do
+ existing_user = create(:user)
+ post :register, params: { user: { email: existing_user.email, password: 'password123', password_confirmation: 'password123' } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['details']['email']).to be_present
+ end
+
+ end
+
+ context 'without API key' do
+ before { request.headers['X-Spree-Api-Key'] = nil }
+
+ it 'returns unauthorized' do
+ post :register, params: valid_params
+
+ expect(response).to have_http_status(:unauthorized)
+ end
+ end
+ end
+
+ describe 'POST #refresh' do
+ before do
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ it 'returns a new token' do
+ post :refresh
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['token']).to be_present
+ end
+
+ context 'without token' do
+ before { request.headers['Authorization'] = nil }
+
+ it 'returns unauthorized' do
+ post :refresh
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ end
+ end
+
+ context 'with invalid token' do
+ before { request.headers['Authorization'] = 'Bearer invalid' }
+
+ it 'returns unauthorized' do
+ post :refresh
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ end
+ end
+
+ context 'with expired token' do
+ let(:expired_token) { Spree::Api::V3::TestingSupport.generate_jwt(user, expiration: -1.hour.to_i) }
+
+ before { request.headers['Authorization'] = "Bearer #{expired_token}" }
+
+ it 'returns unauthorized' do
+ post :refresh
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/countries_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/countries_controller_spec.rb
new file mode 100644
index 0000000000..1b55e79147
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/countries_controller_spec.rb
@@ -0,0 +1,84 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::CountriesController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ let!(:country) { create(:country) }
+ let!(:country2) { create(:country) }
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ end
+
+ describe 'GET #index' do
+ it 'returns list of countries' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data']).to be_present
+ expect(json_response['data'].size).to be >= 2
+ end
+
+ it 'returns country attributes' do
+ get :index
+
+ country_data = json_response['data'].first
+ expect(country_data).to include('id', 'name', 'iso')
+ end
+
+ it 'returns pagination metadata' do
+ get :index
+
+ expect(json_response['meta']).to include('page', 'count', 'pages')
+ end
+
+ context 'without API key' do
+ before { request.headers['X-Spree-Api-Key'] = nil }
+
+ it 'returns unauthorized' do
+ get :index
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('invalid_token')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+
+ describe 'GET #show' do
+ it 'returns the country by id' do
+ get :show, params: { id: country.id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(country.id)
+ expect(json_response['name']).to eq(country.name)
+ expect(json_response['iso']).to eq(country.iso)
+ end
+
+ it 'includes states when available' do
+ state = create(:state, country: country)
+ get :show, params: { id: country.id }
+
+ expect(response).to have_http_status(:ok)
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent country' do
+ get :show, params: { id: 0 }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for invalid id' do
+ get :show, params: { id: 'invalid' }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rb
new file mode 100644
index 0000000000..dc2f2c7206
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rb
@@ -0,0 +1,120 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::CustomersController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ describe 'GET #show' do
+ it 'returns the current user' do
+ get :show, params: { id: 'me' }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(user.id)
+ expect(json_response['email']).to eq(user.email)
+ end
+
+ it 'returns user attributes' do
+ get :show, params: { id: 'me' }
+
+ expect(json_response).to include('id', 'email', 'first_name', 'last_name')
+ end
+
+ context 'without authentication' do
+ before { request.headers['Authorization'] = nil }
+
+ it 'returns unauthorized' do
+ get :show, params: { id: 'me' }
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+
+ context 'with invalid token' do
+ before { request.headers['Authorization'] = 'Bearer invalid' }
+
+ it 'returns unauthorized' do
+ get :show, params: { id: 'me' }
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+
+ context 'with expired token' do
+ let(:expired_token) { Spree::Api::V3::TestingSupport.generate_jwt(user, expiration: -1.hour.to_i) }
+
+ before { request.headers['Authorization'] = "Bearer #{expired_token}" }
+
+ it 'returns unauthorized' do
+ get :show, params: { id: 'me' }
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+
+ describe 'PATCH #update' do
+ it 'updates the current user first name' do
+ patch :update, params: { id: 'me', user: { first_name: 'Updated' } }
+
+ expect(response).to have_http_status(:ok)
+ expect(user.reload.first_name).to eq('Updated')
+ end
+
+ it 'updates multiple fields' do
+ patch :update, params: { id: 'me', user: { first_name: 'John', last_name: 'Doe' } }
+
+ expect(response).to have_http_status(:ok)
+ user.reload
+ expect(user.first_name).to eq('John')
+ expect(user.last_name).to eq('Doe')
+ end
+
+ it 'returns updated user data' do
+ patch :update, params: { id: 'me', user: { first_name: 'Updated' } }
+
+ expect(json_response['first_name']).to eq('Updated')
+ end
+
+ context 'validation errors' do
+ it 'returns errors for blank email' do
+ patch :update, params: { id: 'me', user: { email: '' } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['details']['email']).to be_present
+ end
+
+ it 'returns errors for duplicate email' do
+ other_user = create(:user)
+ patch :update, params: { id: 'me', user: { email: other_user.email } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['details']['email']).to be_present
+ end
+ end
+
+ context 'without authentication' do
+ before { request.headers['Authorization'] = nil }
+
+ it 'returns unauthorized' do
+ patch :update, params: { id: 'me', user: { first_name: 'Updated' } }
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/line_items_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/line_items_controller_spec.rb
new file mode 100644
index 0000000000..cb615dc48b
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/line_items_controller_spec.rb
@@ -0,0 +1,168 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::LineItemsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ let(:product) { create(:product, stores: [store]) }
+ let(:variant) { create(:variant, product: product) }
+ let(:order) { create(:order, user: user, store: store) }
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ describe 'POST #create' do
+ it 'adds a line item to the order' do
+ expect {
+ post :create, params: { order_id: order.number, line_item: { variant_id: variant.id, quantity: 2 } }
+ }.to change(Spree::LineItem, :count).by(1)
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['variant_id']).to eq(variant.id)
+ expect(json_response['quantity']).to eq(2)
+ end
+
+ it 'defaults quantity to 1' do
+ post :create, params: { order_id: order.number, line_item: { variant_id: variant.id } }
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['quantity']).to eq(1)
+ end
+
+ it 'updates order totals' do
+ post :create, params: { order_id: order.number, line_item: { variant_id: variant.id, quantity: 2 } }
+
+ expect(order.reload.item_total).to be > 0
+ end
+
+ context 'with order token (guest)' do
+ let(:guest_order) { create(:order, store: store) }
+
+ before { request.headers['Authorization'] = nil }
+
+ it 'adds line item with valid order token' do
+ request.headers['X-Spree-Order-Token'] = guest_order.token
+ post :create, params: { order_id: guest_order.number, line_item: { variant_id: variant.id, quantity: 1 } }
+
+ expect(response).to have_http_status(:created)
+ end
+
+ it 'returns forbidden without order token' do
+ post :create, params: { order_id: guest_order.number, line_item: { variant_id: variant.id, quantity: 1 } }
+
+ expect(response).to have_http_status(:forbidden)
+ expect(json_response['error']['code']).to eq('access_denied')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+
+ context 'validation errors' do
+ it 'returns error for invalid variant' do
+ post :create, params: { order_id: order.number, line_item: { variant_id: 0, quantity: 1 } }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('variant_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent order' do
+ post :create, params: { order_id: 'invalid', line_item: { variant_id: variant.id, quantity: 1 } }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('order_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns forbidden for other users order' do
+ other_order = create(:order, store: store)
+
+ post :create, params: { order_id: other_order.number, line_item: { variant_id: variant.id, quantity: 1 } }
+
+ expect(response).to have_http_status(:forbidden)
+ expect(json_response['error']['code']).to eq('access_denied')
+ end
+ end
+ end
+
+ describe 'PATCH #update' do
+ let!(:line_item) { create(:line_item, order: order, variant: variant, quantity: 1) }
+
+ it 'updates line item quantity' do
+ patch :update, params: { order_id: order.number, id: line_item.id, line_item: { quantity: 5 } }
+
+ expect(response).to have_http_status(:ok)
+ expect(line_item.reload.quantity).to eq(5)
+ end
+
+ it 'updates order totals' do
+ original_total = order.item_total
+ patch :update, params: { order_id: order.number, id: line_item.id, line_item: { quantity: 5 } }
+
+ expect(order.reload.item_total).to be > original_total
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent line item' do
+ patch :update, params: { order_id: order.number, id: 0, line_item: { quantity: 5 } }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('line_item_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns forbidden for line item in other users order' do
+ other_order = create(:order, store: store)
+ other_line_item = create(:line_item, order: other_order, variant: variant)
+
+ patch :update, params: { order_id: other_order.number, id: other_line_item.id, line_item: { quantity: 5 } }
+
+ expect(response).to have_http_status(:forbidden)
+ expect(json_response['error']['code']).to eq('access_denied')
+ end
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ let!(:line_item) { create(:line_item, order: order, variant: variant) }
+
+ it 'removes line item from order' do
+ expect {
+ delete :destroy, params: { order_id: order.number, id: line_item.id }
+ }.to change(Spree::LineItem, :count).by(-1)
+
+ expect(response).to have_http_status(:no_content)
+ end
+
+ it 'updates order totals' do
+ order.update_columns(item_total: 100)
+ delete :destroy, params: { order_id: order.number, id: line_item.id }
+
+ expect(order.reload.item_total).to eq(0)
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent line item' do
+ delete :destroy, params: { order_id: order.number, id: 0 }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('line_item_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns forbidden for line item in other users order' do
+ other_order = create(:order, store: store)
+ other_line_item = create(:line_item, order: other_order, variant: variant)
+
+ delete :destroy, params: { order_id: other_order.number, id: other_line_item.id }
+
+ expect(response).to have_http_status(:forbidden)
+ expect(json_response['error']['code']).to eq('access_denied')
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/orders_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/orders_controller_spec.rb
new file mode 100644
index 0000000000..b3465b42f5
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/orders_controller_spec.rb
@@ -0,0 +1,222 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::OrdersController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ end
+
+ describe 'POST #create' do
+ it 'creates a new cart' do
+ expect {
+ post :create
+ }.to change(Spree::Order, :count).by(1)
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['number']).to be_present
+ expect(json_response['state']).to eq('cart')
+ end
+
+ it 'creates cart associated with current store' do
+ post :create
+
+ expect(Spree::Order.last.store_id).to eq(store.id)
+ end
+
+ context 'for authenticated user' do
+ before do
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ it 'creates a cart associated with user' do
+ post :create
+
+ expect(response).to have_http_status(:created)
+ expect(Spree::Order.last.user_id).to eq(user.id)
+ end
+ end
+
+ context 'without API key' do
+ before { request.headers['X-Spree-Api-Key'] = nil }
+
+ it 'returns unauthorized' do
+ post :create
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('invalid_token')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+
+ describe 'GET #index' do
+ let!(:order) { create(:order_with_line_items, user: user, store: store) }
+ let!(:other_user_order) { create(:order_with_line_items, store: store) }
+
+ context 'authenticated' do
+ before do
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ it 'returns user orders' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data'].size).to eq(1)
+ expect(json_response['data'].first['number']).to eq(order.number)
+ end
+
+ it 'does not return other users orders' do
+ get :index
+
+ numbers = json_response['data'].map { |o| o['number'] }
+ expect(numbers).not_to include(other_user_order.number)
+ end
+
+ it 'returns pagination metadata' do
+ get :index
+
+ expect(json_response['meta']).to include('page', 'count', 'pages')
+ end
+ end
+
+ context 'unauthenticated' do
+ it 'returns unauthorized' do
+ get :index
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+
+ describe 'GET #show' do
+ let(:order) { create(:order_with_line_items, user: user, store: store) }
+
+ context 'authenticated user' do
+ before do
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ it 'returns the order' do
+ get :show, params: { id: order.number }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['number']).to eq(order.number)
+ expect(json_response['state']).to eq(order.state)
+ end
+
+ it 'returns order with expected attributes' do
+ get :show, params: { id: order.number }
+
+ expect(json_response['id']).to eq(order.id)
+ expect(json_response['number']).to eq(order.number)
+ end
+ end
+
+ context 'with order token' do
+ let(:guest_order) { create(:order_with_line_items, store: store) }
+
+ it 'returns the order for guest with valid token' do
+ request.headers['X-Spree-Order-Token'] = guest_order.token
+ get :show, params: { id: guest_order.number }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['number']).to eq(guest_order.number)
+ end
+
+ it 'returns forbidden with invalid token' do
+ request.headers['X-Spree-Order-Token'] = 'invalid'
+ get :show, params: { id: guest_order.number }
+
+ expect(response).to have_http_status(:forbidden)
+ expect(json_response['error']['code']).to eq('access_denied')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns forbidden without token' do
+ get :show, params: { id: guest_order.number }
+
+ expect(response).to have_http_status(:forbidden)
+ expect(json_response['error']['code']).to eq('access_denied')
+ end
+ end
+
+ context 'error handling' do
+ before do
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ it 'returns not found for invalid order number' do
+ get :show, params: { id: 'invalid' }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('order_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns forbidden for other users order' do
+ other_order = create(:order_with_line_items, store: store)
+ get :show, params: { id: other_order.number }
+
+ expect(response).to have_http_status(:forbidden)
+ expect(json_response['error']['code']).to eq('access_denied')
+ end
+ end
+ end
+
+ describe 'PATCH #update' do
+ let(:order) { create(:order_with_line_items, user: user, store: store) }
+
+ context 'authenticated' do
+ before do
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ it 'updates the order email' do
+ patch :update, params: { id: order.number, order: { email: '[email protected]' } }
+
+ expect(response).to have_http_status(:ok)
+ expect(order.reload.email).to eq('[email protected]')
+ end
+
+ it 'updates special instructions' do
+ patch :update, params: { id: order.number, order: { special_instructions: 'Leave at door' } }
+
+ expect(response).to have_http_status(:ok)
+ expect(order.reload.special_instructions).to eq('Leave at door')
+ end
+ end
+
+ context 'with order token' do
+ let(:guest_order) { create(:order_with_line_items, store: store) }
+
+ it 'updates order for guest with valid token' do
+ request.headers['X-Spree-Order-Token'] = guest_order.token
+ patch :update, params: { id: guest_order.number, order: { email: '[email protected]' } }
+
+ expect(response).to have_http_status(:ok)
+ expect(guest_order.reload.email).to eq('[email protected]')
+ end
+ end
+
+ context 'error handling' do
+ before do
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ it 'returns forbidden for other users order' do
+ other_order = create(:order_with_line_items, store: store)
+ patch :update, params: { id: other_order.number, order: { email: '[email protected]' } }
+
+ expect(response).to have_http_status(:forbidden)
+ expect(json_response['error']['code']).to eq('access_denied')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
new file mode 100644
index 0000000000..48cb4e88f3
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
@@ -0,0 +1,147 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ let!(:product) { create(:product, stores: [store], status: 'active') }
+ let!(:product2) { create(:product, stores: [store], status: 'active') }
+ let!(:draft_product) { create(:product, stores: [store], status: 'draft') }
+ let!(:other_store) { create(:store) }
+ let!(:other_store_product) { create(:product, stores: [other_store]) }
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ end
+
+ describe 'GET #index' do
+ it 'returns a list of products' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data'].size).to eq(2)
+ end
+
+ it 'returns product attributes' do
+ get :index
+
+ product_data = json_response['data'].first
+ expect(product_data).to include('id', 'name', 'slug')
+ end
+
+ it 'returns pagination metadata' do
+ get :index, params: { page: 1, per_page: 1 }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data'].size).to eq(1)
+ expect(json_response['meta']).to include(
+ 'page' => 1,
+ 'limit' => 1,
+ 'count' => 2,
+ 'pages' => 2
+ )
+ end
+
+ it 'respects max per_page limit' do
+ get :index, params: { per_page: 500 }
+
+ expect(json_response['meta']['limit']).to eq(100)
+ end
+
+ context 'store scoping' do
+ it 'does not return products from other stores' do
+ get :index
+
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).not_to include(other_store_product.id)
+ end
+ end
+
+ context 'status scoping' do
+ it 'does not return draft products' do
+ get :index
+
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).not_to include(draft_product.id)
+ end
+ end
+
+ context 'ransack filtering' do
+ it 'filters products by name' do
+ get :index, params: { q: { name_cont: product.name } }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data'].size).to eq(1)
+ expect(json_response['data'].first['id']).to eq(product.id)
+ end
+ end
+
+ context 'authentication' do
+ context 'without API key' do
+ before { request.headers['X-Spree-Api-Key'] = nil }
+
+ it 'returns unauthorized' do
+ get :index
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('invalid_token')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+
+ context 'with invalid API key' do
+ before { request.headers['X-Spree-Api-Key'] = 'invalid' }
+
+ it 'returns unauthorized' do
+ get :index
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('invalid_token')
+ end
+ end
+ end
+ end
+
+ describe 'GET #show' do
+ it 'returns a product by id' do
+ get :show, params: { id: product.id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(product.id)
+ expect(json_response['name']).to eq(product.name)
+ expect(json_response['slug']).to eq(product.slug)
+ end
+
+ it 'returns a product by slug' do
+ get :show, params: { id: product.slug }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(product.id)
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent product' do
+ get :show, params: { id: 'non-existent' }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for product from another store' do
+ get :show, params: { id: other_store_product.id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+
+ it 'returns not found for draft product' do
+ get :show, params: { id: draft_product.id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/stores_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/stores_controller_spec.rb
new file mode 100644
index 0000000000..5b6f54f5e3
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/stores_controller_spec.rb
@@ -0,0 +1,69 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::StoresController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ end
+
+ describe 'GET #current' do
+ it 'returns the current store' do
+ get :current
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(store.id)
+ end
+
+ it 'returns store attributes' do
+ get :current
+
+ expect(json_response).to include('id', 'name', 'url', 'default_currency')
+ end
+
+ it 'returns store settings' do
+ get :current
+
+ expect(json_response['default_currency']).to eq(store.default_currency)
+ end
+
+ context 'without API key' do
+ before { request.headers['X-Spree-Api-Key'] = nil }
+
+ it 'returns unauthorized' do
+ get :current
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('invalid_token')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+
+ context 'with invalid API key' do
+ before { request.headers['X-Spree-Api-Key'] = 'invalid' }
+
+ it 'returns unauthorized' do
+ get :current
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('invalid_token')
+ end
+ end
+
+ context 'with API key from different store' do
+ let(:other_store) { create(:store) }
+ let(:other_api_key) { create(:api_key, :publishable, store: other_store) }
+
+ before { request.headers['X-Spree-Api-Key'] = other_api_key.token }
+
+ it 'returns unauthorized as API keys are scoped to their store' do
+ get :current
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('invalid_token')
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/taxonomies_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/taxonomies_controller_spec.rb
new file mode 100644
index 0000000000..259940828a
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/taxonomies_controller_spec.rb
@@ -0,0 +1,91 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::TaxonomiesController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ let!(:taxonomy) { create(:taxonomy, store: store) }
+ let!(:taxonomy2) { create(:taxonomy, store: store) }
+ let!(:other_store) { create(:store) }
+ let!(:other_store_taxonomy) { create(:taxonomy, store: other_store) }
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ end
+
+ describe 'GET #index' do
+ it 'returns taxonomies' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data'].size).to be >= 2
+ end
+
+ it 'returns taxonomy attributes' do
+ get :index
+
+ taxonomy_data = json_response['data'].first
+ expect(taxonomy_data).to include('id', 'name')
+ end
+
+ it 'does not return taxonomies from other stores' do
+ get :index
+
+ ids = json_response['data'].map { |t| t['id'] }
+ expect(ids).not_to include(other_store_taxonomy.id)
+ end
+
+ it 'returns pagination metadata' do
+ get :index
+
+ expect(json_response['meta']).to include('page', 'count', 'pages')
+ end
+
+ context 'without API key' do
+ before { request.headers['X-Spree-Api-Key'] = nil }
+
+ it 'returns unauthorized' do
+ get :index
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('invalid_token')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+
+ describe 'GET #show' do
+ it 'returns the taxonomy' do
+ get :show, params: { id: taxonomy.id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(taxonomy.id)
+ expect(json_response['name']).to eq(taxonomy.name)
+ end
+
+ it 'returns taxonomy with its attributes' do
+ get :show, params: { id: taxonomy.id }
+
+ expect(json_response['id']).to eq(taxonomy.id)
+ expect(json_response['name']).to be_present
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent taxonomy' do
+ get :show, params: { id: 0 }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for taxonomy from another store' do
+ get :show, params: { id: other_store_taxonomy.id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/taxons_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/taxons_controller_spec.rb
new file mode 100644
index 0000000000..9a22a54ed5
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/taxons_controller_spec.rb
@@ -0,0 +1,76 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::TaxonsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ let(:taxonomy) { create(:taxonomy, store: store) }
+ let!(:taxon) { create(:taxon, taxonomy: taxonomy) }
+ let!(:child_taxon) { create(:taxon, taxonomy: taxonomy, parent: taxon) }
+ let!(:other_store) { create(:store) }
+ let!(:other_taxonomy) { create(:taxonomy, store: other_store) }
+ let!(:other_store_taxon) { create(:taxon, taxonomy: other_taxonomy) }
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ end
+
+ describe 'GET #show' do
+ it 'returns the taxon' do
+ get :show, params: { id: taxon.id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(taxon.id)
+ expect(json_response['name']).to eq(taxon.name)
+ end
+
+ it 'returns taxon attributes' do
+ get :show, params: { id: taxon.id }
+
+ expect(json_response).to include('id', 'name', 'permalink')
+ end
+
+ it 'includes parent information' do
+ get :show, params: { id: child_taxon.id }
+
+ expect(response).to have_http_status(:ok)
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent taxon' do
+ get :show, params: { id: 0 }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for taxon from another store' do
+ get :show, params: { id: other_store_taxon.id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+
+ it 'returns not found for invalid id' do
+ get :show, params: { id: 'invalid' }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+
+ context 'without API key' do
+ before { request.headers['X-Spree-Api-Key'] = nil }
+
+ it 'returns unauthorized' do
+ get :show, params: { id: taxon.id }
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('invalid_token')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/wishlist_items_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/wishlist_items_controller_spec.rb
new file mode 100644
index 0000000000..4ab1120cfd
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/wishlist_items_controller_spec.rb
@@ -0,0 +1,159 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::WishlistItemsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ let(:wishlist) { create(:wishlist, user: user, store: store) }
+ let(:product) { create(:product, stores: [store]) }
+ let(:variant) { create(:variant, product: product) }
+ let!(:wished_item) { create(:wished_item, wishlist: wishlist, variant: variant) }
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ describe 'POST #create' do
+ let(:new_product) { create(:product, stores: [store]) }
+ let(:new_variant) { create(:variant, product: new_product) }
+
+ it 'adds item to wishlist' do
+ expect {
+ post :create, params: { wishlist_id: wishlist.id, wished_item: { variant_id: new_variant.id, quantity: 1 } }
+ }.to change(Spree::WishedItem, :count).by(1)
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['variant_id']).to eq(new_variant.id)
+ end
+
+ it 'sets correct quantity' do
+ post :create, params: { wishlist_id: wishlist.id, wished_item: { variant_id: new_variant.id, quantity: 3 } }
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['quantity']).to eq(3)
+ end
+
+ context 'validation errors' do
+ it 'returns errors for missing variant_id' do
+ post :create, params: { wishlist_id: wishlist.id, wished_item: { quantity: 1 } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns errors for invalid variant_id' do
+ post :create, params: { wishlist_id: wishlist.id, wished_item: { variant_id: 0, quantity: 1 } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['details']['variant']).to be_present
+ end
+ end
+
+ context 'error handling' do
+ it 'returns not found for other users wishlist' do
+ other_user = create(:user)
+ other_wishlist = create(:wishlist, user: other_user, store: store)
+
+ post :create, params: { wishlist_id: other_wishlist.id, wished_item: { variant_id: new_variant.id, quantity: 1 } }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for non-existent wishlist' do
+ post :create, params: { wishlist_id: 0, wished_item: { variant_id: new_variant.id, quantity: 1 } }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+
+ context 'without authentication' do
+ before { request.headers['Authorization'] = nil }
+
+ it 'returns unauthorized' do
+ post :create, params: { wishlist_id: wishlist.id, wished_item: { variant_id: new_variant.id, quantity: 1 } }
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+
+ describe 'PATCH #update' do
+ it 'updates wished item quantity' do
+ patch :update, params: { wishlist_id: wishlist.id, id: wished_item.id, wished_item: { quantity: 5 } }
+
+ expect(response).to have_http_status(:ok)
+ expect(wished_item.reload.quantity).to eq(5)
+ end
+
+ context 'validation errors' do
+ it 'returns errors for invalid quantity' do
+ patch :update, params: { wishlist_id: wishlist.id, id: wished_item.id, wished_item: { quantity: 0 } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['details']['quantity']).to be_present
+ end
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent item' do
+ patch :update, params: { wishlist_id: wishlist.id, id: 0, wished_item: { quantity: 5 } }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for item in other users wishlist' do
+ other_user = create(:user)
+ other_wishlist = create(:wishlist, user: other_user, store: store)
+ other_item = create(:wished_item, wishlist: other_wishlist, variant: variant)
+
+ patch :update, params: { wishlist_id: other_wishlist.id, id: other_item.id, wished_item: { quantity: 5 } }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ it 'removes item from wishlist' do
+ expect {
+ delete :destroy, params: { wishlist_id: wishlist.id, id: wished_item.id }
+ }.to change(Spree::WishedItem, :count).by(-1)
+
+ expect(response).to have_http_status(:no_content)
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent item' do
+ delete :destroy, params: { wishlist_id: wishlist.id, id: 0 }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for item in other users wishlist' do
+ other_user = create(:user)
+ other_wishlist = create(:wishlist, user: other_user, store: store)
+ other_item = create(:wished_item, wishlist: other_wishlist, variant: variant)
+
+ delete :destroy, params: { wishlist_id: other_wishlist.id, id: other_item.id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+ end
+end
diff --git a/api/spec/controllers/spree/api/v3/store/wishlists_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/wishlists_controller_spec.rb
new file mode 100644
index 0000000000..6a0c09a331
--- /dev/null
+++ b/api/spec/controllers/spree/api/v3/store/wishlists_controller_spec.rb
@@ -0,0 +1,188 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::WishlistsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ let!(:wishlist) { create(:wishlist, user: user, store: store) }
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ describe 'GET #index' do
+ it 'returns user wishlists' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data'].size).to eq(1)
+ expect(json_response['data'].first['id']).to eq(wishlist.id)
+ end
+
+ it 'does not return other users wishlists' do
+ other_user = create(:user)
+ other_wishlist = create(:wishlist, user: other_user, store: store)
+
+ get :index
+
+ ids = json_response['data'].map { |w| w['id'] }
+ expect(ids).not_to include(other_wishlist.id)
+ end
+
+ it 'returns pagination metadata' do
+ get :index
+
+ expect(json_response['meta']).to include('page', 'count', 'pages')
+ end
+
+ context 'without authentication' do
+ before { request.headers['Authorization'] = nil }
+
+ it 'returns unauthorized' do
+ get :index
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ expect(json_response['error']['message']).to be_present
+ end
+ end
+ end
+
+ describe 'GET #show' do
+ it 'returns the wishlist' do
+ get :show, params: { id: wishlist.id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(wishlist.id)
+ expect(json_response['name']).to eq(wishlist.name)
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent wishlist' do
+ get :show, params: { id: 0 }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for other users wishlist' do
+ other_user = create(:user)
+ other_wishlist = create(:wishlist, user: other_user, store: store)
+
+ get :show, params: { id: other_wishlist.id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+ end
+
+ describe 'POST #create' do
+ it 'creates a new wishlist' do
+ expect {
+ post :create, params: { wishlist: { name: 'My New Wishlist' } }
+ }.to change(Spree::Wishlist, :count).by(1)
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['name']).to eq('My New Wishlist')
+ end
+
+ it 'associates wishlist with current user' do
+ post :create, params: { wishlist: { name: 'Test Wishlist' } }
+
+ expect(Spree::Wishlist.last.user_id).to eq(user.id)
+ end
+
+ it 'associates wishlist with current store' do
+ post :create, params: { wishlist: { name: 'Test Wishlist' } }
+
+ expect(Spree::Wishlist.last.store_id).to eq(store.id)
+ end
+
+ context 'validation errors' do
+ it 'returns errors for blank name' do
+ post :create, params: { wishlist: { name: '' } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['message']).to be_present
+ expect(json_response['error']['details']['name']).to be_present
+ end
+ end
+
+ context 'without authentication' do
+ before { request.headers['Authorization'] = nil }
+
+ it 'returns unauthorized' do
+ post :create, params: { wishlist: { name: 'Test' } }
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(json_response['error']['code']).to eq('authentication_required')
+ end
+ end
+ end
+
+ describe 'PATCH #update' do
+ it 'updates the wishlist name' do
+ patch :update, params: { id: wishlist.id, wishlist: { name: 'Updated Name' } }
+
+ expect(response).to have_http_status(:ok)
+ expect(wishlist.reload.name).to eq('Updated Name')
+ end
+
+ context 'validation errors' do
+ it 'returns errors for blank name' do
+ patch :update, params: { id: wishlist.id, wishlist: { name: '' } }
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response['error']['code']).to eq('validation_error')
+ expect(json_response['error']['details']['name']).to be_present
+ end
+ end
+
+ context 'error handling' do
+ it 'returns not found for other users wishlist' do
+ other_user = create(:user)
+ other_wishlist = create(:wishlist, user: other_user, store: store)
+
+ patch :update, params: { id: other_wishlist.id, wishlist: { name: 'Hacked' } }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ it 'deletes the wishlist' do
+ expect {
+ delete :destroy, params: { id: wishlist.id }
+ }.to change(Spree::Wishlist, :count).by(-1)
+
+ expect(response).to have_http_status(:no_content)
+ end
+
+ context 'error handling' do
+ it 'returns not found for non-existent wishlist' do
+ delete :destroy, params: { id: 0 }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ expect(json_response['error']['message']).to be_present
+ end
+
+ it 'returns not found for other users wishlist' do
+ other_user = create(:user)
+ other_wishlist = create(:wishlist, user: other_user, store: store)
+
+ delete :destroy, params: { id: other_wishlist.id }
+
+ expect(response).to have_http_status(:not_found)
+ expect(json_response['error']['code']).to eq('record_not_found')
+ end
+ end
+ end
+end
diff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rb
index 2af506d8ac..74470d073b 100644
--- a/api/spec/spec_helper.rb
+++ b/api/spec/spec_helper.rb
@@ -50,6 +50,7 @@ require 'spree/api/testing_support/v2/base'
require 'spree/api/testing_support/v2/current_order'
require 'spree/api/testing_support/v2/platform_contexts'
require 'spree/api/testing_support/v2/serializers_params'
+require 'spree/api/testing_support/v3/base'
require 'spree/api/testing_support/factories'
def json_response
diff --git a/core/app/jobs/spree/api_key_touch_job.rb b/core/app/jobs/spree/api_key_touch_job.rb
new file mode 100644
index 0000000000..b1f941f272
--- /dev/null
+++ b/core/app/jobs/spree/api_key_touch_job.rb
@@ -0,0 +1,9 @@
+module Spree
+ class ApiKeyTouchJob < Spree::BaseJob
+ queue_as Spree.queues.api_keys
+
+ def perform(api_key_id)
+ Spree::ApiKey.find_by(id: api_key_id)&.touch(:last_used_at)
+ end
+ end
+end
diff --git a/core/app/models/spree/legacy_user.rb b/core/app/models/spree/legacy_user.rb
index e31108ec9d..78449fafd0 100644
--- a/core/app/models/spree/legacy_user.rb
+++ b/core/app/models/spree/legacy_user.rb
@@ -10,5 +10,21 @@ module Spree
attr_accessor :password, :password_confirmation
validates :email, presence: true, uniqueness: { case_sensitive: false }
+
+ before_save :encrypt_password, if: :password
+
+ # Simple password validation for testing purposes
+ # In production, Spree.user_class should be overridden with a proper auth solution (e.g., Devise)
+ def valid_password?(check_password)
+ return false if encrypted_password.blank?
+
+ BCrypt::Password.new(encrypted_password) == check_password
+ end
+
+ private
+
+ def encrypt_password
+ self.encrypted_password = BCrypt::Password.create(password)
+ end
end
end
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index ede067e8ca..40d03bda6d 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -118,6 +118,8 @@ module Spree
has_many :customer_groups, class_name: 'Spree::CustomerGroup', dependent: :destroy, inverse_of: :store
+ has_many :api_keys, class_name: 'Spree::ApiKey', dependent: :destroy
+
#
# ActionText
#
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index 9455ee2b94..348e1a64ef 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -110,7 +110,8 @@ module Spree
themes: :default,
addresses: :default,
gift_cards: :default,
- webhooks: :default
+ webhooks: :default,
+ api_keys: :default
)
end
diff --git a/core/spec/jobs/spree/api_key_touch_job_spec.rb b/core/spec/jobs/spree/api_key_touch_job_spec.rb
new file mode 100644
index 0000000000..4eb267988b
--- /dev/null
+++ b/core/spec/jobs/spree/api_key_touch_job_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+RSpec.describe Spree::ApiKeyTouchJob, type: :job do
+ let(:store) { create(:store) }
+ let(:api_key) { create(:api_key, store: store) }
+
+ describe '#perform' do
+ it 'updates last_used_at timestamp' do
+ expect {
+ described_class.new.perform(api_key.id)
+ }.to change { api_key.reload.last_used_at }
+ end
+
+ it 'does nothing for non-existent api key' do
+ expect {
+ described_class.new.perform(999999)
+ }.not_to raise_error
+ end
+
+ it 'uses the api_keys queue' do
+ expect(described_class.new.queue_name).to eq(Spree.queues.api_keys.to_s)
+ end
+ end
+
+ describe 'enqueueing' do
+ it 'enqueues the job' do
+ expect {
+ described_class.perform_later(api_key.id)
+ }.to have_enqueued_job(described_class).with(api_key.id)
+ end
+ end
+end