Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #43

Merged
merged 3 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
sudo: required
services:
- docker
language: node_js
node_js:
- '4.3.2'
- '6.9.0'
- '4.3.2'
- '6.10.0'
script:
- npm run install-dependencies
- npm run lint
#- npm test
- npm run install-dependencies
- npm run lint
- npm test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ To use Cognito User Pool as user database:

* Install Docker and Docker Compose
* Run `npm install` in project root directory
* Run ./specs-docker.sh
* Run ./run-tests.sh
11 changes: 8 additions & 3 deletions authentication/lib/storage/dynamo/dynamoCache.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';

const table = process.env.CACHE_DB_NAME;
const config = { region: process.env.REGION };

if (process.env.LOCAL_DDB_ENDPOINT) config.endpoint = process.env.LOCAL_DDB_ENDPOINT;

// Common
const AWS = require('aws-sdk');

const config = {
region: AWS.config.region || process.env.REGION || 'eu-west-1',
};

if (process.env.LOCAL_DDB_ENDPOINT) {
Object.assign(config, { endpoint: process.env.LOCAL_DDB_ENDPOINT });
}

const dynamodb = new AWS.DynamoDB.DocumentClient(config);
const crypto = require('crypto');
const Promise = require('bluebird');
Expand Down
11 changes: 8 additions & 3 deletions authentication/lib/storage/dynamo/dynamoUser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';

const table = process.env.USERS_DB_NAME;
const config = { region: process.env.REGION };

if (process.env.LOCAL_DDB_ENDPOINT) config.endpoint = process.env.LOCAL_DDB_ENDPOINT;

// Common
const AWS = require('aws-sdk');

const config = {
region: AWS.config.region || process.env.REGION || 'eu-west-1',
};

if (process.env.LOCAL_DDB_ENDPOINT) {
Object.assign(config, { endpoint: process.env.LOCAL_DDB_ENDPOINT });
}

const dynamodb = new AWS.DynamoDB.DocumentClient(config);

const saveUser = (profile) => {
Expand Down
8 changes: 5 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
---
version: '2'

services:
dynamodb:
image: tray/dynamodb-local
image: dwmkerr/dynamodb
ports:
- "8000:8000"
command: -sharedDb -inMemory -cors '*'
- "8000:8000"
command: -sharedDb -inMemory -cors '*'
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"scripts": {
"lint": "./node_modules/.bin/eslint .",
"test": "./run-tests.sh",
"install-dependencies": "npm run install-authentication && npm run install-test-token",
"install-authentication": "cd authentication && npm install",
"install-test-token": "cd test-token && npm install",
Expand Down
15 changes: 15 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
SERVERLESS_PROJECT="serverless-authentication"
SERVERLESS_STAGE="dev"
SERVERLESS_REGION="eu-west-1"
LOCAL_DDB_ENDPOINT="http://127.0.0.1:8000"

docker-compose up -d

STAGE=$SERVERLESS_STAGE \
REGION=$SERVERLESS_REGION \
PROJECT=$SERVERLESS_PROJECT \
LOCAL_DDB_ENDPOINT=$LOCAL_DDB_ENDPOINT \
./node_modules/.bin/mocha test/

docker-compose down
17 changes: 0 additions & 17 deletions specs-docker.sh

This file was deleted.

4 changes: 3 additions & 1 deletion specs/.eslintrc → test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
"settings": {
"import/core-modules": [
"chai",
"dirty-chai",
"serverless-authentication",
"nock",
"js-yaml",
"aws-sdk",
"async"
]
},
"rules": {
"no-unused-expressions": "off"
},
"env": {
"node": true,
"mocha": true
Expand Down
16 changes: 2 additions & 14 deletions specs/_setup.js → test/_setup.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
'use strict';

const chai = require('chai');
const dirtyChai = require('dirty-chai');
const dynamo = require('./dynamo');

const expect = chai.expect;
chai.use(dirtyChai);

process.env.STAGE = 'dev';
process.env.CACHE_DB_NAME = '{stage}-serverless-authentication-cache';
process.env.COGNITO_IDENTITY_POOL_ID = 'cognito-pool-id';
process.env.COGNITO_REGION = 'eu-west-1';
process.env.COGNITO_PROVIDER_NAME = 'serverless-authentication-boilerplate';
process.env.CACHE_DB_NAME = 'dev-serverless-authentication-cache';
process.env.REDIRECT_CLIENT_URI = 'http://127.0.0.1:3000/';
process.env.TOKEN_SECRET = 'token-secret-123';
process.env.PROVIDER_FACEBOOK_ID = 'fb-mock-id';
Expand All @@ -23,14 +17,8 @@ process.env.PROVIDER_MICROSOFT_SECRET = 'ms-mock-secret';
process.env.PROVIDER_CUSTOM_GOOGLE_ID = 'cg-mock-id';
process.env.PROVIDER_CUSTOM_GOOGLE_SECRET = 'cg-mock-secret';

chai.config.includeStack = false;

global.AssertionError = chai.AssertionError;
global.Assertion = chai.Assertion;

describe('Setup specs', () => {
before(done => dynamo.init(done));

it('Local DynamoDB should be ready', () =>
expect(dynamo.isReady()).to.be.true());
expect(dynamo.isReady()).to.be.equal(true));
});
File renamed without changes.
2 changes: 1 addition & 1 deletion specs/authorization-spec.js → test/authorization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Authorization', () => {
};

authorize(event, (error, data) => {
expect(error).to.be.null();
expect(error).to.be.null;
expect(data.principalId).to.be.equal(payload.id);
done(error);
});
Expand Down
2 changes: 1 addition & 1 deletion specs/custom-google-spec.js → test/custom-google.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('Authentication Provider', () => {
};

refreshHandler(event, (error, data) => {
expect(error).to.be.null();
expect(error).to.be.null;
expect(data.authorization_token).to.match(/[a-zA-Z0-9\-_]+?\.[a-zA-Z0-9\-_]+?\.([a-zA-Z0-9\-_]+)?/);
expect(data.refresh_token).to.match(/[A-Fa-f0-9]{64}/);
done(error);
Expand Down
10 changes: 5 additions & 5 deletions specs/dynamo.js → test/dynamo.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const endpoint = process.env.LOCAL_DDB_ENDPOINT;
const project = process.env.SERVERLESS_PROJECT;
const stage = process.env.SERVERLESS_STAGE;
const region = process.env.SERVERLESS_REGION;
const project = process.env.PROJECT;
const stage = process.env.STAGE;
const region = process.env.REGION;
const table = [stage, project, 'cache'].join('-');

const fs = require('fs');
Expand All @@ -20,15 +20,15 @@ const db = new DynamoDB({ endpoint, region });
let ready = false;

function init(cb) {
resources.Dynamo.Properties.TableName = table;
resources.CacheTable.Properties.TableName = table;
async.waterfall([
(callback) => {
db.listTables({}, callback);
},
(data, callback) => {
const tables = data.TableNames;
if (tables.indexOf(table) < 0) {
db.createTable(resources.Dynamo.Properties, (err, created) => {
db.createTable(resources.CacheTable.Properties, (err, created) => {
if (!err) {
callback(null, `table ${created.TableDescription.TableName} created`);
} else {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion specs/facebook-spec.js → test/facebook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('Authentication Provider', () => {
};

refreshHandler(event, (error, data) => {
expect(error).to.be.null();
expect(error).to.be.null;
expect(data.authorization_token).to.match(/[a-zA-Z0-9\-_]+?\.[a-zA-Z0-9\-_]+?\.([a-zA-Z0-9\-_]+)?/);
expect(data.refresh_token).to.match(/[A-Fa-f0-9]{64}/);
done(error);
Expand Down
2 changes: 1 addition & 1 deletion specs/google-spec.js → test/google.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('Authentication Provider', () => {
};

refreshHandler(event, (error, data) => {
expect(error).to.be.null();
expect(error).to.be.null;
expect(data.authorization_token).to.match(/[a-zA-Z0-9\-_]+?\.[a-zA-Z0-9\-_]+?\.([a-zA-Z0-9\-_]+)?/);
expect(data.refresh_token).to.match(/[A-Fa-f0-9]{64}/);
});
Expand Down
2 changes: 1 addition & 1 deletion specs/microsoft-spec.js → test/microsoft.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('Authentication Provider', () => {
};

refreshHandler(event, (error, data) => {
expect(error).to.be.null();
expect(error).to.be.null;
expect(data.authorization_token).to.match(/[a-zA-Z0-9\-_]+?\.[a-zA-Z0-9\-_]+?\.([a-zA-Z0-9\-_]+)?/);
expect(data.refresh_token).to.match(/[A-Fa-f0-9]{64}/);
expect(data.id).to.equal(event.id);
Expand Down