Comparing version 0.1.5 to 0.1.6
36
index.js
@@ -33,1 +33,37 @@ const url = require('url') | ||
} | ||
class Client { | ||
constructor (auth, inputApiUrl = defaultApiUrl) { | ||
if (auth === undefined || auth.apiKey === undefined) { | ||
throw new TypeError('Please provide an apiKey auth option.') | ||
} | ||
const apiUrl = url.parse(inputApiUrl) | ||
if (apiUrl.hostname === null) { | ||
throw new TypeError(`${inputApiUrl} is not a valid URL`) | ||
} | ||
this.apiKey = auth.apiKey | ||
this.apiUrl = apiUrl | ||
} | ||
analyze (options) { | ||
return new Promise((resolve, reject) => { | ||
if (options === undefined || options.bytecode === undefined) { | ||
throw new TypeError('Please provide a bytecode option.') | ||
} | ||
requester.do(options.bytecode, this.apiKey, this.apiUrl) | ||
.then(uuid => { | ||
return poller.do(uuid, this.apiKey, this.apiUrl) | ||
}).then(issues => { | ||
resolve(issues) | ||
}).catch(err => { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
} | ||
module.exports.Client = Client | ||
module.exports.defaultApiUrl = url.parse(defaultApiUrl) |
{ | ||
"name": "armlet", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "A Mythril Platform API client.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,7 +17,9 @@ [![CircleCI](https://circleci.com/gh/fgimenez/armlet.svg?style=svg&circle-token=3794de647a820eabf62e091c80d761a722b17b0c)](https://circleci.com/gh/fgimenez/armlet) | ||
```javascript | ||
const analyze = require('armlet') | ||
const { Client } = require('armlet') | ||
... | ||
analyze(myBytecode, myApiKey) | ||
const client = new Client({apiKey: myApiKey}) | ||
client.analyze(myBytecode) | ||
.then(issues => { | ||
@@ -24,0 +26,0 @@ console.log(issues) |
const analyze = require('../index') | ||
const Client = require('../index').Client | ||
const sinon = require('sinon') | ||
@@ -29,22 +30,66 @@ const url = require('url') | ||
it('should be a function', () => { | ||
analyze.should.be.a('function') | ||
}) | ||
describe('default', () => { | ||
it('should be a function', () => { | ||
analyze.should.be.a('function') | ||
}) | ||
it('should return a thenable', () => { | ||
const result = analyze(bytecode, apiKey) | ||
it('should return a thenable', () => { | ||
const result = analyze(bytecode, apiKey) | ||
result.then.should.be.a('function') | ||
}) | ||
result.then.should.be.a('function') | ||
}) | ||
it('should require a bytecode param', async () => { | ||
await analyze(undefined, apiKey).should.be.rejectedWith(TypeError) | ||
}) | ||
it('should require a bytecode param', async () => { | ||
await analyze(undefined, apiKey).should.be.rejectedWith(TypeError) | ||
}) | ||
it('should require an apiKey param', async () => { | ||
await analyze(bytecode).should.be.rejectedWith(TypeError) | ||
it('should require an apiKey param', async () => { | ||
await analyze(bytecode).should.be.rejectedWith(TypeError) | ||
}) | ||
it('should require a valid api URL if given', async () => { | ||
await analyze(bytecode, apiKey, 'not-a-real-url').should.be.rejectedWith(TypeError) | ||
}) | ||
}) | ||
it('should require a valid api URL if given', async () => { | ||
await analyze(bytecode, apiKey, 'not-a-real-url').should.be.rejectedWith(TypeError) | ||
describe('Client', () => { | ||
it('should be a function', () => { | ||
analyze.should.be.a('function') | ||
}) | ||
describe('should have a constructor which should', () => { | ||
it('require an apiKey auth option', () => { | ||
(() => new Client()).should.throw(TypeError) | ||
}) | ||
it('require a valid apiUrl if given', () => { | ||
(() => new Client({apiKey: apiKey}, 'not-a-valid-url')).should.throw(TypeError) | ||
}) | ||
it('initialize apiUrl to a default value if not given', () => { | ||
const instance = new Client({apiKey: apiKey}) | ||
instance.apiUrl.should.be.deep.equal(analyze.defaultApiUrl) | ||
}) | ||
}) | ||
describe('instances should', () => { | ||
beforeEach(() => { | ||
this.instance = new Client({apiKey: apiKey}) | ||
}) | ||
it('be created with a constructor', () => { | ||
this.instance.constructor.name.should.be.equal('Client') | ||
}) | ||
describe('have an analyze method which', () => { | ||
it('should be a function', () => { | ||
this.instance.analyze.should.be.a('function') | ||
}) | ||
it('should require a bytecode option', async () => { | ||
await this.instance.analyze().should.be.rejectedWith(TypeError) | ||
}) | ||
}) | ||
}) | ||
}) | ||
@@ -59,47 +104,102 @@ }) | ||
it('should chain requester and poller', async () => { | ||
sinon.stub(requester, 'do') | ||
.withArgs(bytecode, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
resolve(uuid) | ||
})) | ||
sinon.stub(poller, 'do') | ||
.withArgs(uuid, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
resolve(issues) | ||
})) | ||
describe('default', () => { | ||
it('should chain requester and poller', async () => { | ||
sinon.stub(requester, 'do') | ||
.withArgs(bytecode, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
resolve(uuid) | ||
})) | ||
sinon.stub(poller, 'do') | ||
.withArgs(uuid, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
resolve(issues) | ||
})) | ||
await analyze(bytecode, apiKey, apiUrl).should.eventually.equal(issues) | ||
}) | ||
await analyze(bytecode, apiKey, apiUrl).should.eventually.equal(issues) | ||
}) | ||
it('should reject with requester failures', async () => { | ||
const errorMsg = 'Booom! from requester' | ||
sinon.stub(requester, 'do') | ||
.withArgs(bytecode, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
reject(new Error(errorMsg)) | ||
})) | ||
sinon.stub(poller, 'do') | ||
.withArgs(uuid, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
resolve(issues) | ||
})) | ||
it('should reject with requester failures', async () => { | ||
const errorMsg = 'Booom! from requester' | ||
sinon.stub(requester, 'do') | ||
.withArgs(bytecode, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
reject(new Error(errorMsg)) | ||
})) | ||
sinon.stub(poller, 'do') | ||
.withArgs(uuid, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
resolve(issues) | ||
})) | ||
await analyze(bytecode, apiKey, apiUrl).should.be.rejectedWith(Error, errorMsg) | ||
await analyze(bytecode, apiKey, apiUrl).should.be.rejectedWith(Error, errorMsg) | ||
}) | ||
it('should reject with poller failures', async () => { | ||
const errorMsg = 'Booom! from poller' | ||
sinon.stub(requester, 'do') | ||
.withArgs(bytecode, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
resolve(uuid) | ||
})) | ||
sinon.stub(poller, 'do') | ||
.withArgs(uuid, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
reject(new Error(errorMsg)) | ||
})) | ||
await analyze(bytecode, apiKey, apiUrl).should.be.rejectedWith(Error, errorMsg) | ||
}) | ||
}) | ||
it('should reject with poller failures', async () => { | ||
const errorMsg = 'Booom! from poller' | ||
sinon.stub(requester, 'do') | ||
.withArgs(bytecode, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
resolve(uuid) | ||
})) | ||
sinon.stub(poller, 'do') | ||
.withArgs(uuid, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
reject(new Error(errorMsg)) | ||
})) | ||
describe('Client', () => { | ||
beforeEach(() => { | ||
this.instance = new Client({apiKey: apiKey}, apiUrl) | ||
}) | ||
await analyze(bytecode, apiKey, apiUrl).should.be.rejectedWith(Error, errorMsg) | ||
it('should chain requester and poller', async () => { | ||
sinon.stub(requester, 'do') | ||
.withArgs(bytecode, apiKey, parsedApiUrl) | ||
.returns(new Promise(resolve => { | ||
resolve(uuid) | ||
})) | ||
sinon.stub(poller, 'do') | ||
.withArgs(uuid, apiKey, parsedApiUrl) | ||
.returns(new Promise(resolve => { | ||
resolve(issues) | ||
})) | ||
await this.instance.analyze({bytecode: bytecode}).should.eventually.equal(issues) | ||
}) | ||
it('should reject with requester failures', async () => { | ||
const errorMsg = 'Booom! from requester' | ||
sinon.stub(requester, 'do') | ||
.withArgs(bytecode, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
reject(new Error(errorMsg)) | ||
})) | ||
sinon.stub(poller, 'do') | ||
.withArgs(uuid, apiKey, parsedApiUrl) | ||
.returns(new Promise(resolve => { | ||
resolve(issues) | ||
})) | ||
await this.instance.analyze({bytecode: bytecode}).should.be.rejectedWith(Error, errorMsg) | ||
}) | ||
it('should reject with poller failures', async () => { | ||
const errorMsg = 'Booom! from poller' | ||
sinon.stub(requester, 'do') | ||
.withArgs(bytecode, apiKey, parsedApiUrl) | ||
.returns(new Promise(resolve => { | ||
resolve(uuid) | ||
})) | ||
sinon.stub(poller, 'do') | ||
.withArgs(uuid, apiKey, parsedApiUrl) | ||
.returns(new Promise((resolve, reject) => { | ||
reject(new Error(errorMsg)) | ||
})) | ||
await this.instance.analyze({bytecode: bytecode}).should.be.rejectedWith(Error, errorMsg) | ||
}) | ||
}) | ||
@@ -106,0 +206,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23543
601
31