UNPKG

5.21 kBJavaScriptView Raw
1const expect = require( "chai" ).expect;
2const assert = require( "chai" ).assert;
3const AWS = require( "aws-sdk" );
4const sinon = require( "sinon" );
5const fs = require( "fs" );
6
7const utils = require( "../lib/utils" );
8
9describe( "colly utils", () => {
10
11 afterEach( () => {
12 process.env.COLLY__PROJECT_DIR = "";
13 });
14
15 it( "should list all of the available config files", () => {
16
17 process.env.COLLY__PROJECT_DIR = `./test/fakeProjectDirectory`;
18 const expectedList = {
19 "live": "colly.json",
20 "test": "colly.test.json"
21 }
22
23 expect( utils.listEnvFiles() ).to.deep.equal( expectedList );
24
25 });
26
27 it( "should choose the correct config file to use", () => {
28
29 expect( utils.chooseProjectFile( { "test": "colly.test.json" }, "test" ) ).to.equal( "colly.test.json" );
30
31 });
32
33 it( "should fetch the correct project config file", () => {
34
35 process.env.COLLY__PROJECT_DIR = "./test/fakeProjectDirectory";
36 process.env.ENV = "live";
37
38 const expected = {
39 "environmentVariables": {
40 "env": "live"
41 }
42 };
43
44 expect( utils.getProjectConfig() ).to.deep.equal( expected );
45
46 } );
47
48 it( "should correctly set options based on the command line params and the config file", () => {
49
50 const cliOptions = {
51 "env": "live",
52 "name": "myLambda",
53 "event": "ref/to/event/file.json",
54 "local": true,
55 "use_bastion": true,
56 "aws_profile": "myAwsProfileName"
57 };
58
59 utils.setOptions( cliOptions );
60
61 expect( process.env.ENV ).to.equal( cliOptions.env );
62 expect( process.env.COLLY__LAMBDA_NAME ).to.equal( cliOptions.name );
63 expect( process.env.COLLY__LAMBDA_EVENT_FILE ).to.equal( cliOptions.event );
64 expect( process.env.COLLY__RUN_LAMBDA_LOCAL ).to.equal( cliOptions.local.toString() );
65 expect( process.env.COLLY__USE_BASTION ).to.equal( cliOptions.use_bastion.toString() );
66 expect( process.env.AWS_PROFILE ).to.equal( cliOptions.aws_profile );
67
68 });
69
70 it( "should return all the characters after the last dot in a string", () => {
71
72 expect( utils.everythingAfterTheLastDot( "everything.before.thelastdot" ) ).to.equal( "thelastdot" );
73
74 } );
75
76 it( "should return the correct env name", () => {
77
78 process.env.ENV = "live";
79 expect( utils.anyEnvButLive() ).to.equal( "" );
80
81 process.env.ENV = "test";
82 expect( utils.anyEnvButLive() ).to.equal( "TEST" );
83
84 } );
85
86 it( "should return the lambda name in relation to the environment (e.g. live, test )", () => {
87
88 process.env.ENV = "live";
89 expect( utils.getLambdaName( "fakeLambdaName" ) ).to.equal( "fakeLambdaName" );
90
91 process.env.ENV = "test";
92 expect( utils.getLambdaName( "fakeLambdaName" ) ).to.equal( "fakeLambdaNameTEST" );
93
94 });
95
96 it( "should add values to the environment", () => {
97
98 utils.addLambdaEnvironmentVariablesToProcess({
99 "FOO": "bar",
100 "LAR": "car"
101 });
102 expect( process.env.FOO ).to.equal( "bar" );
103 expect( process.env.LAR ).to.equal( "car" );
104
105 });
106
107 it( "should return the path to the lambda config file", () => {
108
109 process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
110 process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
111
112 expect( utils.getLambdaConfigFilePath() ).to.equal( "./test/fixtures/utils/fetchConfigFile/function.json" );
113
114 });
115
116 it( "should return fetch the lambda config", () => {
117
118 process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
119 process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
120
121 const expectedConfigFile = {
122 "name": "fetchConfigFile",
123 "handler": "fetchConfigFile/index.handler"
124 }
125
126 expect( utils.getLambdaConfigFile() ).to.deep.equal( expectedConfigFile );
127
128 });
129
130 it( "should return the lambda handler name", () => {
131
132 process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
133 process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
134
135 expect( utils.getLambdaHandlerName() ).to.equal( "handler" );
136
137 } );
138
139 it( "should return the path to the lambda file", () => {
140
141 process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
142 process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
143 expect( utils.getLambdaFilePath() ).to.equal( "./test/fixtures/utils/fetchConfigFile/index.js" );
144 } );
145
146 it( "should return the path to the lambda file with an alt start to path", () => {
147
148 process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
149 process.env.COLLY__LAMBDA_NAME = "fetchConfigFile";
150 expect( utils.getLambdaFilePath( "./offset/dir") ).to.equal( "./offset/dir/fetchConfigFile/index.js" );
151 } );
152
153 it( "should set the AWS region property", () => {
154
155 process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
156 process.env.ENV = "LIVE";
157 utils.setAwsRegion();
158 expect( AWS.config.region ).to.equal( "eu-west-1" );
159
160 } );
161
162 it( "should add a new value to the project config", () => {
163
164 process.env.COLLY__PROJECT_DIR = "./test/fixtures/utils";
165 process.env.ENV = "LIVE";
166 const stubbedWriteFileSync = sinon.stub( fs, "writeFileSync" );
167 const expectedResult = {
168 "name": "fetchConfigFile",
169 "handler": "fetchConfigFile/index.handler",
170 "test": {
171 "foo": {
172 "bar": "test value"
173 }
174 }
175 };
176
177
178 utils.addValueToLambdaConfig( "test.foo.bar", "test value" );
179 expect( JSON.parse( stubbedWriteFileSync.getCall(0).args[1] ) ).to.deep.equal( expectedResult );
180
181 fs.writeFileSync.restore();
182
183 })
184
185});
\No newline at end of file