mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-18 04:39:14 +00:00
Add controller specs
This commit is contained in:
36
spec/controllers/api/product_images_controller_spec.rb
Normal file
36
spec/controllers/api/product_images_controller_spec.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user