-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreserveId.test.js
99 lines (74 loc) · 2.93 KB
/
reserveId.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
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
const axios = require('axios');
const reserveCveId = require('../src/reserveId');
jest.mock('@actions/core', () => ({
getInput: jest.fn().mockReturnValueOnce('your_api_key')
.mockReturnValueOnce('your_api_user')
.mockReturnValueOnce('your_api_org'),
}));
jest.mock('axios');
describe('CVE ID Reservation', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should reserve a CVE ID', async () => {
axios.post.mockResolvedValueOnce({
data: {
cve_ids: [{ cve_id: 'CVE-2023-12345' }],
},
});
const callbackMock = jest.fn();
await reserveCveId(true, 1, callbackMock);
expect(axios.post).toHaveBeenCalledWith(expect.stringContaining('https://cveawg-test.mitre.org/api/cve-id'), null, {
headers: {
'CVE-API-ORG': 'your_api_org',
'CVE-API-USER': 'your_api_user',
'CVE-API-KEY': 'your_api_key',
},
});
expect(callbackMock).toHaveBeenCalledWith('CVE-2023-12345');
});
it('should handle no data received', async () => {
const callbackMock = jest.fn();
const result = await reserveCveId(false, 1, callbackMock);
expect(result).toBeInstanceOf(Error);
expect(result.message).toBe('No data recieved, send CVE data in order to reserve the CVE ID');
expect(callbackMock).not.toHaveBeenCalled();
});
it('should handle axios.post error', async () => {
axios.post.mockRejectedValueOnce(new Error('API error'));
const callbackMock = jest.fn();
await reserveCveId(true, 1, callbackMock);
expect(callbackMock).not.toHaveBeenCalled();
});
it('should handle axios.post rejection', async () => {
axios.post.mockRejectedValueOnce(new Error('API error'));
const callbackMock = jest.fn();
await reserveCveId(true, 1, callbackMock);
expect(callbackMock).not.toHaveBeenCalled();
});
it('should handle axios.post exception', async () => {
axios.post.mockImplementationOnce(() => {
throw new Error('Exception in axios.post');
});
const callbackMock = jest.fn();
await reserveCveId(true, 1, callbackMock);
expect(callbackMock).not.toHaveBeenCalled();
});
it('should handle callback error', async () => {
axios.post.mockResolvedValueOnce({
data: {
cve_ids: [{ cve_id: 'CVE-2023-12345' }],
},
});
const callbackMock = jest.fn(() => {
throw new Error('Callback error');
});
await reserveCveId(true, 1, callbackMock);
expect(callbackMock).toHaveBeenCalledWith('CVE-2023-12345');
});
it('should handle invalid type for amount', async () => {
const callbackMock = jest.fn();
await reserveCveId(true, 'two', callbackMock);
expect(callbackMock).not.toHaveBeenCalled();
});
});