-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonSchema.test.js
69 lines (53 loc) · 2.44 KB
/
jsonSchema.test.js
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
const cveStructureValidator = require('../src/validator//validate');
const invalidJson = require('./test_CVEs/invalid_CVE.json')
const advanceValidJson = require('./test_CVEs/advance_valid_CVE.json')
const basicValidJson = require('./test_CVEs/basic_valid_CVE.json')
describe('CVE Structure Validator', () => {
it('should validate a basic minimum required valid JSON structure', async () => {
const validData = basicValidJson
await cveStructureValidator(JSON.stringify(validData), (error, result) => {
expect(error).toBe(undefined);
expect(result).toBe('JSON validated, CVE data is in specified structure and contains all the necessary');
});
});
it('should validate a advance valid JSON structure', async () => {
const validData = advanceValidJson
await cveStructureValidator(JSON.stringify(validData), (error, result) => {
expect(error).toBe(undefined);
expect(result).toBe('JSON validated, CVE data is in specified structure and contains all the necessary');
});
});
it('should report errors for invalid JSON structure', async () => {
const invalidData = invalidJson
await cveStructureValidator(JSON.stringify(invalidData), (error, result) => {
expect(error).toContain('Invalid');
expect(result).toBe(undefined);
});
});
it('should handle undefined file content', async () => {
await cveStructureValidator(undefined, (error, result) => {
expect(error).toBe('Can not read the CVE JSON file');
expect(result).toBe(undefined);
});
});
it('should handle JSON parsing error', async () => {
const invalidJson = 'invalid_json';
const originalJSONParse = JSON.parse;
JSON.parse = jest.fn().mockImplementation(() => {
});
await cveStructureValidator(invalidJson, (error, result) => {
expect(error).toMatch(/invalid/i)
expect(result).toBe(undefined);
});
JSON.parse = originalJSONParse;
});
it('should handle validation failure', async () => {
const invalidData = invalidJson
JSON.parse = jest.fn().mockReturnValue(invalidData);
await cveStructureValidator(JSON.stringify(invalidData), (error, result) => {
expect(error).toContain('Invalid');
expect(result).toBe(undefined);
});
JSON.parse.mockRestore();
});
});