forked from rowlandekemezie/Document-Manager-REST-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.spec.js
More file actions
executable file
·419 lines (394 loc) · 14.5 KB
/
Copy pathdocument.spec.js
File metadata and controls
executable file
·419 lines (394 loc) · 14.5 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
(function() {
'use strict';
var mongoose = require('mongoose'),
expect = require('chai').expect,
app = require('./../index'),
request = require('supertest')(app),
jwt = require('jsonwebtoken'),
config = require('./../server/config/pass'),
model = require('./../server/app/models'),
date = require('./../seeds/dateHelper')(),
userData = require('./../seeds/users.json'),
roleData = require('./../seeds/roles.json'),
docData = require('./../seeds/documents.json'),
User = model.User,
Role = model.Role,
Document = model.Document;
describe('DOCUMENT TESTS', function() {
describe('CREATE DOCUMENT POST /api/documents/', function() {
var id, userToken;
beforeEach(function(done) {
var newUser = new User(userData[0]);
var newRole = new Role(roleData[0]);
newUser.save();
newRole.save();
id = newUser._id;
userToken = jwt.sign(newUser, config.secret, {
expiresIn: 86400 // expires in 24 hrs
});
done();
});
afterEach(function(done) {
Document.remove({}, function() {
User.remove({}, function() {
Role.remove({}, function() {
done();
});
});
});
});
it('should create document for user with credentials', function(done) {
request.post('/api/documents')
.set('x-access-token', userToken)
.send({
title: docData[0].title,
content: docData[0].content,
role: docData[0].role,
ownerId: id
})
.end(function(err, res) {
expect(res.status).to.equal(200);
expect(err).not.to.be.a('undefined');
expect(res.body).contain({
success: true,
message: 'Document successfully created'
});
done();
});
});
it('should create document with unique title', function(done) {
var doctest = docData[0];
doctest.ownerId = id;
var newDoc = new Document(doctest);
newDoc.save();
request.post('/api/documents/')
.set('x-access-token', userToken)
.send(docData[0])
.end(function(err, res) {
expect(res.status).to.equal(409);
expect(err).not.to.be.a('undefined');
expect(err).to.be.a('null');
expect(res.body).contain({
success: false,
message: 'Document already exist'
});
done();
});
});
it('should not create document for unauthenticated user', function(done) {
request.post('/api/documents/')
.send(docData[1])
.end(function(err, res) {
expect(res.status).to.equal(403);
expect(err).not.to.be.a('undefined');
expect(res.body).contain({
success: false,
message: 'Please provide your token'
});
done();
});
});
it('should not create a document without a valid role', function(done) {
request.post('/api/documents')
.set('x-access-token', userToken)
.send({
title: docData[0].title,
content: docData[0].content,
role: 'DoesntExist'
})
.end(function(err, res) {
expect(res.status).to.equal(406);
expect(err).not.to.be.a('undefined');
expect(err).to.be.a('null');
expect(res.body).contain({
success: false,
message: 'Invalid role'
});
done();
});
});
});
describe('GET, UPDATE, DELETE DOCUMENTS on /api/documents/', function() {
var docId, roleTitle, userToken, limit = 1;
beforeEach(function(done) {
var newRole = new Role(roleData[0]);
var newUser = new User(userData[0]);
newRole.save();
newUser.save();
var docTest = docData[0];
docTest.ownerId = newUser._id;
roleTitle = docTest.role;
var newDoc = new Document(docTest);
newDoc.save();
// assign newDoc._id to a global varaible to be used in this suite
docId = newDoc._id;
date = newDoc.dateCreated;
userToken = jwt.sign(newUser, config.secret, {
expiresIn: 86400
});
done();
});
afterEach(function(done) {
Document.remove({}, function() {
User.remove({}, function() {
Role.remove({}, function() {
done();
});
});
});
});
it('it should return all documents', function(done) {
for (var i = 1, n = docData.length; i < n; i++) {
var newRole = new Role(roleData[i]);
var newUser = new User(userData[i]);
newRole.save();
newUser.save();
var docDetail = docData[i];
docDetail.ownerId = newUser._id;
var newDoc = new Document(docDetail);
newDoc.save();
}
request.get('/api/documents/')
.set('x-access-token', userToken)
.end(function(err, res) {
expect(res.status).to.equal(200);
expect(err).to.be.a('null');
expect(res.body.length).to.equal(3);
expect(res.body[0]).contain({
title: 'The regalia',
role: 'Documentarian',
content: 'Kings and people of royal heritage are known for their ' +
'great outward look at all times as a demonstration of their ' +
'royalty'
});
expect(res.body[1]).contain({
title: 'The quest for dividends of democracy',
role: 'Trainer',
content: 'Democracy without dividend is arnachy in disguise. ' +
'Every through democratic institution must plan to deliver ' +
'to the citizens they represent'
});
expect(res.body[2]).contain({
title: 'The beautiful ones are not yet born',
role: 'Librarian',
content: 'It makes more sense when people understand the essence ' +
'of existence rather outward beauty that fades away and is no more'
});
done();
});
});
it('should return all documents limited by a value', function(done) {
request.get('/api/documents/limit/' + limit)
.set('x-access-token', userToken)
.end(function(err, res) {
expect(res.status).to.equal(200);
expect(res.body.length).to.equal(1);
expect(res.body[0]).contain({
title: 'The regalia',
role: 'Documentarian',
content: 'Kings and people of royal heritage are known for their ' +
'great outward look at all times as a demonstration of their ' +
'royalty'
});
done();
});
});
it('should return all documents for a user', function(done) {
var user1 = new User(userData[1]);
user1.save();
var doc1 = docData[1];
var doc2 = docData[2];
doc1.ownerId = user1._id;
doc2.ownerId = user1._id;
var newDoc1 = new Document(doc1);
var newDoc2 = new Document(doc2);
newDoc1.save();
newDoc2.save();
request.get('/api/users/' + user1._id + '/documents')
.set('x-access-token', userToken)
.end(function(err, res) {
expect(res.status).to.equal(200);
expect(res.body.length).to.equal(2);
expect(res.body[0]).contain({
title: 'The quest for dividends of democracy',
role: 'Trainer',
content: 'Democracy without dividend is arnachy in disguise. ' +
'Every through democratic institution must plan to ' +
'deliver to the citizens they represent'
});
expect(res.body[1]).contain({
title: 'The beautiful ones are not yet born',
role: 'Librarian',
content: 'It makes more sense when people understand the essence ' +
'of existence rather outward beauty that fades away and is no more'
});
done();
});
});
it('should return documents for a specfic role', function(done) {
request.get('/api/documents/role/' + roleTitle + '/' + limit)
.set('x-access-token', userToken)
.end(function(err, res) {
expect(res.status).to.equal(200);
expect(res.body.length).to.equal(1);
expect(res.body[0]).contain({
title: 'The regalia',
content: 'Kings and people of royal heritage are known for ' +
'their great outward look at all times as a demonstration ' +
'of their royalty',
role: 'Documentarian'
});
done();
});
});
it('should return documents for a specfic date', function(done) {
request.get('/api/documents/date/' + date + '/' + limit)
.set('x-access-token', userToken)
.end(function(err, res) {
expect(res.status).to.equal(200);
expect(res.body.length).to.equal(1);
expect(res.body[0]).contain({
title: 'The regalia',
content: 'Kings and people of royal heritage are known for ' +
'their great outward look at all times as a demonstration of ' +
'their royalty',
role: 'Documentarian'
});
done();
});
});
it('should not return document for an invalid user Id', function(done) {
var invalidId = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
request.get('/api/users/' + invalidId + '/documents')
.set('x-access-token', userToken)
.end(function(err, res) {
expect(res.status).to.equal(404);
expect(res.body).not.to.be.a('null');
expect(res.body).contain({
success: false,
message: 'User has no document'
});
done();
});
});
it('should return document for an id', function(done) {
request.get('/api/documents/' + docId)
.set('x-access-token', userToken)
.end(function(err, res) {
expect(res.status).to.equal(200);
expect(res).not.to.be.a('null');
expect(res.body).contain({
title: 'The regalia',
role: 'Documentarian',
content: 'Kings and people of royal heritage are known for their ' +
'great outward look at all times as a demonstration of their ' +
'royalty'
});
done();
});
});
it('should not return any document for invalid id', function(done) {
var invalidId = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
request.get('/api/documents/' + invalidId)
.set('x-access-token', userToken)
.end(function(err, res) {
expect(res.status).to.equal(404);
expect(res).not.to.be.a('null');
expect(res.body).contain({
success: false,
message: 'No document found for the Id'
});
done();
});
});
it('should update document by id', function(done) {
request.put('/api/documents/' + docId)
.set('x-access-token', userToken)
.send({
title: 'Testing update route',
content: 'Nice practicing and doing TDD'
})
.end(function(err, res) {
expect(res.status).to.equal(200);
expect(res.body).contain({
success: true,
message: 'Document updated successfully'
});
done();
});
});
it('should not update document of another user', function(done) {
var newUser1 = new User(userData[1]);
newUser1.save();
var anotheUser = jwt.sign(newUser1, config.secret, {
expiresIn: 86400
});
request.put('/api/documents/' + docId)
.set('x-access-token', anotheUser).send({
title: 'You can\'t update another\'s document',
content: 'Document can only be updated by owner, ' +
'SuperAdmin, or Documentarian'
})
.end(function(err, res) {
expect(err).to.be.a('null');
expect(res.status).to.equal(403);
expect(res.forbidden).to.equal(true);
expect(res.body).contain({
success: false,
message: 'Forbidden. You don\'t have the permission'
});
done();
});
});
it('should not delete document of another user', function(done) {
var newUser1 = new User(userData[1]);
newUser1.save();
var anotheUser = jwt.sign(newUser1, config.secret, {
expiresIn: 86400
});
request.put('/api/documents/' + docId)
.set('x-access-token', anotheUser).send({
title: 'You can\'t delete another\'s document',
content: 'Document can only be delete by owner, ' +
'SuperAdmin, or Documentarian'
})
.end(function(err, res) {
expect(err).to.be.a('null');
expect(res.status).to.equal(403);
expect(res.forbidden).to.equal(true);
expect(res.body).contain({
success: false,
message: 'Forbidden. You don\'t have the permission'
});
done();
});
});
it('should delete document by id', function(done) {
request.delete('/api/documents/' + docId)
.set('x-access-token', userToken)
.end(function(err, res) {
expect(res.status).to.equal(200);
expect(res.body).contain({
success: true,
message: 'Document deleted successfully'
});
done();
});
});
it('should not delete any document by an invalid id', function(done) {
var invalidId = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
request.delete('/api/documents/' + invalidId)
.set('x-access-token', userToken)
.end(function(err, res) {
expect(res.status).to.equal(404);
expect(res).not.to.be.a('null');
expect(res.body).contain({
success: false,
message: 'Document not available'
});
done();
});
});
});
});
})();