simulate clock-skew scenario - node.js

I met this clock-skew correction error(https://aws.amazon.com/blogs/developer/clock-skew-correction/) I'm getting from S3 getobject call from Lambda, and I have made a fix to the issue(this is in Nodejs)and right now is writing the unit test for the function.
Basically I'm trying to simulate the scenario where the client has a time difference of 15+ mins with S3, I have search for few S3 mock up library, but have no clue how to do so.
I'm using mocha as the test framework

Related

How to convert csv to html table in AWS Lamda using nodejs

In AWS, working on AWS Wisdom which accepts only html and text file referring this aws.amazon.com/blogs/contact-center/ingesting-content-to-power-real-time-recommendations-and-search-with-amazon-connect-wisdom/
but i have data source as csv
how to convert the csv to html in lambda code and so that it passes to knowledge base of wisdom
Write Node JS logic the same way as you would in any Node JS app. Then use the AWS Lambda run time to build the Lambda function by using the same business logic.
There is nothing special using an AWS Lambda function when you want to perform this type of task. Its the same logic. And to make it even better, there are many examples on the NET that shows you how to convert csv to html.
If you do not know how to use NODE JS to write an AWS Lambda funciton, check the following example as a reference:
https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/cross-services/lambda-for-browser

How to write a test to validate JSON file in Node.js and Express

I have a Node.js application and it already has Unit Tests and is using the Mocha framework for the same. It is checking the functions individually. These tests are integrated into the CI/CD pipeline in Bamboo and so if there is an error, it will stop the build job and alert the user who has pushed the change.
Now I have a requirement that I need to validate a JSON file, which is available on one of the S3 buckets. It downloads the file once the Node.js application is started in the local environment. I have unit tests to check whether the downloading functionality is working or not and it is working fine. Now for the validation purpose, I am a little confused about whether I need to add it as a unit test or an integration test. I am new to QA and I would like to do it in the right way. As of now, there are no integration tests are in place(No tests are checking the API endpoints). It will be helpful if someone can point me in the right direction. Also, it will be helpful if someone can suggest the framework I need to use with Node.js for writing integration tests.
I have the following code that is used for testing the download functionality.
it(`Download file from S3`, (done) => {
s3Service.getJSONFile('','',Date.now()).then(data => {
setTimeout(() => {
assert.equal(data, "JSON File Download Success");
done();
}, 1000);
}).catch(function(error){
console.log("Error in getJSONFileFromS3: "+ JSON.stringify(error))
})
});
I have a function validateJSON for validating the JSON file and its contents. Not sure whether I need to call this function from a Unit Test so that it will return true or false. But I think in the case of Unit Test it will check whether the validation function is working or not and not the validity of the file. What I need is for my tests should succeed if the JSON file is valid and fail if it is not so that the build will be stopped. By the way, I don't have an API endpoint for the JSON validation
It will be helpful if someone can show me an example of how these types of scenarios should be addressed in testing.

How split a apigw serverless.yml events node?

I try to split a part of my serverless.yml template, events node, into multiple sub-files. I have a fairly large lamba api gateway. The “events” node is growing very long, its becoming difficult to manage.
I CANT split a function or service, its important that reamin only one api service.
Its possible to divide only “events” node of serverless template into sub several files, for example for each event of my api service (sections, roles, etc), such as:
sections-events.sls.yml, roles-events.sls.yml, and so on?
Ive tried more solutions but none worked..
This is my original serverless.yml template.
This is one of the tests I've done using template variables but it doesn't work ..
Looking at the sls documentation, I thought it was possible to structure the template as follows:
functions:
graphql:
handler: src/index.handler
...
events:
${file(./sections-events.serverless.yml):events}
${file(./roles-events.serverless.yml):events}
but the template validator returns an error as the events node must be an array and this syntax is not possible.
TEST 1:
TEST 2:
I hope someone can help me find a possible path, even though im convinced that isn't feasible!
Thanks a lot to the whole community!
Good Job.

Can I associate console logs with specific test cases in Jest?

I've looked at the work done with jest-circus and the new Reporter handler: onTestCaseResult but it doesn't give me what I need. I want to capture the console logging for each test case to allow for better analysis of errors when running large test suites against other people's API implementations. At present the console logs are only available on the TestResult object in the onTestResult handler but I would need it in the TestCaseResult object.
Thanks

How can I know whether it is running inside lambda?

I am deploying nodejs code to AWS lambda and I'd like to know how I can check whether it is running in lambda. Because I need to do something different in code between lambda and local.
AWS Lambda sets various runtime environment variables that you can leverage. You can use the following in Node.js, for example:
const isLambda = !!process.env.LAMBDA_TASK_ROOT;
console.log("Running on Lambda:", isLambda);
Note that the double bang !! converts a truthy/falsey object to a boolean (true/false).
I'd advise using a Lambda environment variable rather than attempting to check against any runtimes of the Lambda executing.
By doing this you can ensure that any infrastructure changes on the AWS side of Lambda will not affect your code.
It also allows you test it locally if you are trying to reproduce a scenario without the need to hardcode logic.

Resources