-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathorganisation.test.js
281 lines (261 loc) · 6.87 KB
/
organisation.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
const app = require('../app').app
const mongoose = require('mongoose')
const request = require('supertest')
const HttpStatus = require('http-status-codes')
const Organization = require('../app/models/Organisation')
const User = require('../app/models/User')
const jwt = require('jsonwebtoken')
const redis = require('../config/redis').redisClient
const adminId = new mongoose.Types.ObjectId()
const moderatorId = new mongoose.Types.ObjectId()
const randomDigit = Math.floor(Math.random() * 90 + 10)
let orgId = ''
let token = ''
const testOrg = {
name: 'test Organization',
description: {
shortDescription: 'this is short description',
longDescription: 'this is long description'
},
contactInfo: {
email: 'organisation@test.com',
website: 'www.codeuino.org',
adminInfo: `${adminId}`,
moderatorInfo: `${moderatorId}`
}
}
const updatedTestOrg = {
name: 'Updated test Organization',
description: {
shortDescription: 'this is updated short description',
longDescription: 'this is updated long description'
},
contactInfo: {
email: 'updated@test.com',
website: 'www.codeuino.org',
adminInfo: `${adminId}`,
moderatorInfo: `${moderatorId}`
}
}
const updateSettings = {
settings: {
enableEmail: true,
language: 'German',
timeFormat: '24'
},
permissions: {
sendInvite: 'ADMINS',
canCreateManage: 'MEMBERS',
canChangeEmail: true,
canChangeName: true
},
authentication: {
email: true,
google: true,
github: true,
gitlab: true
}
}
const testUser = {
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'
}
},
isAdmin: true,
tokens: [{
token: jwt.sign({
_id: `${adminId}`
}, process.env.JWT_SECRET)
}]
}
let server
/**
* This will pe performed once at the beginning of all the test
*/
beforeAll(async (done) => {
await Organization.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()
})
/** CREATE THE ORG **/
describe('POST /org/', () => {
test('should create a new Organization', async (done) => {
const response = await request(app)
.post('/org/')
.send(testOrg)
.expect(HttpStatus.CREATED)
orgId = response.body.orgData._id
/** DB must be changed **/
const org = await Organization.findById(response.body.orgData._id)
expect(org).not.toBeNull()
/** Check the response **/
expect(response.body).toMatchObject({
orgData: {
isArchived: false,
_id: `${orgId}`,
name: `${testOrg.name}`,
description: {
shortDescription: `${testOrg.description.shortDescription}`,
longDescription: `${testOrg.description.longDescription}`
},
contactInfo: {
email: `${testOrg.contactInfo.email}`,
website: `${testOrg.contactInfo.website}`
}
}
})
done()
})
})
/** GET ORG DATA**/
describe('GET /org/:id', () => {
test('Should fetch the Organization data', async (done) => {
await request(app)
.get(`/org/${orgId}`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
done()
})
})
/** UPDATE ORG DETAILS **/
describe('PATCH /org/:id', () => {
test('Should update the Organization data', async (done) => {
await request(app)
.patch(`/org/${orgId}`)
.set('Authorization', `Bearer ${token}`)
.send(updatedTestOrg)
.expect(HttpStatus.OK)
done()
})
})
/** GET ORGANIZATION LOGIN OPTIONS**/
describe('GET login options', () => {
test('Should retrieve the login options', async (done) => {
const res = await request(app)
.get('/org/login/options')
.expect(HttpStatus.OK)
expect(res.body).not.toBeNull()
done()
})
})
/** UPDATE ORGANIZATION SETTINGS**/
describe('UPDATE org-settings', () => {
test('Should update org-settings', async (done) => {
const res = await request(app)
.patch(`/org/${orgId}/settings/update`)
.set('Authorization', `Bearer ${token}`)
.send(updateSettings)
.expect(HttpStatus.OK)
// check res
expect(res.body).not.toBeNull()
done()
})
})
/** GET ORGANIZATION OVERVIEW**/
describe('GET org overview', () => {
test('Should retrieve the organization overview', async (done) => {
const res = await request(app)
.get('/org/overview/all')
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
// check response
expect(res.body).not.toBeNull()
done()
})
})
/** GET ALL MEMBERS **/
describe('GET all members', () => {
test('Should retrieve all the members of the org', async (done) => {
const res = await request(app)
.get('/org/members/all')
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
// check res
expect(res.body).not.toBeNull()
done()
})
})
/** REMOVE ADMIN**/
describe('PATCH /org/remove/:orgId/:userId', () => {
console.log('adminId ', adminId)
test('Should remove the user', async (done) => {
const res = await request(app)
.patch(`/org/remove/${orgId}/${adminId}`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.BAD_REQUEST)
expect(res.body).not.toBeNull()
done()
})
})
/** DELETE ORGANIZATION**/
describe('DELETE /org/:id', () => {
test('Should delete the organization', async (done) => {
await request(app)
.delete(`/org/${orgId}`)
.set('Authorization', `Bearer ${token}`)
.send()
.expect(HttpStatus.OK)
/** Check if deleted or not **/
const org = await Organization.findById(orgId)
expect(org).toBeNull()
done()
})
})
afterAll(async () => {
// avoid jest open handle error
await new Promise((resolve) => setTimeout(() => resolve(), 500))
// close server
await server.close()
// delete all the organization post testing
await Organization.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()
})