Skip to content

Commit 7f49828

Browse files
- put the init steps back as other tests depend on the env vars
- set the params to context instead
1 parent fc9e77f commit 7f49828

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

functions/get-index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ function* loadHtml() {
3131
return html;
3232
}
3333

34-
function* getRestaurants() {
35-
let url = URL.parse(process.env.restaurants_api);
34+
function* getRestaurants(restaurantsApiUrl) {
35+
let url = URL.parse(restaurantsApiUrl);
3636
let opts = {
3737
host: url.hostname,
3838
path: url.pathname
@@ -41,14 +41,14 @@ function* getRestaurants() {
4141
aws4.sign(opts);
4242

4343
let httpReq = http({
44-
uri: process.env.restaurants_api,
44+
uri: restaurantsApiUrl,
4545
headers: opts.headers
4646
});
4747

4848
return new Promise((resolve, reject) => {
4949
let f = co.wrap(function* (subsegment) {
5050
if (subsegment) {
51-
subsegment.addMetadata('url', process.env.restaurants_api);
51+
subsegment.addMetadata('url', restaurantsApiUrl);
5252
}
5353

5454
try {
@@ -80,7 +80,7 @@ const handler = co.wrap(function* (event, context, callback) {
8080

8181
let restaurants = yield cloudwatch.trackExecTime(
8282
"GetRestaurantsLatency",
83-
() => getRestaurants()
83+
() => getRestaurants(context.restaurants_api)
8484
);
8585
log.debug(`loaded ${restaurants.length} restaurants`);
8686

@@ -89,10 +89,10 @@ const handler = co.wrap(function* (event, context, callback) {
8989
dayOfWeek,
9090
restaurants,
9191
awsRegion,
92-
cognitoUserPoolId: process.env.cognito_user_pool_id,
93-
cognitoClientId: process.env.cognito_client_id,
94-
searchUrl: `${process.env.restaurants_api}/search`,
95-
placeOrderUrl: `${process.env.orders_api}`
92+
cognitoUserPoolId: context.cognito_user_pool_id,
93+
cognitoClientId: context.cognito_client_id,
94+
searchUrl: `${context.restaurants_api}/search`,
95+
placeOrderUrl: `${context.orders_api}`
9696
};
9797
let html = Mustache.render(template, view);
9898
log.debug(`rendered HTML [${html.length} bytes]`);
@@ -116,6 +116,7 @@ module.exports.handler = middy(handler)
116116
.use(ssm({
117117
cache: true,
118118
cacheExpiryInMillis: 3 * 60 * 1000, // 3 mins
119+
setToContext: true,
119120
names: {
120121
restaurants_api: `/bigmouth/${STAGE}/restaurants_api`,
121122
orders_api: `/bigmouth/${STAGE}/orders_api`,

tests/steps/init.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,46 @@
11
'use strict';
22

3+
const _ = require('lodash');
34
const co = require('co');
45
const Promise = require('bluebird');
56
const aws4 = require('../../lib/aws4');
7+
const AWS = require('aws-sdk');
8+
AWS.config.region = 'us-east-1';
9+
const SSM = new AWS.SSM();
610

711
let initialized = false;
812

13+
const getParameters = co.wrap(function* (keys) {
14+
const prefix = '/bigmouth/dev/';
15+
const req = {
16+
Names: keys.map(key => `${prefix}${key}`)
17+
}
18+
const resp = yield SSM.getParameters(req).promise();
19+
return _.reduce(resp.Parameters, function(obj, param) {
20+
obj[param.Name.substr(prefix.length)] = param.Value
21+
return obj;
22+
}, {})
23+
});
24+
925
let init = co.wrap(function* () {
1026
if (initialized) {
1127
return;
1228
}
1329

14-
process.env.STAGE = 'dev';
30+
const params = yield getParameters([
31+
'cognito_client_id',
32+
'cognito_user_pool_id',
33+
'restaurants_api'
34+
]);
35+
36+
process.env.restaurants_api = params.restaurants_api;
1537
process.env.restaurants_table = "restaurants";
1638
process.env.AWS_REGION = "us-east-1";
39+
process.env.cognito_client_id = params.cognito_client_id;
40+
process.env.cognito_user_pool_id = params.cognito_user_pool_id;
1741
process.env.cognito_server_client_id = "niv7esuaibla0tj5q36b6mvnr";
1842
process.env.AWS_XRAY_CONTEXT_MISSING = "LOG_ERROR";
43+
process.env.STAGE = 'dev';
1944

2045
yield aws4.init();
2146

0 commit comments

Comments
 (0)