1 | const expect = require( "chai" ).expect;
|
2 | const assert = require( "chai" ).assert;
|
3 | const AWS = require( "aws-sdk" );
|
4 | const sinon = require( "sinon" );
|
5 | const fs = require( "fs" );
|
6 |
|
7 | const utils = require( "../lib/utils" );
|
8 |
|
9 | function stubObjectWithReturnValue( object, method, returnValue ) {
|
10 | const stub = sinon.stub( object, method );
|
11 | stub.returns( returnValue );
|
12 | return stub;
|
13 | }
|
14 |
|
15 | describe( "colly utils", () => {
|
16 |
|
17 | afterEach( () => {
|
18 |
|
19 | process.env.ENV = "";
|
20 | process.env.COLLY__LAMBDA_NAME = "";
|
21 | process.env.COLLY__LAMBDA_EVENT_FILE = "";
|
22 | process.env.COLLY__RUN_LAMBDA_LOCAL = "";
|
23 | process.env.COLLY__PROJECT_DIR = "";
|
24 | process.env.AWS_PROFILE = "";
|
25 | process.env.COLLY__USE_BASTION = "";
|
26 |
|
27 | });
|
28 |
|
29 | it( "should list all of the available config files", () => {
|
30 |
|
31 | process.env.COLLY__PROJECT_DIR = `./test/fakeProjectDirectory`;
|
32 | const expectedList = {
|
33 | "live": "colly.json",
|
34 | "test": "colly.test.json"
|
35 | }
|
36 |
|
37 | expect( utils.listEnvFiles() ).to.deep.equal( expectedList );
|
38 |
|
39 | });
|
40 |
|
41 | it( "should default the config file to live when no env is set", () => {
|
42 |
|
43 | expect( utils.chooseProjectFile( { "live": "colly.json" }, "" ) ).to.equal( "colly.json" );
|
44 |
|
45 | });
|
46 |
|
47 | it( "should choose the correct config file to use", () => {
|
48 |
|
49 | expect( utils.chooseProjectFile( { "test": "colly.test.json" }, "test" ) ).to.equal( "colly.test.json" );
|
50 |
|
51 | });
|
52 |
|
53 | it( "should fetch the correct project config file", () => {
|
54 |
|
55 | process.env.COLLY__PROJECT_DIR = "./test/fakeProjectDirectory";
|
56 | process.env.ENV = "live";
|
57 |
|
58 | const expected = {
|
59 | "environmentVariables": {
|
60 | "env": "live"
|
61 | }
|
62 | };
|
63 |
|
64 | expect( utils.getProjectConfig() ).to.deep.equal( expected );
|
65 |
|
66 | } );
|
67 |
|
68 | it( "should correctly set options based on the command line params and the config file", () => {
|
69 |
|
70 | process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
|
71 | stub = stubObjectWithReturnValue( fs, "readFileSync", "{}" );
|
72 | const cliOptions = {
|
73 | "env": "live",
|
74 | "name": "myLambda",
|
75 | "event": "ref/to/event/file.json",
|
76 | "local": true,
|
77 | "use_bastion": true,
|
78 | "aws_profile": "myAwsProfileName"
|
79 | };
|
80 |
|
81 | utils.setOptions( cliOptions );
|
82 |
|
83 | expect( process.env.ENV ).to.equal( cliOptions.env );
|
84 | expect( process.env.COLLY__LAMBDA_NAME ).to.equal( cliOptions.name );
|
85 | expect( process.env.COLLY__LAMBDA_EVENT_FILE ).to.equal( cliOptions.event );
|
86 | expect( process.env.COLLY__RUN_LAMBDA_LOCAL ).to.equal( cliOptions.local.toString() );
|
87 | expect( process.env.COLLY__USE_BASTION ).to.equal( cliOptions.use_bastion.toString() );
|
88 | expect( process.env.AWS_PROFILE ).to.equal( cliOptions.aws_profile );
|
89 |
|
90 | stub.restore();
|
91 |
|
92 | });
|
93 |
|
94 | it( "should return all the characters after the last dot in a string", () => {
|
95 |
|
96 | expect( utils.everythingAfterTheLastDot( "everything.before.thelastdot" ) ).to.equal( "thelastdot" );
|
97 |
|
98 | } );
|
99 |
|
100 | it( "should return the correct env name", () => {
|
101 |
|
102 | process.env.ENV = "live";
|
103 | expect( utils.anyEnvButLive() ).to.equal( "" );
|
104 |
|
105 | process.env.ENV = "test";
|
106 | expect( utils.anyEnvButLive() ).to.equal( "TEST" );
|
107 |
|
108 | } );
|
109 |
|
110 | it( "should return the lambda name in relation to the environment (e.g. live, test )", () => {
|
111 |
|
112 | process.env.ENV = "live";
|
113 | expect( utils.getLambdaName( "fakeLambdaName" ) ).to.equal( "fakeLambdaName" );
|
114 |
|
115 | process.env.ENV = "test";
|
116 | expect( utils.getLambdaName( "fakeLambdaName" ) ).to.equal( "fakeLambdaNameTEST" );
|
117 |
|
118 | });
|
119 |
|
120 | it( "should add values to the environment", () => {
|
121 |
|
122 | utils.addLambdaEnvironmentVariablesToProcess({
|
123 | "FOO": "bar",
|
124 | "LAR": "car"
|
125 | });
|
126 | expect( process.env.FOO ).to.equal( "bar" );
|
127 | expect( process.env.LAR ).to.equal( "car" );
|
128 |
|
129 | });
|
130 |
|
131 | it( "should return the path to the lambda config file", () => {
|
132 |
|
133 | process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
|
134 | process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
|
135 |
|
136 | expect( utils.getLambdaConfigFilePath() ).to.equal( "./test/fixtures/utils/fetchConfigFile/function.json" );
|
137 |
|
138 | });
|
139 |
|
140 | it( "should return fetch the lambda config", () => {
|
141 |
|
142 | process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
|
143 | process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
|
144 |
|
145 | const expectedConfigFile = {
|
146 | "name": "fetchConfigFile",
|
147 | "handler": "fetchConfigFile/index.handler"
|
148 | }
|
149 |
|
150 | expect( utils.getLambdaConfigFile() ).to.deep.equal( expectedConfigFile );
|
151 |
|
152 | });
|
153 |
|
154 | it( "should return the lambda handler name", () => {
|
155 |
|
156 | process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
|
157 | process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
|
158 |
|
159 | expect( utils.getLambdaHandlerName() ).to.equal( "handler" );
|
160 |
|
161 | } );
|
162 |
|
163 | it( "should return the path to the lambda file", () => {
|
164 |
|
165 | process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
|
166 | process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
|
167 | expect( utils.getLambdaFilePath() ).to.equal( "./test/fixtures/utils/fetchConfigFile/index.js" );
|
168 | } );
|
169 |
|
170 | it( "should return the path to the lambda file with an alt start to path", () => {
|
171 |
|
172 | process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
|
173 | process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
|
174 | expect( utils.getLambdaFilePath( "./offset/dir") ).to.equal( "./offset/dir/fetchConfigFile/index.js" );
|
175 | } );
|
176 |
|
177 | it( "should set the AWS region property", () => {
|
178 |
|
179 | process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
|
180 | process.env.ENV = "LIVE";
|
181 | utils.setAwsRegion();
|
182 | expect( AWS.config.region ).to.equal( "eu-west-1" );
|
183 |
|
184 | } );
|
185 |
|
186 | it( "should add a new value to the project config", () => {
|
187 |
|
188 | process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
|
189 | process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
|
190 | process.env.ENV = "LIVE";
|
191 | const stubbedWriteFileSync = sinon.stub( fs, "writeFileSync" );
|
192 | const expectedResult = {
|
193 | "name": "fetchConfigFile",
|
194 | "handler": "fetchConfigFile/index.handler",
|
195 | "test": {
|
196 | "foo": {
|
197 | "bar": "test value"
|
198 | }
|
199 | }
|
200 | };
|
201 |
|
202 |
|
203 | utils.addValueToLambdaConfig( "test.foo.bar", "test value" );
|
204 | expect( JSON.parse( stubbedWriteFileSync.getCall(0).args[1] ) ).to.deep.equal( expectedResult );
|
205 |
|
206 | fs.writeFileSync.restore();
|
207 |
|
208 | });
|
209 |
|
210 | it( "should reformat the config file to make it compatible with the CLI options object", () => {
|
211 |
|
212 | const configObject = {
|
213 | "useBastion": true,
|
214 | "awsProfile": true,
|
215 | "notWantedProperty": true
|
216 | }
|
217 | const reformatedObject = {
|
218 | "use_bastion": true,
|
219 | "aws_profile": true
|
220 | }
|
221 |
|
222 | expect( utils.formatConfigFile( configObject ) ).to.deep.equal( reformatedObject );
|
223 |
|
224 | })
|
225 |
|
226 | it( "should use settings defined in the config file", () => {
|
227 |
|
228 | process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
|
229 | process.env.ENV = "LIVE";
|
230 | const fakeConfig = {
|
231 | "useBastion": true
|
232 | }
|
233 |
|
234 | const stubbedReadFileSync = stubObjectWithReturnValue( fs, "readFileSync", JSON.stringify( fakeConfig ) );
|
235 |
|
236 | utils.setOptions( {} );
|
237 |
|
238 | expect( process.env.COLLY__USE_BASTION ).to.equal( "true" );
|
239 |
|
240 | stubbedReadFileSync.restore();
|
241 | } );
|
242 |
|
243 | }); |
\ | No newline at end of file |