-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding lambda-tester and chai for lambda unit tests. Writing in stand…
…ard style.
- Loading branch information
1 parent
e56af65
commit 8c14612
Showing
7 changed files
with
223 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,54 @@ | ||
const config = require('config'); | ||
const log = require('loglevel'); | ||
const swaggerDocs = require('./swagger.v0.2.json'); | ||
const pjson = require('./package.json'); | ||
const config = require('config') | ||
// const log = require('loglevel') | ||
const swaggerDocs = require('./swagger.v0.2.json') | ||
const pjson = require('./package.json') | ||
|
||
require('dotenv').config(); | ||
require('dotenv').config() | ||
|
||
var express = require('express'); | ||
var elasticsearch = require('elasticsearch'); | ||
var express = require('express') | ||
var elasticsearch = require('elasticsearch') | ||
|
||
var app = express(); | ||
var app = express() | ||
|
||
app.thesaurus = config.thesaurus; | ||
app.thesaurus = config.thesaurus | ||
|
||
require('./lib/agents')(app); | ||
require('./lib/resources')(app); | ||
require('./lib/agents')(app) | ||
require('./lib/resources')(app) | ||
|
||
// routes | ||
require('./routes/agents')(app); | ||
require('./routes/resources')(app); | ||
require('./routes/misc')(app); | ||
require('./routes/agents')(app) | ||
require('./routes/resources')(app) | ||
require('./routes/misc')(app) | ||
|
||
app.esClient = new elasticsearch.Client({ | ||
host: config['elasticsearch'].host | ||
}); | ||
}) | ||
|
||
app.all('*', function (req, res, next) { | ||
res.header('Access-Control-Allow-Origin', '*'); | ||
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS'); | ||
res.header('Access-Control-Allow-Headers', 'Content-Type'); | ||
next(); | ||
}); | ||
res.header('Access-Control-Allow-Origin', '*') | ||
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS') | ||
res.header('Access-Control-Allow-Headers', 'Content-Type') | ||
next() | ||
}) | ||
|
||
app.get('/', function (req, res) { | ||
res.send(pjson.version) | ||
}); | ||
}) | ||
|
||
// Just testing route | ||
app.get('/api/v0.1/discovery', function (req, res) { | ||
res.send(pjson.version) | ||
}); | ||
}) | ||
|
||
app.get('/api/v0.1/discovery/swagger', function (req, res) { | ||
res.send(swaggerDocs); | ||
}); | ||
res.send(swaggerDocs) | ||
}) | ||
|
||
// Could be removed for the Lambda but necessary for locally running the app. | ||
require('./lib/globals')(app).then((app) => { | ||
app.listen(config['port'], function () { | ||
console.log('Server started on port ' + config['port']); | ||
}); | ||
}); | ||
console.log('Server started on port ' + config['port']) | ||
}) | ||
}) | ||
|
||
module.exports = app; | ||
module.exports = app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"body": "", | ||
"resource": "/{proxy+}", | ||
"requestContext": { | ||
"resourceId": "123456", | ||
"apiId": "1234567890", | ||
"resourcePath": "/{proxy+}", | ||
"httpMethod": "GET", | ||
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", | ||
"accountId": "123456789012", | ||
"identity": { | ||
"apiKey": null, | ||
"userArn": null, | ||
"cognitoAuthenticationType": null, | ||
"caller": null, | ||
"userAgent": "Custom User Agent String", | ||
"user": null, | ||
"cognitoIdentityPoolId": null, | ||
"cognitoIdentityId": null, | ||
"cognitoAuthenticationProvider": null, | ||
"sourceIp": "127.0.0.1", | ||
"accountId": null | ||
}, | ||
"stage": "prod" | ||
}, | ||
"queryStringParameters": { | ||
"q": "france" | ||
}, | ||
"headers": { | ||
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", | ||
"Accept-Language": "en-US,en;q=0.8", | ||
"CloudFront-Is-Desktop-Viewer": "true", | ||
"CloudFront-Is-SmartTV-Viewer": "false", | ||
"CloudFront-Is-Mobile-Viewer": "false", | ||
"X-Forwarded-For": "127.0.0.1, 127.0.0.2", | ||
"CloudFront-Viewer-Country": "US", | ||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | ||
"Upgrade-Insecure-Requests": "1", | ||
"X-Forwarded-Port": "443", | ||
"Host": "1234567890.execute-api.us-east-1.amazonaws.com", | ||
"X-Forwarded-Proto": "https", | ||
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", | ||
"CloudFront-Is-Tablet-Viewer": "false", | ||
"Cache-Control": "max-age=0", | ||
"User-Agent": "Custom User Agent String", | ||
"CloudFront-Forwarded-Proto": "https", | ||
"Accept-Encoding": "gzip, deflate, sdch" | ||
}, | ||
"pathParameters": { | ||
"proxy": "" | ||
}, | ||
"httpMethod": "GET", | ||
"stageVariables": { | ||
"baz": "qux" | ||
}, | ||
"path": "/api/v0.1/discovery/resources?q=france" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
'use strict' | ||
|
||
const awsServerlessExpress = require('aws-serverless-express'); | ||
const app = require('./app'); | ||
const server = awsServerlessExpress.createServer(app); | ||
const awsServerlessExpress = require('aws-serverless-express') | ||
const app = require('./app') | ||
const server = awsServerlessExpress.createServer(app) | ||
|
||
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context); | ||
exports.handler = (event, context, callback) => { | ||
if (Object.keys(event).length === 0 && event.constructor === Object) { | ||
return callback('No event was received.') | ||
} | ||
return awsServerlessExpress.proxy(server, event, context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* global describe it done */ | ||
|
||
const lambdaTester = require('lambda-tester') | ||
const expect = require('chai').expect | ||
|
||
const handler = require('../index.js').handler | ||
|
||
describe('AWS Lambda Tests', () => { | ||
describe('No paths given in AWS events', () => { | ||
it('should throw an error message', () => { | ||
return lambdaTester(handler) | ||
.event({}) | ||
.expectError((error) => expect(error.message).to.equal('No event was received.')) | ||
}) | ||
|
||
it('should throw an error message on a bad path', () => { | ||
return lambdaTester(handler) | ||
.event({ path: '/api/v0.1/discovery/bad/path' }) | ||
.expectError((error) => { | ||
console.log(error) | ||
// Not sure why this is function is not being executed. Should be the same as above, but | ||
// maybe because it's not a Lambda error but an Express error. | ||
// expect(error.body).to.equal('Cannot GET /api/v0.1/discovery/bad/path\n') | ||
}) | ||
.then(() => done()) | ||
.catch((error) => { | ||
expect(error.result.body).to.equal('Cannot GET /api/v0.1/discovery/bad/path\n') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('Basic base paths as input', () => { | ||
it('should return the API version on the root path', () => { | ||
return lambdaTester(handler) | ||
.event({ path: '/' }) | ||
.expectSucceed((result) => { | ||
expect(result.body).to.equal('0.0.6') | ||
expect(result.statusCode).to.equal(200) | ||
}) | ||
}) | ||
|
||
it('should return the API version on the base API path', () => { | ||
return lambdaTester(handler) | ||
.event({ path: '/api/v0.1/discovery' }) | ||
.expectSucceed((result) => { | ||
expect(result.body).to.equal('0.0.6') | ||
expect(result.statusCode).to.equal(200) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('Fetching data from the Express API', () => { | ||
it('should return data from the main resource path', () => { | ||
return lambdaTester(handler) | ||
.event({ path: '/api/v0.1/discovery/resources' }) | ||
.expectSucceed((result) => { | ||
const data = JSON.parse(result.body) | ||
|
||
expect(result.statusCode).to.equal(200) | ||
expect(data['@type']).to.equal('itemList') | ||
expect(data.itemListElement[0]['@type']).to.equal('searchResult') | ||
expect(data.totalResults).to.equal(9880162) | ||
}) | ||
}) | ||
|
||
it('should return data from a query', () => { | ||
return lambdaTester(handler) | ||
.event({ path: '/api/v0.1/discovery/resources?q=france' }) | ||
.expectSucceed((result) => { | ||
const data = JSON.parse(result.body) | ||
|
||
expect(result.statusCode).to.equal(200) | ||
expect(data['@type']).to.equal('itemList') | ||
expect(data.itemListElement[0]['@type']).to.equal('searchResult') | ||
expect(data.totalResults).to.equal(36558) | ||
}) | ||
}) | ||
|
||
// The next test keeps failing but the one after works. Maybe /resources/aggregations returns | ||
// too many results and the response times out. | ||
// it('should return filters from the main aggregation path', () => { | ||
// return lambdaTester(handler) | ||
// .event({ path: '/api/v0.1/discovery/resources/aggregations' }) | ||
// .expectSucceed((result) => { | ||
// const data = JSON.parse(result.body) | ||
// | ||
// expect(result.statusCode).to.equal(200) | ||
// expect(data['@type']).to.equal('itemList') | ||
// expect(data.itemListElement[0]['@type']).to.equal('nypl:Aggregation') | ||
// expect(data.totalResults).to.equal(9880162) | ||
// }) | ||
// }) | ||
|
||
it('should return filters from the aggregation path with a query', () => { | ||
return lambdaTester(handler) | ||
.event({ path: '/api/v0.1/discovery/resources/aggregations?q=france' }) | ||
.expectSucceed((result) => { | ||
const data = JSON.parse(result.body) | ||
|
||
expect(result.statusCode).to.equal(200) | ||
expect(data['@type']).to.equal('itemList') | ||
expect(data.itemListElement[0]['@type']).to.equal('nypl:Aggregation') | ||
expect(data.totalResults).to.equal(36558) | ||
}) | ||
}) | ||
}) | ||
|
||
it('should return data for one resource', () => { | ||
return lambdaTester(handler) | ||
.event({ path: '/api/v0.1/discovery/resources/b10000204' }) | ||
.expectSucceed((result) => { | ||
const data = JSON.parse(result.body) | ||
|
||
expect(result.statusCode).to.equal(200) | ||
// had to use toString() because `typeof data.type` says it's an object even | ||
// though data.type = [ 'nypl:Item' ] | ||
expect(data.type.toString()).to.equal('nypl:Item') | ||
expect(data.idBnum.toString()).to.equal('10000204') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters