Skip to content

Commit

Permalink
Fixes issues in testing (#160)
Browse files Browse the repository at this point in the history
* fix failing test file user.test.js

* fixes EADDRINUSE while testing

* fixes issues in prposal route

* fixed issues in org route and added new tests
  • Loading branch information
ksraj123 authored Jul 29, 2020
1 parent 48e8523 commit 8f92e6b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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'])
Expand Down
3 changes: 3 additions & 0 deletions app/models/Proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const proposalSchema = new Schema(
type: String,
required: true
},
organization: {
type: String
},
content: {
type: String,
required: true
Expand Down
6 changes: 3 additions & 3 deletions test/organisation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
11 changes: 10 additions & 1 deletion test/proposal.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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()
})
40 changes: 36 additions & 4 deletions test/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]'
})
.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: '[email protected]'
})
.expect(HttpStatus.UNAUTHORIZED)
})

/** Delete authenticated user profile */
test('Should delete profile of authenticated user', async () => {
await request(app)
Expand All @@ -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}`
})
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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 */
Expand Down

0 comments on commit 8f92e6b

Please sign in to comment.