Skip to content

Instantly share code, notes, and snippets.

@uijsmilga
Last active September 22, 2020 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uijsmilga/376e9cfc3dde52f96b6d4768113afd71 to your computer and use it in GitHub Desktop.
Save uijsmilga/376e9cfc3dde52f96b6d4768113afd71 to your computer and use it in GitHub Desktop.
stub aws service using sinon
const aws = require('aws-sdk');
const sinon = require('sinon');
###############################
sinon.stub(aws, 'SecretsManager').returns({
getSecretValue: () => {
return {promise: () => Promise.resolve("someValue") }
}
});
###############################
/*
only getRandomPassword is stubbed
*/
let sm = new aws.SecretsManager({region: "us-west-2"})
this.awsStub = sinon.stub(aws, 'SecretsManager').returns(
(() => {
sinon.stub(sm, 'getRandomPassword').withArgs("something").returns(3)
return sm
}).call()
);
###############################
const sinon = require('sinon')
const aws = require('aws-sdk')
const { secretNames } = require('../../../lib/utils/secret-names')
const stub = sinon.stub(aws, "SecretsManager")
stub.callsFake((options) => {
const sm = new stub.wrappedMethod(options)
const stubb = sinon.stub(sm, "getSecretValue")
stubb.callsFake((request) => {
if (request.SecretId === secretNames["placeholder"]) {
return {
promise: async () => {
return {
SecretString: {
reader: {
base: {
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE,
host: process.env.PGHOST,
},
hosts: [
],
},
writer: {
base: {
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE,
host: process.env.PGHOST,
},
hosts: [
],
},
}
}
}
}
}
return stubb.wrappedMethod.call(sm, request)
})
return sm
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment