Last active
September 22, 2020 09:43
-
-
Save uijsmilga/376e9cfc3dde52f96b6d4768113afd71 to your computer and use it in GitHub Desktop.
stub aws service using sinon
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
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