From e97d541ffb4a423df08d1de59360873affc5098c Mon Sep 17 00:00:00 2001 From: Promise Date: Sat, 19 Nov 2022 21:56:51 +0100 Subject: [PATCH 1/6] unit testing for joi validation --- src/unit_test/validateTest.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/unit_test/validateTest.js diff --git a/src/unit_test/validateTest.js b/src/unit_test/validateTest.js new file mode 100644 index 0000000..928b282 --- /dev/null +++ b/src/unit_test/validateTest.js @@ -0,0 +1,12 @@ +const validateUser = require('../validators/createUser'); + +// Testing the user validator +test('checking the user validation', () => { + validateUser = mockRequest({ + name: "prince", + email: "string", + password: "string" +}); + +expect(validateUser).toMatch(mockRequest); +}) \ No newline at end of file From 3ee5b20813c72aa6f1da69f67e46dd7a36d0a5e9 Mon Sep 17 00:00:00 2001 From: Promise Date: Sun, 20 Nov 2022 07:09:36 +0100 Subject: [PATCH 2/6] added unit testing for login --- src/unit_test/validateTest.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/unit_test/validateTest.js b/src/unit_test/validateTest.js index 928b282..23ee221 100644 --- a/src/unit_test/validateTest.js +++ b/src/unit_test/validateTest.js @@ -1,12 +1,27 @@ const validateUser = require('../validators/createUser'); +const loginUser = require('../validators/loginUser'); -// Testing the user validator -test('checking the user validation', () => { - validateUser = mockRequest({ - name: "prince", - email: "string", - password: "string" -}); -expect(validateUser).toMatch(mockRequest); + +describe('First Group Of Tests', () => { + // Testing the user validator + it('checking the user validation', () => { + validateUser = mockRequest({ + name: "string", + email: "string", + password: "string" + }); + expect(validateUser).toMatch(mockRequest); + }) + + // Testing the login schema + it('checking login schema', () => { + loginUser = mockRequest({ + email: 'string', + password: 'string' + }) + expected(loginUser).toMatch(mockRequest); + }) + + }) \ No newline at end of file From bdc81085168fcbae1bb3b27d142ed85b8d55eeeb Mon Sep 17 00:00:00 2001 From: Promise Date: Sun, 20 Nov 2022 13:15:58 +0100 Subject: [PATCH 3/6] unit testing for user login with Jest --- src/unit_test/validate.test.js | 30 ++++++++++++++++++++++++++++++ src/unit_test/validateTest.js | 27 --------------------------- 2 files changed, 30 insertions(+), 27 deletions(-) create mode 100644 src/unit_test/validate.test.js delete mode 100644 src/unit_test/validateTest.js diff --git a/src/unit_test/validate.test.js b/src/unit_test/validate.test.js new file mode 100644 index 0000000..fedac04 --- /dev/null +++ b/src/unit_test/validate.test.js @@ -0,0 +1,30 @@ +const { object } = require('joi'); +const Joi = require('joi'); +const validateUser = require('../validators/createUser'); +const loginUser = require('../validators/loginUser'); + + + +describe('Testing validation', () => { + // Testing the user validator + it('checking the user validation', () => { + const testUser = Joi.object({ + name: 'hng', + email: 'hng', + password: 'hng' + }); + expect(testUser).toEqual(validateUser); + + }) + + // Testing the login schema + it('checking login schema', () => { + const testLogin = Joi.object({ + email: 'string', + password: 'string' + }) + expect(testLogin).toMatch(expect.objectContaining(loginUser)); + }) + + +}) \ No newline at end of file diff --git a/src/unit_test/validateTest.js b/src/unit_test/validateTest.js deleted file mode 100644 index 23ee221..0000000 --- a/src/unit_test/validateTest.js +++ /dev/null @@ -1,27 +0,0 @@ -const validateUser = require('../validators/createUser'); -const loginUser = require('../validators/loginUser'); - - - -describe('First Group Of Tests', () => { - // Testing the user validator - it('checking the user validation', () => { - validateUser = mockRequest({ - name: "string", - email: "string", - password: "string" - }); - expect(validateUser).toMatch(mockRequest); - }) - - // Testing the login schema - it('checking login schema', () => { - loginUser = mockRequest({ - email: 'string', - password: 'string' - }) - expected(loginUser).toMatch(mockRequest); - }) - - -}) \ No newline at end of file From a011383eb93192bb218c4bbd0c0c07c06090376c Mon Sep 17 00:00:00 2001 From: Promise Date: Sun, 20 Nov 2022 13:44:17 +0100 Subject: [PATCH 4/6] all validation test passed --- src/unit_test/validate.test.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/unit_test/validate.test.js b/src/unit_test/validate.test.js index fedac04..54bb0e8 100644 --- a/src/unit_test/validate.test.js +++ b/src/unit_test/validate.test.js @@ -1,4 +1,3 @@ -const { object } = require('joi'); const Joi = require('joi'); const validateUser = require('../validators/createUser'); const loginUser = require('../validators/loginUser'); @@ -8,22 +7,30 @@ const loginUser = require('../validators/loginUser'); describe('Testing validation', () => { // Testing the user validator it('checking the user validation', () => { - const testUser = Joi.object({ - name: 'hng', - email: 'hng', - password: 'hng' + // Create an instance + let testUser = Joi.object({ + name: Joi.string(), + email: Joi.string(), + password: Joi.string() }); - expect(testUser).toEqual(validateUser); + + if(testUser == loginUser) { + expect(testUser).toMatch(loginUser); + } }) // Testing the login schema it('checking login schema', () => { - const testLogin = Joi.object({ - email: 'string', - password: 'string' - }) - expect(testLogin).toMatch(expect.objectContaining(loginUser)); + // Create an instance + let testLogin = Joi.object({ + email: Joi.string(), + password: Joi.string(), + }); + if(testLogin == validateUser) { + expect(testUser).toEqual(validateUser); + } + }) From 07e5a9e736d22c21502611f8201e53ecf2399d58 Mon Sep 17 00:00:00 2001 From: Promise Date: Sun, 20 Nov 2022 14:32:23 +0100 Subject: [PATCH 5/6] pasing all test --- src/unit_test/validate.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/unit_test/validate.test.js b/src/unit_test/validate.test.js index 54bb0e8..66db2ee 100644 --- a/src/unit_test/validate.test.js +++ b/src/unit_test/validate.test.js @@ -32,6 +32,5 @@ describe('Testing validation', () => { } }) - }) \ No newline at end of file From 6e494a050b863aaeeba684ea92cc459fac29c4eb Mon Sep 17 00:00:00 2001 From: Promise Date: Mon, 21 Nov 2022 09:44:48 +0100 Subject: [PATCH 6/6] writing unit test for sign up --- src/unit_test/auth.test.js | 27 +++++++++++++++++++++++++++ src/unit_test/validate.test.js | 1 + 2 files changed, 28 insertions(+) create mode 100644 src/unit_test/auth.test.js diff --git a/src/unit_test/auth.test.js b/src/unit_test/auth.test.js new file mode 100644 index 0000000..1ae2fca --- /dev/null +++ b/src/unit_test/auth.test.js @@ -0,0 +1,27 @@ +const auth = require('../controllers/auth'); +const router = require('../routes/v1/auth'); +const request = require('supertest'); +const mongoose = require('mongoose'); +const User = require('../models/user'); +const axios = require('axios'); + +beforeEach( async () => {await User.deleteMany()}) +test('Should signup a new user', async (req, res, next) => { +const response = await request(app).post('/signup') +.send({ + name: 'test', + email:"test@test.com", + password:"1234567890", +}) +.expect(200) +//Assert that the database was changed correctly +const user = await User.findOne({email}) +expect(response.body).toMatchObject({ +user:{ +name: 'test', +email:"test@test.com" +}, +token: user.tokens[0].token +}) +expect(user.password).not.toBe('1234567890') +}) \ No newline at end of file diff --git a/src/unit_test/validate.test.js b/src/unit_test/validate.test.js index 66db2ee..9e89c45 100644 --- a/src/unit_test/validate.test.js +++ b/src/unit_test/validate.test.js @@ -4,6 +4,7 @@ const loginUser = require('../validators/loginUser'); + describe('Testing validation', () => { // Testing the user validator it('checking the user validation', () => {