-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaccount.test.js
More file actions
664 lines (634 loc) Β· 24.8 KB
/
account.test.js
File metadata and controls
664 lines (634 loc) Β· 24.8 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
"use strict";
const chai = require("chai");
const chaiHttp = require("chai-http");
chai.use(chaiHttp);
const server = require("../app");
const logger = require("../services/logger.service");
const Account = require("../models/account.model");
const should = chai.should();
const Constants = {
Error: require("../constants/error.constant"),
General: require("../constants/general.constant"),
Success: require("../constants/success.constant")
};
const util = {
account: require("./util/account.test.util"),
auth: require("./util/auth.test.util"),
accountConfirmation: require("./util/accountConfirmation.test.util"),
reset: require("./util/resetPassword.test.util")
};
const agent = chai.request.agent(server.app);
// tokens
const confirmationToken = util.accountConfirmation.ConfirmationToken;
const fakeToken = util.accountConfirmation.FakeToken;
const resetToken = util.reset.ResetToken;
// accounts
const Admin0 = util.account.adminAccounts.stored[0];
const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0];
//This account has a confirmation token in the db
const storedAccount1 = util.account.NonConfirmedAccount1;
const storedAccount2 = util.account.NonConfirmedAccount2;
//This account does not have a confirmation token in the DB
const storedAccount3 = util.account.NonConfirmedAccount3;
// admin role binding
const newAccount0 = util.account.unlinkedAccounts.new[0];
describe("GET user account", function() {
// fail on authentication
it("should FAIL to list the user's account on /api/account/self GET due to authentication", function(done) {
chai.request(server.app)
.get("/api/account/self")
.end(function(err, res) {
res.should.have.status(401);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE);
done();
});
});
// fail due to invalid login
it("should FAIL due to invalid password", function(done) {
agent
.post("/api/auth/login")
.type("application/json")
.send({
email: Admin0.email,
password: "FakePassword"
})
.end((err, res) => {
res.should.have.status(401);
res.body.should.have.property("message");
res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE);
done();
});
});
// success case
it("should list the user's account on /api/account/self GET", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
return (
agent
.get("/api/account/self")
// does not have password because of to stripped json
.end(function(err, res) {
if (err) {
return done(err);
}
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.ACCOUNT_READ
);
res.body.should.have.property("data");
res.body.data.should.be.a("object");
res.body.data.should.have.property("firstName");
res.body.data.should.have.property("lastName");
res.body.data.should.have.property("email");
res.body.data.should.have.property(
"dietaryRestrictions"
);
res.body.data.should.have.property("gender");
done();
})
);
});
});
// success case - admin case
it("should list another account specified by id using admin priviledge on /api/account/:id/ GET", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
return (
agent
.get(`/api/account/${teamHackerAccount0._id}`)
// does not have password because of to stripped json
.end(function(err, res) {
if (err) {
return done(err);
}
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.ACCOUNT_READ
);
res.body.should.have.property("data");
// use acc.toStrippedJSON to deal with hidden passwords and convert _id to id
const acc = new Account(teamHackerAccount0);
chai.assert.equal(
JSON.stringify(res.body.data),
JSON.stringify(acc.toStrippedJSON())
);
done();
})
);
});
});
// success case - user case
it("should list an account specified by id on /api/account/:id/ GET", function(done) {
util.auth.login(agent, teamHackerAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return (
agent
.get(`/api/account/${teamHackerAccount0._id}`)
// does not have password because of to stripped json
.end(function(err, res) {
if (err) {
return done(err);
}
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.ACCOUNT_READ
);
res.body.should.have.property("data");
// use acc.toStrippedJSON to deal with hidden passwords and convert _id to id
const acc = new Account(teamHackerAccount0);
chai.assert.equal(
JSON.stringify(res.body.data),
JSON.stringify(acc.toStrippedJSON())
);
done();
})
);
});
});
// // fail case on authorization
it("should FAIL to list an account specified by id on /api/account/:id/ GET due to lack of authorization", function(done) {
util.auth.login(agent, teamHackerAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return (
agent
.get(`/api/account/${Admin0._id}`)
// does not have password because of to stripped json
.end(function(err, res) {
if (err) {
return done(err);
}
res.should.have.status(403);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Error.AUTH_403_MESSAGE
);
res.body.should.have.property("data");
done();
})
);
});
});
});
describe("POST create account", function() {
it("should SUCCEED and create a new account", function(done) {
chai.request(server.app)
.post(`/api/account/`)
.type("application/json")
.send(newAccount0)
.end(function(err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(Constants.Success.ACCOUNT_CREATE);
// use acc.toStrippedJSON to deal with hidden passwords and convert _id to id
const acc = new Account(newAccount0).toStrippedJSON();
// delete id as those are generated
delete acc.id;
delete res.body.data.id;
chai.assert.equal(
JSON.stringify(res.body.data),
JSON.stringify(acc)
);
done();
});
});
it("should FAIL to create an account because the email is already in use", function(done) {
chai.request(server.app)
.post(`/api/account/`)
.type("application/json")
.send(teamHackerAccount0)
.end(function(err, res) {
res.should.have.status(422);
done();
});
});
});
describe("POST confirm account", function() {
it("should SUCCEED and confirm the account", function(done) {
chai.request(server.app)
.post(`/api/auth/confirm/${confirmationToken}`)
.type("application/json")
.end(function(err, res) {
res.should.have.status(200);
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.AUTH_CONFIRM_ACCOUNT
);
done();
});
});
it("should FAIL confirming the account", function(done) {
chai.request(server.app)
.post(`/api/auth/confirm/${fakeToken}`)
.type("application/json")
.end(function(err, res) {
res.should.have.status(401);
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Error.ACCOUNT_TOKEN_401_MESSAGE
);
done();
});
});
it("should FAIL to confirm account that has token with email but no account", function(done) {
chai.request(server.app)
.post(`/api/auth/confirm/${fakeToken}`)
.type("application/json")
.end(function(err, res) {
res.should.have.status(401);
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Error.ACCOUNT_TOKEN_401_MESSAGE
);
done();
});
});
});
describe("PATCH update account", function() {
const updatedInfo = {
_id: teamHackerAccount0._id,
firstName: "new",
lastName: "name"
};
const failUpdatedInfo = {
_id: Admin0._id,
firstName: "fail",
lastName: "fail"
};
// fail on authentication
it("should FAIL to update an account due to authentication", function(done) {
chai.request(server.app)
.patch(`/api/account/${updatedInfo._id}`)
.end(function(err, res) {
res.should.have.status(401);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE);
done();
});
});
// succeed on :all case
it("should SUCCEED and use admin to update another account", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
agent
.patch(`/api/account/${updatedInfo._id}`)
.type("application/json")
.send(updatedInfo)
.end(function(err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.ACCOUNT_UPDATE
);
res.body.should.have.property("data");
// Is this correct matching of data?
res.body.data.firstName.should.equal(updatedInfo.firstName);
res.body.data.lastName.should.equal(updatedInfo.lastName);
done();
});
});
});
// succeed on :self case
it("should SUCCEED and update the user's own account", function(done) {
util.auth.login(agent, teamHackerAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
agent
.patch(`/api/account/${updatedInfo._id}`)
.type("application/json")
.send(updatedInfo)
.end(function(err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.ACCOUNT_UPDATE
);
res.body.should.have.property("data");
// Is this correct matching of data?
res.body.data.firstName.should.equal(updatedInfo.firstName);
res.body.data.lastName.should.equal(updatedInfo.lastName);
done();
});
});
});
// fail due to lack of authorization
it("should FAIL to update an account due to lack of authorization", function(done) {
util.auth.login(agent, teamHackerAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
agent
.patch(`/api/account/${failUpdatedInfo._id}`)
.type("application/json")
.send(updatedInfo)
.end(function(err, res) {
res.should.have.status(403);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Error.AUTH_403_MESSAGE
);
res.body.should.have.property("data");
done();
});
});
});
});
describe("POST reset password", function() {
const password = {
password: "NewPassword"
};
it("should SUCCEED and change the password", function(done) {
chai.request(server.app)
.post("/api/auth/password/reset")
.type("application/json")
.set("X-Reset-Token", resetToken)
.send(password)
.end(function(err, res) {
res.should.have.status(200);
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.AUTH_RESET_PASSWORD
);
done();
});
});
});
describe("PATCH change password for logged in user", function() {
const successChangePassword = {
oldPassword: Admin0.password,
newPassword: "password12345"
};
const failChangePassword = {
oldPassword: "WrongPassword",
newPassword: "password12345"
};
// fail on authentication
it("should FAIL to change the user's password because they are not logged in", function(done) {
chai.request(server.app)
.patch("/api/auth/password/change")
.type("application/json")
.send(failChangePassword)
.end(function(err, res) {
res.should.have.status(401);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE);
done();
});
});
// success case
it("should change the logged in user's password to a new password", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
agent
.patch("/api/auth/password/change")
.type("application/json")
.send(successChangePassword)
.end(function(err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.AUTH_RESET_PASSWORD
);
done();
});
});
});
// fail case because old password in incorrect
it("should FAIL to change the logged in user's password to a new password because old password is incorrect", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
agent
.patch("/api/auth/password/change")
.type("application/json")
.send(failChangePassword)
.end(function(err, res) {
res.should.have.status(401);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Error.AUTH_401_MESSAGE
);
done();
});
});
});
});
describe("GET retrieve permissions", function() {
it("should SUCCEED and retrieve the rolebindings for the user", function(done) {
util.auth.login(agent, teamHackerAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
agent
.get("/api/auth/rolebindings/" + teamHackerAccount0._id)
.type("application/json")
.end(function(err, res) {
res.should.have.status(200);
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.AUTH_GET_ROLE_BINDINGS
);
res.body.should.have.property("data");
res.body.data.should.be.a("object");
res.body.data.should.have.property("roles");
res.body.data.should.have.property("accountId");
res.body.data.accountId.should.equal(
teamHackerAccount0._id.toHexString()
);
done();
});
});
});
it("should FAIL to retrieve the rolebindings as the account is not authenticated", function(done) {
chai.request(server.app)
.get("/api/auth/rolebindings/" + teamHackerAccount0._id)
.type("application/json")
.end(function(err, res) {
res.should.have.status(401);
res.body.should.have.property("message");
res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE);
done();
});
});
});
describe("GET resend confirmation email", function() {
it("should SUCCEED and resend the confirmation email", function(done) {
util.auth.login(agent, storedAccount1, (error) => {
if (error) {
agent.close();
return done(error);
}
agent
.get("/api/auth/confirm/resend")
.type("application/json")
.end(function(err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.AUTH_SEND_CONFIRMATION_EMAIL
);
done();
});
});
});
it("should FAIL as the account is already confirmed", function(done) {
util.auth.login(agent, teamHackerAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
agent
.get("/api/auth/confirm/resend")
.type("application/json")
.end(function(err, res) {
res.should.have.status(422);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal("Account already confirmed");
done();
});
});
});
it("should FAIL as account confirmation token does not exist", function(done) {
util.auth.login(agent, storedAccount3, (error) => {
if (error) {
agent.close();
return done(error);
}
agent
.get("/api/auth/confirm/resend")
.type("application/json")
.end(function(err, res) {
res.should.have.status(428);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
"Account confirmation token does not exist"
);
done();
});
});
});
});
describe("POST invite account", function() {
it("Should succeed to invite a user to create an account", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
return (
agent
.post("/api/account/invite")
.type("application/json")
.send({
email: newAccount0.email,
accountType: Constants.General.VOLUNTEER
})
// does not have password because of to stripped json
.end(function(err, res) {
if (err) {
return done(err);
}
res.should.have.status(200);
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.ACCOUNT_INVITE
);
done();
})
);
});
});
});
describe("GET invites", function() {
it("Should FAIL to get all invites due to Authentication", function(done) {
chai.request(server.app)
.get("/api/account/invite")
.end(function(err, res) {
res.should.have.status(401);
res.body.should.have.property("message");
res.body.message.should.equal(Constants.Error.AUTH_401_MESSAGE);
done();
});
});
it("Should FAIL to get all invites due to Authorization", function(done) {
util.auth.login(agent, teamHackerAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent.get("/api/account/invite").end(function(err, res) {
res.should.have.status(403);
res.body.should.have.property("message");
res.body.message.should.equal(Constants.Error.AUTH_403_MESSAGE);
done();
});
});
});
it("Should SUCCEED to get all invites", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent.get("/api/account/invite").end(function(err, res) {
if (err) {
return done(err);
}
res.should.have.status(200);
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Success.ACCOUNT_GET_INVITES
);
res.body.should.have.property("data");
res.body.data.should.have.property("invites");
res.body.data.invites.length.should.equal(
util.accountConfirmation.AccountConfirmationTokens.length
);
done();
});
});
});
});