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) 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/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/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/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() +}) diff --git a/test/user.test.js b/test/user.test.js index 04534ec..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) @@ -209,7 +241,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 +253,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 +275,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 +290,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 */