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
Add more test scenarios for checkout endpoints and flow for digital and physical goods.
Matthew Kennedyf735e2236be0
diff --git a/api/app/serializers/spree/v2/storefront/digital_link_serializer.rb b/api/app/serializers/spree/v2/storefront/digital_link_serializer.rbindex 7b75ce975b..1297e2a37b 100644--- a/api/app/serializers/spree/v2/storefront/digital_link_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/digital_link_serializer.rb@@ -4,7 +4,7 @@ module Spreeclass DigitalLinkSerializer < BaseSerializerset_type :digital_link- attributes :token+ attribute :tokenendendenddiff --git a/api/lib/spree/api/testing_support/v2/current_order.rb b/api/lib/spree/api/testing_support/v2/current_order.rbindex cda2721ee4..1122e8d857 100644--- a/api/lib/spree/api/testing_support/v2/current_order.rb+++ b/api/lib/spree/api/testing_support/v2/current_order.rb@@ -3,11 +3,40 @@ def ensure_order_totalsorder.persist_totalsend-shared_context 'creates order with line item' do+shared_context 'creates an order with a phisical line item' dolet!(:line_item) { create(:line_item, order: order, currency: currency) }let!(:headers) { headers_bearer }- before { ensure_order_totals }+ before do+ order.reload+ ensure_order_totals+ end+end++shared_context 'creates an order with a digital line item' do+ let!(:digital) { create(:digital) }+ let!(:variant_digital) { digital.variant }+ let!(:line_item) { create(:line_item, variant: variant_digital, order: order, currency: currency) }+ let!(:headers) { headers_bearer }++ before do+ order.reload+ ensure_order_totals+ end+end++shared_context 'creates an order with a phisical and digital line item' do+ let!(:digital) { create(:digital) }+ let!(:variant_digital) { digital.variant }+ let!(:digital_line_item) { create(:line_item, variant: variant_digital, order: order, currency: currency) }+ let!(:phisical_line_item) { create(:line_item, order: order, currency: currency) }++ let!(:headers) { headers_bearer }++ before do+ order.reload+ ensure_order_totals+ endendshared_context 'creates guest order with guest token' dodiff --git a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rbindex af9451a019..ac0869bb16 100644--- a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb@@ -103,7 +103,7 @@ describe 'API V2 Storefront Checkout Spec', type: :request doendcontext 'as a signed in user' do- include_context 'creates order with line item'+ include_context 'creates an order with a phisical line item'it_behaves_like 'perform next'end@@ -154,7 +154,7 @@ describe 'API V2 Storefront Checkout Spec', type: :request doendcontext 'as a signed in user' do- include_context 'creates order with line item'+ include_context 'creates an order with a phisical line item'it_behaves_like 'perform advance'end@@ -210,236 +210,242 @@ describe 'API V2 Storefront Checkout Spec', type: :request doendcontext 'as a signed in user' do- include_context 'creates order with line item'+ include_context 'creates an order with a phisical line item'it_behaves_like 'perform complete'endend- describe 'checkout#update' do- let!(:country_zone) { create(:zone, name: 'CountryZone') }- let!(:state) { create(:state) }- let!(:country) { state.country }- let!(:stock_location) { create(:stock_location) }+ order_contexts = ['creates an order with a phisical line item',+ 'creates an order with a digital line item',+ 'creates an order with a phisical and digital line item'].freeze- let!(:shipping_method) { create(:shipping_method, zones: [country_zone]) }- let!(:payment_method) { create(:credit_card_payment_method, stores: [store]) }+ order_contexts.each do |order_context|+ describe "checkout#update (#{order_context})" do+ let!(:country_zone) { create(:zone, name: 'CountryZone') }+ let!(:state) { create(:state) }+ let!(:country) { state.country }+ let!(:stock_location) { create(:stock_location) }- let(:execute) { patch '/api/v2/storefront/checkout', params: params, headers: headers }+ let!(:shipping_method) { create(:shipping_method, zones: [country_zone]) }+ let!(:payment_method) { create(:credit_card_payment_method, stores: [store]) }- include_context 'creates order with line item'+ let(:execute) { patch '/api/v2/storefront/checkout', params: params, headers: headers }- before do- allow_any_instance_of(Spree::PaymentMethod).to receive(:source_required?).and_return(false)- allow_any_instance_of(Spree::Order).to receive_messages(confirmation_required?: true)- allow_any_instance_of(Spree::Order).to receive_messages(payment_required?: true)- end-- shared_examples 'perform update' do- context 'addresses' do- let(:params) do- {- order: {- bill_address_attributes: address,- ship_address_attributes: address- }- }- end-- before { execute }-- it_behaves_like 'returns 200 HTTP status'- it_behaves_like 'returns valid cart JSON'+ include_context order_context- it 'updates addresses' do- order.reload- expect(order.bill_address).not_to be_nil- expect(order.ship_address).not_to be_nil- expect(order.bill_address.firstname).to eq address[:firstname]- expect(order.bill_address.lastname).to eq address[:lastname]- expect(order.bill_address.address1).to eq address[:address1]- expect(order.bill_address.city).to eq address[:city]- expect(order.bill_address.phone).to eq address[:phone]- expect(order.bill_address.zipcode).to eq address[:zipcode]- expect(order.bill_address.state_id).to eq address[:state_id]- expect(order.bill_address.country.iso).to eq address[:country_iso]- end+ before do+ allow_any_instance_of(Spree::PaymentMethod).to receive(:source_required?).and_return(false)+ allow_any_instance_of(Spree::Order).to receive_messages(confirmation_required?: true)+ allow_any_instance_of(Spree::Order).to receive_messages(payment_required?: true)end- context 'shipment' do- let!(:default_selected_shipping_rate_id) { shipment.selected_shipping_rate_id }- let(:new_selected_shipping_rate_id) { Spree::ShippingRate.last.id }- let!(:shipping_method) { shipment.shipping_method }- let!(:second_shipping_method) { create(:shipping_method, name: 'Fedex') }-- let(:params) do- {- order: {- shipments_attributes: [- { selected_shipping_rate_id: new_selected_shipping_rate_id, id: shipment.id }- ]+ shared_examples 'perform update' do+ context 'addresses' do+ let(:params) do+ {+ order: {+ bill_address_attributes: address,+ ship_address_attributes: address+ }}- }- end+ end- before do- shipment- shipment.add_shipping_method(second_shipping_method)- execute- end+ before { execute }- it_behaves_like 'returns 200 HTTP status'- it_behaves_like 'returns valid cart JSON'+ it_behaves_like 'returns 200 HTTP status'+ it_behaves_like 'returns valid cart JSON'- it 'updates shipment' do- shipment.reload- expect(shipment.shipping_rates.count).to eq(2)- expect(shipment.selected_shipping_rate_id).to eq(new_selected_shipping_rate_id)- expect(shipment.selected_shipping_rate_id).not_to eq(default_selected_shipping_rate_id)- expect(shipment.shipping_method).to eq(second_shipping_method)+ it 'updates addresses' do+ order.reload+ expect(order.bill_address).not_to be_nil+ expect(order.ship_address).not_to be_nil+ expect(order.bill_address.firstname).to eq address[:firstname]+ expect(order.bill_address.lastname).to eq address[:lastname]+ expect(order.bill_address.address1).to eq address[:address1]+ expect(order.bill_address.city).to eq address[:city]+ expect(order.bill_address.phone).to eq address[:phone]+ expect(order.bill_address.zipcode).to eq address[:zipcode]+ expect(order.bill_address.state_id).to eq address[:state_id]+ expect(order.bill_address.country.iso).to eq address[:country_iso]+ endend- end- context 'payment' do- context 'payment method' do+ context 'shipment' do+ let!(:default_selected_shipping_rate_id) { shipment.selected_shipping_rate_id }+ let(:new_selected_shipping_rate_id) { Spree::ShippingRate.last.id }+ let!(:shipping_method) { shipment.shipping_method }+ let!(:second_shipping_method) { create(:shipping_method, name: 'Fedex') }+let(:params) do{order: {- payments_attributes: [- {- payment_method_id: payment_method.id- }+ shipments_attributes: [+ { selected_shipping_rate_id: new_selected_shipping_rate_id, id: shipment.id }]}}end- before { execute }+ before do+ shipment+ shipment.add_shipping_method(second_shipping_method)+ execute+ endit_behaves_like 'returns 200 HTTP status'it_behaves_like 'returns valid cart JSON'- it 'updates payment method' do- expect(order.payments).not_to be_empty- expect(order.payments.first.payment_method_id).to eq payment_method.id+ it 'updates shipment' do+ shipment.reload+ expect(shipment.shipping_rates.count).to eq(2)+ expect(shipment.selected_shipping_rate_id).to eq(new_selected_shipping_rate_id)+ expect(shipment.selected_shipping_rate_id).not_to eq(default_selected_shipping_rate_id)+ expect(shipment.shipping_method).to eq(second_shipping_method)endend- context 'payment source' do- let(:params) { payment_params }+ context 'payment' do+ context 'payment method' do+ let(:params) do+ {+ order: {+ payments_attributes: [+ {+ payment_method_id: payment_method.id+ }+ ]+ }+ }+ end- before { execute }+ before { execute }- it_behaves_like 'returns 200 HTTP status'- it_behaves_like 'returns valid cart JSON'+ it_behaves_like 'returns 200 HTTP status'+ it_behaves_like 'returns valid cart JSON'- it 'updates payment method with source' do- expect(order.payments).not_to be_empty- expect(order.payments.last.source.name).to eq('Spree Commerce')- expect(order.payments.last.source.last_digits).to eq('1111')+ it 'updates payment method' do+ expect(order.payments).not_to be_empty+ expect(order.payments.first.payment_method_id).to eq payment_method.id+ endend- end- context 'when the gateway rejects the payment source' do- let(:params) { payment_params }+ context 'payment source' do+ let(:params) { payment_params }- before do- allow_any_instance_of(Spree::Order).to receive(:update_from_params).and_raise(Spree::Core::GatewayError.new('Card declined'))- execute+ before { execute }++ it_behaves_like 'returns 200 HTTP status'+ it_behaves_like 'returns valid cart JSON'++ it 'updates payment method with source' do+ expect(order.payments).not_to be_empty+ expect(order.payments.last.source.name).to eq('Spree Commerce')+ expect(order.payments.last.source.last_digits).to eq('1111')+ endend- it_behaves_like 'returns 422 HTTP status'+ context 'when the gateway rejects the payment source' do+ let(:params) { payment_params }++ before do+ allow_any_instance_of(Spree::Order).to receive(:update_from_params).and_raise(Spree::Core::GatewayError.new('Card declined'))+ execute+ end++ it_behaves_like 'returns 422 HTTP status'+ endend- end- context 'special instructions' do- let(:params) do- {- order: {- special_instructions: "Don't drop it"+ context 'special instructions' do+ let(:params) do+ {+ order: {+ special_instructions: "Don't drop it"+ }}- }- end+ end- before { execute }+ before { execute }- it_behaves_like 'returns 200 HTTP status'- it_behaves_like 'returns valid cart JSON'+ it_behaves_like 'returns 200 HTTP status'+ it_behaves_like 'returns valid cart JSON'- it 'updates the special instructions' do- expect(order.reload.special_instructions).to eq("Don't drop it")- end+ it 'updates the special instructions' do+ expect(order.reload.special_instructions).to eq("Don't drop it")+ end- it 'returns updated special instructions' do- expect(json_response['data']).to have_attribute(:special_instructions).with_value("Don't drop it")+ it 'returns updated special instructions' do+ expect(json_response['data']).to have_attribute(:special_instructions).with_value("Don't drop it")+ endend- end- context 'email' do- let(:params) do- {- order: {- email: '[email protected]'+ context 'email' do+ let(:params) do+ {+ order: {+ email: '[email protected]'+ }}- }- end+ end- before { execute }+ before { execute }- it_behaves_like 'returns 200 HTTP status'- it_behaves_like 'returns valid cart JSON'+ it_behaves_like 'returns 200 HTTP status'+ it_behaves_like 'returns valid cart JSON'- it 'updates email' do- expect(order.reload.email).to eq('[email protected]')- end+ it 'updates email' do+ expect(order.reload.email).to eq('[email protected]')+ end- it 'returns updated email' do- expect(json_response['data']).to have_attribute(:email).with_value('[email protected]')+ it 'returns updated email' do+ expect(json_response['data']).to have_attribute(:email).with_value('[email protected]')+ endend- end- context 'with invalid params' do- let(:params) do- {- order: {- email: 'wrong_email'+ context 'with invalid params' do+ let(:params) do+ {+ order: {+ email: 'wrong_email'+ }}- }- end+ end- before do- order.update_column(:state, 'delivery')- execute- end+ before do+ order.update_column(:state, 'delivery')+ execute+ end- it_behaves_like 'returns 422 HTTP status'+ it_behaves_like 'returns 422 HTTP status'- it 'returns an error' do- expect(json_response['error']).to eq('Email is invalid')+ it 'returns an error' do+ expect(json_response['error']).to eq('Email is invalid')+ end++ it 'returns validation errors' do+ expect(json_response['errors']).to eq('email' => ['is invalid'])+ endend- it 'returns validation errors' do- expect(json_response['errors']).to eq('email' => ['is invalid'])+ context 'without order' do+ let(:params) { {} }++ it_behaves_like 'no current order'endend- context 'without order' do- let(:params) { {} }+ context 'as a guest user' do+ include_context 'creates guest order with guest token'- it_behaves_like 'no current order'+ it_behaves_like 'perform update'end- end- context 'as a guest user' do- include_context 'creates guest order with guest token'-- it_behaves_like 'perform update'- end+ context 'as a signed in user' do+ include_context order_context- context 'as a signed in user' do- include_context 'creates order with line item'-- it_behaves_like 'perform update'+ it_behaves_like 'perform update'+ endendend@@ -569,7 +575,7 @@ describe 'API V2 Storefront Checkout Spec', type: :request doendcontext 'as a signed in user' do- include_context 'creates order with line item'+ include_context 'creates an order with a phisical line item'it_behaves_like 'returns a list of available payment methods'end@@ -635,13 +641,104 @@ describe 'API V2 Storefront Checkout Spec', type: :request doendcontext 'as a signed in user' do- include_context 'creates order with line item'+ include_context 'creates an order with a phisical line item'it_behaves_like 'returns a list of shipments with shipping rates'endend- describe 'full checkout flow' do+ orders_requiring_delivery = ['creates an order with a phisical line item', 'creates an order with a phisical and digital line item'].freeze++ orders_requiring_delivery.each do |phisical_goods_context|+ describe "full checkout flow (#{phisical_goods_context})" do+ let!(:country) { create(:country) }+ let(:state) { create(:state, country: country) }+ let!(:shipping_method) do+ create(:shipping_method).tap do |shipping_method|+ shipping_method.zones = [zone]+ end+ end+ let!(:zone) { create(:zone) }+ let!(:zone_member) { create(:zone_member, zone: zone, zoneable: country) }+ let!(:payment_method) { create(:credit_card_payment_method, stores: [store]) }++ let(:customer_params) do+ {+ order: {+ email: '[email protected]',+ bill_address_attributes: address,+ ship_address_attributes: address+ }+ }+ end++ let(:shipment_params) do+ {+ order: {+ shipments_attributes: [+ { selected_shipping_rate_id: shipping_rate_id, id: shipment_id }+ ]+ }+ }+ end++ let(:shipping_rate_id) do+ json_response['data'].first['relationships']['shipping_rates']['data'].first['id']+ end+ let(:shipment_id) { json_response['data'].first['id'] }++ shared_examples 'transitions through checkout from start to finish' do+ before do+ zone.countries << country+ shipping_method.zones = [zone]+ end++ it 'completes checkout' do+ # we need to set customer information (email, billing & shipping address)+ patch '/api/v2/storefront/checkout', params: customer_params, headers: headers+ expect(response.status).to eq(200)++ # getting back shipping rates+ get '/api/v2/storefront/checkout/shipping_rates', headers: headers+ expect(response.status).to eq(200)++ # selecting shipping method+ patch '/api/v2/storefront/checkout', params: shipment_params, headers: headers+ expect(response.status).to eq(200)++ # getting back list of available payment methods+ get '/api/v2/storefront/checkout/payment_methods', headers: headers+ expect(response.status).to eq(200)+ expect(json_response['data'].first['id']).to eq(payment_method.id.to_s)++ # creating a CC for selected payment method+ patch '/api/v2/storefront/checkout', params: payment_params, headers: headers+ expect(response.status).to eq(200)++ # complete the checkout+ patch '/api/v2/storefront/checkout/complete', headers: headers+ expect(response.status).to eq(200)+ expect(order.reload.completed_at).not_to be_nil+ expect(order.state).to eq('complete')+ expect(order.payments.valid.first.payment_method).to eq(payment_method)+ end+ end++ context 'as a guest user' do+ include_context 'creates guest order with guest token'++ it_behaves_like 'transitions through checkout from start to finish'+ end++ context 'as a signed in user' do+ include_context phisical_goods_context++ it_behaves_like 'transitions through checkout from start to finish'+ end+ end+ end++ describe 'full checkout flow digital' dolet!(:country) { create(:country) }let(:state) { create(:state, country: country) }let!(:shipping_method) do@@ -684,19 +781,11 @@ describe 'API V2 Storefront Checkout Spec', type: :request doshipping_method.zones = [zone]end- it 'completes checkout' do+ it 'completes checkout skipping delivery stage' do# we need to set customer information (email, billing & shipping address)patch '/api/v2/storefront/checkout', params: customer_params, headers: headersexpect(response.status).to eq(200)- # getting back shipping rates- get '/api/v2/storefront/checkout/shipping_rates', headers: headers- expect(response.status).to eq(200)-- # selecting shipping method- patch '/api/v2/storefront/checkout', params: shipment_params, headers: headers- expect(response.status).to eq(200)-# getting back list of available payment methodsget '/api/v2/storefront/checkout/payment_methods', headers: headersexpect(response.status).to eq(200)@@ -722,7 +811,7 @@ describe 'API V2 Storefront Checkout Spec', type: :request doendcontext 'as a signed in user' do- include_context 'creates order with line item'+ include_context 'creates an order with a digital line item'it_behaves_like 'transitions through checkout from start to finish'end