Skip to content

Instantly share code, notes, and snippets.

@cmawhorter
Last active January 4, 2017 19:28
Show Gist options
  • Select an option

  • Save cmawhorter/67c88aadc13bbd1d3c74a503a22a694c to your computer and use it in GitHub Desktop.

Select an option

Save cmawhorter/67c88aadc13bbd1d3c74a503a22a694c to your computer and use it in GitHub Desktop.

Revisions

  1. cmawhorter revised this gist Jan 4, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions dynogels-credentials-loading-tests.js
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,11 @@ switch (process.env.TEST_STRATEGY) {
    process.env.AWS_ACCESS_KEY_ID = config.accessKeyId + '_not_the_same';
    process.env.AWS_SECRET_ACCESS_KEY = config.secretAccessKey + '_not_the_same';
    break;
    case 'missing':
    config.region = REGION;
    config.accessKeyId = '';
    config.secretAccessKey = '';
    break;
    default:
    throw new Error('credential test strategy missing at process.env.TEST_STRATEGY');
    }
  2. cmawhorter created this gist Jan 4, 2017.
    103 changes: 103 additions & 0 deletions dynogels-credentials-loading-tests.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    'use strict';

    var assert = require('assert');
    var AWS = require('aws-sdk');
    var dynogels = require('dynogels');
    var Enjoi = require('enjoi');

    var config = {
    endpoint: 'http://localhost:8000',
    };
    var table = process.argv[2];
    var lookupId = process.argv[3];

    assert.ok(table, 'table name missing at argv[2]');
    assert.ok(lookupId, 'lookup id missing at argv[3]');

    var REGION = 'us-east-1';

    switch (process.env.TEST_STRATEGY) {
    case 'constructor':
    config.region = REGION;
    config.accessKeyId = 'blah';
    config.secretAccessKey = 'blah';
    break;
    case 'none':
    config.region = REGION;
    break;
    case 'mixed':
    config.region = REGION;
    process.env.AWS_ACCESS_KEY_ID = 'blah';
    process.env.AWS_SECRET_ACCESS_KEY = 'blah';
    break;
    case 'env':
    process.env.AWS_REGION = REGION;
    process.env.AWS_ACCESS_KEY_ID = 'blah';
    process.env.AWS_SECRET_ACCESS_KEY = 'blah';
    break;
    case 'some':
    config.region = REGION;
    config.accessKeyId = 'blah';
    config.secretAccessKey = 'blah';
    process.env.AWS_ACCESS_KEY_ID = 'blah';
    process.env.AWS_SECRET_ACCESS_KEY = 'blah';
    break;
    case 'all':
    config.region = REGION;
    config.accessKeyId = 'blah';
    config.secretAccessKey = 'blah';
    process.env.AWS_REGION = REGION;
    process.env.AWS_ACCESS_KEY_ID = 'blah';
    process.env.AWS_SECRET_ACCESS_KEY = 'blah';
    break;
    case 'mismatch':
    config.region = REGION;
    config.accessKeyId = 'blah';
    config.secretAccessKey = 'blah';
    process.env.AWS_REGION = 'us-west-2';
    assert.notStrictEqual(REGION, process.env.AWS_REGION, 'REGION cannot be same as alternative');
    process.env.AWS_ACCESS_KEY_ID = config.accessKeyId + '_not_the_same';
    process.env.AWS_SECRET_ACCESS_KEY = config.secretAccessKey + '_not_the_same';
    break;
    default:
    throw new Error('credential test strategy missing at process.env.TEST_STRATEGY');
    }
    console.log('options', JSON.stringify({ config, table, env: {
    AWS_REGION: REGION,
    AWS_ACCESS_KEY_ID: 'blah',
    AWS_SECRET_ACCESS_KEY: 'blah',
    } }, null, 2));

    var dynamodb = new AWS.DynamoDB(config);

    dynogels.dynamoDriver(dynamodb);

    assert.strictEqual(REGION, dynamodb.config.region, `region mismatch ${REGION} !== ${dynamodb.config.region}`);

    var SomeModel = dynogels.define('SomeModel', {
    tableName: table,
    hashKey: 'id',

    timestamps: true,
    createdAt: 'created',
    updatedAt: 'updated',

    schema: Enjoi({
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
    "id": {
    "type": "string"
    },
    },
    "required": [ "id" ]
    }),
    });


    SomeModel.get(lookupId, null, (err, result) => {
    assert.ifError(err);
    assert.equal(lookupId, result.get('id'), `lookup data id mismatch ${lookupId} !== ${result.get('id')}`);
    console.log('\n\n\nok %s\n', result.get('id'));
    process.exit();
    });