question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trouble mocking Secrets Manager Client

See original GitHub issue

Checklist

  • I have read Caveats documentation and didn’t find a solution for this problem there.

Bug description

Mocking secrets manager client doesn’t work as the client first tries to validate a security token which is probably not using the mocked .send command. Example below;

import { mockClient } from 'aws-sdk-client-mock';
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';

const smMock = mockClient(new SecretsManagerClient({}));

smMock.on(GetSecretValueCommand)
    .resolves({ 
        SecretString: JSON.stringify({ my_secret_key: 'my_secret_value' }) 
    });

This code throws:

ExpiredTokenException: The security token included in the request is expired

I’ve tried adding a .callsFake(() => console.log('Hit the mock')) before the .resolves... method and it’s not even getting that far (which supports the theory that the token validation doesn’t use send).

Environment

  • Node version: 16.4.1
  • Typescript version: 4.3.5
  • AWS SDK v3 Client mock version: 0.5.3
  • AWS JS SDK libs and versions:

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
m-radzikowskicommented, Sep 26, 2021

I run the test with both v3.28.0 and v3.34.0 of @aws-sdk/client-secrets-manager and it looks to work fine.

The code you posted has a slight mistake - you either pass the client class to the mockClient() function, or the same instance that you want to mock.

This works good for me:

import {mockClient} from 'aws-sdk-client-mock';
import {GetSecretValueCommand, SecretsManagerClient} from '@aws-sdk/client-secrets-manager';

it('mocks SecretsManagerClient', async () => {
    const smMock = mockClient(SecretsManagerClient);
    smMock.on(GetSecretValueCommand)
        .resolves({
            SecretString: JSON.stringify({my_secret_key: 'my_secret_value'}),
        });

    const sm = new SecretsManagerClient({});
    const response = await sm.send(new GetSecretValueCommand({
        SecretId: 'qq',
    }));

    expect(response.SecretString).toBe('{"my_secret_key":"my_secret_value"}');
});

Alternatively, if you want to mock only a single instance of the Client, you need to provide the same instance in the mock as you use later in code:

import {mockClient} from 'aws-sdk-client-mock';
import {GetSecretValueCommand, SecretsManagerClient} from '@aws-sdk/client-secrets-manager';

it('mocks SecretsManagerClient', async () => {
    const sm = new SecretsManagerClient({});

    const smMock = mockClient(sm);
    smMock.on(GetSecretValueCommand)
        .resolves({
            SecretString: JSON.stringify({my_secret_key: 'my_secret_value'}),
        });

    const response = await sm.send(new GetSecretValueCommand({
        SecretId: 'qq',
    }));

    expect(response.SecretString).toBe('{"my_secret_key":"my_secret_value"}');
});

Please let me know if that solves your problem.

1reaction
m-radzikowskicommented, Sep 16, 2021

Hey, I was vacationing so sorry for the delay, but I will look into this in the next days.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking Secrets Manager module for JavaScript jest unit tests
You create a mock constructor for the SecretsManager in the mock ... AWS.config.update({ region: "us-east-1", }); const client = new AWS.
Read more >
Creating Mocks for an AWS Service - Nishant Kaushish
Mock functions for any AWS service to be used in unit tests ... Secrets Manager is an AWS service which allows us to...
Read more >
Secret Manager client libraries - Documentation - Google Cloud
This page shows how to get started with the Cloud Client Libraries for the Secret Manager API. Read more about the client libraries...
Read more >
secretsmanageriface - Amazon Web Services - Go SDK
Package secretsmanageriface provides an interface to enable mocking the AWS Secrets Manager service client for testing your code.
Read more >
secretsmanageriface - Go Packages
Package secretsmanageriface provides an interface to enable mocking the AWS Secrets Manager service client for testing your code.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found