Skip to content

Commit 70e55db

Browse files
committed
fix ts errors
1 parent 0b7ea2a commit 70e55db

File tree

3 files changed

+63
-73
lines changed

3 files changed

+63
-73
lines changed

spec/mocks/s3adapter-v2.js

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,56 @@
11
const S3Adapter = require('../../index.js');
22

3-
function makeS3Adapter(options) {
4-
let s3;
3+
function getMockS3Adapter(options) {
4+
const accessKey = process.env.TEST_S3_ACCESS_KEY || 'ACCESS_KEY';
5+
const secretKey = process.env.TEST_S3_SECRET_KEY || 'SECRET_KEY';
6+
const bucket = process.env.TEST_S3_BUCKET || 'BUCKET';
7+
8+
const s3 = new S3Adapter(Object.assign({
9+
accessKey,
10+
secretKey,
11+
bucket,
12+
}, options));
13+
14+
const objects = {};
15+
16+
s3._s3Client = {
17+
createBucket: callback =>
18+
// @ts-ignore
19+
setTimeout(callback, 100),
20+
21+
upload: (params, callback) =>
22+
// @ts-ignore
23+
setTimeout(() => {
24+
const { Key, Body } = params;
25+
objects[Key] = Body;
26+
callback(null, { Location: `https://${bucket}.s3.amazonaws.com/${Key}` });
27+
}, 100),
28+
29+
deleteObject: (params, callback) =>
30+
// @ts-ignore
31+
setTimeout(() => {
32+
const { Key } = params;
33+
delete objects[Key];
34+
// @ts-ignore
35+
callback(null, {});
36+
}, 100),
37+
38+
getObject: (params, callback) =>
39+
// @ts-ignore
40+
setTimeout(() => {
41+
const { Key } = params;
42+
43+
if (objects[Key]) {
44+
// @ts-ignore
45+
callback(null, { Body: Buffer.from(objects[Key], 'utf8') });
46+
} else {
47+
// @ts-ignore
48+
callback(new Error('Not found'));
49+
}
50+
}, 100),
51+
};
552

6-
if (
7-
process.env.TEST_S3_ACCESS_KEY &&
8-
process.env.TEST_S3_SECRET_KEY &&
9-
process.env.TEST_S3_BUCKET
10-
) {
11-
// Should be initialized from the env
12-
s3 = new S3Adapter(
13-
Object.assign(
14-
{
15-
accessKey: process.env.TEST_S3_ACCESS_KEY,
16-
secretKey: process.env.TEST_S3_SECRET_KEY,
17-
bucket: process.env.TEST_S3_BUCKET,
18-
},
19-
options
20-
)
21-
);
22-
} else {
23-
const bucket = 'FAKE_BUCKET';
24-
25-
s3 = new S3Adapter('FAKE_ACCESS_KEY', 'FAKE_SECRET_KEY', bucket, options);
26-
27-
const objects = {};
28-
29-
s3._s3Client = {
30-
createBucket: callback => setTimeout(callback, 100),
31-
upload: (params, callback) =>
32-
setTimeout(() => {
33-
const { Key, Body } = params;
34-
35-
objects[Key] = Body;
36-
37-
callback(null, {
38-
Location: `https://${bucket}.s3.amazonaws.com/${Key}`,
39-
});
40-
}, 100),
41-
deleteObject: (params, callback) =>
42-
setTimeout(() => {
43-
const { Key } = params;
44-
45-
delete objects[Key];
46-
47-
callback(null, {});
48-
}, 100),
49-
getObject: (params, callback) =>
50-
setTimeout(() => {
51-
const { Key } = params;
52-
53-
if (objects[Key]) {
54-
callback(null, {
55-
Body: Buffer.from(objects[Key], 'utf8'),
56-
});
57-
} else {
58-
callback(new Error('Not found'));
59-
}
60-
}, 100),
61-
};
62-
}
6353
return s3;
6454
}
6555

66-
module.exports = { makeS3Adapter };
56+
module.exports = { getMockS3Adapter };

spec/support/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const { ParseServer } = require('parse-server');
22
const express = require('express');
33
const http = require('http');
4-
const { makeS3Adapter } = require('../mocks/s3adapter-v2');
4+
const { getMockS3Adapter } = require('../mocks/s3adapter-v2');
55
const Config = require('../../node_modules/parse-server/lib/Config.js');
66

77
const expressApp = express();
8-
const S3Adapter = makeS3Adapter();
8+
const S3Adapter = getMockS3Adapter();
99

1010
let serverState = {};
1111

spec/test.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const config = require('config');
33
const filesAdapterTests = require('parse-server-conformance-tests').files;
44
const S3Adapter = require('../index.js');
55
const optionsFromArguments = require('../lib/optionsFromArguments');
6-
const { makeS3Adapter } = require("./mocks/s3adapter-v2.js");
6+
const { getMockS3Adapter } = require("./mocks/s3adapter-v2.js");
77

88
describe('S3Adapter tests', () => {
99
beforeEach(() => {
@@ -480,7 +480,7 @@ describe('S3Adapter tests', () => {
480480
});
481481

482482
it('should return a file with a date stamp inserted in the path', () => {
483-
const s3 = makeS3Adapter(options);
483+
const s3 = getMockS3Adapter(options);
484484
const fileName = 'randomFileName.txt';
485485
const response = s3.createFile(fileName, 'hello world', 'text/utf8').then((value) => {
486486
const url = new URL(value.Location);
@@ -491,7 +491,7 @@ describe('S3Adapter tests', () => {
491491

492492
it('should do nothing when null', () => {
493493
options.generateKey = null;
494-
const s3 = makeS3Adapter(options);
494+
const s3 = getMockS3Adapter(options);
495495
const fileName = 'foo/randomFileName.txt';
496496
const response = s3.createFile(fileName, 'hello world', 'text/utf8').then((value) => {
497497
const url = new URL(value.Location);
@@ -501,7 +501,7 @@ describe('S3Adapter tests', () => {
501501
});
502502

503503
it('should add unique timestamp to the file name after the last directory when there is a path', () => {
504-
const s3 = makeS3Adapter(options);
504+
const s3 = getMockS3Adapter(options);
505505
const fileName = 'foo/randomFileName.txt';
506506
const response = s3.createFile(fileName, 'hello world', 'text/utf8').then((value) => {
507507
const url = new URL(value.Location);
@@ -523,7 +523,7 @@ describe('S3Adapter tests', () => {
523523
});
524524

525525
it('should save a file with metadata added', async () => {
526-
const s3 = makeS3Adapter(options);
526+
const s3 = getMockS3Adapter(options);
527527
s3._s3Client.upload = (params, callback) => {
528528
const { Metadata } = params;
529529
expect(Metadata).toEqual({ foo: 'bar' });
@@ -538,7 +538,7 @@ describe('S3Adapter tests', () => {
538538
});
539539

540540
it('should save a file with tags added', async () => {
541-
const s3 = makeS3Adapter(options);
541+
const s3 = getMockS3Adapter(options);
542542
s3._s3Client.upload = (params, callback) => {
543543
const { Tagging } = params;
544544
expect(Tagging).toEqual('foo=bar&baz=bin');
@@ -555,7 +555,7 @@ describe('S3Adapter tests', () => {
555555
it('should save a file with proper ACL with direct access', async () => {
556556
// Create adapter
557557
options.directAccess = true;
558-
const s3 = makeS3Adapter(options);
558+
const s3 = getMockS3Adapter(options);
559559
spyOn(s3._s3Client, 'upload').and.callThrough();
560560
// Save file
561561
await s3.createFile('file.txt', 'hello world', 'text/utf8', {});
@@ -569,7 +569,7 @@ describe('S3Adapter tests', () => {
569569

570570
it('should save a file with proper ACL without direct access', async () => {
571571
// Create adapter
572-
const s3 = makeS3Adapter(options);
572+
const s3 = getMockS3Adapter(options);
573573
spyOn(s3._s3Client, 'upload').and.callThrough();
574574
// Save file
575575
await s3.createFile('file.txt', 'hello world', 'text/utf8', {});
@@ -585,7 +585,7 @@ describe('S3Adapter tests', () => {
585585
// Create adapter
586586
options.directAccess = true;
587587
options.fileAcl = 'private';
588-
const s3 = makeS3Adapter(options);
588+
const s3 = getMockS3Adapter(options);
589589
spyOn(s3._s3Client, 'upload').and.callThrough();
590590
// Save file
591591
await s3.createFile('file.txt', 'hello world', 'text/utf8', {});
@@ -601,7 +601,7 @@ describe('S3Adapter tests', () => {
601601
// Create adapter
602602
options.directAccess = true;
603603
options.fileAcl = 'none';
604-
const s3 = makeS3Adapter(options);
604+
const s3 = getMockS3Adapter(options);
605605
spyOn(s3._s3Client, 'upload').and.callThrough();
606606
// Save file
607607
await s3.createFile('file.txt', 'hello world', 'text/utf8', {});
@@ -614,5 +614,5 @@ describe('S3Adapter tests', () => {
614614
});
615615
});
616616

617-
filesAdapterTests.testAdapter('S3Adapter', makeS3Adapter({}));
617+
filesAdapterTests.testAdapter('S3Adapter', getMockS3Adapter({}));
618618
});

0 commit comments

Comments
 (0)