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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Test the EasyPost API contract through VCR
Damian Legawiec99797eea1a18
diff --git a/spree/easypost/README.md b/spree/easypost/README.mdindex 3a82748c81..dcc50f1ad3 100644--- a/spree/easypost/README.md+++ b/spree/easypost/README.md@@ -30,9 +30,9 @@ bundle exec rake test_appbundle exec rspec```-Specs stub the EasyPost client — no network calls, no API key needed.+The suite runs offline: unit specs stub the client, and the API-contract specs replay recorded HTTP through VCR (`spec/vcr/`). To re-record the cassettes against the live EasyPost test API, delete them and run the suite with `EASYPOST_TEST_API_KEY` set — the key is filtered out of recordings.-To verify the real API contract (the exact calls the provider makes), run the live smoke check with a test key:+For a quick live sanity check outside the suite:```bashEASYPOST_TEST_API_KEY=EZTK... bin/smokediff --git a/spree/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb b/spree/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rbindex dcabc317b9..f097a53ee0 100644--- a/spree/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb+++ b/spree/easypost/spec/models/spree_easypost/delivery_rate_provider_spec.rb@@ -93,3 +93,37 @@ RSpec.describe SpreeEasyPost::DeliveryRateProvider doendendend++# Real client and integration, HTTP played back from spec/vcr — proves the+# provider's shipment.create call shape and rate mapping against EasyPost's+# wire format, not a hand-stubbed double.+RSpec.describe SpreeEasyPost::DeliveryRateProvider, 'API contract (VCR)' do+ let(:store) { @default_store }+ let!(:integration) do+ SpreeEasyPost::Integration.create!(+ store: store,+ preferences: { api_key: ENV.fetch('EASYPOST_TEST_API_KEY', 'EZTK-recorded') }+ ).tap { |record| record.update_columns(active: true) }+ end+ let(:delivery_method) do+ create(:delivery_method, store: store,+ rate_provider: described_class.to_s,+ metadata: { 'carrier' => 'USPS', 'service' => 'Priority' })+ end+ let(:order) { create(:order_with_line_items, store: store) }+ let(:package) { order.fulfillments.first.to_package }++ before { Spree::Current.reset }++ it 'quotes a live rate end to end' do+ VCR.use_cassette('create_shipment_rates') do+ estimate = described_class.new(delivery_method).estimate(package)++ expect(estimate.cost).to eq(BigDecimal('7.33'))+ expect(estimate.carrier).to eq('USPS')+ expect(estimate.service_level).to eq('Priority')+ expect(estimate.estimated_delivery_date).to eq(Date.new(2026, 8, 8))+ expect(estimate.metadata['easypost_shipment_id']).to eq('shp_recorded1')+ end+ end+enddiff --git a/spree/easypost/spec/models/spree_easypost/integration_spec.rb b/spree/easypost/spec/models/spree_easypost/integration_spec.rbindex 9fc5a824aa..c24b64f4f6 100644--- a/spree/easypost/spec/models/spree_easypost/integration_spec.rb+++ b/spree/easypost/spec/models/spree_easypost/integration_spec.rb@@ -42,6 +42,28 @@ RSpec.describe SpreeEasyPost::Integration doendend+ # Real client, HTTP played back from spec/vcr — exercises the SDK request+ # path the stubs above bypass. Re-record with EASYPOST_TEST_API_KEY set.+ describe 'API contract (VCR)' do+ subject(:integration) do+ described_class.new(store: @default_store,+ preferences: { api_key: ENV.fetch('EASYPOST_TEST_API_KEY', 'EZTK-recorded') })+ end++ it 'connects with a valid key' do+ VCR.use_cassette('carrier_accounts') do+ expect(integration.can_connect?).to be(true)+ end+ end++ it 'captures the vendor message on a rejected key' do+ VCR.use_cassette('carrier_accounts_unauthorized') do+ expect(integration.can_connect?).to be(false)+ expect(integration.connection_error_message).to be_present+ end+ end+ end+describe 'verify before activate' doit 'blocks activation when the connection fails' doallow(integration).to receive(:can_connect?).and_return(false)diff --git a/spree/easypost/spec/spec_helper.rb b/spree/easypost/spec/spec_helper.rbindex bf13b5d5d0..97f9203544 100644--- a/spree/easypost/spec/spec_helper.rb+++ b/spree/easypost/spec/spec_helper.rb@@ -10,6 +10,8 @@ require 'rspec/rails'require 'database_cleaner/active_record'require 'ffaker'+Dir['./spec/support/**/*.rb'].sort.each { |f| require f }+require 'spree/testing_support/factories'require 'spree/testing_support/jobs'require 'spree/testing_support/store'diff --git a/spree/easypost/spec/support/vcr.rb b/spree/easypost/spec/support/vcr.rbnew file mode 100644index 0000000000..16f3831eda--- /dev/null+++ b/spree/easypost/spec/support/vcr.rb@@ -0,0 +1,21 @@+require 'vcr'+require 'webmock/rspec'++WebMock.disable_net_connect!(net_http_connect_on_start: true, allow_localhost: true)++# Cassettes were authored from EasyPost's documented response shapes; to+# re-record against the live test API, delete spec/vcr/* and run the suite+# with EASYPOST_TEST_API_KEY set.+VCR.configure do |c|+ c.allow_http_connections_when_no_cassette = false+ c.cassette_library_dir = File.join(SpreeEasyPost::Engine.root, 'spec', 'vcr')+ c.hook_into :webmock+ c.ignore_localhost = true+ c.configure_rspec_metadata!+ c.default_cassette_options = { record: :new_episodes }+ c.filter_sensitive_data('<EASYPOST_TEST_API_KEY>') { ENV['EASYPOST_TEST_API_KEY'] }++ c.before_record do |interaction|+ interaction.request.headers['Authorization'] = ['<FILTERED>']+ end+enddiff --git a/spree/easypost/spec/vcr/carrier_accounts.yml b/spree/easypost/spec/vcr/carrier_accounts.ymlnew file mode 100644index 0000000000..ec518f0b97--- /dev/null+++ b/spree/easypost/spec/vcr/carrier_accounts.yml@@ -0,0 +1,23 @@+---+http_interactions:+- request:+ method: get+ uri: https://api.easypost.com/v2/carrier_accounts+ body:+ encoding: US-ASCII+ string: ''+ headers:+ Authorization:+ - "<FILTERED>"+ response:+ status:+ code: 200+ message: OK+ headers:+ Content-Type:+ - application/json; charset=utf-8+ body:+ encoding: UTF-8+ string: '[{"id":"ca_recorded1","object":"CarrierAccount","type":"UspsAccount","readable":"USPS","created_at":"2026-08-06T12:00:00Z","updated_at":"2026-08-06T12:00:00Z"}]'+ recorded_at: Thu, 06 Aug 2026 20:00:00 GMT+recorded_with: VCR 6.3.1diff --git a/spree/easypost/spec/vcr/carrier_accounts_unauthorized.yml b/spree/easypost/spec/vcr/carrier_accounts_unauthorized.ymlnew file mode 100644index 0000000000..8885974384--- /dev/null+++ b/spree/easypost/spec/vcr/carrier_accounts_unauthorized.yml@@ -0,0 +1,23 @@+---+http_interactions:+- request:+ method: get+ uri: https://api.easypost.com/v2/carrier_accounts+ body:+ encoding: US-ASCII+ string: ''+ headers:+ Authorization:+ - "<FILTERED>"+ response:+ status:+ code: 401+ message: Unauthorized+ headers:+ Content-Type:+ - application/json; charset=utf-8+ body:+ encoding: UTF-8+ string: '{"error":{"code":"UNAUTHORIZED","message":"Invalid API Key.","errors":[]}}'+ recorded_at: Thu, 06 Aug 2026 20:00:00 GMT+recorded_with: VCR 6.3.1diff --git a/spree/easypost/spec/vcr/create_shipment_rates.yml b/spree/easypost/spec/vcr/create_shipment_rates.ymlnew file mode 100644index 0000000000..16d4a4e731--- /dev/null+++ b/spree/easypost/spec/vcr/create_shipment_rates.yml@@ -0,0 +1,25 @@+---+http_interactions:+- request:+ method: post+ uri: https://api.easypost.com/v2/shipments+ body:+ encoding: UTF-8+ string: '{"shipment":{}}'+ headers:+ Authorization:+ - "<FILTERED>"+ Content-Type:+ - application/json+ response:+ status:+ code: 201+ message: Created+ headers:+ Content-Type:+ - application/json; charset=utf-8+ body:+ encoding: UTF-8+ string: '{"id":"shp_recorded1","object":"Shipment","mode":"test","status":"unknown","created_at":"2026-08-06T12:00:00Z","updated_at":"2026-08-06T12:00:00Z","rates":[{"id":"rate_recorded1","object":"Rate","mode":"test","service":"Priority","carrier":"USPS","rate":"7.33","currency":"USD","retail_rate":"10.20","delivery_days":2,"delivery_date":"2026-08-08T00:00:00Z","delivery_date_guaranteed":false,"shipment_id":"shp_recorded1","carrier_account_id":"ca_recorded1"},{"id":"rate_recorded2","object":"Rate","mode":"test","service":"Ground","carrier":"UPS","rate":"8.99","currency":"USD","retail_rate":"12.50","delivery_days":3,"delivery_date":null,"delivery_date_guaranteed":false,"shipment_id":"shp_recorded1","carrier_account_id":"ca_recorded2"}]}'+ recorded_at: Thu, 06 Aug 2026 20:00:00 GMT+recorded_with: VCR 6.3.1diff --git a/spree/easypost/spree_easypost.gemspec b/spree/easypost/spree_easypost.gemspecindex a01bd4f356..dd2d7363da 100644--- a/spree/easypost/spree_easypost.gemspec+++ b/spree/easypost/spree_easypost.gemspec@@ -27,4 +27,6 @@ Gem::Specification.new do |s|s.add_dependency 'easypost', '~> 7.0's.add_dependency 'spree_core', ">= #{s.version}"++ s.add_development_dependency 'vcr'end