|
| 1 | +const test = require('ava'); |
| 2 | +const sinon = require('sinon'); |
| 3 | + |
| 4 | +const awsServices = require('@cumulus/aws-client/services'); |
| 5 | +const { randomString } = require('@cumulus/common/test-utils'); |
| 6 | + |
| 7 | +let secretId; |
| 8 | +const secretString = JSON.stringify({ testKey: 'testVal' }); |
| 9 | + |
| 10 | +test.before(() => { |
| 11 | + process.env.INIT_ENV_VARS_FUNCTION_TEST = 'true'; |
| 12 | + secretId = randomString(10); |
| 13 | + process.env.api_config_secret_id = secretId; |
| 14 | +}); |
| 15 | + |
| 16 | +test.after.always(() => { |
| 17 | + delete process.env.INIT_ENV_VARS_FUNCTION_TEST; |
| 18 | +}); |
| 19 | + |
| 20 | +test.serial('secretsManager is not called at module load time and called once for multiple invocations', async (t) => { |
| 21 | + const stub = sinon.stub(awsServices, 'secretsManager'); |
| 22 | + const fakeClient = { |
| 23 | + getSecretValue: sinon.stub().resolves({ |
| 24 | + SecretString: secretString, |
| 25 | + }), |
| 26 | + }; |
| 27 | + stub.returns(fakeClient); |
| 28 | + |
| 29 | + // eslint-disable-next-line global-require |
| 30 | + const { handler } = require('../../app'); |
| 31 | + process.env.dynamoTableNameString = '{}'; |
| 32 | + |
| 33 | + // since secretsManager is now called within the handler and not during module load |
| 34 | + // which is done on line 38 above, there should be no calls to secretsManager until |
| 35 | + // the handler itself is called |
| 36 | + t.is(fakeClient.getSecretValue.callCount, 0); |
| 37 | + await handler({}); |
| 38 | + |
| 39 | + // calling handler again to make sure the initPromise cache is working and |
| 40 | + // secretsManager itself is not called again |
| 41 | + await handler({}); |
| 42 | + await handler({}); |
| 43 | + |
| 44 | + // since the handler ran, there should be one call to secretsManager, even if it |
| 45 | + // ran multiple times, the initPromise cache should prevent multiple calls to secretsManager |
| 46 | + t.is(fakeClient.getSecretValue.callCount, 1); |
| 47 | + t.teardown(() => stub.restore()); |
| 48 | +}); |
| 49 | + |
| 50 | +test.serial('handler retries successfully after a secretsManager failure', async (t) => { |
| 51 | + const stub = sinon.stub(awsServices, 'secretsManager'); |
| 52 | + |
| 53 | + const fakeClient = { |
| 54 | + getSecretValue: sinon.stub().resolves({ |
| 55 | + SecretString: secretString, |
| 56 | + }), |
| 57 | + }; |
| 58 | + stub.returns(fakeClient); |
| 59 | + |
| 60 | + fakeClient.getSecretValue.onFirstCall().rejects(); |
| 61 | + fakeClient.getSecretValue.onSecondCall().resolves({ |
| 62 | + SecretString: secretString, |
| 63 | + }); |
| 64 | + |
| 65 | + // refresh the module/app so the previous test doesn't affect this one |
| 66 | + // and then reimport the handler |
| 67 | + delete require.cache[require.resolve('../../app')]; |
| 68 | + // eslint-disable-next-line global-require |
| 69 | + const { handler } = require('../../app'); |
| 70 | + |
| 71 | + t.is(fakeClient.getSecretValue.callCount, 0); |
| 72 | + await t.throwsAsync(handler({})); |
| 73 | + t.is(fakeClient.getSecretValue.callCount, 1); |
| 74 | + |
| 75 | + await handler({}); |
| 76 | + t.is(fakeClient.getSecretValue.callCount, 2); |
| 77 | + |
| 78 | + await handler({}); |
| 79 | + t.is(fakeClient.getSecretValue.callCount, 2); |
| 80 | + t.teardown(() => stub.restore()); |
| 81 | +}); |
0 commit comments