forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellarPaymentController.test.js
More file actions
387 lines (356 loc) · 12.3 KB
/
Copy pathstellarPaymentController.test.js
File metadata and controls
387 lines (356 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { jest } from "@jest/globals";
import express from "express";
import request from "supertest";
import mongoose from "mongoose";
const buildPaymentTransaction = jest.fn();
const buildSep7Uri = jest.fn();
const calculateFeeSplit = jest.fn();
const preflightPayment = jest.fn();
const submitTransaction = jest.fn();
const verifyPaymentOperations = jest.fn();
const getExplorerUrl = jest.fn((hash) => `https://stellar.expert/tx/${hash}`);
const recordSaleEarnings = jest.fn();
const enqueue = jest.fn();
// Mirrors every named export of stellarService.js, not just the ones this
// suite exercises: other test files (preflightPayment.test.js,
// pathPayments.test.js) statically import the real module, and Jest's ESM
// module linker can resolve those against this mock's identity when both
// run in the same worker - an incomplete mock then fails with "does not
// provide an export named X" for exports this file never even references.
jest.unstable_mockModule("../src/services/stellar/stellarService.js", () => ({
STROOPS_PER_UNIT: 10000000n,
toStroops: jest.fn(),
fromStroops: jest.fn(),
applySlippage: jest.fn(),
findPaymentPaths: jest.fn(),
buildPathPaymentTransaction: jest.fn(),
calculateFeeSplit,
buildSep7Uri,
isValidPublicKey: jest.fn(),
getAccountBalance: jest.fn(),
MEMO_REQUIRED_DATA_KEY: "config.memo_required",
isMemoRequired: jest.fn(),
PREFLIGHT_REASON_CODES: {},
preflightPayment,
buildPaymentTransaction,
buildReversePaymentTransaction: jest.fn(),
submitTransaction,
verifyTransaction: jest.fn(),
verifyPaymentOperations,
validateSignedPaymentXdr: jest.fn(),
hasUsdcTrustline: jest.fn(),
getExplorerUrl,
getAccountExplorerUrl: jest.fn(),
server: {},
USDC: "USDC",
USDC_ISSUER: "",
NETWORK: "testnet",
networkPassphrase: "Test SDF Network ; September 2015",
DONATION_WALLET_PUBLIC_KEY: "",
PLATFORM_FEE_PERCENT: 0,
PLATFORM_WALLET_PUBLIC_KEY: "",
}));
jest.unstable_mockModule("../src/services/payoutService.js", () => ({
recordSaleEarnings,
}));
// paymentController.js enqueues background jobs (receipt generation, retry
// verification) via the real job queue - mock it so those calls don't hit
// the actual queue, which has no handlers registered in this suite.
jest.unstable_mockModule("../src/jobs/queue.js", () => ({
enqueue,
}));
const { initializePayment, submitPayment } = await import(
"../src/controllers/stellar/paymentController.js"
);
const User = (await import("../src/models/User.js")).default;
const Book = (await import("../src/models/Book.js")).default;
const Course = (await import("../src/models/Course.js")).default;
const Transaction = (await import("../src/models/Transaction.js")).default;
const makeQuery = (result) => {
const query = {
session: jest.fn(() => Promise.resolve(result)),
populate: jest.fn(() => query),
select: jest.fn(() => query),
sort: jest.fn(() => query),
skip: jest.fn(() => query),
limit: jest.fn(() => query),
then: (resolve, reject) => Promise.resolve(result).then(resolve, reject),
};
return query;
};
const makeSession = () => ({
startTransaction: jest.fn(),
commitTransaction: jest.fn(() => Promise.resolve()),
abortTransaction: jest.fn(() => Promise.resolve()),
endSession: jest.fn(),
});
const mountPaymentApp = (userId) => {
const app = express();
app.use(express.json());
app.use((req, _res, next) => {
req.user = { _id: userId };
next();
});
app.post("/initialize", initializePayment);
app.post("/submit", submitPayment);
return app;
};
describe("Stellar payment controller", () => {
let buyerId;
let creatorId;
let itemId;
let buyerWallet;
let creatorWallet;
let session;
let savedTransactions;
beforeEach(() => {
jest.restoreAllMocks();
buildPaymentTransaction.mockReset();
buildSep7Uri.mockReset();
calculateFeeSplit.mockReset().mockReturnValue(null);
preflightPayment.mockReset().mockResolvedValue({ ok: true });
submitTransaction.mockReset();
verifyPaymentOperations.mockReset();
getExplorerUrl.mockClear();
recordSaleEarnings.mockReset();
enqueue.mockReset().mockResolvedValue(undefined);
buyerId = new mongoose.Types.ObjectId();
creatorId = new mongoose.Types.ObjectId();
itemId = new mongoose.Types.ObjectId();
buyerWallet = "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5";
creatorWallet = "GCKFBEIYTKPXL5UIRZ5OO3KSOFDP5D4R6YGNFWEQSIFGKWO3EZ5F3TGI";
session = makeSession();
savedTransactions = [];
jest.spyOn(mongoose, "startSession").mockResolvedValue(session);
jest.spyOn(Transaction.prototype, "save").mockImplementation(function () {
savedTransactions.push(this);
return Promise.resolve(this);
});
});
afterEach(() => {
jest.restoreAllMocks();
});
it("initializes a book payment and returns unsigned XDR details", async () => {
const buyer = {
_id: buyerId,
stellarWallet: { publicKey: buyerWallet },
purchasedBooks: [],
};
const creator = {
_id: creatorId,
name: "Educator",
stellarWallet: { publicKey: creatorWallet },
};
const book = {
_id: itemId,
title: "Paid Book",
price: 15,
author: creator,
};
jest.spyOn(User, "findById").mockReturnValue(makeQuery(buyer));
jest.spyOn(Book, "findById").mockReturnValue(makeQuery(book));
jest.spyOn(Transaction, "findOne").mockReturnValue(makeQuery(null));
buildPaymentTransaction.mockResolvedValue({
xdr: "unsigned-xdr",
hash: "expected-hash",
networkPassphrase: "Test SDF Network ; September 2015",
feeSplit: null,
});
buildSep7Uri.mockReturnValue("web+stellar:pay?destination=creator");
const res = await request(mountPaymentApp(buyerId))
.post("/initialize")
.send({
itemType: "book",
itemId: itemId.toString(),
buyerWallet,
});
expect(res.statusCode).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.payment).toMatchObject({
xdr: "unsigned-xdr",
expectedHash: "expected-hash",
});
expect(buildPaymentTransaction).toHaveBeenCalledWith(
expect.objectContaining({
sourcePublicKey: buyerWallet,
destinationPublicKey: creatorWallet,
amount: "15",
applyPlatformFee: true,
})
);
expect(savedTransactions).toHaveLength(1);
expect(savedTransactions[0]).toMatchObject({
buyer: buyerId,
buyerWallet,
creator: creatorId,
creatorWallet,
status: "pending",
// #18: the pre-computed hash is stored as expectedHash at init for later
// XDR validation; stellarTxHash is only set after actual submission.
expectedHash: "expected-hash",
});
expect(session.commitTransaction).toHaveBeenCalledTimes(1);
expect(session.abortTransaction).not.toHaveBeenCalled();
});
it("rejects an invalid signed XDR and stores the Stellar failure reason", async () => {
const tx = {
_id: new mongoose.Types.ObjectId(),
buyer: buyerId,
status: "pending",
save: jest.fn(() => Promise.resolve()),
};
jest.spyOn(Transaction, "findOne").mockReturnValue(makeQuery(tx));
submitTransaction.mockRejectedValue(new Error("Invalid XDR"));
const res = await request(mountPaymentApp(buyerId))
.post("/submit")
.send({
transactionId: tx._id.toString(),
signedXdr: "tampered-xdr",
});
expect(res.statusCode).toBe(400);
expect(res.body).toMatchObject({
success: false,
message: "Transaction failed on Stellar network",
error: "Invalid XDR",
});
expect(tx.status).toBe("failed");
expect(tx.failureReason).toBe("Invalid XDR");
expect(session.commitTransaction).toHaveBeenCalledTimes(1);
expect(session.abortTransaction).not.toHaveBeenCalled();
});
it("does not grant access when on-chain verification fails", async () => {
const tx = {
_id: new mongoose.Types.ObjectId(),
buyer: buyerId,
itemType: "course",
itemId,
itemTitle: "Course",
amount: "25",
creatorWallet,
status: "pending",
save: jest.fn(() => Promise.resolve()),
};
jest.spyOn(Transaction, "findOne").mockReturnValue(makeQuery(tx));
const findByIdSpy = jest.spyOn(User, "findById");
submitTransaction.mockResolvedValue({
hash: "hash-failed-verification",
ledger: 44,
successful: true,
});
verifyPaymentOperations.mockResolvedValue({
verified: false,
reason: "Missing expected USDC payment",
});
const res = await request(mountPaymentApp(buyerId))
.post("/submit")
.send({
transactionId: tx._id.toString(),
signedXdr: "signed-xdr",
});
expect(res.statusCode).toBe(400);
expect(res.body.message).toBe(
"Payment could not be verified on the Stellar network"
);
expect(tx.status).toBe("failed");
expect(tx.failureReason).toContain("On-chain verification failed");
expect(findByIdSpy).not.toHaveBeenCalled();
expect(recordSaleEarnings).not.toHaveBeenCalled();
expect(session.commitTransaction).toHaveBeenCalledTimes(1);
});
it("grants course access only after successful Stellar verification", async () => {
const tx = {
_id: new mongoose.Types.ObjectId(),
buyer: buyerId,
creator: creatorId,
itemType: "course",
itemId,
itemTitle: "Course",
amount: "25",
creatorWallet,
status: "pending",
save: jest.fn(() => Promise.resolve()),
};
const buyer = {
_id: buyerId,
purchasedCourses: [],
stat: { coursesEnrolled: 0 },
save: jest.fn(() => Promise.resolve()),
};
jest.spyOn(Transaction, "findOne").mockReturnValue(makeQuery(tx));
jest.spyOn(User, "findById").mockReturnValue(makeQuery(buyer));
jest
.spyOn(Course, "findByIdAndUpdate")
.mockResolvedValue({ _id: itemId });
submitTransaction.mockResolvedValue({
hash: "hash-confirmed",
ledger: 77,
successful: true,
});
verifyPaymentOperations.mockResolvedValue({ verified: true });
recordSaleEarnings.mockResolvedValue({ success: true });
const res = await request(mountPaymentApp(buyerId))
.post("/submit")
.send({
transactionId: tx._id.toString(),
signedXdr: "signed-xdr",
});
expect(res.statusCode).toBe(200);
expect(res.body.success).toBe(true);
expect(verifyPaymentOperations).toHaveBeenCalledWith("hash-confirmed", [{ destination: creatorWallet, amount: "25" }], "USDC");
expect(recordSaleEarnings).toHaveBeenCalledWith(tx, { session });
expect(buyer.purchasedCourses).toHaveLength(1);
expect(buyer.purchasedCourses[0].courseId).toBe(itemId);
expect(buyer.stat.coursesEnrolled).toBe(1);
expect(Course.findByIdAndUpdate).toHaveBeenCalledWith(
itemId,
{ $addToSet: { enrolledUsers: buyerId } },
{ session }
);
expect(tx.status).toBe("confirmed");
expect(session.commitTransaction).toHaveBeenCalledTimes(1);
expect(session.abortTransaction).not.toHaveBeenCalled();
});
it("aborts the Mongo transaction if granting access fails", async () => {
const tx = {
_id: new mongoose.Types.ObjectId(),
buyer: buyerId,
creator: creatorId,
itemType: "book",
itemId,
itemTitle: "Book",
amount: "12",
creatorWallet,
status: "pending",
save: jest.fn(() => Promise.resolve()),
};
const buyer = {
_id: buyerId,
purchasedBooks: [],
stat: { booksRead: 0 },
save: jest.fn(() => Promise.reject(new Error("grant failed"))),
};
jest.spyOn(Transaction, "findOne").mockReturnValue(makeQuery(tx));
jest.spyOn(User, "findById").mockReturnValue(makeQuery(buyer));
submitTransaction.mockResolvedValue({
hash: "hash-before-access-failure",
ledger: 88,
successful: true,
});
verifyPaymentOperations.mockResolvedValue({ verified: true });
recordSaleEarnings.mockResolvedValue({ success: true });
const res = await request(mountPaymentApp(buyerId))
.post("/submit")
.send({
transactionId: tx._id.toString(),
signedXdr: "signed-xdr",
});
expect(res.statusCode).toBe(500);
expect(res.body).toMatchObject({
success: false,
message: "Failed to process payment",
});
expect(recordSaleEarnings).toHaveBeenCalled();
expect(session.abortTransaction).toHaveBeenCalledTimes(1);
expect(session.commitTransaction).not.toHaveBeenCalled();
});
});