lesgo 0.7.8 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -93,6 +93,21 @@ NC='\033[0m';
93
93
  # FUNCTION DECLARATIONS #
94
94
  # #
95
95
  ###############################################################################
96
+ LOCAL_SERVERLESS_VERSION=`npm ls --depth=0 serverless | grep -o "serverless@.*" || true`
97
+ GLOBAL_SERVERLESS_VERSION=`npm ls -g --depth=0 serverless | grep -o "serverless@.*" || true`
98
+ LATEST_SERVERLESS_VERSION_NUMBER=3
99
+
100
+ if [ -z "$LOCAL_SERVERLESS_VERSION" ]
101
+ then
102
+ CURRENT_SERVERLESS_VERSION=$GLOBAL_SERVERLESS_VERSION
103
+ else
104
+ CURRENT_SERVERLESS_VERSION=$LOCAL_SERVERLESS_VERSION
105
+ fi
106
+ SERVERLESS_VERSION=${CURRENT_SERVERLESS_VERSION#*@}
107
+ SERVERLESS_VERSION_NUMBER=${SERVERLESS_VERSION%%.*}
108
+
109
+ echo -e "Running Serverless version:\n${CURRENT_SERVERLESS_VERSION}\n"
110
+ echo -e "Current Serverless version: ${SERVERLESS_VERSION_NUMBER}\n"
96
111
 
97
112
  function deploy_func_check ()
98
113
  {
@@ -106,7 +121,12 @@ function deploy_func_check ()
106
121
  function deploy_func ()
107
122
  {
108
123
  echo -e "${YELLOW}Deploying ${FUNCTION} to ${STAGE}${NC} using ${CONFIG}"
109
- sls deploy -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
124
+
125
+ if [ ${SERVERLESS_VERSION_NUMBER} -ge ${LATEST_SERVERLESS_VERSION_NUMBER} ]; then
126
+ sls deploy function -f ${FUNCTION} --stage ${STAGE} --param="env=${ENVFILE}" --config ${CONFIG}
127
+ else
128
+ sls deploy -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
129
+ fi
110
130
  }
111
131
 
112
132
  function prompt_confirmation_deploy_all ()
@@ -140,29 +160,50 @@ function prompt_confirmation_deploy_function ()
140
160
  function deploy_full ()
141
161
  {
142
162
  echo -e "${YELLOW}Deploying service to ${STAGE}${NC}"
143
- sls deploy --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
163
+ if [ ${SERVERLESS_VERSION_NUMBER} -ge ${LATEST_SERVERLESS_VERSION_NUMBER} ]; then
164
+ sls deploy --stage ${STAGE} --param="env=${ENVFILE}" --config ${CONFIG}
165
+ else
166
+ sls deploy --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
167
+ fi
144
168
  }
145
169
 
146
170
  function invoke_func ()
147
171
  {
148
172
  echo -e "${YELLOW}Invoking function ${FUNCTION} on ${STAGE}${NC} using ${CONFIG}"
149
173
  if [ ${INVOKE_LOCAL} == 1 ]; then
150
- sls invoke local -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -d ${DATA} -l --config ${CONFIG}
174
+ if [ ${SERVERLESS_VERSION_NUMBER} -ge ${LATEST_SERVERLESS_VERSION_NUMBER} ]; then
175
+ sls invoke local function -f ${FUNCTION} --stage ${STAGE} --param="env=${ENVFILE}" -d ${DATA} -l --config ${CONFIG}
176
+ else
177
+ sls invoke local -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -d ${DATA} -l --config ${CONFIG}
178
+ fi
179
+
151
180
  else
152
- sls invoke -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -d ${DATA} -l --config ${CONFIG}
181
+ if [ ${SERVERLESS_VERSION_NUMBER} -ge ${LATEST_SERVERLESS_VERSION_NUMBER} ]; then
182
+ sls invoke function -f ${FUNCTION} --stage ${STAGE} --param="env=${ENVFILE}" -d ${DATA} -l --config ${CONFIG}
183
+ else
184
+ sls invoke -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -d ${DATA} -l --config ${CONFIG}
185
+ fi
153
186
  fi
154
187
  }
155
188
 
156
189
  function log_stream_func ()
157
190
  {
158
191
  echo -e "${YELLOW}Log Streaming function ${FUNCTION} on ${STAGE}${NC} using ${CONFIG}"
159
- sls logs -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -t --config ${CONFIG}
192
+ if [ ${SERVERLESS_VERSION_NUMBER} -ge ${LATEST_SERVERLESS_VERSION_NUMBER} ]; then
193
+ sls logs function -f ${FUNCTION} --stage ${STAGE} --param="env=${ENVFILE}" -t --config ${CONFIG}
194
+ else
195
+ sls logs -f ${FUNCTION} --stage ${STAGE} --env ${ENVFILE} -t --config ${CONFIG}
196
+ fi
160
197
  }
161
198
 
162
199
  function build ()
163
200
  {
164
201
  echo -e "${YELLOW}Building bundle without deployment${NC} using ${CONFIG}"
165
- sls webpack --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
202
+ if [ ${SERVERLESS_VERSION_NUMBER} -ge ${LATEST_SERVERLESS_VERSION_NUMBER} ]; then
203
+ sls webpack --stage ${STAGE} --param="env=${ENVFILE}" --config ${CONFIG}
204
+ else
205
+ sls webpack --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
206
+ fi
166
207
  }
167
208
 
168
209
  function prompt_confirmation_destroy_service ()
@@ -180,7 +221,11 @@ function prompt_confirmation_destroy_service ()
180
221
  function destroy_service ()
181
222
  {
182
223
  echo -e "${YELLOW}Removing service to ${STAGE}${NC}"
183
- sls remove --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
224
+ if [ ${SERVERLESS_VERSION_NUMBER} -ge ${LATEST_SERVERLESS_VERSION_NUMBER} ]; then
225
+ sls remove --stage ${STAGE} --param="env=${ENVFILE}" --config ${CONFIG}
226
+ else
227
+ sls remove --stage ${STAGE} --env ${ENVFILE} --config ${CONFIG}
228
+ fi
184
229
  }
185
230
 
186
231
  ###############################################################################
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lesgo",
3
- "version": "0.7.8",
3
+ "version": "1.0.0",
4
4
  "description": "Core framework for lesgo node.js serverless framework.",
5
5
  "main": "./src/index.js",
6
6
  "author": "Sufiyan Rahmat",
@@ -4,6 +4,38 @@ import basicAuthMiddleware, {
4
4
  } from '../basicAuthMiddleware';
5
5
  import client from '../../../tests/__mocks__/config/client';
6
6
 
7
+ describe('test generateBasicAuthorizationHash', () => {
8
+ test('should return custom when overriden through opts', () => {
9
+ expect(
10
+ generateBasicAuthorizationHash(
11
+ 'e5bb52b012ad4a4e9d862a3410e7013d',
12
+ '4cd19af8f7ca4448bcb2c427754095b5',
13
+ {
14
+ getPreHashString: key => {
15
+ return `=${key}=`;
16
+ },
17
+ }
18
+ )
19
+ ).toBe('PWU1YmI1MmIwMTJhZDRhNGU5ZDg2MmEzNDEwZTcwMTNkPQ==');
20
+ });
21
+
22
+ test('should return basic auth by default', () => {
23
+ const { getPreHashString } = client;
24
+ client.getPreHashString = null;
25
+
26
+ expect(
27
+ generateBasicAuthorizationHash(
28
+ 'e5bb52b012ad4a4e9d862a3410e7013d',
29
+ '4cd19af8f7ca4448bcb2c427754095b5'
30
+ )
31
+ ).toBe(
32
+ 'ZTViYjUyYjAxMmFkNGE0ZTlkODYyYTM0MTBlNzAxM2Q6NGNkMTlhZjhmN2NhNDQ0OGJjYjJjNDI3NzU0MDk1YjU='
33
+ );
34
+
35
+ client.getPreHashString = getPreHashString;
36
+ });
37
+ });
38
+
7
39
  describe('test basicAuthMiddleware middleware', () => {
8
40
  test.each`
9
41
  clientObj
@@ -21,7 +53,6 @@ describe('test basicAuthMiddleware middleware', () => {
21
53
  });
22
54
 
23
55
  expect(result).toHaveProperty('before');
24
- expect(result).toHaveProperty('onError');
25
56
  });
26
57
  });
27
58
 
@@ -33,28 +64,33 @@ describe('test verifyBasicAuthBeforeHandler error handling', () => {
33
64
  'base64'
34
65
  );
35
66
  const invalidSecretKey = Buffer.from(
36
- `${client.platform_2.key}:secret_key`
67
+ `${client.clients.platform_2.key}:secret_key`
37
68
  ).toString('base64');
38
69
 
39
70
  test.each`
40
- headers | errorName | errorMessage | errorStatusCode | errorCode | blacklistMode
71
+ headers | errorName | errorMessage | errorStatusCode | errorCode | platform
41
72
  ${{}} | ${'LesgoException'} | ${'Authorization header not found'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTHORIZATION_HEADER_NOT_FOUND'} | ${undefined}
42
73
  ${{ Authorization: 'auth' }} | ${'LesgoException'} | ${'Invalid authorization type provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_AUTHORIZATION_TYPE'} | ${undefined}
43
74
  ${{ Authorization: 'basic ' }} | ${'LesgoException'} | ${'Empty basic authentication hash provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_EMPTY_BASIC_HASH'} | ${undefined}
44
75
  ${{ Authorization: `basic ${invalidClientKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${undefined}
45
76
  ${{ Authorization: `basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${undefined}
46
77
  ${{ Authorization: `Basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${undefined}
47
- ${{}} | ${'LesgoException'} | ${'Authorization header not found'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTHORIZATION_HEADER_NOT_FOUND'} | ${true}
48
- ${{ Authorization: 'auth' }} | ${'LesgoException'} | ${'Invalid authorization type provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_AUTHORIZATION_TYPE'} | ${true}
49
- ${{ Authorization: 'basic ' }} | ${'LesgoException'} | ${'Empty basic authentication hash provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_EMPTY_BASIC_HASH'} | ${true}
50
- ${{ Authorization: `basic ${invalidClientKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${true}
51
- ${{ Authorization: `basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${true}
52
- ${{ Authorization: `Basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${true}
53
- ${{ Authorization: 'auth' }} | ${'LesgoException'} | ${'Invalid authorization type provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_AUTHORIZATION_TYPE'} | ${false}
54
- ${{ Authorization: 'basic ' }} | ${'LesgoException'} | ${'Empty basic authentication hash provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_EMPTY_BASIC_HASH'} | ${false}
55
- ${{ Authorization: `basic ${invalidClientKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${false}
56
- ${{ Authorization: `basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${false}
57
- ${{ Authorization: `Basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${false}
78
+ ${{}} | ${'LesgoException'} | ${'Authorization header not found'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTHORIZATION_HEADER_NOT_FOUND'} | ${'platform_1'}
79
+ ${{ Authorization: 'auth' }} | ${'LesgoException'} | ${'Invalid authorization type provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_AUTHORIZATION_TYPE'} | ${'platform_1'}
80
+ ${{ Authorization: 'basic ' }} | ${'LesgoException'} | ${'Empty basic authentication hash provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_EMPTY_BASIC_HASH'} | ${'platform_1'}
81
+ ${{ Authorization: `basic ${invalidClientKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${'platform_1'}
82
+ ${{ Authorization: `basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${'platform_1'}
83
+ ${{ Authorization: `Basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${'platform_1'}
84
+ ${{ Authorization: 'auth' }} | ${'LesgoException'} | ${'Invalid authorization type provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_AUTHORIZATION_TYPE'} | ${'blacklist_platform'}
85
+ ${{ Authorization: 'basic ' }} | ${'LesgoException'} | ${'Empty basic authentication hash provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_EMPTY_BASIC_HASH'} | ${'blacklist_platform'}
86
+ ${{ Authorization: `basic ${invalidClientKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${'blacklist_platform'}
87
+ ${{ Authorization: `basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${'blacklist_platform'}
88
+ ${{ Authorization: `Basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${'blacklist_platform'}
89
+ ${{ Authorization: 'auth' }} | ${'LesgoException'} | ${'Invalid authorization type provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_AUTHORIZATION_TYPE'} | ${'blacklist_platform_1'}
90
+ ${{ Authorization: 'basic ' }} | ${'LesgoException'} | ${'Empty basic authentication hash provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_EMPTY_BASIC_HASH'} | ${'blacklist_platform_1'}
91
+ ${{ Authorization: `basic ${invalidClientKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${'blacklist_platform_1'}
92
+ ${{ Authorization: `basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${'blacklist_platform_1'}
93
+ ${{ Authorization: `Basic ${invalidSecretKey}` }} | ${'LesgoException'} | ${'Invalid client key or secret provided'} | ${403} | ${'Middlewares/basicAuthMiddleware::AUTH_INVALID_CLIENT_OR_SECRET_KEY'} | ${'blacklist_platform_1'}
58
94
  `(
59
95
  'should throw $errorMessage when authorization header is $headers',
60
96
  async ({
@@ -63,23 +99,20 @@ describe('test verifyBasicAuthBeforeHandler error handling', () => {
63
99
  errorMessage,
64
100
  errorStatusCode,
65
101
  errorCode,
66
- blacklistMode,
102
+ platform,
67
103
  }) => {
68
104
  const handler = {
69
105
  event: {
70
106
  headers,
71
- site: {
72
- id: 'platform_1',
107
+ platform: {
108
+ id: platform,
109
+ ...client.clients[platform],
73
110
  },
74
111
  },
75
112
  };
76
113
 
77
114
  try {
78
- expect(
79
- verifyBasicAuthBeforeHandler(handler, next, {
80
- blacklistMode,
81
- })
82
- ).toThrow();
115
+ expect(await verifyBasicAuthBeforeHandler(handler, next)).toThrow();
83
116
  } catch (error) {
84
117
  expect(error.name).toBe(errorName);
85
118
  expect(error.message).toBe(errorMessage);
@@ -91,12 +124,45 @@ describe('test verifyBasicAuthBeforeHandler error handling', () => {
91
124
  });
92
125
 
93
126
  describe('test verifyBasicAuthBeforeHandler with valid credentials', () => {
94
- const validBasicAuth = Buffer.from(
95
- generateBasicAuthorizationHash(
96
- client.platform_2.key,
97
- client.platform_2.secret
98
- )
99
- ).toString('base64');
127
+ const validBasicAuth = generateBasicAuthorizationHash(
128
+ client.clients.platform_2.key,
129
+ client.clients.platform_2.secret
130
+ );
131
+
132
+ test.each`
133
+ clientObj
134
+ ${undefined}
135
+ ${{}}
136
+ ${{
137
+ platform_2: {
138
+ key: '2222-2222-2222-2222',
139
+ secret: '2222-2222-2222-2222',
140
+ },
141
+ }}
142
+ `('should return undefined when successful', async ({ clientObj }) => {
143
+ const handler = {
144
+ event: {
145
+ headers: {
146
+ Authorization: `basic ${validBasicAuth}`,
147
+ },
148
+ site: {
149
+ id: 'platform_2',
150
+ },
151
+ },
152
+ };
153
+
154
+ let hasError = false;
155
+
156
+ try {
157
+ await verifyBasicAuthBeforeHandler(handler, next, {
158
+ client: clientObj,
159
+ });
160
+ } catch (e) {
161
+ hasError = true;
162
+ }
163
+
164
+ expect(hasError).toBeFalsy();
165
+ });
100
166
 
101
167
  test.each`
102
168
  clientObj
@@ -108,7 +174,7 @@ describe('test verifyBasicAuthBeforeHandler with valid credentials', () => {
108
174
  secret: '2222-2222-2222-2222',
109
175
  },
110
176
  }}
111
- `('should return undefined when successful', ({ clientObj }) => {
177
+ `('should return undefined when successful', async ({ clientObj }) => {
112
178
  const handler = {
113
179
  event: {
114
180
  headers: {
@@ -123,7 +189,7 @@ describe('test verifyBasicAuthBeforeHandler with valid credentials', () => {
123
189
  let hasError = false;
124
190
 
125
191
  try {
126
- verifyBasicAuthBeforeHandler(handler, next, {
192
+ await verifyBasicAuthBeforeHandler(handler, next, {
127
193
  client: clientObj,
128
194
  });
129
195
  } catch (e) {
@@ -134,22 +200,24 @@ describe('test verifyBasicAuthBeforeHandler with valid credentials', () => {
134
200
  });
135
201
 
136
202
  test.each`
137
- Authorization | blacklistMode
138
- ${undefined} | ${false}
139
- ${`basic ${validBasicAuth}`} | ${false}
140
- ${`Basic ${validBasicAuth}`} | ${false}
141
- ${`basic ${validBasicAuth}`} | ${true}
142
- ${`Basic ${validBasicAuth}`} | ${true}
203
+ Authorization | platform
204
+ ${undefined} | ${'blacklist_platform'}
205
+ ${undefined} | ${'blacklist_platform_1'}
206
+ ${`basic ${validBasicAuth}`} | ${'platform_2'}
207
+ ${`Basic ${validBasicAuth}`} | ${'platform_2'}
208
+ ${`basic ${validBasicAuth}`} | ${'platform_2'}
209
+ ${`Basic ${validBasicAuth}`} | ${'platform_2'}
143
210
  `(
144
211
  'test Exception with valid credentials',
145
- ({ Authorization, blacklistMode }) => {
212
+ async ({ Authorization, platform }) => {
146
213
  const handler = {
147
214
  event: {
148
215
  headers: {
149
216
  Authorization,
150
217
  },
151
- site: {
152
- id: 'platform_2',
218
+ platform: {
219
+ id: platform,
220
+ ...client.clients[platform],
153
221
  },
154
222
  },
155
223
  };
@@ -157,9 +225,7 @@ describe('test verifyBasicAuthBeforeHandler with valid credentials', () => {
157
225
  let hasError = false;
158
226
 
159
227
  try {
160
- verifyBasicAuthBeforeHandler(handler, next, {
161
- blacklistMode,
162
- });
228
+ await verifyBasicAuthBeforeHandler(handler, next);
163
229
  } catch (e) {
164
230
  hasError = true;
165
231
  }
@@ -170,39 +236,12 @@ describe('test verifyBasicAuthBeforeHandler with valid credentials', () => {
170
236
 
171
237
  test.each`
172
238
  siteObjects
239
+ ${{ platform: {
240
+ id: 'platform_2',
241
+ ...client.clients.platform_2,
242
+ } }}
173
243
  ${{}}
174
- ${{ site: { id: undefined } }}
175
- ${{ requestContext: { site: { id: undefined } } }}
176
- ${{ platform: undefined }}
177
- `('test Exception with no site ID', ({ siteObjects }) => {
178
- const handler = {
179
- event: {
180
- headers: {
181
- Authorization: `basic ${validBasicAuth}`,
182
- },
183
- ...siteObjects,
184
- },
185
- };
186
-
187
- try {
188
- expect(verifyBasicAuthBeforeHandler(handler, next)).toThrow();
189
- } catch (error) {
190
- expect(error.name).toBe('LesgoException');
191
- expect(error.message).toBe('Site ID could not be found');
192
- expect(error.statusCode).toBe(403);
193
- expect(error.code).toBe(
194
- 'Middlewares/basicAuthMiddleware::SITE_ID_NOT_FOUND'
195
- );
196
- }
197
- });
198
-
199
- test.each`
200
- siteObjects
201
- ${{ site: { id: 'platform_2' } }}
202
- ${{ requestContext: { site: { id: 'platform_2' } } }}
203
- ${{ requestContext: { site: { id: undefined } }, platform: 'platform_2' }}
204
- ${{ platform: 'platform_2' }}
205
- `('valid site ids', ({ siteObjects }) => {
244
+ `('valid site ids', async ({ siteObjects }) => {
206
245
  const handler = {
207
246
  event: {
208
247
  headers: {
@@ -215,7 +254,7 @@ describe('test verifyBasicAuthBeforeHandler with valid credentials', () => {
215
254
  let hasError = false;
216
255
 
217
256
  try {
218
- verifyBasicAuthBeforeHandler(handler, next);
257
+ await verifyBasicAuthBeforeHandler(handler, next);
219
258
  } catch (e) {
220
259
  hasError = true;
221
260
  }
@@ -7,7 +7,6 @@ describe('test authMiddleware', () => {
7
7
  // eslint-disable-next-line no-unused-vars
8
8
  const result = clientAuthMiddleware();
9
9
  expect(result).toHaveProperty('before');
10
- expect(result).toHaveProperty('onError');
11
10
  });
12
11
  });
13
12
 
@@ -27,7 +26,7 @@ describe('test clientMiddlewareBeforeHandler', () => {
27
26
  expect(await clientAuthMiddlewareBeforeHandler(handler, next)).toThrow();
28
27
  } catch (error) {
29
28
  expect(error.name).toBe('LesgoException');
30
- expect(error.message).toBe("Missing required 'x-client-id'");
29
+ expect(error.message).toBe("Missing required 'clientKey'");
31
30
  expect(error.statusCode).toBe(403);
32
31
  expect(error.code).toBe(
33
32
  'Middlewares/clientAuthMiddleware::INVALID_AUTH_DATA'
@@ -56,6 +55,46 @@ describe('test clientMiddlewareBeforeHandler', () => {
56
55
  }
57
56
  });
58
57
 
58
+ test('should be able to validate from multiple header keys', async () => {
59
+ const handler = {
60
+ event: {
61
+ headers: {
62
+ 'X-Client-Id': '1111-1111-1111-1111',
63
+ },
64
+ },
65
+ };
66
+
67
+ let hasError = false;
68
+
69
+ try {
70
+ await clientAuthMiddlewareBeforeHandler(handler, next);
71
+ } catch (e) {
72
+ hasError = e;
73
+ }
74
+
75
+ expect(hasError).toBe(false);
76
+ });
77
+
78
+ test('should allow getting from query string parameters', async () => {
79
+ const handler = {
80
+ event: {
81
+ queryStringParameters: {
82
+ 'x-client-id': '1111-1111-1111-1111',
83
+ },
84
+ },
85
+ };
86
+
87
+ let hasError = false;
88
+
89
+ try {
90
+ await clientAuthMiddlewareBeforeHandler(handler, next);
91
+ } catch (e) {
92
+ hasError = e;
93
+ }
94
+
95
+ expect(hasError).toBe(false);
96
+ });
97
+
59
98
  test('should return success with valid client id', async () => {
60
99
  const handler = {
61
100
  event: {
@@ -97,6 +136,32 @@ describe('test clientMiddlewareBeforeHandler', () => {
97
136
  expect(hasError).toBe(false);
98
137
  });
99
138
 
139
+ test('should set handler.event.platform with valid client id on input', async () => {
140
+ const handler = {
141
+ event: {
142
+ headers: {},
143
+ input: {
144
+ clientid: '1111-1111-1111-1111',
145
+ },
146
+ },
147
+ };
148
+
149
+ let hasError = false;
150
+
151
+ try {
152
+ await clientAuthMiddlewareBeforeHandler(handler, next);
153
+ } catch (e) {
154
+ hasError = true;
155
+ }
156
+
157
+ expect(hasError).toBe(false);
158
+ expect(handler.event.platform).toStrictEqual({
159
+ id: 'platform_1',
160
+ key: '1111-1111-1111-1111',
161
+ secret: '1111-1111-1111-1111',
162
+ });
163
+ });
164
+
100
165
  test('should execute passed callback function', async () => {
101
166
  const handler = {
102
167
  event: {
@@ -115,7 +180,22 @@ describe('test clientMiddlewareBeforeHandler', () => {
115
180
  expect(handler.event).toHaveProperty('created_obj');
116
181
  });
117
182
 
118
- test('should execute passed top object with callback function', async () => {
183
+ test('should execute with callback function from config', async () => {
184
+ const handler = {
185
+ event: {
186
+ headers: {},
187
+ input: {
188
+ clientid: '1111-1111-1111-1111',
189
+ },
190
+ },
191
+ };
192
+
193
+ await clientAuthMiddlewareBeforeHandler(handler, next);
194
+
195
+ expect(handler.event).toHaveProperty('created_obj');
196
+ });
197
+
198
+ test('should execute passed opt object with async callback function', async () => {
119
199
  const handler = {
120
200
  event: {
121
201
  headers: {},
@@ -126,12 +206,30 @@ describe('test clientMiddlewareBeforeHandler', () => {
126
206
  };
127
207
 
128
208
  await clientAuthMiddlewareBeforeHandler(handler, next, {
129
- callback: h => {
209
+ callback: async h => {
210
+ await new Promise(resolve => setTimeout(resolve, 500));
130
211
  // eslint-disable-next-line no-param-reassign
131
- h.event.created_obj = 'created_obj';
212
+ h.event.created_obj_async = 'created_obj_async';
132
213
  },
133
214
  });
134
215
 
135
- expect(handler.event).toHaveProperty('created_obj');
216
+ expect(handler.event).toHaveProperty('created_obj_async');
217
+ });
218
+
219
+ test('should not execute passed opt object with callback function if not a function', async () => {
220
+ const handler = {
221
+ event: {
222
+ headers: {},
223
+ input: {
224
+ clientid: '1111-1111-1111-1111',
225
+ },
226
+ },
227
+ };
228
+
229
+ await clientAuthMiddlewareBeforeHandler(handler, next, {
230
+ callback: 'not_function',
231
+ });
232
+
233
+ expect(handler.event).not.toHaveProperty('created_obj');
136
234
  });
137
235
  });
@@ -45,6 +45,19 @@ describe('MiddlewareGroup: test errorHandler middleware', () => {
45
45
  expect(dataBody).toHaveProperty('error.details', '');
46
46
  });
47
47
 
48
+ it('test with formatSuccess argument', async () => {
49
+ const data = await errorHttpResponseHandler({
50
+ error: new ValidationErrorException('Test validation error'),
51
+ formatError: options => {
52
+ return options.error.code;
53
+ },
54
+ });
55
+
56
+ expect(data.statusCode).toBe(400);
57
+
58
+ expect(data.body).toBe('VALIDATION_ERROR');
59
+ });
60
+
48
61
  it('test with thrown custom Error with given parameters', async () => {
49
62
  const data = await errorHttpResponseHandler({
50
63
  error: new ValidationErrorException(
@@ -97,7 +97,7 @@ describe('test gzipHttpResponse gzip', () => {
97
97
  expect(resp).toThrow();
98
98
  } catch (err) {
99
99
  expect(err.name).toEqual('LesgoException');
100
- expect(err.code).toEqual('GZIP_LIB_ERROR');
100
+ expect(err.code).toMatch(/^GZIP_[A-Z]+_ERROR$/);
101
101
  }
102
102
  });
103
103
  });