Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Bound the certificate document, require an idempotent commit, tidy specs

Łukasz Adamczyk3a05f1587e20
diff --git a/spree/core/app/models/spree/tax_exemption_certificate.rb b/spree/core/app/models/spree/tax_exemption_certificate.rb
index 94335e6236..803d9cd107 100644
--- a/spree/core/app/models/spree/tax_exemption_certificate.rb
+++ b/spree/core/app/models/spree/tax_exemption_certificate.rb
@@ -28,6 +28,13 @@ module Spree
validates :certificate_number, :reason_code, presence: true
+ # A certificate is a scanned form, not an archive. Bounded because the
+ # download action reads the whole blob into memory to serve it, so an
+ # unbounded upload would be an unbounded allocation per request.
+ MAX_DOCUMENT_SIZE = 10.megabytes
+
+ validates :document, size: { less_than_or_equal_to: MAX_DOCUMENT_SIZE }
+
# Where the certificate holds, as codes: a blank country_iso claims every
# country, and a country with no state_code claims all of its states.
# Upcased on the way in, and unknown codes are kept as entered — a code
diff --git a/spree/core/app/models/spree/tax_provider/base.rb b/spree/core/app/models/spree/tax_provider/base.rb
index cfc2a6d802..57c905e85f 100644
--- a/spree/core/app/models/spree/tax_provider/base.rb
+++ b/spree/core/app/models/spree/tax_provider/base.rb
@@ -91,6 +91,12 @@ module Spree
# Finalizes tax with the provider when the order is placed. No-op for a
# provider without a remote ledger — the rows are already the record.
#
+ # **Must be idempotent.** Completion is replayable: a crash between the
+ # order commit and the cart stamp leaves `Carts::Complete` to re-run its
+ # finalize phase, which calls this again for an order already committed.
+ # Key the filing on the order and treat a duplicate as success, rather
+ # than filing a second document for one sale.
+ #
# @param order [Spree::Order]
# @return [void]
def commit(order); end
diff --git a/spree/core/lib/spree/testing_support/factories/return_factory.rb b/spree/core/lib/spree/testing_support/factories/return_factory.rb
index e6853f2a11..355a39ee67 100644
--- a/spree/core/lib/spree/testing_support/factories/return_factory.rb
+++ b/spree/core/lib/spree/testing_support/factories/return_factory.rb
@@ -41,4 +41,12 @@ FactoryBot.define do
end
end
end
+
+ # A single announced line. Its fulfillment item, line item and variant have to
+ # agree with each other, so the caller passes them — usually copied from a
+ # sibling line it already holds.
+ factory :return_line_item, class: Spree::ReturnLineItem do
+ quantity { 1 }
+ received_quantity { 0 }
+ end
end
diff --git a/spree/core/spec/jobs/spree/tax_identifiers/validate_job_spec.rb b/spree/core/spec/jobs/spree/tax_identifiers/validate_job_spec.rb
index 1c14cf3ec2..4d42bd15ed 100644
--- a/spree/core/spec/jobs/spree/tax_identifiers/validate_job_spec.rb
+++ b/spree/core/spec/jobs/spree/tax_identifiers/validate_job_spec.rb
@@ -10,10 +10,11 @@ describe Spree::TaxIdentifiers::ValidateJob do
Spree::TaxIdentifiers::ValidationResult.new(status: 'verified', checked_at: Time.current)
end
end)
- Spree.tax_identifier_validators['eu_vat'] = 'SpecJobValidator'
end
- after { Spree.tax_identifier_validators.delete('eu_vat') }
+ around do |example|
+ with_tax_identifier_validator('eu_vat', 'SpecJobValidator') { example.run }
+ end
it 'records the registry verdict on the row' do
described_class.perform_now(tax_identifier.id)
diff --git a/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb
index 753f389fe0..408087911d 100644
--- a/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb
+++ b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb
@@ -217,8 +217,8 @@ describe '6.0 data migration tasks' do
let(:france) { Spree::Country.find_by(iso: 'FR') || create(:country, iso: 'FR', name: 'France') }
def zone_with(*zoneables)
- zone = Spree::Zone.create!(name: "Tax Zone #{Time.current.to_f}#{rand(1000)}", kind: 'country')
- zoneables.each { |zoneable| zone.zone_members.create!(zoneable: zoneable) }
+ zone = create(:zone, name: "Tax Zone #{Time.current.to_f}#{rand(1000)}", kind: 'country')
+ zoneables.each { |zoneable| create(:zone_member, zone: zone, zoneable: zoneable) }
zone
end
@@ -336,9 +336,9 @@ describe '6.0 data migration tasks' do
describe 'spree:migrate_zones_to_delivery_zones' do
let!(:country) { Spree::Country.find_by(iso: 'US') || create(:country_us) }
let!(:zone) do
- zone = Spree::Zone.create!(name: "Legacy Ship Zone #{Time.current.to_f}", kind: 'shipping')
- zone.zone_members.create!(zoneable: country)
- zone
+ create(:zone, name: "Legacy Ship Zone #{Time.current.to_f}", kind: 'shipping').tap do |zone|
+ create(:zone_member, zone: zone, zoneable: country)
+ end
end
let!(:delivery_method) { create(:shipping_method) }
diff --git a/spree/core/spec/models/spree/tax_exemption_certificate_spec.rb b/spree/core/spec/models/spree/tax_exemption_certificate_spec.rb
index 7ba892b7d2..e1bb6f7804 100644
--- a/spree/core/spec/models/spree/tax_exemption_certificate_spec.rb
+++ b/spree/core/spec/models/spree/tax_exemption_certificate_spec.rb
@@ -98,6 +98,34 @@ describe Spree::TaxExemptionCertificate, type: :model do
end
end
+ describe 'the attached document' do
+ let(:certificate) { build(:tax_exemption_certificate, company: company) }
+
+ it 'accepts one within the limit' do
+ certificate.document.attach(
+ io: StringIO.new('a' * 1.kilobyte), filename: 'resale.pdf', content_type: 'application/pdf'
+ )
+
+ expect(certificate).to be_valid
+ end
+
+ # Bounded because the download action reads the whole blob into memory.
+ it 'refuses one over the limit' do
+ certificate.document.attach(
+ io: StringIO.new('a' * (described_class::MAX_DOCUMENT_SIZE + 1)),
+ filename: 'huge.pdf', content_type: 'application/pdf'
+ )
+
+ expect(certificate).not_to be_valid
+ expect(certificate.errors[:document]).to be_present
+ end
+
+ it 'is valid with none — a certificate may be recorded before it is scanned' do
+ expect(certificate.document).not_to be_attached
+ expect(certificate).to be_valid
+ end
+ end
+
describe '#can_be_deleted?' do
it 'refuses once verified — revoke instead' do
expect(create(:tax_exemption_certificate, :verified, company: company).can_be_deleted?).to be(false)
diff --git a/spree/core/spec/models/spree/tax_identifier_spec.rb b/spree/core/spec/models/spree/tax_identifier_spec.rb
index 68293643c5..3cf8921f27 100644
--- a/spree/core/spec/models/spree/tax_identifier_spec.rb
+++ b/spree/core/spec/models/spree/tax_identifier_spec.rb
@@ -67,10 +67,7 @@ describe Spree::TaxIdentifier, type: :model do
before { stub_const('SpecEuVatValidator', validator) }
around do |example|
- Spree.tax_identifier_validators['eu_vat'] = 'SpecEuVatValidator'
- example.run
- ensure
- Spree.tax_identifier_validators.delete('eu_vat')
+ with_tax_identifier_validator('eu_vat', 'SpecEuVatValidator') { example.run }
end
it 'rejects a malformed number as a typo to fix' do
@@ -93,10 +90,7 @@ describe Spree::TaxIdentifier, type: :model do
# need the event bus the suite disables by default.
describe 'the registry check', events: true do
around do |example|
- Spree.tax_identifier_validators['eu_vat'] = 'Spree::TaxIdentifiers::Validator::Base'
- example.run
- ensure
- Spree.tax_identifier_validators.delete('eu_vat')
+ with_tax_identifier_validator('eu_vat', 'Spree::TaxIdentifiers::Validator::Base') { example.run }
end
it 'is queued when a checkable number is entered, and marked pending' do
@@ -159,10 +153,9 @@ describe Spree::TaxIdentifier, type: :model do
identifier = create(:tax_identifier, customer: customer, kind: 'eu_vat')
expect(identifier).not_to be_validatable
- Spree.tax_identifier_validators['eu_vat'] = 'Spree::TaxIdentifiers::Validator::Base'
- expect(identifier).to be_validatable
- ensure
- Spree.tax_identifier_validators.delete('eu_vat')
+ with_tax_identifier_validator('eu_vat', 'Spree::TaxIdentifiers::Validator::Base') do
+ expect(identifier).to be_validatable
+ end
end
end
diff --git a/spree/core/spec/services/spree/tax_identifiers/validate_spec.rb b/spree/core/spec/services/spree/tax_identifiers/validate_spec.rb
index 0379677962..d9d49106c5 100644
--- a/spree/core/spec/services/spree/tax_identifiers/validate_spec.rb
+++ b/spree/core/spec/services/spree/tax_identifiers/validate_spec.rb
@@ -9,7 +9,9 @@ describe Spree::TaxIdentifiers::Validate do
Spree.tax_identifier_validators['eu_vat'] = 'SpecRegistryValidator'
end
- after { Spree.tax_identifier_validators.delete('eu_vat') }
+ around do |example|
+ with_tax_identifier_validator('eu_vat', Spree.tax_identifier_validators['eu_vat']) { example.run }
+ end
context 'when the registry answers' do
before do
@@ -90,7 +92,10 @@ describe Spree::TaxIdentifiers::Validate do
it 'reports the failure and records that nobody could answer' do
expect(Rails.error).to receive(:report).with(
- instance_of(Spree::TaxIdentifiers::ValidationError), hash_including(:context)
+ instance_of(Spree::TaxIdentifiers::ValidationError),
+ handled: false,
+ context: hash_including(:tax_identifier_id, :kind),
+ source: 'spree.core'
)
result = described_class.call(tax_identifier: tax_identifier)
diff --git a/spree/core/spec/support/tax_identifier_validators.rb b/spree/core/spec/support/tax_identifier_validators.rb
new file mode 100644
index 0000000000..2aa646ad29
--- /dev/null
+++ b/spree/core/spec/support/tax_identifier_validators.rb
@@ -0,0 +1,23 @@
+# The validator registry is process-global. Specs that register one have to put
+# back whatever was there before rather than deleting their own entry, or they
+# strip a registration another spec (or an installed provider gem) relies on and
+# make the suite order-dependent.
+module TaxIdentifierValidatorHelpers
+ def with_tax_identifier_validator(kind, class_name)
+ previous = Spree.tax_identifier_validators[kind]
+ had_previous = Spree.tax_identifier_validators.key?(kind)
+ Spree.tax_identifier_validators[kind] = class_name
+
+ yield
+ ensure
+ if had_previous
+ Spree.tax_identifier_validators[kind] = previous
+ else
+ Spree.tax_identifier_validators.delete(kind)
+ end
+ end
+end
+
+RSpec.configure do |config|
+ config.include TaxIdentifierValidatorHelpers
+end
diff --git a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb
index 1041f41ce4..7c42c223cc 100644
--- a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb
+++ b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb
@@ -177,13 +177,12 @@ RSpec.describe 'Spree::Returns workflows' do
line = return_record.return_line_items.first
line.update!(quantity: 3, received_quantity: 2)
# A second announced line the warehouse never received.
- unreceived = return_record.return_line_items.create!(
- fulfillment_item: line.fulfillment_item,
- line_item: line.line_item,
- variant: line.variant,
- quantity: 1,
- received_quantity: 0
- )
+ unreceived = create(:return_line_item,
+ return: return_record,
+ fulfillment_item: line.fulfillment_item,
+ line_item: line.line_item,
+ variant: line.variant,
+ quantity: 1)
order = return_record.order
provider = instance_double(Spree::TaxProvider::Internal, refund: nil, estimate: nil)