Skip to main content

NodeJS - JWT Sample Application​

View as Markdown

Running App Locally on Linux/WSL πŸ§β€‹

FreeTeamsScaleEnterprise

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

This section runs on Linux or WSL. On macOS, or on Windows without WSL, use the Docker Compose steps instead β€” natively there, Keploy captures PostgreSQL calls only as raw bytes, which usually don't replay.

Don’t have Keploy installed yet?

Before running this sample, make sure Keploy is installed on your system.

πŸ‘‰ Go to Installation Guide

Setup application​

Clone the repository and move to node-jwt folder

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

# Install the dependencies
npm install

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 network create keploy-network
docker compose up -d postgres

Capture the testcases​

sudo -E env PATH=$PATH keploy record -c 'node app.js'
Sample Keploy Test Result Node JWT

Let's Generate the testcases.​

Make API Calls using 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>'

Replace <TOKEN> with the accessToken returned by the sign-in response in step 3. The token expires 35 seconds after sign-in, so send this request within that window (or sign in again first), otherwise the app answers 401 with Unauthorized!.

We will get

User Content.

Running the testcases​

sudo -E env PATH=$PATH keploy test -c 'node 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.

Sample Keploy Test Result Node JWT

But for this application, the Token expiration is 35 seconds. Let's add the Etag and accessToken as the noise in the keploy.yml on line 21 under globalNoise. The file would look like:-

  globalNoise:
global: {}
test-sets:
test-set-0:
body:
accessToken: []
header:
Etag: []
Date: []

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

Sample Keploy Test Result Node JWT

But wait β€” one test is still failing due to the time-freezing. Time freezing is available on the Enterprise plan.

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 πŸ³β€‹

FreeTeamsScaleEnterprise

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

Don’t have Keploy installed yet?

Before running this sample, make sure Keploy is installed on your system.

πŸ‘‰ Go to Installation Guide

Setup application​

Clone the repository and move to node-jwt folder

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

# Install the dependencies
npm install

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​

The docker-compose.yml attaches the services to an external keploy-network, so create it once before recording:

docker network create keploy-network

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

keploy record -c "docker compose up" --container-name "jwtSqlApp"
Sample Keploy Test Result Node JWT

Let's generate the testcases.​

Make API Calls using 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>'

Replace <TOKEN> with the accessToken returned by the sign-in response in step 3. The token expires 35 seconds after sign-in, so send this request within that window (or sign in again first), otherwise the app answers 401 with Unauthorized!.

We will get

User Content.

Running the testcases​

keploy test -c 'docker compose up' --container-name "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 35 seconds. Let's add the Etag and accessToken as the noise in the keploy.yml on line 21 under globalNoise. The file would look like:-

  globalNoise:
global: {}
test-sets:
test-set-0:
body:
accessToken: []
header:
Etag: []
Date: []

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

Sample Keploy Test Result Node JWT

But wait β€” one test is still failing due to the time-freezing. Time freezing is available on the Enterprise plan.

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.πŸ˜ŠπŸš€