|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { |
|
3
|
adminHeaders, |
|
4
|
createAdminUser, |
|
5
|
generatePublishableKey, |
|
6
|
generateStoreHeaders, |
|
7
|
} from "../../../../helpers/create-admin-user" |
|
8
|
import { getProductFixture } from "../../../../helpers/fixtures" |
|
9
|
|
|
10
|
jest.setTimeout(60000) |
|
11
|
|
|
12
|
medusaIntegrationTestRunner({ |
|
13
|
testSuite: ({ dbConnection, getContainer, api }) => { |
|
14
|
let storeHeaders |
|
15
|
let salesChannel |
|
16
|
let product |
|
17
|
let regionWithProvider |
|
18
|
let regionWithoutProvider |
|
19
|
|
|
20
|
const createRegion = async (name, countries, paymentProviders) => { |
|
21
|
return ( |
|
22
|
await api.post( |
|
23
|
"/admin/regions", |
|
24
|
{ |
|
25
|
name, |
|
26
|
currency_code: "usd", |
|
27
|
countries, |
|
28
|
payment_providers: paymentProviders, |
|
29
|
}, |
|
30
|
adminHeaders |
|
31
|
) |
|
32
|
).data.region |
|
33
|
} |
|
34
|
|
|
35
|
const createPaymentCollection = async (regionId) => { |
|
36
|
const cart = ( |
|
37
|
await api.post( |
|
38
|
"/store/carts", |
|
39
|
{ |
|
40
|
region_id: regionId, |
|
41
|
sales_channel_id: salesChannel.id, |
|
42
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
43
|
}, |
|
44
|
storeHeaders |
|
45
|
) |
|
46
|
).data.cart |
|
47
|
|
|
48
|
const paymentCollection = ( |
|
49
|
await api.post( |
|
50
|
"/store/payment-collections", |
|
51
|
{ cart_id: cart.id }, |
|
52
|
storeHeaders |
|
53
|
) |
|
54
|
).data.payment_collection |
|
55
|
|
|
56
|
return { cart, paymentCollection } |
|
57
|
} |
|
58
|
|
|
59
|
beforeEach(async () => { |
|
60
|
const container = getContainer() |
|
61
|
const publishableKey = await generatePublishableKey(container) |
|
62
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
63
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
64
|
|
|
65
|
regionWithProvider = await createRegion( |
|
66
|
"With provider", |
|
67
|
["us"], |
|
68
|
["pp_system_default"] |
|
69
|
) |
|
70
|
regionWithoutProvider = await createRegion("Without provider", ["ca"], []) |
|
71
|
|
|
72
|
salesChannel = ( |
|
73
|
await api.post("/admin/sales-channels", { name: "Web" }, adminHeaders) |
|
74
|
).data.sales_channel |
|
75
|
|
|
76
|
const shippingProfile = ( |
|
77
|
await api.post( |
|
78
|
"/admin/shipping-profiles", |
|
79
|
{ name: "Test", type: "default" }, |
|
80
|
adminHeaders |
|
81
|
) |
|
82
|
).data.shipping_profile |
|
83
|
|
|
84
|
product = ( |
|
85
|
await api.post( |
|
86
|
"/admin/products", |
|
87
|
getProductFixture({ |
|
88
|
title: "test", |
|
89
|
status: "published", |
|
90
|
shipping_profile_id: shippingProfile.id, |
|
91
|
variants: [ |
|
92
|
{ |
|
93
|
title: "Test variant", |
|
94
|
manage_inventory: false, |
|
95
|
prices: [{ amount: 150, currency_code: "usd" }], |
|
96
|
}, |
|
97
|
], |
|
98
|
}), |
|
99
|
adminHeaders |
|
100
|
) |
|
101
|
).data.product |
|
102
|
}) |
|
103
|
|
|
104
|
describe("POST /store/payment-collections/:id/payment-sessions", () => { |
|
105
|
it("creates a session when the provider is enabled in the cart's region", async () => { |
|
106
|
const { paymentCollection } = await createPaymentCollection( |
|
107
|
regionWithProvider.id |
|
108
|
) |
|
109
|
|
|
110
|
const { data } = await api.post( |
|
111
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
112
|
{ provider_id: "pp_system_default" }, |
|
113
|
storeHeaders |
|
114
|
) |
|
115
|
|
|
116
|
expect(data.payment_collection.payment_sessions).toEqual([ |
|
117
|
expect.objectContaining({ |
|
118
|
provider_id: "pp_system_default", |
|
119
|
status: "pending", |
|
120
|
}), |
|
121
|
]) |
|
122
|
}) |
|
123
|
|
|
124
|
it("rejects a provider that is not enabled in the cart's region", async () => { |
|
125
|
const { paymentCollection } = await createPaymentCollection( |
|
126
|
regionWithoutProvider.id |
|
127
|
) |
|
128
|
|
|
129
|
const error = await api |
|
130
|
.post( |
|
131
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
132
|
{ provider_id: "pp_system_default" }, |
|
133
|
storeHeaders |
|
134
|
) |
|
135
|
.catch((e) => e) |
|
136
|
|
|
137
|
expect(error.response.status).toEqual(400) |
|
138
|
expect(error.response.data.message).toEqual( |
|
139
|
`Payment provider pp_system_default is not enabled in the cart's region ${regionWithoutProvider.id}.` |
|
140
|
) |
|
141
|
}) |
|
142
|
|
|
143
|
it("rejects completing a cart whose session provider was disabled in its region", async () => { |
|
144
|
const { cart, paymentCollection } = await createPaymentCollection( |
|
145
|
regionWithProvider.id |
|
146
|
) |
|
147
|
|
|
148
|
await api.post( |
|
149
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
150
|
{ provider_id: "pp_system_default" }, |
|
151
|
storeHeaders |
|
152
|
) |
|
153
|
|
|
154
|
await api.post( |
|
155
|
`/admin/regions/${regionWithProvider.id}`, |
|
156
|
{ payment_providers: [] }, |
|
157
|
adminHeaders |
|
158
|
) |
|
159
|
|
|
160
|
const error = await api |
|
161
|
.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
162
|
.catch((e) => e) |
|
163
|
|
|
164
|
expect(error.response.status).toEqual(400) |
|
165
|
expect(error.response.data.message).toEqual( |
|
166
|
`Payment provider pp_system_default is not enabled in the cart's region ${regionWithProvider.id}.` |
|
167
|
) |
|
168
|
}) |
|
169
|
}) |
|
170
|
}, |
|
171
|
}) |