From 568d675f815da5483788e7db40556e544ef2fe90 Mon Sep 17 00:00:00 2001 From: Gavin Bauman Date: Thu, 17 May 2018 17:18:09 -0400 Subject: [PATCH] Add ESLint. Adopted standards (#7) --- .eslintrc.json | 3 + index.js | 6 +- package.json | 6 ++ parsers.js | 50 ++++++------- test/basic.js | 186 +++++++++++++++++++++++++------------------------ validators.js | 28 ++++---- 6 files changed, 145 insertions(+), 134 deletions(-) create mode 100644 .eslintrc.json diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..d658236 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "standard" +} \ No newline at end of file diff --git a/index.js b/index.js index f3778d9..712021f 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ module.exports = { - parsers: require('./parsers'), - validators: require('./validators') -} \ No newline at end of file + parsers: require('./parsers'), + validators: require('./validators') +} diff --git a/package.json b/package.json index 6a19b0f..d3eb0e3 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,12 @@ "author": "Ben Greenier", "license": "MIT", "devDependencies": { + "eslint": "^4.19.1", + "eslint-config-standard": "^11.0.0", + "eslint-plugin-import": "^2.11.0", + "eslint-plugin-node": "^6.0.1", + "eslint-plugin-promise": "^3.7.0", + "eslint-plugin-standard": "^3.0.1", "mocha": "^5.0.0" } } diff --git a/parsers.js b/parsers.js index 2775314..1b2efbd 100644 --- a/parsers.js +++ b/parsers.js @@ -1,30 +1,30 @@ const validate = require('./validators') module.exports = { - accessTokenToCredentials: (req) => { - // ensure we have a valid accessToken before proceeding - validate.accessToken(req) + accessTokenToCredentials: (req) => { + // ensure we have a valid accessToken before proceeding + validate.accessToken(req) - // this implements the contract defined by msRestAzure - // see https://github.com/Azure/azure-sdk-for-node/blob/0a52678ea7b3a24a478975f7169fc30e9fc9759e/runtime/ms-rest-azure/lib/credentials/deviceTokenCredentials.js - return { - signRequest: (webResource, callback) => { - webResource.headers['Authorization'] = req.body.accessToken - callback(null) - } - } - }, - resourcesToObjects: (req) => { - // ensure we have a valid resources array before proceeding - validate.resources(req) - - // parse the resources into objects - return req.body.resources.map(r => r.split('/')).map(r => { - return { - subscriptionId: r[0], - resourceGroupName: r[1], - resourceName: r[2] - } - }) + // this implements the contract defined by msRestAzure + // see https://github.com/Azure/azure-sdk-for-node/blob/0a52678ea7b3a24a478975f7169fc30e9fc9759e/runtime/ms-rest-azure/lib/credentials/deviceTokenCredentials.js + return { + signRequest: (webResource, callback) => { + webResource.headers['Authorization'] = req.body.accessToken + callback(null) + } } -} \ No newline at end of file + }, + resourcesToObjects: (req) => { + // ensure we have a valid resources array before proceeding + validate.resources(req) + + // parse the resources into objects + return req.body.resources.map(r => r.split('/')).map(r => { + return { + subscriptionId: r[0], + resourceGroupName: r[1], + resourceName: r[2] + } + }) + } +} diff --git a/test/basic.js b/test/basic.js index a7cde4e..b5cb72e 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,98 +1,100 @@ const assert = require('assert') const index = require('../') +/* eslint-env node, mocha */ + describe('platform-chaos', () => { - it('is named properly', () => { - assert.equal(require('../package.json').name, 'platform-chaos') - assert.equal(require('../package-lock.json').name, 'platform-chaos') - }) - it('parses resources', () => { - assert.throws(() => { - index.validators.resources({ - body: { - resources: [1] - } - }) - }) - - assert.throws(() => { - index.validators.resources({ - body: { - resources: [''] - } - }) - }) - - assert.throws(() => { - index.validators.resources({ - body: { - resources: ['one/two/three/four'] - } - }) - }) - - const instance = index.parsers.resourcesToObjects({ - body: { - resources: ['sub/rg/resource'] - } - })[0] - - assert.equal(instance.subscriptionId, "sub") - assert.equal(instance.resourceGroupName, "rg") - assert.equal(instance.resourceName, "resource") - }) - - it('parses accessTokens', () => { - // invalid at - assert.throws(() => { - index.validators.accessToken({ - body: { - accessToken: 1 - } - }) - }) - - // empty resourceIds - assert.throws(() => { - index.validators.accessToken({ - body: { - accessToken: "valid type" - } - }) - }) - - // invalid resourceIds type - assert.throws(() => { - index.validators.accessToken({ - body: { - accessToken: "valid type" - } - }) - }) - - // invalid resourceIds format - assert.throws(() => { - index.validators.accessToken({ - body: { - accessToken: "valid type" - } - }) - }) - - // valid - const expectedAccessToken = "Bearer 12345234r2" - const instance =index.parsers.accessTokenToCredentials({ - body: { - accessToken: expectedAccessToken - } - }) - - const res = { - headers: {} + it('is named properly', () => { + assert.equal(require('../package.json').name, 'platform-chaos') + assert.equal(require('../package-lock.json').name, 'platform-chaos') + }) + it('parses resources', () => { + assert.throws(() => { + index.validators.resources({ + body: { + resources: [1] } - - instance.signRequest(res, () => {}) - - assert.equal(res.headers['Authorization'], expectedAccessToken) + }) }) -}) \ No newline at end of file + + assert.throws(() => { + index.validators.resources({ + body: { + resources: [''] + } + }) + }) + + assert.throws(() => { + index.validators.resources({ + body: { + resources: ['one/two/three/four'] + } + }) + }) + + const instance = index.parsers.resourcesToObjects({ + body: { + resources: ['sub/rg/resource'] + } + })[0] + + assert.equal(instance.subscriptionId, 'sub') + assert.equal(instance.resourceGroupName, 'rg') + assert.equal(instance.resourceName, 'resource') + }) + + it('parses accessTokens', () => { + // invalid at + assert.throws(() => { + index.validators.accessToken({ + body: { + accessToken: 1 + } + }) + }) + + // empty resourceIds + assert.throws(() => { + index.validators.accessToken({ + body: { + accessToken: 'valid type' + } + }) + }) + + // invalid resourceIds type + assert.throws(() => { + index.validators.accessToken({ + body: { + accessToken: 'valid type' + } + }) + }) + + // invalid resourceIds format + assert.throws(() => { + index.validators.accessToken({ + body: { + accessToken: 'valid type' + } + }) + }) + + // valid + const expectedAccessToken = 'Bearer 12345234r2' + const instance = index.parsers.accessTokenToCredentials({ + body: { + accessToken: expectedAccessToken + } + }) + + const res = { + headers: {} + } + + instance.signRequest(res, () => {}) + + assert.equal(res.headers['Authorization'], expectedAccessToken) + }) +}) diff --git a/validators.js b/validators.js index 3c2facc..6e14766 100644 --- a/validators.js +++ b/validators.js @@ -1,17 +1,17 @@ const assert = require('assert') module.exports = { - accessToken: (req) => { - assert.ok(req.body.accessToken) - assert.ok(typeof req.body.accessToken === 'string') - assert.ok(req.body.accessToken.startsWith('Bearer ')) - }, - resources: (req) => { - assert.ok(req.body.resources) - req.body.resources.forEach((res) => { - assert.ok(typeof res === 'string') - assert.ok(1 < res.split('/').length) - assert.ok(res.split('/').length < 4) - }) - } -} \ No newline at end of file + accessToken: (req) => { + assert.ok(req.body.accessToken) + assert.ok(typeof req.body.accessToken === 'string') + assert.ok(req.body.accessToken.startsWith('Bearer ')) + }, + resources: (req) => { + assert.ok(req.body.resources) + req.body.resources.forEach((res) => { + assert.ok(typeof res === 'string') + assert.ok(res.split('/').length > 1) + assert.ok(res.split('/').length < 4) + }) + } +}