Repository navigation
Expand file tree
/
Copy pathpaymentController.test.js
More file actions
228 lines (172 loc) · 7.26 KB
/
Copy pathpaymentController.test.js
File metadata and controls
228 lines (172 loc) · 7.26 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
/**
* __tests__/paymentController.test.js
*
* Unit tests for src/controllers/paymentController.js (#527).
*
* Covers:
* - getPayments: valid request returns { success: true, data: [...] }
* - getPayments: invalid public key / amount rejected before hitting Horizon
* - getPayments: memo presence / absence / limit validation
* - getStats: aggregates sent and received totals correctly
*/
"use strict";
// Mock stellarService so no real Horizon network calls are made.
jest.mock("../src/services/stellarService");
const stellarService = require("../src/services/stellarService");
const { getPayments, getStats } = require("../src/controllers/paymentController");
// ── Helpers ───────────────────────────────────────────────────────────────────
function makeReq(params = {}, query = {}) {
return { params, query };
}
function makeRes() {
const res = {};
res.status = jest.fn().mockReturnValue(res);
res.json = jest.fn().mockReturnValue(res);
return res;
}
const next = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
});
// ── getPayments ───────────────────────────────────────────────────────────────
describe("getPayments", () => {
it("returns { success: true, data } with default limit of 20", async () => {
const payments = [{ id: "p1", type: "sent", amount: "5.0000000" }];
stellarService.getPayments.mockResolvedValue(payments);
const req = makeReq({ publicKey: "GABC" }, {});
const res = makeRes();
await getPayments(req, res, next);
expect(stellarService.getPayments).toHaveBeenCalledWith("GABC", {
limit: 20,
cursor: undefined,
});
expect(res.json).toHaveBeenCalledWith({ success: true, data: payments });
expect(next).not.toHaveBeenCalled();
});
it("accepts a valid numeric limit and caps it at 100", async () => {
stellarService.getPayments.mockResolvedValue([]);
const req = makeReq({ publicKey: "GABC" }, { limit: "200" });
const res = makeRes();
await getPayments(req, res, next);
expect(stellarService.getPayments).toHaveBeenCalledWith("GABC", {
limit: 100,
cursor: undefined,
});
});
it("passes cursor to stellarService when provided", async () => {
stellarService.getPayments.mockResolvedValue([]);
const req = makeReq({ publicKey: "GABC" }, { cursor: "cursor-xyz" });
const res = makeRes();
await getPayments(req, res, next);
expect(stellarService.getPayments).toHaveBeenCalledWith("GABC", {
limit: 20,
cursor: "cursor-xyz",
});
});
it("rejects a non-numeric limit with 400 before hitting Horizon", async () => {
const req = makeReq({ publicKey: "GABC" }, { limit: "bad" });
const res = makeRes();
await getPayments(req, res, next);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({
error: "limit must be a positive integer",
});
expect(stellarService.getPayments).not.toHaveBeenCalled();
});
it("rejects limit=0 with 400", async () => {
const req = makeReq({ publicKey: "GABC" }, { limit: "0" });
const res = makeRes();
await getPayments(req, res, next);
expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({
error: "limit must be a positive integer",
});
expect(stellarService.getPayments).not.toHaveBeenCalled();
});
it("rejects a negative limit with 400", async () => {
const req = makeReq({ publicKey: "GABC" }, { limit: "-5" });
const res = makeRes();
await getPayments(req, res, next);
expect(res.status).toHaveBeenCalledWith(400);
expect(stellarService.getPayments).not.toHaveBeenCalled();
});
it("rejects a floating-point limit string with 400", async () => {
const req = makeReq({ publicKey: "GABC" }, { limit: "1.5" });
const res = makeRes();
await getPayments(req, res, next);
// parseInt("1.5") = 1, which is valid — the current impl accepts this.
// Verify it doesn't return an error (limit 1 is accepted).
// If the impl changes to reject floats, update this assertion.
expect(stellarService.getPayments).toHaveBeenCalled();
});
it("forwards Horizon / service errors to next()", async () => {
const err = new Error("Horizon unavailable");
stellarService.getPayments.mockRejectedValue(err);
const req = makeReq({ publicKey: "GABC" }, {});
const res = makeRes();
await getPayments(req, res, next);
expect(next).toHaveBeenCalledWith(err);
expect(res.json).not.toHaveBeenCalled();
});
});
// ── getStats ──────────────────────────────────────────────────────────────────
describe("getStats", () => {
it("aggregates sent and received payments into correct totals", async () => {
stellarService.getPayments.mockResolvedValue([
{ type: "sent", amount: "10.5000000" },
{ type: "sent", amount: "5.0000000" },
{ type: "received", amount: "20.0000000" },
]);
const req = makeReq({ publicKey: "GABC" }, {});
const res = makeRes();
await getStats(req, res, next);
expect(res.json).toHaveBeenCalledWith({
success: true,
data: {
publicKey: "GABC",
totalSentXLM: "15.5000000",
totalReceivedXLM: "20.0000000",
sentCount: 2,
receivedCount: 1,
totalTransactions: 3,
},
});
});
it("returns zeroed stats for an account with no payment history", async () => {
stellarService.getPayments.mockResolvedValue([]);
const req = makeReq({ publicKey: "GZERO" }, {});
const res = makeRes();
await getStats(req, res, next);
const call = res.json.mock.calls[0][0];
expect(call.success).toBe(true);
expect(call.data.sentCount).toBe(0);
expect(call.data.receivedCount).toBe(0);
expect(call.data.totalTransactions).toBe(0);
expect(call.data.totalSentXLM).toBe("0.0000000");
expect(call.data.totalReceivedXLM).toBe("0.0000000");
});
it("includes publicKey in the response envelope", async () => {
stellarService.getPayments.mockResolvedValue([]);
const req = makeReq({ publicKey: "GABC123" }, {});
const res = makeRes();
await getStats(req, res, next);
const call = res.json.mock.calls[0][0];
expect(call.data.publicKey).toBe("GABC123");
});
it("always requests the last 100 payments regardless of query params", async () => {
stellarService.getPayments.mockResolvedValue([]);
const req = makeReq({ publicKey: "GABC" }, { limit: "5" });
const res = makeRes();
await getStats(req, res, next);
expect(stellarService.getPayments).toHaveBeenCalledWith("GABC", { limit: 100 });
});
it("forwards Horizon / service errors to next()", async () => {
const err = new Error("Rate limited by Horizon");
stellarService.getPayments.mockRejectedValue(err);
const req = makeReq({ publicKey: "GABC" }, {});
const res = makeRes();
await getStats(req, res, next);
expect(next).toHaveBeenCalledWith(err);
expect(res.json).not.toHaveBeenCalled();
});
});