forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment-integration.test.ts
More file actions
153 lines (142 loc) · 4.95 KB
/
Copy pathpayment-integration.test.ts
File metadata and controls
153 lines (142 loc) · 4.95 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
/**
* Payment Integration Tests
* These tests demonstrate the payment processing flow
*/
import { Test, TestingModule } from "@nestjs/testing";
import { Repository } from "typeorm";
import { DataSource } from "typeorm";
import { getRepositoryToken } from "@nestjs/typeorm";
import { StellarService } from "../stellar/stellar.service";
import { PaymentProcessorService } from "../payments/payment-processor.service";
import { PaymentGateway } from "../websocket/payment.gateway";
import { EventsGateway } from "../gateway/events.gateway";
import { Payment } from "../entities/payment.entity";
import { Participant } from "../entities/participant.entity";
import { Split } from "../entities/split.entity";
import { EmailService } from "../email/email.service";
import { MultiCurrencyService } from "../multi-currency/multi-currency.service";
import { AnalyticsService } from "../analytics/analytics.service";
describe("Payment Integration Tests", () => {
let service: PaymentProcessorService;
let stellarService: StellarService;
let paymentGateway: PaymentGateway;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
PaymentProcessorService,
{
provide: StellarService,
useValue: {
verifyTransaction: jest.fn(),
},
},
{
provide: PaymentGateway,
useValue: {
emitPaymentNotification: jest.fn(),
emitSplitCompletion: jest.fn(),
},
},
{
provide: EventsGateway,
useValue: {
emitPaymentReceived: jest.fn(),
emitSplitUpdated: jest.fn(),
emitParticipantJoined: jest.fn(),
},
},
{
provide: getRepositoryToken(Payment),
useClass: Repository,
},
{
provide: getRepositoryToken(Participant),
useClass: Repository,
},
{
provide: getRepositoryToken(Split),
useClass: Repository,
},
{
provide: EmailService,
useValue: {
sendPaymentConfirmation: jest.fn(),
sendSplitCompletedNotification: jest.fn(),
},
},
{
provide: MultiCurrencyService,
useValue: {
processMultiCurrencyPayment: jest.fn(),
},
},
{
provide: AnalyticsService,
useValue: {
trackPaymentProcessed: jest.fn(),
},
},
{
provide: DataSource,
useValue: {
createQueryRunner: jest.fn().mockReturnValue({
connect: jest.fn().mockResolvedValue(undefined),
startTransaction: jest.fn().mockResolvedValue(undefined),
rollbackTransaction: jest.fn().mockResolvedValue(undefined),
commitTransaction: jest.fn().mockResolvedValue(undefined),
release: jest.fn().mockResolvedValue(undefined),
manager: {
findOne: jest.fn().mockResolvedValue(null),
save: jest.fn().mockResolvedValue(null),
},
}),
},
},
],
}).compile();
service = module.get<PaymentProcessorService>(PaymentProcessorService);
stellarService = module.get<StellarService>(StellarService);
paymentGateway = module.get<PaymentGateway>(PaymentGateway);
});
it("should process a complete payment", async () => {
// Mock the Stellar service to return a valid transaction
jest.spyOn(stellarService, "verifyTransaction").mockResolvedValue({
valid: true,
amount: 50.0,
asset: "XLM-USDC",
sender: "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
receiver: "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
timestamp: "2023-01-01T00:00:00Z",
});
// In a real test, we would mock repository methods and test the full flow
expect(service).toBeDefined();
});
it("should handle partial payments", async () => {
// Mock a transaction with amount less than owed
jest.spyOn(stellarService, "verifyTransaction").mockResolvedValue({
valid: true,
amount: 25.0, // Less than owed amount
asset: "XLM-USDC",
sender: "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
receiver: "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
timestamp: "2023-01-01T00:00:00Z",
});
expect(service).toBeDefined();
});
it("should reject invalid transactions", async () => {
// Mock an invalid transaction
jest.spyOn(stellarService, "verifyTransaction").mockResolvedValue({
valid: false,
amount: 0,
asset: "",
sender: "",
receiver: "",
timestamp: "2023-01-01T00:00:00Z",
});
expect(service).toBeDefined();
});
it("should prevent duplicate transactions", async () => {
// This test would verify that the service prevents duplicate submissions
expect(service).toBeDefined();
});
});