Skip to content

Commit 71a522c

Browse files
committed
fix: Fixing tests and mongoose API for updates
1 parent 4fd9fa1 commit 71a522c

File tree

11 files changed

+48
-132
lines changed

11 files changed

+48
-132
lines changed

app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ app.use(
7272
// Cookie Options
7373
maxAge: 48 * 60 * 60 * 1000, //Logged in for 48 hours
7474
sameSite: process.env.COOKIE_SAME_SITE,
75-
secureProxy: true
75+
secureProxy: !Services.env.isTest()
7676
})
7777
);
7878
app.use(passport.initialize());

controllers/auth.controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const Success = require("../constants/success.constant");
55
module.exports = {
66
onSuccessfulLogin: function(req, res) {
77
return res.status(200).json({
8-
message: Success.LOGIN,
8+
message: Success.AUTH_LOGIN,
99
data: {}
1010
});
1111
},
1212
logout: function(req, res) {
1313
req.logout();
1414
return res.status(200).json({
15-
message: Success.LOGOUT,
15+
message: Success.AUTH_LOGOUT,
1616
data: {}
1717
});
1818
},

middlewares/hacker.middleware.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,13 @@ function validateConfirmedStatus(account) {
127127
message: Constants.Error.ACCOUNT_404_MESSAGE,
128128
data: { account: account }
129129
};
130-
}
131-
/*
132-
else if (!account.confirmed) {
130+
} else if (!account.confirmed) {
133131
return {
134132
status: 403,
135133
message: Constants.Error.ACCOUNT_403_MESSAGE,
136134
data: { account: { id: account.id, confirmed: account.confirmed } }
137135
};
138-
}
139-
*/
140-
else if (account.accountType !== Constants.General.HACKER) {
136+
} else if (account.accountType !== Constants.General.HACKER) {
141137
return {
142138
status: 409,
143139
message: Constants.Error.ACCOUNT_TYPE_409_MESSAGE,
@@ -841,7 +837,7 @@ async function checkDuplicateAccountLinks(req, res, next) {
841837
*/
842838
async function findSelf(req, res, next) {
843839
if (
844-
req.user.accountType != Constants.General.HACKER /*|| !req.user.confirmed*/
840+
req.user.accountType != Constants.General.HACKER|| !req.user.confirmed
845841
) {
846842
return next({
847843
status: 409,

services/account.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function updateOne(id, accountDetails) {
116116
_id: id
117117
};
118118

119-
return logger.logUpdate(TAG, "account", Account.findOneAndUpdate(query, accountDetails));
119+
return logger.logUpdate(TAG, "account", Account.findOneAndUpdate(query, accountDetails, { new: true }));
120120
}
121121

122122
/**

services/hacker.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function updateOne(id, hackerDetails) {
4040
_id: id
4141
};
4242

43-
return logger.logUpdate(TAG, "hacker", Hacker.findOneAndUpdate(query, hackerDetails));
43+
return logger.logUpdate(TAG, "hacker", Hacker.findOneAndUpdate(query, hackerDetails, { new: true }));
4444
}
4545

4646
/**

services/resetPassword.service.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,7 @@ async function create(accountId) {
6060
function deleteToken(id) {
6161
const TAG = `[ PasswordReset Service # deleteToken]:`;
6262
//Create new instance of password reset token
63-
return PasswordReset.deleteOne(
64-
{
65-
_id: id
66-
},
67-
(err) => {
68-
if (err) {
69-
logger.erro(`${TAG} could not delete token id: ${id}`);
70-
}
71-
}
72-
);
63+
return logger.logUpdate(TAG, `token id: ${id}`, PasswordReset.deleteOne({ _id: id }));
7364
}
7465

7566
function generateToken(resetId, accountId) {

services/settings.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function updateSettings(settingsDetails) {
1212
const TAG = "[Setting service # updateSettings]:";
1313
const existingSetting = await getSettings();
1414
if (existingSetting) {
15-
return logger.logQuery(TAG, "settings", {}, Settings.findOneAndUpdate({}, settingsDetails));
15+
return logger.logQuery(TAG, "settings", {}, Settings.findOneAndUpdate({}, settingsDetails, { new: true }));
1616
} else {
1717
const setting = new Settings(settingsDetails);
1818
return setting.save();

services/team.service.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function updateOne(id, teamDetails) {
5353
_id: id
5454
};
5555

56-
return logger.logUpdate(TAG, "team", Team.findOneAndUpdate(query, teamDetails));
56+
return logger.logUpdate(TAG, "team", Team.findOneAndUpdate(query, teamDetails, { new: true }));
5757
}
5858

5959
/**
@@ -113,6 +113,9 @@ async function removeMember(teamId, hackerId) {
113113
$pull: {
114114
members: hackerId
115115
}
116+
},
117+
{
118+
new: true
116119
}
117120
);
118121
}

services/travel.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function updateOne(id, travelDetails) {
3333
_id: id
3434
};
3535

36-
return logger.logUpdate(TAG, "travel", Travel.findOneAndUpdate(query, travelDetails));
36+
return logger.logUpdate(TAG, "travel", Travel.findOneAndUpdate(query, travelDetails, { new: true }));
3737
}
3838

3939
/**

tests/hacker.test.js

Lines changed: 28 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ const newHackerAccount0 = util.account.hackerAccounts.new[0];
3333
const newHacker0 = util.hacker.newHacker0;
3434
const invalidHackerAccount0 = util.account.hackerAccounts.invalid;
3535
const invalidHacker0 = util.hacker.invalidHacker0;
36+
const invalidHacker1 = util.hacker.invalidHacker1;
3637
const invalidHacker2 = util.hacker.invalidHacker2;
3738
const invalidHacker3 = util.hacker.invalidHacker3;
38-
const invalidHacker4 = util.hacker.invalidHacker4;
3939
const newHacker1 = util.hacker.newHacker1;
4040

4141
const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0];
@@ -53,7 +53,6 @@ const unconfirmedHackerAccount0 = util.hacker.unconfirmedAccountHacker0;
5353

5454
const unconfirmedHacker1 = util.hacker.unconfirmedAccountHacker1;
5555

56-
const invalidHacker1 = util.hacker.invalidHacker1;
5756

5857
const BatchAcceptHackerArrayValid = [
5958
util.hacker.TeamHacker0._id,
@@ -114,9 +113,9 @@ describe("GET hacker", function() {
114113
res.body.should.have.property("data");
115114

116115
let hacker = new Hacker(TeamHacker0);
117-
chai.assert.equal(
118-
JSON.stringify(res.body.data),
119-
JSON.stringify(hacker.toJSON())
116+
chai.assert.deepStrictEqual(
117+
res.body.data,
118+
JSON.parse(JSON.stringify(hacker)),
120119
);
121120
done();
122121
});
@@ -185,9 +184,9 @@ describe("GET hacker", function() {
185184
res.body.should.have.property("data");
186185

187186
let hacker = new Hacker(TeamHacker0);
188-
chai.assert.equal(
189-
JSON.stringify(res.body.data),
190-
JSON.stringify(hacker.toJSON())
187+
chai.assert.deepStrictEqual(
188+
res.body.data,
189+
JSON.parse(JSON.stringify(hacker)),
191190
);
192191

193192
done();
@@ -221,9 +220,9 @@ describe("GET hacker", function() {
221220

222221
let hacker = new Hacker(TeamHacker0);
223222

224-
chai.assert.equal(
225-
JSON.stringify(res.body.data),
226-
JSON.stringify(hacker.toJSON())
223+
chai.assert.deepStrictEqual(
224+
res.body.data,
225+
JSON.parse(JSON.stringify(hacker)),
227226
);
228227

229228
done();
@@ -311,9 +310,9 @@ describe("GET hacker", function() {
311310
res.body.should.have.property("data");
312311

313312
let hacker = new Hacker(TeamHacker0);
314-
chai.assert.equal(
315-
JSON.stringify(res.body.data),
316-
JSON.stringify(hacker.toJSON())
313+
chai.assert.deepStrictEqual(
314+
res.body.data,
315+
JSON.parse(JSON.stringify(hacker))
317316
);
318317

319318
done();
@@ -347,9 +346,9 @@ describe("GET hacker", function() {
347346

348347
let hacker = new Hacker(TeamHacker0);
349348

350-
chai.assert.equal(
351-
JSON.stringify(res.body.data),
352-
JSON.stringify(hacker.toJSON())
349+
chai.assert.deepStrictEqual(
350+
res.body.data,
351+
JSON.parse(JSON.stringify(hacker))
353352
);
354353

355354
done();
@@ -450,9 +449,9 @@ describe("POST create hacker", function() {
450449
hacker.status = Constants.General.HACKER_STATUS_APPLIED;
451450
delete res.body.data.id;
452451
delete hacker.id;
453-
chai.assert.equal(
454-
JSON.stringify(res.body.data),
455-
JSON.stringify(hacker),
452+
chai.assert.deepStrictEqual(
453+
res.body.data,
454+
JSON.parse(JSON.stringify(hacker)),
456455
"objects do not match"
457456
);
458457

@@ -488,9 +487,9 @@ describe("POST create hacker", function() {
488487
hacker.status = Constants.General.HACKER_STATUS_APPLIED;
489488
delete res.body.data.id;
490489
delete hacker.id;
491-
chai.assert.equal(
492-
JSON.stringify(res.body.data),
493-
JSON.stringify(hacker)
490+
chai.assert.deepStrictEqual(
491+
res.body.data,
492+
JSON.parse(JSON.stringify(hacker)),
494493
);
495494
done();
496495
});
@@ -547,39 +546,6 @@ describe("POST create hacker", function() {
547546
);
548547
});
549548

550-
// should fail due to travel request larger than 100
551-
it("should FAIL if the new hacker inputs a value larger than 100 for travel reimbursement", function(done) {
552-
util.auth.login(agent, newHackerAccount0, (error) => {
553-
if (error) {
554-
agent.close();
555-
return done(error);
556-
}
557-
return agent
558-
.post(`/api/hacker/`)
559-
.type("application/json")
560-
.send(invalidHacker2)
561-
.end(function(err, res) {
562-
res.should.have.status(422);
563-
res.should.be.json;
564-
res.body.should.have.property("message");
565-
res.body.message.should.equal("Validation failed");
566-
res.body.should.have.property("data");
567-
res.body.data.should.have.property(
568-
"application.accommodation.travel"
569-
);
570-
res.body.data[
571-
"application.accommodation.travel"
572-
].should.have.property("msg");
573-
res.body.data[
574-
"application.accommodation.travel"
575-
].msg.should.equal(
576-
"application.accommodation.travel must be between 0 and 100"
577-
);
578-
done();
579-
});
580-
});
581-
});
582-
583549
// should fail due to 'false' on code of conduct
584550
it("should FAIL if the new hacker does not accept code of conduct", function(done) {
585551
util.auth.login(agent, newHackerAccount0, (error) => {
@@ -624,7 +590,7 @@ describe("POST create hacker", function() {
624590
return agent
625591
.post(`/api/hacker/`)
626592
.type("application/json")
627-
.send(invalidHacker3)
593+
.send(invalidHacker2)
628594
.end(function(err, res) {
629595
res.should.have.status(422);
630596
res.should.be.json;
@@ -652,7 +618,7 @@ describe("POST create hacker", function() {
652618
return agent
653619
.post(`/api/hacker/`)
654620
.type("application/json")
655-
.send(invalidHacker4)
621+
.send(invalidHacker3)
656622
.end(function(err, res) {
657623
res.should.have.status(422);
658624
res.should.be.json;
@@ -759,15 +725,15 @@ describe("POST create hacker", function() {
759725
"application.general.jobInterest"
760726
].msg.should.equal("The value must be part of the enum");
761727
res.body.data.should.have.property(
762-
"application.accommodation.travel"
728+
"application.accommodation.travel.amount"
763729
);
764730
res.body.data[
765-
"application.accommodation.travel"
731+
"application.accommodation.travel.amount"
766732
].should.have.property("msg");
767733
res.body.data[
768-
"application.accommodation.travel"
734+
"application.accommodation.travel.amount"
769735
].msg.should.equal(
770-
"application.accommodation.travel must be an integer."
736+
"application.accommodation.travel.amount must be an integer."
771737
);
772738
res.body.data.should.have.property(
773739
"application.accommodation.attendancePreference"

0 commit comments

Comments
 (0)