diff --git a/spec/controllers/spree/api/products_controller_spec.rb b/spec/controllers/spree/api/products_controller_spec.rb index f372d91e75..70b8b805bd 100644 --- a/spec/controllers/spree/api/products_controller_spec.rb +++ b/spec/controllers/spree/api/products_controller_spec.rb @@ -11,7 +11,8 @@ module Spree let(:product_other_supplier) { create(:product, supplier: supplier2) } let(:product_with_image) { create(:product_with_image, supplier: supplier) } let(:attributes) { ["id", "name", "supplier", "price", "on_hand", "available_on", "permalink_live"] } - let(:all_attributes) { ["id", "name", "description", "price", "available_on", "permalink", "meta_description", "meta_keywords", "shipping_category_id", "taxon_ids", "variants", "option_types", "product_properties"] } + let(:all_attributes) { ["id", "name", "price", "available_on", "variants"] } + let(:variants_attributes) { ["id", "options_text", "unit_value", "unit_description", "unit_to_display", "on_demand", "display_as", "display_name", "name_to_display", "sku", "on_hand", "price"] } let(:current_api_user) { build(:user) } @@ -31,10 +32,9 @@ module Spree product.variants.first.images.create!(attachment: image("thinking-cat.jpg")) product.set_property("spree", "rocks") api_get :show, id: product.to_param - expect(json_response).to have_attributes(keys: all_attributes) - expect(json_response['variants'].first).to have_attributes(keys: ["id", "name", "sku", "price", "weight", "height", "width", "depth", "is_master", "cost_price", "permalink", "option_values", "images"]) - expect(json_response['variants'].first['images'].first).to have_attributes(keys: ["id", "position", "attachment_content_type", "attachment_file_name", "type", "attachment_updated_at", "attachment_width", "attachment_height", "alt", "viewable_type", "viewable_id", "attachment_url"]) - expect(json_response["product_properties"].first).to have_attributes(keys: ["id", "product_id", "property_id", "value", "property_name"]) + + expect(all_attributes.all?{ |attr| json_response.keys.include? attr }).to eq(true) + expect(variants_attributes.all?{ |attr| json_response['variants'].first.keys.include? attr }).to eq(true) end context "finds a product by permalink first then by id" do @@ -46,22 +46,25 @@ module Spree specify do api_get :show, id: product.to_param - expect(json_response["permalink"]).to match(/and-1-ways/) + + expect(json_response["permalink_live"]).to match(/and-1-ways/) product.destroy api_get :show, id: other_product.id - expect(json_response["permalink"]).to match(/droids/) + expect(json_response["permalink_live"]).to match(/droids/) end end it "cannot see inactive products" do api_get :show, id: inactive_product.to_param + expect(json_response["error"]).to eq("The resource you were looking for could not be found.") expect(response.status).to eq(404) end it "returns a 404 error when it cannot find a product" do api_get :show, id: "non-existant" + expect(json_response["error"]).to eq("The resource you were looking for could not be found.") expect(response.status).to eq(404) end @@ -78,6 +81,7 @@ module Spree it "soft deletes my products" do spree_delete :soft_delete, product_id: product.to_param, format: :json + expect(response.status).to eq(204) expect { product.reload }.not_to raise_error expect(product.deleted_at).not_to be_nil @@ -85,6 +89,7 @@ module Spree it "is denied access to soft deleting another enterprises' product" do spree_delete :soft_delete, product_id: product_other_supplier.to_param, format: :json + assert_unauthorized! expect { product_other_supplier.reload }.not_to raise_error expect(product_other_supplier.deleted_at).to be_nil @@ -99,6 +104,7 @@ module Spree it "soft deletes a product" do spree_delete :soft_delete, product_id: product.to_param, format: :json + expect(response.status).to eq(204) expect { product.reload }.not_to raise_error expect(product.deleted_at).not_to be_nil @@ -113,12 +119,14 @@ module Spree variant_unit: "items", variant_unit_name: "things", unit_description: "things" } - expect(json_response).to have_attributes(keys: all_attributes) + + expect(all_attributes.all?{ |attr| json_response.keys.include? attr }).to eq(true) expect(response.status).to eq(201) end it "cannot create a new product with invalid attributes" do api_post :create, product: {} + expect(response.status).to eq(422) expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.") errors = json_response["errors"] @@ -127,11 +135,13 @@ module Spree it "can update a product" do api_put :update, id: product.to_param, product: { name: "New and Improved Product!" } + expect(response.status).to eq(200) end it "cannot update a product with an invalid attribute" do api_put :update, id: product.to_param, product: { name: "" } + expect(response.status).to eq(422) 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"]) @@ -140,6 +150,7 @@ module Spree it "can delete a product" do expect(product.deleted_at).to be_nil api_delete :destroy, id: product.to_param + expect(response.status).to eq(204) expect(product.reload.deleted_at).not_to be_nil end @@ -154,6 +165,7 @@ module Spree it 'denies access' do spree_post :clone, product_id: product.id, format: :json + assert_unauthorized! end end @@ -167,16 +179,19 @@ module Spree it 'responds with a successful response' do spree_post :clone, product_id: product.id, format: :json + expect(response.status).to eq(201) 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.status).to eq(201) expect(json_response['name']).to eq("COPY OF #{product_with_image.name}") end @@ -190,16 +205,19 @@ module Spree it 'responds with a successful response' do spree_post :clone, product_id: product.id, format: :json + expect(response.status).to eq(201) 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.status).to eq(201) expect(json_response['name']).to eq("COPY OF #{product_with_image.name}") end