mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Fix specs
This commit is contained in:
@@ -72,8 +72,25 @@ module Spree
|
||||
render json: @variants, each_serializer: ::Api::Admin::VariantSerializer
|
||||
end
|
||||
|
||||
def destroy
|
||||
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
|
||||
|
||||
@variant = Spree::Variant.find(params[:id])
|
||||
flash[:success] = delete_variant
|
||||
|
||||
redirect_to spree.admin_product_variants_url(params[:product_id], @url_filters)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def delete_variant
|
||||
if VariantDeleter.new.delete(@variant)
|
||||
Spree.t('notice_messages.variant_deleted')
|
||||
else
|
||||
Spree.t('notice_messages.variant_not_deleted')
|
||||
end
|
||||
end
|
||||
|
||||
def create_before
|
||||
@object.save
|
||||
end
|
||||
|
||||
@@ -68,7 +68,7 @@ Spree::Core::Engine.routes.draw do
|
||||
end
|
||||
end
|
||||
|
||||
resources :variants, except: :destroy do
|
||||
resources :variants do
|
||||
collection do
|
||||
post :update_positions
|
||||
end
|
||||
|
||||
@@ -62,33 +62,6 @@ RSpec.describe Api::V0::ProductsController do
|
||||
api_put :update, id: product.to_param, product: { name: "I hacked your store!" }
|
||||
assert_unauthorized!
|
||||
end
|
||||
|
||||
it "cannot delete a product" do
|
||||
api_delete :destroy, id: product.to_param
|
||||
assert_unauthorized!
|
||||
end
|
||||
end
|
||||
|
||||
context "as an enterprise user" do
|
||||
let(:current_api_user) { supplier_enterprise_user(supplier) }
|
||||
|
||||
it "can delete my product" do
|
||||
expect(product.deleted_at).to be_nil
|
||||
api_delete :destroy, id: product.to_param
|
||||
|
||||
expect(response).to have_http_status(:no_content)
|
||||
expect { product.reload }.not_to raise_error
|
||||
expect(product.deleted_at).not_to be_nil
|
||||
end
|
||||
|
||||
it "is denied access to deleting another enterprises' product" do
|
||||
expect(product_other_supplier.deleted_at).to be_nil
|
||||
api_delete :destroy, id: product_other_supplier.to_param
|
||||
|
||||
assert_unauthorized!
|
||||
expect { product_other_supplier.reload }.not_to raise_error
|
||||
expect(product_other_supplier.deleted_at).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "as an administrator" do
|
||||
@@ -137,155 +110,6 @@ RSpec.describe Api::V0::ProductsController do
|
||||
expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
|
||||
expect(json_response["errors"]["name"]).to eq(["can't be blank"])
|
||||
end
|
||||
|
||||
it "can delete a product" do
|
||||
expect(product.deleted_at).to be_nil
|
||||
api_delete :destroy, id: product.to_param
|
||||
|
||||
expect(response).to have_http_status(:no_content)
|
||||
expect(product.reload.deleted_at).not_to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe '#clone' do
|
||||
context 'as a normal user' do
|
||||
before do
|
||||
allow(current_api_user)
|
||||
.to receive(:admin?).and_return(false)
|
||||
end
|
||||
|
||||
it 'denies access' do
|
||||
spree_post :clone, product_id: product.id, format: :json
|
||||
|
||||
assert_unauthorized!
|
||||
end
|
||||
end
|
||||
|
||||
context 'as an enterprise user' do
|
||||
let(:current_api_user) { supplier_enterprise_user(supplier) }
|
||||
let!(:variant) { create(:variant, product_id: product.id) }
|
||||
|
||||
it 'responds with a successful response' do
|
||||
spree_post :clone, product_id: product.id, format: :json
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
end
|
||||
|
||||
it 'clones the product' do
|
||||
spree_post :clone, product_id: product.id, format: :json
|
||||
|
||||
expect(json_response['name']).to eq("COPY OF #{product.name}")
|
||||
end
|
||||
|
||||
it 'clones a product with image' do
|
||||
spree_post :clone, product_id: product_with_image.id, format: :json
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
expect(json_response['name']).to eq("COPY OF #{product_with_image.name}")
|
||||
end
|
||||
|
||||
# test cases related to bug #660: product duplication clones master variant
|
||||
|
||||
# stock info - clone is set to zero
|
||||
it '(does not) clone the stock info of the product' do
|
||||
spree_post :clone, product_id: product.id, format: :json
|
||||
expect(json_response.dig("variants", 0, "on_hand")).to eq(0)
|
||||
end
|
||||
|
||||
# variants: only the master variant of the product is cloned
|
||||
it '(does not) clone variants from a product with several variants' do
|
||||
spree_post :clone, product_id: product.id, format: :json
|
||||
expect(Spree::Product.second.variants.count).not_to eq Spree::Product.first.variants.count
|
||||
end
|
||||
end
|
||||
|
||||
context 'as an administrator' do
|
||||
before do
|
||||
allow(current_api_user)
|
||||
.to receive(:admin?).and_return(true)
|
||||
end
|
||||
|
||||
it 'responds with a successful response' do
|
||||
spree_post :clone, product_id: product.id, format: :json
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
end
|
||||
|
||||
it 'clones the product' do
|
||||
spree_post :clone, product_id: product.id, format: :json
|
||||
|
||||
expect(json_response['name']).to eq("COPY OF #{product.name}")
|
||||
end
|
||||
|
||||
it 'clones a product with image' do
|
||||
spree_post :clone, product_id: product_with_image.id, format: :json
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
expect(json_response['name']).to eq("COPY OF #{product_with_image.name}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#bulk_products' do
|
||||
context "as an enterprise user" do
|
||||
let!(:taxon) { create(:taxon) }
|
||||
let!(:product2) { create(:product, supplier_id: supplier.id, primary_taxon: taxon) }
|
||||
let!(:product3) { create(:product, supplier_id: supplier2.id, primary_taxon: taxon) }
|
||||
let!(:product4) { create(:product, supplier_id: supplier2.id) }
|
||||
let(:current_api_user) { supplier_enterprise_user(supplier) }
|
||||
|
||||
before { current_api_user.enterprise_roles.create(enterprise: supplier2) }
|
||||
|
||||
it "returns a list of products" do
|
||||
api_get :bulk_products, { page: 1, per_page: 15 }, format: :json
|
||||
expect(returned_product_ids).to eq [product4.id, product3.id, product2.id,
|
||||
other_product.id, product.id]
|
||||
end
|
||||
|
||||
it "returns pagination data" do
|
||||
api_get :bulk_products, { page: 1, per_page: 15 }, format: :json
|
||||
expect(json_response['pagination']).to eq "results" => 5, "pages" => 1, "page" => 1,
|
||||
"per_page" => 15
|
||||
end
|
||||
|
||||
it "uses defaults when page and per_page are not supplied" do
|
||||
api_get :bulk_products, format: :json
|
||||
expect(json_response['pagination']).to eq "results" => 5, "pages" => 1, "page" => 1,
|
||||
"per_page" => 15
|
||||
end
|
||||
|
||||
it "returns paginated products by page" do
|
||||
api_get :bulk_products, { page: 1, per_page: 2 }, format: :json
|
||||
expect(returned_product_ids).to eq [product4.id, product3.id]
|
||||
|
||||
api_get :bulk_products, { page: 2, per_page: 2 }, format: :json
|
||||
expect(returned_product_ids).to eq [product2.id, other_product.id]
|
||||
end
|
||||
|
||||
it "filters results by supplier" do
|
||||
api_get :bulk_products,
|
||||
{ page: 1, per_page: 15, q: { variants_supplier_id_eq: supplier.id } },
|
||||
format: :json
|
||||
expect(returned_product_ids).to eq [product2.id, other_product.id, product.id]
|
||||
end
|
||||
|
||||
it "filters results by product category" do
|
||||
api_get :bulk_products,
|
||||
{ page: 1, per_page: 15, q: { variants_primary_taxon_id_eq: taxon.id } },
|
||||
format: :json
|
||||
expect(returned_product_ids).to eq [product3.id, product2.id]
|
||||
end
|
||||
|
||||
it "filters results by import_date" do
|
||||
product.variants.first.update_attribute :import_date, 1.day.ago
|
||||
product2.variants.first.update_attribute :import_date, 2.days.ago
|
||||
product3.variants.first.update_attribute :import_date, 1.day.ago
|
||||
|
||||
api_get :bulk_products, { page: 1, per_page: 15, import_date: 1.day.ago.to_date.to_s },
|
||||
format: :json
|
||||
expect(returned_product_ids).to eq [product3.id, product.id]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -3,130 +3,6 @@
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe Spree::Admin::ProductsController do
|
||||
describe 'bulk_update' do
|
||||
context "updating a product we do not have access to" do
|
||||
let(:s_managed) { create(:enterprise) }
|
||||
let(:s_unmanaged) { create(:enterprise) }
|
||||
let(:product) do
|
||||
create(:simple_product, supplier_id: s_unmanaged.id, name: 'Peas')
|
||||
end
|
||||
|
||||
before do
|
||||
controller_login_as_enterprise_user [s_managed]
|
||||
spree_post :bulk_update,
|
||||
"products" => [{ "id" => product.id, "name" => "Pine nuts" }]
|
||||
end
|
||||
|
||||
it "denies access" do
|
||||
expect(response).to redirect_to unauthorized_path
|
||||
end
|
||||
|
||||
it "does not update any product" do
|
||||
expect(product.reload.name).not_to eq("Pine nuts")
|
||||
end
|
||||
end
|
||||
|
||||
context "when changing a product's variant_unit" do
|
||||
let(:producer) { create(:enterprise) }
|
||||
let!(:product) do
|
||||
create(
|
||||
:simple_product,
|
||||
supplier_id: producer.id,
|
||||
variant_unit: 'items',
|
||||
variant_unit_scale: nil,
|
||||
variant_unit_name: 'bunches',
|
||||
unit_value: nil,
|
||||
unit_description: 'some description'
|
||||
)
|
||||
end
|
||||
|
||||
before { controller_login_as_enterprise_user([producer]) }
|
||||
|
||||
it 'succeeds' do
|
||||
spree_post :bulk_update,
|
||||
"products" => [
|
||||
{
|
||||
"id" => product.id,
|
||||
"variant_unit" => "weight",
|
||||
"variant_unit_scale" => 1
|
||||
}
|
||||
]
|
||||
|
||||
expect(response).to have_http_status(:found)
|
||||
end
|
||||
|
||||
it 'does not redirect to bulk_products' do
|
||||
spree_post :bulk_update,
|
||||
"products" => [
|
||||
{
|
||||
"id" => product.id,
|
||||
"variant_unit" => "weight",
|
||||
"variant_unit_scale" => 1
|
||||
}
|
||||
]
|
||||
|
||||
expect(response).to redirect_to(
|
||||
'/api/v0/products/bulk_products'
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when passing empty variants_attributes' do
|
||||
let(:producer) { create(:enterprise) }
|
||||
let!(:product) do
|
||||
create(
|
||||
:simple_product,
|
||||
supplier_id: producer.id,
|
||||
variant_unit: 'items',
|
||||
variant_unit_scale: nil,
|
||||
variant_unit_name: 'bunches',
|
||||
unit_value: nil,
|
||||
unit_description: 'bunches'
|
||||
)
|
||||
end
|
||||
let!(:another_product) do
|
||||
create(
|
||||
:simple_product,
|
||||
supplier_id: producer.id,
|
||||
variant_unit: 'weight',
|
||||
variant_unit_scale: 1000,
|
||||
variant_unit_name: nil
|
||||
)
|
||||
end
|
||||
let!(:taxon) { create(:taxon) }
|
||||
|
||||
before { controller_login_as_enterprise_user([producer]) }
|
||||
|
||||
it 'does not fail' do
|
||||
spree_post :bulk_update,
|
||||
"products" => [
|
||||
{
|
||||
"id" => another_product.id,
|
||||
"variants_attributes" => [{}]
|
||||
},
|
||||
{
|
||||
"id" => product.id,
|
||||
"variants_attributes" => [
|
||||
{
|
||||
"on_hand" => 2,
|
||||
"price" => "5.0",
|
||||
"unit_value" => 4,
|
||||
"variant_unit" => "weight",
|
||||
"variant_unit_scale" => "1",
|
||||
"unit_description" => "",
|
||||
"display_name" => "name",
|
||||
"primary_taxon_id" => taxon.id,
|
||||
"supplier_id" => producer.id
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
expect(response).to have_http_status(:found)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "creating a new product" do
|
||||
let(:supplier) { create(:supplier_enterprise) }
|
||||
let(:taxon) { create(:taxon) }
|
||||
|
||||
Reference in New Issue
Block a user