Note that in the real world, this avoids a crash, but still requires the user to click the button two more times before it will work, with no hints as to why. So not a great help.
It didn't catch the error I was looking for. I'm not sure if it is a valid use case, but it still seems helpful to add coverage for current functionality.
This will delay the checkout request by a few seconds if there's stock
to sync. But we minimise the chance of missing reduced stock from orders
on another platform.
We still have a gap between the checkout and placing a backorder. In
that time we can't guarantee enough stock. But let's tackle that after
the pilot.
Most of the time this doesn't get called because source_required: false.
But sometimes it [does happen](https://app.bugsnag.com/yaycode/openfoodnetwork-uk/errors/66329690f4b6380007e8a4f8)
I have a feeling that source_required? could be moved to the superclass as payment_source_class.present?. But I don't know enough about this area of the system to try it...
We can see on the respective controller spec, that having a Stripe SCA payment, with no source does not trigger the error 400, observed on the legacy checkout.
The code here runs from a callback which was originally designed to make sure the checkout page was set up correctly in the "normal" checkout workflow. It wasn't really designed to be run when the page is being loaded a second time due to the user being redirected back from Stripe (with SCA). The things it's doing here are necessary in the former case, but a really bad idea in the latter (potentially messing up the order's ship and bill addresses in certain cases like guest checkout).
The test setup left the object with unsaved changes, which doesn't work nicely with the new order locking added recently on the edit action.
Fixes ~5 specs including:
5) CheckoutController redirection to cart and stripe redirects when some items are out of stock
Failure/Error:
order.with_lock do
lock_variants_of(order)
yield
end
RuntimeError:
Locking a record with unpersisted changes is not supported. Use `save` to persist the changes, or `reload` to discard them explicitly.
# ./app/services/current_order_locker.rb:22:in `lock_order_and_variants'
# ./app/services/current_order_locker.rb:11:in `around'
# ./spec/controllers/checkout_controller_spec.rb:57:in `block (3 levels) in <top (required)>'