I'm running some unit test cases for aws-dynamodb using aws-sdk-mock. But I'm getting below error:
UnrecognizedClientException: The security token included in the request is invalid
Here is what my code looks like.
// Code:
async getUser(email) {
const params = {
TableName: 'test',
Key: {
email: email
}
};
return await docClient.get(params).promise();
}
Here is what my current test looks like:
// Testcase:
it('Get all categories data successfully', async done => {
AWSMock.setSDKInstance(AWS);
AWSMock.mock('DynamoDB.DocumentClient', 'get', (params, callback) => {
callback(null, { Item: {
email: 'test@test.com',
name: 'Test profile'
}
});
});
const response = await service.getUserProfile(eventStub.headers.email);
expect(response).to.equal({Items: {
email: 'test@test.com',
name: 'Test profile'
}});;
AWSMock.restore('DynamoDB.DocumentClient');
done();
});
UnrecognizedClientException: The security token included in the request is invalid
at Request.extractError (node_modules/aws-sdk/lib/protocol/json.js:51:27)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:116:18)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:116:18)
at callNextListener (node_modules/aws-sdk/lib/sequential_executor.js:96:12)
at IncomingMessage.onEnd (node_modules/aws-sdk/lib/event_listeners.js:307:13)
I know this is a late answer, but for the sake of anyone else who finds this:
As per aws-sdk-mock readme , see the section with examples on instantiating the AWS service. The AWS service needs to be instantiated inside the function being tested.
From the readme :
NB: The AWS Service needs to be initialised inside the function being tested in order for the SDK method to be mocked eg for an AWS Lambda function example 1 will cause an error region not defined in config whereas in example 2 the sdk will be successfully mocked.
In your example, you would probably need to do this:
async getUser(email) {
// instantiate the service inside the function being tested in order for aws-sdk-mock
// to mock it successfully
if(process.env.NODE_ENV === 'test')
docClient = new AWS.DynamoDB.DocumentClient()
const params = {
TableName: 'test',
Key: {
email: email
}
}
return docClient.get(params).promise()
}
I run afoul of this mistake too. Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.