简体   繁体   English

来自 DynamoDB Documentclient 的模拟承诺:请求中包含的安全令牌无效

[英]Mocking promise from DynamoDB Documentclient: The security token included in the request is invalid

I'm running some unit test cases for aws-dynamodb using aws-sdk-mock.我正在使用 aws-sdk-mock 为 aws-dynamodb 运行一些单元测试用例。 But I'm getting below error:但我得到以下错误:

UnrecognizedClientException: The security token included in the request is invalid UnrecognizedClientException:请求中包含的安全令牌无效

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.根据 aws-sdk-mock自述文件,请参阅实例化 AWS 服务示例部分。 The AWS service needs to be instantiated inside the function being tested. AWS 服务需要在被测试的函数中实例化。

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.注意:AWS 服务需要在被测试的函数中初始化,以便模拟 SDK 方法,例如,对于 AWS Lambda 函数,示例 1 将导致配置中未定义的错误区域,而在示例 2 中,SDK 将被成功模拟.

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.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模拟DynamoDB Documentclient的承诺 - Mocking promise from DynamoDB Documentclient DynamoDB 请求中包含的安全令牌无效 UnrecognizedClientException - DynamoDB The security token included in the request is invalid UnrecognizedClientException 错误:请求中包含的安全令牌无效 - AWS DynamoDB - Error: The security token included in the request is invalid - AWS DynamoDB dynamodb node.js --- UnrecognizedClientException:请求中包含的安全令牌无效 - dynamodb node js --- UnrecognizedClientException: The security token included in the request is invalid DynamoDB-请求中包含的安全令牌已过期 - DynamoDB - The security token included in the request is expired AWS DynamoDB 错误:“请求中包含的安全令牌无效。” 尽管凭据在共享安全文件中 - AWS DynamoDB error: “The security token included in the request is invalid.” although credentials are in shared security file 来自AWS Client的错误“请求中包含的安全令牌无效” - error “security token included in the request is invalid” from AWS Client 无服务器错误:请求中包含的安全令牌无效 - Serverless Error: The security token included in the request is invalid Localstack 抛出请求中包含的安全令牌无效 - Localstack throws The security token included in the request is invalid 嵌套工作流 - 请求中包含的安全令牌无效 - Nested workflow - The security token included in the request is invalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM