Skip to content

Commit

Permalink
fix: Idempotent cart completion
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Sep 22, 2024
1 parent 1215a7c commit c356748
Show file tree
Hide file tree
Showing 11 changed files with 775 additions and 523 deletions.
65 changes: 63 additions & 2 deletions integration-tests/modules/__tests__/cart/store/carts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ProductStatus,
PromotionRuleOperator,
PromotionType,
RuleOperator,
RuleOperator
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import {
Expand Down Expand Up @@ -1900,7 +1900,7 @@ medusaIntegrationTestRunner({
})
})

describe("POST /store/carts/:id/complete", () => {
describe.only("POST /store/carts/:id/complete", () => {
let salesChannel
let product
let shippingProfile
Expand Down Expand Up @@ -2390,6 +2390,67 @@ medusaIntegrationTestRunner({
})
})

it("should return order when cart is already completed", async () => {
const cart = (
await api.post(
`/store/carts`,
{
currency_code: "usd",
email: "tony@stark-industries.com",
shipping_address: {
address_1: "test address 1",
address_2: "test address 2",
city: "ny",
country_code: "us",
province: "ny",
postal_code: "94016",
},
sales_channel_id: salesChannel.id,
items: [{ quantity: 1, variant_id: product.variants[0].id }],
},
storeHeaders
)
).data.cart

const paymentCollection = (
await api.post(
`/store/payment-collections`,
{
cart_id: cart.id,
},
storeHeaders
)
).data.payment_collection

await api.post(
`/store/payment-collections/${paymentCollection.id}/payment-sessions`,
{ provider_id: "pp_system_default" },
storeHeaders
)

await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)

const cartRefetch = (
await api.get(`/store/carts/${cart.id}`, storeHeaders)
).data.cart

expect(cartRefetch.completed_at).toBeTruthy()

const order = await api.post(
`/store/carts/${cart.id}/complete`,
{},
storeHeaders
)

expect(order.status).toEqual(200)
expect(order.data).toEqual({
type: "order",
order: expect.objectContaining({
id: expect.any(String),
}),
})
})

it("should return cart when payment authorization fails", async () => {
const authorizePaymentSessionSpy = jest.spyOn(
PaymentModuleService.prototype,
Expand Down
Loading

0 comments on commit c356748

Please sign in to comment.