Repository navigation
Expand file tree
/
Copy pathtest.integration.js
More file actions
350 lines (316 loc) · 11.6 KB
/
Copy pathtest.integration.js
File metadata and controls
350 lines (316 loc) · 11.6 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
/**
* Integration Tests for QRCodeGenerator PCF Component
* Black-box tests against the real Image-Charts API.
* Based on test scenarios from the Zapier connector.
*/
const crypto = require('crypto');
const https = require('https');
const ACCOUNT_ID = process.env.IMAGE_CHARTS_ACCOUNT_ID;
const SECRET_KEY = process.env.IMAGE_CHARTS_SECRET_KEY;
const PRIVATE_CLOUD_DOMAIN = process.env.IMAGE_CHARTS_PRIVATE_CLOUD_DOMAIN;
const USER_AGENT = process.env.IMAGE_CHARTS_USER_AGENT || 'pcf-image-charts-qrcode/1.0.0-test';
const describeIfCredentials = ACCOUNT_ID && SECRET_KEY ? describe : describe.skip;
const describeIfPrivateCloud = PRIVATE_CLOUD_DOMAIN ? describe : describe.skip;
function computeHmacSha256Sync(secretKey, message) {
return crypto.createHmac('sha256', secretKey).update(message).digest('hex');
}
function buildSignedQRCodeUrl(params) {
const { accountId, secretKey, qrData, chartSize, fgColor, bgColor } = params;
const searchParams = new URLSearchParams();
searchParams.append('cht', 'qr');
searchParams.append('chs', chartSize || '300x300');
searchParams.append('chl', qrData);
searchParams.append('choe', 'UTF-8');
if (fgColor) searchParams.append('icqrf', fgColor);
if (bgColor) searchParams.append('icqrb', bgColor);
searchParams.append('icac', accountId);
const signature = computeHmacSha256Sync(secretKey, searchParams.toString());
searchParams.append('ichm', signature);
return 'https://image-charts.com/chart?' + searchParams.toString();
}
function buildPrivateCloudQRCodeUrl(params) {
const { domain, qrData, chartSize, fgColor, bgColor } = params;
const searchParams = new URLSearchParams();
searchParams.append('cht', 'qr');
searchParams.append('chs', chartSize || '300x300');
searchParams.append('chl', qrData);
searchParams.append('choe', 'UTF-8');
if (fgColor) searchParams.append('icqrf', fgColor);
if (bgColor) searchParams.append('icqrb', bgColor);
const baseUrl = domain.endsWith('/') ? domain.slice(0, -1) : domain;
return baseUrl + '/chart?' + searchParams.toString();
}
function fetchUrl(url) {
return new Promise((resolve, reject) => {
const req = https.get(url, { headers: { 'User-Agent': USER_AGENT } }, (res) => {
const chunks = [];
res.on('data', chunk => chunks.push(chunk));
res.on('end', () => {
resolve({ statusCode: res.statusCode, headers: res.headers, body: Buffer.concat(chunks) });
});
});
req.on('error', reject);
req.setTimeout(10000, () => { req.destroy(); reject(new Error('Request timeout')); });
});
}
// ============================================================
// URL Generation Tests
// ============================================================
describe('URL Generation', () => {
const testAccountId = 'test_account';
const testSecretKey = 'test_secret_key_123';
test('should generate correct URL structure for QR code', () => {
const url = buildSignedQRCodeUrl({
accountId: testAccountId,
secretKey: testSecretKey,
qrData: 'Hello world',
chartSize: '150x150'
});
expect(url).toContain('cht=qr');
expect(url).toContain('chs=150x150');
expect(url).toContain('chl=Hello+world');
expect(url).toContain('choe=UTF-8');
expect(url).toContain('icac=' + testAccountId);
expect(url).toContain('ichm=');
});
test('should include foreground color when provided', () => {
const url = buildSignedQRCodeUrl({
accountId: testAccountId,
secretKey: testSecretKey,
qrData: 'Test',
fgColor: 'FF0000'
});
expect(url).toContain('icqrf=FF0000');
});
test('should include background color when provided', () => {
const url = buildSignedQRCodeUrl({
accountId: testAccountId,
secretKey: testSecretKey,
qrData: 'Test',
bgColor: 'FFFF00'
});
expect(url).toContain('icqrb=FFFF00');
});
test('should include both foreground and background colors', () => {
const url = buildSignedQRCodeUrl({
accountId: testAccountId,
secretKey: testSecretKey,
qrData: 'ColorTest',
fgColor: 'FF0000',
bgColor: 'FFFF00'
});
expect(url).toContain('icqrf=FF0000');
expect(url).toContain('icqrb=FFFF00');
});
test('HMAC signature should be deterministic', () => {
const params = {
accountId: testAccountId,
secretKey: testSecretKey,
qrData: 'Test QR Code',
chartSize: '300x300'
};
const url1 = buildSignedQRCodeUrl(params);
const url2 = buildSignedQRCodeUrl(params);
expect(url1).toBe(url2);
});
test('Private Cloud URL should not include icac or ichm', () => {
const url = buildPrivateCloudQRCodeUrl({
domain: 'https://private.example.com',
qrData: 'Test',
chartSize: '200x200'
});
expect(url).not.toContain('icac=');
expect(url).not.toContain('ichm=');
});
});
// ============================================================
// API Integration Tests - Enterprise Mode
// ============================================================
describeIfCredentials('Enterprise Mode - QR Codes', () => {
test('should return 200 for basic QR code (matching Zapier scenario)', () => {
const url = buildSignedQRCodeUrl({
accountId: ACCOUNT_ID,
secretKey: SECRET_KEY,
qrData: 'Hello world',
chartSize: '150x150'
});
return fetchUrl(url).then((response) => {
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toMatch(/^image\//);
});
}, 15000);
test('should return 200 for QR code with URL data', () => {
const url = buildSignedQRCodeUrl({
accountId: ACCOUNT_ID,
secretKey: SECRET_KEY,
qrData: 'https://example.com/path?query=value',
chartSize: '300x300'
});
return fetchUrl(url).then((response) => {
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toMatch(/^image\//);
});
}, 15000);
test('should return 200 for QR code with custom colors', () => {
const url = buildSignedQRCodeUrl({
accountId: ACCOUNT_ID,
secretKey: SECRET_KEY,
qrData: 'ColorTest',
fgColor: 'FF0000',
bgColor: 'FFFF00'
});
return fetchUrl(url).then((response) => {
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toMatch(/^image\//);
});
}, 15000);
test('should return image without watermark', () => {
const url = buildSignedQRCodeUrl({
accountId: ACCOUNT_ID,
secretKey: SECRET_KEY,
qrData: 'Test QR Code',
chartSize: '200x200'
});
return fetchUrl(url).then((response) => {
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toMatch(/^image\//);
expect(response.body.length).toBeGreaterThan(0);
});
}, 15000);
test('should handle long text data', () => {
const longText = 'This is a longer text message that will be encoded into a QR code for testing purposes.';
const url = buildSignedQRCodeUrl({
accountId: ACCOUNT_ID,
secretKey: SECRET_KEY,
qrData: longText,
chartSize: '400x400'
});
return fetchUrl(url).then((response) => {
expect(response.statusCode).toBe(200);
});
}, 15000);
test('should return 403 for invalid signature', () => {
const url = 'https://image-charts.com/chart?cht=qr&chs=300x300&chl=Test&choe=UTF-8&icac=' + ACCOUNT_ID + '&ichm=invalid_signature';
return fetchUrl(url).then((response) => {
expect([400, 403]).toContain(response.statusCode);
});
}, 15000);
});
// ============================================================
// Private Cloud Mode Tests
// ============================================================
describeIfPrivateCloud('Private Cloud Mode - QR Codes', () => {
test('should return 200 for basic QR code (matching Zapier scenario)', () => {
const url = buildPrivateCloudQRCodeUrl({
domain: PRIVATE_CLOUD_DOMAIN,
qrData: 'Hello world',
chartSize: '150x150'
});
return fetchUrl(url).then((response) => {
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toMatch(/^image\//);
});
}, 15000);
test('should return 200 for QR code with URL data', () => {
const url = buildPrivateCloudQRCodeUrl({
domain: PRIVATE_CLOUD_DOMAIN,
qrData: 'https://example.com/path?query=value',
chartSize: '300x300'
});
return fetchUrl(url).then((response) => {
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toMatch(/^image\//);
});
}, 15000);
test('should return 200 for QR code with custom colors', () => {
const url = buildPrivateCloudQRCodeUrl({
domain: PRIVATE_CLOUD_DOMAIN,
qrData: 'ColorTest',
fgColor: 'FF0000',
bgColor: 'FFFF00'
});
return fetchUrl(url).then((response) => {
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toMatch(/^image\//);
});
}, 15000);
test('should return image without watermark', () => {
const url = buildPrivateCloudQRCodeUrl({
domain: PRIVATE_CLOUD_DOMAIN,
qrData: 'Test QR Code',
chartSize: '200x200'
});
return fetchUrl(url).then((response) => {
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toMatch(/^image\//);
expect(response.body.length).toBeGreaterThan(0);
});
}, 15000);
test('should handle long text data', () => {
const longText = 'This is a longer text message that will be encoded into a QR code for testing purposes.';
const url = buildPrivateCloudQRCodeUrl({
domain: PRIVATE_CLOUD_DOMAIN,
qrData: longText,
chartSize: '400x400'
});
return fetchUrl(url).then((response) => {
expect(response.statusCode).toBe(200);
});
}, 15000);
});
// ============================================================
// Error Handling Tests
// ============================================================
describeIfCredentials('Error Handling - Enterprise', () => {
test('should handle missing icac parameter', () => {
const url = 'https://image-charts.com/chart?cht=qr&chs=300x300&chl=Test&choe=UTF-8';
return fetchUrl(url).then((response) => {
expect([200, 400]).toContain(response.statusCode);
});
}, 15000);
test('should handle invalid chart size', () => {
const url = buildSignedQRCodeUrl({
accountId: ACCOUNT_ID,
secretKey: SECRET_KEY,
qrData: 'Test',
chartSize: 'invalid'
});
return fetchUrl(url).then((response) => {
expect([400, 200]).toContain(response.statusCode);
});
}, 15000);
});
// ============================================================
// Performance Tests
// ============================================================
describeIfCredentials('Performance - Enterprise', () => {
test('should respond within 5 seconds', () => {
const startTime = Date.now();
const url = buildSignedQRCodeUrl({
accountId: ACCOUNT_ID,
secretKey: SECRET_KEY,
qrData: 'Performance Test',
chartSize: '300x300'
});
return fetchUrl(url).then((response) => {
const duration = Date.now() - startTime;
expect(response.statusCode).toBe(200);
expect(duration).toBeLessThan(5000);
});
}, 10000);
});
describeIfPrivateCloud('Performance - Private Cloud', () => {
test('should respond within 5 seconds', () => {
const startTime = Date.now();
const url = buildPrivateCloudQRCodeUrl({
domain: PRIVATE_CLOUD_DOMAIN,
qrData: 'Performance Test',
chartSize: '300x300'
});
return fetchUrl(url).then((response) => {
const duration = Date.now() - startTime;
expect(response.statusCode).toBe(200);
expect(duration).toBeLessThan(5000);
});
}, 10000);
});