From 22547ed21097821415c39f62d4d61bfa61030eb8 Mon Sep 17 00:00:00 2001 From: Hirusha Prabhanath Date: Fri, 15 Mar 2024 10:54:21 +0530 Subject: [PATCH 1/2] Add intergration test for register endpoint --- package-lock.json | 174 ++++++++++++++++++- package.json | 7 +- src/__tests__/intergration/userRouts.test.js | 39 +++++ src/app.js | 30 +--- src/index.js | 32 ++++ src/router/route.js | 4 + src/utils/helpers.js | 3 +- 7 files changed, 254 insertions(+), 35 deletions(-) create mode 100644 src/__tests__/intergration/userRouts.test.js create mode 100644 src/index.js diff --git a/package-lock.json b/package-lock.json index 3acaac0..f7c7220 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,8 @@ }, "devDependencies": { "jest": "^29.7.0", - "nodemon": "^3.1.0" + "nodemon": "^3.1.0", + "supertest": "^6.3.4" } }, "node_modules/@ampproject/remapping": { @@ -1400,11 +1401,23 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, + "node_modules/asap": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", + "integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==", + "dev": true + }, "node_modules/async": { "version": "3.2.5", "resolved": "https://registry.npmjs.org/async/-/async-3.2.5.tgz", "integrity": "sha512-baNZyqaaLhyLVKm/DlvdW051MSgO6b8eVfIezl9E5PqWxFgzLm/wQntEW4zOytVburDEr0JlALEpdOFwvErLsg==" }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "dev": true + }, "node_modules/babel-jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.7.0.tgz", @@ -1903,6 +1916,18 @@ "color-support": "bin.js" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dev": true, + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", @@ -1911,6 +1936,15 @@ "node": ">= 6" } }, + "node_modules/component-emitter": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.1.tgz", + "integrity": "sha512-T0+barUSQRTUQASh8bx02dl+DhF54GtIDY13Y3m9oWTklKbb3Wv974meRpeZ3lp1JpLVECWWNHC4vaG2XHXouQ==", + "dev": true, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -1971,6 +2005,12 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "node_modules/cookiejar": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.4.tgz", + "integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==", + "dev": true + }, "node_modules/cors": { "version": "2.8.5", "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", @@ -2091,6 +2131,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "dev": true, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/delegates": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", @@ -2130,6 +2179,16 @@ "node": ">=8" } }, + "node_modules/dezalgo": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz", + "integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==", + "dev": true, + "dependencies": { + "asap": "^2.0.0", + "wrappy": "1" + } + }, "node_modules/diff-sequences": { "version": "29.6.3", "resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-29.6.3.tgz", @@ -2467,6 +2526,12 @@ "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==", "dev": true }, + "node_modules/fast-safe-stringify": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", + "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==", + "dev": true + }, "node_modules/fb-watchman": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/fb-watchman/-/fb-watchman-2.0.2.tgz", @@ -2545,6 +2610,35 @@ "node": ">=8" } }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dev": true, + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/formidable": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-2.1.2.tgz", + "integrity": "sha512-CM3GuJ57US06mlpQ47YcunuUZ9jpm8Vx+P2CGt2j7HpgkKZO/DJYQ0Bobim8G6PFQmK5lOqOOdUXboU+h73A4g==", + "dev": true, + "dependencies": { + "dezalgo": "^1.0.4", + "hexoid": "^1.0.0", + "once": "^1.4.0", + "qs": "^6.11.0" + }, + "funding": { + "url": "https://ko-fi.com/tunnckoCore/commissions" + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -2816,6 +2910,15 @@ "node": ">=16.0.0" } }, + "node_modules/hexoid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/hexoid/-/hexoid-1.0.0.tgz", + "integrity": "sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/html-escaper": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz", @@ -5244,6 +5347,75 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/superagent": { + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/superagent/-/superagent-8.1.2.tgz", + "integrity": "sha512-6WTxW1EB6yCxV5VFOIPQruWGHqc3yI7hEmZK6h+pyk69Lk/Ut7rLUY6W/ONF2MjBuGjvmMiIpsrVJ2vjrHlslA==", + "dev": true, + "dependencies": { + "component-emitter": "^1.3.0", + "cookiejar": "^2.1.4", + "debug": "^4.3.4", + "fast-safe-stringify": "^2.1.1", + "form-data": "^4.0.0", + "formidable": "^2.1.2", + "methods": "^1.1.2", + "mime": "2.6.0", + "qs": "^6.11.0", + "semver": "^7.3.8" + }, + "engines": { + "node": ">=6.4.0 <13 || >=14" + } + }, + "node_modules/superagent/node_modules/debug": { + "version": "4.3.4", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", + "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", + "dev": true, + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/superagent/node_modules/mime": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.6.0.tgz", + "integrity": "sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==", + "dev": true, + "bin": { + "mime": "cli.js" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/superagent/node_modules/ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true + }, + "node_modules/supertest": { + "version": "6.3.4", + "resolved": "https://registry.npmjs.org/supertest/-/supertest-6.3.4.tgz", + "integrity": "sha512-erY3HFDG0dPnhw4U+udPfrzXa4xhSG+n4rxfRuZWCUvjFWwKl+OxWf/7zk50s84/fAAs7vf5QAb9uRa0cCykxw==", + "dev": true, + "dependencies": { + "methods": "^1.1.2", + "superagent": "^8.1.2" + }, + "engines": { + "node": ">=6.4.0" + } + }, "node_modules/supports-color": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", diff --git a/package.json b/package.json index afc5f19..4501167 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "main": "app.js", "scripts": { "test": "jest", - "start": "node src/app.js", - "dev": "nodemon src/app.js" + "start": "node src/index.js", + "dev": "nodemon src/index.js" }, "keywords": [], "author": "", @@ -29,6 +29,7 @@ }, "devDependencies": { "jest": "^29.7.0", - "nodemon": "^3.1.0" + "nodemon": "^3.1.0", + "supertest": "^6.3.4" } } diff --git a/src/__tests__/intergration/userRouts.test.js b/src/__tests__/intergration/userRouts.test.js new file mode 100644 index 0000000..c115e59 --- /dev/null +++ b/src/__tests__/intergration/userRouts.test.js @@ -0,0 +1,39 @@ +const request = require("supertest"); +const app = require("../../app"); +const mongoose = require("mongoose"); +const UserModel = require("../../models/userModel"); + +// describe("POST /register", () => { +// it("should get test message", async () => { +// const { body } = await request(app).get("/test"); + +// expect(body).toEqual([ +// expect.objectContaining({ +// message: "its working", +// }), +// ]); +// }); +// }); + +const testUser = { + username: "testUser", + email: "test@example.com", + password: "Hello@123455", +}; + +beforeAll(async () => { + await mongoose.connect(process.env.MONGODB_URL_TEST); +}); + +afterAll(async () => { + await UserModel.deleteOne({ username: "testUser" }); + await mongoose.connection.close(); +}); + +describe("POST /register", () => { + test("It should create new user", async () => { + const response = await request(app).post("/api/v1/register").send(testUser); + expect(response.statusCode).toBe(201); + // expect(response.body).toEqual({ helo: "hello" }); + }); +}); diff --git a/src/app.js b/src/app.js index 2cd1460..c66ddf3 100644 --- a/src/app.js +++ b/src/app.js @@ -1,8 +1,6 @@ const express = require("express"); const cors = require("cors"); const morgan = require("morgan"); -const dbConnect = require("./db/conn"); -const mongoose = require("mongoose"); const router = require("./router/route"); const cookieParser = require("cookie-parser"); const { default: rateLimit } = require("express-rate-limit"); @@ -56,30 +54,4 @@ app.use((error, req, res, next) => { }); }); -// Start Server if datbase connected -async function initializeApp() { - try { - await dbConnect(); - app.listen(PORT, () => { - console.log("Server Started"); - }); - } catch (err) { - console.log(err); - } -} - -initializeApp(); - -// Handle MongoDB connection close on application termination -process.on("SIGINT", async () => { - console.log("Received SIGINT. Closing MongoDB connection..."); - - try { - await mongoose.connection.close(); - console.log("MongoDB connection closed due to application termination"); - process.exit(0); - } catch (error) { - console.error("Error closing MongoDB connection:", error); - process.exit(1); - } -}); +module.exports = app; diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..8abcc09 --- /dev/null +++ b/src/index.js @@ -0,0 +1,32 @@ +const mongoose = require("mongoose"); +const app = require("./app"); +const dbConnect = require("./db/conn"); + +// Start Server if datbase connected +async function initializeApp() { + try { + await dbConnect(); + app.listen(PORT, () => { + console.log("Server Started"); + }); + } catch (err) { + console.log(err); + } +} + +// start mongodb and express +initializeApp(); + +// Handle MongoDB connection close on application termination +process.on("SIGINT", async () => { + console.log("Received SIGINT. Closing MongoDB connection..."); + + try { + await mongoose.connection.close(); + console.log("MongoDB connection closed due to application termination"); + process.exit(0); + } catch (error) { + console.error("Error closing MongoDB connection:", error); + process.exit(1); + } +}); diff --git a/src/router/route.js b/src/router/route.js index 5c7b848..0524d45 100644 --- a/src/router/route.js +++ b/src/router/route.js @@ -24,6 +24,10 @@ router.post("/verifyotp", verifyOtp); router.get("/user/:username", authenticate, getUser); router.get("/generateotp", localVariable, generateOtp); router.get("/refresh", refreshToken); +// Testin +router.get("/test", (req, res) => { + res.send({ message: "its working" }); +}); // PUT router.put("/updateuser", authenticate, updateUser); diff --git a/src/utils/helpers.js b/src/utils/helpers.js index ad68255..6579ef8 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -24,9 +24,8 @@ function convertToMilliseconds(timeComponent) { }; const value = parseInt(timeComponent); - console.log(value); + const type = timeComponent.charAt(timeComponent.length - 1); - console.log(type); if (!isNaN(value) && conversions[type]) { return value * conversions[type]; From 3c00afce313ee9e09df59a98a05a1f7403a36156 Mon Sep 17 00:00:00 2001 From: Hirusha Prabhanath Date: Fri, 15 Mar 2024 10:58:07 +0530 Subject: [PATCH 2/2] Add intergration test for register endpoint --- .github/workflows/node.js.yml | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 1d78835..2ae2449 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -5,13 +5,12 @@ name: Node.js CI on: push: - branches: [ "main" ] + branches: ["main"] pull_request: - branches: [ "main" ] + branches: ["main"] jobs: build: - runs-on: ubuntu-latest strategy: @@ -20,12 +19,14 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v3 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} - cache: 'npm' - - run: npm ci - - run: npm run build --if-present - - run: npm test + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + cache: "npm" + - run: npm ci + - run: npm run build --if-present + - run: npm test + env: + MONGODB_URL_TEST: ${{ secrets.MONGODB_URL_TEST }}