diff --git a/constants/general.constant.js b/constants/general.constant.js index 913a1a5b..77af8a5e 100644 --- a/constants/general.constant.js +++ b/constants/general.constant.js @@ -24,6 +24,23 @@ const HACKER_STATUSES = [ HACKER_STATUS_CHECKED_IN ]; +const SAMPLE_DIET_RESTRICTIONS = [ + "None", + "Vegan", + "Vegetarian", + "Keto", + "Gluten free", + "Pescetarian", + "Peanut allergy", + "Milk allergy", + "Egg allergy", + "Allergy", + "No beef", + "No porc", + "No fish", + "No shellfish" +]; + const HACKER = "Hacker"; const VOLUNTEER = "Volunteer"; const STAFF = "Staff"; @@ -123,5 +140,6 @@ module.exports = { POST_ROLES: POST_ROLES, CACHE_TIMEOUT_STATS: CACHE_TIMEOUT_STATS, CACHE_KEY_STATS: CACHE_KEY_STATS, - MAX_TEAM_SIZE: MAX_TEAM_SIZE + MAX_TEAM_SIZE: MAX_TEAM_SIZE, + SAMPLE_DIET_RESTRICTIONS: SAMPLE_DIET_RESTRICTIONS, }; \ No newline at end of file diff --git a/tests/account.test.js b/tests/account.test.js index 687265b6..f718405f 100644 --- a/tests/account.test.js +++ b/tests/account.test.js @@ -19,19 +19,26 @@ const util = { accountConfirmation: require("./util/accountConfirmation.test.util"), reset: require("./util/resetPassword.test.util") }; -// hacker role binding -const storedAccount1 = util.account.Account1; -//This account has a confirmation token in the db -const storedAccount2 = util.account.NonConfirmedAccount1; -//This account does not have a confirmation token in the DB -const storedAccount3 = util.account.NonConfirmedAccount2; -// admin role binding -const Admin1 = util.account.Admin1; -const newAccount1 = util.account.newAccount1; 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.staffAccounts.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 @@ -50,7 +57,7 @@ describe("GET user account", function () { // fail due to invalid login it("should fail due to invalid password", function (done) { agent.post("/api/auth/login").type("application/json").send({ - email: Admin1.email, + email: Admin0.email, password: "FakePassword" }).end((err, res) => { res.should.have.status(401); @@ -62,7 +69,7 @@ describe("GET user account", function () { // success case it("should list the user's account on /api/account/self GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -92,13 +99,13 @@ describe("GET user account", function () { // 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, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/account/${storedAccount1._id}`) + .get(`/api/account/${teamHackerAccount0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -111,7 +118,7 @@ describe("GET user account", function () { res.body.should.have.property("data"); // use acc.toStrippedJSON to deal with hidden passwords and convert _id to id - const acc = new Account(storedAccount1); + const acc = new Account(teamHackerAccount0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(acc.toStrippedJSON())); done(); }); @@ -119,13 +126,13 @@ describe("GET user account", function () { }); // success case - user case it("should list an account specified by id on /api/account/:id/ GET", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/account/${storedAccount1._id}`) + .get(`/api/account/${teamHackerAccount0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -138,7 +145,7 @@ describe("GET user account", function () { res.body.should.have.property("data"); // use acc.toStrippedJSON to deal with hidden passwords and convert _id to id - const acc = new Account(storedAccount1); + const acc = new Account(teamHackerAccount0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(acc.toStrippedJSON())); done(); }); @@ -147,13 +154,13 @@ describe("GET user account", function () { // // 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, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/account/${Admin1._id}`) + .get(`/api/account/${Admin0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -176,7 +183,7 @@ describe("POST create account", function () { chai.request(server.app) .post(`/api/account/`) .type("application/json") - .send(newAccount1) + .send(newAccount0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -184,7 +191,7 @@ describe("POST create account", function () { 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(newAccount1)).toStrippedJSON(); + const acc = (new Account(newAccount0)).toStrippedJSON(); // delete id as those are generated delete acc.id; delete res.body.data.id; @@ -197,7 +204,7 @@ describe("POST create account", function () { chai.request(server.app) .post(`/api/account/`) .type("application/json") - .send(storedAccount1) + .send(teamHackerAccount0) .end(function (err, res) { res.should.have.status(422); done(); @@ -243,13 +250,13 @@ describe("POST confirm account", function () { describe("PATCH update account", function () { const updatedInfo = { - "_id": storedAccount1._id, + "_id": teamHackerAccount0._id, "firstName": "new", "lastName": "name" }; const failUpdatedInfo = { - "_id": Admin1._id, + "_id": Admin0._id, "firstName": "fail", "lastName": "fail" }; @@ -269,7 +276,7 @@ describe("PATCH update account", function () { // succeed on :all case it("should SUCCEED and use admin to update another account", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -294,7 +301,7 @@ describe("PATCH update account", function () { // succeed on :self case it("should SUCCEED and update the user's own account", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -319,7 +326,7 @@ describe("PATCH update account", function () { // fail due to lack of authorization it("should Fail to update an account due to lack of authorization", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -362,7 +369,7 @@ describe("POST reset password", function () { describe("PATCH change password for logged in user", function () { const successChangePassword = { - "oldPassword": Admin1.password, + "oldPassword": Admin0.password, "newPassword": "password12345" }; const failChangePassword = { @@ -385,7 +392,7 @@ describe("PATCH change password for logged in user", function () { }); // success case it("should change the logged in user's password to a new password", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -405,7 +412,7 @@ describe("PATCH change password for logged in user", function () { }); // 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, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -427,13 +434,13 @@ describe("PATCH change password for logged in user", function () { describe("GET retrieve permissions", function () { it("should SUCCEED and retrieve the rolebindings for the user", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } agent - .get("/api/auth/rolebindings/" + storedAccount1._id) + .get("/api/auth/rolebindings/" + teamHackerAccount0._id) .type("application/json") .end(function (err, res) { res.should.have.status(200); @@ -443,14 +450,14 @@ describe("GET retrieve permissions", function () { 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(storedAccount1._id.toHexString()); + 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/" + storedAccount1._id) + .get("/api/auth/rolebindings/" + teamHackerAccount0._id) .type("application/json") .end(function (err, res) { res.should.have.status(401); @@ -463,7 +470,7 @@ describe("GET retrieve permissions", function () { describe("GET resend confirmation email", function () { it("should SUCCEED and resend the confirmation email", function (done) { - util.auth.login(agent, storedAccount3, (error) => { + util.auth.login(agent, storedAccount1, (error) => { if (error) { agent.close(); return done(error); @@ -481,7 +488,7 @@ describe("GET resend confirmation email", function () { }); }); it("should FAIL as the account is already confirmed", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -499,7 +506,7 @@ describe("GET resend confirmation email", function () { }); }); it("should FAIL as account confirmation token does not exist", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, storedAccount3, (error) => { if (error) { agent.close(); return done(error); @@ -520,7 +527,7 @@ describe("GET resend confirmation email", function () { describe("POST invite account", function () { it("Should succeed to invite a user to create an account", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -529,7 +536,7 @@ describe("POST invite account", function () { .post("/api/account/invite") .type("application/json") .send({ - email: newAccount1.email, + email: newAccount0.email, accountType: Constants.General.VOLUNTEER }) // does not have password because of to stripped json @@ -558,7 +565,7 @@ describe("GET invites", function () { }); }); it("Should FAIL to get all invites due to Authorization", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -574,7 +581,7 @@ describe("GET invites", function () { }); }); it("Should SUCCEED to get all invites", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/auth.test.js b/tests/auth.test.js index d0a428bc..78b64ce0 100644 --- a/tests/auth.test.js +++ b/tests/auth.test.js @@ -24,11 +24,11 @@ const constants = { const roles = require("../constants/role.constant"); // hacker role binding -const storedAccount1 = util.account.Account1; +const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; describe("GET roles", function () { it("should list all roles GET", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/hacker.test.js b/tests/hacker.test.js index ec8c347a..01e9d9c6 100644 --- a/tests/hacker.test.js +++ b/tests/hacker.test.js @@ -22,34 +22,30 @@ const util = { }; const StorageService = require("../services/storage.service"); -const Admin1 = util.account.Admin1; -const Volunteer1 = util.account.Account4; +const Admin0 = util.account.staffAccounts.stored[0]; -// storedAccount1 and storedHacker1 are linked together, and have hacker priviledges -// newHackerDuplicateAccountLink1 is also linked with Account1 -// storedHacker1 has status confirmed -const storedAccount1 = util.account.Account1; -const storedHacker1 = util.hacker.HackerA; -const newHackerDuplicateAccountLink1 = util.hacker.duplicateAccountLinkHacker1; +const volunteerAccount0 = util.account.volunteerAccounts.stored[0]; -// storedAccount2 and storedHacker2 are linked together, and have hacker priviledges -const storedAccount2 = util.account.Account2; -const storedHacker2 = util.hacker.HackerB; +const newHackerAccount0 = util.account.hackerAccounts.new[0]; +const newHacker0 = util.hacker.newHacker0; +const invalidHacker0 = util.hacker.invalidHacker0; const newHacker1 = util.hacker.newHacker1; -// badConductHacker1 is the same as newHacker1, even linking to the same account -// the difference is that badConductHacker1 does not accept the code of conducts -const badConductHacker1 = util.hacker.badCodeOfConductHacker1; -const newHackerAccount1 = util.account.allAccounts[13]; -const newHacker2 = util.hacker.newHacker2; +const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0]; +const noTeamHacker0 = util.hacker.NoTeamHacker0; + +const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; +const TeamHacker0 = util.hacker.TeamHacker0; +const duplicateAccountLinkHacker0 = util.hacker.duplicateAccountLinkHacker0; + const invalidHacker1 = util.hacker.invalidHacker1; describe("GET hacker", function () { // fail on authentication it("should fail to list a hacker's information on /api/hacker/:id GET due to authentication", function (done) { chai.request(server.app) - .get(`/api/hacker/` + storedHacker1._id) + .get(`/api/hacker/` + TeamHacker0._id) .end(function (err, res) { res.should.have.status(401); res.should.be.json; @@ -61,7 +57,7 @@ describe("GET hacker", function () { // success case it("should list the user's hacker info on /api/hacker/self GET", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -78,7 +74,7 @@ describe("GET hacker", function () { res.body.message.should.equal(Constants.Success.HACKER_READ); res.body.should.have.property("data"); - let hacker = new Hacker(storedHacker1); + let hacker = new Hacker(TeamHacker0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); done(); }); @@ -87,7 +83,7 @@ describe("GET hacker", function () { // fail case due to wrong account type it("should fail to list the hacker info of an admin due to wrong account type /api/account/self GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -106,13 +102,13 @@ describe("GET hacker", function () { // succeed on admin case it("should list a hacker's information using admin power on /api/hacker/:id GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/hacker/${storedHacker1._id}`) + .get(`/api/hacker/${TeamHacker0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -124,7 +120,7 @@ describe("GET hacker", function () { res.body.message.should.equal(Constants.Success.HACKER_READ); res.body.should.have.property("data"); - let hacker = new Hacker(storedHacker1); + let hacker = new Hacker(TeamHacker0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); done(); @@ -134,13 +130,13 @@ describe("GET hacker", function () { // succeed on :self case it("should list the user's hacker information on /api/hacker/:id GET", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/hacker/${storedHacker1._id}`) + .get(`/api/hacker/${TeamHacker0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -152,7 +148,7 @@ describe("GET hacker", function () { res.body.message.should.equal(Constants.Success.HACKER_READ); res.body.should.have.property("data"); - let hacker = new Hacker(storedHacker1); + let hacker = new Hacker(TeamHacker0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); @@ -163,13 +159,13 @@ describe("GET hacker", function () { // fail due to lack of authorization it("should fail to list a hacker information due to lack of authorization on /api/hacker/:id GET", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/hacker/${storedHacker1._id}`) + .get(`/api/hacker/${TeamHacker0._id}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -188,7 +184,7 @@ describe("GET hacker", function () { // fail due to lack of hacker it("should fail to list an invalid hacker /api/hacker/:id GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -212,13 +208,13 @@ describe("GET hacker", function () { // succeed on admin case it("should list a hacker's information using admin power on /api/hacker/email/:email GET", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/hacker/email/${storedAccount1.email}`) + .get(`/api/hacker/email/${teamHackerAccount0.email}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -230,7 +226,7 @@ describe("GET hacker", function () { res.body.message.should.equal(Constants.Success.HACKER_READ); res.body.should.have.property("data"); - let hacker = new Hacker(storedHacker1); + let hacker = new Hacker(TeamHacker0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); done(); @@ -240,13 +236,13 @@ describe("GET hacker", function () { // succeed on :self case it("should list the user's hacker information on /api/hacker/email/:email GET", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/hacker/email/${storedAccount1.email}`) + .get(`/api/hacker/email/${teamHackerAccount0.email}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -258,7 +254,7 @@ describe("GET hacker", function () { res.body.message.should.equal(Constants.Success.HACKER_READ); res.body.should.have.property("data"); - let hacker = new Hacker(storedHacker1); + let hacker = new Hacker(TeamHacker0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(hacker.toJSON())); @@ -269,13 +265,13 @@ describe("GET hacker", function () { // fail due to lack of authorization it("should fail to list a hacker information due to lack of authorization on /api/hacker/email/:id GET", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/hacker/email/${storedAccount1.email}`) + .get(`/api/hacker/email/${teamHackerAccount0.email}`) // does not have password because of to stripped json .end(function (err, res) { if (err) { @@ -313,7 +309,7 @@ describe("POST create hacker", function () { // succeed on admin case it("should SUCCEED and create a new hacker (with an account that has been confirmed) using admin credentials", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -321,7 +317,7 @@ describe("POST create hacker", function () { return agent .post(`/api/hacker/`) .type("application/json") - .send(newHacker1) + .send(newHacker0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -332,7 +328,7 @@ describe("POST create hacker", function () { // create JSON version of model // delete id as they will be different between model objects // update status to be applied on the comparator hacker object - const hacker = (new Hacker(newHacker1)).toJSON(); + const hacker = (new Hacker(newHacker0)).toJSON(); hacker.status = Constants.General.HACKER_STATUS_APPLIED; delete res.body.data.id; delete hacker.id; @@ -345,7 +341,7 @@ describe("POST create hacker", function () { // succeed on user case it("should SUCCEED and create a new hacker for user (with an account that has been confirmed)", function (done) { - util.auth.login(agent, newHackerAccount1, (error) => { + util.auth.login(agent, newHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -353,7 +349,7 @@ describe("POST create hacker", function () { return agent .post(`/api/hacker/`) .type("application/json") - .send(newHacker1) + .send(newHacker0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -364,7 +360,7 @@ describe("POST create hacker", function () { // create JSON version of model // delete id as they will be different between model objects // update status to be applied on the comparator hacker object - const hacker = (new Hacker(newHacker1)).toJSON(); + const hacker = (new Hacker(newHacker0)).toJSON(); hacker.status = Constants.General.HACKER_STATUS_APPLIED; delete res.body.data.id; delete hacker.id; @@ -376,7 +372,7 @@ describe("POST create hacker", function () { // should fail due to 'false' on code of conduct it("should FAIL if the new hacker does not accept code of conduct", function (done) { - util.auth.login(agent, newHackerAccount1, (error) => { + util.auth.login(agent, newHacker0, (error) => { if (error) { agent.close(); return done(error); @@ -384,7 +380,7 @@ describe("POST create hacker", function () { return agent .post(`/api/hacker/`) .type("application/json") - .send(badConductHacker1) + .send(invalidHacker0) .end(function (err, res) { res.should.have.status(422); res.should.be.json; @@ -400,7 +396,7 @@ describe("POST create hacker", function () { // fail on unconfirmed account, using admin it("should FAIL to create a new hacker if the account hasn't been confirmed", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -408,7 +404,7 @@ describe("POST create hacker", function () { return agent .post(`/api/hacker/`) .type("application/json") - .send(newHacker2) + .send(util.hacker.unconfirmedAccountHacker0) .end(function (err, res) { res.should.be.json; res.body.should.have.property("message"); @@ -421,7 +417,7 @@ describe("POST create hacker", function () { // fail due to duplicate accountId it("should FAIL to create new hacker due to duplicate account link", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -429,7 +425,7 @@ describe("POST create hacker", function () { return agent .post(`/api/hacker/`) .type("application/json") - .send(newHackerDuplicateAccountLink1) + .send(duplicateAccountLinkHacker0) .end(function (err, res) { res.should.have.status(409); res.body.should.have.property("message"); @@ -442,7 +438,7 @@ describe("POST create hacker", function () { // fail on invalid input it("should FAIL to create new hacker due to invalid input", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -464,7 +460,7 @@ describe("PATCH update one hacker", function () { // fail on authentication it("should fail to update a hacker on /api/hacker/:id GET due to authentication", function (done) { chai.request(server.app) - .patch(`/api/hacker/${storedHacker1._id}`) + .patch(`/api/hacker/${TeamHacker0._id}`) .type("application/json") .send({ gender: "Other" @@ -480,13 +476,13 @@ describe("PATCH update one hacker", function () { // should succeed on admin case it("should SUCCEED and update a hacker using admin power", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/${storedHacker1._id}`) + .patch(`/api/hacker/${TeamHacker0._id}`) .type("application/json") .send({ gender: "Other" @@ -506,13 +502,13 @@ describe("PATCH update one hacker", function () { }); it("should SUCCEED and update a hacker STATUS as an Admin", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/status/${storedHacker1._id}`) + .patch(`/api/hacker/status/${TeamHacker0._id}`) .type("application/json") .send({ status: "Accepted" @@ -532,13 +528,13 @@ describe("PATCH update one hacker", function () { }); it("should FAIL and NOT update a hacker STATUS as a Hacker", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/status/${storedHacker1._id}`) + .patch(`/api/hacker/status/${TeamHacker0._id}`) .type("application/json") .send({ status: "Accepted" @@ -556,13 +552,13 @@ describe("PATCH update one hacker", function () { // volunteer should successfully checkin hacker it("should SUCCEED and check in hacker as a volunteer", function (done) { - util.auth.login(agent, Volunteer1, (error) => { + util.auth.login(agent, volunteerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/checkin/${storedHacker1._id}`) + .patch(`/api/hacker/checkin/${TeamHacker0._id}`) .type("application/json") .send({ status: "Checked-in" @@ -583,13 +579,13 @@ describe("PATCH update one hacker", function () { // hacker should fail to checkin hacker it("should FAIL to check in hacker as a hacker", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/checkin/${storedHacker1._id}`) + .patch(`/api/hacker/checkin/${TeamHacker0._id}`) .type("application/json") .send({ status: "Checked-in" @@ -607,13 +603,13 @@ describe("PATCH update one hacker", function () { // should succeed on hacker case it("should SUCCEED and update the user's hacker info", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/${storedHacker2._id}`) + .patch(`/api/hacker/${noTeamHacker0._id}`) .type("application/json") .send({ gender: "Other" @@ -634,13 +630,13 @@ describe("PATCH update one hacker", function () { // should fail due to authorization it("should Fail to update hacker info due to lack of authorization", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/${storedHacker1._id}`) + .patch(`/api/hacker/${TeamHacker0._id}`) .type("application/json") .send({ gender: "Other" @@ -659,7 +655,7 @@ describe("PATCH update one hacker", function () { // fail due to lack of hacker it("should fail to change an invalid hacker's info", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -683,13 +679,13 @@ describe("PATCH update one hacker", function () { // Succeed and change accepted to confirm it("should succeed for hacker to update their own status from accepted to confirmed", function (done) { - util.auth.login(agent, storedAccount2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/confirmation/${storedHacker2._id}`) + .patch(`/api/hacker/confirmation/${noTeamHacker0._id}`) .type("application/json") .send({ confirm: true @@ -714,13 +710,13 @@ describe("PATCH update one hacker", function () { // Succeed and change confirmed to accepted it("should succeed for hacker to update their own status from confirmed to accepted", function (done) { - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/confirmation/${storedHacker1._id}`) + .patch(`/api/hacker/confirmation/${TeamHacker0._id}`) .type("application/json") .send({ confirm: false @@ -745,13 +741,13 @@ describe("PATCH update one hacker", function () { // fail for a hacker that's not accepted it("should fail to update hacker status when hacker status is not accepted or confirmed", function (done) { - util.auth.login(agent, util.account.Hacker3, (error) => { + util.auth.login(agent, util.account.waitlistedHacker0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/confirmation/${util.hacker.HackerC._id}`) + .patch(`/api/hacker/confirmation/${util.hacker.waitlistedHacker0._id}`) .type("application/json") .send({ confirm: true @@ -773,13 +769,13 @@ describe("PATCH update one hacker", function () { // fail for a hacker that's not accepted it("should fail for hacker trying to confirm someone else", function (done) { - util.auth.login(agent, util.account.Hacker3, (error) => { + util.auth.login(agent, util.account.waitlistedHacker0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/hacker/confirmation/${storedHacker1._id}`) + .patch(`/api/hacker/confirmation/${noTeamHacker0._id}`) .type("application/json") .send({ confirm: true @@ -802,12 +798,12 @@ describe("PATCH update one hacker", function () { describe("POST add a hacker resume", function () { it("It should SUCCEED and upload a resume for a hacker", function (done) { //this takes a lot of time for some reason - util.auth.login(agent, storedHacker1, (error) => { + util.auth.login(agent, noTeamHacker0, (error) => { if (error) { return done(error); } return agent - .post(`/api/hacker/resume/${storedHacker1._id}`) + .post(`/api/hacker/resume/${noTeamHacker0._id}`) .type("multipart/form-data") .attach("resume", fs.createReadStream(path.join(__dirname, "testResume.pdf")), { contentType: "application/pdf" @@ -834,7 +830,7 @@ describe("POST add a hacker resume", function () { describe("GET Hacker stats", function () { it("It should FAIL and get hacker stats (invalid validation)", function (done) { //this takes a lot of time for some reason - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { return done(error); } @@ -850,7 +846,7 @@ describe("GET Hacker stats", function () { }); it("It should SUCCEED and get hacker stats", function (done) { //this takes a lot of time for some reason - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { return done(error); } @@ -886,7 +882,7 @@ describe("GET Hacker stats", function () { }); it("It should FAIL and get hacker stats due to invalid Authorization", function (done) { //this takes a lot of time for some reason - util.auth.login(agent, storedAccount1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { return done(error); } diff --git a/tests/role.test.js b/tests/role.test.js index dc811e59..b5444098 100644 --- a/tests/role.test.js +++ b/tests/role.test.js @@ -18,6 +18,9 @@ const Constants = { Success: require("../constants/success.constant"), }; +const Admin0 = util.account.staffAccounts.stored[0]; +const Hacker0 = util.account.hackerAccounts.stored.team[0]; + describe("POST create role", function () { it("should Fail to create a role because staff is not logged in", function (done) { chai.request(server.app) @@ -36,7 +39,7 @@ describe("POST create role", function () { // should succeed on logged in admin it("should SUCCEED and add new role", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -74,7 +77,7 @@ describe("POST create role", function () { // should fail due to lack of authorization it("should Fail to add new role due to lack of authorization", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, Hacker0, (error) => { if (error) { agent.close(); return done(error); @@ -95,7 +98,7 @@ describe("POST create role", function () { // should succeed despite duplicate routes it("should Suceed to add new role despite to duplicate routes", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/search.service.spec.js b/tests/search.service.spec.js index d14785fd..78bf1d59 100644 --- a/tests/search.service.spec.js +++ b/tests/search.service.spec.js @@ -38,8 +38,9 @@ const badQuery = [{ value: "passowrd" }]; -const Admin1 = util.account.Admin1; -const HackerA = util.account.Account2; +const Admin0 = util.account.staffAccounts.stored[0]; + +const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0]; describe("Searching for hackers", function () { it("Should FAIL to search due to invalid authentication", function (done) { @@ -67,7 +68,7 @@ describe("Searching for hackers", function () { }); it("Should FAIL to search due to invalid authorization", function (done) { - util.auth.login(agent, HackerA, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -87,7 +88,7 @@ describe("Searching for hackers", function () { }); }); it("Should return all female hackers", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -107,7 +108,7 @@ describe("Searching for hackers", function () { }); }); it("Should return an error as hackers don't have password stored", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -126,7 +127,7 @@ describe("Searching for hackers", function () { }); it("Should return an error as staff aren't searchable", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -145,7 +146,7 @@ describe("Searching for hackers", function () { }); }); it("Should throw an error because model is not lowercase", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -163,8 +164,8 @@ describe("Searching for hackers", function () { }); }); }); - it("Should throw an error because out of a fake model", function (done) { - util.auth.login(agent, Admin1, (error) => { + it("Should throw an error because of a fake model", function (done) { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -183,7 +184,7 @@ describe("Searching for hackers", function () { }); }); it("Should only return 1 hacker (page size)", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -203,7 +204,7 @@ describe("Searching for hackers", function () { }); }); it("Should only return 1 hacker (pagination)", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -225,7 +226,7 @@ describe("Searching for hackers", function () { }); }); it("Should throw an error because out of bounds (page size)", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -244,7 +245,7 @@ describe("Searching for hackers", function () { }); }); it("Should throw an error because out of bounds (pagination)", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -265,7 +266,7 @@ describe("Searching for hackers", function () { }); it("Should expand the accountId when expand is set to true", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); diff --git a/tests/setup.spec.js b/tests/setup.spec.js index d7b63ba4..a369e2e1 100644 --- a/tests/setup.spec.js +++ b/tests/setup.spec.js @@ -15,9 +15,6 @@ const Util = { AccountConfirmation: require("./util/accountConfirmation.test.util"), ResetPassword: require("./util/resetPassword.test.util.js") }; -const Constants = { - Role: require("../constants/role.constant"), -}; const logger = require("../services/logger.service"); //make sure that we are connected to the database @@ -53,17 +50,17 @@ afterEach(function (done) { }); }); async function storeAll() { - await Util.Account.storeAll(Util.Account.allAccounts); - await Util.Hacker.storeAll(Util.Hacker.Hackers); - await Util.Sponsor.storeAll(Util.Sponsor.Sponsors); - await Util.Team.storeAll(Util.Team.Teams); - await Util.Staff.storeAll(Util.Staff.Staffs); - await Util.AccountConfirmation.storeAll(Util.AccountConfirmation.AccountConfirmationTokens); - await Util.ResetPassword.storeAll(Util.ResetPassword.ResetPasswords); - await Util.Bus.storeAll(Util.Bus.Busses); - await Util.Volunteer.storeAll(Util.Volunteer.Volunteers); - await Util.Role.storeAll(Constants.Role.allRolesArray); - await Util.RoleBinding.storeAll(Util.RoleBinding.RoleBindings); + await Util.Account.storeAll(); + await Util.Hacker.storeAll(); + await Util.Sponsor.storeAll(); + await Util.Team.storeAll(); + await Util.Staff.storeAll(); + await Util.AccountConfirmation.storeAll(); + await Util.ResetPassword.storeAll(); + await Util.Bus.storeAll(); + await Util.Volunteer.storeAll(); + await Util.Role.storeAll(); + await Util.RoleBinding.storeAll(); } async function dropAll() { diff --git a/tests/sponsor.test.js b/tests/sponsor.test.js index 3ab980f3..dab90b9d 100644 --- a/tests/sponsor.test.js +++ b/tests/sponsor.test.js @@ -18,29 +18,20 @@ const util = { account: require("./util/account.test.util"), }; -// associated account -let storedAccount = util.account.Account3; -// configure sponsor data to be the same as output from query -// stringify and parse for deep copy -let storedSponsor = JSON.parse(JSON.stringify(util.sponsor.Sponsor1)); -storedSponsor.id = storedSponsor._id; -delete storedSponsor._id; - -let duplicateAccount = util.account.Account3; -let duplicateSponsor = util.sponsor.duplicateAccountLinkSponsor1; - -let authorizationFailAccount = util.account.Account1; +const Admin0 = util.account.staffAccounts.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; -const newSponsor = util.sponsor.newSponsor1; -const newSponsorAccount = util.account.Account5; +let duplicateSponsor = util.sponsor.duplicateAccountLinkSponsor1; -const Admin1 = util.account.Admin1; -const hackerAccount1 = util.account.Account1; describe("GET user sponsor", 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/` + util.sponsor.Sponsor1._id) + .get(`/api/sponsor/` + T1Sponsor0._id) // does not have password because of to stripped json .end(function (err, res) { res.should.have.status(401); @@ -54,13 +45,13 @@ describe("GET user sponsor", function () { // 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, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/sponsor/${util.sponsor.Sponsor1._id}`) + .get(`/api/sponsor/${T1Sponsor0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -72,7 +63,8 @@ describe("GET user sponsor", function () { res.body.should.have.property("data"); res.body.data.should.be.a("object"); - chai.expect(res.body.data).to.deep.equal(storedSponsor); + let sponsor = new Sponsor(T1Sponsor0); + chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(sponsor.toJSON())); done(); }); }); @@ -80,13 +72,13 @@ describe("GET user sponsor", function () { // 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, storedAccount, (error) => { + util.auth.login(agent, T1SponsorAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/sponsor/${util.sponsor.Sponsor1._id}`) + .get(`/api/sponsor/${util.sponsor.T1Sponsor0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -98,7 +90,8 @@ describe("GET user sponsor", function () { res.body.should.have.property("data"); res.body.data.should.be.a("object"); - chai.expect(res.body.data).to.deep.equal(storedSponsor); + let sponsor = new Sponsor(T1Sponsor0); + chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(sponsor.toJSON())); done(); }); }); @@ -106,13 +99,13 @@ describe("GET user sponsor", function () { // 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, authorizationFailAccount, (error) => { + util.auth.login(agent, HackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/sponsor/${util.sponsor.Sponsor1._id}`) + .get(`/api/sponsor/${util.sponsor.T1Sponsor0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -130,7 +123,7 @@ describe("GET user sponsor", function () { // 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, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -171,7 +164,7 @@ describe("POST create sponsor", function () { // success case with self caes - there is no admin case it("should SUCCEED and create a new sponsor", function (done) { - util.auth.login(agent, newSponsorAccount, (error) => { + util.auth.login(agent, newT2SponsorAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -179,7 +172,7 @@ describe("POST create sponsor", function () { return agent .post(`/api/sponsor/`) .type("application/json") - .send(newSponsor) + .send(newT2Sponsor0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -188,7 +181,7 @@ describe("POST create sponsor", function () { res.body.should.have.property("data"); // deleting id because that was generated, and not part of original data - const sponsor = (new Sponsor(newSponsor)).toJSON(); + 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)); @@ -199,7 +192,7 @@ describe("POST create sponsor", function () { // fail case - duplicate accountId it("should fail to create a sponsor due to duplicate accountId", function (done) { - util.auth.login(agent, duplicateAccount, (error) => { + util.auth.login(agent, T1SponsorAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -220,7 +213,7 @@ describe("POST create sponsor", function () { // unauthorized case it("should FAIL to create a new sponsor", function (done) { - util.auth.login(agent, hackerAccount1, (error) => { + util.auth.login(agent, HackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -228,7 +221,7 @@ describe("POST create sponsor", function () { return agent .post(`/api/sponsor/`) .type("application/json") - .send(newSponsor) + .send(newT2Sponsor0) .end(function (err, res) { res.should.have.status(403); res.should.be.json; diff --git a/tests/team.test.js b/tests/team.test.js index eee33fda..3c37cb95 100644 --- a/tests/team.test.js +++ b/tests/team.test.js @@ -20,6 +20,11 @@ const Constants = { const agent = chai.request.agent(server.app); +const Admin0 = util.account.staffAccounts.stored[0]; +const teamHackerAccount0 = util.account.hackerAccounts.stored.team[0]; +const noTeamHackerAccount0 = util.account.hackerAccounts.stored.noTeam[0]; +const sponsorT1Account0 = util.account.sponsorT1Accounts.stored[0]; + describe("GET team", function () { it("should FAIL to list a team's information due to lack of authentication", function (done) { chai.request(server.app) @@ -36,7 +41,7 @@ describe("GET team", function () { }); it("should Fail and list a team's information from /api/team/ GET due to non existant team id", function (done) { - util.auth.login(agent, util.account.Hacker4, (error) => { + util.auth.login(agent, util.account.hackerAccounts.stored.team[0], (error) => { if (error) { agent.close(); return done(error); @@ -56,7 +61,7 @@ describe("GET team", function () { }); it("should SUCCEED and list a team's information from /api/team/ GET", function (done) { - util.auth.login(agent, util.account.Hacker3, (error) => { + util.auth.login(agent, util.account.waitlistedHacker0, (error) => { if (error) { agent.close(); return done(error); @@ -72,14 +77,14 @@ describe("GET team", function () { res.body.data.should.have.property("team"); res.body.data.team.name.should.equal("FullTeam"); res.body.data.should.have.property("members"); - res.body.data.members[0].firstName.should.equal("abcd"); - res.body.data.members[0].lastName.should.equal("defg4"); - res.body.data.members[1].firstName.should.equal("abcd"); - res.body.data.members[1].lastName.should.equal("defg5"); - res.body.data.members[2].firstName.should.equal("abcd"); - res.body.data.members[2].lastName.should.equal("defg6"); - res.body.data.members[3].firstName.should.equal("abcd"); - res.body.data.members[3].lastName.should.equal("defg7"); + res.body.data.members[0].firstName.should.equal(util.account.hackerAccounts.stored.team[1].firstName); + res.body.data.members[0].lastName.should.equal(util.account.hackerAccounts.stored.team[1].lastName); + res.body.data.members[1].firstName.should.equal(util.account.hackerAccounts.stored.team[2].firstName); + res.body.data.members[1].lastName.should.equal(util.account.hackerAccounts.stored.team[2].lastName); + res.body.data.members[2].firstName.should.equal(util.account.hackerAccounts.stored.team[3].firstName); + res.body.data.members[2].lastName.should.equal(util.account.hackerAccounts.stored.team[3].lastName); + res.body.data.members[3].firstName.should.equal(util.account.hackerAccounts.stored.team[4].firstName); + res.body.data.members[3].lastName.should.equal(util.account.hackerAccounts.stored.team[4].lastName); done(); }); @@ -105,7 +110,7 @@ describe("POST create team", function () { }); it("should FAIL to create a new team due to lack of authorization", function (done) { - util.auth.login(agent, util.account.Account3, (error) => { + util.auth.login(agent, sponsorT1Account0, (error) => { if (error) { agent.close(); return done(error); @@ -127,7 +132,7 @@ describe("POST create team", function () { }); it("should FAIL to create a new team due to logged in user not being a hacker", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -149,7 +154,7 @@ describe("POST create team", function () { }); it("should FAIL to create a new team due to duplicate team name", function (done) { - util.auth.login(agent, util.account.Account2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -172,7 +177,7 @@ describe("POST create team", function () { }); it("should Fail to create a new team due to hacker already being in a team", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -193,7 +198,7 @@ describe("POST create team", function () { }); it("should SUCCEED and create a new team", function (done) { - util.auth.login(agent, util.account.Account2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -240,7 +245,7 @@ describe("PATCH join team", function () { }); it("should FAIL to join a volunteer to a team.", function (done) { - util.auth.login(agent, util.account.Account5, (error) => { + util.auth.login(agent, util.account.volunteerAccounts.stored[0], (error) => { if (error) { agent.close(); return done(error); @@ -264,7 +269,7 @@ describe("PATCH join team", function () { }); it("should FAIL to join a hacker to a team that doesn't exist.", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -288,7 +293,7 @@ describe("PATCH join team", function () { }); it("should FAIL to join a hacker to a team that is full.", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -312,7 +317,7 @@ describe("PATCH join team", function () { }); it("should SUCCEED and join a hacker without a team to a team.", function (done) { - util.auth.login(agent, util.account.Account2, (error) => { + util.auth.login(agent, noTeamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -336,7 +341,7 @@ describe("PATCH join team", function () { }); it("should SUCCEED and join a hacker on a team to aother team.", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -363,7 +368,7 @@ describe("PATCH join team", function () { describe("PATCH change team info", function () { it("should FAIL to change a hacker's team information due to invalid authentication", function (done) { chai.request(server.app) - .patch(`/api/team/${util.hacker.HackerF._id}`) + .patch(`/api/team/${util.hacker.TeamHacker4._id}`) .type("application/json") .send({ name: "BronzeTeamASDF", @@ -380,13 +385,13 @@ describe("PATCH change team info", function () { }); it("should FAIL for a hacker to change another team's information due to invalid authorization", function (done) { - util.auth.login(agent, util.account.Hacker4, (error) => { + util.auth.login(agent, util.account.hackerAccounts.stored.team[1], (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/team/${util.hacker.HackerA._id}`) + .patch(`/api/team/${util.hacker.TeamHacker0._id}`) .type("application/json") .send({ name: "SuccessTeamASDF", @@ -404,13 +409,13 @@ describe("PATCH change team info", function () { }); it("should SUCCEED to change the hacker's team information", function (done) { - util.auth.login(agent, util.account.Hacker4, (error) => { + util.auth.login(agent, util.account.hackerAccounts.stored.team[1], (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/team/${util.hacker.HackerD._id}`) + .patch(`/api/team/${util.hacker.TeamHacker1._id}`) .type("application/json") .send({ name: "SuccessTeamASDF", @@ -429,7 +434,7 @@ describe("PATCH change team info", function () { }); it("should SUCCEED and leave a team.", function (done) { - util.auth.login(agent, util.account.Account1, (error) => { + util.auth.login(agent, teamHackerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -449,13 +454,13 @@ describe("PATCH change team info", function () { }); it("should SUCCEED for an admin to change a team information", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .patch(`/api/team/${util.hacker.HackerD._id}`) + .patch(`/api/team/${util.hacker.TeamHacker3._id}`) .type("application/json") .send({ name: "SuccessTeamASDF", diff --git a/tests/util/account.test.util.js b/tests/util/account.test.util.js index d5628a57..87b5decd 100644 --- a/tests/util/account.test.util.js +++ b/tests/util/account.test.util.js @@ -5,67 +5,225 @@ const mongoose = require("mongoose"); const bcrypt = require("bcrypt"); const logger = require("../../services/logger.service"); -const newAccount1 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "NEW", - "lastName": "Account", - "pronoun": "He/Him", - "email": "newexist@blahblah.com", - "password": "1234567890", - "dietaryRestrictions": ["none"], - "shirtSize": "S", - "accountType": Constants.HACKER, - "birthDate": "1997-12-30", - "phoneNumber": 1234567890, +let counters = { + emailCounter: 0, }; -const nonAccount1 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "non", - "lastName": "Account", - "pronoun": "She/Her", - "email": "notexist@blahblah.com", - "password": "12345789", - "dietaryRestrictions": ["none"], - "shirtSize": "S", - "birthDate": "1990-01-01", - "phoneNumber": 1000000001, + +function incrementCounters() { + for (const key in counters) { + if (counters.hasOwnProperty(key)) { + counters[key] = counters[key] + 1; + } + } +} + +function extractAccountInfo(acc) { + let accDetails = {}; + + for (const val in acc) { + // use .hasOwnProperty instead of 'in' to get rid of inherited properties such as 'should' + if (Account.schema.paths.hasOwnProperty(val)) { + accDetails[val] = acc[val]; + } + } + + return accDetails; +} + +function generateRandomValue(atr) { + switch (atr) { + case "_id": + return mongoose.Types.ObjectId(); + case "firstName": + // generates a random string between length 5 and 10 of random characters from a-z + return Math.random().toString(36).replace(/[^a-z]+/g, "").substr(0, Math.floor(Math.random() * 6 + 5)); + case "lastName": + return Math.random().toString(36).replace(/[^a-z]+/g, "").substr(0, Math.floor(Math.random() * 6 + 5)); + case "pronoun": + // generate random string between length 2 and 4 + return Math.random().toString(36).replace(/[^a-z]+/g, "").substr(0, Math.floor(Math.random() * 3 + 2)); + case "email": + const email = `abc.def${counters.emailCounter}@blahblah.com`; + return email; + case "password": + return Math.random().toString(36).substr(0, 10); + case "dietaryRestrictions": + return [Constants.SAMPLE_DIET_RESTRICTIONS[Math.floor(Math.random() * Constants.SAMPLE_DIET_RESTRICTIONS.length)]]; + case "shirtSize": + return Constants.SHIRT_SIZES[Math.floor(Math.random() * Constants.SHIRT_SIZES.length)]; + case "confirmed": + // return false, because if an account is confirmed there should be a document of that account type, + // which this does not create + return Math.random() < 0.5; + case "accountType": + return Constants.EXTENDED_USER_TYPES[Math.floor(Math.random() * Constants.EXTENDED_USER_TYPES.length)]; + case "birthDate": + return new Date(); + case "phoneNumber": + return Math.floor(Math.random() * 10000000000); + } +} + +function createAccount(acc = {}) { + incrementCounters(); + + const extractedAcc = extractAccountInfo(acc); + + for (const atr in Account.schema.paths) { + if (!Account.schema.paths.hasOwnProperty(atr)) { + continue; + } + + // if this value has been passed in, continue + if (extractedAcc[atr] !== undefined) { + continue; + } + + extractedAcc[atr] = generateRandomValue(atr); + } + + return extractedAcc; +} + +function createNAccounts(n, acc = {}) { + let accounts = []; + for (let i = 0; i < n; i++) { + accounts.push(createAccount(acc)); + } + + return accounts; +} + +let hackerAccounts = { + new: createNAccounts(10, { + "accountType": Constants.HACKER, + "confirmed": true, + }), + stored: { + team: createNAccounts(10, { + "accountType": Constants.HACKER, + "confirmed": true, + }), + noTeam: createNAccounts(10, { + "accountType": Constants.HACKER, + "confirmed": true, + }), + }, + invalid: createNAccounts(10, { + "accountType": Constants.HACKER + }) }; -const Admin1 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "Admin1", - "lastName": "Admin1", - "pronoun": "Ze/Hir", - "email": "Admin1@blahblah.com", - "password": "Admin1", - "dietaryRestrictions": ["none"], - "shirtSize": "S", - "confirmed": true, - "accountType": Constants.STAFF, - "birthDate": "1990-01-02", - "phoneNumber": 1000000002, + +let volunteerAccounts = { + new: createNAccounts(5, { + "accountType": Constants.VOLUNTEER, + "confirmed": true, + }), + stored: createNAccounts(5, { + "accountType": Constants.VOLUNTEER, + "confirmed": true, + }), + invalid: createNAccounts(5, { + "accountType": Constants.VOLUNTEER + }), }; -// hacker -const Account1 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "ABC", - "lastName": "DEF", - "pronoun": "Ze/Zir", - "email": "abc.def1@blahblah.com", - "password": "probsShouldBeHashed1", - "dietaryRestrictions": ["none"], - "shirtSize": "S", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-03", - "phoneNumber": 1000000003, + +let staffAccounts = { + stored: createNAccounts(5, { + "accountType": Constants.STAFF, + "confirmed": true, + }) }; -// hacker -const Account2 = { + +let sponsorT1Accounts = { + new: createNAccounts(5, { + "accountType": Constants.SPONSOR_T1, + "confirmed": false, + }), + stored: createNAccounts(5, { + "accountType": Constants.SPONSOR_T1, + "confirmed": true, + }), + invalid: createNAccounts(5, { + "accountType": Constants.SPONSOR_T1 + }) +}; + +let sponsorT2Accounts = { + new: createNAccounts(5, { + "accountType": Constants.SPONSOR_T2, + "confirmed": true, + }), + stored: createNAccounts(5, { + "accountType": Constants.SPONSOR_T2, + "confirmed": true, + }), + invalid: createNAccounts(5, { + "accountType": Constants.SPONSOR_T2 + }) +}; + +let sponsorT3Accounts = { + new: createNAccounts(5, { + "accountType": Constants.SPONSOR_T3, + "confirmed": true, + }), + stored: createNAccounts(5, { + "accountType": Constants.SPONSOR_T3, + "confirmed": true, + }), + invalid: createNAccounts(5, { + "accountType": Constants.SPONSOR_T3 + }) +}; + +let sponsorT4Accounts = { + new: createNAccounts(5, { + "accountType": Constants.SPONSOR_T4, + "confirmed": true, + }), + stored: createNAccounts(5, { + "accountType": Constants.SPONSOR_T4, + "confirmed": true, + }), + invalid: createNAccounts(5, { + "accountType": Constants.SPONSOR_T4 + }) +}; + +let sponsorT5Accounts = { + new: createNAccounts(5, { + "accountType": Constants.SPONSOR_T5, + "confirmed": true, + }), + stored: createNAccounts(5, { + "accountType": Constants.SPONSOR_T5, + "confirmed": true, + }), + invalid: createNAccounts(5, { + "accountType": Constants.SPONSOR_T5 + }) +}; + +let unlinkedAccounts = { + new: [createAccount({ + "accountType": Constants.HACKER, + "confirmed": false, + })], + invalid: [createAccount()], + stored: [createAccount({ + "accountType": Constants.HACKER + }), createAccount({ + "accountType": Constants.HACKER + })] +}; + +const waitlistedHacker0 = { "_id": mongoose.Types.ObjectId(), - "firstName": "abc", - "lastName": "def", + "firstName": "abcd", + "lastName": "defg3", "pronoun": "They/Them", - "email": "abc.def2@blahblah.com", + "email": "waitlisted1@blahblah.com", "password": "probsShouldBeHashed2", "dietaryRestrictions": ["vegetarian"], "shirtSize": "M", @@ -74,51 +232,6 @@ const Account2 = { "birthDate": "1990-01-04", "phoneNumber": 1000000004, }; -// sponsor -const Account3 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "XYZ", - "lastName": "UST", - "pronoun": "Xey/Xem", - "email": "abc.def3@blahblah.com", - "password": "probsShouldBeHashed3", - "dietaryRestrictions": ["vegan"], - "shirtSize": "L", - "confirmed": true, - "birthDate": "1990-01-05", - "phoneNumber": 1000000005, - "accountType": Constants.SPONSOR_T1 -}; -// volunteer -const Account4 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "xyz", - "lastName": "ust", - "pronoun": "Sie/Hir", - "email": "abc.def4@blahblah.com", - "password": "probsShouldBeHashed4", - "dietaryRestrictions": ["vegetarian", "lactose intolerant"], - "shirtSize": "XL", - "confirmed": true, - "accountType": Constants.VOLUNTEER, - "birthDate": "1980-01-30", - "phoneNumber": 1000000006, -}; -// sponsor -const Account5 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "LMAO", - "lastName": "ROFL", - "pronoun": "It/It", - "email": "abc.def0@blahblah.com", - "password": "probsShouldBeHashed5", - "dietaryRestrictions": ["something1", "something2"], - "shirtSize": "XXL", - "confirmed": true, - "accountType": Constants.SPONSOR_T2, - "birthDate": "1980-06-30", - "phoneNumber": 1000000236 -}; // non confirmed account for hacker const NonConfirmedAccount1 = { @@ -126,7 +239,7 @@ const NonConfirmedAccount1 = { "firstName": "LMAO", "lastName": "ROFL", "pronoun": "Ey/Em", - "email": "abc.def6@blahblah.com", + "email": "notconfirmed1@blahblah.com", "password": "probsShouldBeHashed5", "dietaryRestrictions": ["something1", "something2"], "shirtSize": "XXL", @@ -148,175 +261,44 @@ const NonConfirmedAccount2 = { "accountType": Constants.HACKER, }; -// hacker waitlisted -const Hacker3 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abcd", - "lastName": "defg3", - "pronoun": "They/Them", - "email": "abc.def7@blahblah.com", - "password": "probsShouldBeHashed2", - "dietaryRestrictions": ["vegetarian"], - "shirtSize": "M", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-04", - "phoneNumber": 1000000004, -}; - -const Hacker4 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abcd", - "lastName": "defg4", - "pronoun": "They/Them", - "email": "abc.def.hacker4@blahblah.com", - "password": "probsShouldBeHashed2", - "dietaryRestrictions": ["vegetarian"], - "shirtSize": "M", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-04", - "phoneNumber": 1000000004, -}; -const Hacker5 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abcd", - "lastName": "defg5", - "pronoun": "They/Them", - "email": "abc.def.hacker5@blahblah.com", - "password": "probsShouldBeHashed2", - "dietaryRestrictions": ["vegetarian"], - "shirtSize": "M", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-04", - "phoneNumber": 1000000004, -}; -const Hacker6 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abcd", - "lastName": "defg6", - "pronoun": "They/Them", - "email": "abc.def.hacker6@blahblah.com", - "password": "probsShouldBeHashed2", - "dietaryRestrictions": ["vegetarian"], - "shirtSize": "M", - "confirmed": true, - "accountType": Constants.HACKER, - "birthDate": "1990-01-04", - "phoneNumber": 1000000004, -}; -const Hacker7 = { - "_id": mongoose.Types.ObjectId(), - "firstName": "abcd", - "lastName": "defg7", - "pronoun": "They/Them", - "email": "abc.def.hacker7@blahblah.com", - "password": "probsShouldBeHashed2", - "dietaryRestrictions": ["vegetarian"], - "shirtSize": "M", - "confirmed": true, +const NonConfirmedAccount3 = createAccount({ + "confirmed": false, "accountType": Constants.HACKER, - "birthDate": "1990-01-04", - "phoneNumber": 1000000004, -}; + "email": "notconfirmed3@blahblah.com" +}); -const customAccounts = [ - Admin1, - Account1, - Account2, - Account3, - Account4, - Account5, - Hacker3, - Hacker4, - Hacker5, - Hacker6, - Hacker7, - NonConfirmedAccount1, - NonConfirmedAccount2 -]; - -const generatedAccounts = generateAccounts(20); -// 1-5 Are for admins -// 6-10 Are for hackers (6 and 7 are new) -// 11-15 Are for sponsors -// 16-20 Are for volunteers - - -const allAccounts = customAccounts.concat(generatedAccounts); +const extraAccounts = [waitlistedHacker0, NonConfirmedAccount1, NonConfirmedAccount2, NonConfirmedAccount3]; module.exports = { - nonAccount1: nonAccount1, - newAccount1: newAccount1, + hackerAccounts: hackerAccounts, + volunteerAccounts: volunteerAccounts, + staffAccounts: staffAccounts, + sponsorT1Accounts: sponsorT1Accounts, + sponsorT2Accounts: sponsorT2Accounts, + sponsorT3Accounts: sponsorT3Accounts, + sponsorT4Accounts: sponsorT4Accounts, + sponsorT5Accounts: sponsorT5Accounts, + unlinkedAccounts: unlinkedAccounts, + + waitlistedHacker0: waitlistedHacker0, NonConfirmedAccount1: NonConfirmedAccount1, NonConfirmedAccount2: NonConfirmedAccount2, - Admin1: Admin1, - Account1: Account1, - Account2: Account2, - Account3: Account3, - Account4: Account4, - Account5: Account5, - Hacker3: Hacker3, - Hacker4: Hacker4, - Hacker5: Hacker5, - Hacker6: Hacker6, - Hacker7: Hacker7, - customAccounts: customAccounts, - generatedAccounts: generatedAccounts, - allAccounts: allAccounts, + NonConfirmedAccount3: NonConfirmedAccount3, + + extraAccounts: extraAccounts, + storeAll: storeAll, dropAll: dropAll, - equals: equals + equals: equals, }; -function generateRandomShirtSize() { - return Constants.SHIRT_SIZES[Math.floor(Math.random() * Constants.SHIRT_SIZES.length)]; -} - -function generateAccounts(n) { - let accounts = []; - for (let i = 0; i < n; i++) { - let birthMonth = Math.floor(Math.random() * 12) + 1; - let birthDay = Math.floor(Math.random() * 28) + 1; - let phoneNumber = Math.floor(Math.random() * 10000000000); - - let acc = { - "_id": mongoose.Types.ObjectId(), - "firstName": "first" + String(i), - "lastName": "last" + String(i), - "pronoun": "They/" + String(i), - "email": "test" + String(i) + "@blahblah.com", - "password": "probsShouldBeHashed" + String(i), - "dietaryRestrictions": [], - "shirtSize": generateRandomShirtSize(), - "confirmed": true, - "birthDate": `1980-${birthMonth}-${birthDay}`, - "phoneNumber": phoneNumber, - }; - - if (i < n / 4) { - acc.accountType = Constants.STAFF; - } else if (i >= n / 4 && i < (n / 4) * 2) { - acc.accountType = Constants.HACKER; - } else if (i >= (n / 4) * 2 && i < (n / 4) * 3) { - acc.accountType = Constants.SPONSOR; - } else { - acc.accountType = Constants.VOLUNTEER; - } - - accounts.push(acc); - } - return accounts; -} - function encryptPassword(user) { let encryptedUser = JSON.parse(JSON.stringify(user)); encryptedUser.password = bcrypt.hashSync(user.password, 10); return encryptedUser; } -function storeAll(attributes) { +function store(attributes) { const acctDocs = []; const acctNames = []; for (var i = 0; i < attributes.length; i++) { @@ -328,6 +310,29 @@ function storeAll(attributes) { return Account.collection.insertMany(acctDocs); } +async function storeAll() { + await store(hackerAccounts.stored.team); + await store(hackerAccounts.stored.noTeam); + await store(volunteerAccounts.stored); + await store(staffAccounts.stored); + await store(sponsorT1Accounts.stored); + await store(sponsorT2Accounts.stored); + await store(sponsorT3Accounts.stored); + await store(sponsorT4Accounts.stored); + await store(sponsorT5Accounts.stored); + await store(unlinkedAccounts.stored); + + await store(hackerAccounts.new); + await store(volunteerAccounts.new); + await store(sponsorT1Accounts.new); + await store(sponsorT2Accounts.new); + await store(sponsorT3Accounts.new); + await store(sponsorT4Accounts.new); + await store(sponsorT5Accounts.new); + + await store(extraAccounts); +} + async function dropAll() { try { await Account.collection.drop(); @@ -340,6 +345,8 @@ async function dropAll() { } } +// Try deleting this and see if anything fucks up + /** * Compare two accounts * @param {Account} acc1 @@ -355,5 +362,5 @@ function equals(acc1, acc2) { const email = (acc1.email === acc2.email); const dietaryRestrictions = (acc1.dietaryRestrictions.join(",") === acc2.dietaryRestrictions.join(",")); const shirtSize = (acc1.shirtSize === acc2.shirtSize); - return [id, firstName, lastName, email, dietaryRestrictions, shirtSize]; + return [id, firstName, lastName, email, dietaryRestrictions, shirtSize, pronoun]; } \ No newline at end of file diff --git a/tests/util/accountConfirmation.test.util.js b/tests/util/accountConfirmation.test.util.js index 949add4c..88280d1a 100644 --- a/tests/util/accountConfirmation.test.util.js +++ b/tests/util/accountConfirmation.test.util.js @@ -16,9 +16,9 @@ const logger = require("../../services/logger.service"); const HackerConfirmation = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account2._id, + "accountId": Util.Account.NonConfirmedAccount1._id, "accountType": Constants.HACKER, - "email": Util.Account.Account2.email + "email": Util.Account.NonConfirmedAccount1.email }; const HackerConfirmation2 = { @@ -30,7 +30,7 @@ const HackerConfirmation2 = { const HackerConfirmation3 = { "_id": mongoose.Types.ObjectId(), - "email": Util.Account.newAccount1.email + "email": Util.Account.unlinkedAccounts.new[0].email }; const HackerConfirmation4 = { @@ -42,7 +42,7 @@ const HackerConfirmation4 = { // Using a real ID which is stored but corresponds to another account const FakeHackerToken = { "_id": HackerConfirmation._id, - "accountId": Util.Account.Account3._id, + "accountId": Util.Account.sponsorT1Accounts.stored[0]._id, "accountType": Constants.HACKER }; @@ -56,7 +56,7 @@ const AccountConfirmationTokens = [ HackerConfirmation4 ]; -function storeAll(attributes) { +function store(attributes) { const accountConfirmationDocs = []; const accountConfirmationIds = []; for (var i = 0; i < attributes.length; i++) { @@ -66,6 +66,10 @@ function storeAll(attributes) { return AccountConfirmationToken.collection.insertMany(accountConfirmationDocs); } +async function storeAll() { + await store(AccountConfirmationTokens); +} + async function dropAll() { try { await AccountConfirmationToken.collection.drop(); diff --git a/tests/util/bus.test.util.js b/tests/util/bus.test.util.js index 283c8260..2e1aa1ab 100644 --- a/tests/util/bus.test.util.js +++ b/tests/util/bus.test.util.js @@ -15,7 +15,7 @@ const Bus1 = { "addr2": "addr2-1" }, "capacity": 10, - "hackers": [Util.Hacker.HackerA._id] + "hackers": [Util.Hacker.TeamHacker0._id] }; const Busses = [ Bus1, @@ -28,7 +28,7 @@ module.exports = { dropAll: dropAll }; -function storeAll(attributes) { +function store(attributes) { const busDocs = []; const busZips = []; for (var i = 0; i < attributes.length; i++) { @@ -39,6 +39,10 @@ function storeAll(attributes) { return Bus.collection.insertMany(busDocs); } +async function storeAll() { + await store(Busses); +} + async function dropAll() { try { await Bus.collection.drop(); diff --git a/tests/util/hacker.test.util.js b/tests/util/hacker.test.util.js index 2ea1b3d1..598a8e69 100644 --- a/tests/util/hacker.test.util.js +++ b/tests/util/hacker.test.util.js @@ -10,32 +10,13 @@ const mongoose = require("mongoose"); const Hacker = require("../../models/hacker.model"); const logger = require("../../services/logger.service"); -const invalidHacker1 = { - "_id": mongoose.Types.ObjectId(), - // invalid mongoID - "accountId": "UtilAccountAccount1_id", - // invalid missing school attribute - "degree": "Undersaduate", - "gender": "Female", - "needsBus": true, - "application": { - // invalid portflio with no resume - "portfolioURL": {}, - // invalid jobInterest - "jobInterest": "ASDF", - }, - "ethnicity": ["Asian", "Caucasian"], - "major": "CS", - "graduationYear": 2020, - "codeOfConduct": true, -}; - -// duplicate of newHack1, but with false for code of conduct -const badCodeOfConductHacker1 = { - "accountId": Util.Account.generatedAccounts[6]._id, - "school": "University of ASDF", +const TeamHacker0 = { + "_id": Constants.MongoId.hackerAId, + "accountId": Util.Account.hackerAccounts.stored.team[0]._id, + "status": "Confirmed", + "school": "University of Blah", "degree": "Masters", - "gender": "Female", + "gender": "Male", "needsBus": true, "application": { "portfolioURL": { @@ -50,119 +31,128 @@ const badCodeOfConductHacker1 = { "jobInterest": "Full-time", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": ["Caucasian"], + "ethnicity": ["Native American"], "major": "EE", "graduationYear": 2019, - "codeOfConduct": false, + "codeOfConduct": true, + "teamId": Constants.MongoId.team1Id, }; -const duplicateAccountLinkHacker1 = { - "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account1._id, - "status": "Applied", - "school": "University of Blah", - "degree": "Undergraduate", - "gender": "Male", - "needsBus": true, +const TeamHacker1 = { + "_id": Constants.MongoId.hackerDId, + "accountId": Util.Account.hackerAccounts.stored.team[1]._id, + "status": "Waitlisted", + "school": "University of Blah1", + "degree": "Masters", + "gender": "Female", + "needsBus": false, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume100", - "github": "www.github.com/Person1", + "resume": "www.gcloud.com/myResume2", + "github": "www.github.com/Personasdf", "dropler": undefined, - "personal": "www.person1.com", - "linkedIn": "www.linkedin.com/in/Person1", + "personal": undefined, + "linkedIn": undefined, "other": undefined }, - "jobInterest": "Full-time", + "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": ["Caucasian"], - "major": "CS", + "ethnicity": ["European"], + "major": "EE", "graduationYear": 2019, "codeOfConduct": true, + "teamId": Constants.MongoId.team3Id, }; -const newHacker1 = { - "accountId": Util.Account.generatedAccounts[6]._id, - "school": "University of ASDF", +const TeamHacker2 = { + "_id": Constants.MongoId.hackerEId, + "accountId": Util.Account.hackerAccounts.stored.team[2]._id, + "status": "Waitlisted", + "school": "University of Blah1", "degree": "Masters", "gender": "Female", - "needsBus": true, + "needsBus": false, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume100", - "github": "www.github.com/Person1", + "resume": "www.gcloud.com/myResume2", + "github": "www.github.com/Personasdf", "dropler": undefined, - "personal": "www.person1.com", - "linkedIn": "www.linkedin.com/in/Person1", + "personal": undefined, + "linkedIn": undefined, "other": undefined }, - "jobInterest": "Full-time", + "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": ["Caucasian"], + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, + "teamId": Constants.MongoId.team3Id, }; -const newHacker2 = { - "accountId": Util.Account.NonConfirmedAccount1._id, - "school": "University of YIKES", - "degree": "PhD", +const TeamHacker3 = { + "_id": Constants.MongoId.hackerFId, + "accountId": Util.Account.hackerAccounts.stored.team[3]._id, + "status": "Waitlisted", + "school": "University of Blah1", + "degree": "Masters", "gender": "Female", - "needsBus": true, + "needsBus": false, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume100", - "github": "www.github.com/Person1", + "resume": "www.gcloud.com/myResume2", + "github": "www.github.com/Personasdf", "dropler": undefined, - "personal": "www.person1.com", - "linkedIn": "www.linkedin.com/in/Person1", + "personal": undefined, + "linkedIn": undefined, "other": undefined }, - "jobInterest": "Full-time", + "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": ["African American"], + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, + "teamId": Constants.MongoId.team3Id, }; -const HackerA = { - "_id": Constants.MongoId.hackerAId, - "accountId": Util.Account.Account1._id, - "status": "Confirmed", - "school": "University of Blah", +const TeamHacker4 = { + "_id": Constants.MongoId.hackerGId, + "accountId": Util.Account.hackerAccounts.stored.team[4]._id, + "status": "Waitlisted", + "school": "University of Blah1", "degree": "Masters", - "gender": "Male", - "needsBus": true, + "gender": "Female", + "needsBus": false, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume100", - "github": "www.github.com/Person1", + "resume": "www.gcloud.com/myResume2", + "github": "www.github.com/Personasdf", "dropler": undefined, - "personal": "www.person1.com", - "linkedIn": "www.linkedin.com/in/Person1", + "personal": undefined, + "linkedIn": undefined, "other": undefined }, - "jobInterest": "Full-time", + "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": ["Native American"], + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, - "teamId": Constants.MongoId.team1Id, + "teamId": Constants.MongoId.team3Id, }; -const HackerB = { + +const NoTeamHacker0 = { "_id": Constants.MongoId.hackerBId, - "accountId": Util.Account.Account2._id, + "accountId": Util.Account.hackerAccounts.stored.noTeam[0]._id, "status": "Accepted", "school": "University of Blah1", "degree": "Masters", @@ -187,93 +177,132 @@ const HackerB = { "codeOfConduct": true, }; -const HackerC = { - "_id": Constants.MongoId.hackerCId, - "accountId": Util.Account.Hacker3._id, - "status": "Waitlisted", - "school": "University of Blah1", +const newHacker0 = { + "accountId": Util.Account.hackerAccounts.new[0]._id, + "school": "University of ASDF", "degree": "Masters", "gender": "Female", - "needsBus": false, + "needsBus": true, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume2", - "github": "www.github.com/Personasdf", + "resume": "www.gcloud.com/myResume100", + "github": "www.github.com/Person1", "dropler": undefined, - "personal": undefined, - "linkedIn": undefined, + "personal": "www.person1.com", + "linkedIn": "www.linkedin.com/in/Person1", "other": undefined }, - "jobInterest": "Internship", + "jobInterest": "Full-time", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["Caucasian"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, - "teamId": Constants.MongoId.team2Id, }; -const HackerD = { - "_id": Constants.MongoId.hackerDId, - "accountId": Util.Account.Hacker4._id, - "status": "Waitlisted", - "school": "University of Blah1", - "degree": "Masters", +const newHacker1 = { + "accountId": Util.Account.hackerAccounts.new[1]._id, + "school": "University of YIKES", + "degree": "PhD", "gender": "Female", - "needsBus": false, + "needsBus": true, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume2", - "github": "www.github.com/Personasdf", + "resume": "www.gcloud.com/myResume100", + "github": "www.github.com/Person1", "dropler": undefined, - "personal": undefined, - "linkedIn": undefined, + "personal": "www.person1.com", + "linkedIn": "www.linkedin.com/in/Person1", "other": undefined }, - "jobInterest": "Internship", + "jobInterest": "Full-time", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["African American"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, - "teamId": Constants.MongoId.team3Id, }; -const HackerE = { - "_id": Constants.MongoId.hackerEId, - "accountId": Util.Account.Hacker5._id, - "status": "Waitlisted", - "school": "University of Blah1", +// duplicate of newHack1, but with false for code of conduct +const invalidHacker0 = { + "accountId": Util.Account.hackerAccounts.invalid[0]._id, + "school": "University of ASDF", "degree": "Masters", "gender": "Female", - "needsBus": false, + "needsBus": true, "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume2", - "github": "www.github.com/Personasdf", + "resume": "www.gcloud.com/myResume100", + "github": "www.github.com/Person1", "dropler": undefined, - "personal": undefined, - "linkedIn": undefined, + "personal": "www.person1.com", + "linkedIn": "www.linkedin.com/in/Person1", "other": undefined }, - "jobInterest": "Internship", + "jobInterest": "Full-time", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["Caucasian"], "major": "EE", "graduationYear": 2019, + "codeOfConduct": false, +}; + +const invalidHacker1 = { + "_id": mongoose.Types.ObjectId(), + // invalid mongoID + "accountId": Util.Account.hackerAccounts.invalid[1]._invalidId, + // invalid missing school attribute + "degree": "Undersaduate", + "gender": "Female", + "needsBus": true, + "application": { + // invalid portflio with no resume + "portfolioURL": {}, + // invalid jobInterest + "jobInterest": "ASDF", + }, + "ethnicity": ["Asian", "Caucasian"], + "major": "CS", + "graduationYear": 2020, "codeOfConduct": true, - "teamId": Constants.MongoId.team3Id, }; -const HackerF = { - "_id": Constants.MongoId.hackerFId, - "accountId": Util.Account.Hacker6._id, +const duplicateAccountLinkHacker0 = { + "_id": mongoose.Types.ObjectId(), + "accountId": Util.Account.hackerAccounts.stored.team[0]._id, + "status": "Applied", + "school": "University of Blah", + "degree": "Undergraduate", + "gender": "Male", + "needsBus": true, + "application": { + "portfolioURL": { + //gcloud bucket link + "resume": "www.gcloud.com/myResume100", + "github": "www.github.com/Person1", + "dropler": undefined, + "personal": "www.person1.com", + "linkedIn": "www.linkedin.com/in/Person1", + "other": undefined + }, + "jobInterest": "Full-time", + "skills": ["CSS", "HTML", "JS"], + }, + "ethnicity": ["Caucasian"], + "major": "CS", + "graduationYear": 2019, + "codeOfConduct": true, +}; + +const waitlistedHacker0 = { + "_id": Constants.MongoId.hackerCId, + "accountId": Util.Account.waitlistedHacker0._id, "status": "Waitlisted", "school": "University of Blah1", "degree": "Masters", @@ -292,16 +321,16 @@ const HackerF = { "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, - "teamId": Constants.MongoId.team3Id, + "teamId": Constants.MongoId.team2Id, }; -const HackerG = { - "_id": Constants.MongoId.hackerGId, - "accountId": Util.Account.Hacker7._id, +const unconfirmedAccountHacker0 = { + "_id": Constants.MongoId.hackerCId, + "accountId": Util.Account.NonConfirmedAccount3._id, "status": "Waitlisted", "school": "University of Blah1", "degree": "Masters", @@ -310,7 +339,7 @@ const HackerG = { "application": { "portfolioURL": { //gcloud bucket link - "resume": "www.gcloud.com/myResume2", + "resume": "www.gcloud.com/myResume123", "github": "www.github.com/Personasdf", "dropler": undefined, "personal": undefined, @@ -320,42 +349,50 @@ const HackerG = { "jobInterest": "Internship", "skills": ["CSS", "HTML", "JS"], }, - "ethnicity": "European", + "ethnicity": ["European"], "major": "EE", "graduationYear": 2019, "codeOfConduct": true, - "teamId": Constants.MongoId.team3Id, }; const Hackers = [ - HackerA, - HackerB, - HackerC, - HackerD, - HackerE, - HackerF, - HackerG, + TeamHacker0, + TeamHacker1, + TeamHacker2, + TeamHacker3, + TeamHacker4, + + NoTeamHacker0, + + duplicateAccountLinkHacker0, + waitlistedHacker0 ]; module.exports = { - duplicateAccountLinkHacker1: duplicateAccountLinkHacker1, - invalidHacker1: invalidHacker1, + TeamHacker0: TeamHacker0, + TeamHacker1: TeamHacker1, + TeamHacker2: TeamHacker2, + TeamHacker3: TeamHacker3, + TeamHacker4: TeamHacker4, + + NoTeamHacker0: NoTeamHacker0, + + newHacker0: newHacker0, newHacker1: newHacker1, - newHacker2: newHacker2, - badCodeOfConductHacker1: badCodeOfConductHacker1, - HackerA: HackerA, - HackerB: HackerB, - HackerC: HackerC, - HackerD: HackerD, - HackerE: HackerE, - HackerF: HackerF, - HackerG: HackerG, + + invalidHacker0: invalidHacker0, + invalidHacker1: invalidHacker1, + + duplicateAccountLinkHacker0: duplicateAccountLinkHacker0, + waitlistedHacker0: waitlistedHacker0, + unconfirmedAccountHacker0: unconfirmedAccountHacker0, + Hackers: Hackers, storeAll: storeAll, dropAll: dropAll }; -function storeAll(attributes) { +function store(attributes) { const hackerDocs = []; const hackerIds = []; for (var i = 0; i < attributes.length; i++) { @@ -366,6 +403,10 @@ function storeAll(attributes) { return Hacker.collection.insertMany(hackerDocs); } +async function storeAll() { + await store(Hackers); +} + async function dropAll() { try { await Hacker.collection.drop(); diff --git a/tests/util/resetPassword.test.util.js b/tests/util/resetPassword.test.util.js index 567a0e89..8a45abb5 100644 --- a/tests/util/resetPassword.test.util.js +++ b/tests/util/resetPassword.test.util.js @@ -14,7 +14,7 @@ const logger = require("../../services/logger.service"); const ResetPasswordToken1 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account1._id + "accountId": Util.Account.hackerAccounts.stored.team[0]._id }; const ResetToken = Services.resetPassword.generateToken(ResetPasswordToken1._id, ResetPasswordToken1.accountId); @@ -23,7 +23,7 @@ const ResetPasswords = [ ResetPasswordToken1 ]; -function storeAll(attributes) { +function store(attributes) { const resetPasswordDocs = []; const resetPasswordIds = []; for (var i = 0; i < attributes.length; i++) { @@ -33,6 +33,10 @@ function storeAll(attributes) { return ResetPassword.collection.insertMany(resetPasswordDocs); } +async function storeAll() { + await store(ResetPasswords); +} + async function dropAll() { try { await ResetPassword.collection.drop(); diff --git a/tests/util/role.test.util.js b/tests/util/role.test.util.js index 142b47c5..c9e1da5c 100644 --- a/tests/util/role.test.util.js +++ b/tests/util/role.test.util.js @@ -1,6 +1,9 @@ "use strict"; const Role = require("../../models/role.model"); -const Constants = require("../../constants/general.constant"); +const Constants = { + General: require("../../constants/general.constant"), + Role: require("../../constants/role.constant"), +} const Routes = require("../../constants/routes.constant"); const mongoose = require("mongoose"); const logger = require("../../services/logger.service"); @@ -20,7 +23,7 @@ const duplicateRole1 = { ] }; -function storeAll(attributes) { +function store(attributes) { const roleDocs = []; const roleNames = []; attributes.forEach((attribute) => { @@ -31,6 +34,10 @@ function storeAll(attributes) { return Role.collection.insertMany(roleDocs); } +async function storeAll() { + await store(Constants.Role.allRolesArray); +} + async function dropAll() { try { await Role.collection.drop(); diff --git a/tests/util/roleBinding.test.util.js b/tests/util/roleBinding.test.util.js index d43a3a7a..8d3b42b8 100644 --- a/tests/util/roleBinding.test.util.js +++ b/tests/util/roleBinding.test.util.js @@ -4,104 +4,87 @@ const Util = { Account: require("./account.test.util"), }; const Constants = { - Role: require("../../constants/role.constant") + Role: require("../../constants/role.constant"), + General: require("../../constants/general.constant") }; const logger = require("../../services/logger.service"); -const RoleBinding1 = { - accountId: Util.Account.allAccounts[6]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.adminRole._id], -}; -const RoleBinding2 = { - accountId: Util.Account.allAccounts[7]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.hackerRole._id], -}; -const RoleBinding3 = { - accountId: Util.Account.allAccounts[8]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.volunteerRole._id], -}; -const RoleBinding4 = { - accountId: Util.Account.allAccounts[9]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.sponsorT1Role._id], -}; -const RoleBinding5 = { - accountId: Util.Account.allAccounts[10]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.sponsorT2Role._id], -}; -const RoleBinding6 = { - accountId: Util.Account.allAccounts[11]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.allRolesObject.getSelfAccount._id], -}; +function createRoleBinding(accountId, accountType = null, specificRoles = []) { + let roleBinding = { + accountId: accountId, + roles: [Constants.Role.accountRole] + }; -const RoleBinding7 = { - accountId: Util.Account.allAccounts[12]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.allRolesObject.getAnyByIdHacker._id, Constants.Role.allRolesObject.patchSelfByIdHacker._id], -}; + switch (accountType) { + case Constants.General.HACKER: + roleBinding.roles.push(Constants.Role.hackerRole); + break; + case Constants.General.VOLUNTEER: + roleBinding.roles.push(Constants.Role.volunteerRole); + break; + case Constants.General.STAFF: + roleBinding.roles.push(Constants.Role.adminRole); + break; + case Constants.General.SPONSOR_T1: + roleBinding.roles.push(Constants.Role.sponsorT1Role); + break; + case Constants.General.SPONSOR_T2: + roleBinding.roles.push(Constants.Role.sponsorT2Role); + break; + case Constants.General.SPONSOR_T3: + roleBinding.roles.push(Constants.Role.sponsorT3Role); + break; + case Constants.General.SPONSOR_T4: + roleBinding.roles.push(Constants.Role.sponsorT4Role); + break; + case Constants.General.SPONSOR_T5: + roleBinding.roles.push(Constants.Role.sponsorT5Role); + break; + } -const RoleBindingHacker1 = { - accountId: Util.Account.Account1._id, - roles: [Constants.Role.accountRole._id, Constants.Role.hackerRole._id], -}; -const RoleBindingHacker2 = { - accountId: Util.Account.Account2._id, - roles: [Constants.Role.accountRole._id, Constants.Role.hackerRole._id], -}; -const RoleBindingHacker3 = { - accountId: Util.Account.Hacker3._id, - roles: [Constants.Role.hackerRole._id], -}; + for (const role of specificRoles) { + roleBinding.roles.push(role); + } -const RoleBindingNewHacker1 = { - accountId: Util.Account.allAccounts[13]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.allRolesObject["postHacker"]], -}; + return roleBinding; +} -const RoleBindingSponsor1 = { - accountId: Util.Account.Account3._id, - roles: [Constants.Role.accountRole._id, Constants.Role.sponsorT1Role._id], -}; +function createRoleBindings(accounts) { + let roleBindings = []; -const RoleBindingSponsor2 = { - accountId: Util.Account.Account5._id, - roles: [Constants.Role.accountRole._id, Constants.Role.sponsorT1Role._id], -}; + for (const account of accounts) { + roleBindings.push(createRoleBinding(account._id, account.accountType)); + } -const RoleBindingVolunteer1 = { - accountId: Util.Account.Account4._id, - roles: [Constants.Role.accountRole._id, Constants.Role.volunteerRole._id], -}; + return roleBindings; +} -const RoleBindingNewVolunteer1 = { - accountId: Util.Account.generatedAccounts[15]._id, - roles: [Constants.Role.accountRole._id, Constants.Role.allRolesObject["postVolunteer"]], -}; +const TeamHackerRB = createRoleBindings(Util.Account.hackerAccounts.stored.team); +const NoTeamHackerRB = createRoleBindings(Util.Account.hackerAccounts.stored.noTeam); +const VolunteerRB = createRoleBindings(Util.Account.volunteerAccounts.stored); +const StaffRB = createRoleBindings(Util.Account.staffAccounts.stored); +const SponsorT1RB = createRoleBindings(Util.Account.sponsorT1Accounts.stored); +const SponsorT2RB = createRoleBindings(Util.Account.sponsorT2Accounts.stored); +const SponsorT3RB = createRoleBindings(Util.Account.sponsorT3Accounts.stored); +const SponsorT4RB = createRoleBindings(Util.Account.sponsorT4Accounts.stored); +const SponsorT5RB = createRoleBindings(Util.Account.sponsorT5Accounts.stored); -const RoleBindingAdmin1 = { - accountId: Util.Account.Admin1._id, - roles: [Constants.Role.adminRole._id], -}; +const newHackerRB = createRoleBindings(Util.Account.hackerAccounts.new); +const newVolunteerRB = createRoleBindings(Util.Account.volunteerAccounts.new); +const newSponsorT1RB = createRoleBindings(Util.Account.sponsorT1Accounts.new); +const newSponsorT2RB = createRoleBindings(Util.Account.sponsorT2Accounts.new); +const newSponsorT3RB = createRoleBindings(Util.Account.sponsorT3Accounts.new); +const newSponsorT4RB = createRoleBindings(Util.Account.sponsorT4Accounts.new); +const newSponsorT5RB = createRoleBindings(Util.Account.sponsorT5Accounts.new); -const RoleBindings = [ - RoleBinding1, - RoleBinding2, - RoleBinding3, - RoleBinding4, - RoleBinding5, - RoleBinding6, - RoleBinding7, - RoleBindingHacker1, - RoleBindingHacker2, - RoleBindingNewHacker1, - RoleBindingSponsor1, - RoleBindingSponsor2, - RoleBindingVolunteer1, - RoleBindingNewVolunteer1, - RoleBindingAdmin1, - RoleBindingHacker3, +const extraAccounts = [ + createRoleBinding(Util.Account.NonConfirmedAccount1._id), + createRoleBinding(Util.Account.NonConfirmedAccount2._id), + createRoleBinding(Util.Account.NonConfirmedAccount3._id), + createRoleBinding(Util.Account.waitlistedHacker0._id, Constants.General.HACKER), ]; - -function storeAll(attributes) { +function store(attributes) { const roleBindingDocs = []; const roleBindingNames = []; attributes.forEach((attribute) => { @@ -112,6 +95,28 @@ function storeAll(attributes) { return RoleBinding.collection.insertMany(roleBindingDocs); } +async function storeAll() { + await store(TeamHackerRB); + await store(NoTeamHackerRB); + await store(VolunteerRB); + await store(StaffRB); + await store(SponsorT1RB); + await store(SponsorT2RB); + await store(SponsorT3RB); + await store(SponsorT4RB); + await store(SponsorT5RB); + + await store(newHackerRB); + await store(newVolunteerRB); + await store(newSponsorT1RB); + await store(newSponsorT2RB); + await store(newSponsorT3RB); + await store(newSponsorT4RB); + await store(newSponsorT5RB); + + await store(extraAccounts); +} + async function dropAll() { try { await RoleBinding.collection.drop(); @@ -125,15 +130,26 @@ async function dropAll() { } module.exports = { - RoleBinding1: RoleBinding1, - RoleBinding2: RoleBinding2, - RoleBinding3: RoleBinding3, - RoleBinding4: RoleBinding4, - RoleBinding5: RoleBinding5, - RoleBinding6: RoleBinding6, - RoleBinding7: RoleBinding7, - RoleBindings: RoleBindings, - RoleBindingHacker3: RoleBindingHacker3, + TeamHackerRB: TeamHackerRB, + NoTeamHackerRB: NoTeamHackerRB, + VolunteerRB: VolunteerRB, + StaffRB: StaffRB, + SponsorT1RB: SponsorT1RB, + SponsorT2RB: SponsorT2RB, + SponsorT3RB: SponsorT3RB, + SponsorT4RB: SponsorT4RB, + SponsorT5RB: SponsorT5RB, + + newHackerRB: newHackerRB, + newVolunteerRB: newVolunteerRB, + newSponsorT1RB: newSponsorT1RB, + newSponsorT2RB: newSponsorT2RB, + newSponsorT3RB: newSponsorT3RB, + newSponsorT4RB: newSponsorT4RB, + newSponsorT5RB: newSponsorT5RB, + + extraAccounts: extraAccounts, + storeAll: storeAll, dropAll: dropAll, }; \ No newline at end of file diff --git a/tests/util/sponsor.test.util.js b/tests/util/sponsor.test.util.js index 0301d1e0..8eb7ec1c 100644 --- a/tests/util/sponsor.test.util.js +++ b/tests/util/sponsor.test.util.js @@ -7,36 +7,38 @@ const Sponsor = require("../../models/sponsor.model"); const mongoose = require("mongoose"); const logger = require("../../services/logger.service"); -const newSponsor1 = { +const T1Sponsor0 = { + "_id": mongoose.Types.ObjectId(), + "accountId": Util.Account.sponsorT1Accounts.stored[0]._id, + "tier": 1, + "company": "Best company NA", + "contractURL": "https://linkto.con", + "nominees": [Util.Hacker.TeamHacker0._id], +}; + +const newT2Sponsor0 = { // no _id as that will be generated - "accountId": Util.Account.Account5._id, - "tier": 5, + "accountId": Util.Account.sponsorT2Accounts.new[0]._id, + "tier": 2, "company": "Best company EU", "contractURL": "https://linktocontract2.con", - "nominees": [Util.Hacker.HackerB._id] + "nominees": [Util.Hacker.NoTeamHacker0._id] }; + const duplicateAccountLinkSponsor1 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account3._id, + "accountId": Util.Account.sponsorT1Accounts.stored[0]._id, "tier": 3, "company": "Best company NA1", "contractURL": "https://linkto1.con", - "nominees": [Util.Hacker.HackerA._id], + "nominees": [Util.Hacker.TeamHacker0._id], }; -const Sponsor1 = { - "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account3._id, - "tier": 3, - "company": "Best company NA", - "contractURL": "https://linkto.con", - "nominees": [Util.Hacker.HackerA._id], -}; const Sponsors = [ - Sponsor1, + T1Sponsor0, ]; -function storeAll(attributes) { +function store(attributes) { const sponsorDocs = []; const sponsorComps = []; attributes.forEach((attribute) => { @@ -47,6 +49,10 @@ function storeAll(attributes) { return Sponsor.collection.insertMany(sponsorDocs); } +async function storeAll() { + await store(Sponsors); +} + async function dropAll() { try { await Sponsor.collection.drop(); @@ -60,9 +66,11 @@ async function dropAll() { } module.exports = { - newSponsor1: newSponsor1, + T1Sponsor0: T1Sponsor0, + newT2Sponsor0: newT2Sponsor0, + duplicateAccountLinkSponsor1: duplicateAccountLinkSponsor1, - Sponsor1: Sponsor1, + Sponsors: Sponsors, storeAll: storeAll, dropAll: dropAll, diff --git a/tests/util/staff.test.util.js b/tests/util/staff.test.util.js index 4edf3d87..e43da75e 100644 --- a/tests/util/staff.test.util.js +++ b/tests/util/staff.test.util.js @@ -6,15 +6,15 @@ const Staff = require("../../models/staff.model"); const mongoose = require("mongoose"); const logger = require("../../services/logger.service"); -const Staff1 = { +const Staff0 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account4._id + "accountId": Util.Account.staffAccounts.stored[0], }; const Staffs = [ - Staff1, + Staff0, ]; -function storeAll(attributes) { +function store(attributes) { const staffDocs = []; const staffIds = []; attributes.forEach((attribute) => { @@ -25,6 +25,10 @@ function storeAll(attributes) { return Staff.collection.insertMany(staffDocs); } +async function storeAll() { + await store(Staffs); +} + async function dropAll() { try { await Staff.collection.drop(); @@ -38,7 +42,7 @@ async function dropAll() { } module.exports = { - Staff1: Staff1, + Staff0: Staff0, Staffs: Staffs, storeAll: storeAll, dropAll: dropAll, diff --git a/tests/util/team.test.util.js b/tests/util/team.test.util.js index 45b98b86..e04f6f34 100644 --- a/tests/util/team.test.util.js +++ b/tests/util/team.test.util.js @@ -22,14 +22,14 @@ const newTeam1 = { const createdNewTeam1 = { "name": "BronzeTeam1", - "members": [Util.Hacker.HackerB._id], + "members": [Util.Hacker.NoTeamHacker0._id], "projectName": "YetAnotherProject" }; const Team1 = { "_id": Constants.MongoId.team1Id, "name": "BronzeTeam", - "members": [Util.Hacker.HackerA._id], + "members": [Util.Hacker.TeamHacker0._id], "devpostURL": "justanother.devpost.com", "projectName": "YetAnotherProject" }; @@ -37,7 +37,7 @@ const Team1 = { const Team2 = { "_id": Constants.MongoId.team2Id, "name": "SilverTeam", - "members": [Util.Hacker.HackerC._id], + "members": [Util.Hacker.waitlistedHacker0._id], "devpostURL": "watwatwat.devpost.com", "projectName": "WatWatWat", }; @@ -45,7 +45,7 @@ const Team2 = { const Team3 = { "_id": Constants.MongoId.team3Id, "name": "FullTeam", - "members": [Util.Hacker.HackerD._id, Util.Hacker.HackerE._id, Util.Hacker.HackerF._id, Util.Hacker.HackerG._id] + "members": [Util.Hacker.TeamHacker1._id, Util.Hacker.TeamHacker2._id, Util.Hacker.TeamHacker3._id, Util.Hacker.TeamHacker4._id] }; const Teams = [ @@ -54,7 +54,7 @@ const Teams = [ Team3 ]; -function storeAll(attributes) { +function store(attributes) { const teamDocs = []; const names = []; attributes.forEach((attribute) => { @@ -65,6 +65,10 @@ function storeAll(attributes) { return Team.collection.insertMany(teamDocs); } +async function storeAll() { + await store(Teams); +} + async function dropAll() { try { await Team.collection.drop(); diff --git a/tests/util/volunteer.test.util.js b/tests/util/volunteer.test.util.js index f5fb48f4..c104f905 100644 --- a/tests/util/volunteer.test.util.js +++ b/tests/util/volunteer.test.util.js @@ -6,27 +6,29 @@ const mongoose = require("mongoose"); const Volunteer = require("../../models/volunteer.model"); const logger = require("../../services/logger.service"); -const newVolunteer1 = { - "accountId": Util.Account.generatedAccounts[15]._id +const newVolunteer0 = { + "accountId": Util.Account.volunteerAccounts.new[0]._id }; const duplicateVolunteer1 = { - "accountId": Util.Account.Account4._id + "accountId": Util.Account.volunteerAccounts.stored[0]._id }; -const adminVolunteer1 = { - "accountId": Util.Account.Admin1._id +const Volunteer0 = { + "_id": mongoose.Types.ObjectId(), + "accountId": Util.Account.volunteerAccounts.stored[0]._id }; -const Volunteer1 = { +const invalidVolunteer0 = { "_id": mongoose.Types.ObjectId(), - "accountId": Util.Account.Account4._id + "accountId": Util.Account.staffAccounts.stored[0]._id }; + const Volunteers = [ - Volunteer1, + Volunteer0, ]; -function storeAll(attributes) { +function store(attributes) { const volunteerDocs = []; const volunteerIds = []; attributes.forEach((attribute) => { @@ -37,6 +39,10 @@ function storeAll(attributes) { return Volunteer.collection.insertMany(volunteerDocs); } +async function storeAll() { + await store(Volunteers); +} + async function dropAll() { try { await Volunteer.collection.drop(); @@ -51,9 +57,10 @@ async function dropAll() { module.exports = { duplicateVolunteer1: duplicateVolunteer1, - adminVolunteer1: adminVolunteer1, - newVolunteer1: newVolunteer1, - Volunteer1: Volunteer1, + newVolunteer0: newVolunteer0, + Volunteer0: Volunteer0, + invalidVolunteer0: invalidVolunteer0, + Volunteers: Volunteers, storeAll: storeAll, dropAll: dropAll, diff --git a/tests/volunteer.test.js b/tests/volunteer.test.js index dcb2a39f..bde8924c 100644 --- a/tests/volunteer.test.js +++ b/tests/volunteer.test.js @@ -17,19 +17,24 @@ const util = { auth: require("./util/auth.test.util") }; -const Admin1 = util.account.Admin1; -const adminVolunteer = util.volunteer.adminVolunteer1; +const Admin0 = util.account.staffAccounts.stored[0]; +const HackerAccount0 = util.account.hackerAccounts.stored.team[0]; -const newVolunteerAccount = util.account.generatedAccounts[15]; -const newVolunteer = util.volunteer.newVolunteer1; +const VolunteerAccount0 = util.account.volunteerAccounts.stored[0]; +const Volunteer0 = util.volunteer.Volunteer0; + +const newVolunteerAccount0 = util.account.volunteerAccounts.new[0]; +const newVolunteer0 = util.volunteer.newVolunteer0; const duplicateVolunteer = util.volunteer.duplicateVolunteer1; -const hackerAccount = util.account.Account1; +const invalidVolunteer0 = util.volunteer.invalidVolunteer0; + + describe("GET volunteer", function () { it("should FAIL to get volunteer due to lack of authentication", function (done) { chai.request(server.app) - .get(`/api/volunteer/${util.volunteer.Volunteer1._id}`) + .get(`/api/volunteer/${Volunteer0._id}`) .end(function (err, res) { res.should.have.status(401); res.should.be.json; @@ -42,13 +47,13 @@ describe("GET volunteer", function () { }); it("should Fail to GET volunteer due inappropriate authorization", function (done) { - util.auth.login(agent, util.account.Hacker5, (error) => { + util.auth.login(agent, HackerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/volunteer/${util.volunteer.Volunteer1._id}`) + .get(`/api/volunteer/${util.volunteer.Volunteer0._id}`) .end(function (err, res) { res.should.have.status(403); res.should.be.json; @@ -62,13 +67,13 @@ describe("GET volunteer", function () { }); it("should Fail to GET volunteer due to non existant volunteer id", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/volunteer/${util.account.Admin1._id}`) + .get(`/api/volunteer/${Admin0._id}`) .end(function (err, res) { res.should.have.status(404); res.should.be.json; @@ -83,13 +88,13 @@ describe("GET volunteer", function () { // success case it("should GET volunteer info by id with admin credentials", function (done) { - util.auth.login(agent, util.account.Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/volunteer/${util.volunteer.Volunteer1._id}`) + .get(`/api/volunteer/${Volunteer0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -100,7 +105,7 @@ describe("GET volunteer", function () { res.body.message.should.equal(Constants.Success.VOLUNTEER_GET_BY_ID); res.body.should.have.property("data"); - let volunteer = new Volunteer(util.volunteer.Volunteer1); + let volunteer = new Volunteer(util.volunteer.Volunteer0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(volunteer.toJSON())); done(); }); @@ -109,13 +114,13 @@ describe("GET volunteer", function () { // success case it("should GET the user's volunteer info by id", function (done) { - util.auth.login(agent, util.account.Account4, (error) => { + util.auth.login(agent, VolunteerAccount0, (error) => { if (error) { agent.close(); return done(error); } return agent - .get(`/api/volunteer/${util.volunteer.Volunteer1._id}`) + .get(`/api/volunteer/${util.volunteer.Volunteer0._id}`) .end(function (err, res) { if (err) { return done(err); @@ -126,7 +131,7 @@ describe("GET volunteer", function () { res.body.message.should.equal(Constants.Success.VOLUNTEER_GET_BY_ID); res.body.should.have.property("data"); - let volunteer = new Volunteer(util.volunteer.Volunteer1); + let volunteer = new Volunteer(util.volunteer.Volunteer0); chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(volunteer.toJSON())); done(); }); @@ -140,7 +145,7 @@ describe("POST create volunteer", function () { chai.request(server.app) .post(`/api/volunteer`) .type("application/json") - .send(util.volunteer.newVolunteer1) + .send(util.volunteer.newVolunteer0) .end(function (err, res) { res.should.have.status(401); res.should.be.json; @@ -153,7 +158,7 @@ describe("POST create volunteer", function () { // fail on admin case it("fail to create a volunteer when the logged in account is not a volunteer /api/volunteer POST", function (done) { - util.auth.login(agent, Admin1, (error) => { + util.auth.login(agent, Admin0, (error) => { if (error) { agent.close(); return done(error); @@ -161,7 +166,7 @@ describe("POST create volunteer", function () { return agent .post(`/api/volunteer`) .type("application/json") - .send(adminVolunteer) + .send(invalidVolunteer0) .end(function (err, res) { res.should.have.status(409); res.should.be.json; @@ -176,7 +181,7 @@ describe("POST create volunteer", function () { // succeed on user case it("should create a volunteer for the user /api/volunteer POST", function (done) { - util.auth.login(agent, newVolunteerAccount, (error) => { + util.auth.login(agent, newVolunteerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -184,7 +189,7 @@ describe("POST create volunteer", function () { return agent .post(`/api/volunteer`) .type("application/json") - .send(newVolunteer) + .send(newVolunteer0) .end(function (err, res) { res.should.have.status(200); res.should.be.json; @@ -194,7 +199,7 @@ describe("POST create volunteer", function () { // deleting id because that was generated, and not part of original data delete res.body.data.id; - chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(util.volunteer.newVolunteer1)); + chai.assert.equal(JSON.stringify(res.body.data), JSON.stringify(newVolunteer0)); done(); }); }); @@ -202,7 +207,7 @@ describe("POST create volunteer", function () { // fail due to duplicate accountId it("should create a volunteer for the user /api/volunteer POST", function (done) { - util.auth.login(agent, newVolunteerAccount, (error) => { + util.auth.login(agent, newVolunteerAccount0, (error) => { if (error) { agent.close(); return done(error); @@ -225,7 +230,7 @@ describe("POST create volunteer", function () { // fail on non-volunteer type account trying to create volunteer it("should fail to create a volunteer due to authorization /api/volunteer POST", function (done) { - util.auth.login(agent, hackerAccount, (error) => { + util.auth.login(agent, HackerAccount0, (error) => { if (error) { agent.close(); return done(error);