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.

Improve code in controller and tests

Matthew Kennedy1d8ff35efc00
diff --git a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
index 0507271c3d..58cd2b84d2 100644
--- a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
@@ -4,6 +4,7 @@ module Spree
module Storefront
class WishlistsController < ::Spree::Api::V2::ResourceController
before_action :require_spree_current_user, except: [:show]
+ before_action :ensure_valid_quantity, only: [:add_item, :set_item_quantity]
def show
@resource ||= current_store.wishlists.find_by!(token: params[:id])
@@ -62,12 +63,6 @@ module Spree
def add_item
spree_authorize! :create, Spree::WishedItem
- if params[:quantity].present?
- params[:quantity] = params[:quantity].to_i
- else
- params[:quantity] = 1
- end
-
if resource.wished_items.present? && resource.wished_items.detect { |wv| wv.variant_id.to_s == params[:variant_id].to_s }.present?
@wished_item = resource.wished_items.detect { |wi| wi.variant_id.to_s == params[:variant_id].to_s }
@wished_item.quantity = params[:quantity]
@@ -89,8 +84,6 @@ module Spree
def set_item_quantity
spree_authorize! :update, wished_item
- return render_error_item_quantity unless params[:quantity].to_i > 0
-
wished_item.update(params.permit(:quantity))
if wished_item.errors.empty?
@@ -156,6 +149,16 @@ module Spree
def render_error_item_quantity
render json: { error: I18n.t('spree.api.v2.wishlist.wrong_quantity') }, status: 422
end
+
+ def ensure_valid_quantity
+ return render_error_item_quantity if params[:quantity].present? && params[:quantity].to_i <= 0
+
+ params[:quantity] = if params[:quantity].present?
+ params[:quantity].to_i
+ else
+ 1
+ end
+ end
end
end
end
diff --git a/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb b/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb
index c5e5cb4670..4055a23a0f 100644
--- a/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb
@@ -12,7 +12,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
describe '#default' do
context 'no wishlist' do
- it 'must create default wishlist' do
+ it 'must create a new default wishlist' do
expect { get '/api/v2/storefront/wishlists/default', headers: headers_bearer }.to change { Spree::Wishlist.count }.from(0).to(2)
end
end
@@ -35,7 +35,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
end
end
- context 'has default wishlist in other store' do
+ context 'has default wishlist in another store' do
before do
wishlist.update(is_default: true, store: other_store)
wishlist.save
@@ -101,12 +101,12 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
end
context 'when a request is sent by random user with no auth' do
- it 'returns 404 when wishlist is set to private' do
+ it 'returns 404 when wishlist is set to is_private: true' do
get "/api/v2/storefront/wishlists/#{wishlist_private.token}"
expect(response).to have_http_status(:not_found)
end
- it 'returns wishlist when requested by public but is_private: false' do
+ it 'returns 200 when wishlist is set to is_private: false' do
get "/api/v2/storefront/wishlists/#{wishlist_public.token}"
expect(response).to have_http_status(:ok)
@@ -142,7 +142,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
end
describe '#update' do
- it 'must permit update wishlist name' do
+ it 'must permit updating of the wishlist name' do
patch "/api/v2/storefront/wishlists/#{user.wishlists.first.token}", headers: headers_bearer, params: {
wishlist: {
name: 'books'
@@ -155,7 +155,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
end
describe '#destroy' do
- it 'must permite remove a wishlist' do
+ it 'must permite destroying a wishlist' do
delete "/api/v2/storefront/wishlists/#{user.wishlists.first.token}", headers: headers_bearer
expect(response.status).to eq (200)
expect(user.wishlists.count).to eq (0)
@@ -165,7 +165,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
describe '#add_item' do
let!(:variant) { create(:variant) }
- it 'must permit create wished_item' do
+ it 'must allow creation of a wished_item' do
post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
params: {
variant_id: variant.id.to_s,
@@ -186,11 +186,11 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
expect(json_response['data']['relationships']['wishlist']['data']['type']).to eql ('wishlist')
end
- context 'when variant is already in the wishlist' do
+ context 'when a variant is already in the wishlist' do
let!(:set_variant) { create(:variant)}
let!(:wi) { create(:wished_item, wishlist: user.wishlists.first, variant: set_variant, quantity: 1) }
- it 'quantity set must return the existing wished item' do
+ it 'return the existing wished_item - quantity atribute passed in' do
post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
params: {
variant_id: set_variant.id.to_s,
@@ -204,7 +204,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
expect(json_response['data']['attributes']['quantity']).to eql (1)
end
- it 'quantity omitted must return existing wished item with quantity 1' do
+ it 'return the existing wished_item - quantity atribute ommited' do
post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
params: {
variant_id: set_variant.id.to_s
@@ -218,7 +218,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
end
end
- it 'must permit create wished_item when omitting the quantity attribute' do
+ it 'must permit creation of a wished_item when omitting the quantity attribute' do
post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
params: {
variant_id: variant.id.to_s
@@ -228,7 +228,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
expect(json_response['data']['attributes']['quantity']).to eql (1)
end
- it 'must not permit create new wished_item omitting the variant_id attribute' do
+ it 'must not permit the creation of a new wished_item witout the variant_id attribute' do
post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
params: {
bad_variant_id: variant.id.to_s
@@ -236,7 +236,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
expect(response.status).to eq(422)
end
- it 'must not permit create new wished_item with a quantity of 0' do
+ it 'must not permit the creattion of a new wished_item with a quantity of 0' do
post "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/add_item", headers: headers_bearer,
params: {
variant_id: variant.id.to_s,
@@ -251,7 +251,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
wishlist.wished_items.create({ variant: create(:variant) })
end
- it 'must permit update wishlist name' do
+ it 'must allow setting the quantity when an integer value is passed in' do
patch "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/set_item_quantity/#{wishlist.wished_items.first.id}",
headers: headers_bearer,
params: {
@@ -261,6 +261,47 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
expect(response.status).to eq(200)
expect(json_response['data']['attributes']['quantity']).to eql (17)
end
+
+ it 'must allow setting the quantity when an integer value is passed in' do
+ patch "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/set_item_quantity/#{wishlist.wished_items.first.id}",
+ headers: headers_bearer,
+ params: {
+ quantity: '18'
+ }
+
+ expect(response.status).to eq(200)
+ expect(json_response['data']['attributes']['quantity']).to eql (18)
+ end
+
+ it 'must return error 422 if a random string is passed in as the quantity value' do
+ patch "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/set_item_quantity/#{wishlist.wished_items.first.id}",
+ headers: headers_bearer,
+ params: {
+ quantity: 'some string'
+ }
+
+ expect(response.status).to eq(422)
+ end
+
+ it 'must return error 422 if 0 is passed in as the quantity value' do
+ patch "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/set_item_quantity/#{wishlist.wished_items.first.id}",
+ headers: headers_bearer,
+ params: {
+ quantity: 0
+ }
+
+ expect(response.status).to eq(422)
+ end
+
+ it 'must return error 422 if "0" is passed in as the quantity value' do
+ patch "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/set_item_quantity/#{wishlist.wished_items.first.id}",
+ headers: headers_bearer,
+ params: {
+ quantity: '0'
+ }
+
+ expect(response.status).to eq(422)
+ end
end
describe '#remove_item' do
@@ -268,7 +309,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
wishlist.wished_items.create({ variant: create(:variant) })
end
- it 'must permit update wishlist name' do
+ it 'must permit deletion of a wishlist' do
delete "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/remove_item/#{wishlist.wished_items.first.id}", headers: headers_bearer
expect(response.status).to eq(200)
@@ -276,7 +317,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request d
end
context 'user not authorised to access this action' do
- it 'can not delete item from other users wishlist' do
+ it 'can not delete an item from another users wishlist' do
delete "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/remove_item/#{wishlist.wished_items.first.id}"
expect(response.status).to eq(403)
diff --git a/core/spec/models/spree/wished_item_spec.rb b/core/spec/models/spree/wished_item_spec.rb
index 4e181c4adb..b7b812424b 100644
--- a/core/spec/models/spree/wished_item_spec.rb
+++ b/core/spec/models/spree/wished_item_spec.rb
@@ -19,6 +19,17 @@ RSpec.describe Spree::WishedItem, type: :model do
expect(described_class.new(quantity: 3, wishlist: wishlist, variant: nil)).not_to be_valid
end
+ it 'validates numericality of quantity' do
+ expect(described_class.new(quantity: nil, wishlist: wishlist, variant: variant)).not_to be_valid
+ expect(described_class.new(quantity: 'string', wishlist: wishlist, variant: variant)).not_to be_valid
+ expect(described_class.new(quantity: 0.5, wishlist: wishlist, variant: variant)).not_to be_valid
+ end
+
+ it 'validates numericality must be greater than 0' do
+ expect(described_class.new(quantity: 0, wishlist: wishlist, variant: variant)).not_to be_valid
+ expect(described_class.new(quantity: -1, wishlist: wishlist, variant: variant)).not_to be_valid
+ end
+
describe 'when wished_item is alread associated with the wishlist' do
let!(:existing_wished_item) { create(:wished_item, quantity: 3, wishlist: wishlist, variant: variant) }