|
1 | | -# Node-Mongo-API |
| 1 | +# Express-Mongo |
2 | 2 |
|
3 | | -Small project using Node.js for understanding the working of MongoDB through the mongoose library. |
4 | | -Some validations have been added to the schema using the validator library. |
| 3 | +A simple sample CRUD application to test using Keploy build with Express and MongoDB. |
5 | 4 |
|
6 | | -Different API's have been added to perform CRUD operations on a database containing Students records. |
| 5 | +## Setup application |
| 6 | +Clone the repository and move to express-mongo folder |
| 7 | +```bash |
| 8 | +git clone https://github.com/keploy/samples-typescript && cd samples-typescript/express-mongo |
7 | 9 |
|
8 | | -// curl request post to http://localhost:8000/students |
9 | | -```json |
10 | | -{ |
11 | | - "name":"Animesh", |
12 | | - "email":"abc@xyz.com", |
| 10 | +# Install the dependencies |
| 11 | +npm install |
| 12 | +``` |
| 13 | + |
| 14 | +# Using Keploy : |
| 15 | + |
| 16 | +There are two ways to use Keploy:- |
| 17 | + |
| 18 | +1. [Natively on Linux/WSL](#natively-on-ubuntuwsl) |
| 19 | +2. [Using Docker](#running-sample-app-using-docker) |
| 20 | + |
| 21 | +## Natively on Ubuntu/WSL |
| 22 | + |
| 23 | +Keploy can be installed on Linux directly and on Windows with the help of WSL. Based on your system architecture, install the keploy latest binary release from here:- |
| 24 | + |
| 25 | +#### Linux |
| 26 | +1. AMD Architecture |
| 27 | +```zsh |
| 28 | +curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp |
| 29 | + |
| 30 | +sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy |
| 31 | +``` |
| 32 | + |
| 33 | +<details> |
| 34 | +<Summary> 2. ARM Architecture </Summary> |
| 35 | + |
| 36 | + |
| 37 | +```zsh |
| 38 | +curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_arm64.tar.gz" | tar xz -C /tmp |
| 39 | + |
| 40 | +sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy |
| 41 | +``` |
| 42 | +</details> |
| 43 | + |
| 44 | +#### Windows Subsystem for Linux (WSL) |
| 45 | + |
| 46 | +On Windows, WSL is required to run Keploy Binary. You must be running Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11 to use the commands below. |
| 47 | + |
| 48 | +```bash |
| 49 | +wsl --install |
| 50 | +``` |
| 51 | +Once installed download and Install "Keploy Binary" : |
| 52 | + |
| 53 | +```bash |
| 54 | +curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp |
| 55 | + |
| 56 | +sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy |
| 57 | +``` |
| 58 | + |
| 59 | +### Let's start the MongoDB Instance |
| 60 | +```zsh |
| 61 | +docker-compose up -d |
| 62 | +``` |
| 63 | + |
| 64 | +> **Since we have setup our sample-app natively, we need to update the mongoDB host on line 41, in `db/connection.js`, from `mongodb://mongoDb:27017/keploy` to `mongodb://127.0.0.1:27017/keploy`.** |
| 65 | +
|
| 66 | +### Capture the testcases |
| 67 | + |
| 68 | +```bash |
| 69 | +sudo -E env PATH=$PATH Keploy record -c 'npm run src/app.js' |
| 70 | +``` |
| 71 | + |
| 72 | +#### Let's generate the testcases. |
| 73 | +Make API Calls using [Hoppscotch](https://hoppscotch.io), [Postman](https://postman.com) or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks. |
| 74 | + |
| 75 | +```bash |
| 76 | +curl --request POST \ |
| 77 | +--url http://localhost:8000/students \ |
| 78 | + --header 'content-type: application/json' \ |
| 79 | + --data '{ |
| 80 | + "name":"John Do", |
| 81 | + "email":"john@xyiz.com", |
| 82 | + "phone":"0123456799" |
| 83 | + }' |
| 84 | +``` |
| 85 | + |
| 86 | +we will get the output: |
| 87 | + |
| 88 | +``` |
| 89 | +Student registration successful! |
| 90 | +``` |
| 91 | + |
| 92 | +We will get the following output in our terminal |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +## Running the testcases |
| 97 | + |
| 98 | +```bash |
| 99 | +sudo -E env PATH=$PATH keploy test -c 'npm run src/app.js' --delay 10 |
| 100 | +``` |
| 101 | + |
| 102 | +Our testcases will fail as the Keep-Alive connection won't be available when we are using testmode, this happen because in test mode the Keploy uses the `Mocks.yml`, which was generated in the record mode. |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +Let's add the `connection` and `keep-alive` as the noise in the test-1.yml on line 42 under `header.Date`. The file would look like:- |
| 107 | +``` |
| 108 | + noise: |
| 109 | + | - header.Date |
| 110 | + | - header.Keep-Alive |
| 111 | + | - header.Connection |
| 112 | +``` |
| 113 | + |
| 114 | +Now, let's run the keploy in test mode again:- |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +*Voila!! Our testcases has passed 🌟* |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +# Running sample app using docker |
| 123 | + |
| 124 | +Keploy can be used on Linux & Windows through Docker, and on MacOS by the help of [Colima](https://docs.keploy.io/docs/server/macos/installation/#using-colima). |
| 125 | + |
| 126 | +## Create Keploy Alias |
| 127 | +We need create an alias for Keploy: |
| 128 | +```bash |
| 129 | +alias keploy='sudo docker run --pull always --name keploy-v2 -p 16789:16789 --privileged --pid=host -it -v "$(pwd)":/files -v /sys/fs/cgroup:/sys/fs/cgroup -v /sys/kernel/debug:/sys/kernel/debug -v /sys/fs/bpf:/sys/fs/bpf -v /var/run/docker.sock:/var/run/docker.sock --rm ghcr.io/keploy/keploy' |
| 130 | +``` |
| 131 | + |
| 132 | +## Let's start the MongoDB Instance |
| 133 | +```bash |
| 134 | +docker-compose up -d |
| 135 | +``` |
| 136 | + |
| 137 | +## Capture the testcases |
| 138 | +1. We first need to build dockerimage of our application:- |
| 139 | +```bash |
| 140 | +docker build -t node-app:1.0 . |
| 141 | +``` |
| 142 | + |
| 143 | +2. Now we will run the keploy in record mode:- |
| 144 | +```bash |
| 145 | +keploy record -c "docker run -p 8000:8000 --name nodeMongoApp --network keploy-network node-app:1.0" |
| 146 | +``` |
| 147 | + |
| 148 | +#### Let's generate the testcases. |
| 149 | +Make API Calls using [Hoppscotch](https://hoppscotch.io), [Postman](https://postman.com) or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks. |
| 150 | + |
| 151 | +```bash |
| 152 | +curl --request POST \ |
| 153 | +--url http://localhost:8000/students \ |
| 154 | + --header 'content-type: application/json' \ |
| 155 | + --data '{ |
| 156 | + "name":"John Doe", |
| 157 | + "email":"john@xyz.com", |
13 | 158 | "phone":"0123456798" |
14 | | -} |
15 | | -``` |
| 159 | + }' |
| 160 | +``` |
| 161 | + |
| 162 | +we will get the output: |
| 163 | + |
| 164 | +``` |
| 165 | +Student registration successful! |
| 166 | +``` |
| 167 | + |
| 168 | +We will get the following output in our terminal |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | +## Running the testcases |
| 174 | + |
| 175 | +```bash |
| 176 | +keploy test -c "docker run -p 8000:8000 --name nodeMongoApp --network keploy-network node-app:1.0" --delay 10 |
| 177 | +``` |
| 178 | + |
| 179 | +Our testcases will fail as the Keep-Alive connection won't be available when we are using testmode, this happen because in test mode the Keploy uses the `Mocks.yml`, which was generated in the record mode. |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +Let's add the `connection` and `keep-alive` as the noise in the test-1.yml on line 42 under `header.Date`. The file would look like:- |
| 184 | +``` |
| 185 | + noise: |
| 186 | + | - header.Date |
| 187 | + | - header.Keep-Alive |
| 188 | + | - header.Connection |
| 189 | +``` |
| 190 | + |
| 191 | +Now, let's run the keploy in test mode again:- |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +*Voila!! Our testcases has passed 🌟* |
0 commit comments