-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathingest-manifest.helper.test.ts
More file actions
366 lines (341 loc) · 14.7 KB
/
ingest-manifest.helper.test.ts
File metadata and controls
366 lines (341 loc) · 14.7 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
import { describe, expect, it } from 'vitest';
import {
DocumentSourceType,
IntegrationSubType,
IntegrationType,
} from '../../__generated__/resolvers-types';
import { OPENCTI_INTEGRATION_DOCUMENT_TYPE } from '../services/document/opencti/integrations/integrations.model';
import {
extractManifestInformation,
ManifestExtractionResult,
} from './ingest-manifest.helper';
import sampleManifest from './test/sample-manifest.json';
describe('Ingest manifest helper', () => {
describe('extractManifestInfo', () => {
describe('with valid manifest data', () => {
it('should extract manifest information from valid JSON', () => {
const result = extractManifestInformation(sampleManifest);
expect(result.validContracts).toHaveLength(2);
expect(result.errors).toHaveLength(0);
expect(result.validContracts[0]).toEqual({
product_version: '1.0.0',
name: 'Contract One',
slug: 'contract-one',
description: 'This is the first contract',
short_description: 'First contract',
logo: 'data:image/png;base64,abc123',
use_cases: ['automation', 'integration'],
verified: true,
container_image: 'docker.io/example/image:latest',
integration_subtype: IntegrationSubType.InternalEnrichment,
source_code: 'https://github.com/example/repo',
subscription_link: 'https://example.com/subscribe',
type: OPENCTI_INTEGRATION_DOCUMENT_TYPE,
integration_type: IntegrationType.Connector,
manager_supported: true,
playbook_supported: false,
source_type: DocumentSourceType.External,
service_instance_id: '0f4aad4b-bdd6-4084-8b1f-82c9c66578cc',
});
expect(result.validContracts[1]).toEqual({
product_version: '1.0.0',
name: 'Contract Two',
slug: 'contract-two',
description: 'This is the second contract',
short_description: 'Second contract',
logo: 'https://example.com/logo.png',
use_cases: ['monitoring'],
verified: false,
container_image: 'docker.io/example/image2:latest',
integration_subtype: IntegrationSubType.ExternalImport,
source_code: 'https://github.com/example/repo2',
subscription_link: 'https://example.com/subscribe2',
type: OPENCTI_INTEGRATION_DOCUMENT_TYPE,
integration_type: IntegrationType.Connector,
manager_supported: false,
playbook_supported: true,
source_type: DocumentSourceType.External,
service_instance_id: '0f4aad4b-bdd6-4084-8b1f-82c9c66578cc',
});
});
it('should return correct ManifestInformation type with expected properties', () => {
const result: ManifestExtractionResult =
extractManifestInformation(sampleManifest);
expect(result.validContracts).toBeDefined();
expect(result.validContracts.length).toBeGreaterThan(0);
const firstItem = result.validContracts[0];
expect(firstItem).toBeTruthy();
if (!firstItem) {
return;
}
expect(firstItem).toHaveProperty('product_version');
expect(typeof firstItem.product_version).toBe('string');
expect(firstItem).toHaveProperty('name');
expect(typeof firstItem.name).toBe('string');
expect(firstItem).toHaveProperty('description');
expect(typeof firstItem.description).toBe('string');
expect(firstItem).toHaveProperty('short_description');
expect(typeof firstItem.short_description).toBe('string');
expect(firstItem).toHaveProperty('container_image');
expect(typeof firstItem.container_image).toBe('string');
expect(firstItem).toHaveProperty('slug');
expect(typeof firstItem.slug).toBe('string');
expect(firstItem).toHaveProperty('logo');
expect(typeof firstItem.logo).toBe('string');
expect(firstItem).toHaveProperty('verified');
expect(typeof firstItem.verified).toBe('boolean');
expect(firstItem).toHaveProperty('integration_subtype');
expect(typeof firstItem.integration_subtype).toBe('string');
expect(firstItem).toHaveProperty('integration_type');
expect(typeof firstItem.integration_type).toBe('string');
expect(firstItem).toHaveProperty('type');
expect(typeof firstItem.type).toBe('string');
expect(firstItem).toHaveProperty('use_cases');
expect(Array.isArray(firstItem.use_cases)).toBe(true);
expect(firstItem).toHaveProperty('manager_supported');
expect(typeof firstItem.manager_supported).toBe('boolean');
expect(firstItem).toHaveProperty('playbook_supported');
expect(typeof firstItem.playbook_supported).toBe('boolean');
expect(typeof firstItem.source_code).toBe('string');
if (firstItem.subscription_link !== undefined) {
expect(typeof firstItem.subscription_link).toBe('string');
}
});
});
describe('with invalid manifest data', () => {
it('should return empty validContracts and error for invalid data structure', () => {
const invalidData = {
data: {
id: 'test',
},
};
const result = extractManifestInformation(invalidData);
expect(result.validContracts).toEqual([]);
expect(result.errors.length).toBeGreaterThan(0);
expect(result.errors[0]?.error).toContain('Manifest structure invalid');
});
it('should return empty validContracts for missing required manifest fields', () => {
const invalidManifest = {
contracts: [
{
title: 'Test Contract',
},
],
};
const result = extractManifestInformation(invalidManifest);
expect(result.validContracts).toEqual([]);
expect(result.errors.length).toBeGreaterThan(0);
});
it('should return empty validContracts for null input', () => {
const result = extractManifestInformation(null);
expect(result.validContracts).toEqual([]);
expect(result.errors.length).toBeGreaterThan(0);
});
it('should return empty validContracts for undefined input', () => {
const result = extractManifestInformation(undefined);
expect(result.validContracts).toEqual([]);
expect(result.errors.length).toBeGreaterThan(0);
});
it('should return empty validContracts for non-object input', () => {
const result = extractManifestInformation('not an object');
expect(result.validContracts).toEqual([]);
expect(result.errors.length).toBeGreaterThan(0);
});
});
describe('with empty contracts array', () => {
it('should return empty validContracts when contracts is empty', () => {
const emptyContractsManifest = {
id: 'test-manifest',
name: 'Test Manifest',
description: 'Test description',
version: '1.0.0',
contracts: [],
};
const result = extractManifestInformation(emptyContractsManifest);
expect(result.validContracts).toEqual([]);
expect(result.errors).toEqual([]);
});
});
describe('with mixed valid and invalid contracts (fail-safe behavior)', () => {
it('should process valid contracts and report errors for invalid ones', () => {
const mixedManifest = {
id: 'mixed-manifest',
name: 'Mixed Manifest',
description: 'Manifest with both valid and invalid contracts',
version: '2.0.0',
contracts: [
// Valid contract
{
title: 'Valid Contract',
slug: 'valid-contract',
description: 'This is a valid contract',
short_description: 'Valid contract',
logo: 'https://example.com/logo.png',
use_cases: ['security', 'monitoring'],
verified: true,
container_image: 'docker.io/example/valid:latest',
container_type: IntegrationSubType.InternalExportFile,
source_code: 'https://github.com/example/valid',
subscription_link: 'https://example.com/subscribe',
manager_supported: true,
playbook_supported: false,
},
// Invalid contract - missing required fields
{
title: 'Invalid Contract Missing Fields',
slug: 'invalid-missing',
description: 'Missing required fields',
// Missing: short_description, logo, use_cases, etc.
},
// Invalid contract - wrong types
{
title: 'Invalid Contract Wrong Types',
slug: 'invalid-types',
description: 'Has wrong field types',
short_description: 'Wrong types',
logo: 'https://example.com/logo2.png',
use_cases: 'should-be-array', // Wrong type
verified: 'yes', // Wrong type
container_image: 'docker.io/example/invalid:latest',
container_type: IntegrationSubType.ExternalImport,
source_code: 'not-a-url', // Invalid URL
subscription_link: '',
manager_supported: true,
playbook_supported: false,
},
// Another valid contract
{
title: 'Another Valid Contract',
slug: 'another-valid',
description: 'This is another valid contract',
short_description: 'Another valid',
logo: 'https://example.com/logo3.png',
use_cases: ['automation'],
verified: false,
container_image: 'docker.io/example/valid2:latest',
container_type: IntegrationSubType.Stream,
source_code: 'https://github.com/example/valid2',
subscription_link: '',
manager_supported: false,
playbook_supported: true,
},
],
};
const result = extractManifestInformation(mixedManifest);
// Should have processed 2 valid contracts
expect(result.validContracts).toHaveLength(2);
expect(result.validContracts[0]?.name).toBe('Valid Contract');
expect(result.validContracts[0]?.slug).toBe('valid-contract');
expect(result.validContracts[1]?.name).toBe('Another Valid Contract');
expect(result.validContracts[1]?.slug).toBe('another-valid');
// Should have 2 errors for invalid contracts
expect(result.errors).toHaveLength(2);
// Check first error
expect(result.errors[0]?.contractTitle).toBe(
'Invalid Contract Missing Fields'
);
expect(result.errors[0]?.contractSlug).toBe('invalid-missing');
expect(result.errors[0]?.error).toContain(
'expected string, received undefined'
);
// Check second error
expect(result.errors[1]?.contractTitle).toBe(
'Invalid Contract Wrong Types'
);
expect(result.errors[1]?.contractSlug).toBe('invalid-types');
expect(result.errors[1]?.error).toBeDefined();
});
it('should handle contracts with partial identifiers in errors', () => {
const partialManifest = {
id: 'partial-manifest',
name: 'Partial Manifest',
description: 'Manifest with partial contract data',
version: '1.0.0',
contracts: [
// Contract with only title
{
title: 'Only Title Contract',
// Missing everything else
},
// Contract with only slug
{
slug: 'only-slug-contract',
// Missing everything else
},
// Contract with neither title nor slug
{
description: 'No identifiers',
// Missing title and slug
},
],
};
const result = extractManifestInformation(partialManifest);
// All contracts should be invalid
expect(result.validContracts).toHaveLength(0);
expect(result.errors).toHaveLength(3);
// Check error identifiers
expect(result.errors[0]?.contractTitle).toBe('Only Title Contract');
expect(result.errors[0]?.contractSlug).toBe('Unknown');
expect(result.errors[1]?.contractTitle).toBe('Unknown');
expect(result.errors[1]?.contractSlug).toBe('only-slug-contract');
expect(result.errors[2]?.contractTitle).toBe('Unknown');
expect(result.errors[2]?.contractSlug).toBe('Unknown');
});
it('should continue processing after encountering an invalid contract', () => {
const sequentialManifest = {
id: 'sequential-manifest',
name: 'Sequential Manifest',
description: 'Test processing continues after errors',
version: '1.0.0',
contracts: [
// Valid
{
title: 'First Valid',
slug: 'first-valid',
description: 'First valid contract',
short_description: 'First',
logo: 'https://example.com/1.png',
use_cases: ['test'],
verified: true,
container_image: 'docker.io/first:latest',
container_type: IntegrationSubType.InternalEnrichment,
source_code: 'https://github.com/example/first',
subscription_link: '',
manager_supported: true,
playbook_supported: false,
},
// Invalid
{
title: 'Invalid Middle',
// Missing required fields
},
// Valid
{
title: 'Last Valid',
slug: 'last-valid',
description: 'Last valid contract',
short_description: 'Last',
logo: 'https://example.com/3.png',
use_cases: ['test'],
verified: false,
container_image: 'docker.io/last:latest',
container_type: IntegrationSubType.Stream,
source_code: 'https://github.com/example/last',
subscription_link: '',
manager_supported: false,
playbook_supported: true,
},
],
};
const result = extractManifestInformation(sequentialManifest);
// Should process both valid contracts despite invalid one in middle
expect(result.validContracts).toHaveLength(2);
expect(result.validContracts[0]?.name).toBe('First Valid');
expect(result.validContracts[1]?.name).toBe('Last Valid');
// Should have 1 error for the invalid contract
expect(result.errors).toHaveLength(1);
expect(result.errors[0]?.contractTitle).toBe('Invalid Middle');
});
});
});
});