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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreemodule Storefrontclass WishlistsController < ::Spree::Api::V2::ResourceControllerbefore_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 Spreedef add_itemspree_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 Spreedef set_item_quantityspree_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 Spreedef render_error_item_quantityrender json: { error: I18n.t('spree.api.v2.wishlist.wrong_quantity') }, status: 422end++ 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+ endendendenddiff --git a/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rb b/api/spec/requests/spree/api/v2/storefront/wishlists_spec.rbindex 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 ddescribe '#default' docontext 'no wishlist' do- it 'must create default wishlist' do+ it 'must create a new default wishlist' doexpect { get '/api/v2/storefront/wishlists/default', headers: headers_bearer }.to change { Spree::Wishlist.count }.from(0).to(2)endend@@ -35,7 +35,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request dendend- context 'has default wishlist in other store' do+ context 'has default wishlist in another store' dobefore dowishlist.update(is_default: true, store: other_store)wishlist.save@@ -101,12 +101,12 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request dendcontext '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' doget "/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' doget "/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 denddescribe '#update' do- it 'must permit update wishlist name' do+ it 'must permit updating of the wishlist name' dopatch "/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 denddescribe '#destroy' do- it 'must permite remove a wishlist' do+ it 'must permite destroying a wishlist' dodelete "/api/v2/storefront/wishlists/#{user.wishlists.first.token}", headers: headers_bearerexpect(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 ddescribe '#add_item' dolet!(:variant) { create(:variant) }- it 'must permit create wished_item' do+ it 'must allow creation of a wished_item' dopost "/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 dexpect(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' dolet!(: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' dopost "/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 dexpect(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' dopost "/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 dendend- 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' dopost "/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 dexpect(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' dopost "/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 dexpect(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' dopost "/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 dwishlist.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' dopatch "/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 dexpect(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)+ endenddescribe '#remove_item' do@@ -268,7 +309,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request dwishlist.wished_items.create({ variant: create(:variant) })end- it 'must permit update wishlist name' do+ it 'must permit deletion of a wishlist' dodelete "/api/v2/storefront/wishlists/#{user.wishlists.first.token}/remove_item/#{wishlist.wished_items.first.id}", headers: headers_bearerexpect(response.status).to eq(200)@@ -276,7 +317,7 @@ RSpec.describe Spree::Api::V2::Storefront::WishlistsController, type: :request dendcontext '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' dodelete "/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.rbindex 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 doexpect(described_class.new(quantity: 3, wishlist: wishlist, variant: nil)).not_to be_validend+ 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' dolet!(:existing_wished_item) { create(:wished_item, quantity: 3, wishlist: wishlist, variant: variant) }