forked from InsurNiffy/niff-Stellar-shurance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhorizon.integration.spec.ts
More file actions
277 lines (236 loc) · 9.93 KB
/
Copy pathhorizon.integration.spec.ts
File metadata and controls
277 lines (236 loc) · 9.93 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
/**
* Horizon proxy integration tests.
*
* Uses mocked fetch and mocked Redis — no real Horizon credentials required.
* Covers: field filtering, rate limiting, 429 response, cache hit, address validation.
*/
import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication, HttpStatus } from "@nestjs/common";
import * as request from "supertest";
import { ConfigModule } from "@nestjs/config";
import { HorizonModule } from "../horizon.module";
import { RedisService } from "../../cache/redis.service";
// ── Fixtures ──────────────────────────────────────────────────────────────────
const VALID_ACCOUNT = "GBCPNZ6S7RK5N4BX6HBXBCX7P5QNBOJZFGDWBZBXCLK5T6KHWOPTLR3I";
const MOCK_HORIZON_RESPONSE = {
_links: {
self: {
href: "https://horizon-testnet.stellar.org/accounts/G.../operations?limit=20",
},
next: {
href: "https://horizon-testnet.stellar.org/accounts/G.../operations?cursor=abc123&limit=20",
},
},
_embedded: {
records: [
{
id: "1234567890",
paging_token: "token-1",
type: "payment",
type_int: 1,
created_at: "2024-01-15T10:00:00Z",
transaction_hash: "aabbcc",
transaction_successful: true,
source_account: VALID_ACCOUNT,
asset_type: "native",
amount: "100.0000000",
from: VALID_ACCOUNT,
to: "GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
// Fields that must be stripped:
_links: { self: { href: "..." } },
offer_id: "99",
sponsor: "GSOME_SPONSOR",
funder: "GFUNDER",
},
{
// DEX operation — must be filtered out
id: "999",
paging_token: "token-dex",
type: "manage_sell_offer",
type_int: 3,
created_at: "2024-01-15T09:00:00Z",
transaction_hash: "dex-hash",
transaction_successful: true,
source_account: VALID_ACCOUNT,
offer_id: "42",
price: "1.5",
},
],
},
};
// ── Redis mock ────────────────────────────────────────────────────────────────
function createRedisMock() {
const store = new Map<string, string>();
return {
get: jest.fn(async <T>(key: string): Promise<T | null> => {
const val = store.get(key);
return val ? (JSON.parse(val) as T) : null;
}),
set: jest.fn(async (key: string, value: unknown) => {
store.set(key, JSON.stringify(value));
}),
del: jest.fn(async (key: string) => {
store.delete(key);
}),
delPattern: jest.fn(),
getClient: jest.fn(() => ({
multi: jest.fn(() => ({
zremrangebyscore: jest.fn().mockReturnThis(),
zcard: jest.fn().mockReturnThis(),
zadd: jest.fn().mockReturnThis(),
expire: jest.fn().mockReturnThis(),
exec: jest.fn().mockResolvedValue([
[null, 1],
[null, 0], // zcard returns 0 — under limit
[null, 1],
[null, 1],
]),
})),
zremrangebyscore: jest.fn(),
})),
ping: jest.fn(async () => true),
onModuleDestroy: jest.fn(),
};
}
// ── Test setup ────────────────────────────────────────────────────────────────
describe("HorizonController (integration)", () => {
let app: INestApplication;
let redisMock: ReturnType<typeof createRedisMock>;
let fetchSpy: jest.SpyInstance;
beforeEach(async () => {
redisMock = createRedisMock();
// Mock global fetch — no real Horizon call made
fetchSpy = jest.spyOn(global, "fetch").mockResolvedValue({
ok: true,
status: 200,
json: async () => MOCK_HORIZON_RESPONSE,
} as Response);
// Override STELLAR_NETWORK env so network config does not throw
process.env.STELLAR_NETWORK = "testnet";
process.env.STELLAR_NETWORK_PASSPHRASE = "Test SDF Network ; September 2015";
const moduleRef: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
isGlobal: true,
ignoreEnvFile: true,
load: [
() => ({
REDIS_URL: "redis://mock:6379",
STELLAR_NETWORK: "testnet",
STELLAR_NETWORK_PASSPHRASE: "Test SDF Network ; September 2015",
SOROBAN_RPC_URL: "https://soroban-testnet.stellar.org",
CONTRACT_ID: "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
}),
],
}),
HorizonModule,
],
})
.overrideProvider(RedisService)
.useValue(redisMock)
.compile();
app = moduleRef.createNestApplication();
app.setGlobalPrefix("api");
await app.init();
});
afterEach(async () => {
jest.restoreAllMocks();
await app.close();
});
// ── Field filtering ───────────────────────────────────────────────────────
it("returns only payment operations and strips internal Horizon fields", async () => {
const res = await request(app.getHttpServer())
.get("/api/horizon/transactions")
.query({ account: VALID_ACCOUNT });
expect(res.status).toBe(HttpStatus.OK);
expect(res.body.records).toHaveLength(1); // manage_sell_offer filtered out
const record = res.body.records[0];
// Required fields present
expect(record).toMatchObject({
id: "1234567890",
type: "payment",
amount: "100.0000000",
transaction_successful: true,
});
// Stripped fields must not be present
expect(record).not.toHaveProperty("_links");
expect(record).not.toHaveProperty("offer_id");
expect(record).not.toHaveProperty("sponsor");
expect(record).not.toHaveProperty("funder");
});
it("exposes next_cursor when Horizon provides a next link", async () => {
const res = await request(app.getHttpServer())
.get("/api/horizon/transactions")
.query({ account: VALID_ACCOUNT });
expect(res.status).toBe(HttpStatus.OK);
expect(res.body.next_cursor).toBe("abc123");
});
// ── Response headers must not leak API keys ───────────────────────────────
it("does not expose Authorization or Horizon API key in response headers", async () => {
const res = await request(app.getHttpServer())
.get("/api/horizon/transactions")
.query({ account: VALID_ACCOUNT });
expect(res.headers).not.toHaveProperty("authorization");
expect(res.headers).not.toHaveProperty("x-horizon-api-key");
});
// ── Address validation ────────────────────────────────────────────────────
it("returns 400 for a missing account parameter", async () => {
const res = await request(app.getHttpServer()).get("/api/horizon/transactions");
expect(res.status).toBe(HttpStatus.BAD_REQUEST);
});
it("returns 400 for an invalid Stellar address", async () => {
const res = await request(app.getHttpServer())
.get("/api/horizon/transactions")
.query({ account: "not-a-stellar-address" });
expect(res.status).toBe(HttpStatus.BAD_REQUEST);
});
// ── Cache hit ─────────────────────────────────────────────────────────────
it("serves from cache on second identical request without calling Horizon again", async () => {
await request(app.getHttpServer())
.get("/api/horizon/transactions")
.query({ account: VALID_ACCOUNT });
// Seed the cache with what the first request stored
const cachedValue = { records: [], next_cursor: undefined };
redisMock.get.mockResolvedValueOnce(cachedValue);
await request(app.getHttpServer())
.get("/api/horizon/transactions")
.query({ account: VALID_ACCOUNT });
// fetch should only have been called once (the second served from cache)
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
// ── Rate limiting ─────────────────────────────────────────────────────────
it("returns 429 with Retry-After header when rate limit is exceeded", async () => {
// Make zcard return a count at the limit
redisMock.getClient.mockReturnValue({
multi: jest.fn(() => ({
zremrangebyscore: jest.fn().mockReturnThis(),
zcard: jest.fn().mockReturnThis(),
zadd: jest.fn().mockReturnThis(),
expire: jest.fn().mockReturnThis(),
exec: jest.fn().mockResolvedValue([
[null, 1],
[null, 30], // at limit
[null, 1],
[null, 1],
]),
})),
zremrangebyscore: jest.fn(),
});
const res = await request(app.getHttpServer())
.get("/api/horizon/transactions")
.query({ account: VALID_ACCOUNT });
expect(res.status).toBe(429);
expect(res.headers).toHaveProperty("retry-after");
expect(res.body.error).toBe("Too Many Requests");
});
// ── Horizon upstream failure ──────────────────────────────────────────────
it("returns 502 when Horizon is unreachable", async () => {
fetchSpy.mockRejectedValueOnce(new Error("ECONNREFUSED"));
const res = await request(app.getHttpServer())
.get("/api/horizon/transactions")
.query({ account: VALID_ACCOUNT });
expect(res.status).toBe(HttpStatus.BAD_GATEWAY);
// Error body must not contain any API key
expect(JSON.stringify(res.body)).not.toContain("Bearer");
});
});