Skip to main content
Version: 2.0.0

NodeJS - JWT Sample Application

Introduction

A simple sample CRUD application to test using Keploy build with Node, JWT and Postgres. Buckle up, it's gonna be a fun ride! 🎢

🛠️ Platform-Specific Requirements for Keploy

Below is a table summarizing the tools needed for both native and Docker installations of Keploy on MacOS, Windows, and Linux:

Operating SystemWithout DockerDocker InstallationPrerequisites
MacOS MacOSNot SupportedSupportedDocker Desktop version must be 4.25.2 or above
Windows WindowsSupportedSupported- Use WSL wsl --install
- Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11
Linux LinuxSupportedSupportedLinux kernel 5.15 or higher

On MacOS and Windows, additional tools are required for Keploy due to the lack of native eBPF support.

Quick Installation

Let's get started by setting up the Keploy alias with this command:

 curl --silent -O -L https://keploy.io/install.sh && source install.sh

You should see something like this:

       ▓██▓▄
▓▓▓▓██▓█▓▄
████████▓▒
▀▓▓███▄ ▄▄ ▄ ▌
▄▌▌▓▓████▄ ██ ▓█▀ ▄▌▀▄ ▓▓▌▄ ▓█ ▄▌▓▓▌▄ ▌▌ ▓
▓█████████▌▓▓ ██▓█▄ ▓█▄▓▓ ▐█▌ ██ ▓█ █▌ ██ █▌ █▓
▓▓▓▓▀▀▀▀▓▓▓▓▓▓▌ ██ █▓ ▓▌▄▄ ▐█▓▄▓█▀ █▓█ ▀█▄▄█▀ █▓█
▓▌ ▐█▌ █▌


Keploy CLI

Available Commands:
example Example to record and test via keploy
generate-config generate the keploy configuration file
record record the keploy testcases from the API calls
test run the recorded testcases and execute assertions
update Update Keploy

Flags:
--debug Run in debug mode
-h, --help help for keploy
-v, --version version for keploy

Use "keploy [command] --help" for more information about a command.

🎉 Wohoo! You are all set to use Keploy.

Get Started! 🎬

Setup application

Clone the repository and move to express-mongo folder

git clone https://github.com/keploy/samples-typescript && cd samples-typescript/node-jwt

# Install the dependencies
npm install

Installation 📥

Depending on your OS, choose your adventure: There are 2 ways you can run this sample application.

Running App Locally on Linux/WSL 🐧

We'll be running our sample application right on Linux, but just to make things a tad more thrilling, we'll have the database (postgres) chill on Docker. Ready? Let's get the party started!🎉

If you are using WSL on windows then use below to start wsl in the user's home directory:

wsl ~

Let's start the Postgres Instance

docker-compose up -d

Capture the testcases

sudo -E env PATH=$PATH keploy record -c 'node app.js'

Let's Generate the testcases.

Make API Calls using Hoppscotch, Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.

  1. Create User
curl --location 'http://localhost:8080/api/auth/signup' \
--header 'Content-Type: application/json' \
--data-raw '{
"username":"user",
"email":"user@keploy.io",
"password":"1234"
}'

we will get the output:

{"message": "User was registered successfully!"}

We will get the following output in our terminal

Sample Keploy Test Result Node JWT

Let's go ahead create few more testcases for different endpoints!

  1. Create Admin User
curl --location 'http://localhost:8080/api/auth/signup' \
--header 'Content-Type: application/json' \
--data-raw '{
"username":"admin",
"email":"admin@keploy.io",
"password":"1234",
"role":["admin"]
}'

we will get the output:

{"message": "User was registered successfully!"}
  1. User Signin
curl --location 'http://localhost:8080/api/auth/signin' \
--header 'Content-Type: application/json' \
--data-raw '{
"username":"user",
"email":"user@keploy.io",
"password":"1234"
}'

We will get access token once the user has signed in:

{
"id": 1,
"username": "user",
"email": "user@keploy.io",
"roles": ["ROLE_USER"],
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNzEzNzY0ODY1LCJleHAiOjE3MTM3NjUwNDV9.5LSU1A1jxIbIQFS6Tq26ENNWZBinFt2cJQZ7swpipbc"
}
  1. Access user Content
curl --location 'http://localhost:8080/api/test/all'

We will get:

Public Content
  1. Access user Content
curl --location 'http://localhost:8080/api/test/user' \
--header 'x-access-token: <TOKEN>'

We will get

User Content

Running the testcases

sudo -E env PATH=$PATH keploy test -c 'npm run app.js' --delay 10

Our testcases will fail as the token would expire and new Token will generated again when we are using testmode. To make sure that testcases do not fail, we have use timeFreezing. Our testcases will fail as the token would expire and new Token will generated again when we are using testmode. To make sure that testcases do not fail, we have use timeFreezing.

Sample Keploy Test Result Node JWT

But for this application, the Token expiration is 10 mins so let's go ahead and test the application within 10mins. Let's add the Etag and accessToken as the noise in the test-3.yml on line 45 under header.Date. The file would look like:- But for this application, the Token expiration is 10 mins so let's go ahead and test the application within 10mins. Let's add the Etag and accessToken as the noise in the test-3.yml on line 45 under header.Date. The file would look like:-

        noise:
| - header.Date
| - header.Etag
| - body.accessToken

Now, let's run the keploy in test mode again:-

Sample Keploy Test Result Node JWT

Wrapping it up 🎉

Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible.😊🚀

Happy coding! ✨👩‍💻👨‍💻✨

**********************************___**********************************


Using Docker Compose 🐳

We will be using Docker compose to run the application as well as Postgres on Docker container.

Since we have setup our sample-app using docker, we need to update the postgres host on line 2, in config/db.config.js, from localhost to postgres.

Capture the testcases

We will run the keploy in record mode with docker-compose to start our application:-

keploy record -c "docker-compose up" --containerName "jwtSqlApp"

Let's generate the testcases.

Make API Calls using Hoppscotch, Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.

  1. Create User
curl --location 'http://localhost:8080/api/auth/signup' \
--header 'Content-Type: application/json' \
--data-raw '{
"username":"user",
"email":"user@keploy.io",
"password":"1234"
}'

we will get the output:

{"message": "User was registered successfully!"}

We will get the following output in our terminal

Sample Keploy Test Result Node JWT

Let's go ahead create few more testcases for different endpoints!

  1. Create Admin User
curl --location 'http://localhost:8080/api/auth/signup' \
--header 'Content-Type: application/json' \
--data-raw '{
"username":"admin",
"email":"admin@keploy.io",
"password":"1234",
"role":["admin"]
}'

we will get the output:

{"message": "User was registered successfully!"}
  1. User Signin
curl --location 'http://localhost:8080/api/auth/signin' \
--header 'Content-Type: application/json' \
--data-raw '{
"username":"user",
"email":"user@keploy.io",
"password":"1234"
}'

We will get access token once the user has signed in:

{
"id": 1,
"username": "user",
"email": "user@keploy.io",
"roles": ["ROLE_USER"],
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNzEzNzY0ODY1LCJleHAiOjE3MTM3NjUwNDV9.5LSU1A1jxIbIQFS6Tq26ENNWZBinFt2cJQZ7swpipbc"
}
  1. Access user Content
curl --location 'http://localhost:8080/api/test/all'

We will get:

Public Content
  1. Access user Content
curl --location 'http://localhost:8080/api/test/user' \
--header 'x-access-token: <TOKEN>'

We will get

User Content

Running the testcases

keploy test -c 'sudo docker-compose up'  --containerName "jwtSqlApp" --delay 10

Our testcases will fail as the token would expire and new Token will generated again when we are using testmode. To make sure that testcases do not fail, we have use timeFreezing.

Sample Keploy Test Result Node JWT

But for this application, the Token expiration is 10 mins so let's go ahead and test the application within 10mins. Let's add the Etag and accessToken as the noise in the test-3.yml on line 45 under header.Date. The file would look like:-

        noise:
| - header.Date
| - header.Etag
| - body.accessToken

Now, let's run the keploy in test mode again:-

Sample Keploy Test Result Node JWT

Wrapping it up 🎉

Congrats on the journey so far! You've seen Keploy's power, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible.😊🚀

Happy coding! ✨👩‍💻👨‍💻✨

**********************************___**********************************