diff --git a/spec/controllers/api/product_images_controller_spec.rb b/spec/controllers/api/product_images_controller_spec.rb new file mode 100644 index 0000000000..e0d70c2fac --- /dev/null +++ b/spec/controllers/api/product_images_controller_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' +require 'spree/api/testing_support/helpers' + +module Api + describe ProductImagesController, type: :controller do + include AuthenticationWorkflow + render_views + + describe "uploading an image" do + before do + allow(controller).to receive(:spree_current_user) { current_api_user } + end + + image_path = File.open(Rails.root.join('app', 'assets', 'images', 'logo-black.png')) + let(:image) { Rack::Test::UploadedFile.new(image_path, 'image/png') } + let!(:product_without_image) { create(:product) } + let!(:product_with_image) { create(:product_with_image) } + + sign_in_as_admin! + + it "saves a new image when none is present" do + xhr :post, :update_product_image, product_id: product_without_image.id, file: image, use_route: :product_images + + expect(response.status).to eq 201 + expect(product_without_image.images.first.id).to eq json_response['id'] + end + + it "updates an existing product image" do + xhr :post, :update_product_image, product_id: product_with_image.id, file: image, use_route: :product_images + + expect(response.status).to eq 200 + expect(product_with_image.images.first.id).to eq json_response['id'] + end + end + end +end diff --git a/spec/factories.rb b/spec/factories.rb index 5e2a2da27c..e662e80509 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -425,6 +425,13 @@ FactoryGirl.define do stripe_user_id "abc123" stripe_publishable_key "xyz456" end + + factory :product_with_image, parent: :product do + after(:create) do |product| + image = File.open(Rails.root.join('app', 'assets', 'images', 'logo-white.png')) + Spree::Image.create(attachment: image, viewable_id: product.master.id, viewable_type: 'Spree::Variant') + end + end end