Created
February 21, 2017 15:45
-
-
Save horike37/39a21b37dbf0770e467c0f1ca0176bef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const AWS = require('aws-sdk'); | |
const BbPromise = require('bluebird'); | |
class AwsProvider { | |
request(service, method, params) { | |
const awsService = new AWS[service](); | |
const req = awsService[method](params); | |
return new BbPromise((resolve, reject) => { | |
req.send((errParam, data) => { | |
const err = errParam; | |
if (err) { | |
reject(new Error(err.message, err.statusCode)); | |
} else { | |
resolve(data); | |
} | |
}); | |
}); | |
} | |
} | |
module.exports = new AwsProvider(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const aws = require('./awsProvider'); | |
const BbPromise = require('bluebird'); | |
class Index { | |
constructor() { | |
this.provider = aws; | |
} | |
method() { | |
return this.provider.request('STS', | |
'getCallerIdentity', | |
{} | |
).then((result) => { | |
return BbPromise.resolve(result); | |
}); | |
} | |
} | |
module.exports = Index; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const expect = require('chai').expect; | |
const BbPromise = require('bluebird'); | |
const sinon = require('sinon'); | |
const Index = require('./index'); | |
describe('index', () => { | |
let index; | |
beforeEach(() => { | |
index = new Index(); | |
}); | |
describe('method', () => { | |
let stsStub; | |
beforeEach(() => { | |
stsStub = sinon.stub(index.provider, 'request') | |
.returns(BbPromise.resolve({})); | |
}); | |
it('should the correct value', () => { | |
index.aa().then(() => { | |
expect(stsStub.calledOnce).to.be.equal(true); | |
expect(stsStub.calledWithExactly( | |
'STS', | |
'getCallerIdentity', | |
{} | |
)).to.be.equal(true); | |
index.provider.request.restore(); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment