From f7150b2b2aeb41ae67a71aba67fb31ab3b671dce Mon Sep 17 00:00:00 2001 From: ryanp Date: Tue, 1 Sep 2015 22:16:31 -0500 Subject: [PATCH] todos --- package.json | 3 - tests/all.js | 8 ++- tests/cli/deploy_api.js | 38 ++++++----- tests/cli/deploy_lambda.js | 39 +++++------ tests/cli/env.js | 37 +++++----- tests/cli/install.js | 35 +++++----- tests/cli/new.js | 5 +- tests/cli/tag.js | 93 ++++++++++++------------- tests/config.js | 6 +- tests/deployApiTest-cf.json | 132 ++++++++++++++++++++++++++++++++++++ tests/test_utils.js | 6 +- 11 files changed, 269 insertions(+), 133 deletions(-) create mode 100644 tests/deployApiTest-cf.json diff --git a/package.json b/package.json index ca9f7c1e1..006daa2e6 100644 --- a/package.json +++ b/package.json @@ -28,10 +28,7 @@ "test": "mocha tests/all" }, "devDependencies": { - "adm-zip": "^0.4.7", "chai": "^3.2.0", - "hoek": "^2.11.1", - "lodash": "^3.2.0", "mocha": "^2.2.5" }, "dependencies": { diff --git a/tests/all.js b/tests/all.js index 20d603a65..0451de737 100644 --- a/tests/all.js +++ b/tests/all.js @@ -1,6 +1,8 @@ 'use strict'; -require('config'); //init config +//TODO: doc on needing to set env vars +//TODO: must setup an env var file for unittest +require('./config'); //init config describe('AllTests', function() { before(function(done) { @@ -13,9 +15,9 @@ describe('AllTests', function() { //require tests vs inline so we can run sequentially // Require Tests ("new" must be last) - require('./cli/tag'); + require('./cli/tag'); //does not create AWS resources require('./cli/deploy_lambda'); - require('./cli/deploy_api'); + //require('./cli/deploy_api'); require('./cli/install'); require('./cli/env'); require('./cli/new'); diff --git a/tests/cli/deploy_api.js b/tests/cli/deploy_api.js index 689fad8d0..5647e7947 100644 --- a/tests/cli/deploy_api.js +++ b/tests/cli/deploy_api.js @@ -5,27 +5,29 @@ * - Copies the test-prj template to your system's temp directory * - Deploys an API based on the endpoints in the project */ -var testUtils = require('../test_utils'); +var testUtils = require('../test_utils'), + path = require('path'), + assert = require('chai').assert; -module.exports = function(testData, cb) { +var config = require('../config'); - describe('Test deploy api command', function() { +describe('Test deploy api command', function() { - before(function() { - testData.projectPath = testUtils.createTestProject( - testData.name, - testData.region, - testData.stage, - testData.iamRoleARN, - testData.envBucket); - process.chdir(testData.projectPath); - }); + before(function() { + config.projectPath = testUtils.createTestProject( + config.name, + config.region, + config.stage, + config.iamRoleARN, + config.envBucket); + process.chdir(config.projectPath); + }); - after(function(done) { - cb(testData); - done(); - }); + after(function(done) { + done(); + }); + describe('Positive tests', function() { it('Deploy REST API', function(done) { this.timeout(0); @@ -35,7 +37,7 @@ module.exports = function(testData, cb) { JawsError = require('../../lib/jaws-error'); // Test - JAWS.deployApi(testData.stage) + JAWS.deployApi(config.stage) .then(function() { done(); }) @@ -47,4 +49,4 @@ module.exports = function(testData, cb) { }); }); }); -}; \ No newline at end of file +}); \ No newline at end of file diff --git a/tests/cli/deploy_lambda.js b/tests/cli/deploy_lambda.js index aba71d914..50d736010 100644 --- a/tests/cli/deploy_lambda.js +++ b/tests/cli/deploy_lambda.js @@ -3,29 +3,30 @@ /** * JAWS Test: Deploy Lambda Command */ - var testUtils = require('../test_utils'), - path = require('path'); + path = require('path'), + assert = require('chai').assert; -module.exports = function(testData, cb) { +var config = require('../config'); - describe('Test "deploy lambda" command', function() { - before(function() { - testData.projectPath = testUtils.createTestProject( - testData.name, - testData.region, - testData.stage, - testData.iamRoleARN, - testData.envBucket); - process.chdir(path.join(testData.projectPath, 'back/users/lambdas/show')); - }); +describe('Test "deploy lambda" command', function() { - after(function(done) { - cb(testData); - done(); - }); + before(function() { + config.projectPath = testUtils.createTestProject( + config.name, + config.region, + config.stage, + config.iamRoleARN, + config.envBucket); + process.chdir(path.join(config.projectPath, 'back/users/lambdas/show')); + }); + after(function(done) { + done(); + }); + + describe('Positive tests', function() { it('Deploy Lambda', function(done) { this.timeout(0); @@ -34,7 +35,7 @@ module.exports = function(testData, cb) { var JAWS = require('../../lib/index.js'); // Test - JAWS.deployLambdas(testData.stage, false, false) + JAWS.deployLambdas(config.stage, false, false) .then(function(d) { done(); }) @@ -43,4 +44,4 @@ module.exports = function(testData, cb) { }); }); }); -}; \ No newline at end of file +}); \ No newline at end of file diff --git a/tests/cli/env.js b/tests/cli/env.js index 8bd934259..458f3f784 100644 --- a/tests/cli/env.js +++ b/tests/cli/env.js @@ -5,33 +5,34 @@ */ var testUtils = require('../test_utils'), - path = require('path'); + path = require('path'), + assert = require('chai').assert; -module.exports = function(testData, cb) { +var config = require('../config'); - describe('Test "env" command', function() { +describe('Test "env" command', function() { - before(function() { - testData.projectPath = testUtils.createTestProject( - testData.name, - testData.region, - testData.stage, - testData.iamRoleARN, - testData.envBucket); - process.chdir(path.join(testData.projectPath, 'back/users/lambdas/show')); - }); + before(function() { + config.projectPath = testUtils.createTestProject( + config.name, + config.region, + config.stage, + config.iamRoleARN, + config.envBucket); + process.chdir(path.join(config.projectPath, 'back/users/lambdas/show')); + }); - after(function(done) { - cb(testData); - done(); - }); + after(function(done) { + done(); + }); + describe('Positive tests', function() { it('Test env command', function(done) { this.timeout(0); var JAWS = require('../../lib/index.js'); - JAWS.listEnv(testData.stage) + JAWS.listEnv(config.stage) .then(function(d) { done(); }) @@ -40,4 +41,4 @@ module.exports = function(testData, cb) { }); }); }); -}; \ No newline at end of file +}); \ No newline at end of file diff --git a/tests/cli/install.js b/tests/cli/install.js index 57afcdc5f..137f20e29 100644 --- a/tests/cli/install.js +++ b/tests/cli/install.js @@ -5,27 +5,28 @@ */ var testUtils = require('../test_utils'), - path = require('path'); + path = require('path'), + assert = require('chai').assert; -module.exports = function(testData, cb) { +var config = require('../config'); - describe('Test "install" command', function() { +describe('Test "install" command', function() { - before(function() { - testData.projectPath = testUtils.createTestProject( - testData.name, - testData.region, - testData.stage, - testData.iamRoleARN, - testData.envBucket); - process.chdir(path.join(testData.projectPath, 'back/users/lambdas/show')); - }); + before(function() { + config.projectPath = testUtils.createTestProject( + config.name, + config.region, + config.stage, + config.iamRoleARN, + config.envBucket); + process.chdir(path.join(config.projectPath, 'back/users/lambdas/show')); + }); - after(function(done) { - cb(testData); - done(); - }); + after(function(done) { + done(); + }); + describe('Positive tests', function() { it('Install module', function(done) { this.timeout(0); @@ -44,4 +45,4 @@ module.exports = function(testData, cb) { }); }); }); -}; \ No newline at end of file +}); \ No newline at end of file diff --git a/tests/cli/new.js b/tests/cli/new.js index 0fa62fdf0..5bfb74140 100644 --- a/tests/cli/new.js +++ b/tests/cli/new.js @@ -8,10 +8,9 @@ var path = require('path'), os = require('os'), assert = require('chai').assert, - shortid = require('shortid'), - AWS = require('aws-sdk'); + shortid = require('shortid'); -var config = require('./config'); +var config = require('../config'); describe('Test new command', function() { diff --git a/tests/cli/tag.js b/tests/cli/tag.js index ba5fcd667..6fd00c638 100644 --- a/tests/cli/tag.js +++ b/tests/cli/tag.js @@ -10,57 +10,58 @@ var testUtils = require('../test_utils'), assert = require('chai').assert, Promise = require('bluebird'); -module.exports = function(testData, cb) { +var config = require('../config'); - describe('Test "tag" command', function() { +describe('Test "tag" command', function() { - before(function(done) { - testData.projectPath = testUtils.createTestProject( - testData.name, - testData.region, - testData.stage, - testData.iamRoleARN, - testData.envBucket); - process.chdir(testData.projectPath); + before(function(done) { + config.projectPath = testUtils.createTestProject( + config.name, + config.region, + config.stage, + config.iamRoleARN, + config.envBucket); - // Get Lambda Paths - testData.lambda1 = path.join(testData.projectPath, 'back', 'users', 'lambdas', 'show', 'jaws.json'); - testData.lambda2 = path.join(testData.projectPath, 'back', 'users', 'lambdas', 'signin', 'jaws.json'); - testData.lambda3 = path.join(testData.projectPath, 'back', 'users', 'lambdas', 'signup', 'jaws.json'); - done(); - }); + process.chdir(config.projectPath); - after(function(done) { - cb(testData); - done(); - }); + // Get Lambda Paths + config.lambda1 = path.join(config.projectPath, 'back', 'users', 'lambdas', 'show', 'jaws.json'); + config.lambda2 = path.join(config.projectPath, 'back', 'users', 'lambdas', 'signin', 'jaws.json'); + config.lambda3 = path.join(config.projectPath, 'back', 'users', 'lambdas', 'signup', 'jaws.json'); + done(); + }); - it('tag lambdas', function (done) { + after(function(done) { + done(); + }); + + describe('Positive tests', function() { + it('tag lambdas', function(done) { this.timeout(0); var JAWS = require('../../lib/index.js'); - JAWS.tag('lambda', testData.lambda1, '') - .then(function () { - assert.equal(true, require(testData.lambda1).lambda.deploy); - assert.equal(false, require(testData.lambda2).lambda.deploy); - assert.equal(false, require(testData.lambda3).lambda.deploy); + JAWS.tag('lambda', config.lambda1, false) + .then(function() { + assert.equal(true, require(config.lambda1).lambda.deploy); + assert.equal(false, require(config.lambda2).lambda.deploy); + assert.equal(false, require(config.lambda3).lambda.deploy); return JAWS.tagAll('lambda', false); }) - .then(function () { - assert.equal(true, require(testData.lambda1).lambda.deploy); - assert.equal(true, require(testData.lambda2).lambda.deploy); - assert.equal(true, require(testData.lambda3).lambda.deploy); + .then(function() { + assert.equal(true, require(config.lambda1).lambda.deploy); + assert.equal(true, require(config.lambda2).lambda.deploy); + assert.equal(true, require(config.lambda3).lambda.deploy); return JAWS.tagAll('lambda', true); }) - .then(function () { - assert.equal(false, require(testData.lambda1).lambda.deploy); - assert.equal(false, require(testData.lambda2).lambda.deploy); - assert.equal(false, require(testData.lambda3).lambda.deploy); + .then(function() { + assert.equal(false, require(config.lambda1).lambda.deploy); + assert.equal(false, require(config.lambda2).lambda.deploy); + assert.equal(false, require(config.lambda3).lambda.deploy); done(); }) - .error(function (e) { + .error(function(e) { done(e); }); }); @@ -70,23 +71,23 @@ module.exports = function(testData, cb) { var JAWS = require('../../lib/index.js'); - JAWS.tag('api', testData.lambda1) + JAWS.tag('api', config.lambda1) .then(function() { - assert.equal(true, require(testData.lambda1).lambda.deploy); - assert.equal(false, require(testData.lambda2).lambda.deploy); - assert.equal(false, require(testData.lambda3).lambda.deploy); + assert.equal(true, require(config.lambda1).lambda.deploy); + assert.equal(false, require(config.lambda2).lambda.deploy); + assert.equal(false, require(config.lambda3).lambda.deploy); return JAWS.tagAll('api', false); }) .then(function() { - assert.equal(true, require(testData.lambda1).lambda.deploy); - assert.equal(true, require(testData.lambda2).lambda.deploy); - assert.equal(true, require(testData.lambda3).lambda.deploy); + assert.equal(true, require(config.lambda1).lambda.deploy); + assert.equal(true, require(config.lambda2).lambda.deploy); + assert.equal(true, require(config.lambda3).lambda.deploy); return JAWS.tagAll('api', true); }) .then(function() { - assert.equal(false, require(testData.lambda1).lambda.deploy); - assert.equal(false, require(testData.lambda2).lambda.deploy); - assert.equal(false, require(testData.lambda3).lambda.deploy); + assert.equal(false, require(config.lambda1).lambda.deploy); + assert.equal(false, require(config.lambda2).lambda.deploy); + assert.equal(false, require(config.lambda3).lambda.deploy); done(); }) .error(function(e) { @@ -94,4 +95,4 @@ module.exports = function(testData, cb) { }); }); }); -}; \ No newline at end of file +}); \ No newline at end of file diff --git a/tests/config.js b/tests/config.js index 22d889d64..e7ca6d2b4 100644 --- a/tests/config.js +++ b/tests/config.js @@ -3,8 +3,8 @@ var path = require('path'), AWS = require('aws-sdk'); -// Require ENV vars -require('dotenv').config({path: __dirname + '/.env'}); +// Require ENV vars, can also set ENV vars in your IDE +require('dotenv').config({path: path.join(__dirname, '.env'), silent: true}); var config = { name: 'test-prj', @@ -13,7 +13,7 @@ var config = { region: 'us-east-1', envBucket: process.env.TEST_JAWS_ENV_BUCKET, profile: process.env.TEST_JAWS_PROFILE, - iamRoleARN: process.env.TEST_JAWS_IAM_ROLE, + iamRoleARN: process.env.TEST_JAWS_IAM_ROLE, //must manually create in IAM UI, TODO: create CF that creates this user }; AWS.config.credentials = new AWS.SharedIniFileCredentials({ diff --git a/tests/deployApiTest-cf.json b/tests/deployApiTest-cf.json new file mode 100644 index 000000000..1cfc82352 --- /dev/null +++ b/tests/deployApiTest-cf.json @@ -0,0 +1,132 @@ +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Description": "The AWS CloudFormation template for this JAWS project", + "Parameters": { + "aaProjectName": { + "Type": "String", + "Default": "jaws", + "AllowedValues": [ + "jaws" + ] + }, + "aaStage": { + "Type": "String", + "Default": "test" + }, + "aaDataModelPrefix": { + "Type": "String", + "Default": "test", + "AllowedValues": [ + "test" + ] + }, + "aaHostedZoneName": { + "Type": "String", + "Default": "myapp.com" + }, + "aaNotficationEmail": { + "Type": "String", + "Default": "you@you.com" + }, + "aaDefaultDynamoRWThroughput": { + "Type": "String", + "Default": "1" + } + }, + "Resources": { + "LambdaRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": { + "Service": [ + "apigateway.amazonaws.com" + ] + }, + "Action": [ + "sts:AssumeRole" + ] + } + ] + }, + "Path": "/" + } + }, + "Profile": { + "Type": "AWS::IAM::InstanceProfile", + "Properties": { + "Path": "/", + "Roles": [ + { + "Ref": "LambdaRole" + } + ] + } + }, + "Group": { + "Type": "AWS::IAM::Group", + "Properties": { + "Path": "/dataModel/" + } + }, + "Policy": { + "Type": "AWS::IAM::Policy", + "Properties": { + "PolicyName": { + "Fn::Join": [ + "_-_", + [ + { + "Ref": "aaStage" + }, + { + "Ref": "aaProjectName" + } + ] + ] + }, + "PolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "lambda:InvokeFunction", + "iam:PassRole", + "logs:CreateLogGroup", + "logs:CreateLogStream", + "logs:PutLogEvents" + ], + "Resource": "arn:aws:logs:*:*:*" + } + ] + }, + "Roles": [ + { + "Ref": "LambdaRole" + } + ], + "Groups": [ + { + "Ref": "Group" + } + ] + } + } + }, + "Outputs": { + "LambdaRoleARN": { + "Description": "ARN of the lambda IAM role", + "Value": { + "Fn::GetAtt": [ + "LambdaRole", + "Arn" + ] + } + } + } +} \ No newline at end of file diff --git a/tests/test_utils.js b/tests/test_utils.js index dc3256287..a5be9a580 100644 --- a/tests/test_utils.js +++ b/tests/test_utils.js @@ -12,9 +12,9 @@ var fs = require('fs'), module.exports.createTestProject = function(projectName, projectRegion, projectStage, projectIAMRole, projectEnvBucket) { // Create Test Project - var projectPath = path.join(os.tmpdir(), './', projectName); + var projectPath = path.join(os.tmpdir(), projectName); if (fs.existsSync(projectPath)) { - del.sync([projectPath], { force: true }); + del.sync([projectPath], {force: true}); } // Copy test project to temp directory @@ -37,7 +37,7 @@ module.exports.createTestProject = function(projectName, projectRegion, projectS fs.writeFileSync(path.join(projectPath, 'jaws.json'), projectJSON); // Create admin.env file - fs.writeFileSync(path.join(projectPath, 'admin.env'), 'ADMIN_AWS_PROFILE=jawstest'); + fs.writeFileSync(path.join(projectPath, 'admin.env'), 'ADMIN_AWS_PROFILE=' + process.env.TEST_JAWS_PROFILE); return projectPath; }; \ No newline at end of file