Add ESLint. Adopted standards (#7)

This commit is contained in:
Gavin Bauman 2018-05-17 17:18:09 -04:00 коммит произвёл GitHub
Родитель 5e790f9183
Коммит 568d675f81
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 145 добавлений и 134 удалений

3
.eslintrc.json Normal file
Просмотреть файл

@ -0,0 +1,3 @@
{
"extends": "standard"
}

Просмотреть файл

@ -1,4 +1,4 @@
module.exports = {
parsers: require('./parsers'),
validators: require('./validators')
}
parsers: require('./parsers'),
validators: require('./validators')
}

Просмотреть файл

@ -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"
}
}

Просмотреть файл

@ -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)
}
}
}
},
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]
}
})
}
}

Просмотреть файл

@ -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)
})
})
})
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)
})
})

Просмотреть файл

@ -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)
})
}
}
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)
})
}
}