-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathproposal.test.js
214 lines (189 loc) · 5.02 KB
/
proposal.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const app = require('../app').app
const mongoose = require('mongoose')
const jwt = require('jsonwebtoken')
const HttpStatus = require('http-status-codes')
const request = require('supertest')
const User = require('../app/models/User')
const Organization = require('../app/models/Organisation')
const Proposal = require('../app/models/Proposal')
const redis = require('../config/redis').redisClient
const randomDigit = Math.floor(Math.random() * 90 + 10)
const testUserId = new mongoose.Types.ObjectId()
const testOrganizationId = new mongoose.Types.ObjectId()
const testProposalId = new mongoose.Types.ObjectId()
let token = ''
const demoproposal = {
title: 'Test Proposal',
organization: testOrganizationId,
content: 'Content of the example proposal',
proposalStatus: 'DRAFT',
creator: testUserId
}
const testProposal = {
_id: testProposalId,
...demoproposal
}
const demoUser = {
name: {
firstName: 'test',
lastName: 'test'
},
email: `test${randomDigit}@mailinator.com`,
phone: `12345678${randomDigit}`,
password: 'abc12345',
info: {
about: {
shortDescription: 'this is short description',
longDescription: 'this is a very long description',
website: 'https://www.google.com',
designation: 'software engg',
skills: ['c++', 'java'],
education: [
{
school: {
schoolName: 'firstSchoolName',
year: '2017-2021'
}
},
{
school: {
schoolName: 'secondSchoolName',
year: '2007-2014'
}
}
],
location: 'location'
}
}
}
const demoOrganization = {
name: 'Codeuino',
description: {
shortDescription: 'short desc',
longDescription: 'long Description included here'
},
contactInfo: {
email: 'organisation@test.com',
website: 'www.codeuino.org'
}
}
const testOrganization = {
_id: testOrganizationId,
...demoOrganization
}
const updatedProposalContent = {
content: 'updated proposal content'
}
const testUser = {
_id: testUserId,
...demoUser,
email: `test${randomDigit}@mailinator.com`,
phone: `12345678${randomDigit}`,
tokens: [
{
token: jwt.sign(
{
_id: testUserId
},
process.env.JWT_SECRET
)
}
]
}
let server
/**
* This will pe performed once at the beginning of the test
*/
beforeAll(async (done) => {
await Proposal.deleteMany()
await redis.flushall()
await new User(testUser).save()
await new Organization(testOrganization).save()
server = app.listen(4000, () => {
global.agent = request.agent(server)
})
const response = await request(app).post('/auth/login').send({
email: testUser.email,
password: testUser.password
})
token = response.body.token
done()
})
/**
* This deletes all the existing user in database,
* and creates a new user in database with the provided details.
*/
beforeEach(async () => {
await Proposal.deleteMany()
await new Proposal(testProposal).save()
})
test('Should create new Proposal', async (done) => {
const response = await request(app)
.post('/proposal')
.set('Authorization', `Bearer ${token}`)
.send(demoproposal)
.expect(HttpStatus.CREATED)
const proposal = await Proposal.findById(response.body.proposal._id)
expect(proposal).not.toBeNull()
const userId = response.body.proposal.creator
expect(response.body).toMatchObject({
proposal: {
title: demoproposal.title,
organization: `${testOrganizationId}`,
content: demoproposal.content,
proposalStatus: demoproposal.proposalStatus,
creator: `${userId}`
}
})
done()
})
// Testing proposal update
test('Should update the content of the proposal', async (done) => {
await request(app)
.patch(`/proposal/${testProposalId}`)
.set('Authorization', `Bearer ${token}`)
.send(updatedProposalContent)
.expect(HttpStatus.OK)
done()
})
// Testing proposal delete
const deleteProposalContent = {
proposalId: testProposalId
}
test('Should delete the proposal', async (done) => {
await request(app)
.delete('/proposal')
.set('Authorization', `Bearer ${token}`)
.send(deleteProposalContent)
.expect(HttpStatus.OK)
// confirm that the proposal was deleted
const proposal = await Proposal.findById(testProposalId)
expect(proposal).toBeNull()
done()
})
// Testing get proposalById
const getByIdContent = {
proposalId: testProposalId
}
test('Should return the proposal by the given Id', async (done) => {
await request(app)
.get(`/proposal/${testProposalId}`)
.set('Authorization', `Bearer ${token}`)
.send(getByIdContent)
.expect(HttpStatus.OK)
done()
})
afterAll(async () => {
// avoid jest open handle error
await new Promise((resolve) => setTimeout(() => resolve(), 500))
// close server
await server.close()
// delete proposal
await Proposal.deleteMany()
// delete all the user created
await User.deleteMany()
// flush redis
await redis.flushall()
// Closing the DB connection allows Jest to exit successfully.
await mongoose.connection.close()
})