forked from Emmy123222/Stellar-MicroPay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtipsController.test.js
More file actions
403 lines (325 loc) · 11.2 KB
/
Copy pathtipsController.test.js
File metadata and controls
403 lines (325 loc) · 11.2 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* __tests__/tipsController.test.js
* Unit tests for tipsController.js (#528)
*/
const tipsController = require("../src/controllers/tipsController");
const tipsService = require("../src/services/tipsService");
// Mock the tipsService
jest.mock("../src/services/tipsService");
describe("tipsController", () => {
let req, res, next;
beforeEach(() => {
req = {
body: {},
params: {},
query: {},
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
};
next = jest.fn();
jest.clearAllMocks();
});
describe("Create tip validates required fields", () => {
it("validates required fields before recording tip", async () => {
req.body = {
senderPublicKey: "GABC123456789012345678901234567890123456789012345678",
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
amount: "10.5",
asset: "XLM",
memo: "Great work!",
txHash: "abc123hash",
};
const mockTip = {
id: 1,
...req.body,
timestamp: new Date().toISOString(),
};
tipsService.validateTipInput.mockImplementation(() => {});
tipsService.recordTip.mockReturnValue(mockTip);
await tipsController.recordTip(req, res, next);
expect(tipsService.validateTipInput).toHaveBeenCalledWith({
senderPublicKey: req.body.senderPublicKey,
creatorPublicKey: req.body.creatorPublicKey,
amount: req.body.amount,
});
expect(tipsService.recordTip).toHaveBeenCalledWith({
senderPublicKey: req.body.senderPublicKey,
creatorPublicKey: req.body.creatorPublicKey,
amount: req.body.amount,
asset: "XLM",
memo: "Great work!",
txHash: "abc123hash",
});
expect(res.status).toHaveBeenCalledWith(201);
expect(res.json).toHaveBeenCalledWith({
success: true,
data: mockTip,
message: "Tip recorded successfully",
});
});
it("handles validation errors", async () => {
req.body = {
senderPublicKey: "INVALID",
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
amount: "10.5",
};
const validationError = new Error("Invalid sender public key");
tipsService.validateTipInput.mockImplementation(() => {
throw validationError;
});
await tipsController.recordTip(req, res, next);
expect(next).toHaveBeenCalledWith(validationError);
expect(res.status).not.toHaveBeenCalled();
});
it("uses default values for optional fields", async () => {
req.body = {
senderPublicKey: "GABC123456789012345678901234567890123456789012345678",
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
amount: "5.0",
};
tipsService.validateTipInput.mockImplementation(() => {});
tipsService.recordTip.mockReturnValue({ id: 1 });
await tipsController.recordTip(req, res, next);
expect(tipsService.recordTip).toHaveBeenCalledWith({
senderPublicKey: req.body.senderPublicKey,
creatorPublicKey: req.body.creatorPublicKey,
amount: req.body.amount,
asset: "XLM",
memo: "",
txHash: "",
});
});
it("rejects tip with missing required fields", async () => {
req.body = {
senderPublicKey: "GABC123456789012345678901234567890123456789012345678",
// Missing creatorPublicKey and amount
};
const validationError = new Error("Missing required fields");
tipsService.validateTipInput.mockImplementation(() => {
throw validationError;
});
await tipsController.recordTip(req, res, next);
expect(next).toHaveBeenCalledWith(validationError);
});
});
describe("List received/sent tips supports pagination params", () => {
it("gets received tips with pagination", async () => {
req.params = {
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
};
req.query = {
limit: "10",
offset: "20",
};
const mockResult = {
tips: [
{ id: 1, amount: "5.0", asset: "XLM" },
{ id: 2, amount: "10.0", asset: "XLM" },
],
total: 50,
limit: 10,
offset: 20,
};
const mockStats = {
totalTips: 50,
totalByAsset: { XLM: { count: 50, amount: "250.0" } },
};
tipsService.getTipsReceived.mockReturnValue(mockResult);
tipsService.getTipsStats.mockReturnValue(mockStats);
await tipsController.getTipsReceived(req, res, next);
expect(tipsService.getTipsReceived).toHaveBeenCalledWith(
req.params.creatorPublicKey,
{ limit: 10, offset: 20 }
);
expect(res.json).toHaveBeenCalledWith({
success: true,
data: {
...mockResult,
stats: mockStats,
},
});
});
it("gets sent tips with pagination", async () => {
req.params = {
senderPublicKey: "GABC123456789012345678901234567890123456789012345678",
};
req.query = {
limit: "15",
offset: "5",
};
const mockResult = {
tips: [{ id: 1, amount: "3.0", asset: "XLM" }],
total: 20,
limit: 15,
offset: 5,
};
tipsService.getTipsSent.mockReturnValue(mockResult);
await tipsController.getTipsSent(req, res, next);
expect(tipsService.getTipsSent).toHaveBeenCalledWith(
req.params.senderPublicKey,
{ limit: 15, offset: 5 }
);
expect(res.json).toHaveBeenCalledWith({
success: true,
data: mockResult,
});
});
it("handles pagination with undefined limit/offset", async () => {
req.params = {
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
};
req.query = {};
tipsService.getTipsReceived.mockReturnValue({ tips: [], total: 0 });
tipsService.getTipsStats.mockReturnValue({});
await tipsController.getTipsReceived(req, res, next);
expect(tipsService.getTipsReceived).toHaveBeenCalledWith(
req.params.creatorPublicKey,
{ limit: undefined, offset: undefined }
);
});
it("parses pagination params as integers", async () => {
req.params = {
senderPublicKey: "GABC123456789012345678901234567890123456789012345678",
};
req.query = {
limit: "25",
offset: "50",
};
tipsService.getTipsSent.mockReturnValue({ tips: [], total: 0 });
await tipsController.getTipsSent(req, res, next);
expect(tipsService.getTipsSent).toHaveBeenCalledWith(
req.params.senderPublicKey,
{ limit: 25, offset: 50 }
);
});
it("handles errors in getTipsReceived", async () => {
req.params = {
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
};
const error = new Error("Database error");
tipsService.getTipsReceived.mockImplementation(() => {
throw error;
});
await tipsController.getTipsReceived(req, res, next);
expect(next).toHaveBeenCalledWith(error);
});
});
describe("Stats endpoint returns correct aggregate shape", () => {
it("returns stats for a creator", async () => {
req.params = {
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
};
const mockStats = {
totalTips: 100,
totalByAsset: {
XLM: { count: 80, amount: "400.0" },
USDC: { count: 20, amount: "200.0" },
},
averageTip: "6.0",
largestTip: "50.0",
smallestTip: "1.0",
};
tipsService.getTipsStats.mockReturnValue(mockStats);
await tipsController.getTipsStats(req, res, next);
expect(tipsService.getTipsStats).toHaveBeenCalledWith(
req.params.creatorPublicKey
);
expect(res.json).toHaveBeenCalledWith({
success: true,
data: mockStats,
});
});
it("returns correct shape with multiple assets", async () => {
req.params = {
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
};
const mockStats = {
totalTips: 50,
totalByAsset: {
XLM: { count: 30, amount: "150.0" },
USDC: { count: 15, amount: "75.0" },
BTC: { count: 5, amount: "0.5" },
},
averageTip: "4.5",
largestTip: "25.0",
smallestTip: "0.5",
};
tipsService.getTipsStats.mockReturnValue(mockStats);
await tipsController.getTipsStats(req, res, next);
const response = res.json.mock.calls[0][0];
expect(response.data).toHaveProperty("totalTips");
expect(response.data).toHaveProperty("totalByAsset");
expect(response.data).toHaveProperty("averageTip");
expect(response.data.totalByAsset).toHaveProperty("XLM");
expect(response.data.totalByAsset).toHaveProperty("USDC");
expect(response.data.totalByAsset).toHaveProperty("BTC");
});
it("handles stats for creator with no tips", async () => {
req.params = {
creatorPublicKey: "GHIJ789012345678901234567890123456789012345678901234",
};
const mockStats = {
totalTips: 0,
totalByAsset: {},
averageTip: "0",
largestTip: "0",
smallestTip: "0",
};
tipsService.getTipsStats.mockReturnValue(mockStats);
await tipsController.getTipsStats(req, res, next);
expect(res.json).toHaveBeenCalledWith({
success: true,
data: mockStats,
});
});
it("handles errors in getTipsStats", async () => {
req.params = {
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
};
const error = new Error("Stats calculation failed");
tipsService.getTipsStats.mockImplementation(() => {
throw error;
});
await tipsController.getTipsStats(req, res, next);
expect(next).toHaveBeenCalledWith(error);
});
});
describe("Additional endpoints", () => {
it("gets top tippers with limit", async () => {
req.params = {
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
};
req.query = {
limit: "10",
};
const mockResult = [
{ senderPublicKey: "GABC...", totalAmount: "100.0", tipCount: 5 },
{ senderPublicKey: "GDEF...", totalAmount: "75.0", tipCount: 3 },
];
tipsService.getTopTippers.mockReturnValue(mockResult);
await tipsController.getTopTippers(req, res, next);
expect(tipsService.getTopTippers).toHaveBeenCalledWith(
req.params.creatorPublicKey,
10
);
expect(res.json).toHaveBeenCalledWith({
success: true,
data: mockResult,
});
});
it("uses default limit for top tippers when not provided", async () => {
req.params = {
creatorPublicKey: "GDEF456789012345678901234567890123456789012345678901",
};
req.query = {};
tipsService.getTopTippers.mockReturnValue([]);
await tipsController.getTopTippers(req, res, next);
expect(tipsService.getTopTippers).toHaveBeenCalledWith(
req.params.creatorPublicKey,
5 // Default limit
);
});
});
});