From 4157431589ff993e7eeea9b3594d3348224e5b73 Mon Sep 17 00:00:00 2001 From: ksraj123 Date: Tue, 28 Jul 2020 21:26:34 +0530 Subject: [PATCH 1/4] fix failing test file user.test.js --- app/controllers/user.js | 6 +++--- test/user.test.js | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/user.js b/app/controllers/user.js index 8b3303a..5cc134d 100644 --- a/app/controllers/user.js +++ b/app/controllers/user.js @@ -129,7 +129,7 @@ module.exports = { notification.heading = 'Forgot password!' notification.content = 'Password successfully updated!' notification.tag = TAGS.UPDATE - notificationHelper.addToNotificationForUser(id, res, notification, next) + await notificationHelper.addToNotificationForUser(id, res, notification, next) return res.status(HttpStatus.OK).json({ updated: true }) } else { res.status(HttpStatus.BAD_REQUEST).json({ error: 'Token expired' }) @@ -184,7 +184,7 @@ module.exports = { notification.heading = 'Account activate!' notification.content = 'Account successfully activated!' notification.tag = TAGS.ACTIVATE - notificationHelper.addToNotificationForUser(user._id, res, notification, next) + await notificationHelper.addToNotificationForUser(user._id, res, notification, next) return res.status(HttpStatus.OK).json({ msg: 'Succesfully activated!' }) } } catch (Error) { @@ -266,7 +266,7 @@ module.exports = { notification.heading = 'New follower!' notification.content = `${req.user.name.firstName} started following you!` notification.tag = TAGS.FOLLOWER - notificationHelper.addToNotificationForUser(user._id, res, notification, next) + await notificationHelper.addToNotificationForUser(user._id, res, notification, next) const userData = await User.findById(req.user._id) .populate('followings', ['name.firstName', 'name.lastName', 'info.about.designation', '_id', 'isAdmin']) .populate('followers', ['name.firstName', 'name.lastName', 'info.about.designation', '_id', 'isAdmin']) diff --git a/test/user.test.js b/test/user.test.js index 04534ec..b1fa087 100644 --- a/test/user.test.js +++ b/test/user.test.js @@ -209,7 +209,7 @@ test('Should not delete profile of unauthenticated user', async () => { /** Forgot password request **/ test('Should send the request to change the password ', async () => { const response = await request(app) - .post('/user/password_reset') + .patch('/user/password_reset/request') .send({ email: `${testUser.email}` }) @@ -221,7 +221,7 @@ test('Should send the request to change the password ', async () => { /* Password update */ test('Should update the password ', async () => { await request(app) - .post(`/user/password_reset/${passwordToken}`) + .patch(`/user/password_reset/${passwordToken}`) .send({ password: 'newPassword', id: testUserId @@ -243,7 +243,7 @@ test('Should activate the account ', async (done) => { /* Get invite link */ test('Should generate an invite link and send', async () => { const response = await request(app) - .get('/user/invite') + .get('/user/invite?role=user') .set('Authorization', `Bearer ${testUser.tokens[0].token}`) .send() .expect(HttpStatus.OK) @@ -258,7 +258,7 @@ test('Should validate the invite link token ', async () => { await request(app) .get(`/user/invite/${inviteToken}`) .send() - .expect(HttpStatus.OK) + .expect(HttpStatus.MOVED_TEMPORARILY) }) /* Logout the user */ From 099f6cf4b6fedb32fcb66801d60253af09965635 Mon Sep 17 00:00:00 2001 From: ksraj123 Date: Wed, 29 Jul 2020 03:33:14 +0530 Subject: [PATCH 2/4] fixes EADDRINUSE while testing --- app.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index f32a335..49c4f40 100644 --- a/app.js +++ b/app.js @@ -34,7 +34,9 @@ app.use(bodyParser.urlencoded(fileConstants.fileParameters)) const memoryStorage = multer.memoryStorage() app.use(multer({ storage: memoryStorage }).single('file')) -server.listen(process.env.SOCKET_PORT || 8810) +if (process.env.NODE_ENV !== 'testing') { + server.listen(process.env.SOCKET_PORT || 8810) +} // WARNING: app.listen(80) will NOT work here! const io = socket.listen(server) From f2f98214cdd43792825f6a18a50cc1cd15c880b7 Mon Sep 17 00:00:00 2001 From: ksraj123 Date: Wed, 29 Jul 2020 13:40:39 +0530 Subject: [PATCH 3/4] fixes issues in prposal route --- app/models/Proposal.js | 3 +++ test/proposal.test.js | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/Proposal.js b/app/models/Proposal.js index 4a1f97b..813be0c 100644 --- a/app/models/Proposal.js +++ b/app/models/Proposal.js @@ -7,6 +7,9 @@ const proposalSchema = new Schema( type: String, required: true }, + organization: { + type: String + }, content: { type: String, required: true diff --git a/test/proposal.test.js b/test/proposal.test.js index 1098d9f..5a0b70d 100644 --- a/test/proposal.test.js +++ b/test/proposal.test.js @@ -1,4 +1,4 @@ -const app = require('../app') +const app = require('../app').app const mongoose = require('mongoose') const jwt = require('jsonwebtoken') const HttpStatus = require('http-status-codes') @@ -195,3 +195,12 @@ test('Should return the proposal by the given Id', async (done) => { done() }) + +afterAll(async () => { + // avoid jest open handle error + await new Promise((resolve) => setTimeout(() => resolve(), 500)) + // close server + await server.close() + // Closing the DB connection allows Jest to exit successfully. + await mongoose.connection.close() +}) From ab05538b3758d91e2e3bb21ce7c3060d63177b68 Mon Sep 17 00:00:00 2001 From: ksraj123 Date: Wed, 29 Jul 2020 17:42:01 +0530 Subject: [PATCH 4/4] fixed issues in org route and added new tests --- test/organisation.test.js | 6 +++--- test/user.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/test/organisation.test.js b/test/organisation.test.js index 12f966b..f159266 100644 --- a/test/organisation.test.js +++ b/test/organisation.test.js @@ -108,14 +108,14 @@ describe('POST /org/', () => { .set('Authorization', `Bearer ${token}`) .send(testOrg) .expect(HttpStatus.CREATED) - orgId = response.body.org._id + orgId = response.body.orgData._id /** DB must be changed **/ - const org = await Organization.findById(response.body.org._id) + const org = await Organization.findById(response.body.orgData._id) expect(org).not.toBeNull() /** Check the response **/ expect(response.body).toMatchObject({ - org: { + orgData: { isArchived: false, _id: `${orgId}`, name: `${testOrg.name}`, diff --git a/test/user.test.js b/test/user.test.js index b1fa087..f2df44a 100644 --- a/test/user.test.js +++ b/test/user.test.js @@ -185,6 +185,38 @@ test('Should not get profile for unauthenticated user', async () => { .expect(HttpStatus.UNAUTHORIZED) }) +/** Should update user profile */ +test('Should update profile or authenticated user', async () => { + await request(app) + .patch('/user/me') + .set('Authorization', `Bearer ${testUser.tokens[0].token}`) + .send({ + email: 'updated@example.com' + }) + .expect(HttpStatus.OK) +}) + +/** Should fail to make updates that are not allowed to user profile */ +test('Should be able to make only allowed updates to authenticated user', async () => { + await request(app) + .patch('/user/me') + .set('Authorization', `Bearer ${testUser.tokens[0].token}`) + .send({ + gender: 'Male' + }) + .expect(HttpStatus.BAD_REQUEST) +}) + +/** Should Fail updating profile of unauthenticate user */ +test('Should not update profile or unauthenticated user', async () => { + await request(app) + .patch('/user/me') + .send({ + email: 'updated@example.com' + }) + .expect(HttpStatus.UNAUTHORIZED) +}) + /** Delete authenticated user profile */ test('Should delete profile of authenticated user', async () => { await request(app)