Skip to content

Commit 5d39edd

Browse files
committed
refactor mock
1 parent b52723b commit 5d39edd

File tree

1 file changed

+48
-66
lines changed

1 file changed

+48
-66
lines changed

spec/mocks/s3adapter.js

Lines changed: 48 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,56 @@ const S3Adapter = require('../../index.js');
33
const { GetObjectCommand, PutObjectCommand, DeleteObjectCommand } = require('@aws-sdk/client-s3');
44

55
function getMockS3Adapter(options) {
6-
let s3;
7-
8-
if (
9-
process.env.TEST_S3_ACCESS_KEY &&
10-
process.env.TEST_S3_SECRET_KEY &&
11-
process.env.TEST_S3_BUCKET
12-
) {
13-
// Should be initialized from the env
14-
s3 = new S3Adapter(
15-
Object.assign(
16-
{
17-
accessKey: process.env.TEST_S3_ACCESS_KEY,
18-
secretKey: process.env.TEST_S3_SECRET_KEY,
19-
bucket: process.env.TEST_S3_BUCKET,
20-
region: process.env.TEST_S3_REGION,
21-
},
22-
options
23-
)
24-
);
25-
} else {
26-
const bucket = 'FAKE_BUCKET';
27-
const region = 'us-east-1';
28-
29-
s3 = new S3Adapter('FAKE_ACCESS_KEY', 'FAKE_SECRET_KEY', bucket, options);
30-
31-
const objects = {};
32-
33-
s3._s3Client = {
34-
send: command => {
35-
if (command instanceof PutObjectCommand) {
36-
const { Key, Body } = command.input;
37-
38-
objects[Key] = Body;
39-
40-
return Promise.resolve({
41-
Location: `https://${bucket}.s3.${region}.amazonaws.com/${Key}`,
42-
});
6+
const accessKey = process.env.TEST_S3_ACCESS_KEY || 'ACCESS_KEY';
7+
const secretKey = process.env.TEST_S3_SECRET_KEY || 'SECRET_KEY';
8+
const bucket = process.env.TEST_S3_BUCKET || 'BUCKET';
9+
10+
const s3 = new S3Adapter(Object.assign({
11+
accessKey,
12+
secretKey,
13+
bucket,
14+
}, options));
15+
16+
const objects = {};
17+
18+
s3._s3Client = {
19+
// @ts-ignore
20+
send: command => {
21+
if (command instanceof PutObjectCommand) {
22+
const { Key, Body } = command.input;
23+
objects[Key] = Body;
24+
return Promise.resolve({ Location: `https://${bucket}.s3.${region}.amazonaws.com/${Key}` });
25+
}
26+
27+
if (command instanceof DeleteObjectCommand) {
28+
const { Key } = command.input;
29+
delete objects[Key];
30+
return Promise.resolve({});
31+
}
32+
33+
if (command instanceof GetObjectCommand) {
34+
const { Key } = command.input;
35+
36+
if (objects[Key]) {
37+
const stream = new Readable();
38+
stream.push('hello world');
39+
// End of stream
40+
stream.push(null);
41+
return {
42+
Body: stream,
43+
AcceptRanges: 'bytes',
44+
ContentLength: 36,
45+
ContentRange: 'bytes 0-35/36',
46+
ContentType: 'text/plain',
47+
};
48+
} else {
49+
return Promise.reject(new Error('Not found'));
4350
}
44-
if (command instanceof DeleteObjectCommand) {
45-
const { Key } = command.input;
51+
}
52+
return Promise.resolve();
53+
},
54+
};
4655

47-
delete objects[Key];
48-
49-
return Promise.resolve({});
50-
}
51-
if (command instanceof GetObjectCommand) {
52-
const { Key } = command.input;
53-
54-
if (objects[Key]) {
55-
const stream = new Readable();
56-
stream.push('hello world');
57-
stream.push(null); // End of stream
58-
59-
return {
60-
Body: stream,
61-
AcceptRanges: 'bytes',
62-
ContentLength: 36,
63-
ContentRange: 'bytes 0-35/36',
64-
ContentType: 'text/plain',
65-
};
66-
} else {
67-
return Promise.reject(new Error('Not found'));
68-
}
69-
}
70-
return Promise.resolve();
71-
},
72-
};
73-
}
7456
return s3;
7557
}
7658

0 commit comments

Comments
 (0)