-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathproject.test.js
226 lines (207 loc) · 5.17 KB
/
project.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
215
216
217
218
219
220
221
222
223
224
225
226
const app = require('../app').app
const mongoose = require('mongoose')
const jwt = require('jsonwebtoken')
const HttpStatus = require('http-status-codes')
const request = require('supertest')
const Project = require('../app/models/Project')
const User = require('../app/models/User')
const redis = require('../config/redis').redisClient
const randomDigit = Math.floor(Math.random() * 90 + 10)
const pagination = 10
const page = 1
const testUserId = new mongoose.Types.ObjectId()
const testProjectId = new mongoose.Types.ObjectId()
let token = ''
const demoProject = {
projectName: 'testing project',
description: {
short: 'Short description should be min 10 characters long!',
long: 'this is long description'
},
version: '1.0.1',
links: [{
githubLink: 'https://github.com/codeuino'
}]
}
const testProject = {
_id: testProjectId,
...demoProject
}
const updateProject = {
projectName: 'testing project update',
description: {
short: 'Short description should be min 10 characters long!',
long: 'this is long description'
},
version: '1.0.3',
links: [{
githubLink: 'https://github.com/codeuino'
}]
}
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 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 Project.deleteMany()
await redis.flushall()
await new User(testUser).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 project in database,
* and creates a new project in database with the provided details.
*/
beforeEach(async () => {
await Project.deleteMany()
await new Project(testProject).save()
})
/**
* Testing project creation
*/
test('Should create new project', async (done) => {
const response = await request(app)
.post('/project')
.set('Authorization', `Bearer ${token}`)
.send(demoProject)
.expect(HttpStatus.CREATED)
// Assert that db was changed
const project = await Project.findById(response.body.project._id)
expect(project).not.toBeNull()
const userId = response.body.project.createdBy
// Assertions about the response
expect(response.body).toMatchObject({
project: {
projectName: demoProject.projectName,
description: {
short: demoProject.description.short,
long: demoProject.description.long
},
version: demoProject.version,
links: [{
githubLink: demoProject.links[0].githubLink
}],
createdBy: userId
}
})
done()
})
/**
* Testing get all the projects
*/
test('Should get all projects', async (done) => {
await request(app)
.get(`/project?pagination=${pagination}&page=${page}`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
done()
})
/**
* Testing GET project by id
*/
test('Should get project by id', async (done) => {
await request(app)
.get(`/project/${testProjectId}`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
done()
})
/**
* Get project of a user
*/
test('Should get all the project created by a user', async (done) => {
await request(app)
.get(`/project/${testUserId}/all`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
done()
})
/**
* Testing project update
*/
test('Should update the project info', async (done) => {
await request(app)
.patch(`/project/${testProjectId}`)
.set('Authorization', `Bearer ${token}`)
.send(updateProject)
.expect(HttpStatus.OK)
done()
})
/**
* TODO: FIX ERROR
* This is a temporary fix to issue:
* Jest has detected the following 1 open handle potentially keeping Jest from exiting
*/
afterAll(async () => {
// avoid jest open handle error
await new Promise((resolve) => setTimeout(() => resolve(), 500))
// close server
await server.close()
// delete all the projects project testing
await Project.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()
})