Skip to content
This repository has been archived by the owner on Mar 31, 2020. It is now read-only.

Commit

Permalink
Add Unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gbuddappagari committed Sep 23, 2016
1 parent c1cc40a commit 2d67a3e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -23,10 +23,12 @@
},
"homepage": "https://github.com/gbuddappagari/mnemosyne",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.0.2"
},
"dependencies": {
"aws-sdk": "^2.6.2",
"crypto": "0.0.3",
"dynamodb-doc": "^1.0.0",
"https": "^1.0.0",
"lambda-tester": "^2.6.0",
Expand Down
6 changes: 3 additions & 3 deletions src/registerWebhook.js
Expand Up @@ -15,17 +15,17 @@ var options = {
var data = {
"config": {
"url" : "https://9yui64kvxi.execute-api.us-east-1.amazonaws.com/cd/test-post-lambda",
"content_type" : "json"
"content_type" : "json",
"secret" : process.env.SECRET_KEY
},

"events": [ "node-change/mac:14cfe2142142/.*","SYNC_NOTIFICATION","transaction-status" ]
"events": [ "node-change/mac:14cfe2142142/.*","SYNC_NOTIFICATION","transaction-status", "device-status" ]
};

exports.handler = (event, context, callback) => {
const req = https.request(options, (res) => {
var body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
Expand Down
41 changes: 41 additions & 0 deletions test/registerWebhook.test.js
@@ -0,0 +1,41 @@
var expect = require('chai').expect;
var LambdaTester = require( 'lambda-tester' );
var registerLambda = require( '../src/registerWebhook' );
var verifyLambda = require( './verifyWebhook' );

var data = {
"config": {
"url" : "https://9yui64kvxi.execute-api.us-east-1.amazonaws.com/cd/test-post-lambda",
"content_type" : "json",
"secret" : process.env.SECRET_KEY
},

"events": [ "node-change/mac:14cfe2142142/.*","SYNC_NOTIFICATION","transaction-status", "device-status" ]
};

describe( 'Webhook Unit Testing', function() {
this.timeout(5000);

it( 'Registered', function() {

return LambdaTester( registerLambda.handler )
.event( { } )
.expectResult(function( result )
{
result = JSON.parse(result);
expect(result.message).to.equal('Success');

});
});

it('Verified', function(){

return LambdaTester( verifyLambda.handler )
.event( data )
.expectResult(function( result )
{
expect(result).to.equal(true);
});
});

});
42 changes: 42 additions & 0 deletions test/verifyWebhook.js
@@ -0,0 +1,42 @@
var https = require('https');

var options = {
host: 'api-cd.webpa.comcast.net',
port: 8090,
path: '/api/v2/hooks',
method: 'GET',
headers: {
'content-type': 'application/json',
'Authorization': 'Basic '+process.env.WEBPA_AUTH_HEADER
}
};

exports.handler = (event, context, callback) => {
const req = https.request(options, (res) => {
var body = '';
console.log('Status:', res.statusCode);
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Successfully processed HTTPS response');
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
for(var i=0; i<body.length; i++)
{
if(JSON.stringify(body[i].config) === JSON.stringify(event.config))
{
if(JSON.stringify(body[i].events) === JSON.stringify(event.events))
{
callback(null, true);
break;
}
callback(new Error(false));
break;
}
}
}
});
});
req.on('error', callback);
req.end();
};

0 comments on commit 2d67a3e

Please sign in to comment.