mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-23 05:28:53 +00:00
Transpec and fix rubocop issues in spree/api/variants_controller_spec
This commit is contained in:
@@ -9,8 +9,10 @@ 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] }
|
||||
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 }
|
||||
@@ -48,121 +50,120 @@ module Spree
|
||||
|
||||
keys = json_response["variants"].first.keys.map(&:to_sym)
|
||||
expect(standard_attributes.all?{ |attr| keys.include? attr }).to eq(true)
|
||||
json_response["count"].should == 11
|
||||
json_response["current_page"].should == 1
|
||||
json_response["pages"].should == 1
|
||||
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
|
||||
api_get :index, per_page: 1
|
||||
|
||||
json_response['count'].should == 1
|
||||
json_response['current_page'].should == 1
|
||||
json_response['pages'].should == 14
|
||||
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
|
||||
expected_result = create(:variant, :sku => 'FOOBAR')
|
||||
api_get :index, :q => { :sku_cont => 'FOO' }
|
||||
expected_result = create(:variant, sku: 'FOOBAR')
|
||||
api_get :index, q: { sku_cont: 'FOO' }
|
||||
|
||||
json_response['count'].should == 1
|
||||
json_response['variants'].first['sku'].should eq expected_result.sku
|
||||
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"]
|
||||
option_values.first.should have_attributes(keys: ["id",
|
||||
"name",
|
||||
"presentation",
|
||||
"option_type_name",
|
||||
"option_type_id"])
|
||||
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"))
|
||||
variant.images.create!(attachment: image("thinking-cat.jpg"))
|
||||
|
||||
api_get :index
|
||||
|
||||
json_response["variants"].last["images"].should_not be_nil
|
||||
expect(json_response["variants"].last["images"]).not_to be_nil
|
||||
end
|
||||
|
||||
# Regression test for spree#2141
|
||||
context "a deleted variant" do
|
||||
before do
|
||||
variant.update_column(:deleted_at, Time.now)
|
||||
variant.update_column(:deleted_at, Time.zone.now)
|
||||
end
|
||||
|
||||
it "is not returned in the results" do
|
||||
api_get :index
|
||||
json_response["variants"].count.should == 10 # there are 11 variants
|
||||
expect(json_response["variants"].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
|
||||
json_response["variants"].count.should == 10 # there are 11 variants
|
||||
api_get :index, show_deleted: true
|
||||
expect(json_response["variants"].count).to eq(10) # there are 11 variants
|
||||
end
|
||||
end
|
||||
|
||||
context "pagination" do
|
||||
it "can select the next page of variants" do
|
||||
second_variant = create(:variant)
|
||||
api_get :index, :page => 2, :per_page => 1
|
||||
api_get :index, page: 2, per_page: 1
|
||||
|
||||
keys = json_response["variants"].first.keys.map(&:to_sym)
|
||||
expect(standard_attributes.all?{ |attr| keys.include? attr }).to eq(true)
|
||||
json_response["total_count"].should == 14
|
||||
json_response["current_page"].should == 2
|
||||
json_response["pages"].should == 14
|
||||
expect(json_response["total_count"]).to eq(14)
|
||||
expect(json_response["current_page"]).to eq(2)
|
||||
expect(json_response["pages"]).to eq(14)
|
||||
end
|
||||
end
|
||||
|
||||
it "can see a single variant" do
|
||||
api_get :show, :id => variant.to_param
|
||||
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"]
|
||||
option_values.first.should have_attributes(keys: ["id", "name", "presentation",
|
||||
"option_type_name", "option_type_id"])
|
||||
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
|
||||
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_name,:option_type_id].all?{ |attr| option_values_keys.include? attr }).to eq(true)
|
||||
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
|
||||
|
||||
json_response["attributes"].should == standard_attributes.map(&:to_s)
|
||||
json_response["required_attributes"].should be_empty
|
||||
expect(json_response["attributes"]).to eq(standard_attributes.map(&:to_s))
|
||||
expect(json_response["required_attributes"]).to be_empty
|
||||
end
|
||||
|
||||
it "cannot create a new variant if not an admin" do
|
||||
api_post :create, :variant => { :sku => "12345" }
|
||||
api_post :create, variant: { sku: "12345" }
|
||||
|
||||
assert_unauthorized!
|
||||
end
|
||||
|
||||
it "cannot update a variant" do
|
||||
api_put :update, :id => variant.to_param, :variant => { :sku => "12345" }
|
||||
api_put :update, id: variant.to_param, variant: { sku: "12345" }
|
||||
|
||||
assert_unauthorized!
|
||||
end
|
||||
|
||||
it "cannot delete a variant" do
|
||||
api_delete :destroy, :id => variant.to_param
|
||||
api_delete :destroy, id: variant.to_param
|
||||
|
||||
assert_unauthorized!
|
||||
lambda { variant.reload }.should_not raise_error
|
||||
expect { variant.reload }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
@@ -205,7 +206,7 @@ module Spree
|
||||
|
||||
let(:product) { create(:product) }
|
||||
let(:variant) { product.master }
|
||||
let(:resource_scoping) { { :product_id => variant.product.to_param } }
|
||||
let(:resource_scoping) { { product_id: variant.product.to_param } }
|
||||
|
||||
it "soft deletes a variant" do
|
||||
spree_delete :soft_delete, variant_id: variant.to_param, product_id: product.to_param, format: :json
|
||||
@@ -235,38 +236,38 @@ module Spree
|
||||
|
||||
context "deleted variants" do
|
||||
before do
|
||||
variant.update_column(:deleted_at, Time.now)
|
||||
variant.update_column(:deleted_at, Time.zone.now)
|
||||
end
|
||||
|
||||
it "are visible by admin" do
|
||||
api_get :index, :show_deleted => 1
|
||||
api_get :index, show_deleted: 1
|
||||
|
||||
json_response["variants"].count.should == 2
|
||||
expect(json_response["variants"].count).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
it "can create a new variant" do
|
||||
original_number_of_variants = variant.product.variants.count
|
||||
api_post :create, :variant => { :sku => "12345", :unit_value => "weight", :unit_description => "L" }
|
||||
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)
|
||||
response.status.should == 201
|
||||
json_response["sku"].should == "12345"
|
||||
variant.product.variants.count.should == original_number_of_variants + 1
|
||||
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)
|
||||
end
|
||||
|
||||
it "can update a variant" do
|
||||
api_put :update, :id => variant.to_param, :variant => { :sku => "12345" }
|
||||
api_put :update, id: variant.to_param, variant: { sku: "12345" }
|
||||
|
||||
response.status.should == 200
|
||||
expect(response.status).to eq(200)
|
||||
end
|
||||
|
||||
it "can delete a variant" do
|
||||
api_delete :destroy, :id => variant.to_param
|
||||
api_delete :destroy, id: variant.to_param
|
||||
|
||||
response.status.should == 204
|
||||
lambda { Spree::Variant.find(variant.id) }.should raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
expect(response.status).to eq(204)
|
||||
expect { Spree::Variant.find(variant.id) }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user