Merge pull request #12013 from mkllnk/spec-file-helper

DRY specs with fixture_file_upload helper
This commit is contained in:
David Cook
2024-01-10 10:28:23 +11:00
committed by GitHub
14 changed files with 32 additions and 47 deletions

View File

@@ -18,7 +18,7 @@ module Api
}
describe "removing logo" do
let(:image) { Rack::Test::UploadedFile.new(black_logo_file, "image/png") }
let(:image) { black_logo_file }
let(:enterprise) { create(:enterprise, owner: enterprise_owner, logo: image) }

View File

@@ -8,9 +8,8 @@ describe Api::V0::ProductImagesController, type: :controller do
render_views
describe "uploading an image" do
let(:image) { Rack::Test::UploadedFile.new(black_logo_file, 'image/png') }
let(:pdf) { Rack::Test::UploadedFile.new(pdf_path, 'application/pdf') }
let(:pdf_path) { Rails.public_path.join('Terms-of-service.pdf') }
let(:image) { black_logo_file }
let(:pdf) { terms_pdf_file }
let(:product_without_image) { create(:product) }
let(:product_with_image) { create(:product_with_image) }
let(:current_api_user) { create(:admin_user) }

View File

@@ -25,15 +25,17 @@ describe Api::V0::ProductsController, type: :controller do
end
context "as a normal user" do
let(:attachment) { fixture_file_upload("thinking-cat.jpg") }
before do
allow(current_api_user)
.to receive(:has_spree_role?).with("admin").and_return(false)
end
it "gets a single product" do
product.create_image!(attachment: image("thinking-cat.jpg"))
product.create_image!(attachment:)
product.variants.create!(unit_value: "1", unit_description: "thing", price: 1)
product.variants.first.images.create!(attachment: image("thinking-cat.jpg"))
product.variants.first.images.create!(attachment:)
product.set_property("spree", "rocks")
api_get :show, id: product.to_param

View File

@@ -18,7 +18,7 @@ module Api
}
describe "removing promo image" do
let(:image) { Rack::Test::UploadedFile.new(black_logo_file, "image/png") }
let(:image) { black_logo_file }
let(:enterprise) { create(:enterprise, owner: enterprise_owner, promo_image: image) }

View File

@@ -12,10 +12,7 @@ module Api
let(:enterprise_manager) { create(:user, enterprises: [enterprise]) }
describe "removing terms and conditions file" do
let(:terms_file_path) { Rails.public_path.join('Terms-of-service.pdf') }
let(:terms_and_conditions_file) {
Rack::Test::UploadedFile.new(terms_file_path, "application/pdf")
}
let(:terms_and_conditions_file) { terms_pdf_file }
let(:enterprise) { create(:enterprise, owner: enterprise_owner) }
before do

View File

@@ -35,7 +35,7 @@ FactoryBot.define do
viewable_id: product.id,
viewable_type: 'Spree::Product',
alt: "position 1",
attachment: Rack::Test::UploadedFile.new(white_logo_path),
attachment: white_logo_file,
position: 1
)

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

@@ -3,8 +3,9 @@
require 'spec_helper'
describe TermsOfServiceFile do
let(:pdf) { File.open(Rails.public_path.join('Terms-of-service.pdf')) }
let(:upload) { Rack::Test::UploadedFile.new(pdf, "application/pdf") }
include FileHelper
let(:upload) { terms_pdf_file }
describe ".current" do
it "returns nil" do

View File

@@ -26,9 +26,5 @@ module OpenFoodNetwork
expect(json_response).to eq("error" => "You are not authorized to perform that action.")
expect(response.status).to eq 401
end
def image(filename)
Rack::Test::UploadedFile.new(Rails.root + "spec/support/fixtures" + filename)
end
end
end

View File

@@ -2,11 +2,11 @@
module FileHelper
def black_logo_file
Rack::Test::UploadedFile.new(black_logo_path)
Rack::Test::UploadedFile.new(black_logo_path, "image/png")
end
def white_logo_file
Rack::Test::UploadedFile.new(white_logo_path)
Rack::Test::UploadedFile.new(white_logo_path, "image/png")
end
def black_logo_path
@@ -16,4 +16,9 @@ module FileHelper
def white_logo_path
Rails.root.join('app/webpacker/images/logo-white.png')
end
def terms_pdf_file
file_path = Rails.public_path.join("Terms-of-service.pdf")
fixture_file_upload(file_path, "application/pdf")
end
end

View File

@@ -340,6 +340,7 @@ describe '
context "as an enterprise user" do
let!(:tax_category) { create(:tax_category) }
let(:filter) { { producerFilter: 2 } }
let(:image_file_path) { Rails.root.join(file_fixture_path, "thinking-cat.jpg") }
before do
@new_user = create(:user)
@@ -627,14 +628,13 @@ describe '
end
it "upload a new product image including url filters" do
file_path = Rails.root + "spec/support/fixtures/thinking-cat.jpg"
product = create(:simple_product, supplier: @supplier2)
visit spree.admin_product_images_path(product, filter)
page.find('a#new_image_link').click
attach_file('image_attachment', file_path)
attach_file('image_attachment', image_file_path)
click_button "Create"
uri = URI.parse(current_url)
@@ -680,13 +680,11 @@ describe '
viewable_type: 'Spree::Product', alt: "position 1",
attachment: image, position: 1)
file_path = Rails.root + "spec/support/fixtures/thinking-cat.jpg"
visit spree.admin_product_images_path(product, filter)
page.find("a.icon-edit").click
attach_file('image_attachment', file_path)
attach_file('image_attachment', image_file_path)
click_button "Update"
uri = URI.parse(current_url)

View File

@@ -4,12 +4,10 @@ require 'system_helper'
describe 'Terms of Service banner' do
include AuthenticationHelper
include FileHelper
let(:admin_user) { create(:admin_user, terms_of_service_accepted_at: nil) }
let(:test_file) { "Terms-of-service.pdf" }
let(:pdf_upload) do
Rack::Test::UploadedFile.new(Rails.public_path.join(test_file), "application/pdf")
end
let(:pdf_upload) { terms_pdf_file }
before do
Spree::Config.enterprises_require_tos = true
@@ -43,7 +41,7 @@ describe 'Terms of Service banner' do
# Upload new ToS
visit admin_terms_of_service_files_path
attach_file "Attachment", Rails.public_path.join(test_file)
attach_file "Attachment", pdf_upload.path
click_button "Create Terms of service file"
# check it has been uploaded

View File

@@ -123,14 +123,8 @@ describe "As a consumer, I want to checkout my order" do
describe "terms and conditions" do
let(:customer) { create(:customer, enterprise: order.distributor, user:) }
let(:tos_url) { "https://example.org/tos" }
let(:system_terms_path) { Rails.public_path.join('Terms-of-service.pdf') }
let(:shop_terms_path) { Rails.public_path.join('Terms-of-ServiceUK.pdf') }
let(:system_terms) {
Rack::Test::UploadedFile.new(system_terms_path, "application/pdf")
}
let(:shop_terms) {
Rack::Test::UploadedFile.new(shop_terms_path, "application/pdf")
}
let(:system_terms) { terms_pdf_file }
let(:shop_terms) { terms_pdf_file }
context "when none are required" do
it "doesn't show checkbox or links" do
@@ -152,7 +146,7 @@ describe "As a consumer, I want to checkout my order" do
describe "when customer has not accepted T&Cs before" do
it "shows a link to the T&Cs and disables checkout button until terms are accepted" do
visit checkout_step_path(:summary)
expect(page).to have_link "Terms and Conditions", href: /#{shop_terms_path.basename}$/
expect(page).to have_link "Terms and Conditions", href: /Terms-of-service\.pdf/
expect(page).to have_field "order_accept_terms", checked: false
end
end
@@ -243,8 +237,8 @@ describe "As a consumer, I want to checkout my order" do
it "shows links to both terms and all need accepting" do
visit checkout_step_path(:summary)
expect(page).to have_link "Terms and Conditions", href: /#{shop_terms_path.basename}$/
expect(page).to have_link("Terms of service", href: /Terms-of-service.pdf/, count: 2)
expect(page).to have_link "Terms and Conditions", href: /Terms-of-service\.pdf/
expect(page).to have_link("Terms of service", href: /Terms-of-service\.pdf/, count: 2)
expect(page).to have_field "order_accept_terms", checked: false
end
end

View File

@@ -89,12 +89,7 @@ describe "As a consumer I want to check out my cart" do
pending 'login in as user' do
let(:user) { create(:user) }
let(:pdf_upload) {
Rack::Test::UploadedFile.new(
Rails.public_path.join('Terms-of-service.pdf'),
"application/pdf"
)
}
let(:pdf_upload) { terms_pdf_file }
before do
login_as(user)