我尝试使用模拟KMS创建测试。看到了一些示例代码,但是在尝试时无法正常工作。
示例代码来自https://www.libhive.com/providers/npm/packages/aws-sdk-mock
然后..这就是我尝试的方式
import * as awsMock from "aws-sdk-mock";
import * as AWS from "aws-sdk";
describe('#decrypt', () => {
it('should call decrypt with key and context', async () => {
const key = <key here>;
const context = { key: 'value' };
awsMock.mock('KMS', 'decrypt', (params, cb) => {
expect(params.CiphertextBlob).to.exist;
params.CiphertextBlob.should.equal(key);
expect(params.EncryptionContext).to.exist;
params.EncryptionContext.should.equal(context);
cb();
});
const kms = new AWS.KMS({ region: "eu-west-2" });
const cipherText = { CiphertextBlob: new Buffer(key, "base64") };
const data = await kms.decrypt(cipherText).promise();
const decryptedKey = data.Plaintext.toString();
return decryptedKey;
});
});
我能够检索到解密的密钥,但是它不是来自我创建的模拟。我确认了,但是将console.log放在模拟回调中。
希望你们能帮助我