Skip to content

Commit

Permalink
feat: add unit tests for verbose logging
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-thomas committed Sep 10, 2023
1 parent 10a0230 commit 7565d9e
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const getServerless = (): Serverless => ({
});

describe('index.ts', () => {
beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation();
});

describe('default options are set', () => {
test('secretId and secretPrefix are not set', () => {
const serverless = getServerless();
Expand Down Expand Up @@ -49,7 +53,40 @@ describe('index.ts', () => {
});
});

describe('verbose logging', () => {
afterAll(() => {
nock.cleanAll();
});

test('if verbose is not set, its default value is false', () => {
const plugin = new ServerlesssAwsSecrets(getServerless());
expect(plugin.options.verbose).toBe(false);
});

test('if verbose is set, its value is used', async () => {
const serverless = getServerless();
serverless.service.custom = { 'serverless-aws-secrets': { verbose: true } };
serverless.service.provider.environment = { MYSQL_PASSWORD: 'SECRET:MYSQL_PASSWORD' };

nock(/secretsmanager.eu-west-1.amazonaws.com/)
.post('/')
.reply(200, { SecretString: JSON.stringify({ MYSQL_PASSWORD: 'SECRET_MYSQL_PASSWORD' }) });

const plugin = new ServerlesssAwsSecrets(serverless);
expect(plugin.options.verbose).toBe(true);

await plugin.loadSecrets();
expect(console.log).toBeCalledWith(
'[serverless-aws-secrets]: Replacing MYSQL_PASSWORD with secret of MYSQL_PASSWORD',
);
});
});

describe('loading the secrets', () => {
afterEach(() => {
nock.cleanAll();
});

test('failed to connect to AWS', async () => {
nock(/secretsmanager.eu-west-1.amazonaws.com/)
.post('/')
Expand Down

0 comments on commit 7565d9e

Please sign in to comment.