'Unit testing how to send path parameter

I am new to unit testing. I am using SAM framework, doing unit test for Lambda update record function.

I have the following project record in my Dynamodb table :

{
 "PK": "project_XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
 "GSI_PK": "[email protected]",
 "description": "testDesc",
 "name": "testName"
}

I will pass this as mock data for the output of get record. After that I will call the handler function. My question is how can I pass the user id in the handler function in request context ?

My unit test beforeAll(() => {

getSpy=jest.spyOn(AWS.DynamoDB.DocumentClient.prototype, 'get')
updateSpy = jest.spyOn(AWS.DynamoDB.DocumentClient.prototype, 'update');



getSpy.mockReturnValue({
  promise: () => Promise.resolve({
    Item: {
      'PK': 'project_XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
     
      'userId': '[email protected]',
      
      'role': 1,
      '__apiVersion': 1.4,
      'updatedAt': '2022-04-04T14:20:16.318Z',
      'createdAt': '2021-07-14T04:46:51.808Z',
      'description': 'testDesc',
      'name': 'testName'
      
    },
 
  })
});
updateSpy.mockReturnValue({
      promise: () => Promise.resolve({
        
      })


 });
  });
    it('update tab record', async () => {
const res = await handler({body:requestbody , pathParameters: validatePath});
})

My question is exactly here, how to pass the user id in the handler parameter for request context

The handler function is like the following:

    exports.handler = async (event) => {
      const { body, pathParameters, requestContext } = event;
    const requestContextUserId = requestContext?.authorizer?.claims?.['cognito:username'];
.....
const tabRecord = await DB.get(fProjectId, constants.META);
 if (projectRecord?.userId !== requestContextUserId){
return response_403{}
    ........
    } 
...}

May be this is related to https://github.com/AnomalyInnovations/serverless-stack-com/issues/115#issue-244805059

How to pass user id in handler parameter in unit test , as i am using requestContext in the lambda function?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source