forked from sutt0n/serverless-clamav-lambda-layer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.test.js
129 lines (105 loc) · 3.11 KB
/
handler.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
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
const { virusScan } = require('./handler');
var mockPutObjectTagging;
var mockPromise;
jest.mock('aws-sdk', () => {
mockPromise = jest.fn();
mockPutObjectTagging = jest.fn().mockReturnThis();
return {
S3: jest.fn().mockReturnValue({
getObject: jest.fn().mockReturnThis(),
putObjectTagging: mockPutObjectTagging,
promise: mockPromise,
})
}
});
var mockReadFileSync;
var mockWriteFileSync;
var mockUnlinkSync;
jest.mock('fs', () => {
mockReadFileSync = jest.fn();
mockWriteFileSync = jest.fn();
mockUnlinkSync = jest.fn();
return {
readFileSync: mockReadFileSync,
writeFileSync: mockWriteFileSync,
unlinkSync: mockUnlinkSync
}
});
var mockExecSync;
jest.mock('child_process', () => {
mockExecSync = jest.fn();
return {
execSync: mockExecSync,
}
})
describe('handler.virusScan', () => {
let mockValidEvent;
beforeAll(() => {
// console.log = jest.fn();
mockValidEvent = {
Records: [
{
s3: {
bucket: { name: 'bucket-name' },
object: { key: 'file-to-scan.jpeg' },
},
},
]
};
});
it('calls an event with no event.Records', async () => {
const event = {};
await virusScan(event);
// expect(console.log).toHaveBeenCalled();
// expect(console.log).toHaveBeenCalledWith('Not an S3 event invocation!');
expect(mockWriteFileSync).not.toHaveBeenCalled();
});
it('calls an event with no event.Records[].s3', async () => {
const event = {
Records: [
{
notS3: { foo: 'bar' }
}
]
};
await virusScan(event);
// expect(console.log).toHaveBeenCalled();
// expect(console.log).toHaveBeenCalledWith('Not an S3 Record!');
expect(mockWriteFileSync).not.toHaveBeenCalled();
});
it('calls with a clean file', async () => {
mockPromise.mockImplementationOnce(() => ({ Body: 'body' }));
await virusScan(mockValidEvent);
expect(mockExecSync).toHaveBeenCalledWith('clamscan /tmp/file-to-scan.jpeg');
expect(mockWriteFileSync).toHaveBeenCalledWith("/tmp/file-to-scan.jpeg", 'body');
expect(mockUnlinkSync).toHaveBeenCalledWith('/tmp/file-to-scan.jpeg');
expect(mockPutObjectTagging).toHaveBeenCalledWith({
'Bucket': 'bucket-name',
'Key': 'file-to-scan.jpeg',
'Tagging': {
'TagSet': [
{'Key': 'av-status', 'Value': 'clean'}
]
}
});
});
it.only('calls with a virus file', async () => {
mockPromise.mockImplementationOnce(() => ({ Body: 'body' }));
mockExecSync.mockImplementationOnce(() => {
throw { code: 1 };
});
await virusScan(mockValidEvent);
expect(mockExecSync).toHaveBeenCalledWith('clamscan /tmp/file-to-scan.jpeg');
expect(mockWriteFileSync).toHaveBeenCalledWith("/tmp/file-to-scan.jpeg", 'body');
expect(mockUnlinkSync).toHaveBeenCalledWith('/tmp/file-to-scan.jpeg');
expect(mockPutObjectTagging).toHaveBeenCalledWith({
'Bucket': 'bucket-name',
'Key': 'file-to-scan.jpeg',
'Tagging': {
'TagSet': [
{'Key': 'av-status', 'Value': 'dirty'}
]
}
});
});
});