-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathpost.test.js
286 lines (257 loc) · 5.9 KB
/
post.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const app = require('../app').app
const mongoose = require('mongoose')
const jwt = require('jsonwebtoken')
const HttpStatus = require('http-status-codes')
const request = require('supertest')
const Post = require('../app/models/Post')
const User = require('../app/models/User')
const redis = require('../config/redis').redisClient
const randomDigit = Math.floor(Math.random() * 90 + 10)
const testUserId = new mongoose.Types.ObjectId()
let token = ''
const demoPost = {
content: 'test post content',
userId: testUserId,
votes: {
upVotes: {
user: []
},
downVotes: {
user: []
}
}
}
const updatePost = {
content: 'updated post content'
}
const upvotePost = {
content: 'test post content',
userId: testUserId,
votes: {
upVotes: {
user: [
testUserId
]
},
downVotes: {
user: []
}
}
}
const testPostId = new mongoose.Types.ObjectId()
const testPost = {
_id: testPostId,
...demoPost
}
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 Post.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 user in database,
* and creates a new user in database with the provided details.
*/
beforeEach(async () => {
await Post.deleteMany()
await new Post(testPost).save()
})
/**
* Testing post creation
*/
test('Should create new post', async (done) => {
const response = await request(app)
.post('/post')
.set('Authorization', `Bearer ${token}`)
.send(demoPost)
.expect(HttpStatus.CREATED)
// Assert that db was changed
const post = await Post.findById(response.body.post._id)
expect(post).not.toBeNull()
const userId = response.body.post.userId
// Assertions about the response
expect(response.body).toMatchObject({
post: {
content: demoPost.content,
userId: `${userId}`,
votes: {
upVotes: {
user: response.body.post.votes.upVotes.user
}
}
}
})
done()
})
/**
* Testing post deletion
*/
test('Should delete post', async (done) => {
await request(app)
.delete(`/post/${testPostId}`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
// Assert that post was deleted
const post = await Post.findById(testPostId)
expect(post).toBeNull()
done()
})
/**
* Testing GET post by id
*/
test('Should get single post by id', async (done) => {
await request(app)
.get(`/post/${testPostId}`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
done()
})
/**
* Testing upvote post
*/
test('Should upvote the post', async (done) => {
const response = await request(app)
.patch(`/post/upvote/${testPostId}`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
const userId = response.body.post.userId
expect(response.body).toMatchObject({
post: {
content: upvotePost.content,
userId: `${userId}`,
votes: {
upVotes: {
user: response.body.post.votes.upVotes.user
}
}
}
})
done()
})
/**
* Testing post update
*/
test('Should update the Post data', async (done) => {
await request(app)
.patch(`/post/${testPostId}`)
.set('Authorization', `Bearer ${token}`)
.send(updatePost)
.expect(HttpStatus.OK)
done()
})
/**
* Testing get post of a particular user
*/
test('Should retrieve all posts created by a user', async (done) => {
await request(app)
.get(`/post/${testUserId}/all`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
done()
})
/**
* Testing pin post of by particular user
*/
test('Should pin the post', async (done) => {
await request(app)
.patch(`/post/pin/${testPostId}`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
done()
})
/**
* Testing get all pinned post
*/
test('Should retrieve all the pinned post', async (done) => {
await request(app)
.get('/post/all/pinned?pagination=10&page=1')
.set('Authorization', `Bearer ${token}`)
.send()
.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 posts post testing
await Post.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()
})