From df3cc59f4825d044fb8c31e440361dee2b186247 Mon Sep 17 00:00:00 2001 From: Suyog Jadhav Date: Tue, 13 Aug 2024 17:17:29 +0530 Subject: [PATCH] update testing mock --- junit.xml | 114 +++++++++++++++++++-------------------- key/private.key | 27 ---------- key/public.key | 9 ---- package-lock.json | 42 +++++++++++++++ package.json | 1 + test/auth.test.js | 8 +-- test/middlewares.test.js | 32 +++++------ test/role.test.js | 3 +- test/user.test.js | 3 +- 9 files changed, 121 insertions(+), 118 deletions(-) delete mode 100644 key/private.key delete mode 100644 key/public.key diff --git a/junit.xml b/junit.xml index 67ede1b..d92ca3f 100644 --- a/junit.xml +++ b/junit.xml @@ -1,89 +1,89 @@ - - - + + + - + - + - + - + - + - + - + - + - + - + - + + + - + - + - + - - - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + @@ -91,23 +91,29 @@ - + - + - + - + + + + + - + + + - + - - + + @@ -117,15 +123,9 @@ - - - + - - - - - + \ No newline at end of file diff --git a/key/private.key b/key/private.key deleted file mode 100644 index ed44c44..0000000 --- a/key/private.key +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEpQIBAAKCAQEAqYxGJHD5IWW3QQaIE6t9uMibo73pz60daaTGHJ+sZI5+uQWQ -GWwxrTf7sCgxOOMcQ4gIrc3RzDcEUVTBEEWCbFf/GGmCtYhtragZN2kxIrugrOhg -Kohc3GAKWwFhOdLIWX6/T1w4MX3CHhUGD+y3JaXXhu5Lum+G7F/FQoLDWHFsS5pO -wFJ1Cm9ejnMbFLSYZBW3JxoryMCWezyL4jzhnQC7se9dF63GXOFTQO31qC/V3eHM -ShthoIQNH2zNqLI4+CyXDr4AJ9iOdlbHbLDwFbnF5kpDfDGCyMhNaX0QaLvA6wsh -fpwzoMQ8EiKwcmih/9t/77KcqD6qoVQUv5Yu8wIDAQABAoIBAQCiEEdrxeEdH1EP -guLHioDeBENhPMMlk+AtfycvbSEn7V0MFlkrM9Q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WRB7ZNAj2kCLJdRr77H3z7kVKoWzEcOKXIKMrlVAXbxj3iu91+WA5DlHKrRKbmRV -m2f4ooujqVLqyfINuMUaW4HHNAxUKBiE+bHLDP7gdiRX/7ALTc01ZvY= ------END RSA PRIVATE KEY----- diff --git a/key/public.key b/key/public.key deleted file mode 100644 index 609d2ae..0000000 --- a/key/public.key +++ /dev/null @@ -1,9 +0,0 @@ ------BEGIN PUBLIC KEY----- -MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqYxGJHD5IWW3QQaIE6t9 -uMibo73pz60daaTGHJ+sZI5+uQWQGWwxrTf7sCgxOOMcQ4gIrc3RzDcEUVTBEEWC -bFf/GGmCtYhtragZN2kxIrugrOhgKohc3GAKWwFhOdLIWX6/T1w4MX3CHhUGD+y3 -JaXXhu5Lum+G7F/FQoLDWHFsS5pOwFJ1Cm9ejnMbFLSYZBW3JxoryMCWezyL4jzh -nQC7se9dF63GXOFTQO31qC/V3eHMShthoIQNH2zNqLI4+CyXDr4AJ9iOdlbHbLDw -FbnF5kpDfDGCyMhNaX0QaLvA6wshfpwzoMQ8EiKwcmih/9t/77KcqD6qoVQUv5Yu -8wIDAQAB ------END PUBLIC KEY----- diff --git a/package-lock.json b/package-lock.json index 78ecc79..42fb32e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,6 +28,7 @@ "globals": "^15.9.0", "jest": "^29.7.0", "jest-junit": "^16.0.0", + "mongoose-mock": "^0.4.0", "node-mocks-http": "^1.15.1", "nodemailer-mock": "^2.0.6", "supertest": "^7.0.0", @@ -3744,6 +3745,16 @@ "node": ">= 6" } }, + "node_modules/formatio": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/formatio/-/formatio-1.0.2.tgz", + "integrity": "sha512-db27e1R5chXs7pZcJAh7aPC5iJRKRuCJEUF0cFWBdfG/Q0ZMehLg+dbibkXU5uyNM5AjmwBma8PkloxR+l2I1w==", + "deprecated": "This package is unmaintained. Use @sinonjs/formatio instead", + "dev": true, + "dependencies": { + "samsam": "~1.1" + } + }, "node_modules/formidable": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.1.tgz", @@ -5695,6 +5706,16 @@ "url": "https://opencollective.com/mongoose" } }, + "node_modules/mongoose-mock": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/mongoose-mock/-/mongoose-mock-0.4.0.tgz", + "integrity": "sha512-PHYL3HA+hl7rEysnTwiBVna3ZjPhE98dNDPsclkJ9BbGBLP5PBW8fIN5eAR1+mQM2bbubIadKrzcFZbs4/dOtQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "sinon": "~1.8.1" + } + }, "node_modules/mongoose/node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -6537,6 +6558,14 @@ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", "license": "MIT" }, + "node_modules/samsam": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/samsam/-/samsam-1.1.3.tgz", + "integrity": "sha512-t9rCPskf50hZ53eH8Z+cSWD4LfJBac+8vSSuzi1Y2HzygyXxtAl0BaR3hr6iI6A+nFQbkmJNC/brQLNEeVnrmg==", + "deprecated": "This package has been deprecated in favour of @sinonjs/samsam", + "dev": true, + "license": "BSD-3-Clause" + }, "node_modules/schema-utils": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.3.0.tgz", @@ -6714,6 +6743,19 @@ "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "license": "ISC" }, + "node_modules/sinon": { + "version": "1.8.2", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-1.8.2.tgz", + "integrity": "sha512-lKW2kN7YeAOGbqLAMkT/aDSix9ZYZsNAMTmyqP9GHGFK3gDwL/htfK2Aa/jc2K6rtdI2Y0ekrRzKi5J2ZG99kw==", + "deprecated": "16.1.1", + "dev": true, + "dependencies": { + "formatio": "~1.0" + }, + "engines": { + "node": ">=0.1.103" + } + }, "node_modules/sisteransi": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz", diff --git a/package.json b/package.json index 420584f..b39dda3 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "globals": "^15.9.0", "jest": "^29.7.0", "jest-junit": "^16.0.0", + "mongoose-mock": "^0.4.0", "node-mocks-http": "^1.15.1", "nodemailer-mock": "^2.0.6", "supertest": "^7.0.0", diff --git a/test/auth.test.js b/test/auth.test.js index b0cb052..b10769d 100644 --- a/test/auth.test.js +++ b/test/auth.test.js @@ -22,6 +22,7 @@ require('dotenv').config(); const fs = require('fs'); +const path = require('path'); const httpMocks = require('node-mocks-http'); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); @@ -33,7 +34,7 @@ const authController = require('./../controllers/auth.controller'); jest.mock('bcrypt'); jest.mock('jsonwebtoken'); jest.mock('../utils/emailService'); - +jest.mock('mongoose', () => require('mongoose-mock')); /** * @description Unit tests for the login function of the auth controller. */ @@ -41,6 +42,7 @@ describe('Auth Controller', () => { let req, res; const orignalFs = fs.readFileSync + const orignalPath = path.resolve beforeEach(() => { const mockPublicKey = 'mockPublicKeyContent'; fs.readFileSync = jest.fn().mockResolvedValue(mockPublicKey) @@ -50,7 +52,7 @@ describe('Auth Controller', () => { afterEach(async () => { fs.readFileSync = orignalFs; - await db.mongoose.connection.close(); + path.resolve = orignalPath; jest.clearAllMocks(); }); @@ -65,7 +67,7 @@ describe('Auth Controller', () => { db.user.findOne = jest.fn().mockResolvedValue(mockUser); db.session.create = jest.fn(); jwt.sign.mockReturnValue('mocktoken'); - + path.resolve = jest.fn().mockResolvedValue('/./'); await authController.login(req, res); expect(res.statusCode).toBe(200); diff --git a/test/middlewares.test.js b/test/middlewares.test.js index f5fc7ce..599f0e4 100644 --- a/test/middlewares.test.js +++ b/test/middlewares.test.js @@ -19,6 +19,7 @@ require('dotenv').config(); const fs = require('fs'); +const path = require('path'); const httpMocks = require('node-mocks-http'); const { validationResult } = require('express-validator'); const { verifyToken, logger, roleAuthorization, validationErrorHandler } = require('../middlewares'); // Adjust the path as needed @@ -26,7 +27,7 @@ const jwt = require('jsonwebtoken'); const db = require('../models'); jest.mock('express-validator'); - +jest.mock('mongoose', () => require('mongoose-mock')); /** * @description Unit tests for the verifyToken middleware function. * This middleware verifies JWT tokens, checks for token validity, blacklist status, and associated user information. @@ -34,6 +35,7 @@ jest.mock('express-validator'); describe('verifyToken Middleware', () => { let req, res, next; const orignalFs = fs.readFileSync + const orignalPath = path.resolve beforeEach(() => { const mockPublicKey = 'mockPublicKeyContent'; fs.readFileSync = jest.fn().mockResolvedValue(mockPublicKey) @@ -43,7 +45,7 @@ describe('verifyToken Middleware', () => { }); afterEach(async () => { - await db.mongoose.connection.close(); + path.resolve = orignalPath; fs.readFileSync = orignalFs; }) @@ -73,7 +75,7 @@ describe('verifyToken Middleware', () => { jwt.verify = jest.fn((token, publicKey, options, callback) => { callback(new Error('Invalid token'), null); }); - + jest.spyOn(path, 'resolve').mockReturnValueOnce('/fakepath'); verifyToken(req, res, next); setTimeout(() => { @@ -94,7 +96,7 @@ describe('verifyToken Middleware', () => { jwt.verify = jest.fn((token, publicKey, options, callback) => { callback(null, {}); }); - + jest.spyOn(path, 'resolve').mockReturnValueOnce('/fakepath'); verifyToken(req, res, next); setTimeout(() => { @@ -117,7 +119,7 @@ describe('verifyToken Middleware', () => { }); db.session.findOne = jest.fn().mockResolvedValue({ flag: true }); - + jest.spyOn(path, 'resolve').mockReturnValueOnce('/fakepath'); verifyToken(req, res, next); setTimeout(() => { @@ -142,7 +144,7 @@ describe('verifyToken Middleware', () => { db.session.findOne = jest.fn().mockResolvedValue({ flag: false }); db.user.findOne = jest.fn().mockResolvedValue(null); // User not found - + jest.spyOn(path, 'resolve').mockReturnValueOnce('/fakepath'); verifyToken(req, res, next); setTimeout(() => { @@ -153,9 +155,9 @@ describe('verifyToken Middleware', () => { }); /** - * @description Test case for successful token verification. - * The middleware should attach user information to req and call next(). - */ + * @description Test case for successful token verification. + * The middleware should attach user information to req and call next(). + */ it('should attach user info to req and call next() if the token is valid and user is found', (done) => { const mockToken = 'validtoken'; req.headers.authorization = `Bearer ${mockToken}`; @@ -175,6 +177,7 @@ describe('verifyToken Middleware', () => { db.user_roles.find = jest.fn().mockReturnValue({ populate: jest.fn().mockResolvedValue(userRoles) }); + jest.spyOn(path, 'resolve').mockReturnValueOnce('/fakepath'); verifyToken(req, res, next); setTimeout(() => { @@ -203,7 +206,7 @@ describe('verifyToken Middleware', () => { callback(null, { id: 1, username: 'testuser' }); }); db.session.findOne.mockRejectedValue(mockError); - + jest.spyOn(path, 'resolve').mockReturnValueOnce('/fakepath'); verifyToken(req, res, next); setTimeout(() => { expect(res.statusCode).toBe(401); @@ -230,9 +233,6 @@ describe('logger Middleware', () => { res = httpMocks.createResponse(); next = jest.fn(); }); - afterEach(async () => { - await db.mongoose.connection.close(); - }) /** * @description Test case for logging request details to the database. */ @@ -323,9 +323,6 @@ describe('roleAuthorization Middleware', () => { res = httpMocks.createResponse(); next = jest.fn(); }); - afterEach(async () => { - await db.mongoose.connection.close(); - }) /** * @description Test case for allowing access when the user has the required role. @@ -378,9 +375,6 @@ describe('validationErrorHandler Middleware', () => { res = httpMocks.createResponse(); next = jest.fn(); }); - afterEach(async () => { - await db.mongoose.connection.close(); - }) /** * @description Test case for handling validation errors. * The middleware should return a 400 status with the error details. diff --git a/test/role.test.js b/test/role.test.js index bcc9ed0..2000708 100644 --- a/test/role.test.js +++ b/test/role.test.js @@ -24,13 +24,14 @@ const roleController = require('../controllers/role.controller'); const db = require('../models'); const httpMocks = require('node-mocks-http'); +jest.mock('mongoose', () => require('mongoose-mock')); + describe('Role Controller', () => { beforeEach(() => { jest.clearAllMocks(); }); afterEach(async () => { - await db.mongoose.connection.close(); jest.clearAllMocks(); }); diff --git a/test/user.test.js b/test/user.test.js index df61236..862493c 100644 --- a/test/user.test.js +++ b/test/user.test.js @@ -28,7 +28,7 @@ const userController = require('../controllers/user.controller'); const db = require('../models'); const bcrypt = require('bcrypt'); const httpMocks = require('node-mocks-http'); - +jest.mock('mongoose', () => require('mongoose-mock')); describe('User Controller', () => { let userMock; @@ -57,7 +57,6 @@ describe('User Controller', () => { }); afterEach(async () => { - await db.mongoose.connection.close(); jest.clearAllMocks(); });