|
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 { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer" |
|
9
|
|
|
10
|
jest.setTimeout(60 * 1000) |
|
11
|
|
|
12
|
medusaIntegrationTestRunner({ |
|
13
|
testSuite: ({ dbConnection, api, getContainer, dbUtils }) => { |
|
14
|
let customer |
|
15
|
let customer2 |
|
16
|
let storeCreditAccount, storeCreditAccount2 |
|
17
|
let storeHeaders, storeHeaders2 |
|
18
|
let currencyCode = "usd" |
|
19
|
|
|
20
|
beforeAll(async () => { |
|
21
|
await createAdminUser(dbConnection, adminHeaders, getContainer()) |
|
22
|
const publishableKey = await generatePublishableKey(getContainer()) |
|
23
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
24
|
storeHeaders2 = generateStoreHeaders({ publishableKey }) |
|
25
|
|
|
26
|
const user = await createAuthenticatedCustomer(api, storeHeaders, { |
|
27
|
email: "initial@customer.com", |
|
28
|
}) |
|
29
|
|
|
30
|
const user2 = await createAuthenticatedCustomer(api, storeHeaders, { |
|
31
|
email: "invited@customer.com", |
|
32
|
}) |
|
33
|
|
|
34
|
storeHeaders.headers["Authorization"] = `Bearer ${user.jwt}` |
|
35
|
storeHeaders2.headers["Authorization"] = `Bearer ${user2.jwt}` |
|
36
|
customer = user.customer |
|
37
|
customer2 = user2.customer |
|
38
|
|
|
39
|
storeCreditAccount = ( |
|
40
|
await api.post( |
|
41
|
`/admin/store-credit-accounts`, |
|
42
|
{ currency_code: currencyCode, customer_id: customer.id }, |
|
43
|
adminHeaders |
|
44
|
) |
|
45
|
).data.store_credit_account |
|
46
|
|
|
47
|
storeCreditAccount2 = ( |
|
48
|
await api.post( |
|
49
|
`/admin/store-credit-accounts`, |
|
50
|
{ currency_code: currencyCode, customer_id: customer2.id }, |
|
51
|
adminHeaders |
|
52
|
) |
|
53
|
).data.store_credit_account |
|
54
|
|
|
55
|
await dbUtils.snapshot() |
|
56
|
}) |
|
57
|
|
|
58
|
describe("GET /store/store-credit-accounts", () => { |
|
59
|
it("should return all store credit accounts for a customer", async () => { |
|
60
|
const { |
|
61
|
data: { store_credit_accounts }, |
|
62
|
} = await api.get( |
|
63
|
`/store/store-credit-accounts?currency_code=${currencyCode}`, |
|
64
|
storeHeaders |
|
65
|
) |
|
66
|
|
|
67
|
expect(store_credit_accounts).toHaveLength(1) |
|
68
|
expect(store_credit_accounts).toEqual([ |
|
69
|
expect.objectContaining({ |
|
70
|
currency_code: "usd", |
|
71
|
customer_id: customer.id, |
|
72
|
}), |
|
73
|
]) |
|
74
|
|
|
75
|
const { |
|
76
|
data: { store_credit_accounts: storeCreditAccounts2 }, |
|
77
|
} = await api.get( |
|
78
|
`/store/store-credit-accounts?currency_code=${currencyCode}`, |
|
79
|
storeHeaders2 |
|
80
|
) |
|
81
|
|
|
82
|
expect(storeCreditAccounts2).toHaveLength(1) |
|
83
|
expect(storeCreditAccounts2).toEqual([ |
|
84
|
expect.objectContaining({ |
|
85
|
currency_code: "usd", |
|
86
|
customer_id: customer2.id, |
|
87
|
}), |
|
88
|
]) |
|
89
|
}) |
|
90
|
|
|
91
|
it("should throw error when customer is not authenticated", async () => { |
|
92
|
const unauthenticatedHeaders = { |
|
93
|
headers: { ...storeHeaders.headers }, |
|
94
|
} |
|
95
|
delete unauthenticatedHeaders.headers["Authorization"] |
|
96
|
|
|
97
|
const { response } = await api |
|
98
|
.get( |
|
99
|
`/store/store-credit-accounts?currency_code=${currencyCode}`, |
|
100
|
unauthenticatedHeaders |
|
101
|
) |
|
102
|
.catch((e) => e) |
|
103
|
|
|
104
|
expect(response.status).toBe(401) |
|
105
|
expect(response.data.message).toBe("Unauthorized") |
|
106
|
}) |
|
107
|
}) |
|
108
|
|
|
109
|
describe("GET /store/store-credit-accounts/:id", () => { |
|
110
|
it("should return specific store credit accounts for a customer", async () => { |
|
111
|
const { |
|
112
|
data: { store_credit_account }, |
|
113
|
} = await api.get( |
|
114
|
`/store/store-credit-accounts/${storeCreditAccount.id}`, |
|
115
|
storeHeaders |
|
116
|
) |
|
117
|
|
|
118
|
expect(store_credit_account).toEqual( |
|
119
|
expect.objectContaining({ |
|
120
|
currency_code: "usd", |
|
121
|
customer_id: customer.id, |
|
122
|
}) |
|
123
|
) |
|
124
|
|
|
125
|
const { |
|
126
|
data: { store_credit_account: sca2 }, |
|
127
|
} = await api.get( |
|
128
|
`/store/store-credit-accounts/${storeCreditAccount2.id}`, |
|
129
|
storeHeaders2 |
|
130
|
) |
|
131
|
|
|
132
|
expect(sca2).toEqual( |
|
133
|
expect.objectContaining({ |
|
134
|
currency_code: "usd", |
|
135
|
customer_id: customer2.id, |
|
136
|
}) |
|
137
|
) |
|
138
|
}) |
|
139
|
|
|
140
|
it("should throw error when customer is not authenticated", async () => { |
|
141
|
const unauthenticatedHeaders = { |
|
142
|
headers: { ...storeHeaders.headers }, |
|
143
|
} |
|
144
|
delete unauthenticatedHeaders.headers["Authorization"] |
|
145
|
|
|
146
|
const { response } = await api |
|
147
|
.get( |
|
148
|
`/store/store-credit-accounts/${storeCreditAccount.id}`, |
|
149
|
unauthenticatedHeaders |
|
150
|
) |
|
151
|
.catch((e) => e) |
|
152
|
|
|
153
|
expect(response.status).toBe(401) |
|
154
|
expect(response.data.message).toBe("Unauthorized") |
|
155
|
}) |
|
156
|
}) |
|
157
|
|
|
158
|
describe("POST /store/store-credit-accounts/claim", () => { |
|
159
|
it("should claim an anonymous account for a customer", async () => { |
|
160
|
const container = getContainer() |
|
161
|
const storeCreditService = container.resolve("store_credit") |
|
162
|
|
|
163
|
const [sourceCreditAccount] = |
|
164
|
await storeCreditService.createStoreCreditAccounts([ |
|
165
|
{ |
|
166
|
currency_code: currencyCode, |
|
167
|
code: "SC-XYZ-ABC", |
|
168
|
}, |
|
169
|
]) |
|
170
|
|
|
171
|
await storeCreditService.creditAccounts([ |
|
172
|
{ |
|
173
|
account_id: sourceCreditAccount.id, |
|
174
|
amount: 100, |
|
175
|
reference: "TEST1", |
|
176
|
reference_id: "TEST1", |
|
177
|
}, |
|
178
|
]) |
|
179
|
|
|
180
|
const anonymousCreditAccount = ( |
|
181
|
await api.get( |
|
182
|
`/admin/store-credit-accounts/${sourceCreditAccount.id}`, |
|
183
|
adminHeaders |
|
184
|
) |
|
185
|
).data.store_credit_account |
|
186
|
|
|
187
|
await api.post( |
|
188
|
`/store/store-credit-accounts/claim`, |
|
189
|
{ code: anonymousCreditAccount.code }, |
|
190
|
storeHeaders |
|
191
|
) |
|
192
|
|
|
193
|
const claimedCreditAccount = ( |
|
194
|
await api.get( |
|
195
|
`/admin/store-credit-accounts/${anonymousCreditAccount.id}?fields=*transactions`, |
|
196
|
adminHeaders |
|
197
|
) |
|
198
|
).data.store_credit_account |
|
199
|
|
|
200
|
const targetAccount = ( |
|
201
|
await api.get( |
|
202
|
`/admin/store-credit-accounts/${storeCreditAccount.id}?fields=*transactions`, |
|
203
|
adminHeaders |
|
204
|
) |
|
205
|
).data.store_credit_account |
|
206
|
|
|
207
|
expect(claimedCreditAccount).toEqual( |
|
208
|
expect.objectContaining({ |
|
209
|
balance: 0, |
|
210
|
|
|
211
|
credits: 100, |
|
212
|
debits: 100, |
|
213
|
code: "SC-XYZ-ABC", |
|
214
|
currency_code: "usd", |
|
215
|
customer_id: null, |
|
216
|
|
|
217
|
transactions: expect.arrayContaining([ |
|
218
|
expect.objectContaining({ |
|
219
|
amount: 100, |
|
220
|
type: "credit", // initial crediting |
|
221
|
}), |
|
222
|
expect.objectContaining({ |
|
223
|
amount: 100, |
|
224
|
type: "debit", // claim debit |
|
225
|
reference: "store-credit", |
|
226
|
reference_id: targetAccount.id, |
|
227
|
}), |
|
228
|
]), |
|
229
|
}) |
|
230
|
) |
|
231
|
|
|
232
|
expect(targetAccount).toEqual( |
|
233
|
expect.objectContaining({ |
|
234
|
balance: 100, |
|
235
|
|
|
236
|
debits: 0, |
|
237
|
credits: 100, |
|
238
|
|
|
239
|
currency_code: "usd", |
|
240
|
customer_id: customer.id, |
|
241
|
|
|
242
|
transactions: expect.arrayContaining([ |
|
243
|
expect.objectContaining({ |
|
244
|
amount: 100, |
|
245
|
type: "credit", |
|
246
|
}), |
|
247
|
]), |
|
248
|
}) |
|
249
|
) |
|
250
|
}) |
|
251
|
}) |
|
252
|
}, |
|
253
|
}) |