diff --git a/app/controllers/spree/api/variants_controller.rb b/app/controllers/spree/api/variants_controller.rb index e634a3900f..2f1e6ab342 100644 --- a/app/controllers/spree/api/variants_controller.rb +++ b/app/controllers/spree/api/variants_controller.rb @@ -3,12 +3,11 @@ module Spree class VariantsController < ::Api::BaseController respond_to :json - skip_authorization_check only: :index + skip_authorization_check only: [:index, :show] before_filter :product def index - @variants = scope.includes(:option_values).ransack(params[:q]).result. - page(params[:page]).per(params[:per_page]) + @variants = scope.includes(:option_values).ransack(params[:q]).result render json: @variants, each_serializer: ::Api::VariantSerializer end @@ -17,8 +16,6 @@ module Spree render json: @variant, serializer: ::Api::VariantSerializer end - def new; end - def create authorize! :create, Variant @variant = scope.new(params[:variant]) diff --git a/app/serializers/api/variant_serializer.rb b/app/serializers/api/variant_serializer.rb index 3972c6bf4c..518dd60d7c 100644 --- a/app/serializers/api/variant_serializer.rb +++ b/app/serializers/api/variant_serializer.rb @@ -1,5 +1,5 @@ class Api::VariantSerializer < ActiveModel::Serializer - attributes :id, :is_master, :product_name + attributes :id, :is_master, :product_name, :sku attributes :options_text, :unit_value, :unit_description, :unit_to_display attributes :display_as, :display_name, :name_to_display attributes :price, :on_demand, :on_hand, :fees, :price_with_fees diff --git a/app/views/spree/api/products/bulk_show.v1.rabl b/app/views/spree/api/products/bulk_show.v1.rabl index 0786c5d844..5f6b600287 100644 --- a/app/views/spree/api/products/bulk_show.v1.rabl +++ b/app/views/spree/api/products/bulk_show.v1.rabl @@ -18,4 +18,4 @@ node( :producer_id, &:supplier_id ) node( :category_id, &:primary_taxon_id ) node( :supplier ) do |p| partial 'api/enterprises/bulk_show', object: p.supplier -end \ No newline at end of file +end diff --git a/spec/controllers/spree/api/variants_controller_spec.rb b/spec/controllers/spree/api/variants_controller_spec.rb index 5ffede16d0..2e5fef6cd2 100644 --- a/spec/controllers/spree/api/variants_controller_spec.rb +++ b/spec/controllers/spree/api/variants_controller_spec.rb @@ -9,10 +9,6 @@ module Spree let!(:variant2) { FactoryBot.create(:variant) } let!(:variant3) { FactoryBot.create(:variant) } let(:attributes) { [:id, :options_text, :price, :on_hand, :unit_value, :unit_description, :on_demand, :display_as, :display_name] } - let!(:standard_attributes) { - [:id, :name, :sku, :price, :weight, :height, - :width, :depth, :is_master, :cost_price, :permalink] - } before do allow(controller).to receive(:spree_current_user) { current_api_user } @@ -45,50 +41,12 @@ module Spree expect(variant.deleted_at).to be_nil end - it "can see a paginated list of variants" do - api_get :index - - keys = json_response["variants"].first.keys.map(&:to_sym) - expect(standard_attributes.all?{ |attr| keys.include? attr }).to eq(true) - expect(json_response["count"]).to eq(11) - expect(json_response["current_page"]).to eq(1) - expect(json_response["pages"]).to eq(1) - end - - it 'can control the page size through a parameter' do - create(:variant) - api_get :index, per_page: 1 - - expect(json_response['count']).to eq(1) - expect(json_response['current_page']).to eq(1) - expect(json_response['pages']).to eq(14) - end - - it 'can query the results through a paramter' do + it 'can query the results through a parameter' do expected_result = create(:variant, sku: 'FOOBAR') api_get :index, q: { sku_cont: 'FOO' } - expect(json_response['count']).to eq(1) - expect(json_response['variants'].first['sku']).to eq expected_result.sku - end - - it "variants returned contain option values data" do - api_get :index - - option_values = json_response["variants"].last["option_values"] - expect(option_values.first).to have_attributes(keys: ["id", - "name", - "presentation", - "option_type_name", - "option_type_id"]) - end - - it "variants returned contain images data" do - variant.images.create!(attachment: image("thinking-cat.jpg")) - - api_get :index - - expect(json_response["variants"].last["images"]).not_to be_nil + expect(json_response.size).to eq(1) + expect(json_response.first['sku']).to eq expected_result.sku end # Regression test for spree#2141 @@ -99,12 +57,12 @@ module Spree it "is not returned in the results" do api_get :index - expect(json_response["variants"].count).to eq(10) # there are 11 variants + expect(json_response.count).to eq(10) # there are 11 variants end it "is not returned even when show_deleted is passed" do api_get :index, show_deleted: true - expect(json_response["variants"].count).to eq(10) # there are 11 variants + expect(json_response.count).to eq(10) # there are 11 variants end end @@ -112,26 +70,7 @@ module Spree api_get :show, id: variant.to_param keys = json_response.keys.map(&:to_sym) - expect((standard_attributes + [:options_text, :option_values, :images]).all?{ |attr| keys.include? attr }).to eq(true) - option_values = json_response["option_values"] - expect(option_values.first).to have_attributes(keys: ["id", "name", "presentation", "option_type_name", "option_type_id"]) - end - - it "can see a single variant with images" do - variant.images.create!(attachment: image("thinking-cat.jpg")) - api_get :show, id: variant.to_param - - keys = json_response.keys.map(&:to_sym) - expect((standard_attributes + [:images]).all?{ |attr| keys.include? attr }).to eq(true) - option_values_keys = json_response["option_values"].first.keys.map(&:to_sym) - expect([:name, :presentation, :option_type_id].all?{ |attr| option_values_keys.include? attr }).to eq(true) - end - - it "can learn how to create a new variant" do - api_get :new - - expect(json_response["attributes"]).to eq(standard_attributes.map(&:to_s)) - expect(json_response["required_attributes"]).to be_empty + expect((attributes).all?{ |attr| keys.include? attr }).to eq(true) end it "cannot create a new variant if not an admin" do @@ -229,7 +168,7 @@ module Spree it "are visible by admin" do api_get :index, show_deleted: 1 - expect(json_response["variants"].count).to eq(2) + expect(json_response.count).to eq(2) end end @@ -237,7 +176,7 @@ module Spree original_number_of_variants = variant.product.variants.count api_post :create, variant: { sku: "12345", unit_value: "weight", unit_description: "L" } - expect(standard_attributes.all?{ |attr| json_response.include? attr.to_s }).to eq(true) + expect(attributes.all?{ |attr| json_response.include? attr.to_s }).to eq(true) expect(response.status).to eq(201) expect(json_response["sku"]).to eq("12345") expect(variant.product.variants.count).to eq(original_number_of_variants + 1)