diff --git a/app/decorators/models/solidus_easypost/spree/shipment_decorator.rb b/app/decorators/models/solidus_easypost/spree/shipment_decorator.rb index 5a1fb50..4ab763e 100644 --- a/app/decorators/models/solidus_easypost/spree/shipment_decorator.rb +++ b/app/decorators/models/solidus_easypost/spree/shipment_decorator.rb @@ -29,8 +29,41 @@ def easypost_postage_label_url easypost_shipment&.postage_label&.label_url end + def select_shipping_method(shipping_method) + # Selects the specified shipping method for the shipment. + # This method fetches available shipping rates using the estimator and + # marks the selected shipping rate while deselecting all others. + estimator = ::Spree::Config.stock.estimator_class.new + rates = estimator.shipping_rates(to_package, false) + + # Find the rate that matches the provided shipping method + rate = rates.detect { |detected| detected.shipping_method_id == shipping_method.id } + + # Mark the selected shipping method as true and deselect others + deselect_other_shipping_rates(rate.id) + rate.update(selected: true) + end + + def update_amounts + # NOTE: Reload the shipment to reflect any updates to the selected shipping rate. + # Updates the shipment amounts by reloading the selected shipping rate. + # This ensures that the shipment cost is recalculated based on the latest + # selected shipping rate. + reload + + # Call the parent method from solidus_core to ensure proper cost recalculations. + super + end + private + def deselect_other_shipping_rates(selected_rate_id) + # Deselects all shipping rates except the one selected by the user. + # This ensures that only one shipping rate remains marked as selected. + # Update all other shipping rates, setting `selected` to false. + shipping_rates.where.not(id: selected_rate_id).update_all(selected: false) + end + def buy_easypost_rate # Skip label purchase if tracking information already exists. return if tracking diff --git a/lib/solidus_easypost/estimator.rb b/lib/solidus_easypost/estimator.rb index 63bde44..2772a57 100644 --- a/lib/solidus_easypost/estimator.rb +++ b/lib/solidus_easypost/estimator.rb @@ -3,10 +3,22 @@ module SolidusEasypost class Estimator def shipping_rates(package, _frontend_only = true) - easypost_rates = ShipmentBuilder.from_package(package).rates.sort_by(&:rate) - - shipping_rates = easypost_rates.map { |rate| build_shipping_rate(rate) }.compact - shipping_rates.min_by(&:cost)&.selected = true + # NOTE: The easypost_shipments were being created again when retrieving shipping_rates + # Hence this method has been updated to check if an EasyPost shipment has already been created for the shipment. + # If it exists, it fetches the stored shipping rates instead of creating a new EasyPost shipment. + shipment = package.shipment + + if shipment.easypost_shipment + shipping_rates = shipment.shipping_rates + else + easypost_rates = ShipmentBuilder.from_package(package).rates.sort_by(&:rate) + + # Build shipping rate objects from EasyPost rates. + shipping_rates = easypost_rates.map { |rate| build_shipping_rate(rate) }.compact + + # Automatically select the shipping rate with the lowest cost. + shipping_rates.min_by(&:cost)&.selected = true + end shipping_rates end diff --git a/spec/cassettes/shipment/update_selected_shipping_rates.yml b/spec/cassettes/shipment/update_selected_shipping_rates.yml new file mode 100644 index 0000000..3691a99 --- /dev/null +++ b/spec/cassettes/shipment/update_selected_shipping_rates.yml @@ -0,0 +1,352 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.easypost.com/v2/addresses + body: + encoding: UTF-8 + string: '{"street1":"215 N 7th Ave","street2":"Northwest","city":"Manville","zip":"08835","phone":"555-555-0199","company":"Company","name":"John + Von Doe","state":"AL","country":"US"}' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - EasyPost/v2 RubyClient/6.4.1 Ruby/3.1.2-p20 OS/Linux OSVersion/ OSArch/x86_64 + Host: + - api.easypost.com + Content-Type: + - application/json + Authorization: + - Bearer CvzYtuda6KRI9JjG7SAHbA + response: + status: + code: 201 + message: Created + headers: + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + X-Ep-Request-Uuid: + - f40abc8067c5d5abea87a60b0043f929 + Cache-Control: + - private, no-cache, no-store + Pragma: + - no-cache + Expires: + - '0' + Location: + - "/api/v2/addresses/adr_bf4fc256f84a11ef9861ac1f6bc539ae" + Content-Type: + - application/json; charset=utf-8 + X-Runtime: + - '0.036644' + Transfer-Encoding: + - chunked + X-Node: + - bigweb36nuq + X-Version-Label: + - easypost-202503031310-f5931d0281-master + X-Backend: + - easypost + X-Proxied: + - extlb2nuq 99aac35317 + - intlb3nuq 51d74985a2 + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + body: + encoding: ASCII-8BIT + string: '{"id":"adr_bf4fc256f84a11ef9861ac1f6bc539ae","object":"Address","created_at":"2025-03-03T16:15:39+00:00","updated_at":"2025-03-03T16:15:39+00:00","name":"John + Von Doe","company":"Company","street1":"215 N 7th Ave","street2":"Northwest","city":"Manville","state":"AL","zip":"08835","country":"US","phone":"5555550199","email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}}' + recorded_at: Mon, 03 Mar 2025 16:15:40 GMT +- request: + method: post + uri: https://api.easypost.com/v2/addresses + body: + encoding: UTF-8 + string: '{"street1":"131 S 8th Ave","city":"Manville","zip":"08835","phone":"(202) + 456-1111","name":"NY Warehouse","company":"NY Warehouse","state":"NJ","country":"US"}' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - EasyPost/v2 RubyClient/6.4.1 Ruby/3.1.2-p20 OS/Linux OSVersion/ OSArch/x86_64 + Host: + - api.easypost.com + Content-Type: + - application/json + Authorization: + - Bearer CvzYtuda6KRI9JjG7SAHbA + response: + status: + code: 201 + message: Created + headers: + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + X-Ep-Request-Uuid: + - f40abc7967c5d5adea882e6b0043fc6b + Cache-Control: + - private, no-cache, no-store + Pragma: + - no-cache + Expires: + - '0' + Location: + - "/api/v2/addresses/adr_c0bdb83ef84a11ef9e8d3cecef1b359e" + Content-Type: + - application/json; charset=utf-8 + X-Runtime: + - '0.038604' + Transfer-Encoding: + - chunked + X-Node: + - bigweb43nuq + X-Version-Label: + - easypost-202503031310-f5931d0281-master + X-Backend: + - easypost + X-Canary: + - direct + X-Proxied: + - extlb2nuq 99aac35317 + - intlb4nuq 51d74985a2 + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + body: + encoding: ASCII-8BIT + string: '{"id":"adr_c0bdb83ef84a11ef9e8d3cecef1b359e","object":"Address","created_at":"2025-03-03T16:15:41+00:00","updated_at":"2025-03-03T16:15:41+00:00","name":"NY + Warehouse","company":"NY Warehouse","street1":"131 S 8th Ave","street2":null,"city":"Manville","state":"NJ","zip":"08835","country":"US","phone":"2024561111","email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}}' + recorded_at: Mon, 03 Mar 2025 16:15:41 GMT +- request: + method: post + uri: https://api.easypost.com/v2/parcels + body: + encoding: UTF-8 + string: '{"parcel":{"weight":"10.0"}}' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - EasyPost/v2 RubyClient/6.4.1 Ruby/3.1.2-p20 OS/Linux OSVersion/ OSArch/x86_64 + Host: + - api.easypost.com + Content-Type: + - application/json + Authorization: + - Bearer CvzYtuda6KRI9JjG7SAHbA + response: + status: + code: 201 + message: Created + headers: + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + X-Ep-Request-Uuid: + - f40abc7d67c5d5aeea882e880043fdbf + Cache-Control: + - private, no-cache, no-store + Pragma: + - no-cache + Expires: + - '0' + Location: + - "/api/v2/parcels/prcl_a874800bf2144e68bf5cab766b7823e8" + Content-Type: + - application/json; charset=utf-8 + X-Runtime: + - '0.028326' + Transfer-Encoding: + - chunked + X-Node: + - bigweb36nuq + X-Version-Label: + - easypost-202503031310-f5931d0281-master + X-Backend: + - easypost + X-Proxied: + - extlb2nuq 99aac35317 + - intlb4nuq 51d74985a2 + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + body: + encoding: ASCII-8BIT + string: '{"id":"prcl_a874800bf2144e68bf5cab766b7823e8","object":"Parcel","created_at":"2025-03-03T16:15:42Z","updated_at":"2025-03-03T16:15:42Z","length":null,"width":null,"height":null,"predefined_package":null,"weight":10.0,"mode":"test"}' + recorded_at: Mon, 03 Mar 2025 16:15:42 GMT +- request: + method: post + uri: https://api.easypost.com/v2/shipments + body: + encoding: UTF-8 + string: '{"shipment":{"to_address":{"id":"adr_bf4fc256f84a11ef9861ac1f6bc539ae"},"from_address":{"id":"adr_c0bdb83ef84a11ef9e8d3cecef1b359e"},"parcel":{"id":"prcl_a874800bf2144e68bf5cab766b7823e8"},"options":{}}}' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - EasyPost/v2 RubyClient/6.4.1 Ruby/3.1.2-p20 OS/Linux OSVersion/ OSArch/x86_64 + Host: + - api.easypost.com + Content-Type: + - application/json + Authorization: + - Bearer CvzYtuda6KRI9JjG7SAHbA + response: + status: + code: 201 + message: Created + headers: + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + X-Ep-Request-Uuid: + - f40abc7f67c5d5afea882ea70043ff03 + Cache-Control: + - private, no-cache, no-store + Pragma: + - no-cache + Expires: + - '0' + Location: + - "/api/v2/shipments/shp_ff3d7c35f1b0401a8d375628a163ed7a" + Content-Type: + - application/json; charset=utf-8 + X-Runtime: + - '0.180714' + Transfer-Encoding: + - chunked + X-Node: + - bigweb34nuq + X-Version-Label: + - easypost-202503031310-f5931d0281-master + X-Backend: + - easypost + X-Proxied: + - extlb2nuq 99aac35317 + - intlb3nuq 51d74985a2 + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + body: + encoding: ASCII-8BIT + string: '{"id":"shp_ff3d7c35f1b0401a8d375628a163ed7a","created_at":"2025-03-03T16:15:43Z","is_return":false,"messages":[],"mode":"test","options":{"currency":"USD","payment":{"type":"SENDER"},"date_advance":0},"reference":null,"status":"unknown","tracking_code":null,"updated_at":"2025-03-03T16:15:43Z","batch_id":null,"batch_status":null,"batch_message":null,"customs_info":null,"from_address":{"id":"adr_c0bdb83ef84a11ef9e8d3cecef1b359e","object":"Address","created_at":"2025-03-03T16:15:41+00:00","updated_at":"2025-03-03T16:15:41+00:00","name":"NY + Warehouse","company":"NY Warehouse","street1":"131 S 8th Ave","street2":null,"city":"Manville","state":"NJ","zip":"08835","country":"US","phone":"2024561111","email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"insurance":null,"order_id":null,"parcel":{"id":"prcl_a874800bf2144e68bf5cab766b7823e8","object":"Parcel","created_at":"2025-03-03T16:15:42Z","updated_at":"2025-03-03T16:15:42Z","length":null,"width":null,"height":null,"predefined_package":null,"weight":10.0,"mode":"test"},"postage_label":null,"rates":[{"id":"rate_a67cf31e1a4e411da79f5fa213213020","object":"Rate","created_at":"2025-03-03T16:15:43Z","updated_at":"2025-03-03T16:15:43Z","mode":"test","service":"Priority","carrier":"USPS","rate":"6.54","currency":"USD","retail_rate":"9.35","retail_currency":"USD","list_rate":"7.99","list_currency":"USD","billing_type":"easypost","delivery_days":2,"delivery_date":null,"delivery_date_guaranteed":false,"est_delivery_days":2,"shipment_id":"shp_ff3d7c35f1b0401a8d375628a163ed7a","carrier_account_id":"ca_mtb51Ve0"},{"id":"rate_8ac8570fc7444565aced44a18214059c","object":"Rate","created_at":"2025-03-03T16:15:43Z","updated_at":"2025-03-03T16:15:43Z","mode":"test","service":"GroundAdvantage","carrier":"USPS","rate":"5.00","currency":"USD","retail_rate":"6.75","retail_currency":"USD","list_rate":"5.00","list_currency":"USD","billing_type":"easypost","delivery_days":2,"delivery_date":null,"delivery_date_guaranteed":false,"est_delivery_days":2,"shipment_id":"shp_ff3d7c35f1b0401a8d375628a163ed7a","carrier_account_id":"ca_mtb51Ve0"},{"id":"rate_d388cb63b3504f4e9da22c4e93ad8dfa","object":"Rate","created_at":"2025-03-03T16:15:43Z","updated_at":"2025-03-03T16:15:43Z","mode":"test","service":"Express","carrier":"USPS","rate":"27.60","currency":"USD","retail_rate":"31.95","retail_currency":"USD","list_rate":"27.60","list_currency":"USD","billing_type":"easypost","delivery_days":1,"delivery_date":null,"delivery_date_guaranteed":false,"est_delivery_days":1,"shipment_id":"shp_ff3d7c35f1b0401a8d375628a163ed7a","carrier_account_id":"ca_mtb51Ve0"}],"refund_status":null,"scan_form":null,"selected_rate":null,"tracker":null,"to_address":{"id":"adr_bf4fc256f84a11ef9861ac1f6bc539ae","object":"Address","created_at":"2025-03-03T16:15:39+00:00","updated_at":"2025-03-03T16:15:39+00:00","name":"John + Von Doe","company":"Company","street1":"215 N 7th Ave","street2":"Northwest","city":"Manville","state":"AL","zip":"08835","country":"US","phone":"5555550199","email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"usps_zone":1,"return_address":{"id":"adr_c0bdb83ef84a11ef9e8d3cecef1b359e","object":"Address","created_at":"2025-03-03T16:15:41+00:00","updated_at":"2025-03-03T16:15:41+00:00","name":"NY + Warehouse","company":"NY Warehouse","street1":"131 S 8th Ave","street2":null,"city":"Manville","state":"NJ","zip":"08835","country":"US","phone":"2024561111","email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"buyer_address":{"id":"adr_bf4fc256f84a11ef9861ac1f6bc539ae","object":"Address","created_at":"2025-03-03T16:15:39+00:00","updated_at":"2025-03-03T16:15:39+00:00","name":"John + Von Doe","company":"Company","street1":"215 N 7th Ave","street2":"Northwest","city":"Manville","state":"AL","zip":"08835","country":"US","phone":"5555550199","email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"forms":[],"fees":[],"object":"Shipment"}' + recorded_at: Mon, 03 Mar 2025 16:15:44 GMT +- request: + method: get + uri: https://api.easypost.com/v2/shipments/shp_ff3d7c35f1b0401a8d375628a163ed7a + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - EasyPost/v2 RubyClient/6.4.1 Ruby/3.1.2-p20 OS/Linux OSVersion/ OSArch/x86_64 + Host: + - api.easypost.com + Content-Type: + - application/json + Authorization: + - Bearer CvzYtuda6KRI9JjG7SAHbA + response: + status: + code: 200 + message: OK + headers: + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - strict-origin-when-cross-origin + X-Ep-Request-Uuid: + - f40abc7e67c5d5b1ea882ecc0044017f + Cache-Control: + - private, no-cache, no-store + Pragma: + - no-cache + Expires: + - '0' + Content-Type: + - application/json; charset=utf-8 + X-Runtime: + - '0.069766' + Transfer-Encoding: + - chunked + X-Node: + - bigweb32nuq + X-Version-Label: + - easypost-202503031310-f5931d0281-master + X-Backend: + - easypost + X-Canary: + - direct + X-Proxied: + - extlb2nuq 99aac35317 + - intlb4nuq 51d74985a2 + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + body: + encoding: ASCII-8BIT + string: '{"id":"shp_ff3d7c35f1b0401a8d375628a163ed7a","created_at":"2025-03-03T16:15:43Z","is_return":false,"messages":[],"mode":"test","options":{"currency":"USD","payment":{"type":"SENDER"},"date_advance":0},"reference":null,"status":"unknown","tracking_code":null,"updated_at":"2025-03-03T16:15:43Z","batch_id":null,"batch_status":null,"batch_message":null,"customs_info":null,"from_address":{"id":"adr_c0bdb83ef84a11ef9e8d3cecef1b359e","object":"Address","created_at":"2025-03-03T16:15:41+00:00","updated_at":"2025-03-03T16:15:41+00:00","name":"NY + Warehouse","company":"NY Warehouse","street1":"131 S 8th Ave","street2":null,"city":"Manville","state":"NJ","zip":"08835","country":"US","phone":"2024561111","email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"insurance":null,"order_id":null,"parcel":{"id":"prcl_a874800bf2144e68bf5cab766b7823e8","object":"Parcel","created_at":"2025-03-03T16:15:42Z","updated_at":"2025-03-03T16:15:42Z","length":null,"width":null,"height":null,"predefined_package":null,"weight":10.0,"mode":"test"},"postage_label":null,"rates":[{"id":"rate_a67cf31e1a4e411da79f5fa213213020","object":"Rate","created_at":"2025-03-03T16:15:43Z","updated_at":"2025-03-03T16:15:43Z","mode":"test","service":"Priority","carrier":"USPS","rate":"6.54","currency":"USD","retail_rate":"9.35","retail_currency":"USD","list_rate":"7.99","list_currency":"USD","billing_type":"easypost","delivery_days":2,"delivery_date":null,"delivery_date_guaranteed":false,"est_delivery_days":2,"shipment_id":"shp_ff3d7c35f1b0401a8d375628a163ed7a","carrier_account_id":"ca_mtb51Ve0"},{"id":"rate_8ac8570fc7444565aced44a18214059c","object":"Rate","created_at":"2025-03-03T16:15:43Z","updated_at":"2025-03-03T16:15:43Z","mode":"test","service":"GroundAdvantage","carrier":"USPS","rate":"5.00","currency":"USD","retail_rate":"6.75","retail_currency":"USD","list_rate":"5.00","list_currency":"USD","billing_type":"easypost","delivery_days":2,"delivery_date":null,"delivery_date_guaranteed":false,"est_delivery_days":2,"shipment_id":"shp_ff3d7c35f1b0401a8d375628a163ed7a","carrier_account_id":"ca_mtb51Ve0"},{"id":"rate_d388cb63b3504f4e9da22c4e93ad8dfa","object":"Rate","created_at":"2025-03-03T16:15:43Z","updated_at":"2025-03-03T16:15:43Z","mode":"test","service":"Express","carrier":"USPS","rate":"27.60","currency":"USD","retail_rate":"31.95","retail_currency":"USD","list_rate":"27.60","list_currency":"USD","billing_type":"easypost","delivery_days":1,"delivery_date":null,"delivery_date_guaranteed":false,"est_delivery_days":1,"shipment_id":"shp_ff3d7c35f1b0401a8d375628a163ed7a","carrier_account_id":"ca_mtb51Ve0"}],"refund_status":null,"scan_form":null,"selected_rate":null,"tracker":null,"to_address":{"id":"adr_bf4fc256f84a11ef9861ac1f6bc539ae","object":"Address","created_at":"2025-03-03T16:15:39+00:00","updated_at":"2025-03-03T16:15:39+00:00","name":"John + Von Doe","company":"Company","street1":"215 N 7th Ave","street2":"Northwest","city":"Manville","state":"AL","zip":"08835","country":"US","phone":"5555550199","email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"usps_zone":1,"return_address":{"id":"adr_c0bdb83ef84a11ef9e8d3cecef1b359e","object":"Address","created_at":"2025-03-03T16:15:41+00:00","updated_at":"2025-03-03T16:15:41+00:00","name":"NY + Warehouse","company":"NY Warehouse","street1":"131 S 8th Ave","street2":null,"city":"Manville","state":"NJ","zip":"08835","country":"US","phone":"2024561111","email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"buyer_address":{"id":"adr_bf4fc256f84a11ef9861ac1f6bc539ae","object":"Address","created_at":"2025-03-03T16:15:39+00:00","updated_at":"2025-03-03T16:15:39+00:00","name":"John + Von Doe","company":"Company","street1":"215 N 7th Ave","street2":"Northwest","city":"Manville","state":"AL","zip":"08835","country":"US","phone":"5555550199","email":null,"mode":"test","carrier_facility":null,"residential":null,"federal_tax_id":null,"state_tax_id":null,"verifications":{}},"forms":[],"fees":[],"object":"Shipment"}' + recorded_at: Mon, 03 Mar 2025 16:15:45 GMT +recorded_with: VCR 6.3.1 diff --git a/spec/models/spree/shipment_spec.rb b/spec/models/spree/shipment_spec.rb index f5bb446..6fd709f 100644 --- a/spec/models/spree/shipment_spec.rb +++ b/spec/models/spree/shipment_spec.rb @@ -36,6 +36,20 @@ expect(shipment.shipping_rates.where(selected: true).count).to eq(1) end end + + it 'updates the selected rate when updated by user' do + VCR.use_cassette('shipment/update_selected_shipping_rates') do + easypost_config_setup(purchase_labels: true) + expect(shipment.selected_shipping_rate.cost).to eq(shipment.shipping_rates.minimum(:cost)) + # Fetch and select shipping method with maximum shipping rate cost. + shipping_rate = shipment.shipping_rates.order(cost: :asc).last + shipping_method = shipping_rate.shipping_method + shipment.select_shipping_method(shipping_method) + order.recalculate + shipment.reload + expect(shipment.cost).to eq(shipping_rate.cost) + end + end end context 'when purchase_labels is false' do