-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsponsor.test.js
More file actions
467 lines (428 loc) Β· 16.7 KB
/
sponsor.test.js
File metadata and controls
467 lines (428 loc) Β· 16.7 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
"use strict";
const chai = require("chai");
const chaiHttp = require("chai-http");
chai.use(chaiHttp);
const server = require("../app");
const agent = chai.request.agent(server.app);
const Sponsor = require("../models/sponsor.model");
const should = chai.should();
const mongoose = require("mongoose");
const Constants = {
Success: require("../constants/success.constant"),
Error: require("../constants/error.constant")
};
const util = {
sponsor: require("./util/sponsor.test.util"),
auth: require("./util/auth.test.util"),
account: require("./util/account.test.util")
};
const Admin0 = util.account.adminAccounts.stored[0];
const HackerAccount0 = util.account.hackerAccounts.stored.team[0];
const T1SponsorAccount0 = util.account.sponsorT1Accounts.stored[0];
const newT2SponsorAccount0 = util.account.sponsorT2Accounts.new[0];
const T1Sponsor0 = util.sponsor.T1Sponsor0;
const newT2Sponsor0 = util.sponsor.newT2Sponsor0;
let duplicateSponsor = util.sponsor.duplicateAccountLinkSponsor1;
describe("GET user's sponsor info", function() {
it("should FAIL list a sponsor's information due to authentication from /api/sponsor/self GET", function(done) {
chai.request(server.app)
.get(`/api/sponsor/self`)
// does not have password because of to stripped json
.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();
});
});
it("should FAIL to list a sponsor's info due to authorization /api/sponsor/self GET", function(done) {
util.auth.login(agent, HackerAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent.get(`/api/sponsor/self`).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();
});
});
});
it("should FAIL to list a sponsor's info due to wrong account type on /api/sponsor/self GET", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent.get(`/api/sponsor/self`).end(function(err, res) {
if (err) {
return done(err);
}
res.should.have.status(409);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Error.ACCOUNT_TYPE_409_MESSAGE
);
res.body.should.have.property("data");
done();
});
});
});
it("should FAIL to list a sponsor's info due to lack of sponsor on /api/sponsor/self GET", function(done) {
util.auth.login(agent, newT2SponsorAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent.get(`/api/sponsor/self`).end(function(err, res) {
if (err) {
return done(err);
}
res.should.have.status(404);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Error.SPONSOR_404_MESSAGE
);
res.body.should.have.property("data");
done();
});
});
});
it("should SUCCEED to list user's sponsor info /api/sponsor/self GET", function(done) {
util.auth.login(agent, T1SponsorAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent.get(`/api/sponsor/self`).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.SPONSOR_READ);
res.body.should.have.property("data");
res.body.data.should.be.a("object");
let sponsor = new Sponsor(T1Sponsor0);
chai.assert.equal(
JSON.stringify(res.body.data),
JSON.stringify(sponsor.toJSON())
);
done();
});
});
});
});
describe("GET sponsor by id", function() {
it("should FAIL list a sponsor's information due to authentication from /api/sponsor/:id GET", function(done) {
chai.request(server.app)
.get(`/api/sponsor/` + T1Sponsor0._id)
// does not have password because of to stripped json
.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();
});
});
// admin success
it("should succeed to list a sponsor's info using admin power on /api/sponsor/:id GET", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent
.get(`/api/sponsor/${T1Sponsor0._id}`)
.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.SPONSOR_READ
);
res.body.should.have.property("data");
res.body.data.should.be.a("object");
let sponsor = new Sponsor(T1Sponsor0);
chai.assert.equal(
JSON.stringify(res.body.data),
JSON.stringify(sponsor.toJSON())
);
done();
});
});
});
// regular user access success
it("should succeed to list a user's sponsor info on /api/sponsor/:id GET", function(done) {
util.auth.login(agent, T1SponsorAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent
.get(`/api/sponsor/${util.sponsor.T1Sponsor0._id}`)
.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.SPONSOR_READ
);
res.body.should.have.property("data");
res.body.data.should.be.a("object");
let sponsor = new Sponsor(T1Sponsor0);
chai.assert.equal(
JSON.stringify(res.body.data),
JSON.stringify(sponsor.toJSON())
);
done();
});
});
});
// failure due to lack of auth
it("should FAIL to list a user's sponsor info due to lack of authorization /api/sponsor/:id GET", function(done) {
util.auth.login(agent, HackerAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent
.get(`/api/sponsor/${util.sponsor.T1Sponsor0._id}`)
.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();
});
});
});
// failure due to lack of this sponsor
it("should FAIL to list non existant info on /api/sponsor/:id GET", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent
.get(`/api/sponsor/${mongoose.Types.ObjectId()}`)
.end(function(err, res) {
if (err) {
return done(err);
}
res.should.have.status(404);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Error.SPONSOR_404_MESSAGE
);
res.body.should.have.property("data");
done();
});
});
});
});
describe("POST create sponsor", function() {
it("should FAIL to create a new sponsor due to lack of authentication", function(done) {
chai.request(server.app)
.post(`/api/sponsor`)
.type("application/json")
.send(util.sponsor.newSponsor)
.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 with self caes - there is no admin case
it("should SUCCEED and create a new sponsor", function(done) {
util.auth.login(agent, newT2SponsorAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent
.post(`/api/sponsor/`)
.type("application/json")
.send(newT2Sponsor0)
.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.SPONSOR_CREATE
);
res.body.should.have.property("data");
// deleting id because that was generated, and not part of original data
const sponsor = new Sponsor(newT2Sponsor0).toJSON();
delete res.body.data.id;
delete sponsor.id;
chai.assert.equal(
JSON.stringify(res.body.data),
JSON.stringify(sponsor)
);
done();
});
});
});
// fail case - duplicate accountId
it("should FAIL to create a sponsor due to duplicate accountId", function(done) {
util.auth.login(agent, T1SponsorAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent
.post(`/api/sponsor/`)
.type("application/json")
.send(duplicateSponsor)
.end(function(err, res) {
res.should.have.status(409);
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Error.SPONSOR_ID_409_MESSAGE
);
res.body.should.have.property("data");
done();
});
});
});
// unauthorized case
it("should FAIL to create a new sponsor", function(done) {
util.auth.login(agent, HackerAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent
.post(`/api/sponsor/`)
.type("application/json")
.send(newT2Sponsor0)
.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("PATCH update sponsor", function() {
it("should FAIL to update a sponsor due to lack of authentication", function(done) {
chai.request(server.app)
.patch(`/api/sponsor/${T1Sponsor0._id}/`)
.type("application/json")
.send({
company: "NewCompanyName"
})
.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();
});
});
it("should FAIL to update a sponsor due to authorization", function(done) {
util.auth.login(agent, newT2SponsorAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent
.patch(`/api/sponsor/${T1Sponsor0._id}/`)
.type("application/json")
.send({
company: "NewCompanyName"
})
.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
);
done();
});
});
});
it("should FAIL to update a sponsor due wrong id", function(done) {
util.auth.login(agent, Admin0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent
.patch(`/api/sponsor/${Admin0._id}/`)
.type("application/json")
.send({
company: "NewCompanyName"
})
.end(function(err, res) {
res.should.have.status(500);
res.should.be.json;
res.body.should.have.property("message");
res.body.message.should.equal(
Constants.Error.SPONSOR_UPDATE_500_MESSAGE
);
done();
});
});
});
// success case with self caes - there is no admin case
it("should SUCCEED and update a sponsor", function(done) {
util.auth.login(agent, T1SponsorAccount0, (error) => {
if (error) {
agent.close();
return done(error);
}
return agent
.patch(`/api/sponsor/${T1Sponsor0._id}/`)
.type("application/json")
.send({
company: "NewCompanyName"
})
.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.SPONSOR_UPDATE
);
res.body.should.have.property("data");
res.body.data.should.have.property("company");
res.body.data.company.should.equal("NewCompanyName");
done();
});
});
});
});