Skip to content

Commit fa27701

Browse files
Arthur Cinaderflovilmart
authored andcommitted
Handle immutable configuration (#33)
* Handle immutable configuration The popular and awesome https://github.com/lorenwest/node-config makes all configs values immutable. Good practice! parse-server-s3-adapter attempts to edit config values. This fix will respect immutability allowing this adapter to work with node-config. * PR comments.... 1. move setting NODE_ENV to test command instead of helpers. 2. add a missing ;. lint coming.....
1 parent 2e57c48 commit fa27701

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

lib/optionsFromArguments.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ const optionsFromArguments = function optionsFromArguments(args) {
5050
}
5151
} else {
5252
if (args.length == 1) {
53-
options = stringOrOptions || {};
53+
Object.assign(options, stringOrOptions);
5454
} else if (args.length == 2) {
55-
options = stringOrOptions;
55+
Object.assign(options, stringOrOptions);
5656
s3overrides = args[1];
5757
options.bucket = s3overrides.params.Bucket;
58-
} else {
58+
} else if (args.length > 2) {
5959
throw new Error('Failed to configure S3Adapter. Arguments don\'t make sense');
6060
}
6161
}
62+
6263
options = requiredOrFromEnvironment(options, 'bucket', 'S3_BUCKET');
6364
options = fromEnvironmentOrDefault(options, 'accessKey', 'S3_ACCESS_KEY', null);
6465
options = fromEnvironmentOrDefault(options, 'secretKey', 'S3_SECRET_KEY', null);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "AWS S3 adapter for parse-server",
55
"main": "index.js",
66
"scripts": {
7-
"test": "istanbul cover -x **/spec/** jasmine --captureExceptions"
7+
"test": "NODE_ENV=test NODE_CONFIG_DIR=./spec/config istanbul cover -x **/spec/** jasmine --captureExceptions"
88
},
99
"repository": {
1010
"type": "git",
@@ -26,6 +26,7 @@
2626
},
2727
"devDependencies": {
2828
"codecov": "^1.0.1",
29+
"config": "^1.24.0",
2930
"istanbul": "^0.4.2",
3031
"jasmine": "^2.4.1",
3132
"parse-server-conformance-tests": "^1.0.0"

spec/config/test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
accessKey: 'accessKey',
3+
secretKey: 'secretKey',
4+
insufficientOptions: {
5+
accessKey: 'accessKey',
6+
secretKey: 'secretKey',
7+
},
8+
bucket: 'bucket',
9+
objectWithBucket: {
10+
bucket: 'bucket',
11+
},
12+
emptyObject: {},
13+
paramsObjectWBucket: {
14+
params: {
15+
Bucket: 'bucket',
16+
},
17+
},
18+
};

spec/support/jasmine.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"spec_dir": "spec",
33
"spec_files": [
4-
"test.spec.js"
5-
]
4+
"**/*[sS]pec.js"
5+
],
6+
"helpers": [
7+
"helpers/**/*.js"
8+
]
69
}

spec/test.spec.js

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
'use strict';
2-
let filesAdapterTests = require('parse-server-conformance-tests').files;
32

4-
let S3Adapter = require('../index.js');
5-
let optionsFromArguments = require('../lib/optionsFromArguments');
3+
const config = require('config');
4+
const filesAdapterTests = require('parse-server-conformance-tests').files;
5+
const S3Adapter = require('../index.js');
6+
const optionsFromArguments = require('../lib/optionsFromArguments');
67

7-
describe('S3Adapter tests', () => {
8+
describe('S3Adapter tests', () => {
9+
beforeEach(() => {
10+
delete process.env.S3_BUCKET;
11+
delete process.env.S3_REGION;
12+
});
813

914
it('should throw when not initialized properly', () => {
1015
expect(() => {
1116
var s3 = new S3Adapter();
12-
}).toThrow(new Error('Failed to configure S3Adapter. Arguments don\'t make sense'));
17+
}).toThrow("S3Adapter requires option 'bucket' or env. variable S3_BUCKET");
1318

1419
expect(() =>  {
1520
var s3 = new S3Adapter('accessKey', 'secretKey', {});
@@ -34,6 +39,57 @@ describe('S3Adapter tests', () => {
3439
}).not.toThrow()
3540
});
3641

42+
it('should accept environment for required', () => {
43+
const TEST_BUCKET = 'testBucket';
44+
process.env.S3_BUCKET = TEST_BUCKET;
45+
const s3 = new S3Adapter();
46+
expect(s3._bucket).toBe(TEST_BUCKET);
47+
});
48+
49+
describe('configured with immutable values', () => {
50+
describe('not initialized properly', () => {
51+
it('should fail with two string arguments', () => {
52+
expect(() => {
53+
var s3 = new S3Adapter(config.get('accessKey'), config.get('secretKey'), {});
54+
}).toThrow(new Error('Failed to configure S3Adapter. Arguments don\'t make sense'));
55+
});
56+
57+
it('should fail when passed an object without a bucket', () => {
58+
expect(() => {
59+
var s3 = new S3Adapter(config.get('insufficientOptions'));
60+
}).toThrow("S3Adapter requires option 'bucket' or env. variable S3_BUCKET")
61+
});
62+
});
63+
64+
65+
describe('should not throw when initialized properly', () => {
66+
it('should accept a string bucket', () => {
67+
expect(() => {
68+
var s3 = new S3Adapter(config.get('bucket'));
69+
}).not.toThrow()
70+
});
71+
72+
it('should accept an object with a bucket', () => {
73+
expect(() =>  {
74+
var s3 = new S3Adapter(config.get('objectWithBucket'));
75+
}).not.toThrow()
76+
});
77+
78+
it('should accept a second argument of object with a params object with a bucket', () => {
79+
expect(() => {
80+
var s3 = new S3Adapter(config.get('emptyObject'), config.get('paramsObjectWBucket'));
81+
}).not.toThrow()
82+
});
83+
84+
it('should accept environment over default', () => {
85+
const TEST_REGION = 'test';
86+
process.env.S3_REGION = TEST_REGION;
87+
const s3 = new S3Adapter(config.get('bucket'));
88+
expect(s3._region).toBe(TEST_REGION);
89+
});
90+
});
91+
});
92+
3793
describe('to find the right arg in the right place', () => {
3894
it('should accept just bucket as first string arg', () => {
3995
var args = ['bucket'];

0 commit comments

Comments
 (0)