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.

Resolve digital downloads through a provider strategy

Damian Legawiec6d8835b8c659
diff --git a/packages/dashboard-starter/src/routeTree.gen.ts b/packages/dashboard-starter/src/routeTree.gen.ts
index 3226f35dba..cf6c752f6c 100644
--- a/packages/dashboard-starter/src/routeTree.gen.ts
+++ b/packages/dashboard-starter/src/routeTree.gen.ts
@@ -9,6 +9,7 @@
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.
import { Route as rootRouteImport } from './../../dashboard/src/routes/__root'
+import { Route as setupRouteImport } from './../../dashboard/src/routes/setup'
import { Route as resetPasswordRouteImport } from './../../dashboard/src/routes/reset-password'
import { Route as loginRouteImport } from './../../dashboard/src/routes/login'
import { Route as forgotPasswordRouteImport } from './../../dashboard/src/routes/forgot-password'
@@ -86,6 +87,11 @@ import { Route as ProductsPriceListsPriceListIdIndexRouteImport } from './../../
import { Route as SettingsDeliveryProfilesProfileIdMethodsNewRouteImport } from './../../dashboard/src/routes/_authenticated/$storeId/settings/delivery-profiles/$profileId/methods/new'
import { Route as SettingsDeliveryProfilesProfileIdMethodsMethodIdRouteImport } from './../../dashboard/src/routes/_authenticated/$storeId/settings/delivery-profiles/$profileId/methods/$methodId'
+const setupRoute = setupRouteImport.update({
+ id: '/setup',
+ path: '/setup',
+ getParentRoute: () => rootRouteImport,
+} as any)
const resetPasswordRoute = resetPasswordRouteImport.update({
id: '/reset-password',
path: '/reset-password',
@@ -484,6 +490,7 @@ export interface FileRoutesByFullPath {
'/forgot-password': typeof forgotPasswordRoute
'/login': typeof loginRoute
'/reset-password': typeof resetPasswordRoute
+ '/setup': typeof setupRoute
'/$storeId': typeof authenticatedStoreIdRouteWithChildren
'/accept-invitation/$invitationId': typeof acceptInvitationDotinvitationIdRoute
'/$storeId/$': typeof SplatRoute
@@ -560,6 +567,7 @@ export interface FileRoutesByTo {
'/forgot-password': typeof forgotPasswordRoute
'/login': typeof loginRoute
'/reset-password': typeof resetPasswordRoute
+ '/setup': typeof setupRoute
'/accept-invitation/$invitationId': typeof acceptInvitationDotinvitationIdRoute
'/': typeof authenticatedIndexRoute
'/$storeId/$': typeof SplatRoute
@@ -637,6 +645,7 @@ export interface FileRoutesById {
'/forgot-password': typeof forgotPasswordRoute
'/login': typeof loginRoute
'/reset-password': typeof resetPasswordRoute
+ '/setup': typeof setupRoute
'/_authenticated/$storeId': typeof authenticatedStoreIdRouteWithChildren
'/accept-invitation/$invitationId': typeof acceptInvitationDotinvitationIdRoute
'/_authenticated/': typeof authenticatedIndexRoute
@@ -717,6 +726,7 @@ export interface FileRouteTypes {
| '/forgot-password'
| '/login'
| '/reset-password'
+ | '/setup'
| '/$storeId'
| '/accept-invitation/$invitationId'
| '/$storeId/$'
@@ -793,6 +803,7 @@ export interface FileRouteTypes {
| '/forgot-password'
| '/login'
| '/reset-password'
+ | '/setup'
| '/accept-invitation/$invitationId'
| '/'
| '/$storeId/$'
@@ -869,6 +880,7 @@ export interface FileRouteTypes {
| '/forgot-password'
| '/login'
| '/reset-password'
+ | '/setup'
| '/_authenticated/$storeId'
| '/accept-invitation/$invitationId'
| '/_authenticated/'
@@ -948,11 +960,19 @@ export interface RootRouteChildren {
forgotPasswordRoute: typeof forgotPasswordRoute
loginRoute: typeof loginRoute
resetPasswordRoute: typeof resetPasswordRoute
+ setupRoute: typeof setupRoute
acceptInvitationDotinvitationIdRoute: typeof acceptInvitationDotinvitationIdRoute
}
declare module '@tanstack/react-router' {
interface FileRoutesByPath {
+ '/setup': {
+ id: '/setup'
+ path: '/setup'
+ fullPath: '/setup'
+ preLoaderRoute: typeof setupRouteImport
+ parentRoute: typeof rootRouteImport
+ }
'/reset-password': {
id: '/reset-password'
path: '/reset-password'
@@ -1669,6 +1689,7 @@ const rootRouteChildren: RootRouteChildren = {
forgotPasswordRoute: forgotPasswordRoute,
loginRoute: loginRoute,
resetPasswordRoute: resetPasswordRoute,
+ setupRoute: setupRoute,
acceptInvitationDotinvitationIdRoute: acceptInvitationDotinvitationIdRoute,
}
export const routeTree = rootRouteImport
diff --git a/spree/api/app/controllers/spree/api/v3/store/digital_links_controller.rb b/spree/api/app/controllers/spree/api/v3/store/digital_links_controller.rb
index f5408e5775..dcac3fd807 100644
--- a/spree/api/app/controllers/spree/api/v3/store/digital_links_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/digital_links_controller.rb
@@ -13,14 +13,14 @@ module Spree
# GET /api/v3/store/digital_links/:token
#
- # Redirects to a short-lived signed storage URL rather than streaming
- # the file, so a large download does not hold a web worker open for
- # its duration.
+ # Hands the customer their deliverable: a redirect to a short-lived
+ # signed URL (an uploaded file, an external link) or an inline body (a
+ # license key). The provider decides which; this controller only
+ # settles authorization and renders the shape.
def show
- if (download_url = authorized_download_url)
+ if (delivery = authorized_delivery)
@resource.publish_event('digital_link.downloaded')
-
- redirect_to download_url, allow_other_host: true
+ render_delivery(delivery)
else
render_error(
code: ERROR_CODES[:digital_link_expired],
@@ -32,16 +32,44 @@ module Spree
private
- # Everything that can refuse the download is settled before the click
- # is spent, so a missing file never burns a customer's allowance.
- def authorized_download_url
- return unless @resource.digital_asset.downloadable?
+ # Everything that can refuse is settled before the click is spent: the
+ # link must permit a download, the window must be open, the provider
+ # must actually produce a deliverable (a provider that errors or has
+ # nothing refuses cleanly), and only then is the click consumed under
+ # the lock. So a dry provider or a missing file never burns an
+ # allowance.
+ #
+ # @return [Spree::DigitalDelivery, nil]
+ def authorized_delivery
+ return unless @resource.authorizable?
window = signed_url_window
return if window <= 0
+
+ delivery = safe_deliver(window)
+ return unless delivery&.present?
return unless @resource.authorize!
- @resource.digital_asset.download_url(expires_in: window)
+ delivery
+ end
+
+ # A provider is external I/O; an exception must refuse, not 500, and
+ # must not spend the click (it hasn't been spent yet at this point).
+ def safe_deliver(window)
+ @resource.digital_asset.deliver(@resource, expires_in: window)
+ rescue StandardError => e
+ Rails.logger.error("Digital delivery failed for #{@resource.prefixed_id}: #{e.class}: #{e.message}")
+ nil
+ end
+
+ def render_delivery(delivery)
+ if delivery.redirect?
+ redirect_to delivery.redirect_url, allow_other_host: true
+ else
+ send_data delivery.inline_value,
+ type: delivery.content_type.presence || 'application/octet-stream',
+ disposition: 'inline'
+ end
end
# The signed URL is a bearer credential, so it must never outlive the
@@ -54,12 +82,21 @@ module Spree
[window, expiry - Time.current].min
end
+ # The download token is the request's credential and selects the
+ # store (tokens are globally unique via has_secure_token) — no
+ # publishable key accompanies emailed download links, so there is no
+ # other credential to derive store context from.
def set_resource
@resource = digital_link_scope.find_by!(token: params[:token])
+ Spree::Current.store = current_store
end
def digital_link_scope
- current_store.digital_links
+ Spree::DigitalLink
+ end
+
+ def current_store
+ @current_store ||= @resource&.store || super
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/store/digital_links_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/digital_links_controller_spec.rb
index 496d6d4a64..5b7b55f357 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/digital_links_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/digital_links_controller_spec.rb
@@ -97,9 +97,9 @@ RSpec.describe Spree::Api::V3::Store::DigitalLinksController, type: :controller
it 'clamps the signed URL to the link expiry' do
digital_link.update_column(:created_at, (1.day - 30.seconds).ago)
- expect_any_instance_of(Spree::DigitalAsset).to receive(:download_url) do |_asset, expires_in:|
+ expect_any_instance_of(Spree::DigitalAsset).to receive(:deliver) do |_asset, _link, expires_in:|
expect(expires_in).to be <= 30.seconds
- 'https://storage.example.com/signed'
+ Spree::DigitalDelivery.new(redirect_url: 'https://storage.example.com/signed')
end
get :show, params: { token: digital_link.token }
@@ -120,6 +120,42 @@ RSpec.describe Spree::Api::V3::Store::DigitalLinksController, type: :controller
end
end
+ context 'when the provider returns an inline value (a license key)' do
+ before do
+ allow_any_instance_of(Spree::DigitalAsset).to receive(:deliver).and_return(
+ Spree::DigitalDelivery.new(inline_value: 'KEY-ABC-123', content_type: 'text/plain')
+ )
+ end
+
+ it 'renders the value as the body, not a redirect' do
+ get :show, params: { token: digital_link.token }
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to eq('KEY-ABC-123')
+ expect(response.media_type).to eq('text/plain')
+ end
+
+ it 'spends exactly one download' do
+ expect {
+ get :show, params: { token: digital_link.token }
+ }.to change { digital_link.reload.access_counter }.by(1)
+ end
+ end
+
+ context 'when the provider raises' do
+ before do
+ allow_any_instance_of(Spree::DigitalAsset).to receive(:deliver).and_raise(StandardError, 'upstream down')
+ end
+
+ it 'refuses cleanly without spending a download' do
+ expect {
+ get :show, params: { token: digital_link.token }
+ }.not_to change { digital_link.reload.access_counter }
+
+ expect(response).to have_http_status(:forbidden)
+ end
+ end
+
context 'with invalid token' do
it 'returns not found' do
get :show, params: { token: 'invalid_token' }
@@ -128,17 +164,21 @@ RSpec.describe Spree::Api::V3::Store::DigitalLinksController, type: :controller
end
end
- context 'with token from another store' do
+ # An emailed download link carries no publishable key, so the request has
+ # no store credential — the globally unique token is the credential, and the
+ # store is derived from the link's own order. A link therefore resolves
+ # regardless of which store the request would otherwise resolve to.
+ context 'with a token whose order belongs to another store' do
let(:other_store) { create(:store) }
let(:other_order) { create(:order_with_line_items, store: other_store) }
let(:other_line_item) { other_order.line_items.first }
let(:other_digital_asset) { create(:digital_asset, variant: other_line_item.variant) }
let(:other_digital_link) { create(:digital_link, digital_asset: other_digital_asset, line_item: other_line_item) }
- it 'returns not found' do
+ it 'resolves the link through its token and serves the download' do
get :show, params: { token: other_digital_link.token }
- expect(response).to have_http_status(:not_found)
+ expect(response).to have_http_status(:found)
end
end
diff --git a/spree/core/app/models/spree/digital_asset.rb b/spree/core/app/models/spree/digital_asset.rb
index 2e5c7a4f33..9119ce84ed 100644
--- a/spree/core/app/models/spree/digital_asset.rb
+++ b/spree/core/app/models/spree/digital_asset.rb
@@ -9,7 +9,13 @@ module Spree
has_one_attached :attachment, service: Spree.private_storage_service_name
- validates :attachment, attached: true
+ # A blank provider_type is the uploaded-file default; the provider class
+ # says whether an attachment is required, so file assets keep today's rule
+ # and provider-backed assets (which carry no file) are exempt.
+ DEFAULT_PROVIDER = 'Spree::DigitalAssetProvider::File'.freeze
+
+ validates :attachment, attached: true, if: -> { provider_class.requires_attachment? }
+ validate :provider_type_is_registered, if: -> { provider_type.present? }
validates :authorized_clicks, numericality: { only_integer: true, greater_than: 0 }, allow_nil: true
validates :authorized_days, numericality: { only_integer: true, greater_than: 0 }, allow_nil: true
@@ -40,10 +46,26 @@ module Spree
product.store
end
- # Where the customer is sent to fetch the file. Owning this here keeps
- # callers out of the storage layer, so asset kinds that are not uploaded
- # files can answer it differently.
+ # The strategy that produces this asset's deliverable. A blank provider_type
+ # resolves to the uploaded-file default, exactly as DeliveryMethod resolves
+ # its provider — so an unset column is a File asset, not an error.
#
+ # @return [Class]
+ def provider_class
+ (provider_type.presence || DEFAULT_PROVIDER).safe_constantize || DEFAULT_PROVIDER.constantize
+ end
+
+ # Produces the deliverable for one authorized download — a redirect URL or
+ # an inline value — via the provider. Callers stay out of the storage layer
+ # so a provider-backed asset can answer without an attachment.
+ #
+ # @param digital_link [Spree::DigitalLink]
+ # @param expires_in [ActiveSupport::Duration]
+ # @return [Spree::DigitalDelivery, nil]
+ def deliver(digital_link, expires_in:)
+ provider_class.new(self).deliver(digital_link, expires_in: expires_in)
+ end
+
# @return [Boolean] whether there is anything to hand to a customer
def downloadable?
attachment.attached?
@@ -59,6 +81,14 @@ module Spree
private
+ # A provider_type must name a registered provider — a stale class name from
+ # a removed gem, or a typo, is rejected rather than blowing up at download.
+ def provider_type_is_registered
+ return if Spree.digital_asset_providers.map(&:to_s).include?(provider_type)
+
+ errors.add(:provider_type, :not_registered)
+ end
+
# The legacy digital.* events are dual-emitted for one release
# (webhook contract bridge — removed in 6.1).
def publish_create_event
diff --git a/spree/core/app/models/spree/digital_asset_provider/base.rb b/spree/core/app/models/spree/digital_asset_provider/base.rb
new file mode 100644
index 0000000000..0dc3a78a28
--- /dev/null
+++ b/spree/core/app/models/spree/digital_asset_provider/base.rb
@@ -0,0 +1,57 @@
+module Spree
+ module DigitalAssetProvider
+ # Strategy that answers "how is this digital asset's deliverable produced?".
+ #
+ # Stored as a class-name string in `DigitalAsset#provider_type` and
+ # constantized at call time; registered via `Spree.digital_asset_providers`.
+ # A blank `provider_type` resolves to {File}, the default — so an uploaded
+ # file is just the provider every asset already had.
+ #
+ # Unlike delivery-rate or tax providers, a digital-asset provider does NOT
+ # resolve credentials through a `Spree::Integration`: it is host-app glue
+ # into internal software and owns its own configuration. The base gives it
+ # only the asset.
+ class Base
+ attr_reader :digital_asset
+
+ class << self
+ # Human-readable name for admin UIs. Provider gems following the
+ # `SpreeAcme::LicenseProvider` convention get the outer module
+ # ("Acme"); a bare in-app class gets its own titleized name.
+ #
+ # @return [String]
+ def provider_name
+ leaf = name.demodulize
+ outer = name.deconstantize.delete_prefix('Spree')
+
+ return leaf.titleize if outer.blank? || !leaf.end_with?('Provider')
+
+ outer.delete_prefix('::')
+ end
+
+ # Whether an asset backed by this provider must carry an uploaded file.
+ # {File} needs one; a provider that resolves its deliverable elsewhere
+ # does not. Drives conditional validation and the admin create form.
+ #
+ # @return [Boolean]
+ def requires_attachment?
+ false
+ end
+ end
+
+ # @param digital_asset [Spree::DigitalAsset]
+ def initialize(digital_asset)
+ @digital_asset = digital_asset
+ end
+
+ # Produces the deliverable for one authorized download.
+ #
+ # @param digital_link [Spree::DigitalLink] the authorized grant
+ # @param expires_in [ActiveSupport::Duration] lifetime of any signed URL
+ # @return [Spree::DigitalDelivery]
+ def deliver(digital_link, expires_in:)
+ raise NotImplementedError, "#{self.class} must implement #deliver"
+ end
+ end
+ end
+end
diff --git a/spree/core/app/models/spree/digital_asset_provider/file.rb b/spree/core/app/models/spree/digital_asset_provider/file.rb
new file mode 100644
index 0000000000..88e5d4ef6e
--- /dev/null
+++ b/spree/core/app/models/spree/digital_asset_provider/file.rb
@@ -0,0 +1,20 @@
+module Spree
+ module DigitalAssetProvider
+ # The default provider: an uploaded file on private storage. Reproduces the
+ # pre-wave-6 download behaviour — a short-lived signed storage URL the
+ # customer is redirected to — now expressed through the provider seam.
+ class File < Base
+ def self.requires_attachment?
+ true
+ end
+
+ def deliver(_digital_link, expires_in:)
+ return unless digital_asset.attachment.attached?
+
+ Spree::DigitalDelivery.new(
+ redirect_url: digital_asset.attachment.url(expires_in: expires_in, disposition: :attachment)
+ )
+ end
+ end
+ end
+end
diff --git a/spree/core/app/models/spree/digital_delivery.rb b/spree/core/app/models/spree/digital_delivery.rb
new file mode 100644
index 0000000000..b723a101b8
--- /dev/null
+++ b/spree/core/app/models/spree/digital_delivery.rb
@@ -0,0 +1,28 @@
+module Spree
+ # What a digital asset provider hands back for one authorized download.
+ # Ephemeral, never persisted — the store download controller turns it into
+ # either a redirect (a signed storage URL, an external link) or a rendered
+ # body (a license key to show the customer).
+ #
+ # A delivery carries exactly one shape: `redirect_url`, or `inline_value`
+ # (+ `content_type`). A blank delivery means "nothing to hand over" and the
+ # controller refuses the download.
+ class DigitalDelivery
+ include ActiveModel::Model
+ include ActiveModel::Attributes
+
+ attribute :redirect_url, :string
+ attribute :inline_value, :string
+ attribute :content_type, :string
+
+ # @return [Boolean] send the customer to a URL rather than render a body
+ def redirect?
+ redirect_url.present?
+ end
+
+ # @return [Boolean] whether there is anything to hand over at all
+ def present?
+ redirect_url.present? || inline_value.present?
+ end
+ end
+end
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index ae0b20334e..7a9c4a9950 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -233,6 +233,7 @@ en:
messages:
blank: can't be blank
must_apply_to_products: must be a custom field definition for products
+ not_registered: is not a registered provider
models:
spree/allowed_origin:
attributes:
diff --git a/spree/core/db/migrate/20260812000001_add_provider_type_to_spree_digital_assets.rb b/spree/core/db/migrate/20260812000001_add_provider_type_to_spree_digital_assets.rb
new file mode 100644
index 0000000000..75e74770d6
--- /dev/null
+++ b/spree/core/db/migrate/20260812000001_add_provider_type_to_spree_digital_assets.rb
@@ -0,0 +1,7 @@
+class AddProviderTypeToSpreeDigitalAssets < ActiveRecord::Migration[8.1]
+ def change
+ # Nullable: a blank provider_type means the File provider (an uploaded
+ # file), so every existing row keeps its behaviour with no backfill.
+ add_column :spree_digital_assets, :provider_type, :string
+ end
+end
diff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rb
index 1ec73914b7..489dc74c86 100644
--- a/spree/core/lib/spree/core.rb
+++ b/spree/core/lib/spree/core.rb
@@ -462,6 +462,19 @@ module Spree
Rails.application.config.spree.delivery_rate_providers = value
end
+ # Strategies selectable as a digital asset's source. Core registers the
+ # uploaded-file default; host apps append providers that resolve a
+ # deliverable elsewhere (a licensing system, a code pool).
+ #
+ # @return [Array<Class>]
+ def self.digital_asset_providers
+ Rails.application.config.spree.digital_asset_providers
+ end
+
+ def self.digital_asset_providers=(value)
+ Rails.application.config.spree.digital_asset_providers = value
+ end
+
# Fulfillment profile kinds selectable when creating a profile.
#
# @return [Array<Class>]
diff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rb
index 6e2cfe89b7..330a21481b 100644
--- a/spree/core/lib/spree/core/engine.rb
+++ b/spree/core/lib/spree/core/engine.rb
@@ -23,6 +23,7 @@ module Spree
:delivery_method_rules,
:seller_requirements,
:delivery_rate_providers,
+ :digital_asset_providers,
:delivery_profile_types,
:order_routing,
:promotions,
@@ -168,6 +169,10 @@ module Spree
app.config.spree.inventory_providers = []
end
+ initializer 'spree.register.digital_asset_providers', before: :load_config_initializers do |app|
+ app.config.spree.digital_asset_providers = []
+ end
+
initializer 'spree.register.delivery_profile_types', before: :load_config_initializers do |app|
app.config.spree.delivery_profile_types = []
end
@@ -308,6 +313,12 @@ module Spree
Spree::DeliveryRateProvider::Internal
]
+ # Digital asset sources. Core ships the uploaded-file default; host
+ # apps append providers that resolve a deliverable elsewhere.
+ Rails.application.config.spree.digital_asset_providers.concat [
+ Spree::DigitalAssetProvider::File
+ ]
+
# Profile kinds selectable when creating a delivery profile;
# extension kinds append theirs.
Rails.application.config.spree.delivery_profile_types.concat [
diff --git a/spree/core/spec/models/spree/digital_asset_provider/file_spec.rb b/spree/core/spec/models/spree/digital_asset_provider/file_spec.rb
new file mode 100644
index 0000000000..5e17bd3359
--- /dev/null
+++ b/spree/core/spec/models/spree/digital_asset_provider/file_spec.rb
@@ -0,0 +1,57 @@
+require 'spec_helper'
+
+RSpec.describe Spree::DigitalAssetProvider::File do
+ let(:digital_asset) { create(:digital_asset) }
+ let(:digital_link) { create(:digital_link, digital_asset: digital_asset) }
+
+ subject(:provider) { described_class.new(digital_asset) }
+
+ # The Disk service needs a host to sign a URL; a real request supplies it via
+ # ActiveStorage::SetCurrent. Outside a request the spec sets it directly.
+ around do |example|
+ ActiveStorage::Current.url_options = { host: 'http://test.host' }
+ example.run
+ ensure
+ ActiveStorage::Current.url_options = nil
+ end
+
+ it 'is registered as a selectable provider' do
+ expect(Spree.digital_asset_providers).to include(described_class)
+ end
+
+ it 'requires an attachment' do
+ expect(described_class.requires_attachment?).to be(true)
+ end
+
+ describe '#deliver' do
+ it 'returns a redirect to a signed storage URL' do
+ delivery = provider.deliver(digital_link, expires_in: 5.minutes)
+
+ expect(delivery).to be_redirect
+ expect(delivery.redirect_url).to be_present
+ end
+
+ # File is a refactor of DigitalAsset#download_url; the signed URLs are not
+ # byte-equal (the expiry timestamp is stamped at call time), so decode the
+ # signed disk token from each and compare the blob key + disposition.
+ def decoded_disk_payload(url)
+ token = url[%r{/disk/([^/]+)/}, 1]
+ ActiveStorage.verifier.verified(token, purpose: :blob_key)
+ end
+
+ it 'reproduces the asset download URL (no behaviour change)' do
+ via_provider = decoded_disk_payload(provider.deliver(digital_link, expires_in: 5.minutes).redirect_url)
+ via_model = decoded_disk_payload(digital_asset.download_url(expires_in: 5.minutes))
+
+ expect(via_provider['key']).to eq(via_model['key'])
+ expect(via_provider['disposition']).to eq(via_model['disposition'])
+ expect(via_provider['disposition']).to include('attachment')
+ end
+
+ it 'returns nothing when no file is attached' do
+ digital_asset.attachment.purge
+
+ expect(provider.deliver(digital_link, expires_in: 5.minutes)).to be_nil
+ end
+ end
+end
diff --git a/spree/core/spec/models/spree/digital_asset_spec.rb b/spree/core/spec/models/spree/digital_asset_spec.rb
index 91d1b1a57f..72e20d9243 100644
--- a/spree/core/spec/models/spree/digital_asset_spec.rb
+++ b/spree/core/spec/models/spree/digital_asset_spec.rb
@@ -20,6 +20,48 @@ describe Spree::DigitalAsset, type: :model do
expect(described_class.new(variant: variant, attachment: file_upload)).to be_valid
end
+ describe 'providers' do
+ # A stand-in provider that resolves its deliverable elsewhere, so it needs
+ # no uploaded file. Registered for the duration of the examples.
+ let(:stub_provider) do
+ Class.new(Spree::DigitalAssetProvider::Base) do
+ def self.requires_attachment? = false
+ def deliver(_link, expires_in:) = Spree::DigitalDelivery.new(inline_value: 'STUB')
+ end
+ end
+
+ before do
+ stub_const('Spree::DigitalAssetProvider::Stub', stub_provider)
+ Spree.digital_asset_providers << stub_provider
+ end
+
+ after { Spree.digital_asset_providers.delete(stub_provider) }
+
+ it 'resolves a blank provider_type to the File default' do
+ expect(described_class.new.provider_class).to eq(Spree::DigitalAssetProvider::File)
+ end
+
+ it 'resolves a set provider_type to that class' do
+ asset = described_class.new(provider_type: 'Spree::DigitalAssetProvider::Stub')
+ expect(asset.provider_class).to eq(stub_provider)
+ end
+
+ it 'requires no attachment for a provider that declares so' do
+ asset = described_class.new(variant: variant, provider_type: 'Spree::DigitalAssetProvider::Stub')
+ expect(asset).to be_valid
+ end
+
+ it 'still requires an attachment for a File asset' do
+ expect(described_class.new(variant: variant)).not_to be_valid
+ end
+
+ it 'rejects an unregistered provider_type' do
+ asset = described_class.new(variant: variant, provider_type: 'Spree::DigitalAssetProvider::Gone')
+ expect(asset).not_to be_valid
+ expect(asset.errors[:provider_type]).to be_present
+ end
+ end
+
# The legacy names are the one-release webhook bridge; dropping them silently
# would break every subscriber written before the rename.
describe 'legacy digital.* events', events: true do
diff --git a/spree/core/spec/models/spree/digital_delivery_spec.rb b/spree/core/spec/models/spree/digital_delivery_spec.rb
new file mode 100644
index 0000000000..1082639599
--- /dev/null
+++ b/spree/core/spec/models/spree/digital_delivery_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+RSpec.describe Spree::DigitalDelivery do
+ it 'is a redirect when it carries a url' do
+ delivery = described_class.new(redirect_url: 'https://example.com/file')
+
+ expect(delivery).to be_redirect
+ expect(delivery).to be_present
+ end
+
+ it 'is not a redirect when it carries an inline value' do
+ delivery = described_class.new(inline_value: 'KEY-123', content_type: 'text/plain')
+
+ expect(delivery).not_to be_redirect
+ expect(delivery).to be_present
+ end
+
+ it 'is absent when it carries nothing' do
+ expect(described_class.new).not_to be_present
+ end
+end